piotrmurach/finite_machine

repeatedly sending the same event doesn't work?

kjwierenga opened this issue · 5 comments

I'm playing with finite_machine and I can’t seem to get this very simple example to work. It pushes through a couple of states on a single (repeated) event. Why doesn’t this work?

require 'finite_machine'

bug = FiniteMachine.define do
  initial :initial

  events {
    event :bump,  :initial => :low
    event :bump,  :low     => :medium
    event :bump,  :medium  => :high
  }

  callbacks {
    on_enter_event { |event| puts "#{event.name}\t#{event.from}\t=>\t#{event.to}" }
  }
end

bug.bump
bug.bump
bug.bump

Output is:

$ ruby examples/bug.rb
bump    medium  =>  high
bump    initial =>  high
bump    low =>  high

What am I doing wrong? Or is this a bug?

Changing three event definitions for the same event to one didn't solve the problem either.

  events {
    event :bump, :initial => :low,
                 :low     => :medium,
                 :medium  => :high
  }

Output then becomes

$ ruby examples/bug.rb
bump    initial =>  low

So it only does the one transition. What's going on?

@kjwierenga looking into it!

I've released v0.6.0 that fixes this problem. The machine itself worked correctly in so far as the transitions are concerned, for instance, when you call bump and then bug.current the state changed fine. However, the event payload was at fault, it contained the wrong information about the current transition. I haven't thoroughly tested the case when the same event name is used to specify transitions. This is fixed now! Further, I have rewritten how the states are parsed, so now both examples should work. Sorry for the inconvenience! Please let me know if you spot anything else?

Thanks Peter. That was quick. One problem remaining:

$ ruby examples/finite_machine/lights.rb 
/Users/kjw/.rvm/gems/ruby-1.9.3-p484/gems/finite_machine-0.6.0/lib/finite_machine/state_machine.rb:10:in `<class:StateMachine>': uninitialized constant FiniteMachine::StateMachine::Forwardable (NameError)
    from /Users/kjw/.rvm/gems/ruby-1.9.3-p484/gems/finite_machine-0.6.0/lib/finite_machine/state_machine.rb:6:in `<module:FiniteMachine>'
    from /Users/kjw/.rvm/gems/ruby-1.9.3-p484/gems/finite_machine-0.6.0/lib/finite_machine/state_machine.rb:4:in `<top (required)>'
    from /Users/kjw/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/kjw/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/kjw/.rvm/gems/ruby-1.9.3-p484/gems/finite_machine-0.6.0/lib/finite_machine.rb:21:in `<top (required)>'
    from /Users/kjw/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
    from /Users/kjw/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
    from /Users/kjw/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
    from examples/finite_machine/lights.rb:1:in `<main>'

I think you need a require 'forwardable' somewhere...

Please try v0.6.1.