State

class
Superclass
Object
Extended With
Loggability

A componented state object in a Pushdown automaton

Attributes

data[R]

The state data object that was used to create the State (if any)

Public Class Methods

anchor
inherited( subclass )

Inheritance callback – allow subclasses to be instantiated, and add some class-instance data to them.

# File lib/pushdown/state.rb, line 26
def self::inherited( subclass )
        super

        subclass.public_class_method( :new )
        subclass.instance_variable_set( :@transitions, {} )
end
anchor
new( data=nil )

Set up new States with an optional data object.

# File lib/pushdown/state.rb, line 60
def initialize( data=nil )
        @data = data
end
anchor
register_transition( type )

Register a transition type declaration method.

# File lib/pushdown/state.rb, line 39
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
anchor
type_name()

Return the transition's type as a lowercase Symbol, such as that specified in transition declarations.

# File lib/pushdown/state.rb, line 53
def self::type_name
        class_name = self.name or return :anonymous
        return class_name.sub( /.*::/, '' ).downcase.to_sym
end

Public Instance Methods

anchor
description()

Return a description of the State as an engine phrase.

# File lib/pushdown/state.rb, line 144
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
anchor
on_event( event, *args )

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.

# File lib/pushdown/state.rb, line 109
def on_event( event, *args )
        return nil # no-op
end
anchor
on_pause()

Stack callback – called when another state is pushed over this one.

# File lib/pushdown/state.rb, line 91
def on_pause
        return nil # no-op
end
anchor
on_resume()

Stack callback – called when another state is popped off from in front of this one, making it the current state.

# File lib/pushdown/state.rb, line 98
def on_resume
        return nil # no-op
end
anchor
on_start()

Stack callback – called when the state is added to the stack.

# File lib/pushdown/state.rb, line 79
def on_start
        return nil # no-op
end
anchor
on_stop()

Stack callback – called when the state is removed from the stack.

# File lib/pushdown/state.rb, line 85
def on_stop
        return nil # no-op
end
anchor
shadow_update( *data )

State callback – interval callback called when the state is on the stack, even when the state is not the current one.

# File lib/pushdown/state.rb, line 127
def shadow_update( *data )
        return nil # no-op
end
anchor
transition( transition_name, automaton, stack_name )

Create a new instance of Pushdown::Transition named transition_name that has been declared using one of the Transition Declaration methods.

# File lib/pushdown/state.rb, line 154
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
anchor
type_name()

Return the transition's type as a lowercase Symbol, such as that specified in transition declarations.

# File lib/pushdown/state.rb, line 138
def type_name
        return self.class.type_name
end
anchor
unknown()

Allow introspection on declared transitions

# File lib/pushdown/state.rb, line 21
singleton_class.attr_reader :transitions
anchor
update( *data )

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.

# File lib/pushdown/state.rb, line 120
def update( *data )
        return nil # no-op
end