Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LightSwitch
- # Compact example without annotation; annotated version follows.
- ENUMERATE
- FLIP
- METHODS
- method is_on->Logical
- return (state == IS_ON)
- method flip->Logical
- input( FLIP )
- return is_on
- method to->String
- println "The light switch is $." (select{is_on:"on"||"off"})
- STATES
- define IS_OFF
- onEvent FLIP --> IS_ON
- define IS_ON
- onEvent FLIP --> IS_OFF
- endClass
- class LightSwitch
- # Annotated version of the above.
- ENUMERATE
- # Events are Int32's; usually enumerated.
- FLIP
- METHODS
- method is_on->Logical
- # 'state' is an Int32 that is implicitly created by having a STATES section.
- # Each defined state in STATES automatically becomes an ENUMERATE'd value.
- return (state == IS_ON)
- method flip->Logical
- input( FLIP )
- # If 'input(Int32)' is not defined then 'input(event:Int32): on_event(event)' is automatically created.
- return is_on
- method to->String
- println "The light switch is $." (select{is_on:"on"||"off"})
- STATES
- define IS_OFF
- # The first state to be defined is the start state.
- onEvent FLIP
- --> IS_ON
- # '--> X' is convenience syntax for 'change(X)'.
- # 'A --> X' is convenience syntax for 'A.change(X)'.
- # If no 'change' method is defined then 'change(new_state:Int32): set_state(new_state)' is automatically created.
- define IS_ON
- onEvent FLIP --> IS_OFF # equivalent to onEvent FLIP: --> IS_OFF
- endClass
- # Usage
- local sw = LightSwitch()
- println sw
- sw.flip
- println sw
- sw.flip
- println sw
- class LightSwitch
- # A verbose variation which shows onEnter, onExit, and transitional commands.
- ENUMERATE
- FLIP
- METHODS
- method is_on->Logical
- return (state == IS_ON)
- method flip->Logical
- input( FLIP )
- return is_on
- method to->String
- println "The light switch is $." (select{is_on:"on"||"off"})
- STATES
- define IS_OFF
- onEnter
- println "The light switch is now off."
- onEvent FLIP
- println "Flipping the light switch on."
- --> IS_ON
- onExit
- println "The light switch is no longer off."
- define IS_ON
- onEnter
- println "The light switch is now on."
- onEvent FLIP
- println "Flipping the light switch off."
- --> IS_OFF
- onExit
- println "The light switch is no longer on."
- endClass
- class LightSwitch
- # Expanded code which shows how state syntax is transformed into core syntax.
- ENUMERATE
- FLIP
- IS_OFF # auto-gen
- IS_ON # auto-gen
- PROPERTIES
- state=set_initial_state : Int32 # auto-generated
- METHODS
- method change( new_state:Int32 )
- # '-->' operator maps to this auto-generated change() method.
- set_state( new_state )
- method input( event:Int32 )
- # In this case input(Int32) is auto-generated. In other cases we may
- # want to define our own input() e.g. input(Real64) and convert numerical
- # data into an enumerated integer to pass to on_event(Int32).
- on_event( event )
- method is_on->Logical
- return (state == IS_ON)
- method flip->Logical
- input( FLIP )
- return is_on
- method on_enter
- # Auto-gen.
- which (state)
- case IS_OFF
- println "The light switch is now off."
- case IS_ON
- println "The light switch is now on."
- endWhich
- method on_event( event:Int32 )
- # Auto-generated.
- which (state)
- case IS_OFF
- which (event)
- case FLIP
- println "Flipping the light switch on."
- change( IS_ON )
- endWhich
- case IS_ON
- which (event)
- case FLIP
- println "Flipping the light switch off."
- change( IS_OFF )
- endWhich
- endWhich
- method on_exit( previous_state:Int32 )
- # Auto-gen.
- which (previous_state)
- case IS_OFF
- println "The light switch is no longer off."
- case IS_ON
- println "The light switch is no longer on."
- endWhich
- method set_initial_state->Int32
- # Auto-gen.
- @state = IS_OFF
- on_enter( state )
- return state
- method set_state( new_state:Int32 )
- # Auto-generated method
- if (new_state == state) return
- local previous_state = state
- @state = new_state
- on_exit( previous_state )
- on_enter
- method to->String
- println "The light switch is $." (select{is_on:"on"||"off"})
- endClass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement