Ejemplo n.º 1
0
    def __init__(self, state_machine, meta=None, load_meta_data=True):
        """Constructor
        """
        MetaModel.__init__(self)  # pass columns as separate parameters

        assert isinstance(state_machine, StateMachine)

        self.state_machine = state_machine
        self.state_machine_id = state_machine.state_machine_id

        root_state = self.state_machine.root_state
        if isinstance(root_state, ContainerState):
            self.root_state = ContainerStateModel(
                root_state, parent=self, load_meta_data=load_meta_data)
        else:
            self.root_state = StateModel(root_state,
                                         parent=self,
                                         load_meta_data=load_meta_data)

        if isinstance(meta, Vividict):
            self.meta = meta
        else:
            self.meta = Vividict()

        # ongoing_complex_actions is updated by ComplexActionObserver -> secure encapsulated observation
        # and made observable by state machine model here
        self.ongoing_complex_actions = {}
        self.complex_action_observer = ComplexActionObserver(self)

        self.meta_signal = Signal()
        self.state_meta_signal = Signal()
        self.action_signal = Signal()
        self.state_action_signal = Signal()
        self.sm_selection_changed_signal = Signal()
        self.destruction_signal = Signal()

        self.temp = Vividict()

        if load_meta_data:
            self.load_meta_data(recursively=False)

        self.selection = Selection(self.sm_selection_changed_signal)

        self.storage_lock = threading.Lock(
        )  # lock can not be substituted by the state machine lock -> maybe because it is a RLock

        self.history = None
        if global_gui_config.get_config_value('HISTORY_ENABLED'):
            from rafcon.gui.models.modification_history import ModificationsHistoryModel
            self.history = ModificationsHistoryModel(self)
        else:
            logger.info("The modification history is disabled")

        self.auto_backup = None
        if global_gui_config.get_config_value('AUTO_BACKUP_ENABLED'):
            from rafcon.gui.models.auto_backup import AutoBackupModel
            self.auto_backup = AutoBackupModel(self)

        self.root_state.register_observer(self)
        self.register_observer(self)
Ejemplo n.º 2
0
def test_focus():
    testing_utils.dummy_gui(None)
    from rafcon.gui.models.selection import Selection

    selection = Selection()
    focus_signal_observer = SignalCounter(selection, "focus_signal")
    selection_signal_observer = SignalCounter(selection, "selection_changed_signal")
    states_m, outcomes_e_m, outcomes_h_m = get_models()
    root_state_m, execution_state_m, hierarchy_state_m, child_state_m = states_m

    assert len(selection) == 0

    # Set focus
    selection.focus = execution_state_m
    assert selection.focus is execution_state_m
    assert len(selection) == 1
    assert len(selection.states) == 1
    assert focus_signal_observer.count == 1
    assert selection_signal_observer.count == 1

    # Set focus to same element
    selection.focus = execution_state_m
    assert selection.focus is execution_state_m
    assert len(selection) == 1
    assert len(selection.states) == 1
    assert focus_signal_observer.count == 2
    assert selection_signal_observer.count == 1

    # Clear selection => causes focus to me removed
    selection.clear()
    assert selection.focus is None
    assert len(selection) == 0
    assert len(selection.states) == 0
    assert focus_signal_observer.count == 3
    assert selection_signal_observer.count == 2
Ejemplo n.º 3
0
def test_adding_same_model_twice():
    testing_utils.dummy_gui(None)
    from rafcon.gui.models.selection import Selection, reduce_to_parent_states

    selection = Selection()
    states_m, outcomes_e_m, outcomes_h_m = get_models()
    root_state_m, execution_state_m, hierarchy_state_m, child_state_m = states_m
    duplicate_states = [root_state_m, execution_state_m, execution_state_m]

    selection.add(duplicate_states)
    assert len(selection) == 1

    selection.clear()

    selection.set(duplicate_states)
    assert len(selection) == 1

    reduced_states = reduce_to_parent_states(duplicate_states)
    assert len(reduced_states) == 1

    assert len(duplicate_states) == 3
Ejemplo n.º 4
0
def test_add_set_remove_clear():
    testing_utils.dummy_gui(None)
    from rafcon.gui.models.selection import Selection
    selection = Selection()
    signal_observer = SignalCounter(selection, "selection_changed_signal")
    states_m, outcomes_e_m, outcomes_h_m = get_models()
    root_state_m, execution_state_m, hierarchy_state_m, child_state_m = states_m

    assert len(selection) == 0

    # Add 1st state
    selection.add(execution_state_m)
    assert len(selection) == 1
    assert len(selection.states) == 1
    assert selection.get_selected_state() is execution_state_m
    assert signal_observer.count == 1

    # Add 2nd state
    selection.add(hierarchy_state_m)
    assert len(selection) == 2
    assert len(selection.states) == 2
    assert signal_observer.count == 2

    # Add 1st state again => should not affect selection
    selection.add(execution_state_m)
    assert len(selection) == 2
    assert len(selection.states) == 2
    assert signal_observer.count == 2

    # Remove 1st state
    selection.remove(execution_state_m)
    assert len(selection) == 1
    assert len(selection.states) == 1
    assert selection.get_selected_state() is hierarchy_state_m
    assert signal_observer.count == 3

    # Clear selection
    selection.clear()
    assert len(selection) == 0
    assert len(selection.states) == 0
    assert selection.get_selected_state() is None
    assert signal_observer.count == 4

    # Clear selection again => should cause no effect
    selection.clear()
    assert len(selection) == 0
    assert len(selection.states) == 0
    assert selection.get_selected_state() is None
    assert signal_observer.count == 4

    # Set selection to two outcomes
    selection.set(outcomes_e_m)
    assert len(selection) == 2
    assert len(selection.states) == 0
    assert len(selection.outcomes) == 2
    assert selection.get_selected_state() is None
    assert signal_observer.count == 5

    # Set selection to two outcomes again => should cause no effect
    selection.set(outcomes_e_m)
    assert len(selection) == 2
    assert len(selection.outcomes) == 2
    assert signal_observer.count == 5
