def create_state_machine2(): state1 = ExecutionState("MyFirstState", state_id="FirstLevel2", path=testing_utils.TEST_SCRIPT_PATH, filename="default_data_port_test_state.py") state0 = ExecutionState("MyZeroState", state_id="ZeroLevel2", path=testing_utils.TEST_SCRIPT_PATH, filename="default_data_port_test_state.py") state0.add_outcome("first_outcome", 3) input_state0 = state0.add_input_data_port("input_data_port1", tuple, (1, 3, 2), data_port_id=1) output_state0 = state0.add_output_data_port("output_data_port1", tuple) state1.add_outcome("first_outcome", 3) input_state1 = state1.add_input_data_port("input_data_port1", tuple, (1, 3, 2), data_port_id=1) output_state1 = state1.add_output_data_port("output_data_port1", tuple) state2 = HierarchyState("MyFirstHierarchyState",state_id="FirstLevel1") state2.add_state(state1) state2.add_state(state0) state2.set_start_state(state0.state_id) state2.add_outcome("Container_Outcome", 6) state2.add_transition(state0.state_id, 3, state1.state_id, None) state2.add_transition(state1.state_id, 3, state2.state_id, 6) input_state2 = state2.add_input_data_port("input_data_port1", tuple) output_state2 = state2.add_output_data_port("output_data_port1", tuple) state2.add_data_flow(state0.state_id, output_state0, state1.state_id, input_state1) state2.add_data_flow(state1.state_id, output_state1, state2.state_id, output_state2) return StateMachine(state2)
def create_state_machine(): state1 = ExecutionState("MyFirstState", state_id="FirstLevel2", path=testing_utils.TEST_SCRIPT_PATH, filename="default_data_port_test_state.py") state1.add_outcome("first_outcome", 3) input_state1 = state1.add_input_data_port("input_data_port1", "str", "default_value") output_state1 = state1.add_output_data_port("output_data_port1", "str") state2 = HierarchyState("MyFirstHierarchyState", state_id="FirstLevel1") state2.add_state(state1) state2.set_start_state(state1.state_id) state2.add_outcome("Container_Outcome", 6) state2.add_transition(state1.state_id, 3, state2.state_id, 6) input_state2 = state2.add_input_data_port("input_data_port1", "str") output_state2 = state2.add_output_data_port("output_data_port1", "str") # state2.add_data_flow(state2.state_id, # input_state2, # state1.state_id, # input_state1) state2.add_data_flow(state1.state_id, output_state1, state2.state_id, output_state2) return StateMachine(state2)
def create_state_machine(): state1 = ExecutionState("first_state", path=testing_utils.TEST_SCRIPT_PATH, filename="scoped_data_test_state1.py") state1.add_outcome("first_outcome", 3) state1.add_input_data_port("data_input_port1", "float") state1.add_output_data_port("data_output_port1", "float") state2 = ExecutionState("second_state", path=testing_utils.TEST_SCRIPT_PATH, filename="scoped_data_test_state2.py") state2.add_outcome("first_outcome", 3) state2.add_input_data_port("data_input_port1", "float") state2.add_output_data_port("data_output_port1", "float") state3 = HierarchyState("hierarchy_state") state3.add_state(state1) state3.add_state(state2) state3.set_start_state(state1.state_id) state3.add_outcome("Container_Outcome", 6) state3.add_transition(state1.state_id, 3, state2.state_id, None) state3.add_transition(state2.state_id, 3, state3.state_id, 6) state3.add_input_data_port("data_input_port1", "float", 22.0) state3.add_output_data_port("data_output_port1", "float") state3.add_data_flow(state3.state_id, state3.get_io_data_port_id_from_name_and_type("data_input_port1", InputDataPort), state1.state_id, state1.get_io_data_port_id_from_name_and_type("data_input_port1", InputDataPort)) state3.add_data_flow(state1.state_id, state1.get_io_data_port_id_from_name_and_type("data_output_port1", OutputDataPort), state2.state_id, state2.get_io_data_port_id_from_name_and_type("data_input_port1", InputDataPort)) state3.add_data_flow(state2.state_id, state2.get_io_data_port_id_from_name_and_type("data_output_port1", OutputDataPort), state3.state_id, state3.get_io_data_port_id_from_name_and_type("data_output_port1", OutputDataPort)) return StateMachine(state3)
def create_execution_state_library_state_machine(): rafcon.core.singleton.library_manager.initialize() library_container_state = HierarchyState("libContainerState", state_id="libContainerState") lib_state = LibraryState("temporary_libraries", "execution_library", "0.1", "library_execution_state", state_id="library_execution_state") library_container_state.add_state(lib_state) library_container_state.set_start_state(lib_state.state_id) library_container_state.add_transition(lib_state.state_id, 0, library_container_state.state_id, 0) lib_container_input = library_container_state.add_input_data_port( "data_input_port1", "float", 32.0) lib_container_output = library_container_state.add_output_data_port( "data_output_port1", "float") library_container_state.add_data_flow( library_container_state.state_id, lib_container_input, lib_state.state_id, lib_state.get_io_data_port_id_from_name_and_type( "data_input_port1", InputDataPort)) library_container_state.add_data_flow( lib_state.state_id, lib_state.get_io_data_port_id_from_name_and_type( "data_output_port1", OutputDataPort), library_container_state.state_id, lib_container_output) return StateMachine(library_container_state)
def create_state_machine(): from rafcon.core.states.hierarchy_state import HierarchyState from rafcon.core.states.execution_state import ExecutionState from rafcon.core.state_machine import StateMachine state1 = ExecutionState('State1', state_id='STATE1') state2 = ExecutionState('State2') state4 = ExecutionState('Nested') output_state4 = state4.add_output_data_port("out", "int") state5 = ExecutionState('Nested2') input_state5 = state5.add_input_data_port("in", "int", 0) state3 = HierarchyState(name='State3', state_id='STATE3') state3.add_state(state4) state3.add_state(state5) state3.set_start_state(state4) state3.add_scoped_variable("share", "int", 3) state3.add_transition(state4.state_id, 0, state5.state_id, None) state3.add_transition(state5.state_id, 0, state3.state_id, 0) state3.add_data_flow(state4.state_id, output_state4, state5.state_id, input_state5) ctr_state = HierarchyState(name="Container", state_id='ROOTSTATE') ctr_state.add_state(state1) ctr_state.add_state(state2) ctr_state.add_state(state3) ctr_state.set_start_state(state1) ctr_state.add_transition(state1.state_id, 0, state2.state_id, None) ctr_state.add_transition(state2.state_id, 0, state3.state_id, None) ctr_state.add_transition(state3.state_id, 0, ctr_state.state_id, 0) ctr_state.name = "Container" state_machine = StateMachine(ctr_state) return state_machine
def create_hierarchy_state(): state1 = ExecutionState("MyFirstState", path=testing_utils.TEST_SCRIPT_PATH, filename="first_execution_state.py") state1.add_outcome("MyFirstOutcome", 3) state1.add_input_data_port("data_input_port1", "float") state1.add_output_data_port("faulty_output_port", "float") state1.add_output_data_port("data_output_port1", "float") state2 = HierarchyState("MyFirstHierarchyState") state2.add_state(state1) state2.set_start_state(state1.state_id) state2.add_outcome("Container_Outcome", 6) transition_id = state2.add_transition(state1.state_id, 3, state2.state_id, 6) # print state2.transitions[transition_id] input_data_port_id = state2.add_input_data_port("input1", "float", 42.0, data_port_id=42) state2.add_output_data_port("output1", "float") state2.add_data_flow( state2.state_id, state2.get_io_data_port_id_from_name_and_type("input1", InputDataPort), state1.state_id, state1.get_io_data_port_id_from_name_and_type("data_input_port1", InputDataPort)) state2.add_data_flow( state1.state_id, state1.get_io_data_port_id_from_name_and_type("data_output_port1", OutputDataPort), state2.state_id, state2.get_io_data_port_id_from_name_and_type("output1", OutputDataPort)) return state2
def create_state_machine(): state1 = ExecutionState("scoped_data_test_state", path=testing_utils.TEST_SCRIPT_PATH, filename="scoped_variable_test_state.py") state1.add_outcome("loop", 1) input1_state1 = state1.add_input_data_port("input_data_port1", "float") input2_state1 = state1.add_input_data_port("input_data_port2", "float") output_state1 = state1.add_output_data_port("output_data_port1", "float") state2 = HierarchyState("scoped_data_hierarchy_state") state2.add_state(state1) state2.set_start_state(state1.state_id) state2.add_transition(state1.state_id, 0, state2.state_id, 0) state2.add_transition(state1.state_id, 1, state1.state_id, None) input_state2 = state2.add_input_data_port("input_data_port1", "float", 10.0) output_state2 = state2.add_output_data_port("output_data_port1", "float") scoped_variable_state2 = state2.add_scoped_variable( "scoped_variable1", "float", 12.0) state2.add_data_flow( state2.state_id, state2.get_scoped_variable_from_name("scoped_variable1"), state1.state_id, input1_state1) state2.add_data_flow(state2.state_id, input_state2, state1.state_id, input2_state1) state2.add_data_flow(state1.state_id, output_state1, state2.state_id, output_state2) state2.add_data_flow(state1.state_id, output_state1, state2.state_id, scoped_variable_state2) return StateMachine(state2)
def create_state_machine(): state1 = ExecutionState("DummyState1", path=testing_utils.TEST_SCRIPT_PATH, filename="transition_test_state.py") state1.add_outcome('dummy_outcome_1', 3) state1.add_outcome('dummy_outcome_2', 4) state2 = ExecutionState("DummyState2", path=testing_utils.TEST_SCRIPT_PATH, filename="transition_test_state.py") state2.add_outcome('dummy_outcome_1', 3) state2.add_outcome('dummy_outcome_2', 4) state3 = ExecutionState("DummyState3", path=testing_utils.TEST_SCRIPT_PATH, filename="transition_test_state.py") state3.add_outcome('dummy_outcome_1', 3) state3.add_outcome('dummy_outcome_2', 4) state4 = HierarchyState("DummyHierarchyState") state4.add_state(state1) state4.set_start_state(state1.state_id) state4.add_state(state2) state4.add_state(state3) state4.add_outcome("final_outcome", 5) state4.add_transition(state1.state_id, 3, state2.state_id, None) state4.add_transition(state1.state_id, 4, state3.state_id, None) state4.add_transition(state2.state_id, 3, state4.state_id, 5) state4.add_transition(state3.state_id, 3, state4.state_id, 5) state4.add_transition(state3.state_id, 4, state4.state_id, 5) t = state4.add_transition(state2.state_id, 4, state1.state_id, None) state4.remove_transition(t) state4.add_transition(state2.state_id, 4, state1.state_id, None) # no target at all with raises(ValueError): state4.add_transition(state3.state_id, 4, None, None) # no to_state with raises(ValueError): state4.add_transition(state3.state_id, 4, None, 5) # start transition already existing with raises(ValueError): state4.add_transition(None, None, state3.state_id, None) state4.start_state_id = None state4.add_transition(None, None, state1.state_id, None) return StateMachine(state4)
def test_save_libraries(caplog): s = storage state1 = ExecutionState("library_execution_state1", path=testing_utils.TEST_SCRIPT_PATH, filename="library_execution_state1.py") input_state1 = state1.add_input_data_port("data_input_port1", "float") output_state1 = state1.add_output_data_port("data_output_port1", "float") state2 = ExecutionState("library_execution_state2", path=testing_utils.TEST_SCRIPT_PATH, filename="library_execution_state2.py") input_state2 = state2.add_input_data_port("data_input_port1", "float") output_state2 = state2.add_output_data_port("data_output_port1", "float") state3 = HierarchyState("library_hierarchy_state1") state3.add_state(state1) state3.add_state(state2) state3.set_start_state(state1.state_id) state3.add_transition(state1.state_id, 0, state2.state_id, None) state3.add_transition(state2.state_id, 0, state3.state_id, 0) input_state3 = state3.add_input_data_port("data_input_port1", "float", 1.0) output_state3 = state3.add_output_data_port("data_output_port1", "float", 2.0) state3.add_data_flow(state3.state_id, input_state3, state1.state_id, input_state1) state3.add_data_flow(state1.state_id, output_state1, state2.state_id, input_state2) state3.add_data_flow(state2.state_id, output_state2, state3.state_id, output_state3) # save hierarchy state as state machine s.save_state_machine_to_path(StateMachine(state3), join(TEST_LIBRARY_PATH, "hierarchy_library")) # save execution state as state machine s.save_state_machine_to_path(StateMachine(state1), join(TEST_LIBRARY_PATH, "execution_library")) # save hierarchy state as nested state machines state3.name = "library_nested1" s.save_state_machine_to_path(StateMachine(state3), join(TEST_LIBRARY_PATH, "library_container", "library_nested1"), delete_old_state_machine=True) state3.name = "library_nested2" s.save_state_machine_to_path(StateMachine(state3), join(TEST_LIBRARY_PATH, "library_container", "library_nested2"), delete_old_state_machine=True) testing_utils.assert_logger_warnings_and_errors(caplog)
def create_hierarchy_state(number_child_states=10, sleep=False): hierarchy = HierarchyState("hierarchy1") hierarchy.add_outcome("hierarchy_outcome", 1) hierarchy.add_input_data_port("hierarchy_input_port1", "float", 42.0) hierarchy.add_output_data_port("hierarchy_output_port1", "float") last_state = None for i in range(number_child_states): if sleep: state = ExecutionState("state" + str(i), path=testing_utils.TEST_SCRIPT_PATH, filename="hello_world_sleep.py") else: state = ExecutionState("state" + str(i)) hierarchy.add_state(state) state.add_input_data_port("input1", "float") state.add_output_data_port("output1", "float") if not last_state: hierarchy.set_start_state(state.state_id) hierarchy.add_data_flow( hierarchy.state_id, hierarchy.get_io_data_port_id_from_name_and_type( "hierarchy_input_port1", InputDataPort), state.state_id, state.get_io_data_port_id_from_name_and_type( "input1", InputDataPort)) else: hierarchy.add_transition(last_state.state_id, 0, state.state_id, None) # connect data ports state 1 hierarchy.add_data_flow( last_state.state_id, last_state.get_io_data_port_id_from_name_and_type( "output1", OutputDataPort), state.state_id, state.get_io_data_port_id_from_name_and_type( "input1", InputDataPort)) last_state = state hierarchy.add_data_flow( last_state.state_id, last_state.get_io_data_port_id_from_name_and_type( "output1", OutputDataPort), hierarchy.state_id, hierarchy.get_io_data_port_id_from_name_and_type( "hierarchy_output_port1", OutputDataPort)) hierarchy.add_transition(last_state.state_id, 0, hierarchy.state_id, 1) return hierarchy
def return_loop_state_machine(): state1 = ExecutionState("MyFirstState", path=testing_utils.TEST_SCRIPT_PATH, filename="loop_state1.py") state1.add_outcome("MyFirstOutcome", 3) state2 = ExecutionState("MySecondState", path=testing_utils.TEST_SCRIPT_PATH, filename="loop_state2.py") state2.add_outcome("FirstOutcome", 3) state3 = HierarchyState("MyFirstHierarchyState") state3.add_state(state1) state3.add_state(state2) state3.set_start_state(state1.state_id) state3.add_transition(state1.state_id, 3, state2.state_id, None) state3.add_transition(state2.state_id, 3, state1.state_id, None) return StateMachine(state3)
def create_state_machine(): state1 = ExecutionState("MyFirstState", path=testing_utils.TEST_SCRIPT_PATH, filename="global_variable_state.py") state1.add_outcome("first_outcome", 3) input_state1 = state1.add_input_data_port("input_data_port1", "float") output_state1 = state1.add_output_data_port("output_data_port1", "float") state3 = HierarchyState("MyFirstHierarchyState") state3.add_state(state1) state3.set_start_state(state1.state_id) state3.add_outcome("Container_Outcome", 6) state3.add_transition(state1.state_id, 3, state3.state_id, 6) input_state3 = state3.add_input_data_port("input_data_port1", "float", 22.0) output_state3 = state3.add_output_data_port("output_data_port1", "float") state3.add_data_flow(state3.state_id, input_state3, state1.state_id, input_state1) state3.add_data_flow(state1.state_id, output_state1, state3.state_id, output_state3) return StateMachine(state3)
def create_small_state_machine(): import rafcon.core.singleton import rafcon.gui.singleton from rafcon.core.states.execution_state import ExecutionState from rafcon.core.states.hierarchy_state import HierarchyState from rafcon.core.state_machine import StateMachine state1 = ExecutionState('State1', state_id='STATE1') state1.add_input_data_port("in", "str", "zero") state1.add_output_data_port("out", "int", 5) state2 = ExecutionState('State2', state_id='STATE2') state2.add_input_data_port("in", "int", 0) state2.add_output_data_port("out", "int", 3) root_state = HierarchyState(name='State3', state_id='STATE3') root_state.add_state(state1) root_state.add_state(state2) root_state.set_start_state(state1) root_state.add_transition(state1.state_id, 0, state2.state_id, None) sm = StateMachine(root_state) rafcon.core.singleton.state_machine_manager.add_state_machine(sm)
def create_state_machine(): from rafcon.core.states.execution_state import ExecutionState from rafcon.core.states.hierarchy_state import HierarchyState from rafcon.core.state_machine import StateMachine print("create models") logger.setLevel(logging.VERBOSE) for handler in logging.getLogger('gtkmvc3').handlers: logging.getLogger('gtkmvc3').removeHandler(handler) state1 = ExecutionState('State1', state_id='STATE1') output_state1 = state1.add_output_data_port("output", "int") input_state1 = state1.add_input_data_port("input", "str", "zero") state2 = ExecutionState('State2', state_id='STATE2') input_par_state2 = state2.add_input_data_port("par", "int", 0) output_res_state2 = state2.add_output_data_port("res", "int") state4 = HierarchyState(name='Nested', state_id='NESTED') state4.add_outcome('GoGo') output_state4 = state4.add_output_data_port("out", "int") state5 = ExecutionState('Nested2', state_id='NESTED2') state5.add_outcome('HereWeGo') input_state5 = state5.add_input_data_port("in", "int", 0) state3 = HierarchyState(name='State3', state_id='STATE3') input_state3 = state3.add_input_data_port("input", "int", 0) output_state3 = state3.add_output_data_port("output", "int") state3.add_state(state4) state3.add_state(state5) state3.set_start_state(state4) state3.add_scoped_variable("share", "int", 3) state3.add_transition(state4.state_id, 0, state5.state_id, None) state3.add_transition(state5.state_id, 0, state3.state_id, 0) state3.add_data_flow(state4.state_id, output_state4, state5.state_id, input_state5) state3.add_outcome('Branch1') state3.add_outcome('Branch2') ctr_state = HierarchyState(name="Container", state_id='CONT2') ctr_state.add_state(state1) ctr_state.add_state(state2) ctr_state.add_state(state3) input_ctr_state = ctr_state.add_input_data_port("ctr_in", "str", "zero") output_ctr_state = ctr_state.add_output_data_port("ctr_out", "int") ctr_state.set_start_state(state1) ctr_state.add_transition(state1.state_id, 0, state2.state_id, None) ctr_state.add_transition(state2.state_id, 0, state3.state_id, None) ctr_state.add_transition(state3.state_id, 0, ctr_state.state_id, 0) ctr_state.add_data_flow(state1.state_id, output_state1, state2.state_id, input_par_state2) ctr_state.add_data_flow(state2.state_id, output_res_state2, state3.state_id, input_state3) ctr_state.add_data_flow(ctr_state.state_id, input_ctr_state, state1.state_id, input_state1) ctr_state.add_data_flow(state3.state_id, output_state3, ctr_state.state_id, output_ctr_state) ctr_state.name = "Container" ctr_state.add_input_data_port("input", "str", "default_value1") ctr_state.add_input_data_port("pos_x", "str", "default_value2") ctr_state.add_input_data_port("pos_y", "str", "default_value3") ctr_state.add_output_data_port("output", "str", "default_value1") ctr_state.add_output_data_port("result", "str", "default_value2") scoped_variable1_ctr_state = ctr_state.add_scoped_variable( "scoped", "str", "default_value1") scoped_variable2_ctr_state = ctr_state.add_scoped_variable( "my_var", "str", "default_value1") scoped_variable3_ctr_state = ctr_state.add_scoped_variable( "ctr", "int", 42) ctr_state.add_data_flow(ctr_state.state_id, input_ctr_state, ctr_state.state_id, scoped_variable1_ctr_state) ctr_state.add_data_flow(state1.state_id, output_state1, ctr_state.state_id, scoped_variable3_ctr_state) state_dict = { 'Container': ctr_state, 'State1': state1, 'State2': state2, 'State3': state3, 'Nested': state4, 'Nested2': state5 } sm = StateMachine(ctr_state) return sm
def create_state_machine(): from rafcon.core.states.execution_state import ExecutionState from rafcon.core.states.hierarchy_state import HierarchyState from rafcon.core.state_machine import StateMachine state1 = ExecutionState('State1', state_id="State1") output_state1 = state1.add_output_data_port("output", "int") input_state1 = state1.add_input_data_port("input", "str", "zero") state2 = ExecutionState('State2', state_id="State2") input_par_state2 = state2.add_input_data_port("par", "int", 0) output_res_state2 = state2.add_output_data_port("res", "int") state4 = HierarchyState(name='Nested', state_id="Nested") state4.add_outcome('GoGo') output_state4 = state4.add_output_data_port("out", "int") state5 = ExecutionState('Nested2', state_id="Nested2") state5.add_outcome('HereWeGo') input_state5 = state5.add_input_data_port("in", "int", 0) state3 = HierarchyState(name='State3', state_id="State3") input_state3 = state3.add_input_data_port("input", "int", 0) output_state3 = state3.add_output_data_port("output", "int") state3.add_state(state4) state3.add_state(state5) state3.set_start_state(state4) state3.add_scoped_variable("share", "int", 3) state3.add_transition(state4.state_id, 0, state5.state_id, None) state3.add_transition(state5.state_id, 0, state3.state_id, 0) state3.add_data_flow(state4.state_id, output_state4, state5.state_id, input_state5) state3.add_outcome('Branch1') state3.add_outcome('Branch2') ctr_state = HierarchyState(name="Root", state_id="Root") ctr_state.add_state(state1) ctr_state.add_state(state2) ctr_state.add_state(state3) input_ctr_state = ctr_state.add_input_data_port("ctr_in", "str", "zero") output_ctr_state = ctr_state.add_output_data_port("ctr_out", "int") ctr_state.set_start_state(state1) ctr_state.add_transition(state1.state_id, 0, state2.state_id, None) ctr_state.add_transition(state2.state_id, 0, state3.state_id, None) ctr_state.add_transition(state3.state_id, 0, ctr_state.state_id, 0) ctr_state.add_data_flow(state1.state_id, output_state1, state2.state_id, input_par_state2) ctr_state.add_data_flow(state2.state_id, output_res_state2, state3.state_id, input_state3) ctr_state.add_data_flow(ctr_state.state_id, input_ctr_state, state1.state_id, input_state1) ctr_state.add_data_flow(state3.state_id, output_state3, ctr_state.state_id, output_ctr_state) ctr_state.name = "Container" ctr_state.add_input_data_port("input", "str", "default_value1") ctr_state.add_input_data_port("pos_x", "str", "default_value2") ctr_state.add_input_data_port("pos_y", "str", "default_value3") ctr_state.add_output_data_port("output", "str", "default_value1") ctr_state.add_output_data_port("result", "str", "default_value2") scoped_variable1_ctr_state = ctr_state.add_scoped_variable( "scoped", "str", "default_value1") scoped_variable3_ctr_state = ctr_state.add_scoped_variable( "ctr", "int", 42) ctr_state.add_data_flow(ctr_state.state_id, input_ctr_state, ctr_state.state_id, scoped_variable1_ctr_state) ctr_state.add_data_flow(state1.state_id, output_state1, ctr_state.state_id, scoped_variable3_ctr_state) state_dict = { 'Container': ctr_state, 'State1': state1, 'State2': state2, 'State3': state3, 'Nested': state4, 'Nested2': state5 } sm = StateMachine(ctr_state) return state_dict, sm
def create_turtle_statemachine(base_path, example_path): basic_turtle_demo_state = HierarchyState("BasicTurtleDemo") init_ros_node = LibraryState("ros_libraries", "init_ros_node", "0.1", "init ros node") basic_turtle_demo_state.add_state(init_ros_node) basic_turtle_demo_state.set_start_state(init_ros_node.state_id) ######################################################## # Turtle Concurrency State ######################################################## preemptive_concurrency_state = PreemptiveConcurrencyState( "Turtle Concurrency State") basic_turtle_demo_state.add_state(preemptive_concurrency_state) basic_turtle_demo_state.add_transition( init_ros_node.state_id, 0, preemptive_concurrency_state.state_id, None) basic_turtle_demo_state.add_transition( preemptive_concurrency_state.state_id, 0, basic_turtle_demo_state.state_id, 0) ######################################################## # Subscribe to turtle position concurrency State ######################################################## subscribe_to_turtle_position_hierarchy_state = HierarchyState( "Turtle Position Subscriber Hierarchy State") preemptive_concurrency_state.add_state( subscribe_to_turtle_position_hierarchy_state) spawn_turtle = LibraryState("turtle_libraries", "turtle_position_subscriber", "0.1", "subscribe to turtle position") subscribe_to_turtle_position_hierarchy_state.add_state(spawn_turtle) subscribe_to_turtle_position_hierarchy_state.set_start_state( spawn_turtle.state_id) subscribe_to_turtle_position_hierarchy_state.add_transition( spawn_turtle.state_id, 0, spawn_turtle.state_id, None) ######################################################## # Move Turtle Hierarchy State ######################################################## move_turtle_hierarchy_state = HierarchyState("Move Turtle Hierarchy State") preemptive_concurrency_state.add_state(move_turtle_hierarchy_state) preemptive_concurrency_state.add_transition( move_turtle_hierarchy_state.state_id, 0, preemptive_concurrency_state.state_id, 0) spawn_turtle = LibraryState("turtle_libraries", "spawn_turtle", "0.1", "spawn turtle") move_turtle_hierarchy_state.add_state(spawn_turtle) move_turtle_hierarchy_state.set_start_state(spawn_turtle.state_id) wait1 = ExecutionState("Wait1", path=base_path, filename="wait.py") move_turtle_hierarchy_state.add_state(wait1) move_turtle_hierarchy_state.add_transition(spawn_turtle.state_id, 0, wait1.state_id, None) teleport_turtle = LibraryState("turtle_libraries", "teleport_turtle", "0.1", "teleport turtle") move_turtle_hierarchy_state.add_state(teleport_turtle) move_turtle_hierarchy_state.add_transition(wait1.state_id, 0, teleport_turtle.state_id, None) wait2 = ExecutionState("Wait2", path=base_path, filename="wait.py") move_turtle_hierarchy_state.add_state(wait2) move_turtle_hierarchy_state.add_transition(teleport_turtle.state_id, 0, wait2.state_id, None) clear_field = LibraryState("turtle_libraries", "clear_field", "0.1", "clear field") move_turtle_hierarchy_state.add_state(clear_field) move_turtle_hierarchy_state.add_transition(wait2.state_id, 0, clear_field.state_id, None) wait3 = LibraryState(name="Wait3", library_path="generic", library_name="wait") move_turtle_hierarchy_state.add_state(wait3) move_turtle_hierarchy_state.add_transition(clear_field.state_id, 0, wait3.state_id, None) set_velocity1 = LibraryState("turtle_libraries", "set_velocity", "0.1", "set velocity1") move_turtle_hierarchy_state.add_state(set_velocity1) move_turtle_hierarchy_state.add_transition(wait3.state_id, 0, set_velocity1.state_id, None) wait4 = ExecutionState("Wait4", path=base_path, filename="wait.py") move_turtle_hierarchy_state.add_state(wait4) move_turtle_hierarchy_state.add_transition(set_velocity1.state_id, 0, wait4.state_id, None) move_to_position = LibraryState("turtle_libraries", "move_to_position", "0.1", "move to position") move_turtle_hierarchy_state.add_state(move_to_position) move_turtle_hierarchy_state.add_transition(wait4.state_id, 0, move_to_position.state_id, None) move_turtle_hierarchy_state.add_transition(move_to_position.state_id, 1, move_to_position.state_id, None) kill_turtle = LibraryState("turtle_libraries", "kill_turtle", "0.1", "kill turtle") move_turtle_hierarchy_state.add_state(kill_turtle) move_turtle_hierarchy_state.add_transition(move_to_position.state_id, 0, kill_turtle.state_id, None) move_turtle_hierarchy_state.add_transition( kill_turtle.state_id, 0, move_turtle_hierarchy_state.state_id, 0) return basic_turtle_demo_state
def create_models(): import rafcon.core.singleton from rafcon.core.states.execution_state import ExecutionState from rafcon.core.states.hierarchy_state import HierarchyState from rafcon.core.state_machine import StateMachine state1 = ExecutionState('State1') output_state1 = state1.add_output_data_port("output", "int") input_state1 = state1.add_input_data_port("input", "str", "zero") state2 = ExecutionState('State2') input_par_state2 = state2.add_input_data_port("par", "int", 0) output_res_state2 = state2.add_output_data_port("res", "int") state4 = HierarchyState(name='Nested') state4.add_outcome('GoGo') output_state4 = state4.add_output_data_port("out", "int") state5 = ExecutionState('Nested2') state5.add_outcome('HereWeGo') input_state5 = state5.add_input_data_port("in", "int", 0) state3 = HierarchyState(name='State3') input_state3 = state3.add_input_data_port("input", "int", 0) output_state3 = state3.add_output_data_port("output", "int") state3.add_state(state4) state3.add_state(state5) state3.set_start_state(state4) state3.add_scoped_variable("share", "int", 3) state3.add_transition(state4.state_id, 0, state5.state_id, None) state3.add_transition(state5.state_id, 0, state3.state_id, 0) state3.add_data_flow(state4.state_id, output_state4, state5.state_id, input_state5) state3.add_outcome('Branch1') state3.add_outcome('Branch2') ctr_state = HierarchyState(name="Container") ctr_state.add_state(state1) ctr_state.add_state(state2) ctr_state.add_state(state3) input_ctr_state = ctr_state.add_input_data_port("ctr_in", "str", "zero") output_ctr_state = ctr_state.add_output_data_port("ctr_out", "int") ctr_state.set_start_state(state1) ctr_state.add_transition(state1.state_id, 0, state2.state_id, None) ctr_state.add_transition(state2.state_id, 0, state3.state_id, None) ctr_state.add_transition(state3.state_id, 0, ctr_state.state_id, 0) ctr_state.add_data_flow(state1.state_id, output_state1, state2.state_id, input_par_state2) ctr_state.add_data_flow(state2.state_id, output_res_state2, state3.state_id, input_state3) ctr_state.add_data_flow(ctr_state.state_id, input_ctr_state, state1.state_id, input_state1) ctr_state.add_data_flow(state3.state_id, output_state3, ctr_state.state_id, output_ctr_state) ctr_state.add_input_data_port("input", "str", "default_value1") ctr_state.add_input_data_port("pos_x", "str", "default_value2") ctr_state.add_input_data_port("pos_y", "str", "default_value3") ctr_state.add_output_data_port("output", "str", "default_value1") ctr_state.add_output_data_port("result", "str", "default_value2") scoped_variable1_ctr_state = ctr_state.add_scoped_variable( "scoped", "str", "default_value1") scoped_variable2_ctr_state = ctr_state.add_scoped_variable( "my_var", "str", "default_value1") scoped_variable3_ctr_state = ctr_state.add_scoped_variable( "ctr", "int", 42) ctr_state.add_data_flow(ctr_state.state_id, input_ctr_state, ctr_state.state_id, scoped_variable1_ctr_state) ctr_state.add_data_flow(state1.state_id, output_state1, ctr_state.state_id, scoped_variable3_ctr_state) state_dict = { 'Container': ctr_state, 'State1': state1, 'State2': state2, 'State3': state3, 'Nested': state4, 'Nested2': state5 } sm = StateMachine(ctr_state) rafcon.core.singleton.state_machine_manager.add_state_machine(sm) testing_utils.wait_for_gui() state_machine_model = rafcon.gui.singleton.state_machine_manager_model.state_machines[ sm.state_machine_id] return ctr_state, state_machine_model, state_dict