コード例 #1
0
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)
コード例 #2
0
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
コード例 #3
0
ファイル: storage.py プロジェクト: uservinu/RAFCON
        path_core_data = os.path.join(state_path, FILE_NAME_CORE_DATA_OLD)

    try:
        state_info = load_data_file(path_core_data)
    except ValueError, e:
        logger.exception("Error while loading state data: {0}".format(e))
        return
    except LibraryNotFoundException, e:
        logger.error(
            "Library could not be loaded: {0}\n"
            "Skipping library and continuing loading the state machine".format(
                str(e.message)))
        state_info = storage_utils.load_objects_from_json(path_core_data,
                                                          as_dict=True)
        state_id = state_info["state_id"]
        dummy_state = HierarchyState(LIBRARY_NOT_FOUND_DUMMY_STATE_NAME,
                                     state_id=state_id)
        # set parent of dummy state
        if isinstance(parent, ContainerState):
            parent.add_state(dummy_state, storage_load=True)
        else:
            dummy_state.parent = parent
        return dummy_state

    # Transitions and data flows are not added when loading a state, as also states are not added.
    # We have to wait until the child states are loaded, before adding transitions and data flows, as otherwise the
    # validity checks for transitions and data flows would fail
    if not isinstance(state_info, tuple):
        state = state_info
    else:
        state = state_info[0]
        transitions = state_info[1]
コード例 #4
0
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)
コード例 #5
0
def load_state_recursively(parent, state_path=None, dirty_states=[]):
    """Recursively loads the state

    It calls this method on each sub-state of a container state.

    :param parent:  the root state of the last load call to which the loaded state will be added
    :param state_path: the path on the filesystem where to find the meta file for the state
    :param dirty_states: a dict of states which changed during loading
    :return:
    """
    from rafcon.core.states.execution_state import ExecutionState
    from rafcon.core.states.container_state import ContainerState
    from rafcon.core.states.hierarchy_state import HierarchyState

    path_core_data = os.path.join(state_path, FILE_NAME_CORE_DATA)

    logger.debug("Load state recursively: {0}".format(str(state_path)))

    # TODO: Should be removed with next minor release
    if not os.path.exists(path_core_data):
        path_core_data = os.path.join(state_path, FILE_NAME_CORE_DATA_OLD)

    try:
        state_info = load_data_file(path_core_data)
    except ValueError as e:
        logger.exception("Error while loading state data: {0}".format(e))
        return
    except LibraryNotFoundException as e:
        logger.error(
            "Library could not be loaded: {0}\n"
            "Skipping library and continuing loading the state machine".format(
                e))
        state_info = storage_utils.load_objects_from_json(path_core_data,
                                                          as_dict=True)
        state_id = state_info["state_id"]
        dummy_state = HierarchyState(LIBRARY_NOT_FOUND_DUMMY_STATE_NAME,
                                     state_id=state_id)
        # set parent of dummy state
        if isinstance(parent, ContainerState):
            parent.add_state(dummy_state, storage_load=True)
        else:
            dummy_state.parent = parent
        return dummy_state

    # Transitions and data flows are not added when loading a state, as also states are not added.
    # We have to wait until the child states are loaded, before adding transitions and data flows, as otherwise the
    # validity checks for transitions and data flows would fail
    if not isinstance(state_info, tuple):
        state = state_info
    else:
        state = state_info[0]
        transitions = state_info[1]
        data_flows = state_info[2]

    # set parent of state
    if parent is not None and isinstance(parent, ContainerState):
        parent.add_state(state, storage_load=True)
    else:
        state.parent = parent

    # read script file if an execution state
    if isinstance(state, ExecutionState):
        script_text = read_file(state_path, state.script.filename)
        state.script_text = script_text

    # load semantic data
    try:
        semantic_data = load_data_file(
            os.path.join(state_path, SEMANTIC_DATA_FILE))
        state.semantic_data = semantic_data
    except Exception as e:
        # semantic data file does not have to be there
        pass

    one_of_my_child_states_not_found = False

    # load child states
    for p in os.listdir(state_path):
        child_state_path = os.path.join(state_path, p)
        if os.path.isdir(child_state_path):
            child_state = load_state_recursively(state, child_state_path,
                                                 dirty_states)
            if not child_state:
                return None
            if child_state.name is LIBRARY_NOT_FOUND_DUMMY_STATE_NAME:
                one_of_my_child_states_not_found = True

    if one_of_my_child_states_not_found:
        # omit adding transitions and data flows in this case
        pass
    else:
        # Now we can add transitions and data flows, as all child states were added
        if isinstance(state_info, tuple):
            state.transitions = transitions
            state.data_flows = data_flows

    state.file_system_path = state_path

    if state.marked_dirty:
        dirty_states.append(state)

    return state
