Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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!")
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
def test_error_propagation(caplog):
    testing_utils.initialize_environment_core()

    sm = state_machine_execution_engine.execute_state_machine_from_path(
        path=testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines",
                         "error_propagation_test")))
    state_machine_manager.remove_state_machine(sm.state_machine_id)
    try:
        assert sm.root_state.output_data["error_check"] == "successfull"
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                     expected_warnings=1,
                                                     expected_errors=2)
Ejemplo n.º 5
0
def test_preemption_behaviour_in_preemption_state(caplog):
    testing_utils.initialize_environment_core()

    sm = state_machine_execution_engine.execute_state_machine_from_path(
        path=testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines",
                         "preemption_behaviour_test_sm")))
    rafcon.core.singleton.state_machine_manager.remove_state_machine(
        sm.state_machine_id)
    from rafcon.core.singleton import global_variable_manager
    try:
        assert global_variable_manager.get_variable("s2") == 1.0
        assert not global_variable_manager.variable_exist("s3")
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
Ejemplo n.º 6
0
def test_custom_entry_point(caplog):
    testing_utils.initialize_environment_core()

    start_state_id = "RWUZOP/ZDWBKU/HADSLI"
    sm = rafcon.core.singleton.state_machine_execution_engine.execute_state_machine_from_path(
        path=testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines",
                         "test_custom_entry_point")),
        start_state_path=start_state_id)
    rafcon.core.singleton.state_machine_manager.remove_state_machine(
        sm.state_machine_id)
    try:
        assert not rafcon.core.singleton.global_variable_manager.variable_exist(
            "start_id21")
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
Ejemplo n.º 7
0
def test_multi_events(caplog):
    testing_utils.initialize_environment_core()

    execution_trigger_thread = threading.Thread(
        target=trigger_exectuion_engine,
        args=[global_variable_manager, state_machine_execution_engine])
    execution_trigger_thread.start()

    sm = state_machine_execution_engine.execute_state_machine_from_path(
        path=testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines", "multi_events_test")))

    execution_trigger_thread.join()
    state_machine_manager.remove_state_machine(sm.state_machine_id)
    try:
        assert global_variable_manager.get_variable("sm_status") == 2
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                     expected_warnings=0,
                                                     expected_errors=0)
Ejemplo n.º 8
0
def test_error_propagation(caplog, recompile):
    testing_utils.initialize_environment_core(
        {"SCRIPT_RECOMPILATION_ON_STATE_EXECUTION": recompile})

    sm = state_machine_execution_engine.execute_state_machine_from_path(
        path=testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines",
                         "error_propagation_test")))
    state_machine_manager.remove_state_machine(sm.state_machine_id)
    try:
        assert sm.root_state.output_data["error_check"] == "successfull"
    finally:
        if recompile:
            testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                         expected_warnings=0,
                                                         expected_errors=2)
        else:
            testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                         expected_warnings=0,
                                                         expected_errors=3)
Ejemplo n.º 9
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)
Ejemplo n.º 10
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)
Ejemplo n.º 11
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)
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
def test_execution_log(caplog):
    try:
        testing_utils.initialize_environment_core(
            core_config={
                'IN_MEMORY_EXECUTION_HISTORY_ENABLE':
                True,
                'FILE_SYSTEM_EXECUTION_HISTORY_ENABLE':
                True,
                '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()

        import shelve
        import json
        ss = shelve.open(state_machine.get_last_execution_log_filename())

        target_dict = {}  # can be used for debugging
        for key, value in ss.items():
            # print(value)
            target_dict[int(key[-3:].lstrip("0"))] = value

        assert len(ss) == 36

        start, next, concurrent, hierarchy, collapsed_items = log_helper.log_to_collapsed_structure(
            ss)

        prod1_id = [
            k for k, v in collapsed_items.items()
            if v['state_name'] == 'MakeProd1'
        ][0]
        prod1 = collapsed_items[prod1_id]
        assert prod1['scoped_data_ins']['product'] == 2
        assert prod1['scoped_data_outs']['product'] == 2
        assert prod1['outcome_name'] == 'success'
        assert prod1['semantic_data']['test_key'] == 'TestValue'

        prod2_id = [
            k for k, v in collapsed_items.items()
            if v['state_name'] == 'MakeProd2'
        ][0]
        prod2 = collapsed_items[prod2_id]
        assert prod2['data_ins']['input_1'] == 0
        assert prod2['data_outs']['output_1'] == 3
        assert prod2['scoped_data_ins']['product'] == 1
        assert prod2['scoped_data_outs']['product'] == 1

        start_states = [
            k for k, v in collapsed_items.items() if v['state_name'] == 'Start'
            and v['state_type'] == 'ExecutionState'
        ]
        assert len(
            start_states
        ) == 3  # Start state is executed three times until state machine is done
        start_id = start_states[0]
        start_item = collapsed_items[start_id]
        assert 'Starts the factory' in start_item['description']

        df = log_helper.log_to_DataFrame(ss)
        all_starts = df.groupby('state_name').get_group('Start')
        assert len(all_starts) == 3
        assert list(
            all_starts['outcome_name']) == ['success', 'success', 'done']

        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)
Ejemplo n.º 14
0
def test_step_into_over_out_no_library(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)

    with state_machine_execution_engine._status.execution_condition_variable:
        state_machine_execution_engine.synchronization_counter = 0

    rafcon.core.singleton.state_machine_execution_engine.step_mode(
        state_machine.state_machine_id)
    wait_for_execution_engine_sync_counter(1, logger)

    # sm structure

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

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

    execute_command_synchronized_on_state(state_machine, "GLSUJY/PXTKIH",
                                          "step_over", 1)
    execute_command_synchronized_on_state(state_machine, "GLSUJY/NDIVLD",
                                          "step_over", 1)
    execute_command_synchronized_on_state(state_machine, "GLSUJY/SFZGMH",
                                          "step_over", 1)
    execute_command_synchronized_on_state(state_machine, "GLSUJY/SMCOIB",
                                          "step_over", 1)

    execute_command_synchronized_on_state(state_machine, "GLSUJY/PXTKIH",
                                          "step_into")
    execute_command_synchronized_on_state(state_machine, "GLSUJY/NDIVLD",
                                          "step_into")
    execute_command_synchronized_on_state(state_machine, "GLSUJY/SFZGMH",
                                          "step_into")
    execute_command_synchronized_on_state(state_machine,
                                          "GLSUJY/SMCOIB",
                                          "step_into",
                                          join=False)
    execute_command_synchronized_on_state(state_machine,
                                          "GLSUJY/SMCOIB/YSBJGK", "step_into")

    rafcon.core.singleton.state_machine_execution_engine.step_out()
    wait_for_execution_engine_sync_counter(1, logger)

    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)
