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 test_state_hash(): state1 = ExecutionState('ES', state_id="12345") state2 = ExecutionState('ES', state_id="12345") state1.add_output_data_port("out1", "int", data_port_id=1) state1.add_output_data_port("out2", "int", data_port_id=2) state1.add_input_data_port("in1", "int", data_port_id=3) state1.add_input_data_port("in2", "int", data_port_id=4) state1.add_outcome("o1", outcome_id=1) state1.add_outcome("o2", outcome_id=2) state2.add_outcome("o2", outcome_id=2) state2.add_outcome("o1", outcome_id=1) state2.add_input_data_port("in2", "int", data_port_id=4) state2.add_input_data_port("in1", "int", data_port_id=3) state2.add_output_data_port("out2", "int", data_port_id=2) state2.add_output_data_port("out1", "int", data_port_id=1) hash1 = hashlib.sha256() hash2 = hashlib.sha256() assert hash1.hexdigest() == hash2.hexdigest() Hashable.update_hash_from_dict(hash1, state1) Hashable.update_hash_from_dict(hash2, state2) assert hash1.hexdigest() == hash2.hexdigest()
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_preemption_state_machine(): state1 = ExecutionState("FirstState", path=testing_utils.TEST_SCRIPT_PATH, filename="concurrence_preemption1.py") state1.add_outcome("FirstOutcome", 3) input_state1 = state1.add_input_data_port("input_data_port1", "float") state2 = ExecutionState("SecondState", path=testing_utils.TEST_SCRIPT_PATH, filename="concurrence_preemption2.py") state2.add_outcome("FirstOutcome", 3) input_state2 = state2.add_input_data_port("input_data_port1", "float") state3 = PreemptiveConcurrencyState("FirstConcurrencyState") state3.add_state(state1) state3.add_state(state2) state3.add_outcome("State1 preempted", 3) input_state3 = state3.add_input_data_port("input_data_port1", "float", 0.1) input2_state3 = state3.add_input_data_port("input_data_port2", "float", 0.1) state3.add_data_flow(state3.state_id, input_state3, state1.state_id, input_state1) state3.add_data_flow(state3.state_id, input2_state3, state2.state_id, input_state2) state3.add_transition(state1.state_id, 3, state3.state_id, 3) return StateMachine(state3)
def test_lock_state_machine(caplog): state_machine = StateMachine() @lock_state_machine def custom_function(object, number): raise AttributeError("Test error") State.custom_method = custom_function state1 = ExecutionState("s1") state_machine.root_state = state1 try: state1.custom_method(5) except Exception as e: import traceback print("Could not stop state machine: {0} {1}".format( e, traceback.format_exc())) assert global_lock_counter == 0 state1.add_outcome("outcome1", 3) assert len(state1.outcomes) == 4 assert_logger_warnings_and_errors(caplog)
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("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 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_concurrency_barrier_state(): state1 = ExecutionState("FirstState", "id_of_state_1", path=testing_utils.TEST_SCRIPT_PATH, filename="concurrency_barrier1.py") state1.add_outcome("FirstOutcomeState1", 3) state1.add_outcome("SecondOutcomeState1", 4) input_state1 = state1.add_input_data_port("input_data_port1", "float") output_state1 = state1.add_output_data_port("output_data_port1", "float") state2 = ExecutionState("SecondState", "id_of_state_2", path=testing_utils.TEST_SCRIPT_PATH, filename="concurrency_barrier2.py") state2.add_outcome("FirstOutcomeState2", 3) state2.add_outcome("SecondOutcomeState2", 4) input_state2 = state2.add_input_data_port("input_data_port1", "float") output_state2 = state2.add_output_data_port("output_data_port1", "float") barrier_state = BarrierConcurrencyState("FirstConcurrencyState", "barrier_state_id") barrier_state.add_state(state1) barrier_state.add_state(state2) input1_state3 = barrier_state.add_input_data_port("input_data_port1", "float", 0.1) input2_state3 = barrier_state.add_input_data_port("input_data_port2", "float", 0.1) barrier_state.add_data_flow(barrier_state.state_id, input1_state3, state1.state_id, input_state1) barrier_state.add_data_flow(barrier_state.state_id, input2_state3, state2.state_id, input_state2) barrier_state.add_output_data_port("output_data_port1", "str", "default_output_value") barrier_state.add_outcome("success_outcome", 3) barrier_state.add_outcome("error_outcome", 4) barrier_state.states[UNIQUE_DECIDER_STATE_ID].name = "decider_state" barrier_state.states[UNIQUE_DECIDER_STATE_ID]._script = Script( path=testing_utils.TEST_SCRIPT_PATH, filename="decider_state.py", check_path=True, parent=barrier_state.states[UNIQUE_DECIDER_STATE_ID]) barrier_state.states[UNIQUE_DECIDER_STATE_ID].add_outcome( "FirstOutcomeDecider", 3) barrier_state.states[UNIQUE_DECIDER_STATE_ID].add_outcome( "SecondOutcomeDecider", 4) barrier_state.add_transition( barrier_state.states[UNIQUE_DECIDER_STATE_ID].state_id, 3, barrier_state.state_id, 3) barrier_state.add_transition( barrier_state.states[UNIQUE_DECIDER_STATE_ID].state_id, 4, barrier_state.state_id, 4) return barrier_state
def create_preemptive_wait_state_machine(): state1 = ExecutionState("state_1", path=testing_utils.TEST_SCRIPT_PATH, filename="preemptive_wait_test.py") state1.add_outcome("FirstOutcome", 3) state2 = ExecutionState("state_2", path=testing_utils.TEST_SCRIPT_PATH, filename="preemptive_wait_test.py") state2.add_outcome("FirstOutcome", 3) ctr_state = PreemptiveConcurrencyState("FirstConcurrencyState") ctr_state.add_state(state1) ctr_state.add_state(state2) ctr_state.add_outcome("end", 3) ctr_state.add_transition(state1.state_id, 3, ctr_state.state_id, 3) ctr_state.add_transition(state2.state_id, 3, ctr_state.state_id, 3) return StateMachine(ctr_state)
def get_models(): from rafcon.core.states.execution_state import ExecutionState from rafcon.core.states.hierarchy_state import HierarchyState from rafcon.gui.models.container_state import ContainerStateModel def get_outcome_with_name(state_m, outcome_name): return [ outcome_m for outcome_m in state_m.outcomes if outcome_m.outcome.name == outcome_name ][0] def get_state_with_name(parent_state_m, state_name): return [ state_m for state_m in parent_state_m.states.values() if state_m.state.name == state_name ][0] execution_state = ExecutionState("ex1") execution_state.add_outcome("oe1") child_state = ExecutionState("ex2") hierarchy_state = HierarchyState("h1") hierarchy_state.add_outcome("oh1") hierarchy_state.add_state(child_state) root_state = HierarchyState("root") root_state.add_state(execution_state) root_state.add_state(hierarchy_state) root_state_m = ContainerStateModel(root_state) execution_state_m = get_state_with_name(root_state_m, "ex1") hierarchy_state_m = get_state_with_name(root_state_m, "h1") child_state_m = get_state_with_name(hierarchy_state_m, "ex2") outcome_e_success_m = get_outcome_with_name(execution_state_m, "success") outcome_e_1_m = get_outcome_with_name(execution_state_m, "oe1") outcome_h_success_m = get_outcome_with_name(hierarchy_state_m, "success") outcome_h_1_m = get_outcome_with_name(hierarchy_state_m, "oh1") return (root_state_m, execution_state_m, hierarchy_state_m, child_state_m), (outcome_e_success_m, outcome_e_1_m), \ (outcome_h_success_m, outcome_h_1_m)
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_create_state(caplog): state1 = ExecutionState("MyFirstState") assert len(state1.outcomes) == 3 out = state1.add_outcome("MyFirstOutcome", 3) assert len(state1.outcomes) == 4 state1.remove_outcome(out) with raises(AttributeError): # AttributeError should be raised if income is to be removed state1.remove_income() with raises(AttributeError): # AttributeError should be raised if not existing outcome ID is to be removed state1.remove_outcome(out) with raises(AttributeError): # AttributeError should be raised if outcome preempted is to be removed state1.remove_outcome(-1) with raises(AttributeError): # AttributeError should be raised if outcome aborted is to be removed state1.remove_outcome(-2) assert len(state1.outcomes) == 3 assert len(state1.input_data_ports) == 0 assert len(state1.output_data_ports) == 0 input_port_id = state1.add_input_data_port("input", "str") output_port_id = state1.add_output_data_port("output", "float") assert len(state1.input_data_ports) == 1 assert len(state1.output_data_ports) == 1 state1.remove_input_data_port(input_port_id) state1.remove_output_data_port(output_port_id) assert len(state1.input_data_ports) == 0 assert len(state1.output_data_ports) == 0 with raises(AttributeError): # AttributeError should be raised if not existing input is to be removed state1.remove_input_data_port(input_port_id) with raises(AttributeError): # AttributeError should be raised if not existing output is to be removed state1.remove_output_data_port(output_port_id) state2 = ExecutionState(name="State2", state_id=state1.state_id) # This should work, as data_type and default_value are optional parameters port = InputDataPort('input', data_port_id=99) with raises(AttributeError): # The name of the port differs in key and class member ExecutionState("test_execution_state", input_data_ports={'diff_input': port}) # UTF8 strings should be allowed at least for descriptions state1.description = u'My English is not v\xc3ry good' assert_logger_warnings_and_errors(caplog) a = ExecutionState("test", state_id=10) b = ExecutionState("test", state_id=10) assert a == b assert a is not b
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
def do_check_for_state(state_name): sm_history.modifications.reset() # Note: The elements always need to be retrieved before performing an operation, as undo/redo operations replace # both core and model objects state_m = get_state_model_by_name(state_name, state_path_dict) ############# # outcome add & remove outcome_super, state = perform_history_action( state_m.state.add_outcome, "super") state = get_state_by_name(state_name, state_path_dict) _, state = perform_history_action(state_m.state.remove_outcome, outcome_super) ############# # add two states state4 = ExecutionState('State4', state_id='STATE4') state_path_dict['state4'] = state.get_path() + "/" + "STATE4" _, state = perform_history_action(state.add_state, state4) state5 = ExecutionState('State5', state_id='STATE5') state_path_dict['state5'] = state.get_path() + "/" + "STATE5" _, state = perform_history_action(state.add_state, state5) perform_multiple_undo_redo(2) state4 = get_state_by_name('state4', state_path_dict) outcome_state4 = state4.add_outcome('UsedHere') assert len(sm_history.modifications) == 6 state5 = get_state_by_name('state5', state_path_dict) outcome_state5 = state5.add_outcome('UsedHere') assert len(sm_history.modifications) == 7 ################ # add transition from_state_id, from_outcome, to_state_id=None, to_outcome=None, transition_id new_transition_id1, state = perform_history_action( state.add_transition, from_state_id=state4.state_id, from_outcome=outcome_state4, to_state_id=state5.state_id, to_outcome=None) _, state = perform_history_action(state.add_transition, from_state_id=state5.state_id, from_outcome=outcome_state5, to_state_id=state.state_id, to_outcome=-1) ################### # remove transition _, state = perform_history_action(state.remove_transition, new_transition_id1) ############# # remove state _, state = perform_history_action(state.remove_state, state5.state_id) ############# # add input_data_port state4 = get_state_by_name('state4', state_path_dict) input_state4_id, state4 = perform_history_action( state4.add_input_data_port, "input", "str", "zero") ############# # remove input_data_port _, state4 = perform_history_action(state4.remove_input_data_port, input_state4_id) ############# # add output_data_port output_state4_id, state4 = perform_history_action( state4.add_output_data_port, "output_" + state4.state_id, "int") ############# # remove output_data_port _, state4 = perform_history_action(state4.remove_output_data_port, output_state4_id) # prepare again state4 state4.add_output_data_port("output", "int") state4.add_input_data_port("input_new", "str", "zero") assert len(sm_history.modifications) == 17 output_state4_id = state4.add_output_data_port("output_new", "int") assert len(sm_history.modifications) == 18 state5 = ExecutionState('State5', 'STATE5') state = get_state_by_name(state_name, state_path_dict) state.add_state(state5) assert state_path_dict['state5'] == state5.get_path() assert len(sm_history.modifications) == 19 input_par_state5 = state5.add_input_data_port("par", "int", 0) assert len(sm_history.modifications) == 20 output_res_state5 = state5.add_output_data_port("res", "int") assert len(sm_history.modifications) == 21 ##################### # add scoped_variable scoped_buffer_nested, state = perform_history_action( state.add_scoped_variable, "buffer", "int") ##################### # remove scoped_variable _, state = perform_history_action(state.remove_scoped_variable, scoped_buffer_nested) ############# # add data_flow new_df_id, state = perform_history_action( state.add_data_flow, from_state_id=state4.state_id, from_data_port_id=output_state4_id, to_state_id=state5.state_id, to_data_port_id=input_par_state5) ################ # remove data_flow perform_history_action(state.remove_data_flow, new_df_id)
def test_state_property_modifications_history(caplog): ################## # state properties # TODO LibraryState test for properties like mentioned in the notification-test but also general for add and remove # change name # change parent # change states # change outcomes # change transitions # change input_data_ports # change output_data_ports # change scoped_variables # change data_flows # change script # change script_text # change description # change active # set_start_state # change start_state_id # change child_execution testing_utils.dummy_gui(None) testing_utils.initialize_environment(gui_config={ 'AUTO_BACKUP_ENABLED': False, 'HISTORY_ENABLED': True }, gui_already_started=False) sm_model, state_dict = create_state_machine_m() state1 = ExecutionState('State1', state_id="STATE1") state1.add_input_data_port("input", "str", "zero") state1.add_output_data_port("output", "int") state1.add_output_data_port("count", "int") state2 = ExecutionState('State2') state2.add_input_data_port("par", "int", 0) state2.add_input_data_port("number", "int", 5) state2.add_output_data_port("res", "int") nested_state = state_dict['Nested'] nested_state.add_state(state1) nested_state.add_state(state2) state2_path = state2.get_path() nested_state.add_output_data_port("res", "int") state1.add_outcome("again") state1.add_outcome("counted") assert len(sm_model.history.modifications) == 6 state2.add_outcome("done") state2.add_outcome("best") state2.add_outcome("full") assert len(sm_model.history.modifications) == 9 nested_state.add_outcome("great") assert len(sm_model.history.modifications) == 10 ####################################### ######## Properties of State ########## # name(self, name) _, nested_state = perform_history_action(nested_state.__setattr__, "name", "nested") # TODO: The following commented operations are not correctly supported by the history! # input_data_ports(self, input_data_ports) None or dict # _, nested_state = perform_history_action(nested_state.__setattr__, "input_data_ports", {}) # _, nested_state = perform_history_action(nested_state.__setattr__, "output_data_ports", {}) # outcomes(self, outcomes) None or dict # _, nested_state = perform_history_action(nested_state.__setattr__, "outcomes", nested_state.outcomes) # _, nested_state = perform_history_action(nested_state.__setattr__, "outcomes", {}) script_text = '\ndef execute(self, inputs, outputs, gvm):\n\tself.logger.debug("Hello World")\n\treturn 0\n' script_text1 = '\ndef execute(self, inputs, outputs, gvm):\n\tself.logger.debug("Hello NERD")\n\treturn 0\n' # script(self, script) Script -> no script setter any more only script_text !!! nested2_state = state_dict['Nested2'] _, nested2_state = perform_history_action(nested2_state.__setattr__, "script_text", script_text) # script_text(self, script_text) _, nested2_state = perform_history_action(nested2_state.__setattr__, "script_text", script_text1) # description(self, description) str _, nested_state = perform_history_action(nested_state.__setattr__, "description", "awesome") ############################################ ###### Properties of ContainerState ######## # set_start_state(self, state) State or state_id _, nested_state = perform_history_action(nested_state.set_start_state, "STATE1") # set_start_state(self, start_state) state2 = sm_model.state_machine.get_state_by_path(state2_path) _, nested_state = perform_history_action(nested_state.set_start_state, state2, additional_operations=1) # transitions(self, transitions) None or dict _, nested_state = perform_history_action(nested_state.__setattr__, "transitions", {}) # data_flows(self, data_flows) None or dict _, nested_state = perform_history_action(nested_state.__setattr__, "data_flows", {}) # scoped_variables(self, scoped_variables) None or dict _, nested_state = perform_history_action(nested_state.__setattr__, "scoped_variables", {}) # states(self, states) None or dict _, nested_state = perform_history_action(nested_state.__setattr__, "states", {}) testing_utils.shutdown_environment(caplog=caplog, unpatch_threading=False)
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 test_transition_property_modifications_history(caplog): ################## # transition properties # change modify_origin # change from_outcome # change to_state # change to_outcome # modify_transition_from_state # modify_transition_from_outcome # modify_transition_to_outcome # modify_transition_to_state testing_utils.dummy_gui(None) testing_utils.initialize_environment(gui_config={ 'AUTO_BACKUP_ENABLED': False, 'HISTORY_ENABLED': True }, gui_already_started=False) sm_model, state_dict = create_state_machine_m() state1 = ExecutionState('State1') outcome_again_state1 = state1.add_outcome("again") state2 = ExecutionState('State2') oc_done_state2 = state2.add_outcome("done") state2.add_outcome("best") nested_state = state_dict['Nested'] nested_state.add_state(state1) nested_state.add_state(state2) nested_state.add_outcome("great") state1.add_outcome("counted") oc_full_state2 = state2.add_outcome("full") new_trans_id, nested_state = perform_history_action( nested_state.add_transition, from_state_id=state1.state_id, from_outcome=outcome_again_state1, to_state_id=state1.state_id, to_outcome=None) # modify_origin(self, from_state, from_outcome) _, nested_state = perform_history_action( nested_state.transitions[new_trans_id].modify_origin, from_state=state2.state_id, from_outcome=oc_full_state2) # from_outcome(self, from_outcome) _, nested_state = perform_history_action( nested_state.transitions[new_trans_id].__setattr__, "from_outcome", oc_done_state2) # to_state(self, to_state) _, nested_state = perform_history_action( nested_state.transitions[new_trans_id].__setattr__, "to_state", state2.state_id) # reset observer and testbed _, nested_state = perform_history_action(nested_state.remove_transition, new_trans_id) new_df_id, nested_state = perform_history_action( nested_state.add_transition, from_state_id=state1.state_id, from_outcome=outcome_again_state1, to_state_id=state1.state_id, to_outcome=None) _, nested_state = perform_history_action( nested_state.transitions[new_df_id].modify_origin, state2.state_id, oc_full_state2) # modify_transition_from_outcome(self, transition_id, from_outcome) _, nested_state = perform_history_action( nested_state.transitions[new_df_id].__setattr__, "from_outcome", oc_done_state2) # modify_transition_to_state(self, transition_id, to_state, to_outcome) _, nested_state = perform_history_action( nested_state.transitions[new_df_id].__setattr__, "to_state", state1.state_id) testing_utils.shutdown_environment(caplog=caplog, unpatch_threading=False)