Class: Roby::EventConstraints::UnboundTaskPredicate

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

Overview

Represents a temporal logic predicate that applies on the internal events of a single task. As the events are represented by their name, the predicate can be reused to be applied on different tasks.

Defined Under Namespace

Classes: And, BinaryCommutativePredicate, CompiledPredicate, False, FollowedBy, Negate, Never, NotFollowedBy, Or, SingleEvent

Instance Method Summary collapse

Instance Method Details

#and(other_predicate) ⇒ Object

Returns a predicate that is true if both self and other_predicate are true.

Because of the “and” semantic, the predicate is static if one of the two predicates is false and static, or if both predicates are static.



175
176
177
178
179
180
181
182
# File 'lib/roby/event_constraints.rb', line 175

def and(other_predicate)
    if self == other_predicate then self
    elsif other_predicate.kind_of?(UnboundTaskPredicate::False)
        other_predicate
    else
        And.new(self, other_predicate)
    end
end

#compileObject

Predicates are first represented as an AST using the subclasses of UnboundTaskPredicate, but are then compiled into code before being evaluated (for performance reasons).

This is the main call that performs this compilation



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/roby/event_constraints.rb', line 245

def compile
    prelude = required_events.map do |event_name|
        "    task_event_#{event_name} = task.event(:#{event_name})\n" +
        "    task_#{event_name} = task_event_#{event_name}.last"
    end.join("\n")

    compiled_predicate = CompiledPredicate.new
    eval "def compiled_predicate.evaluate(task)\n\#{prelude}\n    \#{code}\nend\n    END\n    @compiled_predicate = compiled_predicate\nend\n", binding, __FILE__, __LINE__+1

#evaluate(task) ⇒ Object

Evaluates this predicate on task. It returns either true or false.



263
264
265
266
# File 'lib/roby/event_constraints.rb', line 263

def evaluate(task)
    compile if !@compiled_predicate || !@compiled_predicate.respond_to?(:evaluate)
    @compiled_predicate.evaluate(task)
end

#explain_false(task) ⇒ Object

Returns an Explanation object that explains why self is false. Note that it is valid only if evaluate(task) actually returned false (it will silently return an invalid explanation if evaluate(task) returns true).



217
# File 'lib/roby/event_constraints.rb', line 217

def explain_false(task); nil end

#explain_static(task) ⇒ Object

Returns an Explanation object that explains why self will not change its value anymore.

Note that it is valid only if static?(task) actually returned true (it will silently return an invalid explanation otherwise)



224
225
# File 'lib/roby/event_constraints.rb', line 224

def explain_static(task)
end

#explain_true(task) ⇒ Object

Returns an Explanation object that explains why self is true. Note that it is valid only if evaluate(task) actually returned true (it will silently return an invalid explanation if evaluate(task) returns false).



211
# File 'lib/roby/event_constraints.rb', line 211

def explain_true(task); nil end

#negateObject

Returns a predicate that is the negation of self

Because of the “not” semantic, the predicate is static if self is static.



203
204
205
# File 'lib/roby/event_constraints.rb', line 203

def negate
    Negate.new(self)
end

#or(other_predicate) ⇒ Object

Returns a predicate that is true if either or both of self and other_predicate are true.

Because of the “or” semantic, the predicate is static if one of the two predicates are true and static, or if both predicates are static.



190
191
192
193
194
195
196
197
# File 'lib/roby/event_constraints.rb', line 190

def or(other_predicate)
    if self == other_predicate then self
    elsif other_predicate.kind_of?(UnboundTaskPredicate::False)
        self
    else
        Or.new(self, other_predicate)
    end
end

#pretty_print(pp) ⇒ Object



227
228
229
# File 'lib/roby/event_constraints.rb', line 227

def pretty_print(pp)
    pp.text to_s
end

#to_unbound_task_predicateObject



165
166
167
# File 'lib/roby/event_constraints.rb', line 165

def to_unbound_task_predicate
    self
end