def add_state(container_state_m, state_type): """Add a state to a container state Adds a state of type state_type to the given container_state :param rafcon.gui.models.container_state.ContainerState container_state_m: A model of a container state to add the new state to :param rafcon.core.enums.StateType state_type: The type of state that should be added :return: True if successful, False else """ if container_state_m is None: logger.error("Cannot add a state without a parent.") return False if not isinstance(container_state_m, StateModel) or \ (isinstance(container_state_m, StateModel) and not isinstance(container_state_m, ContainerStateModel)): logger.error("Parent state must be a container, for example a Hierarchy State." + str(container_state_m)) return False state_class = state_type_to_state_class_dict.get(state_type, None) if state_class is None: logger.error("Cannot create state of type {0}".format(state_type)) return False new_state = state_class() from rafcon.gui.models.abstract_state import get_state_model_class_for_state new_state_m = get_state_model_class_for_state(new_state)(new_state) gui_helper_meta_data.put_default_meta_on_state_m(new_state_m, container_state_m) container_state_m.expected_future_models.add(new_state_m) container_state_m.state.add_state(new_state) return True
def create_models_lib(output_list): from rafcon.core.states.library_state import LibraryState import rafcon.gui.helpers.state as gui_helper_state import rafcon.gui.helpers.meta_data as gui_helper_meta_data [state, sm_model, state_dict] = create_models() root_state_m = sm_model.get_state_model_by_path( state_dict['Container'].get_path()) wait3 = LibraryState(name="Wait3", library_path="generic", library_name="wait") state_dict['Nested'].add_state(wait3) dialog2 = LibraryState(name="2 Option", library_path=join("generic", "dialog"), library_name="Dialog [2 options]") dialog3 = LibraryState(name="3 Option", library_path=join("generic", "dialog"), library_name="Dialog [3 options]") state_dict['Nested'].add_state(dialog2) state_dict['Container'].add_state(dialog3) # insert useful meta data d3_m = sm_model.get_state_model_by_path(dialog3.get_path()) gui_helper_meta_data.put_default_meta_on_state_m( d3_m, root_state_m, len(root_state_m.states) - 1) msg = gui_helper_meta_data.MetaSignalMsg(d3_m, 'set_default_meta_data', affects_children=True) root_state_m.meta_signal.emit(msg) # insert library state with correct meta data -> this sm has at the moment only meta data for the root state last_wins = LibraryState(name="last wins", library_path="unit_test_state_machines", library_name="last_data_wins_test") last_wins_root_state = last_wins.state_copy gui_helper_state.insert_state_as(root_state_m, last_wins, True) new_state_id = last_wins_root_state.state_id for state_id in sm_model.root_state.states[new_state_id].states: state_m = sm_model.root_state.states[new_state_id].states[state_id] print(state_m.state.state_id, state_m.state.get_path(), state_m.meta) # sm_loaded = storage.load_state_machine_from_path( # os.path.join(testing_utils.TEST_PATH, "assets", "unit_test_state_machines", "last_data_wins_test")) # # root_state = sm_loaded.root_state # # state_machine = StateMachine(root_state) # rafcon.core.singleton.state_machine_manager.add_state_machine(sm_loaded) # sm_model = rafcon.gui.singleton.state_machine_manager_model.state_machines[sm_loaded.state_machine_id] # return sm_loaded.root_state, sm_model, {} output_list.append(sm_model)
def insert_state_as(target_state_m, state, as_template): """ Add a state into a target state In case the state to be insert is a LibraryState it can be chosen to be insert as template. :param rafcon.gui.models.container_state.ContainerStateModel target_state_m: State model of the target state :param rafcon.core.states.State state: State to be insert as template or not :param bool as_template: The flag determines if a handed state of type LibraryState is insert as template :return: """ if not isinstance(target_state_m, ContainerStateModel) or \ not isinstance(target_state_m.state, ContainerState): logger.error("States can only be inserted in container states") return False state_m = get_state_model_class_for_state(state)(state) if not as_template: gui_helper_meta_data.put_default_meta_on_state_m( state_m, target_state_m) # If inserted as template, we have to extract the state_copy and respective model else: assert isinstance(state, LibraryState) old_lib_state_m = state_m state_m = state_m.state_copy previous_state_size = state_m.get_meta_data_editor()['size'] gui_helper_meta_data.put_default_meta_on_state_m( state_m, target_state_m) # TODO check if the not as template case maybe has to be run with the prepare call prepare_state_m_for_insert_as(state_m, previous_state_size) old_lib_state_m.prepare_destruction(recursive=False) # explicit secure that there is no state_id conflict within target state child states while state_m.state.state_id in target_state_m.state.states: state_m.state.change_state_id() target_state_m.expected_future_models.add(state_m) target_state_m.state.add_state(state_m.state) # secure possible missing models to be generated update_models_recursively(state_m, expected=False)