class Roby::Queries::TaskMatcher

This class represents a predicate which can be used to filter tasks. To filter plan-related properties, use Query.

A TaskMatcher object is a AND combination of various tests against tasks.

For instance, if one does

matcher = TaskMatcher.new.which_fullfills(Tasks::Simple).pending

Then

matcher === task

will return true if task is an instance of the Tasks::Simple model and is pending (not started yet), and false if one of these two characteristics is not true.

Attributes

arguments[R]

Set of arguments that should be tested on the task

@return [Hash]

Public Class Methods

new() click to toggle source

Initializes an empty TaskMatcher object

Calls superclass method Roby::Queries::PlanObjectMatcher::new
# File lib/roby/queries/task_matcher.rb, line 27
def initialize
    super
    @arguments            = Hash.new
    @interruptible        = nil
end

Public Instance Methods

===(task) click to toggle source

True if task matches all the criteria defined on this object.

Calls superclass method Roby::Queries::PlanObjectMatcher#===
# File lib/roby/queries/task_matcher.rb, line 285
def ===(task)
    return unless task.kind_of?(Roby::Task)
    return unless task.arguments.slice(*arguments.keys) == arguments
    return super
end
abstract() click to toggle source

Matches if the task is abstract

See also not_abstract, Task#abstract?

# File lib/roby/queries/task_matcher.rb, line 149
        
failed() click to toggle source

Matches if the task is failed

See also not_failed, Task#failed?

# File lib/roby/queries/task_matcher.rb, line 219
        
find_event(event_name) click to toggle source
# File lib/roby/queries/task_matcher.rb, line 298
def find_event(event_name)
    event_name = event_name.to_sym
    models = if !@model.empty?
                 @model
             else [Roby::Task]
             end
    models.each do |m|
        if event_m = m.find_event(event_name)
            return TaskEventGeneratorMatcher.new(self, event_name)
        end
    end
    nil
end
finished() click to toggle source

Matches if the task is finished

See also not_finished, Task#finished?

# File lib/roby/queries/task_matcher.rb, line 191
        
finishing() click to toggle source

Matches if the task is finishing

See also not_finishing, Task#finishing?

# File lib/roby/queries/task_matcher.rb, line 247
        
fully_instanciated() click to toggle source

Matches if the task is fully instanciated

See also partially_instanciated, Task#fully_instanciated?

# File lib/roby/queries/task_matcher.rb, line 128
        
indexed_query?() click to toggle source

Returns true if filtering with this TaskMatcher using === is equivalent to calling filter() using a Index. This is used to avoid an explicit O(N) filtering step after filter() has been called

# File lib/roby/queries/task_matcher.rb, line 294
def indexed_query?
    arguments.empty? && super
end
interruptible() click to toggle source

Matches if the task is interruptible

See also not_interruptible, Task#interruptible?

# File lib/roby/queries/task_matcher.rb, line 233
        
