MINT-scxml 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gemtest +0 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +18 -0
- data/History.txt +4 -0
- data/MINT-scxml.gemspec +35 -0
- data/Manifest.txt +49 -0
- data/README.rdoc +50 -0
- data/Rakefile +31 -0
- data/lib/MINT-scxml.rb +5 -0
- data/lib/MINT-scxml/scxml-parser.rb +228 -0
- data/spec/atm_spec.rb +69 -0
- data/spec/parser_spec.rb +394 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/test_handgestures_spec.rb +53 -0
- data/spec/testmachines/atm_enhanced.rb +129 -0
- data/spec/testmachines/handgestures-scxmlgui.png +0 -0
- data/spec/testmachines/handgestures-scxmlgui.scxml +106 -0
- data/spec/testmachines/multiplestates.rb +25 -0
- data/spec/testmachines/traffic_light_enhanced.rb +56 -0
- data/spec/testmachines/vending_machine1.rb +18 -0
- data/spec/testmachines/vending_machine2.rb +42 -0
- data/spec/testmachines/vending_machine3.rb +48 -0
- data/spec/testmachines/vending_machine4.rb +29 -0
- data/spec_helper.rb +8 -0
- data/statemachines/AICommand_scxml_spec.rb +38 -0
- data/statemachines/AIO_scxml_spec.rb +36 -0
- data/statemachines/aui-scxml/AIC.scxml +26 -0
- data/statemachines/aui-scxml/AICommand.scxml +34 -0
- data/statemachines/aui-scxml/AICommand2.scxml +32 -0
- data/statemachines/aui-scxml/AIO.scxml +25 -0
- data/statemachines/aui/AIC.rb +29 -0
- data/statemachines/aui/AIChoiceElement.rb +32 -0
- data/statemachines/aui/AICommand.rb +34 -0
- data/statemachines/aui/AIIN.rb +7 -0
- data/statemachines/aui/AIINContinous.rb +41 -0
- data/statemachines/aui/AIMultiChoice.rb +46 -0
- data/statemachines/aui/AIMultiChoiceElement.rb +46 -0
- data/statemachines/aui/AIOUTContinous.rb +41 -0
- data/statemachines/aui/AISingleChoice.rb +42 -0
- data/statemachines/aui/AISingleChoiceElement.rb +45 -0
- data/statemachines/aui/aio.rb +28 -0
- data/statemachines/specs/AICommand_spec.rb +58 -0
- data/statemachines/specs/AIINContinous_spec.rb +61 -0
- data/statemachines/specs/AIMultiChoiceElement_spec.rb +70 -0
- data/statemachines/specs/AIMultiChoice_spec.rb +56 -0
- data/statemachines/specs/AIOUTContinous_spec.rb +71 -0
- data/statemachines/specs/AIO_spec.rb +53 -0
- data/statemachines/specs/AISingleChoiceElement_spec.rb +58 -0
- data/statemachines/specs/AISingleChoice_spec.rb +68 -0
- metadata +146 -0
@@ -0,0 +1,129 @@
|
|
1
|
+
# -*- coding: raw-text -*-
|
2
|
+
require 'rubygems'
|
3
|
+
require 'statemachine'
|
4
|
+
|
5
|
+
module Statemachine
|
6
|
+
module SuperstateBuilding
|
7
|
+
|
8
|
+
def state(id, &block)
|
9
|
+
builder = StateBuilder.new(id, @subject, @statemachine)
|
10
|
+
builder.instance_eval(&block) if block
|
11
|
+
p "id>> #{id}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
class AtmContext
|
18
|
+
|
19
|
+
attr_accessor :statemachine
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@conta = 100000
|
23
|
+
@senha = "0000"
|
24
|
+
end
|
25
|
+
|
26
|
+
def verifica_senha(senha)
|
27
|
+
if senha == @senha
|
28
|
+
@statemachine.senha_correta
|
29
|
+
else
|
30
|
+
@statemachine.senha_errada
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def verifica_escolha(escolha)
|
35
|
+
case escolha.downcase
|
36
|
+
when "saldo": @statemachine.saldo
|
37
|
+
when "saque": puts "Entre com a quantia a ser sacada: "
|
38
|
+
@statemachine.saque(gets.chomp!)
|
39
|
+
when "cancelar": @statemachine.cancelar
|
40
|
+
else @statemachine.invalida
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def emitindo(quantia)
|
45
|
+
if (Integer(quantia) <= @conta)
|
46
|
+
puts "Emitindo: R$#{quantia}"
|
47
|
+
@conta -= Integer(quantia)
|
48
|
+
else
|
49
|
+
puts "Saldo insuficiente"
|
50
|
+
end
|
51
|
+
nova_operacao
|
52
|
+
end
|
53
|
+
|
54
|
+
def saldo
|
55
|
+
puts "Seu saldo: R$#{@conta}"
|
56
|
+
nova_operacao
|
57
|
+
end
|
58
|
+
|
59
|
+
def nova_operacao
|
60
|
+
puts "Deseja realizar outra operacao? (S/N)"
|
61
|
+
decisao = gets.chomp!
|
62
|
+
if decisao.downcase == "s"
|
63
|
+
@statemachine.nova_opcao
|
64
|
+
else
|
65
|
+
@statemachine.cancelar
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
if __FILE__ == $0
|
71
|
+
|
72
|
+
atm = Statemachine.build do
|
73
|
+
startstate :ocioso
|
74
|
+
superstate :operando do
|
75
|
+
state :esperando_senha do
|
76
|
+
event :senha, :senha_inserida
|
77
|
+
on_entry Proc.new{puts "Digite a senha"}
|
78
|
+
end
|
79
|
+
state :senha_inserida do
|
80
|
+
event :senha_errada, :esperando_senha, Proc.new {puts "Senha incorreta."}
|
81
|
+
event :senha_correta, :esperando_opcao
|
82
|
+
on_entry :verifica_senha
|
83
|
+
end
|
84
|
+
state :esperando_opcao do
|
85
|
+
event :opcao, :opcao_escolhida
|
86
|
+
on_entry Proc.new{puts "Digite a sua escolha"}
|
87
|
+
end
|
88
|
+
state :opcao_escolhida do
|
89
|
+
event :saldo, :exibindo_saldo
|
90
|
+
event :saque, :emitindo_quantia
|
91
|
+
event :invalida, :esperando_opcao
|
92
|
+
on_entry :verifica_escolha
|
93
|
+
end
|
94
|
+
state :exibindo_saldo do
|
95
|
+
event :nova_opcao, :esperando_opcao
|
96
|
+
on_entry :saldo
|
97
|
+
|
98
|
+
end
|
99
|
+
state :emitindo_quantia do
|
100
|
+
event :nova_opcao, :esperando_opcao
|
101
|
+
on_entry :emitindo
|
102
|
+
end
|
103
|
+
|
104
|
+
event :cancelar, :ocioso, Proc.new {puts "Saindo..."}
|
105
|
+
end
|
106
|
+
|
107
|
+
state :ocioso do
|
108
|
+
event :cartao, :esperando_senha
|
109
|
+
event :cancelar, :ocioso
|
110
|
+
end
|
111
|
+
|
112
|
+
context AtmContext.new
|
113
|
+
end
|
114
|
+
|
115
|
+
atm.context.statemachine = atm
|
116
|
+
atm.cartao
|
117
|
+
# Continua perguntando a senha at� acertar
|
118
|
+
begin
|
119
|
+
atm.senha(gets.chomp!)
|
120
|
+
end until atm.state != :esperando_senha
|
121
|
+
|
122
|
+
# Continua perguntando at� acertar
|
123
|
+
begin
|
124
|
+
atm.opcao(gets.chomp!)
|
125
|
+
end until atm.state != :esperando_opcao
|
126
|
+
atm.cancelar
|
127
|
+
end
|
128
|
+
|
129
|
+
|
Binary file
|
@@ -0,0 +1,106 @@
|
|
1
|
+
<scxml id="SCXML" xmlns="http://www.w3.org/2005/07/scxml"><!-- node-size-and-position x=0.0 y=0.0 w=500.0 h=940.0 -->
|
2
|
+
<state id="hand_gestures" initial="no_hands"><!-- node-size-and-position x=20.0 y=40.0 w=450.0 h=880.0 -->
|
3
|
+
<state id="two_hands" initial="wait_two"><!-- node-size-and-position x=30.0 y=40.0 w=340.0 h=410.0 -->
|
4
|
+
<transition event="one_hand" target="one_hand"><!-- edge-path [one_hand] x=190.0 y=517.0 pointx=-1.0 pointy=23.0 offsetx=6.0 offsety=23.0 --></transition>
|
5
|
+
<transition event="no_hands" target="no_hands"><!-- edge-path [no_hands] x=94.49800059980008 y=474.80003998800396 pointx=0.0 pointy=7.0 offsetx=5.0 offsety=4.0 --></transition>
|
6
|
+
<state id="n" initial="narrowing"><!-- node-size-and-position x=20.0 y=240.0 w=135.53364159488612 h=147.0 -->
|
7
|
+
<transition event="widen" target="w">
|
8
|
+
<invoke src="print_distance" type="x-mint"></invoke>
|
9
|
+
<!-- edge-path [w] x=62.766820797443074 y=214.0 pointx=0.0 pointy=29.0 offsetx=-2.0 offsety=-8.0 --></transition>
|
10
|
+
<state id="narrowed"><!-- node-size-and-position x=30.0 y=30.0 w=60.0 h=20.0 -->
|
11
|
+
<transition event="narrow" target="narrowing">
|
12
|
+
<invoke src="print_distance" type="x-mint"></invoke>
|
13
|
+
<!-- edge-path [narrowing] x=82.76682079744307 y=84.0 pointx=0.0 pointy=22.0 offsetx=2.0 offsety=3.0 --></transition>
|
14
|
+
</state>
|
15
|
+
<state id="narrowing"><!-- node-size-and-position x=20.0 y=110.0 w=60.0 h=20.0 -->
|
16
|
+
<onentry>
|
17
|
+
<invoke src="start_confirm_timeout" type="x-mint"></invoke>
|
18
|
+
</onentry>
|
19
|
+
<onexit>
|
20
|
+
<invoke src="stop_confirm_timeout" type="x-mint"></invoke>
|
21
|
+
</onexit>
|
22
|
+
<transition event="confirm" target="narrowed"><!-- edge-path [narrowed] x=32.766820797443074 y=74.0 pointx=0.0 pointy=9.0 offsetx=-3.0 offsety=-6.0 --></transition>
|
23
|
+
<transition event="narrow">
|
24
|
+
<invoke src="reset_confirm_ticker" type="x-mint"></invoke>
|
25
|
+
<!-- edge-path [narrowing] pointx=0.0 pointy=-10.0 offsetx=27.0 offsety=-3.0 --></transition>
|
26
|
+
</state>
|
27
|
+
</state>
|
28
|
+
<state id="w" initial="widening"><!-- node-size-and-position x=20.953427405958184 y=42.99999999999999 w=133.62678678296976 h=147.0 -->
|
29
|
+
<transition event="narrow" target="n">
|
30
|
+
<invoke src="print_distance" type="x-mint"></invoke>
|
31
|
+
<!-- edge-path [n] x=111.0 y=215.0 pointx=0.0 pointy=31.0 offsetx=2.0 offsety=6.0 --></transition>
|
32
|
+
<state id="widened"><!-- node-size-and-position x=29.999999999999986 y=40.0 w=50.0 h=20.0 -->
|
33
|
+
<transition event="widen" target="widening">
|
34
|
+
<invoke src="print_distance" type="x-mint"></invoke>
|
35
|
+
<!-- edge-path [widening] x=81.8133933914849 y=81.0 pointx=0.0 pointy=15.0 offsetx=9.0 offsety=-10.0 --></transition>
|
36
|
+
</state>
|
37
|
+
<state id="widening"><!-- node-size-and-position x=21.962724282969738 y=111.0 w=50.0 h=20.0 -->
|
38
|
+
<onentry>
|
39
|
+
<invoke src="start_confirm_timeout" type="x-mint"></invoke>
|
40
|
+
</onentry>
|
41
|
+
<onexit>
|
42
|
+
<invoke src="stop_confirm_timeout" type="x-mint"></invoke>
|
43
|
+
</onexit>
|
44
|
+
<transition event="confirm" target="widened"><!-- edge-path [widened] x=43.50171423674868 y=80.18515344006022 pointx=0.0 pointy=21.0 offsetx=-1.0 offsety=-12.0 --></transition>
|
45
|
+
<transition event="widen" target="widening">
|
46
|
+
<invoke src="reset_confirm_ticker" type="x-mint"></invoke>
|
47
|
+
<!-- edge-path [widening] pointx=0.0 pointy=20.0 offsetx=0.0 offsety=0.0 --></transition>
|
48
|
+
</state>
|
49
|
+
</state>
|
50
|
+
<state id="wait_two"><!-- node-size-and-position x=210.0 y=160.0 w=100.0 h=40.0 -->
|
51
|
+
<transition event="widen" target="w"><!-- edge-path [w] pointx=-1.0 pointy=-20.0 offsetx=-7.0 offsety=-3.0 --></transition>
|
52
|
+
<transition event="narrow" target="n"></transition>
|
53
|
+
</state>
|
54
|
+
</state>
|
55
|
+
<state id="one_hand" initial="wait_one"><!-- node-size-and-position x=20.0 y=570.0 w=190.0 h=286.828125 -->
|
56
|
+
<transition event="two_hands" target="two_hands"><!-- edge-path [two_hands] x=129.50312353591877 y=509.75007808839797 pointx=0.0 pointy=-30.0 offsetx=1.0 offsety=-5.0 --></transition>
|
57
|
+
<transition event="no_hands" target="no_hands"><!-- edge-path [no_hands] x=50.0 y=547.0 pointx=0.0 pointy=19.0 offsetx=-3.0 offsety=-9.0 --></transition>
|
58
|
+
<state id="previous"><!-- node-size-and-position x=65.0 y=43.00000000000003 w=50.0 h=20.0 -->
|
59
|
+
<onentry>
|
60
|
+
<invoke src="command_timeout_start_prev" type="x-mint"></invoke>
|
61
|
+
</onentry>
|
62
|
+
<onexit>
|
63
|
+
<invoke src="command_timeout_stop" type="x-mint"></invoke>
|
64
|
+
</onexit>
|
65
|
+
<transition event="stop" target="wait_one"><!-- edge-path [wait_one] x=80.57492925712545 y=93.14495755427527 pointx=0.0 pointy=-12.0 offsetx=-3.0 offsety=3.0 --></transition>
|
66
|
+
<transition event="previous" target="previous_tick">
|
67
|
+
<invoke src="issued_rev" type="x-mint"></invoke>
|
68
|
+
<!-- edge-path [previous_tick] x=130.0 y=77.0 pointx=0.0 pointy=22.0 offsetx=5.0 offsety=6.0 --></transition>
|
69
|
+
</state>
|
70
|
+
<state id="wait_one"><!-- node-size-and-position x=20.0 y=113.00000000000003 w=50.0 h=20.0 -->
|
71
|
+
<transition event="next" target="next"><!-- edge-path [next] x=60.0 y=157.0 pointx=0.0 pointy=18.0 offsetx=4.0 offsety=9.0 --></transition>
|
72
|
+
<transition event="previous" target="previous"><!-- edge-path [previous] x=40.0 y=77.0 pointx=0.0 pointy=9.0 offsetx=-1.0 offsety=-13.0 --></transition>
|
73
|
+
</state>
|
74
|
+
<state id="next_tick"><!-- node-size-and-position x=20.999999999999996 y=246.82812499999991 w=50.0 h=20.0 -->
|
75
|
+
<onentry>
|
76
|
+
<invoke src="tick" type="x-mint"></invoke>
|
77
|
+
</onentry>
|
78
|
+
<transition event="tick" target="next"><!-- edge-path [next] x=33.50171423674869 y=219.81484655993978 pointx=0.0 pointy=15.0 offsetx=0.0 offsety=0.0 --></transition>
|
79
|
+
</state>
|
80
|
+
<state id="previous_tick"><!-- node-size-and-position x=110.0 y=106.99999999999999 w=70.0 h=20.0 -->
|
81
|
+
<onentry>
|
82
|
+
<invoke src="tick" type="x-mint"></invoke>
|
83
|
+
</onentry>
|
84
|
+
<transition event="tick" target="previous"><!-- edge-path [previous] x=110.0 y=87.0 pointx=0.0 pointy=8.0 offsetx=4.0 offsety=3.0 --></transition>
|
85
|
+
</state>
|
86
|
+
<state id="next"><!-- node-size-and-position x=20.0 y=176.99999999999997 w=50.0 h=20.0 -->
|
87
|
+
<onentry>
|
88
|
+
<invoke src="command_timeout_start_next" type="x-mint"></invoke>
|
89
|
+
</onentry>
|
90
|
+
<onexit>
|
91
|
+
<invoke src="command_timeout_stop" type="x-mint"></invoke>
|
92
|
+
</onexit>
|
93
|
+
<transition event="stop" target="wait_one"><!-- edge-path [wait_one] x=33.00799041278211 y=158.39968038348871 pointx=0.0 pointy=11.0 offsetx=-4.0 offsety=5.0 --></transition>
|
94
|
+
<transition event="next" target="next_tick">
|
95
|
+
<invoke src="issued_next" type="x-mint"></invoke>
|
96
|
+
<!-- edge-path [next_tick] x=53.49828576325132 y=220.1851534400602 pointx=0.0 pointy=17.0 offsetx=0.0 offsety=-8.0 --></transition>
|
97
|
+
</state>
|
98
|
+
</state>
|
99
|
+
<state id="no_hands"><!-- node-size-and-position x=20.0 y=497.0 w=60.0 h=30.0 -->
|
100
|
+
<transition event="one_hand" target="one_hand"><!-- edge-path [one_hand] x=96.0 y=545.0 -->
|
101
|
+
</transition>
|
102
|
+
<transition event="two_hands" target="two_hands"><!-- edge-path [two_hands] x=70.0 y=477.0 pointx=0.0 pointy=35.0 offsetx=-5.0 offsety=11.0 -->
|
103
|
+
</transition>
|
104
|
+
</state>
|
105
|
+
</state>
|
106
|
+
</scxml>
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'statemachine'
|
3
|
+
|
4
|
+
if __FILE__ == $0
|
5
|
+
@state = Statemachine.build do
|
6
|
+
state :state1 do
|
7
|
+
event :event1, :state2, Proc.new {puts("This is action1")}
|
8
|
+
end
|
9
|
+
|
10
|
+
state :state2 do
|
11
|
+
event :event2, :state3, Proc.new {puts("This is action2")}
|
12
|
+
end
|
13
|
+
|
14
|
+
state :state3 do
|
15
|
+
event :event3, :state1, Proc.new {puts("This is action3")}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
@state.event1
|
20
|
+
puts @state.state
|
21
|
+
@state.event2
|
22
|
+
puts @state.state
|
23
|
+
@state.event3
|
24
|
+
puts @state.state
|
25
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'statemachine'
|
3
|
+
|
4
|
+
class TrafficLightContext
|
5
|
+
|
6
|
+
attr_accessor :statemachine
|
7
|
+
|
8
|
+
def color_green
|
9
|
+
puts "red yellow :GREEN:"
|
10
|
+
end
|
11
|
+
|
12
|
+
def color_yellow
|
13
|
+
puts "red :YELLOW: green"
|
14
|
+
end
|
15
|
+
|
16
|
+
def color_red
|
17
|
+
puts ":RED: yellow green"
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
traffic_light = Statemachine.build do
|
24
|
+
|
25
|
+
superstate :operational do
|
26
|
+
startstate :green
|
27
|
+
state :green do
|
28
|
+
event :change, :yellow
|
29
|
+
on_entry :color_green
|
30
|
+
end
|
31
|
+
state :yellow do
|
32
|
+
event :change, :red
|
33
|
+
on_entry :color_yellow
|
34
|
+
end
|
35
|
+
state :red do
|
36
|
+
event :change, :green
|
37
|
+
on_entry :color_red
|
38
|
+
end
|
39
|
+
event :pedestrian, :pedestrian_mode, Proc.new {puts "Entering Pedestrian Mode"}
|
40
|
+
end
|
41
|
+
|
42
|
+
trans :pedestrian_mode, :operate, :operational_H, Proc.new {puts "Exit Pedestrian Mode"}
|
43
|
+
|
44
|
+
context TrafficLightContext.new
|
45
|
+
end
|
46
|
+
|
47
|
+
traffic_light.change
|
48
|
+
traffic_light.change
|
49
|
+
traffic_light.change
|
50
|
+
traffic_light.pedestrian
|
51
|
+
traffic_light.operate
|
52
|
+
traffic_light.change
|
53
|
+
traffic_light.change
|
54
|
+
traffic_light.pedestrian
|
55
|
+
traffic_light.operate
|
56
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'statemachine'
|
3
|
+
|
4
|
+
if __FILE__ == $0
|
5
|
+
|
6
|
+
vending_machine = Statemachine.build do
|
7
|
+
trans :waiting, :dollar, :paid
|
8
|
+
trans :paid, :selection, :waiting
|
9
|
+
trans :waiting, :selection, :waiting
|
10
|
+
trans :paid, :dollar, :paid
|
11
|
+
end
|
12
|
+
|
13
|
+
puts vending_machine.state
|
14
|
+
vending_machine.dollar
|
15
|
+
puts vending_machine.state
|
16
|
+
vending_machine.selection
|
17
|
+
puts vending_machine.state
|
18
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'statemachine'
|
3
|
+
|
4
|
+
class VendingMachineContext
|
5
|
+
def activate
|
6
|
+
puts "activating"
|
7
|
+
end
|
8
|
+
|
9
|
+
def release(product)
|
10
|
+
puts "releasing product: #{product}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def refund
|
14
|
+
puts "refuding dollar"
|
15
|
+
end
|
16
|
+
|
17
|
+
def sales_mode
|
18
|
+
puts "going into sales mode"
|
19
|
+
end
|
20
|
+
|
21
|
+
def operation_mode
|
22
|
+
puts "going into operation mode"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
if __FILE__ == $0
|
27
|
+
vending_machine = Statemachine.build do
|
28
|
+
state :waiting do
|
29
|
+
event :dollar, :paid, :activate # event, destination state, action
|
30
|
+
event :selection, :waiting
|
31
|
+
on_entry :sales_mode # action
|
32
|
+
on_exit :operation_mode
|
33
|
+
end
|
34
|
+
trans :paid, :selection, :waiting, :release # state, event, destination state, action
|
35
|
+
trans :paid, :dollar, :paid, :refund
|
36
|
+
context VendingMachineContext.new
|
37
|
+
end
|
38
|
+
|
39
|
+
vending_machine.dollar
|
40
|
+
vending_machine.dollar
|
41
|
+
vending_machine.selection "Peanuts"
|
42
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'statemachine'
|
3
|
+
|
4
|
+
class VendingMachineContext
|
5
|
+
attr_accessor :statemachine
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@amount_tendered = 0
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_coin
|
12
|
+
@amount_tendered = @amount_tendered + 25
|
13
|
+
end
|
14
|
+
|
15
|
+
def count_amount_tendered
|
16
|
+
if @amount_tendered >= 100
|
17
|
+
@statemachine.paid
|
18
|
+
else
|
19
|
+
@statemachine.not_paid_yet
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def prompt_money
|
24
|
+
puts "$.#{@amount_tendered}: more money please"
|
25
|
+
end
|
26
|
+
|
27
|
+
def prompt_selection
|
28
|
+
puts "please make a selection"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
if __FILE__ == $0
|
33
|
+
vending_machine = Statemachine.build do
|
34
|
+
trans :accept_money, :coin, :coin_inserted, :add_coin
|
35
|
+
state :coin_inserted do
|
36
|
+
event :not_paid_yet, :accept_money, :prompt_money
|
37
|
+
event :paid, :await_selection, :prompt_selection
|
38
|
+
on_entry :count_amount_tendered
|
39
|
+
end
|
40
|
+
context VendingMachineContext.new
|
41
|
+
end
|
42
|
+
vending_machine.context.statemachine = vending_machine
|
43
|
+
|
44
|
+
vending_machine.coin
|
45
|
+
vending_machine.coin
|
46
|
+
vending_machine.coin
|
47
|
+
vending_machine.coin
|
48
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'statemachine'
|
3
|
+
|
4
|
+
if __FILE__ == $0
|
5
|
+
vending_machine = Statemachine.build do
|
6
|
+
superstate :operational do
|
7
|
+
state :waiting do
|
8
|
+
event :dollar, :paid
|
9
|
+
event :selection, :waiting
|
10
|
+
end
|
11
|
+
trans :paid, :selection, :waiting
|
12
|
+
trans :paid, :dollar, :paid
|
13
|
+
|
14
|
+
event :repair, :repair_mode, Proc.new {puts "Entering Repair Mode"}
|
15
|
+
end
|
16
|
+
|
17
|
+
trans :repair_mode, :operate, :operational_H, Proc.new {puts "Exit Repair Mode"}
|
18
|
+
|
19
|
+
on_entry_of :waiting, Proc.new {puts "Entering Waiting State"}
|
20
|
+
on_entry_of :paid, Proc.new {puts "Entering Paid State"}
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
vending_machine.repair
|
25
|
+
vending_machine.operate
|
26
|
+
vending_machine.dollar
|
27
|
+
vending_machine.repair
|
28
|
+
vending_machine.operate
|
29
|
+
end
|