def test_relocate_library(caplog):
    testing_utils.initialize_environment(gui_already_started=False,
                                         libraries={
                                             "tutorials":
                                             testing_utils.TUTORIAL_PATH,
                                         })

    try:
        from rafcon.gui.helpers.state_machine import relocate_library

        assert os.path.exists(LIBRARY_OS_PATH)
        assert not os.path.exists(NEW_LIBRARY_OS_PATH)

        state_machine = storage.load_state_machine_from_path(
            STATE_MACHINE_OS_PATH)
        library = list(state_machine.root_state.states.values())[0]

        assert library.library_path == LIBRARY_PATH
        assert library.library_name == LIBRARY_NAME

        relocate_library(LIBRARY_OS_PATH, LIBRARY_PATH, LIBRARY_NAME,
                         NEW_DIRECTORY)

        assert not os.path.exists(LIBRARY_OS_PATH)
        assert os.path.exists(NEW_LIBRARY_OS_PATH)

        state_machine = storage.load_state_machine_from_path(
            STATE_MACHINE_OS_PATH)
        library = list(state_machine.root_state.states.values())[0]

        assert library.library_path == NEW_LIBRARY_PATH
        assert library.library_name == LIBRARY_NAME

        relocate_library(NEW_LIBRARY_OS_PATH, NEW_LIBRARY_PATH, LIBRARY_NAME,
                         testing_utils.TUTORIAL_PATH)

        assert os.path.exists(LIBRARY_OS_PATH)
        assert not os.path.exists(NEW_LIBRARY_OS_PATH)

        state_machine = storage.load_state_machine_from_path(
            STATE_MACHINE_OS_PATH)
        library = list(state_machine.root_state.states.values())[0]

        assert library.library_path == LIBRARY_PATH
        assert library.library_name == LIBRARY_NAME

    finally:
        testing_utils.shutdown_environment(caplog=caplog,
                                           unpatch_threading=False)
Beispiel #2
0
def test_hierarchy_save_load_test(caplog):
    storage_path = testing_utils.get_unique_temp_path()

    hierarchy_state = create_hierarchy_state()
    sm = StateMachine(hierarchy_state)

    storage.save_state_machine_to_path(sm, storage_path)
    sm_loaded = storage.load_state_machine_from_path(storage_path)

    state_machine = StateMachine(sm_loaded.root_state)

    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.start()
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_manager.remove_state_machine(
        state_machine.state_machine_id)
    try:
        assert state_machine.root_state.output_data["output1"] == 52.0
        # 2 type error -> one child output port data type error and root state scoped data type error
        testing_utils.assert_logger_warnings_and_errors(caplog,
                                                        expected_errors=2)
    finally:
        testing_utils.test_multithreading_lock.release()
Beispiel #3
0
    def get_library_state_copy_instance(self, lib_os_path):
        """ A method to get a state copy of the library specified via the lib_os_path.

        :param lib_os_path: the location of the library to get a copy for
        :return:
        """

        # originally libraries were called like this; DO NOT DELETE; interesting for performance tests
        # state_machine = storage.load_state_machine_from_path(lib_os_path)
        # return state_machine.version, state_machine.root_state

        # TODO observe changes on file system and update data
        if lib_os_path in self._loaded_libraries:
            # this list can also be taken to open library state machines TODO -> implement it -> because faster
            state_machine = self._loaded_libraries[lib_os_path]
            # logger.info("Take copy of {0}".format(lib_os_path))
            # as long as the a library state root state is never edited so the state first has to be copied here
            state_copy = copy.deepcopy(state_machine.root_state)
            return state_machine.version, state_copy
        else:
            state_machine = storage.load_state_machine_from_path(lib_os_path)
            self._loaded_libraries[lib_os_path] = state_machine
            if config.global_config.get_config_value(
                    "NO_PROGRAMMATIC_CHANGE_OF_LIBRARY_STATES_PERFORMED",
                    False):
                return state_machine.version, state_machine.root_state
            else:
                state_copy = copy.deepcopy(state_machine.root_state)
                return state_machine.version, state_copy
