Created
December 10, 2009 18:28
-
-
Save tosch/253545 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# require all the libs we need | |
require 'rubygems' | |
require 'patron' # fast HTTP client using libcurl | |
require 'yajl/json_gem' # fast JSON parsing | |
require 'ruote-kit/client' # the most important bit of all of course ;-) | |
module RuoteKit | |
module Client | |
class BeerExample | |
RATINGS = ['Outstanding', 'Exeeds Expectations', 'Acceptable', 'Poor', 'Dreadful', 'Troll'] | |
def self.run! | |
puts "Welcome to the ruote-kit-client example." | |
# let's have an example workflow definition | |
process_definition = <<EOT | |
<process-definition name="beer-test" revision="0.1"> | |
<sequence> | |
<participant ref="arrival_qa" /> | |
<concurrence merge_type="mix"> | |
<participant ref="tester_a" /> | |
<participant ref="tester_b" /> | |
<participant ref="tester_c" /> | |
</concurrence> | |
<participant ref="chief_tester" /> | |
<participant ref="present_result" /> | |
</sequence> | |
</process-definition> | |
EOT | |
# get a client instance | |
@@client = RuoteKit::Client('http://ruote.torstenschoenebaum.de') | |
puts "Got client connection to http://ruote.torstenschoenebaum.de" if @@client | |
puts "Will launch a process in just a moment" | |
puts "Before we do this, tell us the brand of the beer to test" | |
# let's launch a first process | |
@@process = @@client.launch_process process_definition, 'brand' => gets.chomp | |
puts "The process was launched and has the wfid #{@@process.wfid}" | |
threads = [] | |
threads << Thread.new do | |
puts "The arrival qa is waiting for work" | |
workitem = wait_for_workitem 'arrival_qa' | |
puts "Arrival qa has something to do!" | |
if(workitem['fields'] and workitem['fields']['brand']) | |
workitem['fields']['arrival_qa'] = "#{workitem['fields']['brand']} is suitable for testing" | |
@@client.proceed_workitem(workitem) | |
puts "Arrival qa has its work done!" | |
else | |
puts 'No beer arrived! *arg* Have to quit' | |
@@process.cancel! | |
exit | |
end | |
end | |
%w(tester_a tester_b tester_c chief_tester).each do |name| | |
threads << Thread.new do | |
puts "#{name} is waiting for work" | |
workitem = wait_for_workitem name | |
puts "#{name} has something to do!" | |
workitem['fields'][name] = get_rating | |
@@client.proceed_workitem(workitem) | |
puts "#{name} has his work done!" | |
end | |
end | |
threads << Thread.new do | |
puts "The result presentation engine is waiting for work" | |
workitem = wait_for_workitem 'present_result' | |
puts "Result presentation has something to do" | |
brand = workitem['fields']['brand'] | |
puts "==================================================================" | |
puts "And here are the results of our little test round on #{brand}:" | |
puts "Arrival QA said: #{workitem['fields']['arrival_qa']}" | |
sum = %w(a b c).inject(0) do |s, n| | |
puts "Tester #{n} rates #{brand} #{RATINGS[workitem['fields']["tester_#{n}"]]}" | |
s += workitem['fields']["tester_#{n}"] | |
end | |
puts "Chief Tester rates #{brand} #{RATINGS[workitem['fields']['chief_tester']]}" | |
sum += workitem['fields']['chief_tester'] * 3 | |
puts "So over all, #{brand} gets a rating of #{RATINGS[(sum / 6.0).round]}" | |
puts "==================================================================" | |
@@client.proceed_workitem(workitem) | |
puts "Result presentation finished" | |
end | |
# wait for threads to finish | |
threads.each {|t| t.join } | |
puts "The example is finished" | |
end | |
private | |
# fetches workitems for a participant till a workitem is found | |
def self.wait_for_workitem(participant) | |
workitem = nil | |
while workitem.nil? | |
items = @@client.workitems :participant => participant | |
workitem = items.find{|i| i and i['fields'] and i['fields']['params'] and i['fields']['params']['ref'] and i['fields']['params']['ref'] == participant } | |
sleep 1.5 | |
end | |
workitem | |
end | |
def self.get_rating | |
rand(RATINGS.length) | |
end | |
end | |
end | |
end | |
RuoteKit::Client::BeerExample.run! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment