Class: WEEL::DSLRealization

Inherits:
Object show all
Defined in:
lib/weel.rb

Overview

}}}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSLRealization

{{{



334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/weel.rb', line 334

def  initialize #{{{
  @__weel_search_positions = {}
  @__weel_positions = Array.new
  @__weel_main = nil
  @__weel_data ||= Hash.new
  @__weel_endpoints ||= Hash.new
  @__weel_handlerwrapper = HandlerWrapperBase
  @__weel_handlerwrapper_args = []
  @__weel_state = :ready
  @__weel_status = Status.new(0,"undefined")
  @__weel_sim = -1
end

Instance Attribute Details

#__weel_dataObject

}}}



346
347
348
# File 'lib/weel.rb', line 346

def __weel_data
  @__weel_data
end

#__weel_endpointsObject

}}}



346
347
348
# File 'lib/weel.rb', line 346

def __weel_endpoints
  @__weel_endpoints
end

#__weel_handlerwrapperObject

}}}



346
347
348
# File 'lib/weel.rb', line 346

def __weel_handlerwrapper
  @__weel_handlerwrapper
end

#__weel_handlerwrapper_argsObject

}}}



346
347
348
# File 'lib/weel.rb', line 346

def __weel_handlerwrapper_args
  @__weel_handlerwrapper_args
end

#__weel_mainObject

}}}



346
347
348
# File 'lib/weel.rb', line 346

def __weel_main
  @__weel_main
end

#__weel_positionsObject

}}}



346
347
348
# File 'lib/weel.rb', line 346

def __weel_positions
  @__weel_positions
end

#__weel_search_positionsObject

}}}



346
347
348
# File 'lib/weel.rb', line 346

def __weel_search_positions
  @__weel_search_positions
end

#__weel_stateObject

Returns the value of attribute __weel_state.



347
348
349
# File 'lib/weel.rb', line 347

def __weel_state
  @__weel_state
end

#__weel_statusObject (readonly)

Returns the value of attribute __weel_status.



347
348
349
# File 'lib/weel.rb', line 347

def __weel_status
  @__weel_status
end

Instance Method Details

#__weel_finalizeObject

{{{



898
899
900
901
902
# File 'lib/weel.rb', line 898

def __weel_finalize #{{{
  __weel_recursive_join(@__weel_main)
  @__weel_state = :stopped
  @__weel_handlerwrapper::inform_state_change @__weel_handlerwrapper_args, @__weel_state
end

#alternative(condition, args = {}) ⇒ Object

Defines a possible choice of a choose-Construct Block is executed if condition == true or searchmode is active (to find the starting position)



493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/weel.rb', line 493

def alternative(condition,args={})# {{{
  return if self.__weel_state == :stopping || self.__weel_state == :finishing || self.__weel_state == :stopped || Thread.current[:nolongernecessary]
  hw, pos = __weel_sim_start(:alternative,args.merge(:mode => Thread.current[:alternative_mode].last, :condition => ((condition.is_a?(String) || condition.is_a?(Proc)) ? condition : nil))) if __weel_sim
  Thread.current[:mutex] ||= Mutex.new
  Thread.current[:mutex].synchronize do
    return if Thread.current[:alternative_mode][-1] == :exclusive && Thread.current[:alternative_executed][-1] == true
    if (condition.is_a?(String) || condition.is_a?(Proc)) && !__weel_sim
      condition = __weel_eval_condition(condition)
    end
    Thread.current[:alternative_executed][-1] = true if condition
  end
  __weel_protect_yield(&Proc.new) if __weel_is_in_search_mode || __weel_sim || condition
  __weel_sim_stop(:alternative,hw,pos,args.merge(:mode => Thread.current[:alternative_mode].last, :condition => ((condition.is_a?(String) || condition.is_a?(Proc)) ? condition : nil))) if __weel_sim
end

#call(position, endpoint, parameters: {}, finalize: nil, update: nil, &finalizeblk) ⇒ Object

DSL-Constructs for atomic calls to external services (calls) and pure context manipulations (manipulate). Calls can also manipulate context (after the invoking the external services) position: a unique identifier within the wf-description (may be used by the search to identify a starting point) endpoint: (only with :call) ep of the service parameters: (only with :call) service parameters



354
355
356
# File 'lib/weel.rb', line 354

def call(position, endpoint, parameters: {}, finalize: nil, update: nil, &finalizeblk) #{{{
  __weel_activity(position,:call,endpoint,parameters,finalize||finalizeblk,update)
end

#choose(mode = :inclusive) ⇒ Object

Choose DSL-Construct Defines a choice in the Workflow path. May contain multiple execution alternatives



476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/weel.rb', line 476

def choose(mode=:inclusive) # {{{
  return if self.__weel_state == :stopping || self.__weel_state == :finishing || self.__weel_state == :stopped || Thread.current[:nolongernecessary]
  Thread.current[:alternative_executed] ||= []
  Thread.current[:alternative_mode] ||= []
  Thread.current[:alternative_executed] << false
  Thread.current[:alternative_mode] << mode
  hw, pos = __weel_sim_start(:choose,:mode => Thread.current[:alternative_mode].last) if __weel_sim
  __weel_protect_yield(&Proc.new)
  __weel_sim_stop(:choose,hw,pos,:mode => Thread.current[:alternative_mode].last) if __weel_sim
  Thread.current[:alternative_executed].pop
  Thread.current[:alternative_mode].pop
  nil
end

#critical(id) ⇒ Object

Defines a critical block (=Mutex)



515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/weel.rb', line 515

def critical(id)# {{{
  @__weel_critical ||= Mutex.new
  semaphore = nil
  @__weel_critical.synchronize do
    @__weel_critical_sections ||= {}
    semaphore = @__weel_critical_sections[id] ? @__weel_critical_sections[id] : Mutex.new
    @__weel_critical_sections[id] = semaphore if id
  end
  semaphore.synchronize do
    __weel_protect_yield(&Proc.new)
  end
end

#dataObject

}}}



