Please disable Adblockers and enable JavaScript for domain CEWebS.cs.univie.ac.at! We have NO ADS, but they may interfere with some of our course material.

Name: patterns/p17+40.rb 
1:
#!/usr/bin/env ruby
2:
require 'rubygems'
3:
require 'pp'
4:
require 'term/ansicolor'
5:
require File.expand_path(File.dirname(__FILE__) + '/../lib/remar')
6:
 
7:
m = REMAR.new.ruleset do
8:
  value :endpoint, 'http://y', :a
9:
  value :endpoint, 'http://b', :b
10:
  value :endpoint, 'http://n', :c
11:
  
12:
  event :before, :instance, :position, :endpoint
13:
  event :after, :instance, :position, :endpoint
14:
  
15:
  context :sync, :buf => {:a=>[],:b=>[],:c=>[]}, :fin => [], :step => :a, :wait => false
16:
  
17:
  call :continue
18:
  
19:
  rule event.before do
20:
    context.sync.buf[event.endpoint] << [event.instance,event.position]
21:
  end  
22:
  
23:
  rule context.sync{buf[:c].length >= 1 && step == :c && wait == false} do
24:
    context.sync.step = :finish
25:
    context.sync.wait = true
26:
    continue context.sync.buf[:c].shift
27:
  end
28:
  rule context.sync{buf[:b].length >= 1 && step == :b && wait == false} do
29:
    context.sync.step = :c
30:
    context.sync.wait = true
31:
    continue context.sync.buf[:b].shift
32:
  end  
33:
  rule context.sync{buf[:a].length >= 1 && step == :a && wait == false} do
34:
    context.sync.step = :b
35:
    context.sync.wait = true
36:
    continue context.sync.buf[:a].shift
37:
  end  
38:
  
39:
  rule event.after do
40:
    context.sync.fin << [event.instance,event.position]
41:
    context.sync.wait = false
42:
  end
43:
  rule context.sync{step == :finish && wait == false} do
44:
    context.sync.step = :a
45:
    context.sync.fin.each do |instance,position|
46:
      continue instance, position
47:
    end
48:
    context.sync.fin.clear
49:
  end
50:
end
51:
 
52:
m.on_continue do |instance,position|
53:
  puts Term::ANSIColor::bold(Term::ANSIColor::red("  Continuing #{instance}, #{position}"))
54:
end  
55:
m.before_push do |evt_name,evt_data,context|
56:
  puts Term::ANSIColor::bold("Pushing #{evt_name} #{evt_data.inspect}")
57:
end
58:
m.after_push do |context|
59:
  puts "  After:\n" + context.pretty_inspect.gsub(/^/,'    |')
60:
end
61:
 
62:
puts Term::ANSIColor::bold("Initial context:")
63:
puts m.context.pretty_inspect.gsub(/^/,'    |')
64:
 
65:
m.push :before, :position => :p, :endpoint => 'http://not_valid', :instance => 'http://localhost:9188/2'
66:
 
67:
m.push :before, :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/2'
68:
m.push :before, :position => :p, :endpoint => 'http://y', :instance => 'http://localhost:9188/1'
69:
m.push :before, :position => :p, :endpoint => 'http://n', :instance => 'http://localhost:9188/3'
70:
 
71:
m.push :after,  :position => :p, :endpoint => 'http://y', :instance => 'http://localhost:9188/1'
72:
m.push :after,  :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/2'
73:
m.push :after,  :position => :p, :endpoint => 'http://n', :instance => 'http://localhost:9188/3'