Class Statemachine::Statemachine
In: lib/statemachine/generate/dot_graph/dot_graph_statemachine.rb
lib/statemachine/generate/java/java_statemachine.rb
lib/statemachine/statemachine.rb
Parent: Object

Used at runtime to execute the behavior of the statemachine. Should be created by using the Statemachine.build method.

  sm = Statemachine.build do
    trans :locked, :coin, :unlocked
    trans :unlocked, :pass, :locked:
  end

  sm.coin
  sm.state

This class will accept any method that corresponds to an event. If the current state respons to the event, the appropriate transtion will be invoked. Otherwise an exception will be raised.

Methods

new   process_event   reset   respond_to?   startstate   state   state=   to_dot   to_java  

Included Modules

ActionInvokation

Attributes

context  [RW]  Provides access to the context of the statemachine. The context is a object where all actions will be invoked. This provides a way to separate logic from behavior. The statemachine is responsible for all the logic and the context is responsible for all the behavior.
states  [R] 
states  [R] 
tracer  [RW]  The tracer is an IO object. The statemachine will write run time execution information to the tracer. Can be helpful in debugging. Defaults to nil.

Public Class methods

Should not be called directly. Instances of Statemachine::Statemachine are created through the Statemachine.build method.

[Source]

    # File lib/statemachine/statemachine.rb, line 40
40:     def initialize(root = Superstate.new(:root, nil, self))
41:       @root = root
42:       @states = {}
43:     end

Public Instance methods

The key method to exercise the statemachine. Any extra arguments supplied will be passed into any actions associated with the transition.

Alternatively to this method, you may invoke methods, names the same as the event, on the statemachine. The advantage of using process_event is that errors messages are more informative.

[Source]

    # File lib/statemachine/statemachine.rb, line 84
84:     def process_event(event, *args)
85:       event = event.to_sym
86:       trace "Event: #{event}"
87:       if @state
88:         transition = @state.transition_for(event)
89:         if transition
90:           transition.invoke(@state, self, args)
91:         else
92:           raise TransitionMissingException.new("#{@state} does not respond to the '#{event}' event.")
93:         end
94:       else
95:         raise StatemachineException.new("The state machine isn't in any state while processing the '#{event}' event.")
96:       end
97:     end

Resets the statemachine back to its starting state.

[Source]

    # File lib/statemachine/statemachine.rb, line 51
51:     def reset
52:       @state = get_state(@root.startstate_id)
53:       while @state and not @state.concrete?
54:         @state = get_state(@state.startstate_id)
55:       end
56:       raise StatemachineException.new("The state machine doesn't know where to start. Try setting the startstate.") if @state == nil
57:       @state.enter
58: 
59:       @states.values.each { |state| state.reset }
60:     end

[Source]

     # File lib/statemachine/statemachine.rb, line 130
130:     def respond_to?(message)
131:       return true if super(message)
132:       return true if @state and @state.transition_for(message)
133:       return false
134:     end

Returns the id of the startstate of the statemachine.

[Source]

    # File lib/statemachine/statemachine.rb, line 46
46:     def startstate
47:       return @root.startstate_id
48:     end

Return the id of the current state of the statemachine.

[Source]

    # File lib/statemachine/statemachine.rb, line 63
63:     def state
64:       return @state.id
65:     end

You may change the state of the statemachine by using this method. The parameter should be the id of the desired state.

[Source]

    # File lib/statemachine/statemachine.rb, line 69
69:     def state= value
70:       if value.is_a? State
71:         @state = value
72:       elsif @states[value]
73:         @state = @states[value]
74:       elsif value and @states[value.to_sym]
75:         @state = @states[value.to_sym]
76:       end
77:     end

[Source]

    # File lib/statemachine/generate/dot_graph/dot_graph_statemachine.rb, line 9
 9:     def to_dot(options = {})
10:       generator = Generate::DotGraph::DotGraphStatemachine.new(self, options)
11:       generator.generate!
12:     end

[Source]

    # File lib/statemachine/generate/java/java_statemachine.rb, line 9
 9:     def to_java(options = {})
10:       generator = Generate::Java::JavaStatemachine.new(self, options)
11:       generator.generate!
12:     end

[Validate]