597
598
599
# File 'lib/weel.rb', line 597

def data # {{{
  ReadHash.new(@__weel_data,__weel_sim)
end

#endpointsObject

}}}



600
601
602
# File 'lib/weel.rb', line 600

def endpoints # {{{
  ReadHash.new(@__weel_endpoints)
end

#escapeObject

}}}



579
580
581
582
# File 'lib/weel.rb', line 579

def escape #{{{
  return if __weel_is_in_search_mode
  throw :escape
end

#loop(condition, args = {}) ⇒ Object

Defines a Cycle (loop/iteration)



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/weel.rb', line 529

def loop(condition,args={})# {{{
  unless condition.is_a?(Array) && (condition[0].is_a?(Proc) || condition[0].is_a?(String)) && [:pre_test,:post_test].include?(condition[1]) && args.is_a?(Hash)
    raise "condition must be called pre_test{} or post_test{}"
  end
  return if self.__weel_state == :stopping || self.__weel_state == :finishing || self.__weel_state == :stopped || Thread.current[:nolongernecessary]
  if __weel_is_in_search_mode
    catch :escape do
      __weel_protect_yield(&Proc.new)
    end
    if __weel_is_in_search_mode
      return
    else
      ### in case it was a :post_test we wake inside the loop so we can check
      ### condition first thing
      condition[1] = :pre_test
    end
  end
  if __weel_sim
    cond = condition[0].is_a?(Proc) ? true : condition[0]
    hw, pos = __weel_sim_start(:loop,args.merge(:testing=>condition[1],:condition=>cond))
    catch :escape do
      __weel_protect_yield(&Proc.new)
    end
    __weel_sim_stop(:loop,hw,pos,args.merge(:testing=>condition[1],:condition=>cond))
    return
  end
  catch :escape do
    case condition[1]
      when :pre_test
        while __weel_eval_condition(condition[0]) && self.__weel_state != :stopping && self.__weel_state != :stopped && self.__weel_state != :finishing
          __weel_protect_yield(&Proc.new)
        end
      when :post_test
        begin
          __weel_protect_yield(&Proc.new)
        end while __weel_eval_condition(condition[0]) && self.__weel_state != :stopping && self.__weel_state != :stopped && self.__weel_state != :finishing
    end
  end
end

#manipulate(position, parameters = nil, script = nil, &scriptblk) ⇒ Object

when two params, second param always script when block and two params, parameters stays



359
360
361
362
363
364
# File 'lib/weel.rb', line 359

def manipulate(position, parameters=nil, script=nil, &scriptblk) #{{{
  if scriptblk.nil? && script.nil? && !parameters.nil?
    script, parameters = parameters, nil
  end
  __weel_activity(position,:manipulate,nil,parameters||{},script||scriptblk)
end

#otherwise(args = {}) ⇒ Object

}}}



507
508
509
510
511
512
# File 'lib/weel.rb', line 507

def otherwise(args={}) # {{{
  return if self.__weel_state == :stopping || self.__weel_state == :finishing || self.__weel_state == :stopped || Thread.current[:nolongernecessary]
  hw, pos = __weel_sim_start(:otherwise,args.merge(:mode => Thread.current[:alternative_mode].last)) if __weel_sim
  __weel_protect_yield(&Proc.new) if __weel_is_in_search_mode || __weel_sim || !Thread.current[:alternative_executed].last
  __weel_sim_stop(:otherwise,hw,pos,args.merge(:mode => Thread.current[:alternative_mode].last)) if __weel_sim
end

#parallel(type = nil) ⇒ Object

Parallel DSL-Construct Defines Workflow paths that can be executed parallel. May contain multiple branches (parallel_branch)



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/weel.rb', line 369

