Class: Roby::Queries::MatcherBase

Inherits:
Object
  • Object
show all
Defined in:
lib/roby/queries/matcher_base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#neg_predicatesArray<Symbol> (readonly)

Set of predicats that should be false for the object

The predicates are predicate method names (e.g. ‘executable’ for #executable?)

Returns:

  • (Array<Symbol>)


80
81
82
# File 'lib/roby/queries/matcher_base.rb', line 80

def neg_predicates
  @neg_predicates
end

#predicatesArray<Symbol> (readonly)

Set of predicates that should be true for the object

Returns:

  • (Array<Symbol>)


72
73
74
# File 'lib/roby/queries/matcher_base.rb', line 72

def predicates
  @predicates
end

Class Method Details

.declare_class_methods(*names) ⇒ Object

:nodoc:



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/roby/queries/matcher_base.rb', line 107

def declare_class_methods(*names) # :nodoc:
    names.each do |name|
        unless method_defined?(name)
            raise "no instance method #{name} on #{self}"
        end

        singleton_class.send(:define_method, name) do |*args|
            new.send(name, *args)
        end
    end
end

.match_predicate(name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/roby/queries/matcher_base.rb', line 119

def match_predicate(name)
    method_name = name.to_s.gsub(/\?$/, "")
    class_eval <<~PREDICATE_CODE, __FILE__, __LINE__ + 1
        def #{method_name}
            add_predicate(:#{name})
        end
        def not_#{method_name}
            add_neg_predicate(:#{name})
        end
    PREDICATE_CODE
    declare_class_methods(method_name, "not_#{method_name}")
end

.match_predicates(*names) ⇒ Object

For each name in names, define a #name and a #not_name method. If the first is called, the matcher will match only tasks whose #name? method returns true. If the second is called, the opposite will be done.



136
137
138
139
140
# File 'lib/roby/queries/matcher_base.rb', line 136

def match_predicates(*names)
    names.each do |name|
        match_predicate(name)
    end
end

Instance Method Details

#&(other) ⇒ Object

AND-combination of two predicates

The returned task matcher will yield tasks that are matched by both predicates.



58
59
60
# File 'lib/roby/queries/matcher_base.rb', line 58

def &(other)
    AndMatcher.new(self, other)
end

#add_neg_predicate(predicate) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add the given predicate to the set of predicates that must match



97
98
99
100
101
102
103
104
# File 'lib/roby/queries/matcher_base.rb', line 97

def add_neg_predicate(predicate)
    if @predicates.include?(predicate)
        raise ArgumentError, "trying to match (#{predicate} & !#{predicate})"
    end

    @neg_predicates << predicate unless @neg_predicates.include?(predicate)
    self
end

#add_predicate(predicate) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add the given predicate to the set of predicates that must match



85
86
87
88
89
90
91
92
# File 'lib/roby/queries/matcher_base.rb', line 85

def add_predicate(predicate)
    if @neg_predicates.include?(predicate)
        raise ArgumentError, "trying to match (#{predicate} & !#{predicate})"
    end

    @predicates << predicate unless @predicates.include?(predicate)
    self
end

#describe_failed_match(exception) ⇒ nil, String

Describe a failed match in a human-readable way

It is meant to help debugging in tests

Returns:

  • (nil, String)


157
# File 'lib/roby/queries/matcher_base.rb', line 157

def describe_failed_match(exception); end

#each(plan) ⇒ Object



12
13
14
15
16
17
# File 'lib/roby/queries/matcher_base.rb', line 12

def each(plan)
    Roby.warn_deprecated "MatcherBase#each is deprecated, "\
                         "use #each_in_plan instead"

    each_in_plan(plan)
end

#each_in_plan(plan) ⇒ Object

Enumerates all tasks of plan which match this TaskMatcher object



20
21
22
23
24
25
26
27
# File 'lib/roby/queries/matcher_base.rb', line 20

def each_in_plan(plan)
    return enum_for(__method__, plan) unless block_given?

    plan.each_task do |t|
        yield(t) if self === t
    end
    self
end

#indexed_query?Boolean

Returns true if calling #filter with a task set and a relevant index will return the exact query result or not

Returns:

  • (Boolean)


8
9
10
# File 'lib/roby/queries/matcher_base.rb', line 8

def indexed_query?
    false
end

#matchObject

The #match method is used to convert any object to the corresponding Query object. For instance, Models::TaskEvent#match returns the corresponding TaskEventGeneratorMatcher.

For matchers, it returns self



148
149
150
# File 'lib/roby/queries/matcher_base.rb', line 148

def match
    self
end

#negateObject



50
51
52
# File 'lib/roby/queries/matcher_base.rb', line 50

def negate
    NotMatcher.new(self)
end

#resetObject



45
46
47
48
# File 'lib/roby/queries/matcher_base.rb', line 45

def reset
    Roby.warn_deprecated "Matcher#reset is a no-op now, matchers "\
                         "don't cache their results anymore"
end

#to_a(plan) ⇒ Object

Finds all matching objects in plan and returns them as an Array

It is essentially equivalent to each_in_plan(plan).to_a, but might be optimized in some cases



33
34
35
# File 'lib/roby/queries/matcher_base.rb', line 33

def to_a(plan)
    each_in_plan(plan).to_a
end

#to_set(plan) ⇒ Object

Finds all matching objects in plan and returns them as a Set

It is essentially equivalent to each_in_plan(plan).to_set, but is optimized in indexed resolutions where a Set is already available



41
42
43
# File 'lib/roby/queries/matcher_base.rb', line 41

def to_set(plan)
    each_in_plan(plan).to_set
end

#|(other) ⇒ Object

OR-combination of two predicates

The returned task matcher will yield tasks that match either one predicate or the other.



66
67
68
# File 'lib/roby/queries/matcher_base.rb', line 66

def |(other)
    OrMatcher.new(self, other)
end