Ejemplo n.º 5
0
def test_selection_reduction():
    testing_utils.dummy_gui(None)
    from rafcon.gui.models.selection import Selection
    selection = Selection()
    states_m, outcomes_e_m, outcomes_h_m = get_models()
    root_state_m, execution_state_m, hierarchy_state_m, child_state_m = states_m

    # Select outcomes
    selection.set(outcomes_e_m)
    assert len(selection) == 2
    assert len(selection.outcomes) == 2

    # Select parent state of outcomes
    selection.add(execution_state_m)
    assert len(selection) == 1
    assert len(selection.states) == 1
    assert len(selection.outcomes) == 0
    assert selection.get_selected_state() is execution_state_m

    # select root state
    selection.add(root_state_m)
    assert len(selection) == 1
    assert len(selection.states) == 1
    assert len(selection.outcomes) == 0
    assert selection.get_selected_state() is root_state_m

    # Select child state of root state
    selection.add(hierarchy_state_m)
    assert len(selection) == 1
    assert len(selection.states) == 1
    assert len(selection.outcomes) == 0
    assert selection.get_selected_state() is root_state_m

    # Select outcomes of child state
    selection.add(outcomes_h_m)
    assert len(selection) == 1
    assert len(selection.states) == 1
    assert len(selection.outcomes) == 0
    assert selection.get_selected_state() is root_state_m
Ejemplo n.º 6
0
def test_invalid_model():
    testing_utils.dummy_gui(None)
    from rafcon.core.states.state import State
    from rafcon.gui.models.selection import Selection
    from rafcon.gui.models.meta import MetaModel

    selection = Selection()
    meta_m = MetaModel()

    with pytest.raises(TypeError):
        selection.add(meta_m)
    with pytest.raises(TypeError):
        selection.set(meta_m)
    with pytest.raises(TypeError):
        selection.focus(meta_m)
    with pytest.raises(TypeError):
        selection.handle_new_selection(meta_m)
    with pytest.raises(TypeError):
        selection.handle_prepared_selection_of_core_class_elements(
            meta_m, State)
Ejemplo n.º 7
0
def test_all_models():
    testing_utils.dummy_gui(None)
    from rafcon.core.states.state import State
    from rafcon.core.state_elements.data_port import InputDataPort, OutputDataPort
    from rafcon.core.state_elements.scope import ScopedVariable
    from rafcon.core.state_elements.logical_port import Outcome
    from rafcon.core.state_elements.transition import Transition
    from rafcon.core.state_elements.data_flow import DataFlow

    from rafcon.gui.models.selection import Selection
    from rafcon.gui.models.data_port import DataPortModel
    from rafcon.gui.models.scoped_variable import ScopedVariableModel
    from rafcon.gui.models.transition import TransitionModel
    from rafcon.gui.models.data_flow import DataFlowModel

    selection = Selection()
    states_m, outcomes_e_m, outcomes_h_m = get_models()

    input_data_port = InputDataPort("i")
    input_data_port_m = DataPortModel(input_data_port, parent=None)
    output_data_port = OutputDataPort("o")
    output_data_port_m = DataPortModel(output_data_port, parent=None)
    scoped_variable = ScopedVariable("sv")
    scoped_variable_m = ScopedVariableModel(scoped_variable, parent=None)

    transition = Transition("0", 0, "1", 0, 0)
    transition_m = TransitionModel(transition, parent=None)
    data_flow = DataFlow("0", 0, "1", 0, 0)
    data_flow_m = DataFlowModel(data_flow, parent=None)

    selection.add(states_m[3])  # child_state_m
    selection.add(outcomes_e_m)
    selection.add(outcomes_h_m)
    selection.add((input_data_port_m, output_data_port_m, scoped_variable_m))
    selection.add(transition_m)
    selection.add(data_flow_m)

    assert len(selection) == 10
    assert len(selection.states) == 1
    assert len(selection.outcomes) == 4
    assert len(selection.input_data_ports) == 1
    assert len(selection.output_data_ports) == 1
    assert len(selection.scoped_variables) == 1
    assert len(selection.data_flows) == 1
    assert len(selection.transitions) == 1
    assert selection.states == selection.get_selected_elements_of_core_class(
        State)
    assert selection.outcomes == selection.get_selected_elements_of_core_class(
        Outcome)
    assert selection.input_data_ports == selection.get_selected_elements_of_core_class(
        InputDataPort)
    assert selection.output_data_ports == selection.get_selected_elements_of_core_class(
        OutputDataPort)
    assert selection.scoped_variables == selection.get_selected_elements_of_core_class(
        ScopedVariable)
    assert selection.data_flows == selection.get_selected_elements_of_core_class(
        DataFlow)
    assert selection.transitions == selection.get_selected_elements_of_core_class(
        Transition)

    selection.clear()

    assert 0 == len(selection) == len(selection.states) == len(selection.outcomes) == len(selection.input_data_ports)\
             == len(selection.output_data_ports) == len(selection.scoped_variables) == len(selection.data_flows)\
             == len(selection.transitions)