Beispiel #4
0
def test_run_to_selected_state(caplog):
    testing_utils.initialize_environment_core()

    sm_path = testing_utils.get_test_sm_path(
        os.path.join("unit_test_state_machines", "run_to_selected_state_test"))
    sm = storage.load_state_machine_from_path(sm_path)
    # select state machine for this purpose
    rafcon.core.singleton.state_machine_manager.add_state_machine(sm)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = sm.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.run_to_selected_state(
        "VVBPOY/AOZXRY", sm.state_machine_id)
    # run the statemachine to the state before AOYXRY, this is an asynchronous task
    timeout = time.time()
    while not sm.get_state_by_path(
            "VVBPOY/ABNQFK"
    ).state_execution_status is StateExecutionStatus.WAIT_FOR_NEXT_STATE:
        time.sleep(.05)
        if time.time() - timeout > 2:
            raise RuntimeError(
                "execution_state ABNQFK not reached --> timeout")
    # wait until the statemachine is executed until ABNQFK the state before AOZXRY, so it doesnt check for the file
    # before its even written

    with open(os.path.join(RAFCON_TEMP_PATH_BASE, 'test_file'),
              'r') as test_file:
        lines = test_file.readlines()

    # the state machines waits at ABNQFK with state WAIT_FOR_NEXT_STATE, so it needs to be stopped manually
    rafcon.core.singleton.state_machine_execution_engine.stop()
    rafcon.core.singleton.state_machine_execution_engine.join()

    try:
        assert len(lines) < 3
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
Beispiel #5
0
def test_preemption_behaviour_during_stop(caplog):
    testing_utils.initialize_environment_core()

    path = testing_utils.get_test_sm_path(
        os.path.join("unit_test_state_machines",
                     "preemption_behaviour_during_stop"))
    state_machine = storage.load_state_machine_from_path(path)
    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)

    thread = threading.Thread(
        target=trigger_stop,
        args=[
            state_machine, rafcon.core.singleton.state_machine_execution_engine
        ])
    thread.start()

    rafcon.core.singleton.state_machine_execution_engine.start(
        state_machine.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()

    rafcon.core.singleton.state_machine_manager.remove_state_machine(
        state_machine.state_machine_id)
    try:
        assert global_variable_manager.get_variable("s1") == 1
        assert global_variable_manager.get_variable("s2") == 1
        assert not global_variable_manager.variable_exist("s3")
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
def test_library_relocation(caplog):

    testing_utils.initialize_environment_core(
        libraries={"test_scripts": testing_utils.TEST_ASSETS_PATH})

    interface.open_folder_func = open_folder

    interface.show_notice_func = show_notice

    state_machine = storage.load_state_machine_from_path(
        testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines",
                         "library_relocation_test")))

    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)

    rafcon.core.singleton.state_machine_execution_engine.start(
        state_machine.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_execution_engine.stop()

    try:
        assert state_machine.root_state.output_data["output_0"] == 27
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                     expected_warnings=0,
                                                     expected_errors=1)

    logger.info("State machine execution finished!")
Beispiel #7
0
def test_on_clean_storing_with_name_in_path(caplog):
    testing_utils.dummy_gui(None)

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

    path_old_format = testing_utils.get_test_sm_path(
        os.path.join("unit_test_state_machines",
                     "id_to_name_plus_id_storage_format_test_do_not_update"))
    path_new_format = os.path.join(
        testing_utils.get_unique_temp_path(),
        "id_to_name_plus_id_storage_format_test_do_not_update")

    # gui imports better after initialization
    from rafcon.gui.models.state_machine import StateMachineModel

    shutil.copytree(path_old_format, path_new_format)
    from rafcon.core.storage import storage
    sm = storage.load_state_machine_from_path(path_new_format)
    check_state_recursively_if_state_scripts_are_valid(sm.root_state)
    sm.base_path = path_new_format
    sm_m = StateMachineModel(sm)
    try:
        on_save_activate(sm_m, logger)
        check_that_all_files_are_there(sm, with_print=False)
        check_id_and_name_plus_id_format(path_old_format, path_new_format,
                                         sm_m)
    finally:
        testing_utils.shutdown_environment(caplog=caplog,
                                           unpatch_threading=False)
