Class: Syskit::Coordination::Models::DataMonitorPredicateFromBlock
- Defined in:
- lib/syskit/coordination/models/data_monitor_predicate_from_block.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Hash
readonly
predicate block.
-
#block ⇒ #call
readonly
The predicate block.
-
#samples ⇒ Array<Object,nil>
readonly
The per-stream set of samples last received.
-
#stream_to_index ⇒ {Syskit::Models::OutputReader=>Integer}
readonly
A mapping from a reader model to the index in the list of samples (and, therefore, in the block's argument list).
Instance Method Summary collapse
- #bind(table, data_streams) ⇒ Object
-
#call(stream, sample) ⇒ Object
Called when a new sample has been received.
-
#finalize ⇒ Boolean
Called to know whether this predicate matched or not.
-
#has_new_sample? ⇒ Boolean
Indicates whether #call has been received with a new sample since the last call to #finalize.
-
#initialize(data_streams, predicate_block, arguments = Hash.new) ⇒ DataMonitorPredicateFromBlock
constructor
A new instance of DataMonitorPredicateFromBlock.
- #method_missing(m, *args) ⇒ Object
- #respond_to_missing?(m, include_private) ⇒ Boolean
Constructor Details
#initialize(data_streams, predicate_block, arguments = Hash.new) ⇒ DataMonitorPredicateFromBlock
Returns a new instance of DataMonitorPredicateFromBlock
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/syskit/coordination/models/data_monitor_predicate_from_block.rb', line 31 def initialize(data_streams, predicate_block, arguments = Hash.new) check_arity(predicate_block, data_streams.size) @arguments = arguments @stream_to_index = Hash.new data_streams.each_with_index do |s, idx| stream_to_index[s] = idx end @samples = Array.new(data_streams.size) @block = predicate_block # Flag used to know whether we have at least a sample per # stream @full = false @new_sample = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/syskit/coordination/models/data_monitor_predicate_from_block.rb', line 79 def method_missing(m, *args) if arguments.has_key?(m) if !args.empty? raise ArgumentError, "#{args.size} provided to #{m}, zero expected" end return arguments[m] end return super end |
Instance Attribute Details
#arguments ⇒ Hash (readonly)
predicate block
24 25 26 |
# File 'lib/syskit/coordination/models/data_monitor_predicate_from_block.rb', line 24 def arguments @arguments end |
#block ⇒ #call (readonly)
Returns the predicate block. It is called with the data samples in the same order than defined by the data_streams argument of #initialize
8 9 10 |
# File 'lib/syskit/coordination/models/data_monitor_predicate_from_block.rb', line 8 def block @block end |
#samples ⇒ Array<Object,nil> (readonly)
Returns the per-stream set of samples last received. The sample order is the same than the data_streams argument of #initialize
12 13 14 |
# File 'lib/syskit/coordination/models/data_monitor_predicate_from_block.rb', line 12 def samples @samples end |
#stream_to_index ⇒ {Syskit::Models::OutputReader=>Integer} (readonly)
Returns a mapping from a reader model to the index in the list of samples (and, therefore, in the block's argument list)
16 17 18 |
# File 'lib/syskit/coordination/models/data_monitor_predicate_from_block.rb', line 16 def stream_to_index @stream_to_index end |
Instance Method Details
#bind(table, data_streams) ⇒ Object
48 49 50 |
# File 'lib/syskit/coordination/models/data_monitor_predicate_from_block.rb', line 48 def bind(table, data_streams) self.class.new(data_streams, block, table.arguments) end |
#call(stream, sample) ⇒ Object
Called when a new sample has been received
57 58 59 60 |
# File 'lib/syskit/coordination/models/data_monitor_predicate_from_block.rb', line 57 def call(stream, sample) samples[stream_to_index[stream]] = sample @new_sample = true end |
#finalize ⇒ Boolean
Called to know whether this predicate matched or not
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/syskit/coordination/models/data_monitor_predicate_from_block.rb', line 64 def finalize return if !has_new_sample? if !@full return if samples.compact.size != samples.size @full = true end @new_samples = false instance_exec(*samples, &block) end |
#has_new_sample? ⇒ Boolean
Returns indicates whether #call has been received with a new sample since the last call to #finalize
19 20 21 |
# File 'lib/syskit/coordination/models/data_monitor_predicate_from_block.rb', line 19 def has_new_sample? @new_sample end |
#respond_to_missing?(m, include_private) ⇒ Boolean
75 76 77 |
# File 'lib/syskit/coordination/models/data_monitor_predicate_from_block.rb', line 75 def respond_to_missing?(m, include_private) arguments.has_key?(m) || super end |