diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2012-06-27 17:27:38 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2012-06-27 17:27:38 +0200 |
commit | f93d32f4ecb278a9a7592c3a1e053585a2251088 (patch) | |
tree | b85896b2454562b2be20b931498f9347b23c383a /examples/links.rb | |
parent | 77cacf27b6afd6da9201252854472b89d93c2d80 (diff) | |
download | edoors-ruby-f93d32f4ecb278a9a7592c3a1e053585a2251088.zip edoors-ruby-f93d32f4ecb278a9a7592c3a1e053585a2251088.tar.gz |
examples: add links.rb and data.json
Diffstat (limited to 'examples/links.rb')
-rw-r--r-- | examples/links.rb | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/examples/links.rb b/examples/links.rb new file mode 100644 index 0000000..ae3293c --- /dev/null +++ b/examples/links.rb @@ -0,0 +1,97 @@ +#! /usr/bin/env ruby +# -*- coding: UTF-8 -*- +# +# from the project top directory : +# +# run this script which builds the example system and spin it untill it's empty: +# $ ruby -Ilib examples/links.rb +# +require 'edoors' +# +class FileReader < Edoors::Door + # + def initialize n, p, path + super n, p + @file = File.open(path,'r') + end + # + def start! + # stimulate myself on system boot up + send_p require_p(Edoors::Particle), Edoors::ACT_GET + end + # + def receive_p p + if p.action==Edoors::ACT_GET + # stop everything if EOF reached + return if @file.eof? + p.set_data 'person', JSON.load(@file.readline) + # will follow the non conditional link. + # see Room#_send and Room#_try_links + send_p p + # stimulate myself + start! + end + end + # +end +# +class Filter < Edoors::Door + # + def initialize n, p, &block + super n, p + @filter = block + end + # + def receive_p p + if p.action!=Edoors::ACT_ERROR + # apply the filter + @filter.call p + # will follow the conditional link. + # see Room#_send and Room#_try_links + send_p p + end + end + # +end +# +class OutputDoor < Edoors::Door + # + def initialize n, p, t + super n, p + @title = t + end + # + def receive_p p + if p.action!=Edoors::ACT_ERROR + p = p.get_data('person') + puts "#{p['name']} is a #{p['age']} year(s) old #{@title}" + end + end + # +end +# +if $0 == __FILE__ + # basic setup, see hello_world.rb + dom0 = Edoors::Spin.new 'dom0' + # + FileReader.new 'input', dom0, './examples/data.json' + Filter.new('age_filter', dom0) { |p| p['old'] = (p['person']['age']>=30); p['sex']=p['person']['sex'] } + OutputDoor.new 'output_f', dom0, 'woman' + OutputDoor.new 'output_m', dom0, 'man' + OutputDoor.new 'output_child', dom0, 'child' + OutputDoor.new 'output_parent', dom0, 'parent' + # default link directing everything from input into age_filter + dom0.add_link Edoors::Link.new('input', 'age_filter') + # different links directing to different outputs depending on 'sex' key + dom0.add_link Edoors::Link.new('age_filter', 'output_f', nil, {'sex'=>'f'}) + dom0.add_link Edoors::Link.new('age_filter', 'output_m', nil, {'sex'=>'m'}) + # different links directing to different outputs depending on 'old' key + dom0.add_link Edoors::Link.new('age_filter', 'output_child', nil, {'old'=>false}) + dom0.add_link Edoors::Link.new('age_filter', 'output_parent', nil, {'old'=>true}) + # + # schedule the spinning particles untill the system cools down + dom0.spin! + # +end +# +# EOF |