Class: Roby::EventConstraints::UnboundTaskPredicate
- 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.
Direct Known Subclasses
BinaryCommutativePredicate, False, Negate, Never, SingleEvent
Defined Under Namespace
Classes: And, BinaryCommutativePredicate, CompiledPredicate, False, FollowedBy, Negate, Never, NotFollowedBy, Or, SingleEvent
Instance Method Summary collapse
-
#and(other_predicate) ⇒ Object
Returns a predicate that is true if both
self
andother_predicate
are true. -
#compile ⇒ Object
Predicates are first represented as an AST using the subclasses of UnboundTaskPredicate, but are then compiled into code before being evaluated (for performance reasons).
-
#evaluate(task) ⇒ Object
Evaluates this predicate on
task
. -
#explain_false(task) ⇒ Object
Returns an Explanation object that explains why
self
is false. -
#explain_static(task) ⇒ Object
Returns an Explanation object that explains why
self
will not change its value anymore. -
#explain_true(task) ⇒ Object
Returns an Explanation object that explains why
self
is true. -
#initialize ⇒ UnboundTaskPredicate
constructor
A new instance of UnboundTaskPredicate.
-
#negate ⇒ Object
Returns a predicate that is the negation of
self
. -
#or(other_predicate) ⇒ Object
Returns a predicate that is true if either or both of
self
andother_predicate
are true. - #pretty_print(pp) ⇒ Object
- #to_unbound_task_predicate ⇒ Object
Constructor Details
#initialize ⇒ UnboundTaskPredicate
Returns a new instance of UnboundTaskPredicate.
169 170 171 172 |
# File 'lib/roby/event_constraints.rb', line 169 def initialize @compiled_predicate = CompiledPredicate.new freeze end |
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.
184 185 186 187 188 189 190 191 |
# File 'lib/roby/event_constraints.rb', line 184 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 |
#compile ⇒ Object
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
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/roby/event_constraints.rb', line 256 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 .singleton_class .class_eval <<-CODE, __FILE__, __LINE__ + 1 def evaluate(task) #{prelude} #{code} end CODE @compiled_predicate.ready = true end |
#evaluate(task) ⇒ Object
Evaluates this predicate on task
. It returns either true or false.
275 276 277 278 |
# File 'lib/roby/event_constraints.rb', line 275 def evaluate(task) compile unless @compiled_predicate.ready @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).
226 |
# File 'lib/roby/event_constraints.rb', line 226 def explain_false(task); 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)
233 |
# File 'lib/roby/event_constraints.rb', line 233 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).
220 |
# File 'lib/roby/event_constraints.rb', line 220 def explain_true(task); end |
#negate ⇒ Object
Returns a predicate that is the negation of self
Because of the “not” semantic, the predicate is static if self
is static.
212 213 214 |
# File 'lib/roby/event_constraints.rb', line 212 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.
199 200 201 202 203 204 205 206 |
# File 'lib/roby/event_constraints.rb', line 199 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
235 236 237 |
# File 'lib/roby/event_constraints.rb', line 235 def pretty_print(pp) pp.text to_s end |
#to_unbound_task_predicate ⇒ Object
174 175 176 |
# File 'lib/roby/event_constraints.rb', line 174 def to_unbound_task_predicate self end |