flexible_calculation_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 FlexibleCalculationState(EventState):
6  '''
7  Implements a state that can perform a calculation based on multiple userdata inputs
8  provided as a list to the calculation function.
9 
10  -- calculation function The function that performs the desired calculation.
11  It could be a private function (self.foo) manually defined in a behavior's source code
12  or a lambda function (e.g., lambda x: x[0]^2 + x[1]^2).
13  -- input_keys string[] List of available input keys.
14 
15  ># input_keys object[] Input(s) to the calculation function as a list of userdata.
16  The individual inputs can be accessed as list elements (see lambda expression example).
17 
18  #> output_value object The result of the calculation.
19 
20  <= done Indicates completion of the calculation.
21  '''
22 
23  def __init__(self, calculation, input_keys):
24  super(FlexibleCalculationState, self).__init__(outcomes=['done'],
25  input_keys=input_keys,
26  output_keys=['output_value'])
27  self._calculation = calculation
28  self._calculation_result = None
29 
30  def execute(self, userdata):
31  userdata.output_value = self._calculation_result
32  # nothing to check
33  return 'done'
34 
35  def on_enter(self, userdata):
36  if self._calculation is not None:
37  try:
38  self._calculation_result = self._calculation(**{key: userdata[key] for key in self._input_keys})
39  except Exception as e:
40  Logger.logwarn('Failed to execute calculation function!\n%s' % str(e))
41  else:
42  Logger.logwarn('Passed no calculation!')


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