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/p36.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://b', :syn
9:
  value :endpoint, 'http://y', :ctla
10:
  value :endpoint, 'http://z', :ctle
11:
  
12:
  event :before, :instance, :position, :endpoint
13:
  event :after, :instance, :position, :endpoint
14:
  
15:
  context :ctl,  :buf => [], :add => false, :done => true
16:
  context :wait, :buf => []
17:
  context :sync, :run => [], :fin => []
18:
  
19:
  call :continue
20:
  
21:
  #### s1
22:
  rule event.before{endpoint == :syn}, context.ctl{add == false} do
23:
    context.wait.buf << [event.instance, event.position]
24:
  end
25:
  rule event.before{endpoint == :syn}, context.ctl{add == true} do
26:
    context.sync.run << [event.instance, event.position]
27:
    continue event.instance, event.position
28:
  end
29:
  rule context.ctl{buf.length > 0}, context.sync{run.empty?} do
30:
    context.ctl.done = false
31:
    context.ctl.add = true
32:
    context.ctl.buf.shift
33:
    context.sync.run = context.wait.buf.dup
34:
    context.wait.buf.clear
35:
    context.sync.run.each do |instance,position|
36:
      continue instance,position
37:
    end
38:
  end
39:
  rule event.before{endpoint == :ctla} do
40:
    context.ctl.buf << [event.instance, event.position]
41:
  end
42:
 
43:
  #### s2
44:
  rule event.after{endpoint == :ctla} do
45:
    context.ctl.add = false
46:
  end
47:
 
48:
  #### s3
49:
  rule event.after{endpoint == :syn}, context.ctl{done == false} do
50:
    context.sync.fin << [event.instance, event.position]
51:
  end
52:
  rule event.after{endpoint == :syn}, context.ctl{done == true} do
53:
    context.sync.fin << [event.instance, event.position]
54:
    continue event.instance, event.position
55:
  end
56:
  rule event.after{endpoint == :ctle} do
57:
    context.ctl.done = true
58:
    context.sync.fin.each do |instance,position|
59:
      continue instance,position
60:
    end
61:
  end
62:
  rule context.sync{fin.length == run.length} do
63:
    context.sync.fin.clear
64:
    context.sync.run.clear
65:
  end
66:
end
67:
 
68:
m.on_continue do |instance,position|
69:
  puts Term::ANSIColor::bold(Term::ANSIColor::red("  Continuing #{instance}, #{position}"))
70:
end  
71:
 
72:
m.before_push do |evt_name,evt_data,context|
73:
  puts Term::ANSIColor::bold("Pushing #{evt_name} #{evt_data.inspect}")
74:
end
75:
m.after_push do |context|
76:
  puts "  After:\n" + context.pretty_inspect.gsub(/^/,'    |')
77:
end
78:
 
79:
puts Term::ANSIColor::bold("Initial context:")
80:
puts m.context.pretty_inspect.gsub(/^/,'    |')
81:
 
82:
m.push :before, :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/1'
83:
m.push :before, :position => :p, :endpoint => 'http://y', :instance => 'http://localhost:9188/2'
84:
m.push :before, :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/3'
85:
m.push :before, :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/4'
86:
 
87:
m.push :after,  :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/1'
88:
m.push :after,  :position => :p, :endpoint => 'http://y', :instance => 'http://localhost:9188/2'
89:
 
90:
m.push :before, :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/5'
91:
 
92:
m.push :before, :position => :p, :endpoint => 'http://z', :instance => 'http://localhost:9188/2'
93:
m.push :after , :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/3'
94:
m.push :after,  :position => :p, :endpoint => 'http://z', :instance => 'http://localhost:9188/2'
95:
 
96:
m.push :after,  :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/4'