Ejemplo n.º 15
0
def test_step_through_library(caplog):

    testing_utils.initialize_environment_core()

    testing_utils.rewind_and_set_libraries({
        "unit_test_state_machines":
        os.path.join(testing_utils.TEST_ASSETS_PATH,
                     "unit_test_state_machines")
    })

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

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

    with state_machine_execution_engine._status.execution_condition_variable:
        state_machine_execution_engine.synchronization_counter = 0

    rafcon.core.singleton.state_machine_execution_engine.step_mode(
        state_machine.state_machine_id)
    wait_for_execution_engine_sync_counter(1, logger)

    # step till library
    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    # step into library and step out
    rafcon.core.singleton.state_machine_execution_engine.step_into()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.step_out()
    wait_for_execution_engine_sync_counter(1, logger)

    # step till library
    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    # step into library and step over library children
    rafcon.core.singleton.state_machine_execution_engine.step_into()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    # step over last library item
    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

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

    try:
        from rafcon.core.state_elements.scope import ScopedVariable
        for s in state_machine.root_state.scoped_data.values():
            if s.name == 'bottles' and s.data_port_type == ScopedVariable:
                assert s.value == 4
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
def test_execution_log(caplog):
    try:
        testing_utils.initialize_environment_core(
            core_config={
                'EXECUTION_LOG_ENABLE':
                True,
                '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)
        rafcon.core.singleton.state_machine_execution_engine.join()

        import shelve
        import json
        ss = shelve.open(state_machine.get_last_execution_log_filename())

        assert len(ss) == 36

        start, next, concurrent, hierarchy, collapsed_items = log_helper.log_to_collapsed_structure(
            ss)

        prod1_id = [
            k for k, v in collapsed_items.items()
            if v['state_name'] == 'MakeProd1'
        ][0]
        prod1 = collapsed_items[prod1_id]
        assert prod1['scoped_data_ins']['product'] == 2
        assert prod1['scoped_data_outs']['product'] == 2
        assert prod1['outcome_name'] == 'success'
        assert prod1['semantic_data']['test_key'] == 'TestValue'

        prod2_id = [
            k for k, v in collapsed_items.items()
            if v['state_name'] == 'MakeProd2'
        ][0]
        prod2 = collapsed_items[prod2_id]
        assert prod2['data_ins']['input_1'] == 0
        assert prod2['data_outs']['output_1'] == 3
        assert prod2['scoped_data_ins']['product'] == 1
        assert prod2['scoped_data_outs']['product'] == 1

        start_states = [
            k for k, v in collapsed_items.items() if v['state_name'] == 'Start'
            and v['state_type'] == 'ExecutionState'
        ]
        assert len(
            start_states
        ) == 3  # Start state is executed three times until state machine is done
        start_id = start_states[0]
        start_item = collapsed_items[start_id]
        assert 'Starts the factory' in start_item['description']

        df = log_helper.log_to_DataFrame(ss)
        all_starts = df.groupby('state_name').get_group('Start')
        assert len(all_starts) == 3
        assert list(
            all_starts['outcome_name']) == ['success', 'success', 'done']

        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            state_machine.state_machine_id)
    except ImportError:  # if pandas is not installed
        pass
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                     expected_warnings=0,
                                                     expected_errors=0)