Skip to content

Commit

Permalink
Still a lot of changes, getting there
Browse files Browse the repository at this point in the history
  • Loading branch information
tallakt committed Feb 25, 2009
1 parent dfb518c commit 7ac19cd
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 316 deletions.
61 changes: 27 additions & 34 deletions lib/ruby-plc/etc/notify.rb
@@ -1,42 +1,35 @@
module Notify
@debug = nil
class<<self
# force Notification to show up in stack backtraces of delegated methods
attr_accessor :debug
end

def notify(name)
n = name.to_s
listeners = "@notify_#{n}_listeners"
module_eval(<<-EOS, '(__NOTIFICATION__)', 1)
#{listeners} = nil
def notify(*names)
names.each do |name|
n = name.to_s
listeners = "@notify_#{n}_listeners"
module_eval <<-EOS
#{listeners} = nil
def notify_#{n}
exceptions = nil
if #{listeners}
#{listeners}.each do |l|
begin
|l| l.call
rescue Exception => ex
$@.delete_if{|s| /^\\(__NOTIFICATION__\\):/ =~ s} unless Notification::debug
exceptions ||= []
exceptions << ex
def notify_#{n}(*args)
errors = nil
if #{listeners}
#{listeners}.each do |l|
begin
l.call(*args)
rescue Exception => ex
errors ||= []
errors << ex
end
end
if errors && respond_to?(:handle_notify_error)
handle_notify_error(errors)
end
end
if exceptions && respond_to? :handle_notify_exception
handle_notify_exception(exceptions)
end
end
end
def on_#{n}(&proc)
#{listeners} ||= []
#{listeners} << proc
end
private :notify_#{n}
EOS
def on_#{n}(&proc)
#{listeners} ||= []
#{listeners} << proc
end
EOS
end
end
end

79 changes: 0 additions & 79 deletions lib/ruby-plc/sequences/branch.rb

This file was deleted.

62 changes: 62 additions & 0 deletions lib/ruby-plc/sequences/choose_one.rb
@@ -0,0 +1,62 @@
include 'ruby_plc/sequences/step_base'

module RubyPlc
module Sequences
class ChooseOne
include StepBase

attr_reader :name

def initialize(name = nil)
init_step_listeners
@name = name
@idle_step = Step.new
@end_step = Step.new {|s| s.on_exit continue! }
@steps = [@idle_step, @end_step]
yield self if block_given?
end

def branch(condition, step = nil)
s = to_step(step) || Sequence.new
@idle_step.continue_if condition, s
s.default_next_step = @end_step
@steps << s
yield s if block_given?
end

def otherwise(step)
s = to_step(step) || Sequence.new
@idle_step.otherwise_to s
s.default_next_step = @end_step
@steps << s
yield s if block_given?
end

def active?
@steps.inject(false) {|act, s| act || s.active? }
end

def perform_start
@idle_step.start
end

def perform_reset
@steps.each {|br| br.last.reset }
end


private

def to_step(step)
if step.respond_to? :to_step
step.to_step
else
step
end
end
end
end
end



25 changes: 8 additions & 17 deletions lib/ruby-plc/sequences/in_parallel.rb
@@ -1,13 +1,12 @@
include 'ruby_plc/sequences/step_listeners'
include 'ruby_plc/sequences/step_base'

module RubyPlc
module Sequences
class InParallel
include StepListeners
include StepBase
attr_reader :name

def initialize(name = nil)
init_step_listeners
@name = name
@steps = []
yield self if block_given?
Expand All @@ -19,29 +18,21 @@ def step(s)
else
@steps << s
end
step = @steps.last
step.on_exit do
notify_exit if finished?
@steps.last.on_exit do
continue! if finished?
end
end

def finished?
# All steps finished
@steps.inject(true) {|result, step| result &&= step.finished? }
end

def active?
not finished?
@steps.inject(false) {|act, step| act || step.active? }
end

def start
notify_enter
def perform_start
@steps.each {|s| s.start }
end

def reset(mode = :all)
@steps.each {|s| s.reset(mode) }
notify_reset
def perform_reset
@steps.each {|s| s.reset }
end
end
end
Expand Down
61 changes: 16 additions & 45 deletions lib/ruby-plc/sequences/sequence.rb
@@ -1,20 +1,18 @@
include 'ruby-plc/sequences/step_listeners'
include 'ruby-plc/sequences/step_base'
include 'ruby-plc/timedomain/wait_step'
include 'ruby-plc/timedomain/timer'
include 'ruby-plc/timedomain/sequencer'

mmodule RubyPlc
module Sequences
class Sequence
include StepListeners
include StepBase

attr_reader :name

def initialize(name = nil, options = {})
init_step_listeners
@name = name
@steps = []
@current_step_index = nil # index of current step
@options = options
yield self if block_given?
if @options[:auto_start]
Expand Down Expand Up @@ -42,67 +40,40 @@ def wait(time)
end

def circular(delay = 100)
Timer t = Timer.new delay
on_exit do
t.start
t.at_end do
start
end
end
end


def finished?
current_step
on_exit { Sequencer::wait(delay, self) { start } }
end

def active?
not finished
steps.inject(false) {|act, step| act || step.active? }
end

def start
if not current_step || @steps.any?
notify_enter
@current_step_index = 0
end
def may_continue?
finished?
end

def perform_start
@steps.first && @steps.first.start
end

def current_step
@current_step_index && @steps[@current_step_index]
@steps.find {|s| s.active? }
end

def reset(mode = :all)
@steps.each {|s| s.reset(mode) } if mode == :all
@steps[0..@current_step_index].each {|s| s.reset(mode) } if (mode == :used && @current_step_index)
@current_step_index = nil
notify_reset
def perform_reset
@steps.each {|s| s.reset }
end

private

def add_step(s)
if @options[:reverse]
s.default_next_step = @steps.first
@steps.unshift s
else
@steps.last.default_next_step = s
@steps << s
end

step.on_exit do
Sequencer::at_once do
current_step_finished
end
end
end

def current_step_finished
if @current_step_index
@current_step_index += 1
if @current_step_index >= @steps.size
@current_step_index = nil
notify_exit
end
current_step.start if current_step
end
s.on_exit { continue! if s == @steps.last }
end
end
end
Expand Down

0 comments on commit 7ac19cd

Please sign in to comment.