MINT-statemachine 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/CHANGES +135 -0
  2. data/LICENSE +16 -0
  3. data/MINT-statemachine.gemspec +27 -0
  4. data/README.rdoc +69 -0
  5. data/Rakefile +88 -0
  6. data/TODO +2 -0
  7. data/lib/statemachine.rb +26 -0
  8. data/lib/statemachine/action_invokation.rb +83 -0
  9. data/lib/statemachine/builder.rb +383 -0
  10. data/lib/statemachine/generate/dot_graph.rb +1 -0
  11. data/lib/statemachine/generate/dot_graph/dot_graph_statemachine.rb +127 -0
  12. data/lib/statemachine/generate/java.rb +1 -0
  13. data/lib/statemachine/generate/java/java_statemachine.rb +265 -0
  14. data/lib/statemachine/generate/src_builder.rb +48 -0
  15. data/lib/statemachine/generate/util.rb +50 -0
  16. data/lib/statemachine/parallelstate.rb +196 -0
  17. data/lib/statemachine/state.rb +102 -0
  18. data/lib/statemachine/statemachine.rb +279 -0
  19. data/lib/statemachine/stub_context.rb +26 -0
  20. data/lib/statemachine/superstate.rb +53 -0
  21. data/lib/statemachine/transition.rb +76 -0
  22. data/lib/statemachine/version.rb +17 -0
  23. data/spec/action_invokation_spec.rb +101 -0
  24. data/spec/builder_spec.rb +243 -0
  25. data/spec/default_transition_spec.rb +111 -0
  26. data/spec/generate/dot_graph/dot_graph_stagemachine_spec.rb +27 -0
  27. data/spec/generate/java/java_statemachine_spec.rb +349 -0
  28. data/spec/history_spec.rb +107 -0
  29. data/spec/noodle.rb +23 -0
  30. data/spec/sm_action_parameterization_spec.rb +99 -0
  31. data/spec/sm_activation_spec.rb +116 -0
  32. data/spec/sm_entry_exit_actions_spec.rb +99 -0
  33. data/spec/sm_odds_n_ends_spec.rb +67 -0
  34. data/spec/sm_parallel_state_spec.rb +207 -0
  35. data/spec/sm_simple_spec.rb +26 -0
  36. data/spec/sm_super_state_spec.rb +55 -0
  37. data/spec/sm_turnstile_spec.rb +76 -0
  38. data/spec/spec_helper.rb +121 -0
  39. data/spec/transition_spec.rb +107 -0
  40. metadata +115 -0
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "simple cases:" do
4
+ before(:each) do
5
+ @sm = Statemachine::Statemachine.new
6
+ @sm.context = self
7
+ @count = 0
8
+ @proc = Proc.new {@count = @count + 1}
9
+ end
10
+
11
+ it "reset" do
12
+ Statemachine.build(@sm) { |s| s.trans :start, :blah, :end, @proc }
13
+ @sm.process_event(:blah)
14
+
15
+ @sm.reset
16
+
17
+ @sm.state.should equal(:start)
18
+ end
19
+
20
+ it "no proc in transition" do
21
+ Statemachine.build(@sm) { |s| s.trans :on, :flip, :off }
22
+
23
+ @sm.flip
24
+ end
25
+
26
+ end
@@ -0,0 +1,55 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "Turn Stile" do
4
+ include TurnstileStatemachine
5
+
6
+ before(:each) do
7
+ create_turnstile
8
+
9
+ @out_out_order = false
10
+
11
+ @sm = Statemachine.build do
12
+ superstate :operative do
13
+ trans :locked, :coin, :unlocked, Proc.new { @locked = false;true}
14
+ trans :unlocked, :pass, :locked, Proc.new { @locked = true ;true}
15
+ trans :locked, :pass, :locked, Proc.new { @alarm_status = true ;true}
16
+ trans :unlocked, :coin, :locked, Proc.new { @thankyou_status = true ;true}
17
+ event :maintain, :maintenance, Proc.new { @out_of_order = true ;true}
18
+ end
19
+ trans :maintenance, :operate, :operative, Proc.new { @out_of_order = false;true }
20
+ startstate :locked
21
+ end
22
+ @sm.context = self
23
+ end
24
+
25
+ it "substates respond to superstate transitions" do
26
+ @sm.process_event(:maintain)
27
+ @sm.state.should equal(:maintenance)
28
+ @locked.should equal(true)
29
+ @out_of_order.should equal(true)
30
+ end
31
+
32
+ it "after transitions, substates respond to superstate transitions" do
33
+ @sm.coin
34
+ @sm.maintain
35
+ @sm.state.should equal(:maintenance)
36
+ @locked.should equal(false)
37
+ @out_of_order.should equal(true)
38
+ end
39
+
40
+ it "states could be redefined as superstates" do
41
+ @sm = Statemachine.build @sm do
42
+ superstate :unlocked do
43
+ trans :u1, :u, :u2
44
+ trans :u2, :e, :maintenance
45
+ end
46
+ end
47
+
48
+ @sm.coin
49
+ @sm.state.should equal(:u1)
50
+ @sm.u
51
+ @sm.state.should equal(:u2)
52
+ @sm.coin
53
+ @sm.state.should equal(:locked)
54
+ end
55
+ end
@@ -0,0 +1,76 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "Turn Stile" do
4
+ include TurnstileStatemachine
5
+
6
+ before(:each) do
7
+ create_turnstile
8
+ end
9
+
10
+ it "connections" do
11
+ locked_state = @sm.get_state(:locked)
12
+ unlocked_state = @sm.get_state(:unlocked)
13
+
14
+ locked_state.transitions.length.should equal(2)
15
+ unlocked_state.transitions.length.should equal(2)
16
+
17
+ check_transition(locked_state.transitions[:coin], :locked, :unlocked, :coin, @unlock)
18
+ check_transition(locked_state.transitions[:pass], :locked, :locked, :pass, @alarm)
19
+ check_transition(unlocked_state.transitions[:pass], :unlocked, :locked, :pass, @lock)
20
+ check_transition(unlocked_state.transitions[:coin], :unlocked, :locked, :coin, @thankyou)
21
+ end
22
+
23
+ it "start state" do
24
+ @sm.reset
25
+ @sm.startstate.should equal(:locked)
26
+ @sm.state.should equal(:locked)
27
+ end
28
+
29
+ it "bad event" do
30
+ begin
31
+ @sm.process_event(:blah)
32
+ self.should.fail_with_message("Exception expected")
33
+ rescue Exception => e
34
+ e.class.should equal(Statemachine::TransitionMissingException)
35
+ e.to_s.should eql("'locked' state does not respond to the 'blah' event.")
36
+ end
37
+ end
38
+
39
+ it "locked state with a coin" do
40
+ @sm.process_event(:coin)
41
+
42
+ @sm.state.should equal(:unlocked)
43
+ @locked.should equal(false)
44
+ end
45
+
46
+ it "locked state with pass event" do
47
+ @sm.process_event(:pass)
48
+
49
+ @sm.state.should equal(:locked)
50
+ @locked.should equal(true)
51
+ @alarm_status.should equal(true)
52
+ end
53
+
54
+ it "unlocked state with coin" do
55
+ @sm.process_event(:coin)
56
+ @sm.process_event(:coin)
57
+
58
+ @sm.state.should equal(:locked)
59
+ @thankyou_status.should equal(true)
60
+ end
61
+
62
+ it "unlocked state with pass event" do
63
+ @sm.process_event(:coin)
64
+ @sm.process_event(:pass)
65
+
66
+ @sm.state.should equal(:locked)
67
+ @locked.should equal(true)
68
+ end
69
+
70
+ it "events invoked via method_missing" do
71
+ @sm.coin
72
+ @sm.state.should equal(:unlocked)
73
+ @sm.pass
74
+ @sm.state.should equal(:locked)
75
+ end
76
+ end
@@ -0,0 +1,121 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ require 'rubygems'
3
+ require 'spec'
4
+ require 'statemachine'
5
+
6
+ $IS_TEST = true
7
+
8
+ def check_transition(transition, origin_id, destination_id, event, action)
9
+ transition.should_not equal(nil)
10
+ transition.event.should equal(event)
11
+ transition.origin_id.should equal(origin_id)
12
+ transition.destination_id.should equal(destination_id)
13
+ transition.action.should eql(action)
14
+ end
15
+
16
+ module SwitchStatemachine
17
+
18
+ def create_switch
19
+ @status = "off"
20
+ @sm = Statemachine.build do
21
+ trans :off, :toggle, :on, Proc.new { @status = "on" }
22
+ trans :on, :toggle, :off, Proc.new { @status = "off" }
23
+ end
24
+ @sm.context = self
25
+ end
26
+
27
+ end
28
+
29
+ module ParallelStatemachine
30
+
31
+ def create_parallel
32
+
33
+ @cooked = "false"
34
+ @out_of_order = false
35
+
36
+ @sm = Statemachine.build do
37
+ trans :start,:go,:p
38
+ state :maintenance
39
+ parallel :p do
40
+ statemachine :s1 do
41
+ superstate :operative do
42
+ trans :locked, :coin, :unlocked, Proc.new { @cooked = true;true}
43
+ trans :unlocked, :coin, :locked
44
+ event :maintain, :maintenance, Proc.new { @out_of_order = true ;true}
45
+ end
46
+ end
47
+ statemachine :s2 do
48
+ superstate :onoff do
49
+ trans :on, :toggle, :off
50
+ trans :off, :toggle, :on
51
+ end
52
+ end
53
+ end
54
+ end
55
+ @sm.context = self
56
+ end
57
+
58
+ def create_tick
59
+ @status = "off"
60
+ @sm = Statemachine.build do
61
+ trans :off, :toggle, :on
62
+ trans :on, :toggle, :off
63
+ state :on do
64
+ on_entry Proc.new { @sm.toggle;true}
65
+ end
66
+
67
+ end
68
+ @sm.context = self
69
+ end
70
+
71
+ def create_tome
72
+ @sm = Statemachine.build do
73
+ trans :me, :toggle, :me
74
+ end
75
+ @sm.context = self
76
+ end
77
+ end
78
+
79
+
80
+
81
+ module TurnstileStatemachine
82
+
83
+ def create_turnstile
84
+ @locked = true
85
+ @alarm_status = false
86
+ @thankyou_status = false
87
+ @lock = "@locked = true;true"
88
+ @unlock = "@locked = false;true"
89
+ @alarm = "@alarm_status = true;true"
90
+ @thankyou = "@thankyou_status = true;true"
91
+
92
+ @sm = Statemachine.build do
93
+ trans :locked, :coin, :unlocked, "@locked = false;true"
94
+ trans :unlocked, :pass, :locked, "@locked = true;true"
95
+ trans :locked, :pass, :locked, "@alarm_status = true;true"
96
+ trans :unlocked, :coin, :locked, "@thankyou_status = true;true"
97
+ end
98
+ @sm.context = self
99
+ end
100
+
101
+ end
102
+
103
+ TEST_DIR = File.expand_path(File.dirname(__FILE__) + "/../test_dir/")
104
+
105
+ def test_dir(name = nil)
106
+ Dir.mkdir(TEST_DIR) if !File.exist?(TEST_DIR)
107
+ return TEST_DIR if name.nil?
108
+ dir = File.join(TEST_DIR, name)
109
+ Dir.mkdir(dir) if !File.exist?(dir)
110
+ return dir
111
+ end
112
+
113
+ def remove_test_dir(name)
114
+ system "rm -rf #{test_dir(name)}" if File.exist?(test_dir(name))
115
+ end
116
+
117
+ def load_lines(*segs)
118
+ filename = File.join(*segs)
119
+ File.should exist( filename)
120
+ return IO.read(filename).split("\n")
121
+ end
@@ -0,0 +1,107 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "Transition Calculating Exits and Entries" do
4
+
5
+ before(:each) do
6
+ @transition = Statemachine::Transition.new(nil, nil, nil, nil, true)
7
+ end
8
+
9
+ it "to nil" do
10
+ @a = Statemachine::State.new("a", nil, nil)
11
+ exits, entries = @transition.exits_and_entries(@a, nil)
12
+ exits.to_s.should eql([@a].to_s)
13
+ entries.to_s.should eql([].to_s)
14
+ entries.length.should equal(0)
15
+ end
16
+
17
+ it "to itself" do
18
+ @a = Statemachine::State.new("a", nil, nil)
19
+ exits, entries = @transition.exits_and_entries(@a, @a)
20
+ exits.to_s.should eql([@a].to_s)
21
+ entries.to_s.should eql([@a].to_s)
22
+ end
23
+
24
+ it "to friend" do
25
+ @a = Statemachine::State.new("a", nil, nil)
26
+ @b = Statemachine::State.new("b", nil, nil)
27
+ exits, entries = @transition.exits_and_entries(@a, @b)
28
+ exits.to_s.should eql([@a].to_s)
29
+ entries.to_s.should eql([@b].to_s)
30
+ end
31
+
32
+ it "to parent" do
33
+ @b = Statemachine::State.new("b", nil, nil)
34
+ @a = Statemachine::State.new("a", @b, nil)
35
+ exits, entries = @transition.exits_and_entries(@a, @b)
36
+ exits.to_s.should eql([@a, @b].to_s)
37
+ entries.to_s.should eql([@b].to_s)
38
+ end
39
+
40
+ it "to uncle" do
41
+ @b = Statemachine::State.new("b", nil, nil)
42
+ @a = Statemachine::State.new("a", @b, nil)
43
+ @c = Statemachine::State.new("c", nil, nil)
44
+ exits, entries = @transition.exits_and_entries(@a, @c)
45
+ exits.to_s.should eql([@a, @b].to_s)
46
+ entries.to_s.should eql([@c].to_s)
47
+ end
48
+
49
+ it "to cousin" do
50
+ @b = Statemachine::State.new("b", nil, nil)
51
+ @d = Statemachine::State.new("d", nil, nil)
52
+ @a = Statemachine::State.new("a", @b, nil)
53
+ @c = Statemachine::State.new("c", @d, nil)
54
+ exits, entries = @transition.exits_and_entries(@a, @c)
55
+ exits.to_s.should eql([@a, @b].to_s)
56
+ entries.to_s.should eql([@d, @c].to_s)
57
+ end
58
+
59
+ it "to nephew" do
60
+ @b = Statemachine::State.new("b", nil, nil)
61
+ @c = Statemachine::State.new("c", nil, nil)
62
+ @a = Statemachine::State.new("a", @b, nil)
63
+ exits, entries = @transition.exits_and_entries(@c, @a)
64
+ exits.to_s.should eql([@c].to_s)
65
+ entries.to_s.should eql([@b,@a].to_s)
66
+ end
67
+
68
+ it "to sister" do
69
+ @c = Statemachine::State.new("c", nil, nil)
70
+ @a = Statemachine::State.new("a", @c, nil)
71
+ @b = Statemachine::State.new("b", @c, nil)
72
+ exits, entries = @transition.exits_and_entries(@a, @b)
73
+ exits.to_s.should eql([@a].to_s)
74
+ entries.to_s.should eql([@b].to_s)
75
+ end
76
+
77
+ it "to second cousin" do
78
+ @c = Statemachine::State.new("c", nil, nil)
79
+ @b = Statemachine::State.new("b", @c, nil)
80
+ @a = Statemachine::State.new("a", @b, nil)
81
+ @e = Statemachine::State.new("e", @c, nil)
82
+ @d = Statemachine::State.new("d", @e, nil)
83
+ exits, entries = @transition.exits_and_entries(@a, @d)
84
+ exits.to_s.should eql([@a, @b].to_s)
85
+ entries.to_s.should eql([@e, @d].to_s)
86
+ end
87
+
88
+ it "to grandparent" do
89
+ @c = Statemachine::State.new("c", nil, nil)
90
+ @b = Statemachine::State.new("b", @c, nil)
91
+ @a = Statemachine::State.new("a", @b, nil)
92
+ exits, entries = @transition.exits_and_entries(@a, @c)
93
+ exits.to_s.should eql([@a, @b, @c].to_s)
94
+ entries.to_s.should eql([@c].to_s)
95
+ end
96
+
97
+ it "to parent's grandchild" do
98
+ @c = Statemachine::State.new("c", nil, nil)
99
+ @b = Statemachine::State.new("b", @c, nil)
100
+ @a = Statemachine::State.new("a", @b, nil)
101
+ @d = Statemachine::State.new("d", @c, nil)
102
+ exits, entries = @transition.exits_and_entries(@d, @a)
103
+ exits.to_s.should eql([@d].to_s)
104
+ entries.to_s.should eql([@b, @a].to_s)
105
+ end
106
+
107
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: MINT-statemachine
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 2
9
+ - 2
10
+ version: 1.2.2
11
+ platform: ruby
12
+ authors:
13
+ - Sebastian Feuerstack
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-30 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: The MINT Statemachine is a ruby library for building Finite State Machines, based on the Statemachine gem by Micah Martin.
22
+ email: Sebastian@Feuerstack.org
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - Rakefile
31
+ - README.rdoc
32
+ - MINT-statemachine.gemspec
33
+ - CHANGES
34
+ - TODO
35
+ - LICENSE
36
+ - lib/statemachine/generate/java/java_statemachine.rb
37
+ - lib/statemachine/generate/src_builder.rb
38
+ - lib/statemachine/generate/java.rb
39
+ - lib/statemachine/generate/dot_graph.rb
40
+ - lib/statemachine/generate/util.rb
41
+ - lib/statemachine/generate/dot_graph/dot_graph_statemachine.rb
42
+ - lib/statemachine/transition.rb
43
+ - lib/statemachine/superstate.rb
44
+ - lib/statemachine/version.rb
45
+ - lib/statemachine/statemachine.rb
46
+ - lib/statemachine/stub_context.rb
47
+ - lib/statemachine/state.rb
48
+ - lib/statemachine/parallelstate.rb
49
+ - lib/statemachine/action_invokation.rb
50
+ - lib/statemachine/builder.rb
51
+ - lib/statemachine.rb
52
+ - spec/sm_turnstile_spec.rb
53
+ - spec/generate/java/java_statemachine_spec.rb
54
+ - spec/generate/dot_graph/dot_graph_stagemachine_spec.rb
55
+ - spec/sm_odds_n_ends_spec.rb
56
+ - spec/noodle.rb
57
+ - spec/sm_entry_exit_actions_spec.rb
58
+ - spec/default_transition_spec.rb
59
+ - spec/action_invokation_spec.rb
60
+ - spec/sm_parallel_state_spec.rb
61
+ - spec/builder_spec.rb
62
+ - spec/sm_activation_spec.rb
63
+ - spec/sm_super_state_spec.rb
64
+ - spec/transition_spec.rb
65
+ - spec/spec_helper.rb
66
+ - spec/sm_simple_spec.rb
67
+ - spec/sm_action_parameterization_spec.rb
68
+ - spec/history_spec.rb
69
+ homepage: http://www.multi-access.de
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.5
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: MINT-Statemachine-1.2.2 - Statemachine Library for Ruby based on statemachine from http://slagyr.github.com/statemachine http://www.multi-access.de/open-source-software/third-party-software-extensions/
102
+ test_files:
103
+ - spec/sm_turnstile_spec.rb
104
+ - spec/sm_odds_n_ends_spec.rb
105
+ - spec/sm_entry_exit_actions_spec.rb
106
+ - spec/default_transition_spec.rb
107
+ - spec/action_invokation_spec.rb
108
+ - spec/sm_parallel_state_spec.rb
109
+ - spec/builder_spec.rb
110
+ - spec/sm_activation_spec.rb
111
+ - spec/sm_super_state_spec.rb
112
+ - spec/transition_spec.rb
113
+ - spec/sm_simple_spec.rb
114
+ - spec/sm_action_parameterization_spec.rb
115
+ - spec/history_spec.rb