Beispiel #8
0
    def execute_state_machine_from_path(self,
                                        state_machine=None,
                                        path=None,
                                        start_state_path=None,
                                        wait_for_execution_finished=True):
        """ A helper function to start an arbitrary state machine at a given path.

        :param path: The path where the state machine resides
        :param start_state_path: The path to the state from which the execution will start
        :return: a reference to the created state machine
        """
        import rafcon.core.singleton
        if not state_machine:
            if not path:
                raise ValueError(
                    "You must provide either a state machine or a path to a state machine for execution"
                )
            from rafcon.core.storage import storage
            state_machine = storage.load_state_machine_from_path(path)
            rafcon.core.singleton.state_machine_manager.add_state_machine(
                state_machine)

        rafcon.core.singleton.state_machine_execution_engine.start(
            state_machine.state_machine_id, start_state_path=start_state_path)

        if wait_for_execution_finished:
            self.join()
            self.stop()
        return state_machine
Beispiel #9
0
def test_scoped_data(caplog):
    storage_path = testing_utils.get_unique_temp_path()

    sm = create_state_machine()

    storage.save_state_machine_to_path(sm, storage_path)
    sm_loaded = storage.load_state_machine_from_path(storage_path)

    state_machine = StateMachine(sm_loaded.root_state)

    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.start()
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_manager.remove_state_machine(
        state_machine.state_machine_id)

    try:
        assert state_machine.root_state.output_data[
            "data_output_port1"] == 42.0
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
def test_only_run_this_state(caplog):
    # run_selected
    # Initialize testing environment
    testing_utils.initialize_environment_core()
    # Load State Machine
    sm = storage.load_state_machine_from_path(testing_utils.get_test_sm_path(os.path.join(
        "unit_test_state_machines", "test_run_only_this_state")))
    rafcon.core.singleton.state_machine_manager.add_state_machine(sm)
    # Run only selected state machine
    rafcon.core.singleton.global_variable_manager.set_variable("test_value", 0)
    state_machine_execution_engine.run_only_selected_state("BSSKWR/YEKRMH/TZILCN", sm.state_machine_id)
    sm.join()

    assert rafcon.core.singleton.global_variable_manager.get_variable("test_value") == 1

    # Test running a state inside a concurrency state
    rafcon.core.singleton.global_variable_manager.set_variable("test_value_concurrency", 2)
    state_machine_execution_engine.run_only_selected_state("BSSKWR/IQURCQ/LLRMSU/VYGYRO", sm.state_machine_id)
    sm.join()

    # assert variable state
    try:
        assert rafcon.core.singleton.global_variable_manager.get_variable("test_value_concurrency") == 1
    # Shutdown testing environment
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
Beispiel #11
0
def test_default_values_of_data_ports(caplog):

    storage_path = testing_utils.get_unique_temp_path()
    print(storage_path)

    sm = create_state_machine()

    storage.save_state_machine_to_path(sm, storage_path)
    sm_loaded = storage.load_state_machine_from_path(storage_path)

    root_state = sm_loaded.root_state

    state_machine = StateMachine(root_state)
    testing_utils.test_multithreading_lock.acquire()

    rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine)
    rafcon.core.singleton.state_machine_execution_engine.start(state_machine.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_manager.remove_state_machine(state_machine.state_machine_id)

    print(root_state.output_data)
    try:
        assert root_state.output_data["output_data_port1"] == "default_value"
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
def test_concurrency_barrier_save_load(caplog):
    concurrency_barrier_state = create_concurrency_barrier_state()

    state_machine = StateMachine(concurrency_barrier_state)
    test_path = testing_utils.get_unique_temp_path()
    storage.save_state_machine_to_path(state_machine, test_path)
    sm_loaded = storage.load_state_machine_from_path(test_path)

    root_state = sm_loaded.root_state
    input_data = {"input_data_port1": 0.1, "input_data_port2": 0.1}
    output_data = {"output_data_port1": None}
    root_state.input_data = input_data
    root_state.output_data = output_data

    state_machine = StateMachine(root_state)
    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine)
    rafcon.core.singleton.state_machine_execution_engine.start(state_machine.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()

    try:
        assert rafcon.core.singleton.global_variable_manager.get_variable("var_x") == 10
        assert rafcon.core.singleton.global_variable_manager.get_variable("var_y") == 20
        assert root_state.final_outcome.outcome_id == 4

        with pytest.raises(ValueError):
            concurrency_barrier_state.remove(UNIQUE_DECIDER_STATE_ID)

        with pytest.raises(AttributeError):
            concurrency_barrier_state.remove_state(UNIQUE_DECIDER_STATE_ID)

        rafcon.core.singleton.state_machine_manager.remove_state_machine(state_machine.state_machine_id)
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog, expected_warnings=0, expected_errors=1)
 def setup_class(cls):
     # This methods runs on class creation and creates the state machine
     testing_utils.test_multithreading_lock.acquire()
     state_machine = global_storage.load_state_machine_from_path(
         testing_utils.get_test_sm_path(os.path.join("unit_test_state_machines", "action_block_execution_test")))
     cls.state_machine = state_machine
     state_machine_manager.add_state_machine(state_machine)
