Class: FSM::FSM

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/fsm-0.0.0/dsl.rb,
lib/fsm-0.0.0/fsm.rb

Defined Under Namespace

Classes: DSL, TransitionError

Constant Summary collapse

LIBDIR =
File::dirname(File::expand_path(__FILE__)) + File::SEPARATOR
INCDIR =
File::dirname(FSM::LIBDIR) + File::SEPARATOR

Instance Method Summary collapse

Methods included from Util

included

Constructor Details

#initialize(*a, &b) ⇒ FSM

Returns a new instance of FSM.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fsm-0.0.0/fsm.rb', line 26

def initialize *a, &b 
  super

  @graph = DirectedGraph.new
  @state_attributes = Hash.new{|h,k| h[k] = Hash.new}

  @graph.add_node 'start'
  @state_attributes['start'].update 'shape' => 'point'
  @state = 'start' 

  @subscribers = [] 

  states = a.flatten
  states.each{|state| @graph.add_node state}

  @dsl = DSL.new self
  configure &b if b
end

Instance Method Details

#add_observer(o) ⇒ Object


68
69
70
# File 'lib/fsm-0.0.0/fsm.rb', line 68

def add_observer o
  ex{ @subscribers << o }
end

#add_state(*states) ⇒ Object


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fsm-0.0.0/fsm.rb', line 140

def add_state *states
  ex{
    string_list(states).each{|state| 
      @graph.add_node state
      case @graph.num_nodes
        when 2
          add_transition 'start', 'start' => state
          @state_attributes[state].update 'shape' => 'doublecircle'
        else
          @state_attributes[state].update 'shape' => 'circle'
      end
    }
    self
  }
end

#add_transition(*a, &b) ⇒ Object


156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fsm-0.0.0/fsm.rb', line 156

def add_transition *a, &b
  ex{
    hashes, argv = a.partition{|arg| Hash === arg} 
    info = argv.shift || 'default'
    raise ArgumentError, "too many argv in <#{ argv.inspect }>"  unless argv.empty?
    raise ArgumentError, "too few transitions in <#{ hashes.inspect }>"  if hashes.empty?
    transitions = hashes.inject({}){|t,h| t.update h}
    transitions.each do |u,v|
      @graph.add_link *string_list(u,v,info)
      #add_transition_action u, info, &b if b
    end
    self
  }
end

#configure(&b) ⇒ Object


45
46
47
48
49
# File 'lib/fsm-0.0.0/fsm.rb', line 45

def configure &b
  ex{
    @dsl.configure &b
  }
end

#displayObject


199
200
201
202
203
204
205
206
207
208
# File 'lib/fsm-0.0.0/fsm.rb', line 199

def display
  dot_out = plot
  at_exit{ File.unlink dot_out rescue nil}
  Thread.new{
    Thread.current.abort_on_exception = true
    cmd = "#{ DISPLAY_CMD } #{ dot_out } </dev/null >/dev/null 2>&1"
    system cmd or raise "cmd <#{ cmd } failed with <#{ $?.exitstatus }>" 
    File.unlink dot_out rescue nil
  }
end

#input(*data) ⇒ Object


133
134
135
136
137
138
# File 'lib/fsm-0.0.0/fsm.rb', line 133

def input *data
  sh{
    p data
    notify Event::Input, *data
  }
end

#inspectObject


57
58
59
60
61
# File 'lib/fsm-0.0.0/fsm.rb', line 57

def inspect
  sh{
    @graph.links.inspect
  }
end

#notify(type, *data) ⇒ Object


117
118
119
120
121
122
# File 'lib/fsm-0.0.0/fsm.rb', line 117

def notify type, *data
  sh{
    e = type::new @state, *data
    @subscribers.each{|s| e.notify s}
  }
end

#plot(dot_in = nil, dot_out = nil, fmt = 'jpg', &b) ⇒ Object


171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/fsm-0.0.0/fsm.rb', line 171

def plot dot_in = nil, dot_out = nil, fmt = 'jpg', &b
  dot_in ||= 
    (tmp = Tempfile.new("#{ Process.pid }-#{ Time.now.to_i }-#{ rand }")).path 
  if tmp
    tmp.puts to_dot
    tmp.close
  else
    open(dot_in, 'w'){|f| f.puts to_dot}
  end
  dot_out = dot_in + '.' + fmt
  cmd = "#{ DOT_CMD } -T#{ fmt } #{ dot_in } -o #{ dot_out }"
  system cmd or raise "cmd <#{ cmd } failed with <#{ $?.exitstatus }>" 
  dot_out
end

#startObject Also known as: start!


63
64
65
# File 'lib/fsm-0.0.0/fsm.rb', line 63

def start
  transition 'start', 'start'
end

#subscribe(subscriber, event, *events) ⇒ Object


51
52
53
54
55
# File 'lib/fsm-0.0.0/fsm.rb', line 51

def subscribe subscriber, event, *events
  ex{
    @subscriptions << Subscription.new(subscriber, event, *events)
  }
end

#to_dotObject


186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/fsm-0.0.0/fsm.rb', line 186

def to_dot
  sh{
    dot = @graph.to_dot
    @state_attributes.each do |state, attributes|
      dot.set_node_attributes state, attributes 
    end
    class << dot
      def to_s(*a, &b) to_dot_specification(*a, &b) end
    end
    dot
  }
end

#transition(state = nil, edge = nil, &b) ⇒ Object Also known as: transitioning


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fsm-0.0.0/fsm.rb', line 74

def transition state = nil, edge = nil, &b
  validate_state = lambda do |state|
    if state
      raise TransitionError, "state == <#{ @state.inspect }> not <#{ state.inspect }>" unless
        @state == state
    end
  end

  validate_edge = lambda do |edge|
    if edge
      raise TransitionError, "no path <#{ state }> -->> <#{ edge }>" unless
        @graph.links_from(state).map{|link| link.info}.include? edge
    end
  end

  ex{
    validate_state[state]
    state ||= @state

    validate_edge[edge]
    edge ||= 'default'

    notify Event::Exit

    @state = [@state, edge]
    notify Event::Transition

    bcall b, *@state if b

    begin
      @state = @graph.transition *@state
    rescue => e
      m,c,b = e.message, e.class, e.backtrace.join("\n")
      raise TransitionError, "#{ m } (#{ c })\n#{ b }"
    end

    notify Event::Entry

    @state
  }
end

#traverse(*pairs) ⇒ Object

Raises:

  • (ArgumentError)

124
125
126
127
128
129
130
131
# File 'lib/fsm-0.0.0/fsm.rb', line 124

def traverse *pairs
  pairs.to_a.flatten!
  raise ArgumentError, 'odd number of arguments' unless
    pairs.size.modulo(2).zero?
  ex{
    string_list(*pairs).each_slice(2){|state, edge| transition state, edge}
  }
end