/test/lib/automaton/aux_automaton_01.e

http://github.com/tybor/Liberty · Specman e · 133 lines · 112 code · 19 blank · 2 comment · 3 complexity · f139721673994611cd572e2f8ffcb2ac MD5 · raw file

  1. class AUX_AUTOMATON_01
  2. create {TEST_AUTOMATON_01}
  3. make
  4. feature {TEST_AUTOMATON_01}
  5. run
  6. do
  7. automaton.run(once "start", context);
  8. end
  9. is_number: BOOLEAN
  10. do
  11. Result := context.success
  12. end
  13. found_number: FIXED_STRING
  14. require
  15. is_number
  16. do
  17. Result := context.output
  18. end
  19. feature {}
  20. make (a_input: ABSTRACT_STRING)
  21. require
  22. a_input /= Void
  23. do
  24. create context.make(a_input)
  25. end
  26. context: AUX_AUTOMATON_01_CONTEXT
  27. feature {} -- automaton implementation
  28. -- Note: since the automaton is a once function, no agent may rely on Current.
  29. -- That's why the automaton allows us to pass some context around.
  30. read_next (ctx: like context)
  31. do
  32. ctx.next
  33. debug("test/aux_automaton_01")
  34. if ctx.is_off then
  35. std_output.put_line("No more characters")
  36. else
  37. std_output.put_line("Read character '" + ctx.current_character.out + "'")
  38. end
  39. end
  40. end
  41. on_read_figure (ctx: like context): BOOLEAN
  42. do
  43. if not ctx.is_off then
  44. Result := ctx.current_character.is_digit
  45. end
  46. end
  47. on_read_dot (ctx: like context): BOOLEAN
  48. do
  49. if not ctx.is_off then
  50. Result := ctx.current_character = '.'
  51. end
  52. end
  53. on_end_of_text (ctx: like context): BOOLEAN
  54. do
  55. Result := ctx.is_off
  56. end
  57. otherwise: BOOLEAN
  58. do
  59. Result := True
  60. end
  61. transition (next: STRING; ctx: like context): STRING
  62. do
  63. ctx.extend
  64. Result := next
  65. end
  66. transition_end (next: STRING; ctx: like context): STRING
  67. do
  68. inspect next
  69. when "success" then
  70. ctx.set_success(True)
  71. when "error" then
  72. ctx.set_success(False)
  73. end
  74. Result := next
  75. end
  76. finished: STRING
  77. do
  78. check Result = Void end
  79. end
  80. automaton: AUTOMATON[AUX_AUTOMATON_01_CONTEXT]
  81. once
  82. Result := {AUTOMATON[AUX_AUTOMATON_01_CONTEXT] <<
  83. "start", {STATE[AUX_AUTOMATON_01_CONTEXT] <<
  84. agent on_read_figure(?), agent transition("integral", ?);
  85. agent on_read_dot(?), agent transition("after_dot", ?);
  86. agent otherwise, agent transition_end("error", ?)
  87. >>};
  88. "integral", {STATE[AUX_AUTOMATON_01_CONTEXT] <<
  89. agent on_read_figure(?), agent transition("integral", ?);
  90. agent on_read_dot(?), agent transition("after_dot", ?);
  91. agent on_end_of_text(?), agent transition_end("success", ?);
  92. agent otherwise, agent transition_end("error", ?)
  93. >>};
  94. "after_dot", {STATE[AUX_AUTOMATON_01_CONTEXT] <<
  95. agent on_read_figure(?), agent transition("fractional", ?);
  96. agent otherwise, agent transition_end("error", ?)
  97. >>};
  98. "fractional", {STATE[AUX_AUTOMATON_01_CONTEXT] <<
  99. agent on_read_figure(?), agent transition("fractional", ?);
  100. agent on_end_of_text(?), agent transition_end("success", ?);
  101. agent otherwise, agent transition_end("error", ?)
  102. >>};
  103. "success", {STATE[AUX_AUTOMATON_01_CONTEXT] <<
  104. agent otherwise, agent finished
  105. >>};
  106. "error", {STATE[AUX_AUTOMATON_01_CONTEXT] <<
  107. agent otherwise, agent finished
  108. >>}
  109. >>}
  110. Result.set_before_guards(agent read_next(?))
  111. end
  112. invariant
  113. context /= Void
  114. end