lib/fsm/machine.rb in simplificator-fsm-0.2.4 vs lib/fsm/machine.rb in simplificator-fsm-0.3.0
- old
+ new
@@ -17,11 +17,11 @@
(@machines ||= {})[args.first] = args.last
end
def state(name, options = {})
raise "State is already defined: '#{name}'" if self.state_for_name(name, true)
- self.states << State.new(name, options)
+ self.states << State.new(name, @target_class, options)
self.initial_state_name=(name) unless self.initial_state_name
end
def initial_state_name=(value)
raise UnknownState.new("Unknown state '#{value}'. Define states first with state(name)") unless self.state_for_name(value, true)
@@ -77,18 +77,23 @@
state = self.states.detect() {|state| state.name == name}
raise ArgumentError.new("Unknonw state '#{name}'") unless quiet || state
state
end
- def to_dot
- dots = self.transitions.map do |transition|
- " #{transition.from.name} -> #{transition.to.name}"
+ # Convert this state machine to the dot format of graphviz
+ def to_dot(options = {})
+ s = self.states.map do |state|
+ " #{state.to_dot(options)};"
end
- "digraph FSM {\n#{dots.join(";\n")}\n}"
+ t = self.transitions.map do |transition|
+ " #{transition.to_dot(options)};"
+ end
+ "digraph FSM_#{@target_class.name} {\n#{s.join("\n")}\n\n#{t.join("\n")}\n}"
end
- def dot(options = {})
+ #
+ def draw_graph(options = {})
format = options[:format] || :png
extension = options[:extension] || format
file_name = options[:outfile] || "#{@target_class.name.downcase}.#{extension}"
cmd = "dot -T#{format} -o#{file_name}"
IO.popen cmd, 'w' do |io|
@@ -113,13 +118,17 @@
transition = entry.last if entry
raise InvalidStateTransition.new("No transition with name '#{name}' defined from '#{from_name}'") unless transition
to_state = transition.to
from_state.exit(self)
- transition.fire_event(self, args)
- to_state.enter(self)
- Machine.set_current_state_name(self, to_state.name)
- true # at the moment always return true ... as soon as we have guards or thelike this could be false as well
+ if transition.fire?(self, args)
+ transition.fire_event(self, args)
+ to_state.enter(self)
+ Machine.set_current_state_name(self, to_state.name)
+ true
+ else
+ false
+ end
end
end
end
\ No newline at end of file