flexible_check_condition_state.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 from flexbe_core import EventState, Logger
3 
4 
5 class FlexibleCheckConditionState(EventState):
6  '''
7  Implements a state that checks if the given condition is true based on multiple userdata inputs
8  provided as a list to the calculation function and returns the corresponding outcome.
9  This state can be used if the further control flow of the behavior depends on a simple condition.
10 
11  -- predicate function The condition whose truth value will be evaluated.
12  It could be a private function (self.foo) manually defined in a behavior's source code
13  or a lambda function (e.g., lambda x: x[0]^2 + x[1]^2).
14  -- input_keys string[] List of available input keys.
15 
16  ># input_keys object[] Input(s) to the calculation function as a list of userdata.
17  The individual inputs can be accessed as list elements (see lambda expression example).
18 
19  <= true Returned if the condition evaluates to True
20  <= false Returned if the condition evaluates to False
21  '''
22 
23  def __init__(self, predicate, input_keys):
24  '''Constructor'''
25  super(FlexibleCheckConditionState, self).__init__(outcomes=['true', 'false'],
26  input_keys=input_keys)
27  self._predicate = predicate
28  self._outcome = 'false'
29 
30  def execute(self, userdata):
31  return self._outcome
32 
33  def on_enter(self, userdata):
34  if self._predicate is not None:
35  try:
36  self._outcome = "true" if self._predicate([userdata[key] for key in self._input_keys]) else 'false'
37  except Exception as e:
38  Logger.logwarn('Failed to execute condition function!\n%s' % str(e))
39  else:
40  Logger.logwarn('Passed no predicate!')


flexbe_states
Author(s): Philipp Schillinger
autogenerated on Sun Dec 13 2020 04:01:46