Beispiel #14
0
    def execute_state_machine_from_path(self,
                                        state_machine=None,
                                        path=None,
                                        start_state_path=None,
                                        wait_for_execution_finished=True):
        """ A helper function to start an arbitrary state machine at a given path.

        :param path: The path where the state machine resides
        :param start_state_path: The path to the state from which the execution will start
        :return: a reference to the created state machine
        """

        import rafcon.core.singleton
        from rafcon.core.storage import storage
        rafcon.core.singleton.library_manager.initialize()
        if not state_machine:
            state_machine = storage.load_state_machine_from_path(path)
            rafcon.core.singleton.state_machine_manager.add_state_machine(
                state_machine)

        rafcon.core.singleton.state_machine_execution_engine.start(
            state_machine.state_machine_id, start_state_path=start_state_path)

        if wait_for_execution_finished:
            self.join()
            self.stop()
        return rafcon.core.singleton.state_machine_manager.get_active_state_machine(
        )
def test_custom_entry_point(caplog):

    testing_utils.initialize_environment_core()

    state_machine = storage.load_state_machine_from_path(
        testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines", "stepping_test")))

    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id

    rafcon.core.singleton.state_machine_execution_engine.step_mode()
    time.sleep(0.2)  # let the state machine start properly

    # sm structure

    # GLSUJY
    # GLSUJY/PXTKIH
    # GLSUJY/NDIVLD
    # GLSUJY/SFZGMH

    # GLSUJY/SMCOIB
    # GLSUJY/SMCOIB/YSBJGK
    # GLSUJY/SMCOIB/OUWQUJ
    # GLSUJY/SMCOIB/UGGFFI

    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_and_join(state_machine, "GLSUJY/PXTKIH")
    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_and_join(state_machine, "GLSUJY/NDIVLD")
    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_and_join(state_machine, "GLSUJY/SFZGMH")
    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_and_join(state_machine, "GLSUJY/SMCOIB")

    rafcon.core.singleton.state_machine_execution_engine.step_into()
    wait_and_join(state_machine, "GLSUJY/PXTKIH")
    rafcon.core.singleton.state_machine_execution_engine.step_into()
    wait_and_join(state_machine, "GLSUJY/NDIVLD")
    rafcon.core.singleton.state_machine_execution_engine.step_into()
    wait_and_join(state_machine, "GLSUJY/SFZGMH")
    rafcon.core.singleton.state_machine_execution_engine.step_into(
    )  # step into hierarchy state GLSUJY/SMCOIB
    rafcon.core.singleton.state_machine_execution_engine.step_into()
    wait_and_join(state_machine, "GLSUJY/SMCOIB/YSBJGK")

    rafcon.core.singleton.state_machine_execution_engine.step_out()
    wait_and_join(state_machine, "GLSUJY/SMCOIB")

    rafcon.core.singleton.state_machine_execution_engine.stop()
    rafcon.core.singleton.state_machine_execution_engine.join()

    try:
        assert rafcon.core.singleton.global_variable_manager.get_variable(
            "bottles") == 95
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
Beispiel #16
0
def open_state_machine(state_machine_path):
    """Executes the specified state machine

    :param str state_machine_path: The file path to the state machine
    :return StateMachine: The loaded state machine
    """
    sm = storage.load_state_machine_from_path(state_machine_path)
    core_singletons.state_machine_manager.add_state_machine(sm)

    return sm
Beispiel #17
0
def test_unchanged_storage_format(caplog):
    """This test ensures that the state machine storage format does not change in patch releases"""

    from rafcon.core.storage import storage
    from rafcon.gui.models.state_machine import StateMachineModel
    import rafcon

    path = get_backward_compatibility_state_machines_path()
    if not os.path.exists(path):
        logger.info(
            "test_unchanged_storage_format: the current python interpreter version is not supported"
        )
        return

    testing_utils.initialize_environment(
        gui_config={
            'HISTORY_ENABLED': False,
            'AUTO_BACKUP_ENABLED': False
        },
        libraries={
            'unit_test_state_machines':
            testing_utils.get_test_sm_path("unit_test_state_machines")
        },
        gui_already_started=False)
    try:
        current_rafcon_version = StrictVersion(rafcon.__version__).version
        current_minor = "{}.{}".format(current_rafcon_version[0],
                                       current_rafcon_version[1])
        for filename in os.listdir(path):
            if filename.startswith(current_minor):
                old_state_machine_path = os.path.join(path, filename)
                break
        else:
            assert False, "There is no state machine for the current RAFCON minor version {}".format(
                current_minor)

        state_machine = storage.load_state_machine_from_path(
            old_state_machine_path)
        state_machine_m = StateMachineModel(state_machine)
        new_state_machine_path = testing_utils.get_unique_temp_path()
        storage.save_state_machine_to_path(state_machine,
                                           new_state_machine_path, True, True)
        state_machine_m.store_meta_data(copy_path=new_state_machine_path)

        old_state_machine_hash = calculate_state_machine_hash(
            old_state_machine_path)
        new_state_machine_hash = calculate_state_machine_hash(
            new_state_machine_path)
        assert old_state_machine_hash.digest(
        ) == new_state_machine_hash.digest()
    except Exception:
        raise
    finally:
        testing_utils.shutdown_environment(caplog=caplog,
                                           unpatch_threading=False)
Beispiel #18
0
def create_state_machine():
    state_machine_path = testing_utils.get_test_sm_path(os.path.join("unit_test_state_machines", "history_test"))
    state_machine = storage.load_state_machine_from_path(state_machine_path)
    state_dict = {
        'Container': state_machine.get_state_by_path("CONT2"),
        'State1': state_machine.get_state_by_path("CONT2/STATE1"),
        'State2': state_machine.get_state_by_path("CONT2/STATE2"),
        'State3': state_machine.get_state_by_path("CONT2/STATE3"),
        'Nested': state_machine.get_state_by_path("CONT2/STATE3/NESTED"),
        'Nested2': state_machine.get_state_by_path("CONT2/STATE3/NESTED2")
    }
    return state_machine, state_dict
def test_missing_rafcon_library_state_error_raise(caplog):
    config_before = rafcon.core.config.global_config.get_config_value(
        "RAISE_ERROR_ON_MISSING_LIBRARY_STATES", None)
    rafcon.core.config.global_config.set_config_value(
        "RAISE_ERROR_ON_MISSING_LIBRARY_STATES", True)
    test_path = testing_utils.get_test_sm_path(
        os.path.join("unit_test_state_machines",
                     "test_sm_with_missing_lib_state"))
    try:
        storage.load_state_machine_from_path(test_path)
        if config_before is not None:
            rafcon.core.config.global_config.set_config_value(
                "RAISE_ERROR_ON_MISSING_LIBRARY_STATES", config_before)
        pytest.fail(
            msg=
            "Loading a state machine with missing library state did not raise a LibraryNotFoundException",
            pytrace=True)
    except Exception as e:
        if config_before is not None:
            rafcon.core.config.global_config.set_config_value(
                "RAISE_ERROR_ON_MISSING_LIBRARY_STATES", config_before)
        assert type(e) is LibraryNotFoundException
Beispiel #20
0
def test_rename_library_root(caplog):
    testing_utils.initialize_environment(gui_already_started=False,
                                         libraries={})

    try:
        from rafcon.gui.helpers.state_machine import rename_library_root
        from rafcon.gui.singleton import global_config

        state_machine_path = os.path.abspath(
            os.path.join(testing_utils.TEST_STATE_MACHINES_PATH,
                         STATE_MACHINE_NAME))

        rename_library_root(CURRENT_LIBRARY_ROOT_NAME, NEW_LIBRARY_ROOT_NAME)
        library_manager.clean_loaded_libraries()
        library_manager.refresh_libraries()

        testing_utils.shutdown_environment(caplog=caplog,
                                           unpatch_threading=False)
        testing_utils.initialize_environment(
            gui_already_started=False,
            libraries={"new_generic": testing_utils.GENERIC_PATH})
        library = storage.load_state_machine_from_path(state_machine_path)

        assert library is not None

        library_paths = global_config.get_config_value('LIBRARY_PATHS')
        del library_paths[CURRENT_LIBRARY_ROOT_NAME]
        global_config.save_configuration()

        rename_library_root(NEW_LIBRARY_ROOT_NAME, CURRENT_LIBRARY_ROOT_NAME)
        library_manager.clean_loaded_libraries()
        library_manager.refresh_libraries()

        library = storage.load_state_machine_from_path(state_machine_path)

        assert library is not None
    finally:
        testing_utils.shutdown_environment(caplog=caplog,
                                           unpatch_threading=False)
Beispiel #21
0
def test_functionality_example(caplog):
    """Test for now only tests:
    - if the state machine can be open
    - if test can be run and stopped
    - and everything can be closed again
    """
    import rafcon.core.singleton
    from rafcon.core.storage import storage

    # The test maybe should also test if functionality are correct depicted.
    # TODO check if this is done in the common tests already

    for name in [
            'backward_step_barrier', 'backward_step_hierarchy',
            'backward_step_preemption', 'decider_statemachine',
            'hierarchy_abortion_handling'
    ]:
        sm_path = join(testing_utils.EXAMPLES_PATH, 'functionality_examples',
                       name)
        print(sm_path)
        state_machine = storage.load_state_machine_from_path(sm_path)
        rafcon.core.singleton.state_machine_manager.add_state_machine(
            state_machine)

    testing_utils.test_multithreading_lock.acquire()
    try:
        # main_window_controller = rafcon.gui.singleton.main_window_controller
        for state_machine_id in list(
                rafcon.core.singleton.state_machine_manager.state_machines.
                keys()):
            rafcon.core.singleton.state_machine_execution_engine.start(
                state_machine_id)
            max_time = 3
            current_time = 0.0
            sleep_time = 0.2
            while not rafcon.core.singleton.state_machine_execution_engine.finished_or_stopped(
            ):
                time.sleep(sleep_time)
                current_time += sleep_time
                if current_time >= max_time:
                    break
            rafcon.core.singleton.state_machine_execution_engine.stop()
            rafcon.core.singleton.state_machine_execution_engine.join()
    finally:
        testing_utils.wait_for_gui(
        )  # to avoid execution and model notification clinches
        testing_utils.shutdown_environment(gui_config=False,
                                           caplog=caplog,
                                           expected_warnings=2,
                                           expected_errors=4,
                                           unpatch_threading=False)
def test_concurrency_preemption_save_load(caplog):
    testing_utils.test_multithreading_lock.acquire()

    storage_path = testing_utils.get_unique_temp_path()

    preemption_state_sm = create_preemption_state_machine()

    storage.save_state_machine_to_path(preemption_state_sm, storage_path)
    storage.load_state_machine_from_path(storage_path)

    rafcon.core.singleton.state_machine_manager.add_state_machine(
        preemption_state_sm)
    rafcon.core.singleton.state_machine_execution_engine.start(
        preemption_state_sm.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()

    try:
        assert rafcon.core.singleton.global_variable_manager.get_variable(
            "preempted_state2_code") == "DF3LFXD34G"
        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            preemption_state_sm.state_machine_id)
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
Beispiel #23
0
def test_last_wins_value_collection_for_data_ports(caplog):

    sm_path = testing_utils.get_test_sm_path(os.path.join("unit_test_state_machines", "last_data_wins_test"))
    sm_loaded = storage.load_state_machine_from_path(sm_path)

    root_state = sm_loaded.root_state

    state_machine = StateMachine(root_state)
    testing_utils.test_multithreading_lock.acquire()

    rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine)
    rafcon.core.singleton.state_machine_execution_engine.start(state_machine.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_manager.remove_state_machine(state_machine.state_machine_id)
    testing_utils.shutdown_environment_only_core(caplog=caplog)
def test_execute_script_returns_none(caplog):
    testing_utils.initialize_environment_core()

    state_machine_path = testing_utils.get_test_sm_path(os.path.join("unit_test_state_machines", "return_none_test_sm"))

    state_machine = storage.load_state_machine_from_path(state_machine_path)
    rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine)

    assert state_machine.file_system_path == state_machine_path

    rafcon.core.singleton.state_machine_execution_engine.start(state_machine.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()

    try:
        assert state_machine.root_state.final_outcome.outcome_id == 0
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog, expected_warnings=0, expected_errors=1)
Beispiel #25
0
def test_execution_log_without_file_system(caplog):
    try:
        testing_utils.initialize_environment_core(
            core_config={
                'IN_MEMORY_EXECUTION_HISTORY_ENABLE':
                True,
                'FILE_SYSTEM_EXECUTION_HISTORY_ENABLE':
                False,
                'EXECUTION_LOG_PATH':
                testing_utils.get_unique_temp_path() + '/test_execution_log'
            })

        state_machine = global_storage.load_state_machine_from_path(
            testing_utils.get_test_sm_path(
                os.path.join("unit_test_state_machines",
                             "execution_file_log_test")))

        rafcon.core.singleton.state_machine_manager.add_state_machine(
            state_machine)
        rafcon.core.singleton.state_machine_execution_engine.start(
            state_machine.state_machine_id)
        while not state_machine.root_state.final_outcome:
            time.sleep(0.1)
        rafcon.core.singleton.state_machine_execution_engine.join()

        execution_history = state_machine.execution_histories[0]

        assert len(execution_history) == 32
        assert isinstance(execution_history[0], StateMachineStartItem)
        assert isinstance(execution_history[1], CallItem)
        assert isinstance(execution_history[2], CallItem)
        assert isinstance(execution_history[3], ReturnItem)

        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            state_machine.state_machine_id)
    except ImportError:  # if pandas is not installed
        print("test_execution_log skipped as pandas is not installed")
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                     expected_warnings=0,
                                                     expected_errors=0)
