Class: Syskit::Coordination::DataMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/syskit/coordination/data_monitor.rb

Overview

A Models::DataMonitor instanciated for a data monitor table attached to an actual component

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, data_streams) ⇒ DataMonitor

Returns a new instance of DataMonitor



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

def initialize(model, data_streams)
    @model, @data_streams = model, data_streams
    @emitted_events = Array.new
    @raises = false
end

Instance Attribute Details

#data_streamsSyskit::OutputReader (readonly)

Returns the data streams that are monitored

Returns:



10
11
12
# File 'lib/syskit/coordination/data_monitor.rb', line 10

def data_streams
  @data_streams
end

#emitted_eventsArray<Roby::Coordination::Event> (readonly)

Returns the set of events that should be emitted when this monitor triggers

Returns:

  • (Array<Roby::Coordination::Event>)

    the set of events that should be emitted when this monitor triggers



20
21
22
# File 'lib/syskit/coordination/data_monitor.rb', line 20

def emitted_events
  @emitted_events
end

#modelModels::DataMonitor (readonly)

Returns the monitor model

Returns:



7
8
9
# File 'lib/syskit/coordination/data_monitor.rb', line 7

def model
  @model
end

#predicate#call, #finalize (readonly)

Returns the predicate. It will be called each time there is a new sample on one of the data streams with the data stream that got a new sample as well as the sample itself. In addition, #finalize is called at the end of a data gathering cycle, i.e. after #call has been called with all new samples. It must return whether the predicate matches (true) or not (false)

Returns:

  • (#call, #finalize)

    the predicate. It will be called each time there is a new sample on one of the data streams with the data stream that got a new sample as well as the sample itself. In addition, #finalize is called at the end of a data gathering cycle, i.e. after #call has been called with all new samples. It must return whether the predicate matches (true) or not (false)



17
18
19
# File 'lib/syskit/coordination/data_monitor.rb', line 17

def predicate
  @predicate
end

Instance Method Details

#emit(event) ⇒ Object



41
42
43
44
# File 'lib/syskit/coordination/data_monitor.rb', line 41

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

#poll(root_task) ⇒ Object

Reads the data streams, and pushes the data to the predicate when applicable



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/syskit/coordination/data_monitor.rb', line 48

def poll(root_task)
    data_streams.each do |reader|
        while sample = reader.read_new
            predicate.call(reader, sample)
        end
    end
    if predicate.finalize
        trigger(root_task)
        true
    else
        false
    end
end

#raises=(value) ⇒ Boolean

Returns whether this monitor should generate a Syskit::Coordination::DataMonitoringError when it triggers

Returns:



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

attr_predicate :raises?, true

#raises?Boolean

Returns whether this monitor should generate a Syskit::Coordination::DataMonitoringError when it triggers

Returns:



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

attr_predicate :raises?, true

#ready?Boolean

Whether the data monitor is attached to all its source streams

Returns:

  • (Boolean)


32
33
34
# File 'lib/syskit/coordination/data_monitor.rb', line 32

def ready?
    data_streams.all?(&:ready?)
end

#to_sObject



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

def to_s; "monitor(#{model.name}(#{data_streams.map(&:to_s).join(", ")})" end

#trigger(root_task) ⇒ Object

Issue an error because the predicate returned true (match)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/syskit/coordination/data_monitor.rb', line 63

def trigger(root_task)
    emitted_events.each do |ev|
        ev = ev.resolve
        if ev.executable?
            ev.emit
        end
    end
    if raises?
        samples = data_streams.map do |reader|
            reader.read
        end
        error = DataMonitoringError.new(root_task, self, Time.now, samples)
        root_task.plan.add_error(error)
    end
end

#trigger_on(predicate) ⇒ Object



36
37
38
39
# File 'lib/syskit/coordination/data_monitor.rb', line 36

def trigger_on(predicate)
    @predicate = predicate
    self
end