コード例 #6
0
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)
コード例 #7
0
def create_state_machine():
    from rafcon.core.state_machine import StateMachine
    from rafcon.core.states.hierarchy_state import HierarchyState
    from rafcon.core.states.execution_state import ExecutionState
    root = HierarchyState(name='root')
    ex1 = ExecutionState(name='1',
                         filename="script_small_wait.py",
                         path=TEST_SCRIPT_PATH)
    root.add_state(ex1)
    ex2 = ExecutionState(name='2',
                         filename="script_small_wait.py",
                         path=TEST_SCRIPT_PATH)
    root.add_state(ex2)
    ex3 = ExecutionState(name='3',
                         filename="script_small_wait.py",
                         path=TEST_SCRIPT_PATH)
    root.add_state(ex3)

    # hierarchy state at the beginning
    h4 = HierarchyState('H4')
    ex41 = ExecutionState(name='41',
                          filename="script_small_wait.py",
                          path=TEST_SCRIPT_PATH)
    h4.add_state(ex41)
    ex42 = ExecutionState(name='42',
                          filename="script_small_wait.py",
                          path=TEST_SCRIPT_PATH)
    h4.add_state(ex42)
    ex43 = ExecutionState(name='43',
                          filename="script_small_wait.py",
                          path=TEST_SCRIPT_PATH)
    h4.add_state(ex43)
    h4.start_state_id = ex41.state_id
    h4.add_transition(ex41.state_id, 0, ex42.state_id, None)
    h4.add_transition(ex42.state_id, 0, ex43.state_id, None)
    h4.add_transition(ex43.state_id, 0, h4.state_id, 0)

    root.add_state(h4)
    root.start_state_id = ex1.state_id
    root.add_transition(h4.state_id, 0, ex1.state_id, None)
    root.add_transition(ex1.state_id, 0, ex2.state_id, None)
    root.add_transition(ex2.state_id, 0, ex3.state_id, None)
    t_id = root.add_transition(ex3.state_id, 0, root.state_id, 0)
    return StateMachine(root_state=root), t_id, h4.state_id
コード例 #8
0
def create_models(*args, **kargs):
    import rafcon.core.singleton
    from rafcon.core.states.hierarchy_state import HierarchyState
    from rafcon.core.states.execution_state import ExecutionState
    from rafcon.core.state_machine import StateMachine

    state1 = HierarchyState('State1', state_id="State1")
    state2 = ExecutionState('State2', state_id="State2")

    ctr_state = HierarchyState(name="Root", state_id="Root")
    ctr_state.add_state(state1)
    ctr_state.add_state(state2)
    ctr_state.start_state_id = state1.state_id
    ctr_state.add_transition(state1.state_id,
                             from_outcome=0,
                             to_state_id=state2.state_id,
                             to_outcome=None)
    ctr_state.add_transition(state2.state_id,
                             from_outcome=0,
                             to_state_id=ctr_state.state_id,
                             to_outcome=0)
    ctr_state.name = "Container"

    sm = StateMachine(ctr_state)

    # add new state machine
    rafcon.core.singleton.state_machine_manager.add_state_machine(sm)
コード例 #9
0
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
コード例 #10
0
ファイル: start.py プロジェクト: loongsunchan/RAFCON
def create_new_state_machine():
    root_state = HierarchyState()
    state_machine = StateMachine(root_state)
    core_singletons.state_machine_manager.add_state_machine(state_machine)
コード例 #11
0
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)
コード例 #12
0
ファイル: test_history.py プロジェクト: loongsunchan/RAFCON
 def create_simple_state_machine():
     root_state = HierarchyState("new root state", state_id="ROOT")
     state_machine = StateMachine(root_state)
     sm_manager_model.state_machine_manager.add_state_machine(state_machine)
     return state_machine
コード例 #13
0
ファイル: test_history.py プロジェクト: loongsunchan/RAFCON
def test_add_remove_history(caplog):
    testing_utils.dummy_gui(None)
    ##################
    # Root_state elements

    # add state
    # - change state
    # remove state
    # add outcome
    # - change outcome
    # remove outcome
    # add transition
    # - change transition
    # remove transition
    # add input_data_port
    # - change input_data_port
    # remove input_data_port
    # add output_data_port
    # - change output_data_port
    # remove output_data_port
    # add scoped_variable
    # - change scoped_variable
    # remove scoped_variable
    # add data_flow
    # - change data_flow
    # remove data_flow

    testing_utils.dummy_gui(None)

    testing_utils.initialize_environment(gui_config={'AUTO_BACKUP_ENABLED': False,
                                                     'HISTORY_ENABLED': True}, gui_already_started=False)
    state_machine_m, state_dict = create_state_machine_m()

    state_machine_path = TEST_PATH + '_test_add_remove'
    save_state_machine(state_machine_m, state_machine_path + '_before', logger, with_gui=False, menubar_ctrl=None)

    sm_history = state_machine_m.history

    state1 = HierarchyState('state1', state_id='STATE1')
    state2 = ExecutionState('state2', state_id='STATE2')

    state_dict['Nested'].add_state(state1)
    state_dict['Nested'].add_state(state2)
    state_dict['state1'] = state1
    state_dict['state2'] = state2

    state_path_dict = {}
    for key in state_dict:
        state_path_dict[key] = state_dict[key].get_path()

    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)

    do_check_for_state(state_name='Nested')
    do_check_for_state(state_name='Container')

    testing_utils.shutdown_environment(caplog=caplog, unpatch_threading=False)