method_missing(m, *args) click to toggle source
Calls superclass method
# File lib/roby/queries/task_matcher.rb, line 316
def method_missing(m, *args)
    if m =~ /_event$/
        event_name = $`
        model = find_event(event_name)
        if !model
            task_models =
                if !@model.empty?
                    @model
                else [Roby::Task]
                end
            raise NoMethodError.new(m), "no event '#{event_name}' in match model #{task_models.map(&:to_s).join(", ")}, use #which_fullfills to narrow the task model"
        elsif !args.empty?
            raise ArgumentError, "#{m} expected zero arguments, got #{args.size}"
        else
            return TaskEventGeneratorMatcher.new(self, event_name)
        end
    else
        super
    end
end
not_abstract() click to toggle source

Matches if the task is not abstract

See also abstract, Task#abstract?

# File lib/roby/queries/task_matcher.rb, line 142
        
not_failed() click to toggle source

Matches if the task is not failed

See also failed, Task#failed?

# File lib/roby/queries/task_matcher.rb, line 226
        
not_finished() click to toggle source

Matches if the task is not finished

See also finished, Task#finished?

# File lib/roby/queries/task_matcher.rb, line 198
        
not_finishing() click to toggle source

Matches if the task is not finishing

See also finishing, Task#finishing?

# File lib/roby/queries/task_matcher.rb, line 260
match_predicates :abstract?, :partially_instanciated?, :fully_instanciated?,
    :starting?, :pending?, :running?, :finished?, :success?, :failed?, :interruptible?
not_interruptible() click to toggle source

Matches if the task is not interruptible

See also interruptible, Task#interruptible?

# File lib/roby/queries/task_matcher.rb, line 240
        
not_pending() click to toggle source

Matches if the task is not pending

See also pending, Task#pending?

# File lib/roby/queries/task_matcher.rb, line 170
        
not_running() click to toggle source

Matches if the task is not running

See also running, Task#running?

# File lib/roby/queries/task_matcher.rb, line 184
        
not_success() click to toggle source

Matches if the task is not success

See also success, Task#success?

# File lib/roby/queries/task_matcher.rb, line 212
        
partially_instanciated() click to toggle source

Matches if the task is partially instanciated

See also fully_instanciated, Task#partially_instanciated?

# File lib/roby/queries/task_matcher.rb, line 135
        
pending() click to toggle source

Matches if the task is pending

See also not_pending, Task#pending?

# File lib/roby/queries/task_matcher.rb, line 163
        
respond_to_missing?(m, include_private) click to toggle source
Calls superclass method
# File lib/roby/queries/task_matcher.rb, line 312
def respond_to_missing?(m, include_private)
    m =~ /_event$/ || super
end
running() click to toggle source

Matches if the task is running

See also not_running, Task#running?

# File lib/roby/queries/task_matcher.rb, line 177
        
success() click to toggle source

Matches if the task is success

See also not_success, Task#success?

# File lib/roby/queries/task_matcher.rb, line 205
        
to_s() click to toggle source
Calls superclass method Roby::Queries::PlanObjectMatcher#to_s
# File lib/roby/queries/task_matcher.rb, line 33
def to_s
    result = super
    if !arguments.empty?
        result << ".with_arguments(#{arguments.map { |k, v| ":#{k} => #{v}" }.join(", ")})"
    end
    result
end
which_fullfills(model, arguments = nil) click to toggle source

Filters on task model and arguments

Will match if the task is an instance of model or one of its subclasses, and if parts of its arguments are the ones provided. Set arguments to nil if you don't want to filter on arguments.

# File lib/roby/queries/task_matcher.rb, line 46
def which_fullfills(model, arguments = nil)
    with_model(model)
    if arguments
        with_model_arguments(arguments)
    end
    self
end
with_arguments(arguments) click to toggle source

Filters on the arguments that are declared in the model

Will match if the task arguments for which there is a value in arguments are set to that very value. Unlike with_model_arguments, all values set in arguments are considered.

See also with_model_arguments

Example:

class TaskModel < Roby::Task
  argument :a
  argument :b
end
task = TaskModel.new(a: 10, b: 20)

# Matches on :a, :b is ignored altogether
TaskMatcher.new.
    with_arguments(a: 10) === task # => true
# Looks for both :a and :b
TaskMatcher.new.
    with_arguments(a: 10, b: 30) === task # => false
# Looks for both :a and :c, even though :c is not declared in TaskModel
TaskMatcher.new.
    with_arguments(a: 10, c: 30) === task # => false
# File lib/roby/queries/task_matcher.rb, line 116
def with_arguments(arguments)
    @arguments ||= Hash.new
    self.arguments.merge!(arguments) do |k, old, new| 
        if old != new
            raise ArgumentError, "a constraint has already been set on the #{k} argument" 
        end
        old
    end
    self
end
with_model_arguments(arguments) click to toggle source

Filters on the arguments that are declared in the model

Will match if the task arguments for which there is a value in arguments are set to that very value, only looking at arguments that are defined in the model set by with_model.

See also with_arguments

Example:

class TaskModel < Roby::Task
  argument :a
  argument :b
end
task = TaskModel.new(a: 10, b: 20)

# Matches on :a, :b is ignored altogether
TaskMatcher.new.
    with_model(TaskModel).
    with_model_arguments(a: 10) === task # => true
# Looks for both :a and :b
TaskMatcher.new.
    with_model(TaskModel).
    with_model_arguments(a: 10, b: 30) === task # => false
# Matches on :a, :c is ignored as it is not an argument of +TaskModel+
TaskMatcher.new.
    with_model(TaskModel).
    with_model_arguments(a: 10, c: 30) === task # => true

In general, one would use which_fullfills, which sets both the model and the model arguments

# File lib/roby/queries/task_matcher.rb, line 85
def with_model_arguments(arguments)
    valid_arguments = model.inject(Array.new) { |set, model| set | model.arguments.to_a }
    with_arguments(arguments.slice(*valid_arguments))
    self
end