Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. class LightSwitch
  2. # Compact example without annotation; annotated version follows.
  3. ENUMERATE
  4. FLIP
  5.  
  6. METHODS
  7. method is_on->Logical
  8. return (state == IS_ON)
  9.  
  10. method flip->Logical
  11. input( FLIP )
  12. return is_on
  13.  
  14. method to->String
  15. println "The light switch is $." (select{is_on:"on"||"off"})
  16.  
  17. STATES
  18. define IS_OFF
  19. onEvent FLIP --> IS_ON
  20. define IS_ON
  21. onEvent FLIP --> IS_OFF
  22. endClass
  23.  
  24.  
  25. class LightSwitch
  26. # Annotated version of the above.
  27. ENUMERATE
  28. # Events are Int32's; usually enumerated.
  29. FLIP
  30.  
  31. METHODS
  32. method is_on->Logical
  33. # 'state' is an Int32 that is implicitly created by having a STATES section.
  34. # Each defined state in STATES automatically becomes an ENUMERATE'd value.
  35. return (state == IS_ON)
  36.  
  37. method flip->Logical
  38. input( FLIP )
  39. # If 'input(Int32)' is not defined then 'input(event:Int32): on_event(event)' is automatically created.
  40. return is_on
  41.  
  42. method to->String
  43. println "The light switch is $." (select{is_on:"on"||"off"})
  44.  
  45. STATES
  46. define IS_OFF
  47. # The first state to be defined is the start state.
  48. onEvent FLIP
  49. --> IS_ON
  50. # '--> X' is convenience syntax for 'change(X)'.
  51. # 'A --> X' is convenience syntax for 'A.change(X)'.
  52. # If no 'change' method is defined then 'change(new_state:Int32): set_state(new_state)' is automatically created.
  53.  
  54. define IS_ON
  55. onEvent FLIP --> IS_OFF # equivalent to onEvent FLIP: --> IS_OFF
  56. endClass
  57.  
  58. # Usage
  59. local sw = LightSwitch()
  60. println sw
  61. sw.flip
  62. println sw
  63. sw.flip
  64. println sw
  65.  
  66. class LightSwitch
  67. # A verbose variation which shows onEnter, onExit, and transitional commands.
  68. ENUMERATE
  69. FLIP
  70.  
  71. METHODS
  72. method is_on->Logical
  73. return (state == IS_ON)
  74.  
  75. method flip->Logical
  76. input( FLIP )
  77. return is_on
  78.  
  79. method to->String
  80. println "The light switch is $." (select{is_on:"on"||"off"})
  81.  
  82. STATES
  83. define IS_OFF
  84. onEnter
  85. println "The light switch is now off."
  86. onEvent FLIP
  87. println "Flipping the light switch on."
  88. --> IS_ON
  89. onExit
  90. println "The light switch is no longer off."
  91.  
  92. define IS_ON
  93. onEnter
  94. println "The light switch is now on."
  95. onEvent FLIP
  96. println "Flipping the light switch off."
  97. --> IS_OFF
  98. onExit
  99. println "The light switch is no longer on."
  100. endClass
  101.  
  102. class LightSwitch
  103. # Expanded code which shows how state syntax is transformed into core syntax.
  104. ENUMERATE
  105. FLIP
  106. IS_OFF # auto-gen
  107. IS_ON # auto-gen
  108.  
  109. PROPERTIES
  110. state=set_initial_state : Int32 # auto-generated
  111.  
  112. METHODS
  113. method change( new_state:Int32 )
  114. # '-->' operator maps to this auto-generated change() method.
  115. set_state( new_state )
  116.  
  117. method input( event:Int32 )
  118. # In this case input(Int32) is auto-generated. In other cases we may
  119. # want to define our own input() e.g. input(Real64) and convert numerical
  120. # data into an enumerated integer to pass to on_event(Int32).
  121. on_event( event )
  122.  
  123. method is_on->Logical
  124. return (state == IS_ON)
  125.  
  126. method flip->Logical
  127. input( FLIP )
  128. return is_on
  129.  
  130. method on_enter
  131. # Auto-gen.
  132. which (state)
  133. case IS_OFF
  134. println "The light switch is now off."
  135. case IS_ON
  136. println "The light switch is now on."
  137. endWhich
  138.  
  139. method on_event( event:Int32 )
  140. # Auto-generated.
  141. which (state)
  142. case IS_OFF
  143. which (event)
  144. case FLIP
  145. println "Flipping the light switch on."
  146. change( IS_ON )
  147. endWhich
  148. case IS_ON
  149. which (event)
  150. case FLIP
  151. println "Flipping the light switch off."
  152. change( IS_OFF )
  153. endWhich
  154. endWhich
  155.  
  156. method on_exit( previous_state:Int32 )
  157. # Auto-gen.
  158. which (previous_state)
  159. case IS_OFF
  160. println "The light switch is no longer off."
  161. case IS_ON
  162. println "The light switch is no longer on."
  163. endWhich
  164.  
  165. method set_initial_state->Int32
  166. # Auto-gen.
  167. @state = IS_OFF
  168. on_enter( state )
  169. return state
  170.  
  171. method set_state( new_state:Int32 )
  172. # Auto-generated method
  173. if (new_state == state) return
  174. local previous_state = state
  175. @state = new_state
  176. on_exit( previous_state )
  177. on_enter
  178.  
  179. method to->String
  180. println "The light switch is $." (select{is_on:"on"||"off"})
  181.  
  182. endClass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement