Differences

This shows you the differences between two versions of the page.

Link to this comparison view

doc:pycram:fluents [2021/07/08 12:01] – created jdechdoc:pycram:fluents [2021/07/09 09:21] (current) jdech
Line 25: Line 25:
 (f / 2 == 0).wait_for() (f / 2 == 0).wait_for()
 </code> </code>
-Every time the value of f is changed the condition will be checked and because operator on fluents generate new fluents to wait_for method is also available.+Every time the value of f is changed the condition will be checked and because operator on fluents generate new fluents the wait_for method is also available
 +For the purpose the comparison operators <, <=, ==, !=, >, >= are overloaded as well as the arithmetic operators +, -, *, / so they return a fluent network when used. Because 'is', 'and', 'or', 'not' and 'is not' are keywords and can't therefore not be overloaded the fluent implements its own version which has the same functionality. The fluent implementations are IS, AND, OR, NOT, IS_NOT.
  
 +For these operators to work it is enough if one of the two parameter is a fluent. However, the parameter should be compatible with one another under this operator. For example, a fluent holding an integer can't be used with the / operator with a string because the operator is not defined for the two types. 
  
 +If you want to execute code every time the value of a fluent is changed you can use the macro 'whenever', this macro is meant to be used as a loop and executes the code block every time the given fluent is changed and the new value is not None. An example on how to use whenever can be seen below. 
 +<code>
 +from pycram.fluent import macros, whenever
 +
 +with whenever(f):
 +    statement1 
 +    statement2
 +</code>
 +
 +It is also possible to execute a block every time a fluent is changed, even when the new value is None. This can be done by calling the pulsed function of the fluent in question.
 +<code>
 +from pycram.fluent import macros, whenever
 +
 +with whenever(f.pulsed()):
 +    statement1 
 +    statement2
 +</code>
 +The pulsed function returns again a fluent of which the value changes to True if the value of the parent fluent changes. 
 +
 +A problem that might occur when using whenever is that the value of the fluent can change while the code is still executed. The whenever macro has different behaviour implemented how to handle missed changes. The behaviours are:
 +
 +    * Changes while execution are ignored 
 +    * The code is executed once more, no matter how many times the value was changed during execution 
 +    * The code is executed again for every missed change
 +
 +The desired behaviour can be chosen by passing the corresponding value from the enum to the pulsed function. The available values are NEVER, ONCE, ALWAYS. This can also be seen in the example below.
 +<code>
 +from pycram.fluent import macros, whenever, Behaviour
 +
 +with whenever(f.pulsed(Behaviour.ALWAYS)):
 +    statement1 
 +    statement2
 +</code>
 +With this the code in the with statement will be run again for every missed change of the value of f.