def execute_library_state_forwards_backwards(): from rafcon.core.singleton import state_machine_execution_engine, state_machine_manager import rafcon.gui.singleton as gui_singleton menubar_ctrl = gui_singleton.main_window_controller.get_controller( 'menu_bar_controller') call_gui_callback( menubar_ctrl.on_open_activate, None, None, testing_utils.get_test_sm_path( os.path.join("unit_test_state_machines", "backward_step_library_execution_test"))) testing_utils.wait_for_gui() # reset the synchronization counter; although the tests run in different processes they share their memory # as the import statements are at the top of the file and not inside the parallel called functions state_machine_execution_engine.synchronization_lock.acquire() state_machine_execution_engine.synchronization_counter = 0 state_machine_execution_engine.synchronization_lock.release() call_gui_callback(menubar_ctrl.on_step_mode_activate, None, None) wait_for_execution_engine_sync_counter(1, logger) # forward for i in range(5): call_gui_callback(menubar_ctrl.on_step_into_activate, None, None) wait_for_execution_engine_sync_counter(1, logger) # backward for i in range(4): call_gui_callback(menubar_ctrl.on_backward_step_activate, None, None) wait_for_execution_engine_sync_counter(1, logger) call_gui_callback(menubar_ctrl.on_backward_step_activate, None, None) sm = state_machine_manager.get_active_state_machine() while not state_machine_execution_engine.finished_or_stopped(): time.sleep(0.1) for key, sd in sm.root_state.scoped_data.iteritems(): if sd.name == "beer_count": assert sd.value == 100 call_gui_callback(menubar_ctrl.on_stop_activate, None)
def execute_dynamic_state_insertion(): from rafcon.core.singleton import state_machine_execution_engine, state_machine_manager import rafcon.gui.singleton as gui_singleton menubar_ctrl = gui_singleton.main_window_controller.get_controller('menu_bar_controller') call_gui_callback( menubar_ctrl.on_open_activate, None, None, testing_utils.get_test_sm_path(os.path.join("unit_test_state_machines", "dynamic_library_insertion")) ) testing_utils.wait_for_gui() call_gui_callback(menubar_ctrl.on_start_activate, None, None) testing_utils.wait_for_gui() sm = state_machine_manager.get_active_state_machine() while not state_machine_execution_engine.finished_or_stopped(): time.sleep(0.1) call_gui_callback(menubar_ctrl.on_stop_activate, None)
def synchronize_with_client_threads(queue_dict, execution_engine): from rafcon.core.singleton import global_variable_manager as gvm from rafcon.core.execution.execution_status import StateMachineExecutionStatus from rafcon.core.singleton import state_machine_manager from rafcon.core.execution.execution_engine import ExecutionEngine assert isinstance(execution_engine, ExecutionEngine) active_sm = state_machine_manager.get_active_state_machine() root_state = active_sm.root_state sleep_time = 0.01 # wait for the client to start queue_dict[CLIENT1_TO_SERVER_QUEUE].get() queue_dict[SERVER_TO_CLIENT1_QUEUE].put("ready") # start pause resume test # assuming that inter process communication is faster than networking the state machine should be started, # paused and resumed in the meantime # wait until execution engine is started while execution_engine.status.execution_mode is not StateMachineExecutionStatus.STARTED: time.sleep(sleep_time) # let the state machine run for a while time.sleep(0.5) # synchronize with client client2 queue_dict[CLIENT2_TO_SERVER_QUEUE].get() queue_dict[SERVER_TO_CLIENT2_QUEUE].put("ready") # wait until execution is finished while not execution_engine.finished_or_stopped(): time.sleep(sleep_time) queue_dict[SERVER_TO_CLIENT1_QUEUE].put( "state machine executed successfully") # synchronize with client1 queue_dict[SERVER_TO_CLIENT2_QUEUE].put( "state machine executed successfully") # synchronize with client2 print "server: start pause resume test successful\n\n" execution_engine.stop() execution_engine.join() queue_dict[KILL_SERVER_QUEUE].get(3) os._exit(0)
def change_semantic_data_values(): # core elements from rafcon.core.singleton import state_machine_manager # gui elements import rafcon.gui.singleton as gui_singleton from rafcon.gui.controllers.state_editor.semantic_data_editor import SemanticDataEditorController menu_bar_controller = gui_singleton.main_window_controller.get_controller( "menu_bar_controller") # All executions from a thread which is not the gtk main thread must be called via "idle_add" or call_gui_callback # see https://developer.gnome.org/gdk3/stable/gdk3-Threads.html#gdk-threads-add-idle-full # and https://stackoverflow.com/questions/35700140/pygtk-run-gtk-main-loop-in-a-seperate-thread call_gui_callback(menu_bar_controller.on_new_activate) state_machine = state_machine_manager.get_active_state_machine() root_state = state_machine.root_state call_gui_callback(initialize_data, root_state) state_machine_model = gui_singleton.state_machine_manager_model.state_machines[ state_machine.state_machine_id] states_editor_controller = gui_singleton.main_window_controller.get_controller( "states_editor_ctrl") page_info, state_identifier = states_editor_controller.find_page_of_state_m( state_machine_model.root_state) # print page_info, state_identifier state_editor_controller = states_editor_controller.tabs[state_identifier][ "controller"] semantic_data_controller = state_editor_controller.get_controller( "semantic_data_ctrl") assert isinstance(semantic_data_controller, SemanticDataEditorController) # TODO: why is this reload necessary? call_gui_callback(semantic_data_controller.reload_tree_store_data) call_gui_callback(semantic_data_controller.select_entry, ['key 2']) tree_test_path = (1, ) # semantic_data_controller.tree_view.get_selection().select_path(tree_test_path) # wait while an element is selected # while True: # treeiter, path = semantic_data_controller.get_selected_object() # # check if an element is selected # if treeiter: # dict_path_as_list = semantic_data_controller.tree_store[path][semantic_data_controller.ID_STORAGE_ID] # # semantic_data_controller.model.state.remove_semantic_data(dict_path_as_list) # data = semantic_data_controller.model.state.get_semantic_data(dict_path_as_list) # print data # break # time.sleep(0.1) # model, paths = semantic_data_controller.tree_view.get_selection().get_selected_rows() # print model, paths # print root_state.semantic_data # test delete assert len(root_state.semantic_data.keys()) == 5 call_gui_callback(semantic_data_controller.select_entry, ['dict 2']) # semantic_data_controller.select_entry(['dict 2']) call_gui_callback(semantic_data_controller.on_remove, None) assert len(root_state.semantic_data.keys() ) == 4 and 'dict 2' not in root_state.semantic_data.keys() # test add normal entry into first hierarchy call_gui_callback( semantic_data_controller.tree_view.get_selection().select_path, tree_test_path) call_gui_callback(semantic_data_controller.on_add, None, False) assert len(root_state.semantic_data.keys()) == 5 # test add dictionary entry in second hierarchy # tree_test_path = semantic_data_controller.get_path_for_core_element(["dict 1"]) # print "second hierarchy: ", tree_test_path assert len(root_state.semantic_data["dict 1"].keys()) == 2 # call_gui_callback(semantic_data_controller.tree_view.get_selection().select_path, tree_test_path) call_gui_callback(semantic_data_controller.select_entry, ['dict 1']) call_gui_callback(semantic_data_controller.on_add, None, False) assert len(root_state.semantic_data.keys()) == 5 assert len(root_state.semantic_data["dict 1"].keys()) == 3
def synchronize_with_clients_threads(queue_dict, execution_engine): from rafcon.core.singleton import global_variable_manager as gvm from rafcon.core.execution.execution_status import StateMachineExecutionStatus from rafcon.core.singleton import state_machine_manager from rafcon.core.execution.execution_engine import ExecutionEngine assert isinstance(execution_engine, ExecutionEngine) active_sm = state_machine_manager.get_active_state_machine() root_state = active_sm.root_state sleep_time = 0.01 # check when run to is finished while gvm.get_variable("sing_counter") < 1: time.sleep(sleep_time) synchronize_with_root_state(root_state, execution_engine, "PXTKIH") # wait for the client to start queue_dict[CLIENT1_TO_SERVER_QUEUE].get() queue_dict[SERVER_TO_CLIENT1_QUEUE].put("start stepping") print "starting tests\n\n" print "server: cp0" # step test while gvm.get_variable("decimate_counter") < 1: time.sleep(sleep_time) # sleep just to prevent busy loop synchronize_with_root_state(root_state, execution_engine, "NDIVLD") queue_dict[SERVER_TO_CLIENT1_QUEUE].put( TestSteps[0]) # step mode also means a step into print "server: cp1" while gvm.get_variable("count_counter") < 1: time.sleep(sleep_time) synchronize_with_root_state(root_state, execution_engine, "SFZGMH") queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[1]) # step over print "server: cp2" while gvm.get_variable("sing_counter") < 2: time.sleep(sleep_time) synchronize_with_root_state(root_state, execution_engine, "PXTKIH") queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[2]) # step into print "server: cp3" while gvm.get_variable("sing_counter") > 1: time.sleep(sleep_time) # wait until the backward execution of the state is done synchronize_with_root_state(root_state, execution_engine, "PXTKIH") queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[3]) # backward step print "server: cp4" while not execution_engine.finished_or_stopped(): time.sleep(sleep_time) execution_engine.join() queue_dict[SERVER_TO_CLIENT1_QUEUE].put( TestSteps[4]) # step out and run until the end reset_global_variable_manager(gvm) print "server: step test successful\n\n" # start and finish execution test while execution_engine.status.execution_mode is not StateMachineExecutionStatus.STARTED: time.sleep(sleep_time) # wait until the state machine executed successfully while not execution_engine.finished_or_stopped(): time.sleep(sleep_time) reset_global_variable_manager(gvm) # as the state machine run to the end this is safe execution_engine.stop() # reset state machine before the next test # do not notify the client before the state machine is really stopped execution_engine.join() print "server: start and wait until finished test successful\n\n" # old_sync_counter = execution_engine.synchronization_counter queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[5]) # run until end # run-until test # this is dangerous: the child state is still being executed when setting the "decimate_counter" # global variable and thus the root_state=Hierarchy state is not YET handling the execution mode # while gvm.get_variable("decimate_counter") < 1 and not root_state.handling_execution_mode: # time.sleep(sleep_time) # this also can produce race conditions # while not(execution_engine.synchronization_counter == old_sync_counter + 3): # time.sleep(sleep_time) # this is safe: while gvm.get_variable("decimate_counter") < 1: time.sleep(sleep_time) synchronize_with_root_state(root_state, execution_engine, "NDIVLD") assert gvm.get_variable("count_counter") == 0 queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[6]) while not execution_engine.finished_or_stopped(): time.sleep(sleep_time) execution_engine.join() reset_global_variable_manager(gvm) print "server: run until test successful\n\n" queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[7]) # start from test while execution_engine.status.execution_mode is not StateMachineExecutionStatus.STARTED: time.sleep(sleep_time) while not execution_engine.finished_or_stopped(): time.sleep(sleep_time) assert gvm.get_variable("sing_counter") == 2 assert gvm.get_variable("decimate_counter") == 3 # as the state machine run to the end this is safe execution_engine.stop() execution_engine.join() print "server: start from test successful\n" queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[8]) execution_engine.stop() execution_engine.join() print "server: wait for sync message from client\n" queue_dict[CLIENT1_TO_SERVER_QUEUE].get() print "server: send sync message to client\n" queue_dict[SERVER_TO_CLIENT1_QUEUE].put("sync") print "server: wait for kill command from main queue\n" # set a timeout of 3 seconds queue_dict[KILL_SERVER_QUEUE].get(3) os._exit(0)
def synchronize_with_clients_threads(queue_dict, execution_engine): from rafcon.core.singleton import global_variable_manager as gvm from rafcon.core.execution.execution_status import StateMachineExecutionStatus from rafcon.core.singleton import state_machine_manager active_sm = state_machine_manager.get_active_state_machine() root_state = active_sm.root_state sleep_time = 0.01 # check when run to is finished while gvm.get_variable("sing_counter") < 1: time.sleep(sleep_time) """ TEST1 """ queue_dict[SERVER_TO_CLIENT].put("start") print "starting tests\n\n" # stop start test while not execution_engine.finished_or_stopped(): time.sleep(sleep_time) queue_dict[SERVER_TO_CLIENT].put("stop received") # wait until state machine started while execution_engine.status.execution_mode is not StateMachineExecutionStatus.STARTED: time.sleep(sleep_time) while not execution_engine.finished_or_stopped(): time.sleep(sleep_time) reset_global_variable_manager(gvm) execution_engine.stop() # reset state machine before the next test if execution_engine.status.execution_mode is not StateMachineExecutionStatus.STOPPED: execution_engine.join() queue_dict[SERVER_TO_CLIENT].put( "start received and successfully ran the state machine") print "server: stop start test successful\n\n" """ TEST2 disconnect -> run sm -> connect -> run sm """ from monitoring import server from monitoring.monitoring_manager import global_monitoring_manager # test disconnect by client run print "disconnect/connect by client test" queue_dict[SERVER_TO_CLIENT].put("disconnect_me_and_run") queue_dict[CLIENT_TO_SERVER].get() assert gvm.get_variable("count_counter") == 0 assert gvm.get_variable("sing_counter") == 0 assert gvm.get_variable("decimate_counter") == 0 queue_dict[SERVER_TO_CLIENT].put("reconnect_me_and_run") queue_dict[CLIENT_TO_SERVER].get() if not execution_engine.finished_or_stopped(): execution_engine.join() print "server sm finished" assert gvm.get_variable("count_counter") == 3 assert gvm.get_variable("sing_counter") == 3 assert gvm.get_variable("decimate_counter") == 3 queue_dict[SERVER_TO_CLIENT].put("succeeded") execution_engine.stop() if not execution_engine.finished_or_stopped(): execution_engine.join() reset_global_variable_manager(gvm) print "server: disconnect/connect by client test successful\n\n" """ TEST3 disable -> run sm -> enable -> run sm """ # test dis- enabled by server run print "dis- /enabled test by server" for address in server.network_manager_model.connected_ip_port: global_monitoring_manager.disable(address) # while not server.network_manager_model.get_connected_status(address) == "disabled": # time.sleep(0.01) queue_dict[SERVER_TO_CLIENT].put("you are disabled") queue_dict[CLIENT_TO_SERVER].get() print "sm on client executed and stopped" assert gvm.get_variable("count_counter") == 0 assert gvm.get_variable("sing_counter") == 0 assert gvm.get_variable("decimate_counter") == 0 for address in server.network_manager_model.connected_ip_port: global_monitoring_manager.disable(address) queue_dict[SERVER_TO_CLIENT].put("you are enabled") queue_dict[CLIENT_TO_SERVER].get() if not execution_engine.finished_or_stopped(): execution_engine.join() print "server sm finished" assert gvm.get_variable("count_counter") == 3 assert gvm.get_variable("sing_counter") == 3 assert gvm.get_variable("decimate_counter") == 3 queue_dict[SERVER_TO_CLIENT].put("succeeded") execution_engine.stop() if not execution_engine.finished_or_stopped(): execution_engine.join() reset_global_variable_manager(gvm) print "server: dis- /enabled by server test successful\n\n" """ TEST4 change client ID in config -> apply -> check connected client ID """ print "apply config test" queue_dict[SERVER_TO_CLIENT].put("ready to change config") queue_dict[CLIENT_TO_SERVER].get() client_id = [] for address in server.network_manager_model.connected_ip_port: client_id.append( server.network_manager_model.get_connected_id(address)) assert "apply_test_client_id" in client_id queue_dict[SERVER_TO_CLIENT].put("succeeded") print "apply config test successful\n\n" execution_engine.stop() execution_engine.join() queue_dict[KILL_SERVER_QUEUE].get() os._exit(0)
def server_interaction_worker(queue_dict, execution_engine): from rafcon.core.singleton import global_variable_manager as gvm from rafcon.core.execution.execution_status import StateMachineExecutionStatus from rafcon.core.singleton import state_machine_manager from monitoring import server from monitoring.monitoring_manager import global_monitoring_manager active_sm = state_machine_manager.get_active_state_machine() print "active_sm", active_sm root_state = active_sm.root_state sleep_time = 0.99 for key, sv in active_sm.root_state.scoped_variables.iteritems(): if sv.name == "bottles": sv.default_value = 3 ####################################################### print("\n\n\nserver TEST1 stop - start - wait_for_stop") ####################################################### queue_element = queue_dict[CLIENT_TO_SERVER].get() assert queue_element == "start_test_1" print "received: ", queue_element execution_engine.run_to_selected_state( "GLSUJY/NDIVLD") # wait before Decimate Bottles # check when run to is finished; run_to is issued from the server thread while gvm.get_variable("sing_counter") < 1: print "wait for client" time.sleep(sleep_time) print "starting tests\n\n" print "put: started" queue_dict[SERVER_TO_CLIENT].put("started") # step1: stop start test while not execution_engine.finished_or_stopped(): print "wait until state machine finished!" time.sleep(sleep_time) print "put: stop_received" queue_dict[SERVER_TO_CLIENT].put("stop_received") # step2: wait until state machine started while execution_engine.status.execution_mode is not StateMachineExecutionStatus.STARTED: print "execution_engine.status.execution_mode: ", execution_engine.status.execution_mode # this time has to be very small otherwise the state machine start and finish during one sleep period # each state has a sleep of 0.01s time.sleep(0.01) print "put: started" queue_dict[SERVER_TO_CLIENT].put("restarted") # stop the state machine locally execution_engine.stop() # reset state machine before the next test execution_engine.join() reset_global_variable_manager(gvm) queue_dict[SERVER_TO_CLIENT].put("successfully stopped the state machine") print "server: stop start test successful\n\n" # queue_dict[KILL_SERVER_QUEUE].get() # os._exit(0) # return ####################################################### print("\n\n\nserver TEST2 disconnect -> run sm -> connect -> run sm") ####################################################### print "server: starting test 2" reset_global_variable_manager(gvm) queue_element = queue_dict[CLIENT_TO_SERVER].get() assert queue_element == "start_test_2" print "received: ", queue_element # step 1 # test disconnect by client run print "disconnect/connect by client test" queue_dict[SERVER_TO_CLIENT].put("disconnect_me_and_run") queue_element = queue_dict[CLIENT_TO_SERVER].get() assert queue_element == "disconnected_and_executed" print "received: ", queue_element # the client executed a start command, which must not do anything as the client disconnected beforehand assert gvm.get_variable("count_counter") == 0 assert gvm.get_variable("sing_counter") == 0 assert gvm.get_variable("decimate_counter") == 0 # step 2 queue_dict[SERVER_TO_CLIENT].put("reconnect_me_and_run") queue_element = queue_dict[CLIENT_TO_SERVER].get() assert queue_element == "reconnected_and_executed" print "received: ", queue_element execution_engine.join() print "server sm finished" assert gvm.get_variable("count_counter") == 3 assert gvm.get_variable("sing_counter") == 3 assert gvm.get_variable("decimate_counter") == 3 queue_dict[SERVER_TO_CLIENT].put("succeeded") execution_engine.stop() execution_engine.join() reset_global_variable_manager(gvm) print "server: disconnect/connect by client test successful\n\n" # queue_dict[KILL_SERVER_QUEUE].get() # os._exit(0) # return ####################################################### print("\n\n\nserver TEST3 disable -> run sm -> enable -> run sm ") ####################################################### reset_global_variable_manager(gvm) queue_element = queue_dict[CLIENT_TO_SERVER].get() assert queue_element == "start_test_3" print "server received: ", queue_element for address in server.network_manager_model.connected_ip_port: global_monitoring_manager.disable(address) # while not server.network_manager_model.get_connected_status(address) == "disabled": # time.sleep(0.01) queue_dict[SERVER_TO_CLIENT].put("you_are_disabled") queue_element = queue_dict[CLIENT_TO_SERVER].get() assert queue_element == "reached end" print "server received: ", queue_element print "sm on client executed and stopped" assert gvm.get_variable("count_counter") == 0 assert gvm.get_variable("sing_counter") == 0 assert gvm.get_variable("decimate_counter") == 0 # TODO: reconnect is not properly implemented # for address in server.network_manager_model.connected_ip_port: # global_monitoring_manager.reconnect(address) queue_element = queue_dict[CLIENT_TO_SERVER].get() assert queue_element == "enabled_again" print "server received: ", queue_element queue_element = queue_dict[CLIENT_TO_SERVER].get() assert queue_element == "started_execution" print "server received: ", queue_element if not execution_engine.finished_or_stopped(): execution_engine.join() print "server sm finished" assert gvm.get_variable("count_counter") == 3 assert gvm.get_variable("sing_counter") == 3 assert gvm.get_variable("decimate_counter") == 3 queue_dict[SERVER_TO_CLIENT].put("succeeded") execution_engine.stop() execution_engine.join() reset_global_variable_manager(gvm) print "server: dis- /enabled by server test successful\n\n" # queue_dict[KILL_SERVER_QUEUE].get() # os._exit(0) # return ####################################################### print( "server TEST4 change client ID in config -> apply -> check connected client ID" ) ####################################################### reset_global_variable_manager(gvm) queue_element = queue_dict[CLIENT_TO_SERVER].get() assert queue_element == "start_test_4" print "server received: ", queue_element queue_dict[SERVER_TO_CLIENT].put("ready_to_change_config") queue_element = queue_dict[CLIENT_TO_SERVER].get() assert queue_element == "on_apply_button_clicked" print "server received: ", queue_element client_id = [] for address in server.network_manager_model.connected_ip_port: client_id.append( server.network_manager_model.get_connected_id(address)) assert "apply_test_client_id" in client_id queue_dict[SERVER_TO_CLIENT].put("succeeded") print "apply config test successful\n\n" execution_engine.stop() execution_engine.join() queue_dict[KILL_SERVER_QUEUE].get() os._exit(0)