Class: Syskit::Coordination::Models::DataMonitor

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_streamsSyskit::Models::OutputReader (readonly)

Returns the data streams that are monitored

Returns:



15
16
17
# File 'lib/syskit/coordination/models/data_monitor.rb', line 15

def data_streams
  @data_streams
end

#emitted_eventsRoby::Coordination::Models::Event (readonly)

Returns the events that should be emitted when this monitor triggers

Returns:

  • (Roby::Coordination::Models::Event)

    the events that should be emitted when this monitor triggers

See Also:

  • {emit}


27
28
29
# File 'lib/syskit/coordination/models/data_monitor.rb', line 27

def emitted_events
  @emitted_events
end

#nameString (readonly)

Returns the monitor name

Returns:

  • (String)

    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

Returns:

  • (#bind)

    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

Returns:

  • self



62
63
64
65
# File 'lib/syskit/coordination/models/data_monitor.rb', line 62

def emit(event)
    @emitted_events << event
    self
end

#raise_exceptionObject

Call to cause this monitor to generate a DataMonitoringError whenever the predicate triggers

Returns:

  • self



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

Returns:

  • (Boolean)

    whether instances of this model should generate a DataMonitoringError when it triggers

See Also:

  • {raise}


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

Returns:

  • (Boolean)

    whether instances of this model should generate a DataMonitoringError when it triggers

See Also:

  • {raise}


23
# File 'lib/syskit/coordination/models/data_monitor.rb', line 23

attr_predicate :raises?, true

#to_execution_exception_matcherObject



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.

Parameters:

  • predicate (#bind) (defaults to: nil)

    the predicate model object. See the description of the #predicate attribute.

Returns:

  • self

Raises:

  • ArgumentError if a predicate is already defined on this data monitor



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