PageRenderTime 4ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/tutorial/agent/example7.e

http://github.com/tybor/Liberty
Specman e | 82 lines | 51 code | 9 blank | 22 comment | 0 complexity | 501f486dd2491f378bcc4b32fa2893c2 MD5 | raw file
 1class EXAMPLE7
 2   -- This example demonstrates creating and calling agents.
 3
 4create {}
 5   make
 6
 7feature {}
 8   f1: FUNCTION[TUPLE, INTEGER]
 9      -- f1 is a function that takes no argument and that returns
10      -- an INTEGER
11
12   f2: FUNCTION[TUPLE[STRING], STRING]
13      -- f2 is a function that takes a STRING argument and returns
14      -- a STRING
15
16   p1: PROCEDURE[TUPLE]
17      -- p1 is a procedure that takes no argument
18
19   p2: PROCEDURE[TUPLE[STRING, INTEGER]]
20      -- p2 is a procedure that takes a STRING and an INTEGER
21      -- argument
22
23feature {}
24   make
25      local
26         random: PRESS_RANDOM_NUMBER_GENERATOR; s: STRING
27      do
28         -- Creating and calling agents with no open arguments.  Any
29         -- argument that is needed by the agent is supplied when
30         -- creating the agent.  No argument needs to be supplied
31         -- when calling the agent, hence the empty tuple [].
32         create random.with_seed(25)
33         f1 := agent random.last_integer(100)
34         p1 := agent random.next
35         random := Void
36         io.put_string("Calling f1 ")
37         io.put_integer(f1.item([]))
38         io.put_new_line
39         io.put_string("Calling p1")
40         p1.call([])
41         io.put_new_line
42         io.put_string("Calling f1 ")
43         io.put_integer(f1.item([]))
44         io.put_new_line
45         -- Creating and calling an agent with an open argument.  The
46         -- open arguments are replaced with question marks when
47         -- creating the agent.  When calling the agent, the open
48         -- arguments must be supplied in the tuple.
49
50         f2 := agent "Agents " + ?
51         io.put_string("Calling f2(%"are easy%") ")
52         io.put_string(f2.item(["are easy"]))
53         io.put_new_line
54         -- The target of an agent can also be open.  To this effect,
55         -- just replace the target of the call with a type between
56         -- curly braces.  When calling the agent, the open target
57         -- treated exactly like an open argument.
58
59         f2 := agent {STRING}.as_upper
60         io.put_string("Calling f2(%"abcdexxxyz%") ")
61         io.put_string(f2.item(["abcdexxxyz"]))
62         io.put_new_line
63         -- Procedure agents can also have open arguments
64         p2 := agent {STRING}.remove(?)
65         s := "SmartXEiffel"
66         io.put_string("value of s ")
67         io.put_string(s)
68         io.put_new_line
69         io.put_string("Calling p2(s, 6) ")
70         p2.call([s, {INTEGER_32 6}])
71         io.put_new_line
72         io.put_string("value of s ")
73         io.put_string(s)
74         io.put_new_line
75         -- Discarding extra arguments is allowed
76         p2 := agent ("Test").print_on(std_output)
77         io.put_string("Calling p2(%"Abracadabra%", 32) ")
78         p2.call(["Abracadabra", {INTEGER_32 32}])
79         io.put_new_line
80      end
81
82end -- class EXAMPLE7