Class: AASMExport
- Inherits:
-
Object
- Object
- AASMExport
- Defined in:
- lib/lucidMachines/aasm_export.rb
Constant Summary collapse
- EventCallbacks =
["before", "guards", "guard", "before_enter", "enter", "before_success", "success", "after", "error", "ensure"]
- GlobalEventCallbacks =
["before_all_events", "after_all_events", "error_on_all_events", "ensure_on_all_events"]
- StateCallbacks =
["before_exit", "exit", "before_enter", "enter", "after_exit", "after_enter"]
- TransitionCallbacks =
["guard", "guards", "after", "success"]
- GlobalTransitionCallbacks =
["after_all_transitions"]
Instance Attribute Summary collapse
-
#method_names ⇒ Object
readonly
Returns the value of attribute method_names.
-
#transitions ⇒ Object
readonly
Returns the value of attribute transitions.
Instance Method Summary collapse
- #create ⇒ Object
- #create_multi_events ⇒ Object
-
#export ⇒ Object
Export the document to code (String) It exports only the aasm code, not the methods called by it.
- #generate_attributes_code ⇒ Object
- #generate_callback(event, element_type, callbacks) ⇒ Object
- #generate_code_callback(event, callbacks) ⇒ Object
- #generate_events_code ⇒ Object
-
#generate_methods_placeholder ⇒ Object
Generate code that creates methods for each method declared in the graph.
-
#generate_methods_placeholder_for(klass, debug: true) ⇒ Object
Generate methods that are not already Defined in the class.
- #generate_state_code ⇒ Object
- #init_method_names ⇒ Object
-
#initialize(chart) ⇒ AASMExport
constructor
A new instance of AASMExport.
- #print_warning(event, from_states, to_state) ⇒ Object
- #sanitize(name) ⇒ Object
- #transition_code(event, inputs, output) ⇒ Object
Constructor Details
#initialize(chart) ⇒ AASMExport
Returns a new instance of AASMExport.
24 25 26 27 28 29 |
# File 'lib/lucidMachines/aasm_export.rb', line 24 def initialize(chart) @chart = chart init_method_names create @spaces = " " end |
Instance Attribute Details
#method_names ⇒ Object (readonly)
Returns the value of attribute method_names.
22 23 24 |
# File 'lib/lucidMachines/aasm_export.rb', line 22 def method_names @method_names end |
#transitions ⇒ Object (readonly)
Returns the value of attribute transitions.
62 63 64 |
# File 'lib/lucidMachines/aasm_export.rb', line 62 def transitions @transitions end |
Instance Method Details
#create ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/lucidMachines/aasm_export.rb', line 47 def create @states = @chart.get_blocks_of_type "state" @states = @states + @chart.get_blocks_of_type("Process") # puts "States count: " + @states.size.to_s @events = @chart.get_blocks_of_type "event" @events = @events + @chart.get_blocks_of_type("Decision") # puts "Events count: " + @events.size.to_s ## Arrows between states are also events ## handled in generate_events_code create_multi_events end |
#create_multi_events ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/lucidMachines/aasm_export.rb', line 64 def create_multi_events transitions = @chart.get_blocks_of_type("transition") + @chart.get_blocks_of_type("Summing Junction") # puts transitions.size.to_s + " found" @transitions = {} # Hash with events as keys ## Values: Array of [from_state, to_state] ## ex: { event1 => [[[state1], state2], [[state1, state4], state3]] } # puts "raw transitions found " + transitions.size.to_s ## select the valid one transitions.each do |t| # Link from a block. # and link to a block. # and link from or to an event. from_states = t.links_from_type("state") + t.links_from_type("Process") to_state = t.links_to_type("state") + t.links_to_type("Process") event = t.links_to_type("event") + t.links_from_type("event") event = event + t.links_to_type("Decision") + t.links_from_type("Decision") priority = 0 t.links_from.each do |l, e| if(l.text.to_i != 0) priority = l.text.to_i end end ## Remove possible duplicate links... from_states.uniq! to_state.uniq! event.uniq! if from_states.size < 1 or to_state.size != 1 or event.size != 1 print_warning(event, from_states, to_state) next end event = event.first to_state = to_state.first # puts "Adding, from " + from_states.first.text + " to " + to_state.text + " through " + event.text ## get the ID from the link. # Find the priority all_links = t.links_to.merge(t.links_from) prio = 0 from_states.each do |s| link = all_links.key(to_state) next if link.nil? or link.text.nil? link_prio = link.to_i prio = link_prio if link_prio > prio end # puts "Priority found " + prio.to_s if prio != 0 @transitions[event] = [] if @transitions[event].nil? @transitions[event] << [from_states, to_state, priority, t] end # puts "Transitions found " + @transitions.size.to_s end |
#export ⇒ Object
Export the document to code (String) It exports only the aasm code, not the methods called by it.
399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/lucidMachines/aasm_export.rb', line 399 def export start = <<-START #{@spaces}aasm do START finish = <<-END #{@spaces}end END return generate_attributes_code + start + generate_state_code + generate_events_code + finish # generate_methods_placeholder end |
#generate_attributes_code ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/lucidMachines/aasm_export.rb', line 168 def generate_attributes_code attributes_code = "" attributes_code = <<-INIT #{@spaces}def event_attributes #{@spaces*2}attributes = {} INIT events = @chart.get_blocks_of_type "event" events.each do |event| if event.has_links? attributes = event.links_from_type("attribute").map do |attr| attr.text end unless attributes.empty? c = <<-START #{@spaces*3}attributes["#{event.text}"] = #{attributes.to_s} START attributes_code = attributes_code + c # attributes_code = attributes_code + end end end attributes_code + "#{@spaces*2}return attributes\n#{@spaces}end\n\n" end |
#generate_callback(event, element_type, callbacks) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/lucidMachines/aasm_export.rb', line 307 def generate_callback(event, element_type, callbacks) ## TODO: Handle the Guards -> create an array l1 = event.links_to.each.select{|link, e| e.type.eql? element_type } l2 = event.links_from.each.select{|link, e| e.type.eql? element_type } links = l1 + l2 code = "" unless links.empty? links.each do |l| link = l[0] element = l[1] if(callbacks.any? link.text) code = code + ", :" + sanitize(link.text) + " => :" + sanitize(element.text) @method_names << element.text end end end return code end |
#generate_code_callback(event, callbacks) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/lucidMachines/aasm_export.rb', line 330 def generate_code_callback(event, callbacks) l1 = event.links_to.each.select{|link, e| e.type.eql? "code" } l2 = event.links_from.each.select{|link, e| e.type.eql? "code" } links = l1 + l2 code = "" unless links.empty? links.each do |l| link = l[0] element = l[1] if(callbacks.any? link.text) code = code + " " + link.text + " do" if link.text == "error" code = code + " |e|\n" else code = code + "\n" end indent = " " element.text.split("\n").each do |t| code = code + indent + t end code = code + "\n end\n" end end end return code end |
#generate_events_code ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/lucidMachines/aasm_export.rb', line 204 def generate_events_code events_code = "" ## Arrows between two blocks are events. @chart.links.each_value do |link| if (@chart.blocks[link.source].type == "state" or @chart.blocks[link.source].type == "process") and (@chart.blocks[link.destination].type == "state" or @chart.blocks[link.destination].type == "process") and not link.text.nil? name = sanitize(link.text) code = <<-SMALL_EVENT #{@spaces*2}event :#{name} do #{@spaces*3}transitions :from => :#{@chart.blocks[link.source].text}, :to => :#{@chart.blocks[link.destination].text} #{@spaces*2}end SMALL_EVENT events_code = events_code + code end end @events.each do |event| code = @spaces*2 + "event :" + sanitize(event.text) ## Add event :before :after... callback_code = generate_callback(event, "method", EventCallbacks) code = code + callback_code + " do \n" ## Add the code if it is defined. code = code + generate_code_callback(event, EventCallbacks) ## Simple case: This event links from and to a state inputs = event.links_from_type "state" outputs = event.links_to_type "state" inputs = inputs + event.links_from_type("Process") outputs = outputs + event.links_to_type("Process") if inputs.size > 0 and outputs.size == 1 transition = transition_code(event, inputs, outputs.first) code = code + transition + "\n" end unless @transitions[event].nil? ## Sort by ID if present @sorted = @transitions[event].each.sort do |tr| input, output, priority = tr priority end @sorted.each do |t| inputs, output, priority, t_element = t transition = transition_code(event, inputs, output) transition_callback = generate_callback(t_element, "method", TransitionCallbacks) code = code + transition + transition_callback + "\n" end end code = code + @spaces*2 + "end\n\n" events_code = events_code + code end events_code end |
#generate_methods_placeholder ⇒ Object
Generate code that creates methods for each method declared in the graph.
363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/lucidMachines/aasm_export.rb', line 363 def generate_methods_placeholder code = "\n" method_names().each do |method_name| code = code + <<-METH #{@spaces}def #{method_name} #{@spaces*2}puts "Please implement the method #{method_name}" #{@spaces*2}true #{@spaces}end METH end return code end |
#generate_methods_placeholder_for(klass, debug: true) ⇒ Object
Generate methods that are not already Defined in the class.
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/lucidMachines/aasm_export.rb', line 380 def generate_methods_placeholder_for(klass, debug: true) method_names().each do |method_name| if klass.method_defined? method_name.to_sym puts "Found: #{method_name}. " if debug next end puts "To implement: #{method_name}. " if debug klass.define_method method_name.to_sym do puts "Please implement the method #{method_name}" true end end end |
#generate_state_code ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/lucidMachines/aasm_export.rb', line 156 def generate_state_code states_code = "" @states.each do |state| code = @spaces*2 + "state :" + sanitize(state.text) code = code + ", :initial => true" if state.initial callback_code = generate_callback(state, "method", StateCallbacks) states_code = states_code + code + callback_code + "\n" end states_code + "\n" end |
#init_method_names ⇒ Object
31 32 33 |
# File 'lib/lucidMachines/aasm_export.rb', line 31 def init_method_names @method_names = [] end |
#print_warning(event, from_states, to_state) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/lucidMachines/aasm_export.rb', line 132 def print_warning(event, from_states, to_state) if(event.size != 1) puts "Warning: Transition without any event !" if event.size == 0 if event.size > 1 puts "Warning: Transition with multiple events !" event.each do |e| puts " - Event: " + e.to_s end end end if(to_state != 1) puts "Warning: Transition without output !" if to_state.size == 0 puts "Warning: Transition with multiple outputs !" if to_state.size > 1 end if(from_states.size < 1) puts "Warning: Transition without input !" if from_states.size == 0 end end |
#sanitize(name) ⇒ Object
43 44 45 |
# File 'lib/lucidMachines/aasm_export.rb', line 43 def sanitize(name) name.strip.tr " ", "_" end |
#transition_code(event, inputs, output) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/lucidMachines/aasm_export.rb', line 281 def transition_code(event, inputs, output) code = " transitions :from => " if inputs.size == 1 code = code + ":" + sanitize(inputs[0].text) else code = code + "[" inputs.each do |input| code = code + ":" + sanitize(input.text) + "," end ## remove last "," code = code.chop + "]" end code = code + ", :to => :" + sanitize(output.text) ## TODO: add options # TransitionCallbacks ## Add event :before :after... METHOD out of this ## Not yet testable # callback_code = generate_callback(event, "method", TransitionCallbacks) callback_code = "" return code + callback_code end |