Class: Pushdown::State
- Inherits:
-
Object
- Object
- Pushdown::State
- Extended by:
- Loggability
- Defined in:
- lib/pushdown/state.rb
Overview
A componented state object in a Pushdown automaton
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The state data object that was used to create the State (if any).
-
#transitions ⇒ Object
readonly
Allow introspection on declared transitions.
Class Method Summary collapse
-
.inherited(subclass) ⇒ Object
Inheritance callback – allow subclasses to be instantiated, and add some class-instance data to them.
-
.register_transition(type) ⇒ Object
Register a transition
typedeclaration method. -
.type_name ⇒ Object
Return the transition’s type as a lowercase Symbol, such as that specified in transition declarations.
Instance Method Summary collapse
-
#description ⇒ Object
Return a description of the State as an engine phrase.
-
#initialize(data = nil) ⇒ State
constructor
Set up new States with an optional
dataobject. -
#on_event(event, *args) ⇒ Object
Event callback – called by the automaton when its #on_<stackname>_event method is called.
-
#on_pause ⇒ Object
Stack callback – called when another state is pushed over this one.
-
#on_resume ⇒ Object
Stack callback – called when another state is popped off from in front of this one, making it the current state.
-
#on_start ⇒ Object
Stack callback – called when the state is added to the stack.
-
#on_stop ⇒ Object
Stack callback – called when the state is removed from the stack.
-
#shadow_update(*data) ⇒ Object
State callback – interval callback called when the state is on the stack, even when the state is not the current one.
-
#transition(transition_name, automaton, stack_name) ⇒ Object
Create a new instance of Pushdown::Transition named
transition_namethat has been declared using one of the Transition Declaration methods. -
#type_name ⇒ Object
Return the transition’s type as a lowercase Symbol, such as that specified in transition declarations.
-
#update(*data) ⇒ Object
State callback – interval callback called when the state is the current one.
Constructor Details
#initialize(data = nil) ⇒ State
Set up new States with an optional data object.
61 62 63 |
# File 'lib/pushdown/state.rb', line 61 def initialize( data=nil ) @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
The state data object that was used to create the State (if any)
72 73 74 |
# File 'lib/pushdown/state.rb', line 72 def data @data end |
#transitions ⇒ Object (readonly)
Allow introspection on declared transitions
22 23 24 |
# File 'lib/pushdown/state.rb', line 22 def transitions @transitions end |
Class Method Details
.inherited(subclass) ⇒ Object
Inheritance callback – allow subclasses to be instantiated, and add some class-instance data to them.
27 28 29 30 31 32 |
# File 'lib/pushdown/state.rb', line 27 def self::inherited( subclass ) super subclass.public_class_method( :new ) subclass.instance_variable_set( :@transitions, {} ) end |
.register_transition(type) ⇒ Object
Register a transition type declaration method.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/pushdown/state.rb', line 40 def self::register_transition( type ) type = type.to_sym meth = lambda do |transition_name, *args| self.transitions[ transition_name ] = [ type, *args ] end method_name = "transition_%s" % [ type ] self.log.info "Setting up transition declaration method %p" % [ method_name ] define_singleton_method( method_name, &meth ) end |
.type_name ⇒ Object
Return the transition’s type as a lowercase Symbol, such as that specified in transition declarations.
54 55 56 57 |
# File 'lib/pushdown/state.rb', line 54 def self::type_name class_name = self.name or return :anonymous return class_name.sub( /.*::/, '' ).downcase.to_sym end |
Instance Method Details
#description ⇒ Object
Return a description of the State as an engine phrase.
145 146 147 148 149 150 |
# File 'lib/pushdown/state.rb', line 145 def description return "%#x" % [ self.class.object_id ] unless self.class.name return self.class.name.sub( /.*::/, '' ). gsub( /([A-Z])([A-Z])/ ) { "#$1 #$2" }. gsub( /([a-z])([A-Z])/ ) { "#$1 #$2" }.downcase end |
#on_event(event, *args) ⇒ Object
Event callback – called by the automaton when its #on_<stackname>_event method is called. This method can return a Transition or a Symbol which maps to one.
110 111 112 |
# File 'lib/pushdown/state.rb', line 110 def on_event( event, *args ) return nil # no-op end |
#on_pause ⇒ Object
Stack callback – called when another state is pushed over this one.
92 93 94 |
# File 'lib/pushdown/state.rb', line 92 def on_pause return nil # no-op end |
#on_resume ⇒ Object
Stack callback – called when another state is popped off from in front of this one, making it the current state.
99 100 101 |
# File 'lib/pushdown/state.rb', line 99 def on_resume return nil # no-op end |
#on_start ⇒ Object
Stack callback – called when the state is added to the stack.
80 81 82 |
# File 'lib/pushdown/state.rb', line 80 def on_start return nil # no-op end |
#on_stop ⇒ Object
Stack callback – called when the state is removed from the stack.
86 87 88 |
# File 'lib/pushdown/state.rb', line 86 def on_stop return nil # no-op end |
#shadow_update(*data) ⇒ Object
State callback – interval callback called when the state is on the stack, even when the state is not the current one.
128 129 130 |
# File 'lib/pushdown/state.rb', line 128 def shadow_update( *data ) return nil # no-op end |
#transition(transition_name, automaton, stack_name) ⇒ Object
Create a new instance of Pushdown::Transition named transition_name that has been declared using one of the Transition Declaration methods.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/pushdown/state.rb', line 155 def transition( transition_name, automaton, stack_name ) self.log.debug "Looking up the %p transition for %p via %p" % [ transition_name, self, automaton ] transition_type, state_class_name = self.class.transitions[ transition_name ] raise "no such transition %p for %p" % [ transition_name, self.class ] unless transition_type if state_class_name state_class = automaton.class.pushdown_state_class( stack_name, state_class_name ) state_data = self.data return Pushdown::Transition. create( transition_type, transition_name, state_class, state_data ) else return Pushdown::Transition.create( transition_type, transition_name ) end end |
#type_name ⇒ Object
Return the transition’s type as a lowercase Symbol, such as that specified in transition declarations.
139 140 141 |
# File 'lib/pushdown/state.rb', line 139 def type_name return self.class.type_name end |
#update(*data) ⇒ Object
State callback – interval callback called when the state is the current one. This method can return a Transition or a Symbol which maps to one.
121 122 123 |
# File 'lib/pushdown/state.rb', line 121 def update( *data ) return nil # no-op end |