pycram.fluent

Module Contents

Classes

Behavior

Enumeration which can be passed as argument to the pulsed method of fluents to describe how to handle missed pulses in the whenever macro.

Fluent

Implementation of fluents.

class pycram.fluent.Behavior

Bases: enum.Enum

Enumeration which can be passed as argument to the pulsed method of fluents to describe how to handle missed pulses in the whenever macro.

Fields: NEVER – ignore missed pulses. ONCE – if pulses were missed, execute the body once more in total. ALWAYS – if pulses were missed, execute the body once more for each.

NEVER = 1
ONCE = 2
ALWAYS = 3
class pycram.fluent.Fluent(value: typing_extensions.Optional[typing_extensions.Any] = None, name: str = None)

Implementation of fluents.

Fluents are thread-safe proxy objects which are used as variables with changing value. This allows threads to observe them and wait for (specific) changes. One can also observe fluents created by the pulsed method of a fluent. These change their value from None to True whenever the parent gets pulsed (or changes its value and thus gets pulsed).

Fluents can be combined to fluent networks which allows to express complex conditions. A network updates its value whenever one of the fluents it is constructed from changes its value. The most important comparison and math operators (<, <=, ==, !=, >, >=, +, -, *, /) are overloaded to construct a fluent network whenever they are called with at least one fluent as parameter. In addition the comparison operators IS and IS_NOT as well as the logical operators AND, OR and NOT are defined as methods. This is necessary because these operators can’t be overloaded. Fluents constructed by comparison or logical operators have the value True or None instead of False, so that they can be used with the wait_for method because it blocks until a value is not None. User defined operators can be created by passing a function as the fluents value.

Create a new fluent.

Parameters:
  • value – the value of the fluent which can also be a function to create user defined operators (default is None).

  • name – the name of the fluent (default is a random string).

pulsed(handle_missed: Behavior = 2) Fluent

Create a fluent which changes its value from None to True whenever the parent gets pulsed.

Parameters:

handle_missed – see the docstring of the Behavior enumeration to find out more (default is Behavior.ONCE).

pulse() None

Pulse a fluent without changing its value.

whenever(callback: typing_extensions.Callable) None

Registers a callback which is called everytime this Fluent is pulsed. The callback should be a Callable. When the callback is called it gets the current value of this Fluent as an argument.

Parameters:

callback – The callback which should be called when pulsed as a Callable.

add_child(child: Fluent) None

Add a child to the fluent which gets pulsed whenever this fluent gets pulsed, too.

Parameters:

child – the child to add.

get_value() typing_extensions.Any

Return the value of the fluent.

set_value(value: typing_extensions.Any) None

Change the value of the fluent. Changing the value will also pulse the fluent.

Parameters:

value – the new value of the fluent.

wait_for(timeout: typing_extensions.Optional[float] = None)

Block the current thread if the value of the fluent is None, until it is not None or until it timed out.

If the fluent was created by the pulsed method of a fluent, the method blocks until the parent gets pulsed.

The return value is the last return value of the predicate (value is not None) and will evaluate to False if the method timed out.

Parameters:

timeout – the maximum time to wait (default is None).

_compare(operator, other: Fluent) Fluent

This is a helper method for internal usage only.

Create a fluent which value is a function returning True or None depending on the given comparison operator applied to the operands.

Parameters:
  • operator – the comparison operator to apply.

  • other – the other operand.

__lt__(other: Fluent) Fluent

Overload the < comparsion operator.

Parameters:

other – – the other operand.

__leq__(other: Fluent) Fluent

Overload the <= comparsion operator.

Parameters:

other – the other operand.

__eq__(other: Fluent) Fluent

Overload the == comparsion operator.

Parameters:

other – the other operand.

__ne__(other: Fluent) Fluent

Overload the != comparsion operator.

Parameters:

other – the other operand.

IS(other: Fluent) Fluent

Create a fluent which value is True if the value of its parent is the value of the given operand, None otherwise.

Parameters:

other – – the other operand which can also be a fluent.

IS_NOT(other: Fluent) Fluent

Create a fluent which value is True if the value of its parent is not the value of the given operand, None otherwise.

Parameters:

other – – the other operand which can also be a fluent.

__gt__(other: Fluent) Fluent

Overload the > comparison operator.

Parameters:

other – the other operand.

__geq__(other: Fluent) Fluent

Overload the >= comparison operator.

Parameters:

other – the other operand.

_math(operator: typing_extensions.Callable, operand: Fluent, other: Fluent) Fluent

This is a helper method for internal usage only.

Create a fluent which value is a function returning the value of the given math operator applied to the operands.

Parameters:
  • operator – the math operator to apply.

  • operand – the first operand.

  • other – the other operand.

__add__(other: Fluent) Fluent

Overload the + math operator.

Parm other:

the other operand.

__radd__(other: Fluent) Fluent

Overload the + math operator with the first operand not being a fluent.

Parameters:

other – the other operand.

__sub__(other: Fluent) Fluent

Overload the - math operator.

Parameters:

other – the other operand.

__rsub__(other: Fluent) Fluent

Overload the - math operator with the first operand not being a fluent.

Parameters:

other – the other operand.

__mul__(other: Fluent) Fluent

Overload the * math operator.

Parameters:

other – the other operand.

__rmul__(other: Fluent) Fluent

Overload the * math operator with the first operand not being a fluent.

Parameters:

other – the other operand.

__truediv__(other: Fluent) Fluent

Overload the / math operator.

Parameters:

other – the other operand.

__rtruediv__(other) Fluent

Overload the / math operator with the first operand not being a fluent.

Parameters:

other – the other operand.

AND(other: Fluent) Fluent

Create a fluent which value is True if both, the value of its parent and the other operand express True, None otherwise.

Parameters:

other – the other operand which can also be a fluent.

OR(other: Fluent) Fluent

Create a fluent which value is True if either the value of its parent or the other operand express True, None otherwise.

Parameters:

other – the other operand which can also be a fluent.

NOT() Fluent

Create a fluent which value is True if the value of its parent expresses False, None otherwise.