コード例 #14
0
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)
コード例 #15
0
ファイル: storage.py プロジェクト: DLR-RM/RAFCON
def load_state_recursively(parent, state_path=None, dirty_states=[]):
    """Recursively loads the state

    It calls this method on each sub-state of a container state.

    :param parent:  the root state of the last load call to which the loaded state will be added
    :param state_path: the path on the filesystem where to find the meta file for the state
    :param dirty_states: a dict of states which changed during loading
    :return:
    """
    from rafcon.core.states.execution_state import ExecutionState
    from rafcon.core.states.container_state import ContainerState
    from rafcon.core.states.hierarchy_state import HierarchyState
    from rafcon.core.singleton import library_manager

    path_core_data = get_core_data_path(state_path)
    path_meta_data = get_meta_data_path(state_path)

    logger.debug("Load state recursively: {0}".format(str(state_path)))

    try:
        state_info = load_data_file(path_core_data)
    except ValueError as e:
        logger.exception("Error while loading state data: {0}".format(e))
        return
    except LibraryNotFoundException as e:
        if global_config.get_config_value(
                "RAISE_ERROR_ON_MISSING_LIBRARY_STATES",
                False) or not library_manager.show_dialog:
            raise
        logger.error(
            "Library could not be loaded: {0}\n"
            "Skipping library and continuing loading the state machine".format(
                e))
        state_info = storage_utils.load_objects_from_json(path_core_data,
                                                          as_dict=True)
        missing_library_meta_data = None
        if os.path.exists(path_meta_data):
            missing_library_meta_data = Vividict(
                storage_utils.load_objects_from_json(path_meta_data))
        state_id = state_info["state_id"]
        outcomes = {
            outcome['outcome_id']: Outcome(outcome['outcome_id'],
                                           outcome['name'])
            for outcome in state_info["outcomes"].values()
        }
        dummy_state = HierarchyState(
            LIBRARY_NOT_FOUND_DUMMY_STATE_NAME,
            state_id=state_id,
            outcomes=outcomes,
            is_dummy=True,
            missing_library_meta_data=missing_library_meta_data)
        library_name = state_info['library_name']
        path_parts = os.path.join(state_info['library_path'],
                                  library_name).split(os.sep)
        dummy_state.description = 'The Missing Library Path: %s\nThe Missing Library Name: %s\n\n' % (
            state_info['library_path'], library_name)
        from rafcon.core.singleton import library_manager
        if path_parts[0] in library_manager.library_root_paths:
            dummy_state.description += 'The Missing Library OS Path: %s' % os.path.join(
                library_manager.library_root_paths[path_parts[0]], *
                path_parts[1:])
        else:
            dummy_state.description += 'The missing library was located in the missing library root "%s"' % path_parts[
                0]
        # set parent of dummy state
        if isinstance(parent, ContainerState):
            parent.add_state(dummy_state, storage_load=True)
        else:
            dummy_state.parent = parent
        return dummy_state
    except LibraryNotFoundSkipException:
        return None

    # Transitions and data flows are not added when loading a state, as also states are not added.
    # We have to wait until the child states are loaded, before adding transitions and data flows, as otherwise the
    # validity checks for transitions and data flows would fail
    if not isinstance(state_info, tuple):
        state = state_info
    else:
        state = state_info[0]
        transitions = state_info[1]
        data_flows = state_info[2]

    # set parent of state
    if parent is not None and isinstance(parent, ContainerState):
        parent.add_state(state, storage_load=True)
    else:
        state.parent = parent

    # read script file if state is an ExecutionState
    if isinstance(state, ExecutionState):
        script_text = read_file(state_path, state.script.filename)
        state.script.set_script_without_compilation(script_text)

    # load semantic data
    try:
        semantic_data = load_data_file(
            os.path.join(state_path, SEMANTIC_DATA_FILE))
        state.semantic_data = semantic_data
    except Exception as e:
        # semantic data file does not have to be there
        pass

    # load child states
    for p in os.listdir(state_path):
        child_state_path = os.path.join(state_path, p)
        if os.path.isdir(child_state_path):
            if not os.path.exists(
                    os.path.join(child_state_path, FILE_NAME_CORE_DATA)):
                # this means that child_state_path is a folder, not containing a valid state
                # this also happens when pip creates __pycache__ folders for the script.py files upon installing rafcon
                continue
            child_state = load_state_recursively(state, child_state_path,
                                                 dirty_states)
            if not child_state:
                return None

    # Now we can add transitions and data flows, as all child states were added
    if isinstance(state_info, tuple):
        safe_init = global_config.get_config_value("LOAD_SM_WITH_CHECKS", True)
        if safe_init:
            # this will trigger all validity checks the state machine
            state.transitions = transitions
        else:
            state._transitions = transitions
            state._data_flows = data_flows
        for _, transition in state.transitions.items():
            transition._parent = ref(state)
        state._data_flows = data_flows
        for _, data_flow in state.data_flows.items():
            data_flow._parent = ref(state)

    state.file_system_path = state_path

    if state.marked_dirty:
        dirty_states.append(state)

    return state
