forked from team-vigir/flexbe_behavior_engine
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request team-vigir#11 from team-vigir/develop
sync upstream repo
- Loading branch information
Showing
3 changed files
with
47 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 45 additions & 52 deletions
97
flexbe_testing/src/flexbe_testing/test/selftest_behavior_sm.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,74 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
########################################################### | ||
# WARNING: Generated code! # | ||
# ************************** # | ||
# Manual changes may get lost if file is generated again. # | ||
# Only code inside the [MANUAL] tags will be kept. # | ||
########################################################### | ||
|
||
from flexbe_core import Behavior, Autonomy, OperatableStateMachine, ConcurrencyContainer, PriorityContainer, Logger | ||
from flexbe_states.calculation_state import CalculationState | ||
from flexbe_states.decision_state import DecisionState | ||
# Additional imports can be added inside the following tags | ||
# [MANUAL_IMPORT] | ||
# | ||
# Content is only intended for the flexbe_testing self-test | ||
# | ||
|
||
# [/MANUAL_IMPORT] | ||
from flexbe_core import Behavior, Autonomy, OperatableStateMachine, EventState, Logger | ||
|
||
|
||
''' | ||
Created on Fri Apr 17 2020 | ||
@author: Philipp Schillinger | ||
''' | ||
class SelftestBehaviorSM(Behavior): | ||
''' | ||
Simple behavior for the flexbe_testing self-test of behaviors. | ||
''' | ||
|
||
''' Simple behavior for the flexbe_testing self-test of behaviors. ''' | ||
|
||
def __init__(self): | ||
super(SelftestBehaviorSM, self).__init__() | ||
self.name = 'Selftest Behavior' | ||
|
||
# parameters of this behavior | ||
self.add_parameter('value', 'wrong') | ||
|
||
# references to used behaviors | ||
|
||
# Additional initialization code can be added inside the following tags | ||
# [MANUAL_INIT] | ||
|
||
# [/MANUAL_INIT] | ||
|
||
# Behavior comments: | ||
|
||
|
||
|
||
def create(self): | ||
# x:30 y:365, x:130 y:365 | ||
_state_machine = OperatableStateMachine(outcomes=['finished', 'failed'], input_keys=['data'], output_keys=['result']) | ||
_state_machine.userdata.data = None | ||
_state_machine.userdata.result = None | ||
|
||
# Additional creation code can be added inside the following tags | ||
# [MANUAL_CREATE] | ||
|
||
# [/MANUAL_CREATE] | ||
|
||
|
||
with _state_machine: | ||
# x:40 y:73 | ||
OperatableStateMachine.add('Modify Data', | ||
CalculationState(calculation=lambda x: x * 2), | ||
SelftestBehaviorSM._CalculationState(calculation=lambda x: x * 2), | ||
transitions={'done': 'Decide Param'}, | ||
autonomy={'done': Autonomy.Off}, | ||
remapping={'input_value': 'data', 'output_value': 'result'}) | ||
|
||
# x:37 y:201 | ||
OperatableStateMachine.add('Decide Param', | ||
DecisionState(outcomes=['finished', 'failed'], conditions=lambda x: 'finished' if self.value == 'correct' else 'failed'), | ||
SelftestBehaviorSM._DecisionState(outcomes=['finished', 'failed'], conditions=lambda x: 'finished' if self.value == 'correct' else 'failed'), | ||
transitions={'finished': 'finished', 'failed': 'failed'}, | ||
autonomy={'finished': Autonomy.Off, 'failed': Autonomy.Off}, | ||
remapping={'input_value': 'data'}) | ||
|
||
|
||
return _state_machine | ||
|
||
|
||
# Private functions can be added inside the following tags | ||
# [MANUAL_FUNC] | ||
|
||
# [/MANUAL_FUNC] | ||
class _CalculationState(EventState): | ||
''' Copy of the flexbe_states.CalculationState for use in the test behavior. ''' | ||
|
||
def __init__(self, calculation): | ||
super(SelftestBehaviorSM._CalculationState, self).__init__(outcomes=['done'], input_keys=['input_value'], output_keys=['output_value']) | ||
self._calculation = calculation | ||
self._calculation_result = None | ||
|
||
def execute(self, userdata): | ||
userdata.output_value = self._calculation_result | ||
return 'done' | ||
|
||
def on_enter(self, userdata): | ||
if self._calculation is not None: | ||
try: | ||
self._calculation_result = self._calculation(userdata.input_value) | ||
except Exception as e: | ||
Logger.logwarn('Failed to execute calculation function!\n%s' % str(e)) | ||
else: | ||
Logger.logwarn('Passed no calculation!') | ||
|
||
class _DecisionState(EventState): | ||
''' Copy of the flexbe_states.DecisionState for use in the test behavior. ''' | ||
|
||
def __init__(self, outcomes, conditions): | ||
super(SelftestBehaviorSM._DecisionState, self).__init__(outcomes=outcomes, input_keys=['input_value']) | ||
self._conditions = conditions | ||
|
||
def execute(self, userdata): | ||
if self._conditions is not None: | ||
outcome = None | ||
try: | ||
outcome = str(self._conditions(userdata.input_value)) | ||
except Exception as e: | ||
Logger.logwarn('Passed no function as predicate!\n%s' % str(e)) | ||
outcome = None | ||
if outcome is not None and outcome in self._outcomes: | ||
return outcome |