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/p35.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: | |
10: | event :before, :instance, :position, :endpoint |
11: | event :after, :instance, :position, :endpoint |
12: | |
13: | context :wait, :buf => [] |
14: | context :sync, :run => [], :fin => [] |
15: | |
16: | call :continue |
17: | call :skip |
18: | |
19: | rule event.before do |
20: | context.wait.buf << [event.instance, event.position] |
21: | end |
22: | rule context.wait{buf.length >= 3}, context.sync{run.empty?} do |
23: | context.sync.run = context.wait.buf.slice!(0..2) |
24: | context.sync.run.each do |instance,position| |
25: | continue instance,position |
26: | end |
27: | end |
28: | |
29: | rule event.after do |
30: | context.sync.fin << [event.instance, event.position] |
31: | context.sync.run.delete [event.instance, event.position] |
32: | end |
33: | rule context.sync{fin.length == 2} do |
34: | context.sync.fin.each do |instance,position| |
35: | continue instance,position |
36: | end |
37: | context.sync.run.each do |instance,position| |
38: | skip instance, position |
39: | end |
40: | context.sync.run.clear |
41: | context.sync.fin.clear |
42: | end |
43: | end |
44: | |
45: | m.on_continue do |instance,position| |
46: | puts Term::ANSIColor::bold(Term::ANSIColor::red(" Continuing #{instance}, #{position}")) |
47: | end |
48: | m.on_skip do |instance, position| |
49: | puts Term::ANSIColor::bold(Term::ANSIColor::red(" Skipping #{instance}, #{position}")) |
50: | end |
51: | m.before_push do |evt_name,evt_data,context| |
52: | puts Term::ANSIColor::bold("Pushing #{evt_name} #{evt_data.inspect}") |
53: | end |
54: | m.after_push do |context| |
55: | puts " After:\n" + context.pretty_inspect.gsub(/^/,' |') |
56: | end |
57: | |
58: | puts Term::ANSIColor::bold("Initial context:") |
59: | puts m.context.pretty_inspect.gsub(/^/,' |') |
60: | |
61: | m.push :before, :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/1' |
62: | m.push :before, :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/2' |
63: | m.push :before, :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/3' |
64: | |
65: | m.push :after, :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/1' |
66: | m.push :after, :position => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/2' |