コード例 #16
0
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"

    return StateMachine(ctr_state)
コード例 #17
0
def test_unique_port_names(caplog):

    state = ExecutionState('execution state')

    state.add_input_data_port("in", "int", 0)
    state.add_output_data_port("out", "int", 0)

    # Data port name must be unique within data port set (input or output)
    with raises(ValueError):
        state.add_input_data_port("in", "int", 0)
    with raises(ValueError):
        state.add_input_data_port("in", "double", 0)
    with raises(ValueError):
        state.add_output_data_port("out", "int", 0)
    with raises(ValueError):
        state.add_output_data_port("out", "double", 0)

    # Names are allowed for other data port set
    state.add_output_data_port("in", "int", 0)
    state.add_input_data_port("out", "int", 0)

    assert len(state.input_data_ports) == 2
    assert len(state.output_data_ports) == 2

    state = HierarchyState('hierarchy state')

    in_id = state.add_input_data_port("in", "int", 0)
    out_id = state.add_output_data_port("out", "int", 0)
    scope_id = state.add_scoped_variable("scope", "int", 0)

    # Data port name must be unique within data port set (input, output or scope)
    with raises(ValueError):
        state.add_input_data_port("in", "int", 0)
    with raises(ValueError):
        state.add_input_data_port("in", "double", 0)
    with raises(ValueError):
        state.add_output_data_port("out", "int", 0)
    with raises(ValueError):
        state.add_output_data_port("out", "double", 0)
    with raises(ValueError):
        state.add_scoped_variable("scope", "int", 0)
    with raises(ValueError):
        state.add_scoped_variable("scope", "double", 0)

    # check programmatic direct data_type and default_value usage DataPort-Class
    state.add_input_data_port("_in", int, 0)
    list_op_id = state.add_output_data_port("_out", list, [1, 2, 3])
    with raises(TypeError):
        state.add_scoped_variable("_scope", float, 0)
    with raises(TypeError):
        state.output_data_ports[list_op_id].default_value = (1, 3)

    # Names are allowed for other data port set
    state.add_output_data_port("in", "int", 0)
    state.add_scoped_variable("in", "int", 0)
    state.add_input_data_port("out", "int", 0)
    state.add_scoped_variable("out", "int", 0)
    state.add_input_data_port("scope", "int", 0)
    state.add_output_data_port("scope", "int", 0)

    # Also renaming should raise exceptions
    with raises(ValueError):
        state.input_data_ports[in_id].name = "out"
    with raises(ValueError):
        state.output_data_ports[out_id].name = "in"
    with raises(ValueError):
        state.scoped_variables[scope_id].name = "out"

    assert len(state.input_data_ports) == 4
    assert len(state.output_data_ports) == 4
    assert len(state.scoped_variables) == 3

    testing_utils.assert_logger_warnings_and_errors(caplog)
コード例 #18
0
def add_state_machine(widget, event=None):
    """Create a new state-machine when the user clicks on the '+' next to the tabs"""
    logger.debug("Creating new state-machine...")
    root_state = HierarchyState("new root state")
    state_machine = StateMachine(root_state)
    rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine)
コード例 #19
0
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)
コード例 #20
0
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)
コード例 #21
0
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)
コード例 #22
0
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
コード例 #23
0
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')
    # print state3.input_data_ports
    # print state3.output_data_ports
    # exit(0)

    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")
    #print input_ctr_state
    output_ctr_state = ctr_state.add_output_data_port("ctr_out", "int")
    #print output_ctr_state
    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")
    # print ctr_state.input_data_ports
    # print ctr_state.output_data_ports
    # exit(0)

    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)

    tmp_dict = {
        'Container': ctr_state,
        'State1': state1,
        'State2': state2,
        'State3': state3,
        'Nested': state4,
        'Nested2': state5
    }
    sm = StateMachine(ctr_state)

    return tmp_dict, sm
コード例 #24
0
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
コード例 #25
0
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