def parallel(type=nil)# {{{
  return if self.__weel_state == :stopping || self.__weel_state == :finishing || self.__weel_state == :stopped || Thread.current[:nolongernecessary]

  Thread.current[:branches] = []
  Thread.current[:branch_finished_count] = 0
  Thread.current[:branch_event] = Continue.new
  Thread.current[:mutex] = Mutex.new

  hw, pos = __weel_sim_start(:parallel) if __weel_sim

  __weel_protect_yield(&Proc.new)

  Thread.current[:branch_wait_count] = (type.is_a?(Hash) && type.size == 1 && type[:wait] != nil && (type[:wait].is_a?(Integer) && type[:wait] > 0) ? type[:wait] : Thread.current[:branches].size)
  1.upto Thread.current[:branches].size do
    Thread.current[:branch_event].wait
  end

  Thread.current[:branches].each do |thread|
    # decide after executing block in parallel cause for coopis
    # it goes out of search mode while dynamically counting branches
    if Thread.current[:branch_search] == false
      thread[:branch_search] = false
    end
    thread[:start_event].continue
  end

  Thread.current[:branch_event].wait

  __weel_sim_stop(:parallel,hw,pos) if __weel_sim

  unless self.__weel_state == :stopping || self.__weel_state == :finishing || self.__weel_state == :stopped
    # first set all to no_longer_neccessary
    Thread.current[:branches].each do |thread|
      if thread.alive?
        thread[:nolongernecessary] = true
        __weel_recursive_continue(thread)
      end
    end
    # wait for all
    Thread.current[:branches].each do |thread|
      __weel_recursive_join(thread)
    end
  end
end

#parallel_branch(*vars) ⇒ Object

Defines a branch of a parallel-Construct



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/weel.rb', line 415

def parallel_branch(*vars)# {{{
  return if self.__weel_state == :stopping || self.__weel_state == :finishing || self.__weel_state == :stopped || Thread.current[:nolongernecessary]
  branch_parent = Thread.current

  if __weel_sim
    # catch the potential execution in loops inside a parallel
    current_branch_sim_pos = branch_parent[:branch_sim_pos]
  end

  Thread.current[:branches] << Thread.new(*vars) do |*local|
    Thread.current.abort_on_exception = true
    Thread.current[:branch_status] = false
    Thread.current[:branch_parent] = branch_parent
    Thread.current[:start_event] = Continue.new

    if __weel_sim
      Thread.current[:branch_sim_pos] = @__weel_sim += 1
    end

    # parallel_branch could be possibly around an alternative. Thus thread has to inherit the alternative_executed
    # after branching, update it in the parent (TODO)
    if branch_parent[:alternative_executed] && branch_parent[:alternative_executed].length > 0
      Thread.current[:alternative_executed] = [branch_parent[:alternative_executed].last]
      Thread.current[:alternative_mode] = [branch_parent[:alternative_mode].last]
    end
    branch_parent[:branch_event].continue
    Thread.current[:start_event].wait

    if __weel_sim
      handlerwrapper = @__weel_handlerwrapper.new @__weel_handlerwrapper_args
      handlerwrapper.simulate(:parallel_branch,:start,Thread.current[:branch_sim_pos],current_branch_sim_pos)
    end

    __weel_protect_yield(*local, &Proc.new)

    __weel_sim_stop(:parallel_branch,handlerwrapper,current_branch_sim_pos) if __weel_sim

    branch_parent[:mutex].synchronize do
      Thread.current[:branch_status] = true
      branch_parent[:branch_finished_count] += 1
      if branch_parent[:branch_finished_count] == branch_parent[:branch_wait_count] && self.__weel_state != :stopping && self.__weel_state != :finishing
        branch_parent[:branch_event].continue
      end
    end
    if self.__weel_state != :stopping && self.__weel_state != :stopped && self.__weel_state != :finishing
      if Thread.current[:branch_position]
        @__weel_positions.delete Thread.current[:branch_position]
        begin
          ipc = {}
          ipc[:unmark] = [Thread.current[:branch_position]]
          @__weel_handlerwrapper::inform_position_change(@__weel_handlerwrapper_args,ipc)
        end rescue nil
        Thread.current[:branch_position] = nil
      end
    end
  end
end

#post_test(code = nil, &blk) ⇒ Object

}}}



575
576
577
# File 'lib/weel.rb', line 575

def post_test(code=nil,&blk)# {{{
  [code || blk, :post_test]
end

#pre_test(code = nil, &blk) ⇒ Object

}}}



572
573
574
# File 'lib/weel.rb', line 572

def pre_test(code=nil,&blk)# {{{
  [code || blk, :pre_test]
end

#statusObject

}}}



594
595
596
# File 'lib/weel.rb', line 594

def status # {{{
  @__weel_status
end

#stop(position) ⇒ Object

}}}



587
588
589
590
591
592
# File 'lib/weel.rb', line 587

def stop(position) #{{{
  searchmode = __weel_is_in_search_mode(position)
  return if searchmode
  __weel_progress searchmode, position, true
  self.__weel_state = :stopping
end

#terminateObject

}}}



583
584
585
586
# File 'lib/weel.rb', line 583

def terminate #{{{
  return if __weel_is_in_search_mode
  self.__weel_state = :finishing
end

#test(code = nil, &blk) ⇒ Object

}}}



569
570
571
# File 'lib/weel.rb', line 569

def test(code=nil,&blk)# {{{
  code || blk
end