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/p34.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 :running_syncing_before, :instance, :activity, :endpoint |
| 11: | event :running_syncing_after, :instance, :activity, :endpoint |
| 12: | |
| 13: | context :wait, :buf => [] |
| 14: | context :sync, :run => [], :fin => [] |
| 15: | |
| 16: | call :continue |
| 17: | |
| 18: | rule event.running_syncing_before do |
| 19: | context.wait.buf << [event.instance, event.activity] |
| 20: | end |
| 21: | rule context.wait{buf.length >= 3}, context.sync{run.empty?} do |
| 22: | context.sync.run = context.wait.buf.slice!(0..2) |
| 23: | context.sync.run.each do |instance,activity| |
| 24: | continue instance,activity |
| 25: | end |
| 26: | end |
| 27: | |
| 28: | rule event.running_syncing_after do |
| 29: | context.sync.fin << [event.instance, event.activity] |
| 30: | end |
| 31: | rule context.sync{fin.length == 2} do |
| 32: | context.sync.fin.each do |instance,activity| |
| 33: | continue instance,activity |
| 34: | end |
| 35: | end |
| 36: | rule context.sync{fin.length > 2} do |
| 37: | continue context.sync.fin.last |
| 38: | end |
| 39: | rule context.sync{fin.length == run.length} do |
| 40: | context.sync.fin.clear |
| 41: | context.sync.run.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: | |
| 49: | m.before_push do |evt_name,evt_data,context| |
| 50: | puts Term::ANSIColor::bold("Pushing #{evt_name} #{evt_data.inspect}") |
| 51: | end |
| 52: | m.after_push do |context| |
| 53: | puts " After:\n" + context.pretty_inspect.gsub(/^/,' |') |
| 54: | end |
| 55: | |
| 56: | puts Term::ANSIColor::bold("Initial context:") |
| 57: | puts m.context.pretty_inspect.gsub(/^/,' |') |
| 58: | |
| 59: | m.push :running_syncing_before, :activity => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/p1' |
| 60: | m.push :running_syncing_before, :activity => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/p3' |
| 61: | m.push :running_syncing_before, :activity => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/p2' |
| 62: | |
| 63: | m.push :running_syncing_after, :activity => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/p1' |
| 64: | m.push :running_syncing_after, :activity => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/p2' |
| 65: | |
| 66: | m.push :running_syncing_before, :activity => :p, :endpoint => 'http://b', :instance => 'http://localhost:9189/p4' |
| 67: | |
| 68: | m.push :running_syncing_after, :activity => :p, :endpoint => 'http://b', :instance => 'http://localhost:9188/p3' |