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 CalculationState(EventState):
6  '''
7  Implements a state that can perform a calculation based on userdata.
8  calculation is a function which takes exactly one parameter, input_value from userdata,
9  and its return value is stored in output_value after leaving the state.
10 
11  -- calculation function The function that performs the desired calculation.
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^2, where x will be the input_value).
14 
15  ># input_value object Input to the calculation function.
16 
17  #> output_value object The result of the calculation.
18 
19  <= done Indicates completion of the calculation.
20  '''
21 
22  def __init__(self, calculation):
23  super(CalculationState, self).__init__(outcomes=['done'],
24  input_keys=['input_value'],
25  output_keys=['output_value'])
26  self._calculation = calculation
27  self._calculation_result = None
28 
29  def execute(self, userdata):
30  userdata.output_value = self._calculation_result
31  # nothing to check
32  return 'done'
33 
34  def on_enter(self, userdata):
35  if self._calculation is not None:
36  try:
37  self._calculation_result = self._calculation(userdata.input_value)
38  except Exception as e:
39  Logger.logwarn('Failed to execute calculation function!\n%s' % str(e))
40  else:
41  Logger.logwarn('Passed no calculation!')


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