/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

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