lucidMachines 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/integration.csv ADDED
@@ -0,0 +1,213 @@
1
+ Id,Name,Shape Library,Page ID,Contained By,Group,Line Source,Line Destination,Source Arrow,Destination Arrow,Text Area 1,Duration,GPS,ID,Nom,STEPS
2
+ 1,Page,,,,,,,,,Page 1,,,,,
3
+ 2,Text,Default,1,,,,,,,"Find file , 5 mins",5,,,,Find file
4
+ 3,Test étapes,Default,1,,,,,,,"Download file , 10 mins",10,,,,Download file
5
+ 4,Test étapes,Default,1,,,,,,,"Make coffee
6
+ , 3 mins",3,,,,"Make coffee
7
+ "
8
+ 5,Test étapes,Default,1,,,,,,,"check download , 2 mins",2,,,,check download
9
+ 6,Test étapes,Default,1,,,,,,,"Clean mug
10
+ , 2 mins",2,,,,"Clean mug
11
+ "
12
+ 7,Test étapes,Default,1,,,,,,,"end , 1 mins",1,,,,end
13
+ 8,Text,Default,1,11:1,,,,,,"ID: 2
14
+ Nom: Stockage1
15
+ GPS: ",,,2,Stockage1,
16
+ 9,Text,Default,1,11:1,,,,,,"ID: 3
17
+ Nom: Stockage2
18
+ GPS: ",,,3,Stockage2,
19
+ 10,Swim Lane,Flowchart Shapes,1,,,,,,,Stockage externe,,,,,
20
+ 11,Swim Lane,Flowchart Shapes,1,,,,,,,Swim Lane,,,,,
21
+ 12,Text,Default,1,10:1,,,,,,"ID: 2
22
+ Nom: Stockage1
23
+ GPS: ",,,2,Stockage1,
24
+ 13,Text,Default,1,10:1,,,,,,Text,,,,,
25
+ 14,Text,Default,1,10:1,,,,,,Text,,,,,
26
+ 15,Line,,1,,,2,3,None,Arrow,,,,,,
27
+ 16,Line,,1,,,3,4,None,Arrow,,,,,,
28
+ 17,Line,,1,,,4,6,None,Arrow,,,,,,
29
+ 18,Line,,1,,,6,7,None,Arrow,,,,,,
30
+ 19,Line,,1,,,3,5,None,Arrow,,,,,,
31
+ 20,Line,,1,,,5,7,None,Arrow,,,,,,
32
+ 21,Page,,,,,,,,,Page 2,,,,,
33
+ 22,SVGPathBlock2,,21,,,,,,,,,,,,
34
+ 23,Text,Default,21,25:1,,,,,,"ID: 2
35
+ Nom: Stockage1
36
+ GPS: ",,,2,Stockage1,
37
+ 24,Text,Default,21,25:1,,,,,,"ID: 3
38
+ Nom: Stockage2
39
+ GPS: ",,,3,Stockage2,
40
+ 25,Swim Lane,Flowchart Shapes,21,,,,,,,Stockage externe,,,,,
41
+ 26,Page,,,,,,,,,Root,,,,,
42
+ 27,Page,,,,,,,,,Financement,,,,,
43
+ 28,Process,Flowchart Shapes,27,,,,,,,Ouverture Financement,,,,,
44
+ 29,Terminator,Flowchart Shapes,27,,,,,,,Financement acquis,,,,,
45
+ 30,Terminator,Flowchart Shapes,27,,,,,,,Financement refusé,,,,,
46
+ 31,Decision,Flowchart Shapes,27,,,,,,,Besoin financement,,,,,
47
+ 32,Process,Flowchart Shapes,27,,,,,,,Recherche financement,,,,,
48
+ 33,Process,Flowchart Shapes,27,,,,,,,Financement bancaire en cours,,,,,
49
+ 34,Decision,Flowchart Shapes,27,,,,,,,accepté?,,,,,
50
+ 35,Process,Flowchart Shapes,27,,,,,,,Proposition crédit,,,,,
51
+ 36,Decision,Flowchart Shapes,27,,,,,,,accepté?,,,,,
52
+ 37,Note,Flowchart Shapes,27,,,,,,,Initial,,,,,
53
+ 38,Process,Flowchart Shapes,27,,,,,,,Attente décision,,,,,
54
+ 39,Line,,27,,,31,32,None,Arrow,Yes,,,,,
55
+ 40,Line,,27,,,31,29,None,Arrow,No,,,,,
56
+ 41,Line,,27,,,28,31,None,Arrow,,,,,,
57
+ 42,Line,,27,,,32,33,None,Arrow,,,,,,
58
+ 43,Line,,27,,,33,34,None,Arrow,,,,,,
59
+ 44,Line,,27,,,34,29,None,Arrow,Yes,,,,,
60
+ 45,Line,,27,,,34,30,None,Arrow,No,,,,,
61
+ 46,Line,,27,,,32,35,None,Arrow,,,,,,
62
+ 47,Line,,27,,,35,36,None,Arrow,,,,,,
63
+ 48,Line,,27,,,36,29,None,Arrow,Yes,,,,,
64
+ 49,Line,,27,,,36,30,None,Arrow,No,,,,,
65
+ 50,Line,,27,,,32,32,None,Arrow,Vérification solvabilité,,,,,
66
+ 51,Line,,27,,,37,28,None,Arrow,,,,,,
67
+ 52,Line,,27,,,35,38,Open Arrow,Hollow Arrow,,,,,,
68
+ 53,Page,,,,,,,,,state machine def,,,,,
69
+ 54,event,Flowchart Shapes,53,,,,,,,event,,,,,
70
+ 55,method,Flowchart Shapes,53,,,,,,,method call,,,,,
71
+ 56,method,Flowchart Shapes,53,,,,,,,method call,,,,,
72
+ 57,method,Flowchart Shapes,53,,,,,,,method call,,,,,
73
+ 58,method,Flowchart Shapes,53,,,,,,,method call,,,,,
74
+ 59,method,Flowchart Shapes,53,,,,,,,method call,,,,,
75
+ 60,state,Flowchart Shapes,53,,,,,,,state,,,,,
76
+ 61,method,Flowchart Shapes,53,,,,,,,method call,,,,,
77
+ 62,code,Flowchart Shapes,53,,,,,,,code,,,,,
78
+ 63,state,Flowchart Shapes,53,,,,,,,state,,,,,
79
+ 64,event,Flowchart Shapes,53,,,,,,,event1,,,,,
80
+ 65,state,Flowchart Shapes,53,,,,,,,state,,,,,
81
+ 66,method,Flowchart Shapes,53,,,,,,,method call,,,,,
82
+ 67,state,Flowchart Shapes,53,,,,,,,state,,,,,
83
+ 68,event,Flowchart Shapes,53,,,,,,,event1,,,,,
84
+ 69,state,Flowchart Shapes,53,,,,,,,state,,,,,
85
+ 70,method,Flowchart Shapes,53,,,,,,,method call,,,,,
86
+ 71,method,Flowchart Shapes,53,,,,,,,method call,,,,,
87
+ 72,method,Flowchart Shapes,53,,,,,,,method call,,,,,
88
+ 73,method,Flowchart Shapes,53,,,,,,,method call,,,,,
89
+ 74,method,Flowchart Shapes,53,,,,,,,method call,,,,,
90
+ 75,method,Flowchart Shapes,53,,,,,,,method call,,,,,
91
+ 76,method,Flowchart Shapes,53,,,,,,,method call,,,,,
92
+ 77,Process,Flowchart Shapes,53,,,,,,,state,,,,,
93
+ 78,Note,Flowchart Shapes,53,,,,,,,Initial,,,,,
94
+ 79,method,Flowchart Shapes,53,,,,,,,method call,,,,,
95
+ 80,transition,Flowchart Shapes,53,,,,,,,,,,,,
96
+ 81,Line,,53,,,55,54,None,Arrow,before,,,,,
97
+ 82,Line,,53,,,54,56,None,Arrow,after,,,,,
98
+ 83,Line,,53,,,54,57,None,Arrow,error,,,,,
99
+ 84,Line,,53,,,54,58,None,Arrow,success,,,,,
100
+ 85,Line,,53,,,59,60,None,Arrow,before_enter,,,,,
101
+ 86,Line,,53,,,61,54,None,Arrow,guard,,,,,
102
+ 87,Line,,53,,,62,60,None,Arrow,before_enter,,,,,
103
+ 88,Line,,53,,,63,80,None,Arrow,1,,,,,
104
+ 89,Line,,53,,,80,65,None,Arrow,,,,,,
105
+ 90,Line,,53,,,64,80,None,Arrow,,,,,,
106
+ 91,Line,,53,,,66,80,None,Arrow,guard,,,,,
107
+ 92,Line,,53,,,67,80,None,Arrow,1,,,,,
108
+ 93,Line,,53,,,80,69,None,Arrow,,,,,,
109
+ 94,Line,,53,,,68,80,None,Arrow,,,,,,
110
+ 95,Line,,53,,,70,80,None,Arrow,after,,,,,
111
+ 96,Line,,53,,,71,80,None,Arrow,success,,,,,
112
+ 97,Line,,53,,,72,72,None,Arrow,after_all_transitions,,,,,
113
+ 98,Line,,53,,,73,73,None,Arrow,before_all_events,,,,,
114
+ 99,Line,,53,,,74,74,None,Arrow,after_all_events,,,,,
115
+ 100,Line,,53,,,54,75,None,Arrow,before_success,,,,,
116
+ 101,Line,,53,,,76,76,None,Arrow,error_on_all_events,,,,,
117
+ 102,Line,,53,,,78,77,None,Arrow,,,,,,
118
+ 103,Line,,53,,,79,79,None,Arrow,ensure_on_all_events,,,,,
119
+ 104,Page,,,,,,,,,ex1,,,,,
120
+ 105,Note,Flowchart Shapes,104,,,,,,,Initial,,,,,
121
+ 106,state,Flowchart Shapes,104,,,,,,,running,,,,,
122
+ 107,state,Flowchart Shapes,104,,,,,,,cleaning,,,,,
123
+ 108,event,Flowchart Shapes,104,,,,,,,run,,,,,
124
+ 109,event,Flowchart Shapes,104,,,,,,,clean,,,,,
125
+ 110,event,Flowchart Shapes,104,,,,,,,sleep,,,,,
126
+ 111,state,Flowchart Shapes,104,,,,,,,sleeping,,,,,
127
+ 112,method,Flowchart Shapes,104,,,,,,,tie_shoes,,,,,
128
+ 113,method,Flowchart Shapes,104,,,,,,,check_pillow,,,,,
129
+ 114,code,Flowchart Shapes,104,,,,,,,"puts(""preparing to sleep"")",,,,,
130
+ 115,Line,,104,,,105,111,None,Arrow,,,,,,
131
+ 116,Line,,104,,,111,108,None,Arrow,,,,,,
132
+ 117,Line,,104,,,108,106,None,Arrow,,,,,,
133
+ 118,Line,,104,,,109,107,None,Arrow,,,,,,
134
+ 119,Line,,104,,,110,111,None,Arrow,,,,,,
135
+ 120,Line,,104,,,106,109,None,Arrow,,,,,,
136
+ 121,Line,,104,,,106,110,None,Arrow,,,,,,
137
+ 122,Line,,104,,,107,110,None,Arrow,,,,,,
138
+ 123,Line,,104,,,112,108,None,Arrow,before,,,,,
139
+ 124,Line,,104,,,113,111,None,Arrow,before_enter,,,,,
140
+ 125,Line,,104,,,114,110,None,Arrow,before,,,,,
141
+ 126,Page,,,,,,,,,ex2,,,,,
142
+ 127,Note,Flowchart Shapes,126,,,,,,,Initial,,,,,
143
+ 128,state,Flowchart Shapes,126,,,,,,,running,,,,,
144
+ 129,event,Flowchart Shapes,126,,,,,,,run,,,,,
145
+ 130,method,Flowchart Shapes,126,,,,,,,log_status_change,,,,,
146
+ 131,method,Flowchart Shapes,126,,,,,,,do_something,,,,,
147
+ 132,method,Flowchart Shapes,126,,,,,,,notify_somebody,,,,,
148
+ 133,code,Flowchart Shapes,126,,,,,,,"puts(""preparing to run"")",,,,,
149
+ 134,state,Flowchart Shapes,126,,,,,,,finished,,,,,
150
+ 135,event,Flowchart Shapes,126,,,,,,,sleep,,,,,
151
+ 136,code,Flowchart Shapes,126,,,,,,,after do 
...
end 
error do |e| 
....
end,,,,,
152
+ 137,method,Flowchart Shapes,126,,,,,,,method1,,,,,
153
+ 138,method,Flowchart Shapes,126,,,,,,,method2,,,,,
154
+ 139,transition,Flowchart Shapes,126,,,,,,,,,,,,
155
+ 140,transition,Flowchart Shapes,126,,,,,,,,,,,,
156
+ 141,state,Flowchart Shapes,126,,,,,,,sleeping,,,,,
157
+ 142,Line,,126,,,127,141,None,Arrow,,,,,,
158
+ 143,Line,,126,,,130,130,None,Arrow,after_all_transitions,,,,,
159
+ 144,Line,,126,,,131,141,None,Arrow,before_enter,,,,,
160
+ 145,Line,,126,,,133,129,None,Arrow,before,,,,,
161
+ 146,Line,,126,,,139,128,None,Arrow,,,,,,
162
+ 147,Line,,126,,,139,129,None,Arrow,,,,,,
163
+ 148,Line,,126,,,128,140,None,Arrow,,,,,,
164
+ 149,Line,,126,,,140,134,None,Arrow,,,,,,
165
+ 150,Line,,126,,,140,129,None,Arrow,,,,,,
166
+ 151,Line,,126,,,128,135,None,Arrow,,,,,,
167
+ 152,Line,,126,,,137,135,None,Arrow,after,,,,,
168
+ 153,Line,,126,,,138,135,None,Arrow,error,,,,,
169
+ 154,Line,,126,,,132,129,None,Arrow,after,,,,,
170
+ 155,Line,,126,,,141,139,None,Arrow,,,,,,
171
+ 156,Line,,126,,,135,141,None,Arrow,,,,,,
172
+ 157,Page,,,,,,,,,ex3,,,,,
173
+ 158,Process,Flowchart Shapes,157,,,,,,,idle,,,,,
174
+ 159,Note,Flowchart Shapes,157,,,,,,,Initial,,,,,
175
+ 160,state,Flowchart Shapes,157,,,,,,,cleaning,,,,,
176
+ 161,event,Flowchart Shapes,157,,,,,,,clean,,,,,
177
+ 162,method,Flowchart Shapes,157,,,,,,,cleaning_needed?,,,,,
178
+ 163,event,Flowchart Shapes,157,,,,,,,clean_if_needed,,,,,
179
+ 164,transition,Flowchart Shapes,157,,,,,,,,,,,,
180
+ 165,transition,Flowchart Shapes,157,,,,,,,,,,,,
181
+ 166,event,Flowchart Shapes,157,,,,,,,clean_if_dirty,,,,,
182
+ 167,method,Flowchart Shapes,157,,,,,,,if_dirty?,,,,,
183
+ 168,Line,,157,,,159,158,None,Arrow,,,,,,
184
+ 169,Line,,157,,,158,161,None,Arrow,,,,,,
185
+ 170,Line,,157,,,161,160,None,Arrow,Yes,,,,,
186
+ 171,Line,,157,,,162,161,None,Arrow,guard,,,,,
187
+ 172,Line,,157,,,158,164,None,Arrow,1,,,,,
188
+ 173,Line,,157,,,164,160,None,Arrow,,,,,,
189
+ 174,Line,,157,,,163,164,None,Arrow,,,,,,
190
+ 175,Line,,157,,,158,165,None,Arrow,2,,,,,
191
+ 176,Line,,157,,,165,158,None,Arrow,,,,,,
192
+ 177,Line,,157,,,163,165,None,Arrow,,,,,,
193
+ 178,Line,,157,,,158,166,None,Arrow,,,,,,
194
+ 179,Line,,157,,,166,160,None,Arrow,,,,,,
195
+ 180,Line,,157,,,167,166,None,Arrow,guard,,,,,
196
+ 181,Line,,157,,,162,164,None,Arrow,guard,,,,,
197
+ 182,Page,,,,,,,,,ex4,,,,,
198
+ 183,Process,Flowchart Shapes,182,,,,,,,stage1,,,,,
199
+ 184,Note,Flowchart Shapes,182,,,,,,,Initial,,,,,
200
+ 185,state,Flowchart Shapes,182,,,,,,,stage3,,,,,
201
+ 186,state,Flowchart Shapes,182,,,,,,,2,,,,,
202
+ 187,event,Flowchart Shapes,182,,,,,,,stage1_completed,,,,,
203
+ 188,transition,Flowchart Shapes,182,,,,,,,,,,,,
204
+ 189,transition,Flowchart Shapes,182,,,,,,,,,,,,
205
+ 190,method,Flowchart Shapes,182,,,,,,,stage2_completed?,,,,,
206
+ 191,Line,,182,,,184,183,None,Arrow,,,,,,
207
+ 192,Line,,182,,,187,188,None,Arrow,Yes,,,,,
208
+ 193,Line,,182,,,187,189,None,Arrow,No,,,,,
209
+ 194,Line,,182,,,183,189,None,Arrow,2,,,,,
210
+ 195,Line,,182,,,190,188,None,Arrow,,,,,,
211
+ 196,Line,,182,,,188,185,None,Arrow,,,,,,
212
+ 197,Line,,182,,,189,186,None,Arrow,,,,,,
213
+ 198,Line,,182,,,183,188,None,Arrow,1,,,,,
@@ -0,0 +1,376 @@
1
+ # coding: utf-8
2
+
3
+ class AASMExport
4
+
5
+ EventCallbacks = ["before", "guards", "guard",
6
+ "before_enter", "enter",
7
+ "before_success",
8
+ "success", "after",
9
+ "error", "ensure"]
10
+
11
+ GlobalEventCallbacks = ["before_all_events", "after_all_events",
12
+ "error_on_all_events", "ensure_on_all_events"]
13
+
14
+ StateCallbacks = ["before_exit", "exit",
15
+ "before_enter", "enter",
16
+ "after_exit", "after_enter"]
17
+
18
+ TransitionCallbacks = ["guard", "guards", "after", "success"]
19
+ GlobalTransitionCallbacks = ["after_all_transitions"]
20
+
21
+ attr_reader :method_names
22
+
23
+ def initialize(chart)
24
+ @chart = chart
25
+ init_method_names
26
+ create
27
+ end
28
+
29
+ def init_method_names
30
+ @method_names = []
31
+ end
32
+
33
+ def method_names
34
+ # @method_names = @method_names.uniq
35
+ # return @method_names
36
+
37
+ # unique, no trailing spaces, inside spaces are underscores
38
+ @method_names.uniq.map{|t| sanitize(t)}
39
+ end
40
+
41
+ def sanitize(name)
42
+ name.strip.tr " ", "_"
43
+ end
44
+
45
+ def create
46
+ @states = @chart.get_blocks_of_type "state"
47
+ @states = @states + @chart.get_blocks_of_type("Process")
48
+ # puts "States count: " + @states.size.to_s
49
+
50
+ @events = @chart.get_blocks_of_type "event"
51
+ @events = @events + @chart.get_blocks_of_type("Decision")
52
+ # puts "Events count: " + @events.size.to_s
53
+
54
+ ## Arrows between states are also events
55
+ ## handled in generate_events_code
56
+
57
+ create_multi_events
58
+ end
59
+
60
+ attr_reader :transitions
61
+
62
+ def create_multi_events
63
+ transitions = @chart.get_blocks_of_type("transition") +
64
+ @chart.get_blocks_of_type("Summing Junction")
65
+
66
+ # puts transitions.size.to_s + " found"
67
+
68
+ @transitions = {}
69
+ # Hash with events as keys
70
+ ## Values: Array of [from_state, to_state]
71
+ ## ex: { event1 => [[[state1], state2], [[state1, state4], state3]] }
72
+
73
+ # puts "raw transitions found " + transitions.size.to_s
74
+
75
+ ## select the valid one
76
+ transitions.each do |t|
77
+ # Link from a block.
78
+ # and link to a block.
79
+ # and link from or to an event.
80
+
81
+ from_states = t.links_from_type("state") + t.links_from_type("Process")
82
+ to_state = t.links_to_type("state") + t.links_to_type("Process")
83
+
84
+ event = t.links_to_type("event") + t.links_from_type("event")
85
+ event = event + t.links_to_type("Decision") + t.links_from_type("Decision")
86
+
87
+ priority = 0
88
+ t.links_from.each do |l, e|
89
+ if(l.text.to_i != 0)
90
+ priority = l.text.to_i
91
+ end
92
+ end
93
+
94
+ ## Remove possible duplicate links...
95
+ from_states.uniq!
96
+ to_state.uniq!
97
+ event.uniq!
98
+
99
+ if from_states.size < 1 or to_state.size != 1 or event.size != 1
100
+ print_warning(event, from_states, to_state)
101
+ next
102
+ end
103
+
104
+ event = event.first
105
+ to_state = to_state.first
106
+
107
+ # puts "Adding, from " + from_states.first.text + " to " + to_state.text + " through " + event.text
108
+ ## get the ID from the link.
109
+
110
+ # Find the priority
111
+ all_links = t.links_to.merge(t.links_from)
112
+ prio = 0
113
+ from_states.each do |s|
114
+ link = all_links.key(to_state)
115
+ next if link.nil? or link.text.nil?
116
+ link_prio = link.to_i
117
+ prio = link_prio if link_prio > prio
118
+ end
119
+ # puts "Priority found " + prio.to_s if prio != 0
120
+
121
+ @transitions[event] = [] if @transitions[event].nil?
122
+ @transitions[event] << [from_states, to_state, priority, t]
123
+ end
124
+
125
+ # puts "Transitions found " + @transitions.size.to_s
126
+
127
+ end
128
+
129
+ def print_warning(event, from_states, to_state)
130
+
131
+ if(event.size != 1)
132
+ puts "Warning: Transition without any event !" if event.size == 0
133
+
134
+ if event.size > 1
135
+ puts "Warning: Transition with multiple events !"
136
+ event.each do |e|
137
+ puts " - Event: " + e.to_s
138
+ end
139
+ end
140
+ end
141
+
142
+ if(to_state != 1)
143
+ puts "Warning: Transition without output !" if to_state.size == 0
144
+ puts "Warning: Transition with multiple outputs !" if to_state.size > 1
145
+ end
146
+
147
+ if(from_states.size < 1)
148
+ puts "Warning: Transition without input !" if from_states.size == 0
149
+ end
150
+ end
151
+
152
+
153
+ def generate_state_code
154
+ states_code = ""
155
+ @states.each do |state|
156
+
157
+ code = " state :" + sanitize(state.text)
158
+ code = code + ", :initial => true" if state.initial
159
+
160
+ callback_code = generate_callback(state, "method", StateCallbacks)
161
+
162
+ states_code = states_code + code + callback_code + "\n"
163
+ end
164
+ states_code + "\n"
165
+ end
166
+
167
+
168
+
169
+ def generate_events_code
170
+
171
+ events_code = ""
172
+
173
+ ## Arrows between two blocks are events.
174
+ @chart.links.each_value do |link|
175
+ if (@chart.blocks[link.source].type == "state" or
176
+ @chart.blocks[link.source].type == "process") and
177
+ (@chart.blocks[link.destination].type == "state" or
178
+ @chart.blocks[link.destination].type == "process") and
179
+ not link.text.nil?
180
+
181
+ name = sanitize(link.text)
182
+
183
+ code = <<-SMALL_EVENT
184
+ event :#{name} do
185
+ transitions :from => :#{@chart.blocks[link.source].text}, :to => :#{@chart.blocks[link.destination].text}
186
+ end
187
+
188
+ SMALL_EVENT
189
+
190
+ events_code = events_code + code
191
+ end
192
+ end
193
+
194
+ @events.each do |event|
195
+ code = " event :" + sanitize(event.text)
196
+
197
+ ## Add event :before :after...
198
+ callback_code = generate_callback(event, "method", EventCallbacks)
199
+
200
+ code = code + callback_code + " do \n"
201
+
202
+ ## Add the code if it is defined.
203
+ code = code + generate_code_callback(event, EventCallbacks)
204
+
205
+ ## Simple case: This event links from and to a state
206
+ inputs = event.links_from_type "state"
207
+ outputs = event.links_to_type "state"
208
+
209
+ inputs = inputs + event.links_from_type("Process")
210
+ outputs = outputs + event.links_to_type("Process")
211
+
212
+
213
+ if inputs.size > 0 and outputs.size == 1
214
+ transition = transition_code(event,
215
+ inputs,
216
+ outputs.first)
217
+ code = code + transition + "\n"
218
+
219
+ end
220
+
221
+ unless @transitions[event].nil?
222
+ ## Sort by ID if present
223
+
224
+ @sorted = @transitions[event].each.sort do |tr|
225
+ input, output, priority = tr
226
+ priority
227
+ end
228
+
229
+ @sorted.each do |t|
230
+ inputs, output, priority, t_element = t
231
+ transition = transition_code(event, inputs, output)
232
+ transition_callback = generate_callback(t_element,
233
+ "method",
234
+ TransitionCallbacks)
235
+ code = code + transition + transition_callback + "\n"
236
+ end
237
+ end
238
+
239
+ code = code + " end\n\n"
240
+ events_code = events_code + code
241
+ end
242
+
243
+ events_code
244
+ end
245
+
246
+ def transition_code(event, inputs, output)
247
+
248
+ code = " transitions :from => "
249
+ if inputs.size == 1
250
+ code = code + ":" + sanitize(inputs[0].text)
251
+ else
252
+ code = code + "["
253
+ inputs.each do |input|
254
+ code = code + ":" + sanitize(input.text) + ","
255
+ end
256
+ ## remove last ","
257
+ code = code.chop + "]"
258
+ end
259
+
260
+ code = code + ", :to => :" + sanitize(output.text)
261
+ ## TODO: add options
262
+
263
+ # TransitionCallbacks
264
+ ## Add event :before :after... METHOD out of this
265
+ ## Not yet testable
266
+ # callback_code = generate_callback(event, "method", TransitionCallbacks)
267
+ callback_code = ""
268
+
269
+ return code + callback_code
270
+ end
271
+
272
+ def generate_callback(event, element_type, callbacks)
273
+
274
+ ## TODO: Handle the Guards -> create an array
275
+ l1 = event.links_to.each.select{|link, e| e.type.eql? element_type }
276
+ l2 = event.links_from.each.select{|link, e| e.type.eql? element_type }
277
+ links = l1 + l2
278
+ code = ""
279
+ unless links.empty?
280
+ links.each do |l|
281
+
282
+ link = l[0]
283
+ element = l[1]
284
+ if(callbacks.any? link.text)
285
+ code = code + ", :" + sanitize(link.text) +
286
+ " => :" + sanitize(element.text)
287
+
288
+ @method_names << element.text
289
+ end
290
+ end
291
+ end
292
+ return code
293
+ end
294
+
295
+ def generate_code_callback(event, callbacks)
296
+ l1 = event.links_to.each.select{|link, e| e.type.eql? "code" }
297
+ l2 = event.links_from.each.select{|link, e| e.type.eql? "code" }
298
+ links = l1 + l2
299
+ code = ""
300
+
301
+ unless links.empty?
302
+ links.each do |l|
303
+ link = l[0]
304
+ element = l[1]
305
+
306
+ if(callbacks.any? link.text)
307
+ code = code + " " + link.text + " do"
308
+
309
+ if link.text == "error"
310
+ code = code + " |e|\n"
311
+ else
312
+ code = code + "\n"
313
+ end
314
+ indent = " "
315
+ element.text.split("\n").each do |t|
316
+ code = code + indent + t
317
+ end
318
+
319
+ code = code + "\n end\n"
320
+ end
321
+ end
322
+ end
323
+ return code
324
+ end
325
+
326
+ # Generate code that creates methods for each
327
+ # method declared in the graph.
328
+ def generate_methods_placeholder
329
+ code = "\n"
330
+ method_names().each do |method_name|
331
+
332
+ code = code + <<-METH
333
+ def #{method_name}
334
+ puts "Please implement the method #{method_name}"
335
+ true
336
+ end
337
+
338
+ METH
339
+ end
340
+ return code
341
+ end
342
+
343
+ # Generate methods that are not already
344
+ # Defined in the class.
345
+ def generate_methods_placeholder_for(klass, debug: true)
346
+ method_names().each do |method_name|
347
+ if klass.method_defined? method_name.to_sym
348
+ puts "Found: #{method_name}. " if debug
349
+ next
350
+ end
351
+
352
+ puts "To implement: #{method_name}. " if debug
353
+ klass.define_method method_name.to_sym do
354
+ puts "Please implement the method #{method_name}"
355
+ true
356
+ end
357
+
358
+ end
359
+ end
360
+
361
+ # Export the document to code (String)
362
+ # It exports only the aasm code, not the
363
+ # methods called by it.
364
+ def export
365
+ start = <<-START
366
+ aasm do
367
+ START
368
+
369
+ finish = <<-END
370
+ end
371
+ END
372
+ return start + generate_state_code +
373
+ generate_events_code + finish
374
+ # generate_methods_placeholder
375
+ end
376
+ end