Beispiel #26
0
def test_transition_creation(caplog):

    storage_path = testing_utils.get_unique_temp_path()

    sm = create_state_machine()

    storage.save_state_machine_to_path(sm, storage_path)
    sm_loaded = storage.load_state_machine_from_path(storage_path)

    root_state = sm_loaded.root_state

    state_machine = StateMachine(root_state)
    with testing_utils.test_multithreading_lock:
        rafcon.core.singleton.state_machine_manager.add_state_machine(
            state_machine)
        rafcon.core.singleton.state_machine_execution_engine.start(
            state_machine.state_machine_id)
        rafcon.core.singleton.state_machine_execution_engine.join()
        testing_utils.assert_logger_warnings_and_errors(caplog)

    rafcon.core.singleton.state_machine_manager.delete_all_state_machines()
Beispiel #27
0
def test_scoped_variables(caplog):

    storage_path = testing_utils.get_unique_temp_path()

    sm = create_state_machine()

    storage.save_state_machine_to_path(sm, storage_path)
    sm_loaded = storage.load_state_machine_from_path(storage_path)

    state_machine = StateMachine(sm_loaded.root_state)

    with testing_utils.test_multithreading_lock:
        rafcon.core.singleton.state_machine_manager.add_state_machine(
            state_machine)
        rafcon.core.singleton.state_machine_execution_engine.start(
            state_machine.state_machine_id)
        rafcon.core.singleton.state_machine_execution_engine.join()
        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            state_machine.state_machine_id)
        assert state_machine.root_state.output_data["output_data_port1"] == 42
        testing_utils.assert_logger_warnings_and_errors(caplog)
