Class: Syskit::Coordination::Models::DataMonitorPredicateFromBlock

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_streams, predicate_block, arguments = Hash.new) ⇒ DataMonitorPredicateFromBlock

Returns a new instance of DataMonitorPredicateFromBlock

Parameters:

  • data_streams (Array<Syskit::Models::OutputReader>)

    the data streams, in the same order than expected by the given block

  • predicate_block (#call)

    the predicate object. See the documentation of #block



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

#argumentsHash (readonly)

predicate block

Returns:

  • (Hash)

    set of arguments that are needed by the



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

Returns:

  • (#call)

    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

#samplesArray<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

Returns:

  • (Array<Object,nil>)

    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)

Returns:

  • ({Syskit::Models::OutputReader=>Integer})

    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

Parameters:



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

#finalizeBoolean

Called to know whether this predicate matched or not

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)

    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

Returns:

  • (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