Class: Syskit::Coordination::Models::DataMonitor
- Defined in:
- lib/syskit/coordination/models/data_monitor.rb
Overview
Representation of a single data monitor
Data monitors read some data streams (read: port data readers), feed them into a predicate object that returns a boolean. If the predicate returns true ('triggers'), the monitor will emit a set of events and/or generate a DataMonitoringError Roby exception.
Instance Attribute Summary collapse
-
#data_streams ⇒ Syskit::Models::OutputReader
readonly
The data streams that are monitored.
-
#emitted_events ⇒ Roby::Coordination::Models::Event
readonly
The events that should be emitted when this monitor triggers.
-
#name ⇒ String
readonly
The monitor name.
-
#predicate ⇒ #bind
readonly
The predicate model.
Instance Method Summary collapse
-
#bind(table) ⇒ Syskit::Coordination::DataMonitor
Called to generate an object that can be used by a data monitoring table instance.
-
#emit(event) ⇒ Object
Call to make this monitor emit the given event when it triggers.
-
#initialize(name, data_streams) ⇒ DataMonitor
constructor
A new instance of DataMonitor.
-
#raise_exception ⇒ Object
Call to cause this monitor to generate a DataMonitoringError whenever the predicate triggers.
-
#raises=(value) ⇒ Boolean
Whether instances of this model should generate a DataMonitoringError when it triggers.
-
#raises? ⇒ Boolean
Whether instances of this model should generate a DataMonitoringError when it triggers.
- #to_execution_exception_matcher ⇒ Object
-
#trigger_on(predicate = nil, &predicate_block) ⇒ Object
Defines the predicate that will cause this monitor to trigger.
Constructor Details
#initialize(name, data_streams) ⇒ DataMonitor
Returns a new instance of DataMonitor
29 30 31 32 33 34 |
# File 'lib/syskit/coordination/models/data_monitor.rb', line 29 def initialize(name, data_streams) @name, @data_streams = name, data_streams @predicate = nil @emitted_events = [] @raises = false end |
Instance Attribute Details
#data_streams ⇒ Syskit::Models::OutputReader (readonly)
Returns the data streams that are monitored
15 16 17 |
# File 'lib/syskit/coordination/models/data_monitor.rb', line 15 def data_streams @data_streams end |
#emitted_events ⇒ Roby::Coordination::Models::Event (readonly)
Returns the events that should be emitted when this monitor triggers
27 28 29 |
# File 'lib/syskit/coordination/models/data_monitor.rb', line 27 def emitted_events @emitted_events end |
#name ⇒ String (readonly)
Returns the monitor name
12 13 14 |
# File 'lib/syskit/coordination/models/data_monitor.rb', line 12 def name @name end |
#predicate ⇒ #bind (readonly)
The predicate model. Its #new method must return an object that matches the description of DataMonitor#predicate
19 20 21 |
# File 'lib/syskit/coordination/models/data_monitor.rb', line 19 def predicate @predicate end |
Instance Method Details
#bind(table) ⇒ Syskit::Coordination::DataMonitor
Called to generate an object that can be used by a data monitoring table instance
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/syskit/coordination/models/data_monitor.rb', line 40 def bind(table) if !predicate raise ArgumentError, "no predicate defined in #{self}" end data_streams = self.data_streams.map do |reader| reader.bind(table.instance_for(reader.port.component_model)) end predicate = self.predicate.bind(table, data_streams) monitor = Syskit::Coordination::DataMonitor.new(self, data_streams) monitor.trigger_on(predicate) emitted_events.each do |ev| monitor.emit(table.instance_for(ev)) end monitor.raises = self.raises? monitor end |
#emit(event) ⇒ Object
Call to make this monitor emit the given event when it triggers
62 63 64 65 |
# File 'lib/syskit/coordination/models/data_monitor.rb', line 62 def emit(event) @emitted_events << event self end |
#raise_exception ⇒ Object
Call to cause this monitor to generate a DataMonitoringError whenever the predicate triggers
71 72 73 74 |
# File 'lib/syskit/coordination/models/data_monitor.rb', line 71 def raise_exception @raises = true self end |
#raises=(value) ⇒ Boolean
Returns whether instances of this model should generate a DataMonitoringError when it triggers
23 |
# File 'lib/syskit/coordination/models/data_monitor.rb', line 23 attr_predicate :raises?, true |
#raises? ⇒ Boolean
Returns whether instances of this model should generate a DataMonitoringError when it triggers
23 |
# File 'lib/syskit/coordination/models/data_monitor.rb', line 23 attr_predicate :raises?, true |
#to_execution_exception_matcher ⇒ Object
104 105 106 |
# File 'lib/syskit/coordination/models/data_monitor.rb', line 104 def to_execution_exception_matcher DataMonitoringErrorMatcher.new.from_monitor(self).to_execution_exception_matcher end |
#trigger_on(predicate = nil, &predicate_block) ⇒ Object
Defines the predicate that will cause this monitor to trigger
If a block is given, it is a shortcut to using the DataMonitorPredicateFromBlock. The block will be called with samples from each of the monitor's data sources, and must return true if the monitor should trigger and false otherwise.
Both cannot be given.
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/syskit/coordination/models/data_monitor.rb', line 92 def trigger_on(predicate = nil, &predicate_block) if self.predicate raise ArgumentError, "#{self} already has a trigger, you cannot add another one" elsif predicate && predicate_block raise ArgumentError, "you can give either a predicate object or a predicate block, not both" elsif predicate_block predicate = DataMonitorPredicateFromBlock.new(data_streams, predicate_block) end @predicate = predicate self end |