Beispiel #28
0
def test_execution_log(caplog):

    testing_utils.initialize_environment_core(
        core_config={
            'IN_MEMORY_EXECUTION_HISTORY_ENABLE':
            False,
            'FILE_SYSTEM_EXECUTION_HISTORY_ENABLE':
            False,
            'EXECUTION_LOG_PATH':
            testing_utils.get_unique_temp_path() + '/test_execution_log'
        })

    # this state machine features:
    # * hierarchies
    # * barrier concurrency
    # * execution states
    # * data flows
    # * logic flows
    state_machine = global_storage.load_state_machine_from_path(
        testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines",
                         "execution_file_log_test")))

    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)
    rafcon.core.singleton.state_machine_execution_engine.start(
        state_machine.state_machine_id)
    while not state_machine.root_state.final_outcome:
        time.sleep(0.1)
    rafcon.core.singleton.state_machine_execution_engine.join()

    rafcon.core.singleton.state_machine_manager.remove_state_machine(
        state_machine.state_machine_id)

    # the test assertions are that there are no errors/warnings
    testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                 expected_warnings=0,
                                                 expected_errors=0)
Beispiel #29
0
def test_run_this_state(caplog):
    # run_selected
    # Initialize testing environment
    testing_utils.initialize_environment_core()
    # Load State Machine
    sm = storage.load_state_machine_from_path(
        testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines", "test_run_this_state")))
    rafcon.core.singleton.state_machine_manager.add_state_machine(sm)
    # Run selected state machine
    rafcon.core.singleton.global_variable_manager.set_variable("test_value", 1)
    state_machine_execution_engine.run_selected_state("BTWFZQ/EPQSTG",
                                                      sm.state_machine_id)
    wait_for_execution_engine_sync_counter(1, logger)
    state_machine_execution_engine.stop()
    rafcon.core.singleton.state_machine_execution_engine.join()
    # assert variable state
    try:
        assert rafcon.core.singleton.global_variable_manager.get_variable(
            "test_value") == 2
    # Shutdown testing environment
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
Beispiel #30
0
def start_server(interacting_function, queue_dict):
    import sys
    import os

    import rafcon
    from rafcon.utils import log
    from rafcon.utils import plugins

    from rafcon.core.config import global_config
    import rafcon.core.singleton as core_singletons
    from rafcon.core.storage import storage as global_storage
    # needed for yaml parsing
    from rafcon.core.states.hierarchy_state import HierarchyState
    from rafcon.core.states.execution_state import ExecutionState
    from rafcon.core.states.preemptive_concurrency_state import PreemptiveConcurrencyState
    from rafcon.core.states.barrier_concurrency_state import BarrierConcurrencyState

    from rafcon.core.start import signal_handler, register_signal_handlers
    register_signal_handlers(signal_handler)

    logger = log.get_logger("start-no-gui")
    logger.info("initialize RAFCON ... ")

    plugins.load_plugins()
    plugins.run_pre_inits()

    global_config.load(path=os.path.dirname(os.path.abspath(__file__)))

    # Initialize libraries
    core_singletons.library_manager.initialize()

    from tests import utils as testing_utils
    state_machine = global_storage.load_state_machine_from_path(
        testing_utils.get_test_sm_path(os.path.join("unit_test_state_machines", "99_bottles_of_beer_monitoring")))
    sm_id = rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine)

    sm_thread = threading.Thread(target=check_for_sm_finished, args=[state_machine, ])
    sm_thread.start()

    setup_config = dict()
    setup_config["net_config_path"] = os.path.abspath(path=os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                                                        "server"))

    plugins.run_post_inits(setup_config)

    if "twisted" in sys.modules:
        print("################# twisted found #######################")
        interacting_thread = threading.Thread(target=interacting_function,
                                              args=[queue_dict, sm_id])
        interacting_thread.start()
        from twisted.internet import reactor
        reactor.run()
    else:
        logger.error("Server: Twisted is not in sys.modules or twisted is not working! Exiting program ... !")
        import os
        os._exit(0)

    state_machine.root_state.join()

    rafcon.core.singleton.state_machine_execution_engine.stop()
    logger.info("State machine execution finished!")