simplefsm 0.1.3 → 0.2.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.
Files changed (3) hide show
  1. data/lib/simplefsm.rb +48 -32
  2. metadata +11 -7
  3. checksums.yaml +0 -15
@@ -15,7 +15,7 @@
15
15
  # License:: MIT License
16
16
 
17
17
  module SimpleFSM
18
- VERSION = '0.1.2'
18
+ VERSION = '0.2.0'
19
19
 
20
20
  def initialize
21
21
  @state ||= {}
@@ -54,6 +54,7 @@ module SimpleFSM
54
54
  fsm_prepare_state args
55
55
 
56
56
  if args
57
+ # If we have args here it must be an Array
57
58
  if args.class != Array
58
59
  return
59
60
  end
@@ -71,52 +72,67 @@ module SimpleFSM
71
72
  uniquestates = []
72
73
 
73
74
  if statetrans
75
+ # All transitions for this event in the current state
74
76
  trans = statetrans.select{|t| !t.select{|k, v| k==:event and v == ev}.empty?}
75
77
 
76
78
  if trans and trans.size>0
77
- newstates = trans.select do |v|
78
- if v.has_key?(:guard)
79
- send(v[:guard], args)
80
- else
81
- true
82
- end
83
- end
84
-
85
- newstates.each do |a|
86
- uniquestates << a[:new] if a.has_key?(:new)
79
+ # The first transition that is triggered
80
+ index_triggered = trans.index do |v|
81
+ # Guard specifiers:
82
+ # :guard - all must be true
83
+ # :guard_not - all must be false
84
+ # :guard_or - at least one must be true
85
+ # All guard specified specifiers must evaluate to true
86
+ # in order for transition to be triggered.
87
+ guard_all = true
88
+ guards_and = []
89
+ guards_or = []
90
+ guards_not = []
91
+ if v.has_key?(:guard)
92
+ guards_and << v[:guard]
93
+ guards_and.flatten!
87
94
  end
88
- uniquestates.uniq!
89
- numstates = uniquestates.size
90
-
91
- if numstates > 1
92
- raise "Error in transition (event #{ev}, state #{st}): More than 1 (#{numstates}) new state (#{uniquestates.inspect})."
93
- return
94
- elsif numstates < 1
95
- return
95
+ if v.has_key?(:guard_or)
96
+ guards_or << v[:guard_or]
97
+ guards_or.flatten!
96
98
  end
99
+ if v.has_key?(:guard_not)
100
+ guards_not << v[:guard_not]
101
+ guards_not.flatten!
102
+ end
103
+
104
+ guard_all &&= guards_and.all? {|g| send(g, args) } if guards_and.size > 0
105
+ guard_all &&= guards_or.any? {|g| send(g, args) } if guards_or.size > 0
106
+ guard_all &&= !guards_not.any? {|g| send(g, args) } if guards_not.size > 0
107
+ guard_all
108
+ end
109
+ if index_triggered
110
+ trans_triggered = trans[index_triggered]
111
+ new_state = trans_triggered[:new] if trans_triggered.has_key?(:new)
112
+
113
+ #START of :action keyword => call procs for event
114
+ # :do keyword - is not prefered
115
+ # because it confuses source code editors
116
+ action_keys = ['do'.to_sym, :action]
97
117
 
98
- #:do keyword => call proc for event
99
118
  doprocs = []
100
- newstates.each do |a|
101
- doprocs << a[:do] if a.has_key?(:do)
119
+ action_keys.each do |key|
120
+ doprocs << trans_triggered[key] if trans_triggered.has_key?(key)
102
121
  end
103
- doprocs.uniq!
122
+ doprocs.flatten!
104
123
 
105
- if doprocs.size > 0
106
- doprocs.each do |p|
107
- send(p, args)
108
- end
109
- end
110
- #end :do keyword
124
+ doprocs.each {|p| send(p, args)} if doprocs.size > 0
125
+ #END of :action keyword
111
126
 
112
- do_transform uniquestates.first, args
127
+ do_transform new_state, args
113
128
  end
114
129
  end
130
+ end
115
131
  fsm_save_state args
116
132
  end
117
133
  end
118
134
  end
119
-
135
+
120
136
 
121
137
  ### FSM keywords: state, transitions_for ###
122
138
 
@@ -200,7 +216,7 @@ module SimpleFSM
200
216
 
201
217
  ## private class methods ######################
202
218
 
203
- # add transition to state's transitions if it does not exist
219
+ # Add transition to state's transitions if it does not exist
204
220
  def self.add_transition st, t
205
221
  if !@@transitions[st].any? {|v| v == t}
206
222
  @@transitions[st] << t
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplefsm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Edin Pjanic
@@ -9,7 +10,7 @@ authors:
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2013-02-15 00:00:00.000000000 Z
13
+ date: 2013-03-22 00:00:00.000000000 Z
13
14
  dependencies: []
14
15
  description: A simple and lightweight domain specific language (DSL) for modeling finite state machines (FSM).
15
16
  email:
@@ -22,7 +23,6 @@ files:
22
23
  - lib/simplefsm.rb
23
24
  homepage: http://github.com/edictlab/SimpleFSM
24
25
  licenses: []
25
- metadata: {}
26
26
  post_install_message:
27
27
  rdoc_options: []
28
28
  require_paths:
@@ -31,16 +31,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: '0'
34
+ version: !binary |-
35
+ MA==
36
+ none: false
35
37
  required_rubygems_version: !ruby/object:Gem::Requirement
36
38
  requirements:
37
39
  - - ">="
38
40
  - !ruby/object:Gem::Version
39
- version: '0'
41
+ version: !binary |-
42
+ MA==
43
+ none: false
40
44
  requirements: []
41
45
  rubyforge_project:
42
- rubygems_version: 2.0.3
46
+ rubygems_version: 1.8.24
43
47
  signing_key:
44
- specification_version: 4
48
+ specification_version: 3
45
49
  summary: SimpleFSM - a Ruby DSL for FSMs
46
50
  test_files: []
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: !binary |-
4
- NzAxMzU4MGU4YWZjMzZjOGI5MWZkNTU2ZjI2MzFiYzgyZWRkZjkyOA==
5
- data.tar.gz: !binary |-
6
- YzAyODkyYjFiZWRkOWUwMWI3MGZkOTliMmI3Yzk3NmY1ZDRjNmRjZg==
7
- SHA512:
8
- metadata.gz: !binary |-
9
- OTE0MmRiNmUwZTc3NjgyZTdkYWJlZjNhOTEzZmNhZmE3ZGFhMTExMDBiNDAy
10
- OTM0M2Y5Y2VkNDhlNGIyMWJmMjVjYmMxNGIwNGIxY2U1Y2ZkYjk0ZDE1N2U4
11
- MmE0OTM0ZWVhZmYwZWZkNWI3YWQyMWNhY2U0MjgzNmQxOTE5ZWY=
12
- data.tar.gz: !binary |-
13
- N2Q4MTFiOTBmNDBkZDlmNTYwZTVkYWU2ZWI4N2VmZWE3NWU3NTdhY2MzMTJh
14
- MGNmZjdlZmE0Yzc5NjkxOGQ3ZjkzNDI2MDBiODUxZjFkMjBjYTA1ZTk1OTIx
15
- NjQyZDg2NWVjODlkYTZkMGQwNGI3ODZiMmQwZWY4NWY2M2Q5ZTA=