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>)


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

def neg_predicates
  @neg_predicates
end

#predicatesArray<Symbol> (readonly)

Set of predicates that should be true for the object

Returns:

  • (Array<Symbol>)


38
39
40
# File 'lib/roby/queries/matcher_base.rb', line 38

def predicates
  @predicates
end

Class Method Details

.declare_class_methods(*names) ⇒ Object

:nodoc:



48
49
50
51
52
53
54
55
# File 'lib/roby/queries/matcher_base.rb', line 48

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

.match_predicate(name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/roby/queries/matcher_base.rb', line 57

def match_predicate(name)
    method_name = name.to_s.gsub(/\?$/, '')
    class_eval <<-EOD, __FILE__, __LINE__+1
    def #{method_name}
        if neg_predicates.include?(:#{name})
            raise ArgumentError, "trying to match (#{name} & !#{name})"
        end
        predicates << :#{name}
        self
    end
    def not_#{method_name}
        if predicates.include?(:#{name})
            raise ArgumentError, "trying to match (#{name} & !#{name})"
        end
        neg_predicates << :#{name}
        self
    end
    EOD
    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.



82
83
84
85
86
# File 'lib/roby/queries/matcher_base.rb', line 82

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.



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

def &(other); AndMatcher.new(self, other) 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)


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

def describe_failed_match(exception)
end

#each(plan) ⇒ Object

Enumerates all tasks of plan which match this TaskMatcher object

It is O(N). You should prefer use Query which uses the plan’s task indexes, thus leading to O(1) in simple cases.



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

def each(plan)
    return enum_for(__method__, plan) if !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)


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

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



94
95
96
# File 'lib/roby/queries/matcher_base.rb', line 94

def match
    self
end

#negateObject

Negates this predicate

The returned task matcher will yield tasks that are not matched by self



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

def negate; NotMatcher.new(self) end

#|(other) ⇒ Object

OR-combination of two predicates

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



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

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