class Roby::Queries::Query

A query is a TaskMatcher that applies on a plan. It should, in general, be preferred to TaskMatcher as it uses task indexes to be more efficient.

Queries cache their result. I.e. once each has been called to get the query results, the query will always return the same results until reset has been called.

Attributes

neg_plan_predicates[R]

The set of predicates of Plan which must return false for === to return true.

plan[R]

The plan this query acts on

plan_predicates[R]

The set of predicates of Plan which must return true for === to return true

scope[R]

Search scope for queries on transactions. If equal to :local, the query will apply only on the scope of the searched transaction, otherwise it applies on a virtual plan that is the result of the transaction stack being applied.

The default is :global.

See local_scope and global_scope

Public Class Methods

match_plan_predicates(names) click to toggle source

For each name in names, define the name and not_name methods on Query objects. When one of these methods is called on a Query object, plan.name?(task) must return true (resp. false) for the task to match.

# File lib/roby/queries/query.rb, line 98
            def match_plan_predicates(names)
                names.each do |name, predicate_name|
                    predicate_name ||= name
                    class_eval <<-EOD, __FILE__, __LINE__+1
                    def #{name}
                        if neg_plan_predicates.include?(:#{predicate_name})
                            raise ArgumentError, "trying to match (#{name} & !#{name})"
                        end
                        plan_predicates << :#{predicate_name}
                        self
                    end
                    def not_#{name}
                        if plan_predicates.include?(:#{predicate_name})
                            raise ArgumentError, "trying to match (#{name} & !#{name})"
                        end
                        neg_plan_predicates << :#{predicate_name}
                        self
                    end
                    EOD
                end
            end
new(plan = nil) click to toggle source

Create a query object on the given plan

Calls superclass method Roby::Queries::TaskMatcher::new
# File lib/roby/queries/query.rb, line 14
def initialize(plan = nil)
    @scope = :global
    @plan = plan
    super()
    @plan_predicates = Set.new
    @neg_plan_predicates = Set.new
end

Public Instance Methods

===(task) click to toggle source

True if task matches the query. Call result_set to have the set of tasks which match in the given plan.

Calls superclass method Roby::Queries::TaskMatcher#===
# File lib/roby/queries/query.rb, line 163
def ===(task)
    for pred in plan_predicates
        return unless plan.send(pred, task)
    end
    for neg_pred in neg_plan_predicates
        return if plan.send(neg_pred, task)
    end

    return unless super

    true
end
each(&block) click to toggle source

Iterates on all the tasks in the given plan which match the query

This set is cached, i.e. each will yield the same task set until reset is called.

# File lib/roby/queries/query.rb, line 180
def each(&block)
    return enum_for(__method__) if !block_given?
    plan.query_each(result_set, &block)
end
global_scope() click to toggle source

Changes the scope of this query. See scope.

# File lib/roby/queries/query.rb, line 42
def global_scope
    @scope = :global
    self
end
indexed_sets(index) click to toggle source
# File lib/roby/queries/query.rb, line 59
def indexed_sets(index)
    positive_sets, negative_sets = super

    if plan_predicates.include?(:mission_task?)
        positive_sets << plan.mission_tasks
    elsif neg_plan_predicates.include?(:mission_task?)
        negative_sets << plan.mission_tasks
    end

    if plan_predicates.include?(:permanent_task?)
        positive_sets << plan.permanent_tasks
    elsif neg_plan_predicates.include?(:permanent_task?)
        negative_sets << plan.permanent_tasks
    end
    return positive_sets, negative_sets
end
local_scope() click to toggle source

Changes the scope of this query. See scope.

# File lib/roby/queries/query.rb, line 37
def local_scope
    @scope = :local
    self
end
mission() click to toggle source

Filters missions

Matches tasks in plan that are missions

# File lib/roby/queries/query.rb, line 122
        
not_mission() click to toggle source

Filters out missions

Matches tasks in plan that are not missions

# File lib/roby/queries/query.rb, line 129
        
not_permanent() click to toggle source

Filters out permanent tasks

Matches tasks in plan that are not declared as permanent tasks

# File lib/roby/queries/query.rb, line 149
match_plan_predicates mission: :mission_task?
permanent() click to toggle source

Filters permanent tasks

Matches tasks in plan that are declared as permanent tasks.

# File lib/roby/queries/query.rb, line 136
        
plan=(new_plan) click to toggle source

Changes the plan this query works on. This calls reset (obviously)

# File lib/roby/queries/query.rb, line 48
def plan=(new_plan)
    reset
    @plan = new_plan
end
query() click to toggle source
# File lib/roby/queries/query.rb, line 22
def query
    self
end
reset() click to toggle source

Reinitializes the cached query result.

Queries cache their result, i.e. each will always return the same task set. reset makes sure that the next call to each will return the same value.

# File lib/roby/queries/query.rb, line 81
def reset
    @result_set = nil
    self
end
result_set() click to toggle source

The set of tasks which match in plan. This is a cached value, so use reset to actually recompute this set.

# File lib/roby/queries/query.rb, line 55
def result_set
    @result_set ||= plan.query_result_set(self)
end
roots(relation) click to toggle source

Filters tasks which have no parents in the query itself.

Will filter out tasks which have parents in relation that are included in the query result.

# File lib/roby/queries/query.rb, line 156
def roots(relation)
    @result_set = plan.query_roots(result_set, relation)
    self
end