Пример #1
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!")
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)
Пример #3
0
def test_runtime_checks_for_data_port_data_types(caplog):

    storage_path = testing_utils.get_unique_temp_path()
    print(storage_path)

    sm = create_state_machine2()

    # test output port type check
    script_text = 'def execute(self, inputs, outputs, gvm):\n' \
                  '    outputs["output_data_port1"] = [1, 2, 3]\n' \
                  '    return 3'
    sm.get_state_by_path('FirstLevel1/ZeroLevel2').script_text = script_text

    # TODO comment (next 2 lines) in after final fix of tuple storing bug is applied and remove 3 line (4 storage check)
    # storage.save_state_machine_to_path(sm, storage_path)
    # sm_loaded = storage.load_state_machine_from_path(storage_path)
    sm_loaded = sm

    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)
    # 6 errors -> IN ORDER output port-, root state scoped-, input port-, output port-, root state scoped- and
    # root state output port-data-type-errors
    testing_utils.shutdown_environment_only_core(caplog=caplog, expected_errors=6)
Пример #4
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)
Пример #5
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)
Пример #6
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)
Пример #7
0
def test_load_wrong_data_types(caplog):
    testing_utils.test_multithreading_lock.acquire()

    rafcon.core.singleton.state_machine_manager.delete_all_state_machines()
    rafcon.core.singleton.library_manager.initialize()
    state_machine = get_test_state_machine("wrong_data_types")

    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 rafcon.core.singleton.global_variable_manager.get_variable(
            "x") == 1

        # 4 data type errors -> 2 data flow port to port data type inequality and while runtime 1 input- and 1 output data type error
        # 2 data type warnings -> 1 input- and  1 output-data port  data type warnings while loading of state machine
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                     expected_warnings=2,
                                                     expected_errors=4)

    logger.info("State machine execution finished!")
Пример #8
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)
Пример #9
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)
Пример #10
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)
Пример #11
0
def test_load_not_existing_outcome(caplog):
    testing_utils.test_multithreading_lock.acquire()

    rafcon.core.singleton.state_machine_manager.delete_all_state_machines()
    rafcon.core.singleton.library_manager.initialize()
    state_machine = get_test_state_machine("outcome_not_existing")
    # state_machine = get_test_state_machine("correct_library_inclusion")

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

    testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                 expected_warnings=0,
                                                 expected_errors=2)

    logger.info("State machine execution finished!")
Пример #12
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)
Пример #13
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)
Пример #14
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)
Пример #15
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)
Пример #16
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)
Пример #17
0
def test_load_data_ports_not_existing(caplog):
    testing_utils.test_multithreading_lock.acquire()

    state_machine = get_test_state_machine("data_ports_not_existing")
    # state_machine = get_test_state_machine("correct_library_inclusion")

    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 rafcon.core.singleton.global_variable_manager.get_variable(
            "x") == 1
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                     expected_warnings=2,
                                                     expected_errors=0)

    logger.info("State machine execution finished!")
Пример #18
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)
Пример #19
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)
Пример #20
0
 def teardown_class(cls):
     cls.state_machine = None
     testing_utils.shutdown_environment_only_core()
Пример #21
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)
Пример #22
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)
Пример #23
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)
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)