sentence1 stringlengths 52 3.87M | sentence2 stringlengths 1 47.2k | label stringclasses 1 value |
|---|---|---|
def run_to_selected_state(self, path, state_machine_id=None):
"""Execute the state machine until a specific state. This state won't be executed. This is an asynchronous task
"""
if self.state_machine_manager.get_active_state_machine() is not None:
self.state_machine_manager.get_active_state_machine().root_state.recursively_resume_states()
if not self.finished_or_stopped():
logger.debug("Resume execution engine and run to selected state!")
self.run_to_states = []
self.run_to_states.append(path)
self.set_execution_mode(StateMachineExecutionStatus.RUN_TO_SELECTED_STATE)
else:
logger.debug("Start execution engine and run to selected state!")
if state_machine_id is not None:
self.state_machine_manager.active_state_machine_id = state_machine_id
self.set_execution_mode(StateMachineExecutionStatus.RUN_TO_SELECTED_STATE)
self.run_to_states = []
self.run_to_states.append(path)
self._run_active_state_machine() | Execute the state machine until a specific state. This state won't be executed. This is an asynchronous task | entailment |
def _wait_while_in_pause_or_in_step_mode(self):
""" Waits as long as the execution_mode is in paused or step_mode
"""
while (self._status.execution_mode is StateMachineExecutionStatus.PAUSED) \
or (self._status.execution_mode is StateMachineExecutionStatus.STEP_MODE):
try:
self._status.execution_condition_variable.acquire()
self.synchronization_counter += 1
logger.verbose("Increase synchronization_counter: " + str(self.synchronization_counter))
self._status.execution_condition_variable.wait()
finally:
self._status.execution_condition_variable.release() | Waits as long as the execution_mode is in paused or step_mode | entailment |
def _wait_if_required(self, container_state, next_child_state_to_execute, woke_up_from_pause_or_step_mode):
""" Calls a blocking wait for the calling thread, depending on the execution mode.
:param container_state: the current hierarhcy state to handle the execution mode for
:param next_child_state_to_execute: the next child state for :param container_state to be executed
:param woke_up_from_pause_or_step_mode: a flag to check if the execution just woke up from paused- or step-mode
"""
wait = True
# if there is a state in self.run_to_states then RAFCON was commanded
# a) a step_over
# b) a step_out
# c) a run_until
for state_path in copy.deepcopy(self.run_to_states):
next_child_state_path = None
# can be None in case of no transition given
if next_child_state_to_execute:
next_child_state_path = next_child_state_to_execute.get_path()
if state_path == container_state.get_path():
# the execution did a whole step_over inside hierarchy state "state" (case a) )
# or a whole step_out into the hierarchy state "state" (case b) )
# thus we delete its state path from self.run_to_states
# and wait for another step (of maybe different kind)
wait = True
self.run_to_states.remove(state_path)
break
elif state_path == next_child_state_path:
# this is the case that execution has reached a specific state explicitly marked via
# run_to_selected_state() (case c) )
# if this is the case run_to_selected_state() is finished and the execution
# has to wait for new execution commands
wait = True
self.run_to_states.remove(state_path)
break
# don't wait if its just a normal step
else:
wait = False
# do not break here, the state_path may be of another state machine branch
# break
# don't wait if the the execution just woke up from step mode or pause
if wait and not woke_up_from_pause_or_step_mode:
logger.debug("Stepping mode: waiting for next step!")
try:
self._status.execution_condition_variable.acquire()
self.synchronization_counter += 1
logger.verbose("Increase synchronization_counter: " + str(self.synchronization_counter))
self._status.execution_condition_variable.wait()
finally:
self._status.execution_condition_variable.release()
# if the status was set to PAUSED or STEP_MODE don't wake up!
self._wait_while_in_pause_or_in_step_mode()
# container_state was notified => thus, a new user command was issued, which has to be handled!
container_state.execution_history.new_execution_command_handled = False | Calls a blocking wait for the calling thread, depending on the execution mode.
:param container_state: the current hierarhcy state to handle the execution mode for
:param next_child_state_to_execute: the next child state for :param container_state to be executed
:param woke_up_from_pause_or_step_mode: a flag to check if the execution just woke up from paused- or step-mode | entailment |
def handle_execution_mode(self, container_state, next_child_state_to_execute=None):
"""Checks the current execution status and returns it.
Depending on the execution state, the calling thread (currently only hierarchy states) waits for the
execution to continue.
If the execution mode is any of the step modes, a condition variable stops the current execution,
until it gets notified by the step_*() or backward_step() functions.
:param container_state: the container_state, for which the execution mode is handled
:param next_child_state_to_execute: is the next child state of :param state to be executed
:return: the current state machine execution status
"""
self.state_counter_lock.acquire()
self.state_counter += 1
# logger.verbose("Increase state_counter!" + str(self.state_counter))
self.state_counter_lock.release()
woke_up_from_pause_or_step_mode = False
if (self._status.execution_mode is StateMachineExecutionStatus.PAUSED) \
or (self._status.execution_mode is StateMachineExecutionStatus.STEP_MODE):
self._wait_while_in_pause_or_in_step_mode()
# new command was triggered => execution command has to handled
container_state.execution_history.new_execution_command_handled = False
woke_up_from_pause_or_step_mode = True
# no elif here: if the execution woke up from e.g. paused mode, it has to check the current execution mode
if self._status.execution_mode is StateMachineExecutionStatus.STARTED:
# logger.debug("Execution engine started!")
pass
elif self._status.execution_mode is StateMachineExecutionStatus.STOPPED:
logger.debug("Execution engine stopped. State '{0}' is going to quit in the case of "
"no preemption handling has to be done!".format(container_state.name))
elif self._status.execution_mode is StateMachineExecutionStatus.FINISHED:
# this must never happen during execution of the execution engine
raise Exception
else: # all other step modes
logger.verbose("before wait")
self._wait_if_required(container_state, next_child_state_to_execute, woke_up_from_pause_or_step_mode)
logger.verbose("after wait")
# calculate states to which should be run
if self._status.execution_mode is StateMachineExecutionStatus.BACKWARD:
pass
elif self._status.execution_mode is StateMachineExecutionStatus.FORWARD_INTO:
pass
elif self._status.execution_mode is StateMachineExecutionStatus.FORWARD_OVER:
if not container_state.execution_history.new_execution_command_handled:
# the state that called this method is a hierarchy state => thus we save this state and wait until
# thise very state will execute its next state; only then we will wait on the condition variable
self.run_to_states.append(container_state.get_path())
else:
pass
elif self._status.execution_mode is StateMachineExecutionStatus.FORWARD_OUT:
from rafcon.core.states.state import State
if isinstance(container_state.parent, State):
if not container_state.execution_history.new_execution_command_handled:
from rafcon.core.states.library_state import LibraryState
if isinstance(container_state.parent, LibraryState):
parent_path = container_state.parent.parent.get_path()
else:
parent_path = container_state.parent.get_path()
self.run_to_states.append(parent_path)
else:
pass
else:
# if step_out is called from the highest level just run the state machine to the end
self.run_to_states = []
self.set_execution_mode(StateMachineExecutionStatus.STARTED)
elif self._status.execution_mode is StateMachineExecutionStatus.RUN_TO_SELECTED_STATE:
# "run_to_states" were already updated thus doing nothing
pass
container_state.execution_history.new_execution_command_handled = True
# in the case that the stop method wakes up the paused or step mode a StateMachineExecutionStatus.STOPPED
# will be returned
return_value = self._status.execution_mode
return return_value | Checks the current execution status and returns it.
Depending on the execution state, the calling thread (currently only hierarchy states) waits for the
execution to continue.
If the execution mode is any of the step modes, a condition variable stops the current execution,
until it gets notified by the step_*() or backward_step() functions.
:param container_state: the container_state, for which the execution mode is handled
:param next_child_state_to_execute: is the next child state of :param state to be executed
:return: the current state machine execution status | entailment |
def _modify_run_to_states(self, state):
"""
This is a special case. Inside a hierarchy state a step_over is triggered and affects the last child.
In this case the self.run_to_states has to be modified in order to contain the parent of the hierarchy state.
Otherwise the execution won't respect the step_over any more and run until the end of the state machine.
The same holds for a step_out.
The reason for this is, that handle_execution_mode() can not be called between
the last state of a hierarchy state and the termination of the hierarchy state itself.
"""
if self._status.execution_mode is StateMachineExecutionStatus.FORWARD_OVER or \
self._status.execution_mode is StateMachineExecutionStatus.FORWARD_OUT:
for state_path in copy.deepcopy(self.run_to_states):
if state_path == state.get_path():
logger.verbose("Modifying run_to_states; triggered by state %s!", state.name)
self.run_to_states.remove(state_path)
from rafcon.core.states.state import State
if isinstance(state.parent, State):
from rafcon.core.states.library_state import LibraryState
if isinstance(state.parent, LibraryState):
parent_path = state.parent.parent.get_path()
else:
parent_path = state.parent.get_path()
self.run_to_states.append(parent_path)
break | This is a special case. Inside a hierarchy state a step_over is triggered and affects the last child.
In this case the self.run_to_states has to be modified in order to contain the parent of the hierarchy state.
Otherwise the execution won't respect the step_over any more and run until the end of the state machine.
The same holds for a step_out.
The reason for this is, that handle_execution_mode() can not be called between
the last state of a hierarchy state and the termination of the hierarchy state itself. | entailment |
def execute_state_machine_from_path(self, state_machine=None, path=None, start_state_path=None, wait_for_execution_finished=True):
""" A helper function to start an arbitrary state machine at a given path.
:param path: The path where the state machine resides
:param start_state_path: The path to the state from which the execution will start
:return: a reference to the created state machine
"""
import rafcon.core.singleton
from rafcon.core.storage import storage
rafcon.core.singleton.library_manager.initialize()
if not state_machine:
state_machine = storage.load_state_machine_from_path(path)
rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine)
rafcon.core.singleton.state_machine_execution_engine.start(
state_machine.state_machine_id, start_state_path=start_state_path)
if wait_for_execution_finished:
self.join()
self.stop()
return state_machine | A helper function to start an arbitrary state machine at a given path.
:param path: The path where the state machine resides
:param start_state_path: The path to the state from which the execution will start
:return: a reference to the created state machine | entailment |
def set_execution_mode(self, execution_mode, notify=True):
""" An observed setter for the execution mode of the state machine status. This is necessary for the
monitoring client to update the local state machine in the same way as the root state machine of the server.
:param execution_mode: the new execution mode of the state machine
:raises exceptions.TypeError: if the execution mode is of the wrong type
"""
if not isinstance(execution_mode, StateMachineExecutionStatus):
raise TypeError("status must be of type StateMachineExecutionStatus")
self._status.execution_mode = execution_mode
if notify:
self._status.execution_condition_variable.acquire()
self._status.execution_condition_variable.notify_all()
self._status.execution_condition_variable.release() | An observed setter for the execution mode of the state machine status. This is necessary for the
monitoring client to update the local state machine in the same way as the root state machine of the server.
:param execution_mode: the new execution mode of the state machine
:raises exceptions.TypeError: if the execution mode is of the wrong type | entailment |
def run_to_states(self):
"""Property for the _run_to_states field
"""
self.execution_engine_lock.acquire()
return_value = self._run_to_states
self.execution_engine_lock.release()
return return_value | Property for the _run_to_states field | entailment |
def recursively_preempt_states(self):
""" Preempt the state and all of it child states.
"""
super(ContainerState, self).recursively_preempt_states()
# notify the transition condition variable to let the state instantaneously stop
self._transitions_cv.acquire()
self._transitions_cv.notify_all()
self._transitions_cv.release()
for state in self.states.values():
state.recursively_preempt_states() | Preempt the state and all of it child states. | entailment |
def recursively_pause_states(self):
""" Pause the state and all of it child states.
"""
super(ContainerState, self).recursively_pause_states()
for state in self.states.values():
state.recursively_pause_states() | Pause the state and all of it child states. | entailment |
def recursively_resume_states(self):
""" Resume the state and all of it child states.
"""
super(ContainerState, self).recursively_resume_states()
for state in self.states.values():
state.recursively_resume_states() | Resume the state and all of it child states. | entailment |
def setup_run(self):
""" Executes a generic set of actions that has to be called in the run methods of each derived state class.
:return:
"""
super(ContainerState, self).setup_run()
# reset the scoped data
self._scoped_data = {}
self._start_state_modified = False
self.add_default_values_of_scoped_variables_to_scoped_data()
self.add_input_data_to_scoped_data(self.input_data) | Executes a generic set of actions that has to be called in the run methods of each derived state class.
:return: | entailment |
def handle_no_transition(self, state):
""" This function handles the case that there is no transition for a specific outcome of a sub-state.
The method waits on a condition variable to a new transition that will be connected by the programmer or
GUI-user.
:param state: The sub-state to find a transition for
:return: The transition for the target state.
:raises exceptions.RuntimeError: if the execution engine is stopped
(this will be caught at the end of the run method)
"""
transition = None
while not transition:
# (child) state preempted or aborted
if self.preempted or state.final_outcome.outcome_id in [-2, -1]:
if self.concurrency_queue:
self.concurrency_queue.put(self.state_id)
self.state_execution_status = StateExecutionStatus.WAIT_FOR_NEXT_STATE
if state.final_outcome.outcome_id == -1:
self.final_outcome = Outcome(-1, "aborted")
else:
self.final_outcome = Outcome(-2, "preempted")
logger.debug("{0} of {1} not connected, using default transition to parental {2}".format(
state.final_outcome, state, self.final_outcome))
return None
# depending on the execution mode pause execution
execution_signal = state_machine_execution_engine.handle_execution_mode(self)
if execution_signal is StateMachineExecutionStatus.STOPPED:
# this will be caught at the end of the run method
self.last_child.state_execution_status = StateExecutionStatus.INACTIVE
logger.warning("State machine was stopped, while state {} waited for the next transition.".format(
state.name
))
return None
# wait until the user connects the outcome of the state with a transition
logger.warning("Waiting for new transition at {1} of {0} ".format(state, state.final_outcome))
self._transitions_cv.acquire()
self._transitions_cv.wait(3.0)
self._transitions_cv.release()
transition = self.get_transition_for_outcome(state, state.final_outcome)
return transition | This function handles the case that there is no transition for a specific outcome of a sub-state.
The method waits on a condition variable to a new transition that will be connected by the programmer or
GUI-user.
:param state: The sub-state to find a transition for
:return: The transition for the target state.
:raises exceptions.RuntimeError: if the execution engine is stopped
(this will be caught at the end of the run method) | entailment |
def handle_no_start_state(self):
"""Handles the situation, when no start state exists during execution
The method waits, until a transition is created. It then checks again for an existing start state and waits
again, if this is not the case. It returns the None state if the the state machine was stopped.
"""
start_state = self.get_start_state(set_final_outcome=True)
while not start_state:
# depending on the execution mode pause execution
execution_signal = state_machine_execution_engine.handle_execution_mode(self)
if execution_signal is StateMachineExecutionStatus.STOPPED:
# this will be caught at the end of the run method
return None
self._transitions_cv.acquire()
self._transitions_cv.wait(3.0)
self._transitions_cv.release()
start_state = self.get_start_state(set_final_outcome=True)
return start_state | Handles the situation, when no start state exists during execution
The method waits, until a transition is created. It then checks again for an existing start state and waits
again, if this is not the case. It returns the None state if the the state machine was stopped. | entailment |
def group_states(self, state_ids, scoped_variable_ids=None):
""" Group states and scoped variables into a new hierarchy state and remain internal connections.
Interconnecting transitions and data flows to parent and other child states are removed, at the moment.
:param state_ids: state_id's of all states that are to be grouped.
:param scoped_variable_ids: data_port_id's of all scoped variables that are to be grouped, too.
:return:
"""
# TODO remember changed state or state element ids and provide them for the model functionalities
assert all([state_id in self.states.keys() for state_id in state_ids])
if scoped_variable_ids is None:
scoped_variable_ids = []
assert all([p_id in self.scoped_variables.keys() for p_id in scoped_variable_ids])
from rafcon.core.states.barrier_concurrency_state import DeciderState
if any(isinstance(self.states[child_state_id], DeciderState) for child_state_id in state_ids):
raise ValueError("State of type DeciderState can not be grouped.")
def create_name(name_str, used_names):
number_str = ""
number_of_str = 0
while name_str + number_str in used_names:
number_str = "_{}".format(number_of_str)
number_of_str += 1
return name_str + number_str
[related_transitions, related_data_flows] = self.related_linkage_states_and_scoped_variables(state_ids,
scoped_variable_ids)
def assign_ingoing_outgoing(df, going_data_linkage_for_port, ingoing=True):
internal = 'internal' if ingoing else 'external'
external = 'external' if ingoing else 'internal'
if (df.to_state, df.to_key) in going_data_linkage_for_port['to']:
going_data_linkage_for_port['to'][(df.to_state, df.to_key)][external].append(df)
else:
going_data_linkage_for_port['to'][(df.to_state, df.to_key)] = {external: [df], internal: [df]}
if (df.from_state, df.from_key) in going_data_linkage_for_port['from']:
going_data_linkage_for_port['from'][(df.from_state, df.from_key)][internal].append(df)
else:
going_data_linkage_for_port['from'][(df.from_state, df.from_key)] = {external: [df], internal: [df]}
def print_df_from_and_to(going_data_linkage_for_port):
logger.verbose('data linkage FROM: ')
for port, port_dfs in going_data_linkage_for_port['from'].items():
logger.verbose("\tport: {0} {1}".format(port, '' if 'args' not in port_dfs else port_dfs['args']))
logger.verbose("\t\texternal: \n\t\t\t" + "\n\t\t\t".join([str(df) for df in port_dfs['external']]))
logger.verbose("\t\tinternal: \n\t\t\t" + "\n\t\t\t".join([str(df) for df in port_dfs['internal']]))
logger.verbose('data linkage TO: ')
for port, port_dfs in going_data_linkage_for_port['to'].items():
logger.verbose("\tport: {0} {1}".format(port, '' if 'args' not in port_dfs else port_dfs['args']))
logger.verbose("\t\texternal: \n\t\t\t" + "\n\t\t\t".join([str(df) for df in port_dfs['external']]))
logger.verbose("\t\tinternal: \n\t\t\t" + "\n\t\t\t".join([str(df) for df in port_dfs['internal']]))
def reduce_dfs(port_data_linkages, df_id):
for port_key in list(port_data_linkages.keys()):
port_df = port_data_linkages[port_key]
port_df['internal'] = [df for df in port_df['internal'] if df.data_flow_id != df_id]
port_df['external'] = [df for df in port_df['external'] if df.data_flow_id != df_id]
if not port_df['internal'] and not port_df['external']:
del port_data_linkages[port_key]
def do_prior_in_out_going(going_data_linkage_for_port, prior_port_key, prior_locate_key):
# logger.info("PRIOR IN OUT {0}, {1}".format(prior_port_key, prior_locate_key))
minor_port_key = 'from' if prior_port_key == 'to' else 'to'
minor_locate_key = 'external' if prior_locate_key == 'internal' else 'internal'
for port_key in list(going_data_linkage_for_port[prior_port_key].keys()):
port_dfs = going_data_linkage_for_port[prior_port_key][port_key]
# print(prior_port_key, ": check: ", port_key, " length: ", len(port_dfs[prior_locate_key]))
if len(port_dfs[prior_locate_key]) > 1:
for df in port_dfs[prior_locate_key]:
# print("remove: ", df.data_flow_id, prior_locate_key, minor_port_key)
reduce_dfs(going_data_linkage_for_port[minor_port_key], df.data_flow_id)
for port_key in list(going_data_linkage_for_port[minor_port_key].keys()):
port_dfs = going_data_linkage_for_port[minor_port_key][port_key]
# print(minor_port_key, ": check: ", port_key, " length: ", len(port_dfs[minor_locate_key]))
if len(port_dfs[minor_locate_key]) > 1:
for df in port_dfs[minor_locate_key]:
# print("remove: ", df.data_flow_id, minor_locate_key, prior_port_key)
reduce_dfs(going_data_linkage_for_port[prior_port_key], df.data_flow_id)
for port_key in list(going_data_linkage_for_port[prior_port_key].keys()):
port_dfs = going_data_linkage_for_port[prior_port_key][port_key]
# print(prior_port_key, ": check: ", port_key, " length: ", len(port_dfs[prior_locate_key]))
if len(port_dfs[prior_locate_key]) == 1:
for df in port_dfs[prior_locate_key]:
# print("remove: ", df.data_flow_id, prior_locate_key, minor_port_key)
reduce_dfs(going_data_linkage_for_port[minor_port_key], df.data_flow_id)
def create_data_port_args(going_data_linkage_for_port):
names = []
for goal, data_port_linkage in going_data_linkage_for_port['to'].items():
state = self.states[goal[0]] if goal[0] != self.state_id else self
port = state.get_data_port_by_id(goal[1])
data_port_linkage['args'] = port.state_element_to_dict(port)
data_port_linkage['args']['name'] = create_name(data_port_linkage['args']['name'], names)
names.append(data_port_linkage['args']['name'])
for goal, data_port_linkage in going_data_linkage_for_port['from'].items():
state = self.states[goal[0]] if goal[0] != self.state_id else self
port = state.get_data_port_by_id(goal[1])
data_port_linkage['args'] = port.state_element_to_dict(port)
data_port_linkage['args']['name'] = create_name(data_port_linkage['args']['name'], names)
names.append(data_port_linkage['args']['name'])
################## IDENTIFY/PRE-PROCESS INGOING DATA FLOWS ###################
# ingoing data linkage to rebuild -> overview of all with duplicates
ingoing_data_linkage_for_port = {'from': {}, 'to': {}}
for df in related_data_flows['ingoing']:
assign_ingoing_outgoing(df, ingoing_data_linkage_for_port)
# logger.info("GROUP DATA INGOING BEFORE")
# print_df_from_and_to(ingoing_data_linkage_for_port)
# prior to-linkage-merge for ingoing over from-linkage-merge -> less internal ingoing data flows
do_prior_in_out_going(ingoing_data_linkage_for_port, prior_port_key='to', prior_locate_key='external')
# logger.info("GROUPED INGOING DATA AFTER")
# print_df_from_and_to(ingoing_data_linkage_for_port)
# get name and args for ports
create_data_port_args(ingoing_data_linkage_for_port)
# logger.info("GROUPED INGOING DATA PORTS")
# print_df_from_and_to(ingoing_data_linkage_for_port)
################## IDENTIFY/PRE-PROCESS OUTGOING DATA FLOWS ###################
# outgoing data linkage to rebuild -> overview of all with duplicates
outgoing_data_linkage_for_port = {'from': {}, 'to': {}}
for df in related_data_flows['outgoing']:
assign_ingoing_outgoing(df, outgoing_data_linkage_for_port, ingoing=False)
# logger.info("GROUP DATA OUTGOING BEFORE")
# print_df_from_and_to(outgoing_data_linkage_for_port)
# prior from-linkage-merge for outgoing over to-linkage-merge -> less outgoing data flows
do_prior_in_out_going(outgoing_data_linkage_for_port, prior_port_key='from', prior_locate_key='external')
# logger.info("GROUPED OUTGOING DATA AFTER")
# print_df_from_and_to(outgoing_data_linkage_for_port)
# get name and args for ports
create_data_port_args(outgoing_data_linkage_for_port)
# logger.info("GROUPED OUTGOING DATA PORTS")
# print_df_from_and_to(outgoing_data_linkage_for_port)
############################# CREATE NEW STATE #############################
# all internal transitions
transitions_internal = {t.transition_id: self.remove_transition(t.transition_id, destroy=False)
for t in related_transitions['enclosed']}
# all internal data flows
data_flows_internal = {df.data_flow_id: self.remove_data_flow(df.data_flow_id, destroy=False)
for df in related_data_flows['enclosed']}
# TODO add warning if a data-flow is connected to scoped variables which has ingoing and outgoing data flows
# TODO linked with selected states -> group would change behavior and scoped would need to be selected or
# TODO local scoped variable need to be introduced in new hierarchy state
# all internal scoped variables
scoped_variables_to_group = {dp_id: self.remove_scoped_variable(dp_id, destroy=False)
for dp_id in scoped_variable_ids}
# all states
states_to_group = {state_id: self.remove_state(state_id, recursive=False, destroy=False)
for state_id in state_ids}
from rafcon.core.states.hierarchy_state import HierarchyState
# secure state id conflicts for the taken transitions
from rafcon.core.id_generator import state_id_generator
state_id = state_id_generator(used_state_ids=state_ids + [self.state_id])
# if scoped variables are used all data flows have to be checked if those link to those and correct the state_id
if scoped_variable_ids:
for data_flow in data_flows_internal.values():
if data_flow.from_state == self.state_id:
data_flow.from_state = state_id
if data_flow.to_state == self.state_id:
data_flow.to_state = state_id
s = HierarchyState(states=states_to_group, transitions=transitions_internal, data_flows=data_flows_internal,
scoped_variables=scoped_variables_to_group, state_id=state_id)
state_id = self.add_state(s)
def find_logical_destinations_of_transitions(transitions):
destinations = {}
for t in transitions:
if (t.to_state, t.to_outcome) in destinations:
destinations[(t.to_state, t.to_outcome)].append(t)
else:
destinations[(t.to_state, t.to_outcome)] = [t]
return destinations
################## IDENTIFY TRANSITIONS #####################
# transition from ingoing transition
ingoing_logical_destinations = find_logical_destinations_of_transitions(related_transitions['ingoing'])
if len(ingoing_logical_destinations) > 1:
logger.warning("There is only one ingoing transition on a state possible. \n"
"The following transitions are removed by 'group_states': \n{}"
"".format('\n'.join([str(destination) for destination in ingoing_logical_destinations.items()[1:]])))
ingoing_transitions = None
if len(ingoing_logical_destinations) > 0:
ingoing_transitions = list(ingoing_logical_destinations.items())[0][1]
# transitions from outgoing transitions
transitions_outgoing = {t.transition_id: t for t in related_transitions['outgoing']}
outgoing_logical_destinations = find_logical_destinations_of_transitions(related_transitions['outgoing'])
################## DO INGOING TRANSITIONS ###################
if ingoing_transitions:
t = ingoing_transitions[0]
s.add_transition(None, None, t.to_state, t.to_outcome)
for t in ingoing_transitions:
self.add_transition(t.from_state, t.from_outcome, s.state_id, None)
################## DO OUTGOING OUTCOMES ###################
# outcomes from outgoing transitions
outcomes_outgoing_transitions = {}
new_outcome_ids = {}
state_outcomes_by_name = {oc.name: oc_id for oc_id, oc in s.outcomes.items()}
for goal, transitions in list(outgoing_logical_destinations.items()):
t = transitions[0]
name = s.states[t.from_state].outcomes[t.from_outcome].name
# print((t.to_state, t.to_outcome))
# print(outcomes_outgoing_transitions)
if goal in outcomes_outgoing_transitions:
# logger.info("old outcome {}".format((t.to_state, t.to_outcome)))
name = outcomes_outgoing_transitions[goal]
else:
name = create_name(name, list(new_outcome_ids.keys()))
outcomes_outgoing_transitions[goal] = name
# print(outcomes_outgoing_transitions, "\n", new_outcome_ids)
if name not in new_outcome_ids:
if name in state_outcomes_by_name:
new_outcome_ids[name] = state_outcomes_by_name[name]
# logger.info("old outcome_id {0}\n{1}".format(state_outcomes_by_name[name], new_outcome_ids))
else:
new_outcome_ids[name] = s.add_outcome(name=name)
# logger.info("new outcome_id {0}\n{1}".format(new_outcome_ids[name], new_outcome_ids))
# else:
# logger.info("name {0} in {1} -> {2}".format(name, new_outcome_ids, outcomes_outgoing_transitions))
################## DO OUTGOING TRANSITIONS ###################
# external outgoing transitions
# print("external transitions to create", outcomes_outgoing_transitions)
for goal, name in outcomes_outgoing_transitions.items():
try:
# avoid to use a outcome twice
if any([t for t in s.parent.transitions.values()
if t.from_state == s.state_id and t.from_outcome == new_outcome_ids[name]]):
continue
# add the transition for the outcome
self.add_transition(s.state_id, new_outcome_ids[name], goal[0], goal[1])
except ValueError:
logger.exception("Error while recreation of logical linkage.")
# internal outgoing transitions
# print("internal transitions to create", transitions_outgoing)
for t_id, t in transitions_outgoing.items():
name = outcomes_outgoing_transitions[(t.to_state, t.to_outcome)]
s.add_transition(t.from_state, t.from_outcome, s.state_id, new_outcome_ids[name], t_id)
new_state_ids = {self.state_id: s.state_id}
############## REBUILD INGOING DATA LINKAGE ################
full_linkage = copy(ingoing_data_linkage_for_port['from'])
full_linkage.update(ingoing_data_linkage_for_port['to'])
for port, data_port_linkage in full_linkage.items():
# input data ports from ingoing data flows
args = data_port_linkage['args']
args['data_port_id'] = None
args['data_port_id'] = s.add_input_data_port(**args)
# internal data flows from ingoing data flows
# print("ingoing internal data flows")
for df in data_port_linkage['internal']:
# print(df)
s.add_data_flow(from_state_id=s.state_id, from_data_port_id=args['data_port_id'],
to_state_id=new_state_ids.get(df.to_state, df.to_state),
to_data_port_id=df.to_key, data_flow_id=df.data_flow_id)
# external data flows from ingoing data flows
# print("ingoing external data flows")
for df in data_port_linkage['external']:
# print(df)
self.add_data_flow(from_state_id=df.from_state,
from_data_port_id=df.from_key, to_state_id=s.state_id,
to_data_port_id=args['data_port_id'], data_flow_id=df.data_flow_id)
############## REBUILD OUTGOING DATA LINKAGE ################
full_linkage = copy(outgoing_data_linkage_for_port['from'])
full_linkage.update(outgoing_data_linkage_for_port['to'])
for port, data_port_linkage in full_linkage.items():
# output data ports from outgoing data flows
args = data_port_linkage['args']
args['data_port_id'] = None
args['data_port_id'] = s.add_output_data_port(**args)
# internal data flows from outgoing data flows
# print("outgoing internal data flows")
for df in data_port_linkage['internal']:
# print(df)
s.add_data_flow(from_state_id=new_state_ids.get(df.from_state, df.from_state),
from_data_port_id=df.from_key, to_state_id=s.state_id,
to_data_port_id=args['data_port_id'], data_flow_id=df.data_flow_id)
# external data flows from outgoing data flows
# print("outgoing external data flows")
for df in data_port_linkage['external']:
# print(df)
self.add_data_flow(from_state_id=s.state_id, from_data_port_id=args['data_port_id'],
to_state_id=df.to_state, to_data_port_id=df.to_key, data_flow_id=df.data_flow_id)
return self.states[state_id] | Group states and scoped variables into a new hierarchy state and remain internal connections.
Interconnecting transitions and data flows to parent and other child states are removed, at the moment.
:param state_ids: state_id's of all states that are to be grouped.
:param scoped_variable_ids: data_port_id's of all scoped variables that are to be grouped, too.
:return: | entailment |
def ungroup_state(self, state_id):
""" Ungroup state with state id state_id into its parent and remain internal linkage in parent.
Interconnecting transitions and data flows to parent and other child states are preserved except:
- a transition that is going from income to outcome directly and
- a data-flow that is linking input and output directly.
:param state_id: State that is to be ungrouped.
:return:
"""
state = self.states[state_id]
assert isinstance(state, ContainerState)
from rafcon.core.states.barrier_concurrency_state import BarrierConcurrencyState, UNIQUE_DECIDER_STATE_ID
if isinstance(state, BarrierConcurrencyState):
state.remove_state(state_id=UNIQUE_DECIDER_STATE_ID, force=True)
[related_transitions, related_data_flows] = self.related_linkage_state(state_id)
# ingoing logical linkage to rebuild -> related_transitions['external']['ingoing']
# outgoing logical linkage to rebuild -> related_transitions['external']['outgoing']
# ingoing data linkage to rebuild
ingoing_data_linkage_for_port = {}
for df in related_data_flows['internal']['ingoing']:
if (df.from_state, df.from_key) in ingoing_data_linkage_for_port:
ingoing_data_linkage_for_port[(df.from_state, df.from_key)]['internal'].append(df)
else:
ingoing_data_linkage_for_port[(df.from_state, df.from_key)] = {'external': [], 'internal': [df]}
if not ingoing_data_linkage_for_port[(df.from_state, df.from_key)]['external']:
for ext_df in self.data_flows.values():
if (ext_df.to_state, ext_df.to_key) == (df.from_state, df.from_key):
ingoing_data_linkage_for_port[(df.from_state, df.from_key)]['external'].append(ext_df)
# outgoing data linkage to rebuild
outgoing_data_linkage_for_port = {}
for df in related_data_flows['internal']['outgoing']:
if (df.to_state, df.to_key) in outgoing_data_linkage_for_port:
outgoing_data_linkage_for_port[(df.to_state, df.to_key)]['internal'].append(df)
else:
outgoing_data_linkage_for_port[(df.to_state, df.to_key)] = {'external': [], 'internal': [df]}
if not outgoing_data_linkage_for_port[(df.to_state, df.to_key)]['external']:
for ext_df in self.data_flows.values():
if (ext_df.from_state, ext_df.from_key) == (df.to_state, df.to_key):
outgoing_data_linkage_for_port[(df.to_state, df.to_key)]['external'].append(ext_df)
# hold states and scoped variables to rebuild
child_states = [state.remove_state(s_id, recursive=False, destroy=False) for s_id in list(state.states.keys())]
child_scoped_variables = [sv for sv_id, sv in list(state.scoped_variables.items())]
# remove state that should be ungrouped
old_state = self.remove_state(state_id, recursive=False, destroy=False)
# fill elements into parent state and remember id mapping from child to parent state to map other properties
state_id_dict = {}
sv_id_dict = {}
enclosed_df_id_dict = {}
enclosed_t_id_dict = {}
# re-create states
old_state_ids = [state.state_id for state in child_states]
for child_state in child_states:
old_state_id = child_state.state_id
# needed to change state id here because not handled in add state and to avoid old state ids
new_id = None
if child_state.state_id in list(self.states.keys()):
new_id = state_id_generator(used_state_ids=list(self.states.keys()) + old_state_ids + [self.state_id])
child_state.change_state_id(new_id)
new_state_id = self.add_state(child_state)
if new_id is not None and not new_id == new_state_id:
logger.error("In ungroup state the changed state id should not be changed again by add_state because it"
" could become a old_state_id again and screw data flows and transitions.")
# remember new and old state id relations
state_id_dict[old_state_id] = new_state_id
# re-create scoped variables
for sv in child_scoped_variables:
name = sv.name
if name in [parent_sv.name for parent_sv in self.scoped_variables.values()]:
name = state_id + name
new_sv_id = self.add_scoped_variable(name, sv.data_type, sv.default_value)
sv_id_dict[sv.data_port_id] = new_sv_id
# re-create transitions
for t in related_transitions['internal']['enclosed']:
new_t_id = self.add_transition(state_id_dict[t.from_state], t.from_outcome,
state_id_dict[t.to_state], t.to_outcome)
enclosed_t_id_dict[t.transition_id] = new_t_id
assert len(related_transitions['internal']['ingoing']) <= 1
if related_transitions['internal']['ingoing']:
ingoing_t = related_transitions['internal']['ingoing'][0]
for t in related_transitions['external']['ingoing']:
self.add_transition(t.from_state, t.from_outcome, state_id_dict[ingoing_t.to_state],
ingoing_t.to_outcome)
for ext_t in related_transitions['external']['outgoing']:
for t in related_transitions['internal']['outgoing']:
if (t.to_state, t.to_outcome) == (ext_t.from_state, ext_t.from_outcome):
try:
self.add_transition(state_id_dict[t.from_state], t.from_outcome,
ext_t.to_state, ext_t.to_outcome)
except ValueError:
from rafcon.core.states.barrier_concurrency_state import BarrierConcurrencyState
if not isinstance(self, BarrierConcurrencyState):
logger.exception("Error while recreation of logical linkage.")
# re-create data flow linkage
for df in related_data_flows['internal']['enclosed']:
# print("enclosed: ", df)
new_df_id = self.add_data_flow(self.state_id if state_id == df.from_state else state_id_dict[df.from_state],
sv_id_dict[df.from_key] if state_id == df.from_state else df.from_key,
self.state_id if state_id == df.to_state else state_id_dict[df.to_state],
sv_id_dict[df.to_key] if state_id == df.to_state else df.to_key)
enclosed_df_id_dict[df.data_flow_id] = new_df_id
for data_port_linkage in ingoing_data_linkage_for_port.values():
for ext_df in data_port_linkage['external']:
for df in data_port_linkage['internal']:
# print("ingoing: ", ext_df, df)
if df.to_state not in state_id_dict and df.to_state == state_id:
self.add_data_flow(ext_df.from_state, ext_df.from_key, self.state_id, sv_id_dict[df.to_key])
else:
self.add_data_flow(ext_df.from_state, ext_df.from_key, state_id_dict[df.to_state], df.to_key)
for data_port_linkage in outgoing_data_linkage_for_port.values():
for ext_df in data_port_linkage['external']:
for df in data_port_linkage['internal']:
# print("outgoing: ", ext_df, df)
if df.from_state not in state_id_dict and df.from_state == state_id:
self.add_data_flow(self.state_id, sv_id_dict[df.from_key], ext_df.to_state, ext_df.to_key)
else:
self.add_data_flow(state_id_dict[df.from_state], df.from_key, ext_df.to_state, ext_df.to_key)
self.ungroup_state.__func__.state_id_dict = state_id_dict
self.ungroup_state.__func__.sv_id_dict = sv_id_dict
self.ungroup_state.__func__.enclosed_df_id_dict = enclosed_df_id_dict
self.ungroup_state.__func__.enclosed_t_id_dict = enclosed_t_id_dict
old_state.destroy(recursive=True)
return old_state | Ungroup state with state id state_id into its parent and remain internal linkage in parent.
Interconnecting transitions and data flows to parent and other child states are preserved except:
- a transition that is going from income to outcome directly and
- a data-flow that is linking input and output directly.
:param state_id: State that is to be ungrouped.
:return: | entailment |
def add_state(self, state, storage_load=False):
"""Adds a state to the container state.
:param state: the state that is going to be added
:param storage_load: True if the state was directly loaded from filesystem
:return: the state_id of the new state
:raises exceptions.AttributeError: if state.state_id already exist
"""
assert isinstance(state, State)
# logger.info("add state {}".format(state))
# handle the case that the child state id is the same as the container state id or future sibling state id
while state.state_id == self.state_id or state.state_id in self.states:
state.change_state_id()
# TODO: add validity checks for states and then remove this check => to discuss
if state.state_id in self._states.keys():
raise AttributeError("State id %s already exists in the container state", state.state_id)
else:
state.parent = self
self._states[state.state_id] = state
return state.state_id | Adds a state to the container state.
:param state: the state that is going to be added
:param storage_load: True if the state was directly loaded from filesystem
:return: the state_id of the new state
:raises exceptions.AttributeError: if state.state_id already exist | entailment |
def remove_state(self, state_id, recursive=True, force=False, destroy=True):
"""Remove a state from the container state.
:param state_id: the id of the state to remove
:param recursive: a flag to indicate a recursive disassembling of all substates
:param force: a flag to indicate forcefully deletion of all states (important for the decider state in the
barrier concurrency state)
:param destroy: a flag which indicates if the state should not only be disconnected from the state but also
destroyed, including all its state elements
:raises exceptions.AttributeError: if state.state_id does not
"""
if state_id not in self.states:
raise AttributeError("State_id %s does not exist" % state_id)
if state_id == self.start_state_id:
self.set_start_state(None)
# first delete all transitions and data_flows, which are connected to the state to be deleted
keys_to_delete = []
for key, transition in self.transitions.items():
if transition.from_state == state_id or transition.to_state == state_id:
keys_to_delete.append(key)
for key in keys_to_delete:
self.remove_transition(key, True)
keys_to_delete = []
for key, data_flow in self.data_flows.items():
if data_flow.from_state == state_id or data_flow.to_state == state_id:
keys_to_delete.append(key)
for key in keys_to_delete:
self.remove_data_flow(key)
if recursive and not destroy:
raise AttributeError("The recursive flag requires the destroy flag to be set, too.")
if destroy:
# Recursively delete all transitions, data flows and states within the state to be deleted
self.states[state_id].destroy(recursive)
# final delete the state it self
self.states[state_id].parent = None
return self.states.pop(state_id) | Remove a state from the container state.
:param state_id: the id of the state to remove
:param recursive: a flag to indicate a recursive disassembling of all substates
:param force: a flag to indicate forcefully deletion of all states (important for the decider state in the
barrier concurrency state)
:param destroy: a flag which indicates if the state should not only be disconnected from the state but also
destroyed, including all its state elements
:raises exceptions.AttributeError: if state.state_id does not | entailment |
def destroy(self, recursive):
""" Removes all the state elements.
:param recursive: Flag whether to destroy all state elements which are removed
"""
for transition_id in list(self.transitions.keys()):
self.remove_transition(transition_id, destroy=recursive)
for data_flow_id in list(self.data_flows.keys()):
self.remove_data_flow(data_flow_id, destroy=recursive)
for scoped_variable_id in list(self.scoped_variables.keys()):
self.remove_scoped_variable(scoped_variable_id, destroy=recursive)
for state_id in list(self.states.keys()):
if recursive:
self.remove_state(state_id, recursive, force=True, destroy=recursive)
else:
del self.states[state_id]
super(ContainerState, self).destroy(recursive) | Removes all the state elements.
:param recursive: Flag whether to destroy all state elements which are removed | entailment |
def related_linkage_state(self, state_id):
""" TODO: document
"""
related_transitions = {'external': {'ingoing': [], 'outgoing': []},
'internal': {'enclosed': [], 'ingoing': [], 'outgoing': []}}
related_data_flows = {'external': {'ingoing': [], 'outgoing': []},
'internal': {'enclosed': [], 'ingoing': [], 'outgoing': []}}
# ingoing logical linkage to rebuild
related_transitions['external']['ingoing'] = [t for t in self.transitions.values() if t.to_state == state_id]
# outgoing logical linkage to rebuild
related_transitions['external']['outgoing'] = [t for t in self.transitions.values() if t.from_state == state_id]
# ingoing data linkage to rebuild
related_data_flows['external']['ingoing'] = [df for df in self.data_flows.values() if df.to_state == state_id]
# outgoing outgoing linkage to rebuild
related_data_flows['external']['outgoing'] = [df for df in self.data_flows.values() if df.from_state == state_id]
state = self.states[state_id]
if not isinstance(state, ContainerState):
return related_transitions, related_data_flows
for t_id, t in state.transitions.items():
# check if internal of new hierarchy state
if t.from_state in state.states and t.to_state in state.states:
related_transitions['internal']['enclosed'].append(t)
elif t.to_state in state.states:
related_transitions['internal']['ingoing'].append(t)
elif t.from_state in state.states:
related_transitions['internal']['outgoing'].append(t)
else:
raise AttributeError("All transition have to be ingoing, outgoing or internal.")
for df_id, df in state.data_flows.items():
# check if internal of hierarchy state
if df.from_state in state.states and df.to_state in state.states or \
df.from_state in state.states and state.state_id == df.to_state and df.to_key in state.scoped_variables or \
state.state_id == df.from_state and df.from_key in state.scoped_variables and df.to_state in state.states:
related_data_flows['internal']['enclosed'].append(df)
elif df.to_state in state.states or \
state.state_id == df.to_state and df.to_key in state.scoped_variables or \
df.to_state == df.from_state and df.from_key in state.input_data_ports:
related_data_flows['internal']['ingoing'].append(df)
elif df.from_state in state.states or \
state.state_id == df.from_state and df.from_key in state.scoped_variables or \
df.to_state == df.from_state and df.to_key in state.output_data_ports:
related_data_flows['internal']['outgoing'].append(df)
else:
raise AttributeError("All data flow have to be ingoing, outgoing or internal.")
return related_transitions, related_data_flows | TODO: document | entailment |
def related_linkage_states_and_scoped_variables(self, state_ids, scoped_variables):
""" TODO: document
"""
# find all related transitions
related_transitions = {'enclosed': [], 'ingoing': [], 'outgoing': []}
for t in self.transitions.values():
# check if internal of new hierarchy state
if t.from_state in state_ids and t.to_state in state_ids:
related_transitions['enclosed'].append(t)
elif t.to_state in state_ids:
related_transitions['ingoing'].append(t)
elif t.from_state in state_ids:
related_transitions['outgoing'].append(t)
# find all related data flows
related_data_flows = {'enclosed': [], 'ingoing': [], 'outgoing': []}
for df in self.data_flows.values():
# check if internal of new hierarchy state
if df.from_state in state_ids and df.to_state in state_ids or \
df.from_state in state_ids and self.state_id == df.to_state and df.to_key in scoped_variables or \
self.state_id == df.from_state and df.from_key in scoped_variables and df.to_state in state_ids:
related_data_flows['enclosed'].append(df)
elif df.to_state in state_ids or \
self.state_id == df.to_state and df.to_key in scoped_variables:
related_data_flows['ingoing'].append(df)
elif df.from_state in state_ids or \
self.state_id == df.from_state and df.from_key in scoped_variables:
related_data_flows['outgoing'].append(df)
return related_transitions, related_data_flows | TODO: document | entailment |
def change_state_type(self, state, new_state_class):
""" Changes the type of the state to another type
:param state: the state to be changed
:param new_state_class: the new type of the state
:return: the new state having the new state type
:rtype: :py:class:`rafcon.core.states.state.State`
:raises exceptions.ValueError: if the state does not exist in the container state
"""
from rafcon.gui.helpers.state import create_new_state_from_state_with_type
state_id = state.state_id
if state_id not in self.states:
raise ValueError("State '{0}' with id '{1}' does not exist".format(state.name, state_id))
new_state = create_new_state_from_state_with_type(state, new_state_class)
new_state.parent = self
assert new_state.state_id == state_id
self.states[state_id] = new_state
return new_state | Changes the type of the state to another type
:param state: the state to be changed
:param new_state_class: the new type of the state
:return: the new state having the new state type
:rtype: :py:class:`rafcon.core.states.state.State`
:raises exceptions.ValueError: if the state does not exist in the container state | entailment |
def set_start_state(self, state):
"""Sets the start state of a container state
:param state: The state_id of a state or a direct reference ot he state (that was already added
to the container) that will be the start state of this container state.
"""
if state is None:
self.start_state_id = None
elif isinstance(state, State):
self.start_state_id = state.state_id
else:
self.start_state_id = state | Sets the start state of a container state
:param state: The state_id of a state or a direct reference ot he state (that was already added
to the container) that will be the start state of this container state. | entailment |
def get_start_state(self, set_final_outcome=False):
"""Get the start state of the container state
:param set_final_outcome: if the final_outcome of the state should be set if the income directly connects to
an outcome
:return: the start state
"""
# overwrite the start state in the case that a specific start state is specific e.g. by start_from_state
if self.get_path() in state_machine_execution_engine.start_state_paths:
for state_id, state in self.states.items():
if state.get_path() in state_machine_execution_engine.start_state_paths:
state_machine_execution_engine.start_state_paths.remove(self.get_path())
self._start_state_modified = True
return state
if self.start_state_id is None:
return None
# It is possible to connect the income directly with an outcome
if self.start_state_id == self.state_id:
if set_final_outcome:
for transition_id in self.transitions:
# the transition of which the from state is None is the transition that directly connects the income
if self.transitions[transition_id].from_state is None:
to_outcome_id = self.transitions[transition_id].to_outcome
self.final_outcome = self.outcomes[to_outcome_id]
break
return self
return self.states[self.start_state_id] | Get the start state of the container state
:param set_final_outcome: if the final_outcome of the state should be set if the income directly connects to
an outcome
:return: the start state | entailment |
def remove(self, state_element, recursive=True, force=False, destroy=True):
"""Remove item from state
:param StateElement state_element: State or state element to be removed
:param bool recursive: Only applies to removal of state and decides whether the removal should be called
recursively on all child states
:param bool force: if the removal should be forced without checking constraints
:param bool destroy: a flag that signals that the state element will be fully removed and disassembled
"""
if isinstance(state_element, State):
return self.remove_state(state_element.state_id, recursive=recursive, force=force, destroy=destroy)
elif isinstance(state_element, Transition):
return self.remove_transition(state_element.transition_id, destroy=destroy)
elif isinstance(state_element, DataFlow):
return self.remove_data_flow(state_element.data_flow_id, destroy=destroy)
elif isinstance(state_element, ScopedVariable):
return self.remove_scoped_variable(state_element.data_port_id, destroy=destroy)
else:
super(ContainerState, self).remove(state_element, force=force, destroy=destroy) | Remove item from state
:param StateElement state_element: State or state element to be removed
:param bool recursive: Only applies to removal of state and decides whether the removal should be called
recursively on all child states
:param bool force: if the removal should be forced without checking constraints
:param bool destroy: a flag that signals that the state element will be fully removed and disassembled | entailment |
def check_transition_id(self, transition_id):
""" Check the transition id and calculate a new one if its None
:param transition_id: The transition-id to check
:return: The new transition id
:raises exceptions.AttributeError: if transition.transition_id already exists
"""
if transition_id is not None:
if transition_id in self._transitions.keys():
raise AttributeError("The transition id %s already exists. Cannot add transition!", transition_id)
else:
transition_id = generate_transition_id()
while transition_id in self._transitions.keys():
transition_id = generate_transition_id()
return transition_id | Check the transition id and calculate a new one if its None
:param transition_id: The transition-id to check
:return: The new transition id
:raises exceptions.AttributeError: if transition.transition_id already exists | entailment |
def check_if_outcome_already_connected(self, from_state_id, from_outcome):
""" check if outcome of from state is not already connected
:param from_state_id: The source state of the transition
:param from_outcome: The outcome of the source state to connect the transition to
:raises exceptions.AttributeError: if the outcome of the state with the state_id==from_state_id
is already connected
"""
for trans_key, transition in self.transitions.items():
if transition.from_state == from_state_id:
if transition.from_outcome == from_outcome:
raise AttributeError("Outcome %s of state %s is already connected" %
(str(from_outcome), str(from_state_id))) | check if outcome of from state is not already connected
:param from_state_id: The source state of the transition
:param from_outcome: The outcome of the source state to connect the transition to
:raises exceptions.AttributeError: if the outcome of the state with the state_id==from_state_id
is already connected | entailment |
def create_transition(self, from_state_id, from_outcome, to_state_id, to_outcome, transition_id):
""" Creates a new transition.
Lookout: Check the parameters first before creating a new transition
:param from_state_id: The source state of the transition
:param from_outcome: The outcome of the source state to connect the transition to
:param to_state_id: The target state of the transition
:param to_outcome: The target outcome of a container state
:param transition_id: An optional transition id for the new transition
:raises exceptions.AttributeError: if the from or to state is incorrect
:return: the id of the new transition
"""
# get correct states
if from_state_id is not None:
if from_state_id == self.state_id:
from_state = self
else:
from_state = self.states[from_state_id]
# finally add transition
if from_outcome is not None:
if from_outcome in from_state.outcomes:
if to_outcome is not None:
if to_outcome in self.outcomes: # if to_state is None then the to_outcome must be an outcome of self
self.transitions[transition_id] = \
Transition(from_state_id, from_outcome, to_state_id, to_outcome, transition_id, self)
else:
raise AttributeError("to_state does not have outcome %s", to_outcome)
else: # to outcome is None but to_state is not None, so the transition is valid
self.transitions[transition_id] = \
Transition(from_state_id, from_outcome, to_state_id, to_outcome, transition_id, self)
else:
raise AttributeError("from_state does not have outcome %s", from_state)
else:
self.transitions[transition_id] = \
Transition(None, None, to_state_id, to_outcome, transition_id, self)
# notify all states waiting for transition to be connected
self._transitions_cv.acquire()
self._transitions_cv.notify_all()
self._transitions_cv.release()
return transition_id | Creates a new transition.
Lookout: Check the parameters first before creating a new transition
:param from_state_id: The source state of the transition
:param from_outcome: The outcome of the source state to connect the transition to
:param to_state_id: The target state of the transition
:param to_outcome: The target outcome of a container state
:param transition_id: An optional transition id for the new transition
:raises exceptions.AttributeError: if the from or to state is incorrect
:return: the id of the new transition | entailment |
def add_transition(self, from_state_id, from_outcome, to_state_id, to_outcome, transition_id=None):
"""Adds a transition to the container state
Note: Either the toState or the toOutcome needs to be "None"
:param from_state_id: The source state of the transition
:param from_outcome: The outcome id of the source state to connect the transition to
:param to_state_id: The target state of the transition
:param to_outcome: The target outcome id of a container state
:param transition_id: An optional transition id for the new transition
"""
transition_id = self.check_transition_id(transition_id)
# Set from_state_id to None for start transitions, as from_state_id and from_outcome should both be None for
# these transitions
if from_state_id == self.state_id and from_outcome is None:
from_state_id = None
new_transition = Transition(from_state_id, from_outcome, to_state_id, to_outcome, transition_id, self)
self.transitions[transition_id] = new_transition
# notify all states waiting for transition to be connected
self._transitions_cv.acquire()
self._transitions_cv.notify_all()
self._transitions_cv.release()
# self.create_transition(from_state_id, from_outcome, to_state_id, to_outcome, transition_id)
return transition_id | Adds a transition to the container state
Note: Either the toState or the toOutcome needs to be "None"
:param from_state_id: The source state of the transition
:param from_outcome: The outcome id of the source state to connect the transition to
:param to_state_id: The target state of the transition
:param to_outcome: The target outcome id of a container state
:param transition_id: An optional transition id for the new transition | entailment |
def get_transition_for_outcome(self, state, outcome):
"""Determines the next transition of a state.
:param state: The state for which the transition is determined
:param outcome: The outcome of the state, that is given in the first parameter
:return: the transition specified by the the state and the outcome
:raises exceptions.TypeError: if the types of the passed parameters are incorrect
"""
if not isinstance(state, State):
raise TypeError("state must be of type State")
if not isinstance(outcome, Outcome):
raise TypeError("outcome must be of type Outcome")
result_transition = None
for key, transition in self.transitions.items():
if transition.from_state == state.state_id and transition.from_outcome == outcome.outcome_id:
result_transition = transition
return result_transition | Determines the next transition of a state.
:param state: The state for which the transition is determined
:param outcome: The outcome of the state, that is given in the first parameter
:return: the transition specified by the the state and the outcome
:raises exceptions.TypeError: if the types of the passed parameters are incorrect | entailment |
def remove_transition(self, transition_id, destroy=True):
"""Removes a transition from the container state
:param transition_id: the id of the transition to remove
:raises exceptions.AttributeError: if the transition_id is already used
"""
if transition_id == -1 or transition_id == -2:
raise AttributeError("The transition_id must not be -1 (Aborted) or -2 (Preempted)")
if transition_id not in self._transitions:
raise AttributeError("The transition_id %s does not exist" % str(transition_id))
self.transitions[transition_id].parent = None
return self.transitions.pop(transition_id) | Removes a transition from the container state
:param transition_id: the id of the transition to remove
:raises exceptions.AttributeError: if the transition_id is already used | entailment |
def remove_outcome_hook(self, outcome_id):
"""Removes internal transition going to the outcome
"""
for transition_id in list(self.transitions.keys()):
transition = self.transitions[transition_id]
if transition.to_outcome == outcome_id and transition.to_state == self.state_id:
self.remove_transition(transition_id) | Removes internal transition going to the outcome | entailment |
def check_data_flow_id(self, data_flow_id):
""" Check the data flow id and calculate a new one if its None
:param data_flow_id: The data flow id to check
:return: The new data flow id
:raises exceptions.AttributeError: if data_flow.data_flow_id already exists
"""
if data_flow_id is not None:
if data_flow_id in self._data_flows.keys():
raise AttributeError("The data_flow id %s already exists. Cannot add data_flow!", data_flow_id)
else:
data_flow_id = generate_data_flow_id()
while data_flow_id in self._data_flows.keys():
data_flow_id = generate_data_flow_id()
return data_flow_id | Check the data flow id and calculate a new one if its None
:param data_flow_id: The data flow id to check
:return: The new data flow id
:raises exceptions.AttributeError: if data_flow.data_flow_id already exists | entailment |
def add_data_flow(self, from_state_id, from_data_port_id, to_state_id, to_data_port_id, data_flow_id=None):
"""Adds a data_flow to the container state
:param from_state_id: The id source state of the data_flow
:param from_data_port_id: The output_key of the source state
:param to_state_id: The id target state of the data_flow
:param to_data_port_id: The input_key of the target state
:param data_flow_id: an optional id for the data flow
"""
data_flow_id = self.check_data_flow_id(data_flow_id)
self.data_flows[data_flow_id] = DataFlow(from_state_id, from_data_port_id, to_state_id, to_data_port_id,
data_flow_id, self)
return data_flow_id | Adds a data_flow to the container state
:param from_state_id: The id source state of the data_flow
:param from_data_port_id: The output_key of the source state
:param to_state_id: The id target state of the data_flow
:param to_data_port_id: The input_key of the target state
:param data_flow_id: an optional id for the data flow | entailment |
def remove_data_flow(self, data_flow_id, destroy=True):
""" Removes a data flow from the container state
:param int data_flow_id: the id of the data_flow to remove
:raises exceptions.AttributeError: if the data_flow_id does not exist
"""
if data_flow_id not in self._data_flows:
raise AttributeError("The data_flow_id %s does not exist" % str(data_flow_id))
self._data_flows[data_flow_id].parent = None
return self._data_flows.pop(data_flow_id) | Removes a data flow from the container state
:param int data_flow_id: the id of the data_flow to remove
:raises exceptions.AttributeError: if the data_flow_id does not exist | entailment |
def remove_data_flows_with_data_port_id(self, data_port_id):
"""Remove an data ports whose from_key or to_key equals the passed data_port_id
:param int data_port_id: the id of a data_port of which all data_flows should be removed, the id can be a input or
output data port id
"""
# delete all data flows in parent related to data_port_id and self.state_id = external data flows
# checking is_root_state_of_library is only necessary in case of scoped variables, as the scoped variables
# they are not destroyed by the library state, as the library state does not have a reference to the scoped vars
if not self.is_root_state and not self.is_root_state_of_library:
data_flow_ids_to_remove = []
for data_flow_id, data_flow in self.parent.data_flows.items():
if data_flow.from_state == self.state_id and data_flow.from_key == data_port_id or \
data_flow.to_state == self.state_id and data_flow.to_key == data_port_id:
data_flow_ids_to_remove.append(data_flow_id)
for data_flow_id in data_flow_ids_to_remove:
self.parent.remove_data_flow(data_flow_id)
# delete all data flows in self related to data_port_id and self.state_id = internal data flows
data_flow_ids_to_remove = []
for data_flow_id, data_flow in self.data_flows.items():
if data_flow.from_state == self.state_id and data_flow.from_key == data_port_id or \
data_flow.to_state == self.state_id and data_flow.to_key == data_port_id:
data_flow_ids_to_remove.append(data_flow_id)
for data_flow_id in data_flow_ids_to_remove:
self.remove_data_flow(data_flow_id) | Remove an data ports whose from_key or to_key equals the passed data_port_id
:param int data_port_id: the id of a data_port of which all data_flows should be removed, the id can be a input or
output data port id | entailment |
def get_scoped_variable_from_name(self, name):
""" Get the scoped variable for a unique name
:param name: the unique name of the scoped variable
:return: the scoped variable specified by the name
:raises exceptions.AttributeError: if the name is not in the the scoped_variables dictionary
"""
for scoped_variable_id, scoped_variable in self.scoped_variables.items():
if scoped_variable.name == name:
return scoped_variable_id
raise AttributeError("Name %s is not in scoped_variables dictionary", name) | Get the scoped variable for a unique name
:param name: the unique name of the scoped variable
:return: the scoped variable specified by the name
:raises exceptions.AttributeError: if the name is not in the the scoped_variables dictionary | entailment |
def add_scoped_variable(self, name, data_type=None, default_value=None, scoped_variable_id=None):
""" Adds a scoped variable to the container state
:param name: The name of the scoped variable
:param data_type: An optional data type of the scoped variable
:param default_value: An optional default value of the scoped variable
:param scoped_variable_id: An optional scoped variable id of the
:return: the unique id of the added scoped variable
:raises exceptions.ValueError: if the scoped variable is not valid
"""
if scoped_variable_id is None:
# All data port ids have to passed to the id generation as the data port id has to be unique inside a state
scoped_variable_id = generate_data_port_id(self.get_data_port_ids())
self._scoped_variables[scoped_variable_id] = ScopedVariable(name, data_type, default_value,
scoped_variable_id, self)
# Check for name uniqueness
valid, message = self._check_data_port_name(self._scoped_variables[scoped_variable_id])
if not valid:
self._scoped_variables[scoped_variable_id].parent = None
del self._scoped_variables[scoped_variable_id]
raise ValueError(message)
return scoped_variable_id | Adds a scoped variable to the container state
:param name: The name of the scoped variable
:param data_type: An optional data type of the scoped variable
:param default_value: An optional default value of the scoped variable
:param scoped_variable_id: An optional scoped variable id of the
:return: the unique id of the added scoped variable
:raises exceptions.ValueError: if the scoped variable is not valid | entailment |
def remove_scoped_variable(self, scoped_variable_id, destroy=True):
"""Remove a scoped variable from the container state
:param scoped_variable_id: the id of the scoped variable to remove
:raises exceptions.AttributeError: if the id of the scoped variable already exists
"""
if scoped_variable_id not in self._scoped_variables:
raise AttributeError("A scoped variable with id %s does not exist" % str(scoped_variable_id))
# delete all data flows connected to scoped_variable
if destroy:
self.remove_data_flows_with_data_port_id(scoped_variable_id)
# delete scoped variable
self._scoped_variables[scoped_variable_id].parent = None
return self._scoped_variables.pop(scoped_variable_id) | Remove a scoped variable from the container state
:param scoped_variable_id: the id of the scoped variable to remove
:raises exceptions.AttributeError: if the id of the scoped variable already exists | entailment |
def get_data_port(self, state_id, port_id):
"""Searches for a data port
The data port specified by the state id and data port id is searched in the state itself and in its children.
:param str state_id: The id of the state the port is in
:param int port_id: The id of the port
:return: The searched port or None if it is not found
"""
if state_id == self.state_id:
return self.get_data_port_by_id(port_id)
for child_state_id, child_state in self.states.items():
if state_id != child_state_id:
continue
port = child_state.get_data_port_by_id(port_id)
if port:
return port
return None | Searches for a data port
The data port specified by the state id and data port id is searched in the state itself and in its children.
:param str state_id: The id of the state the port is in
:param int port_id: The id of the port
:return: The searched port or None if it is not found | entailment |
def get_data_port_by_id(self, data_port_id):
"""Search for the given data port id in the data ports of the state
The method tries to find a data port in the input and output data ports as well as in the scoped variables.
:param data_port_id: the unique id of the data port
:return: the data port with the searched id or None if not found
"""
data_port = super(ContainerState, self).get_data_port_by_id(data_port_id)
if data_port:
return data_port
if data_port_id in self.scoped_variables:
return self.scoped_variables[data_port_id]
return None | Search for the given data port id in the data ports of the state
The method tries to find a data port in the input and output data ports as well as in the scoped variables.
:param data_port_id: the unique id of the data port
:return: the data port with the searched id or None if not found | entailment |
def get_inputs_for_state(self, state):
"""Retrieves all input data of a state. If several data flows are connected to an input port the
most current data is used for the specific input port.
:param state: the state of which the input data is determined
:return: the input data of the target state
"""
result_dict = {}
tmp_dict = self.get_default_input_values_for_state(state)
result_dict.update(tmp_dict)
for input_port_key, value in state.input_data_ports.items():
# for all input keys fetch the correct data_flow connection and read data into the result_dict
actual_value = None
actual_value_time = 0
for data_flow_key, data_flow in self.data_flows.items():
if data_flow.to_key == input_port_key:
if data_flow.to_state == state.state_id:
# fetch data from the scoped_data list: the key is the data_port_key + the state_id
key = str(data_flow.from_key) + data_flow.from_state
if key in self.scoped_data:
if actual_value is None or actual_value_time < self.scoped_data[key].timestamp:
actual_value = deepcopy(self.scoped_data[key].value)
actual_value_time = self.scoped_data[key].timestamp
if actual_value is not None:
result_dict[value.name] = actual_value
return result_dict | Retrieves all input data of a state. If several data flows are connected to an input port the
most current data is used for the specific input port.
:param state: the state of which the input data is determined
:return: the input data of the target state | entailment |
def add_input_data_to_scoped_data(self, dictionary):
"""Add a dictionary to the scoped data
As the input_data dictionary maps names to values, the functions looks for the proper data_ports keys in the
input_data_ports dictionary
:param dictionary: The dictionary that is added to the scoped data
:param state: The state to which the input_data was passed (should be self in most cases)
"""
for dict_key, value in dictionary.items():
for input_data_port_key, data_port in list(self.input_data_ports.items()):
if dict_key == data_port.name:
self.scoped_data[str(input_data_port_key) + self.state_id] = \
ScopedData(data_port.name, value, type(value), self.state_id, ScopedVariable, parent=self)
# forward the data to scoped variables
for data_flow_key, data_flow in self.data_flows.items():
if data_flow.from_key == input_data_port_key and data_flow.from_state == self.state_id:
if data_flow.to_state == self.state_id and data_flow.to_key in self.scoped_variables:
current_scoped_variable = self.scoped_variables[data_flow.to_key]
self.scoped_data[str(data_flow.to_key) + self.state_id] = \
ScopedData(current_scoped_variable.name, value, type(value), self.state_id,
ScopedVariable, parent=self) | Add a dictionary to the scoped data
As the input_data dictionary maps names to values, the functions looks for the proper data_ports keys in the
input_data_ports dictionary
:param dictionary: The dictionary that is added to the scoped data
:param state: The state to which the input_data was passed (should be self in most cases) | entailment |
def add_state_execution_output_to_scoped_data(self, dictionary, state):
"""Add a state execution output to the scoped data
:param dictionary: The dictionary that is added to the scoped data
:param state: The state that finished execution and provide the dictionary
"""
for output_name, value in dictionary.items():
for output_data_port_key, data_port in list(state.output_data_ports.items()):
if output_name == data_port.name:
if not isinstance(value, data_port.data_type):
if (not ((type(value) is float or type(value) is int) and
(data_port.data_type is float or data_port.data_type is int)) and
not (isinstance(value, type(None)))):
logger.error("The data type of output port {0} should be of type {1}, but is of type {2}".
format(output_name, data_port.data_type, type(value)))
self.scoped_data[str(output_data_port_key) + state.state_id] = \
ScopedData(data_port.name, value, type(value), state.state_id, OutputDataPort, parent=self) | Add a state execution output to the scoped data
:param dictionary: The dictionary that is added to the scoped data
:param state: The state that finished execution and provide the dictionary | entailment |
def add_default_values_of_scoped_variables_to_scoped_data(self):
"""Add the scoped variables default values to the scoped_data dictionary
"""
for key, scoped_var in self.scoped_variables.items():
self.scoped_data[str(scoped_var.data_port_id) + self.state_id] = \
ScopedData(scoped_var.name, scoped_var.default_value, scoped_var.data_type, self.state_id,
ScopedVariable, parent=self) | Add the scoped variables default values to the scoped_data dictionary | entailment |
def update_scoped_variables_with_output_dictionary(self, dictionary, state):
"""Update the values of the scoped variables with the output dictionary of a specific state.
:param: the dictionary to update the scoped variables with
:param: the state the output dictionary belongs to
"""
for key, value in dictionary.items():
output_data_port_key = None
# search for the correct output data port key of the source state
for o_key, o_port in state.output_data_ports.items():
if o_port.name == key:
output_data_port_key = o_key
break
if output_data_port_key is None:
if not key == "error":
logger.warning("Output variable %s was written during state execution, "
"that has no data port connected to it.", str(key))
for data_flow_key, data_flow in self.data_flows.items():
if data_flow.from_key == output_data_port_key and data_flow.from_state == state.state_id:
if data_flow.to_state == self.state_id: # is target of data flow own state id?
if data_flow.to_key in self.scoped_variables.keys(): # is target data port scoped?
current_scoped_variable = self.scoped_variables[data_flow.to_key]
self.scoped_data[str(data_flow.to_key) + self.state_id] = \
ScopedData(current_scoped_variable.name, value, type(value), state.state_id,
ScopedVariable, parent=self) | Update the values of the scoped variables with the output dictionary of a specific state.
:param: the dictionary to update the scoped variables with
:param: the state the output dictionary belongs to | entailment |
def change_state_id(self, state_id=None):
"""
Changes the id of the state to a new id. This functions replaces the old state_id with the new state_id in all
data flows and transitions.
:param state_id: The new state if of the state
"""
old_state_id = self.state_id
super(ContainerState, self).change_state_id(state_id)
# Use private variables to change ids to prevent validity checks
# change id in all transitions
for transition in self.transitions.values():
if transition.from_state == old_state_id:
transition._from_state = self.state_id
if transition.to_state == old_state_id:
transition._to_state = self.state_id
# change id in all data_flows
for data_flow in self.data_flows.values():
if data_flow.from_state == old_state_id:
data_flow._from_state = self.state_id
if data_flow.to_state == old_state_id:
data_flow._to_state = self.state_id | Changes the id of the state to a new id. This functions replaces the old state_id with the new state_id in all
data flows and transitions.
:param state_id: The new state if of the state | entailment |
def get_state_for_transition(self, transition):
"""Calculate the target state of a transition
:param transition: The transition of which the target state is determined
:return: the to-state of the transition
:raises exceptions.TypeError: if the transition parameter is of wrong type
"""
if not isinstance(transition, Transition):
raise TypeError("transition must be of type Transition")
# the to_state is None when the transition connects an outcome of a child state to the outcome of a parent state
if transition.to_state == self.state_id or transition.to_state is None:
return self
else:
return self.states[transition.to_state] | Calculate the target state of a transition
:param transition: The transition of which the target state is determined
:return: the to-state of the transition
:raises exceptions.TypeError: if the transition parameter is of wrong type | entailment |
def write_output_data(self, specific_output_dictionary=None):
""" Write the scoped data to output of the state. Called before exiting the container state.
:param specific_output_dictionary: an optional dictionary to write the output data in
:return:
"""
if isinstance(specific_output_dictionary, dict):
output_dict = specific_output_dictionary
else:
output_dict = self.output_data
for output_name, value in self.output_data.items():
output_port_id = self.get_io_data_port_id_from_name_and_type(output_name, OutputDataPort)
actual_value = None
actual_value_was_written = False
actual_value_time = 0
for data_flow_id, data_flow in self.data_flows.items():
if data_flow.to_state == self.state_id:
if data_flow.to_key == output_port_id:
scoped_data_key = str(data_flow.from_key) + data_flow.from_state
if scoped_data_key in self.scoped_data:
# if self.scoped_data[scoped_data_key].timestamp > actual_value_time is True
# the data of a previous execution of the same state is overwritten
if actual_value is None or self.scoped_data[scoped_data_key].timestamp > actual_value_time:
actual_value = deepcopy(self.scoped_data[scoped_data_key].value)
actual_value_time = self.scoped_data[scoped_data_key].timestamp
actual_value_was_written = True
else:
if not self.backward_execution:
logger.debug(
"Output data with name {0} of state {1} was not found in the scoped data "
"of state {2}. Thus the state did not write onto this output. "
"This can mean a state machine design error.".format(
str(output_name), str(self.states[data_flow.from_state].get_path()),
self.get_path()))
if actual_value_was_written:
output_dict[output_name] = actual_value | Write the scoped data to output of the state. Called before exiting the container state.
:param specific_output_dictionary: an optional dictionary to write the output data in
:return: | entailment |
def check_child_validity(self, child):
"""Check validity of passed child object
The method is called by state child objects (transitions, data flows) when these are initialized or changed. The
method checks the type of the child and then checks its validity in the context of the state.
:param object child: The child of the state that is to be tested
:return bool validity, str message: validity is True, when the child is valid, False else. message gives more
information especially if the child is not valid
"""
# First let the state do validity checks for outcomes and data ports
valid, message = super(ContainerState, self).check_child_validity(child)
if not valid and not message.startswith("Invalid state element"):
return False, message
# Continue with checks if previous ones did not fail
# Check type of child and call appropriate validity test
if isinstance(child, DataFlow):
return self._check_data_flow_validity(child)
if isinstance(child, Transition):
return self._check_transition_validity(child)
return valid, message | Check validity of passed child object
The method is called by state child objects (transitions, data flows) when these are initialized or changed. The
method checks the type of the child and then checks its validity in the context of the state.
:param object child: The child of the state that is to be tested
:return bool validity, str message: validity is True, when the child is valid, False else. message gives more
information especially if the child is not valid | entailment |
def check_data_port_connection(self, check_data_port):
"""Checks the connection validity of a data port
The method is called by a child state to check the validity of a data port in case it is connected with data
flows. The data port does not belong to 'self', but to one of self.states.
If the data port is connected to a data flow, the method checks, whether these connect consistent data types
of ports.
:param rafcon.core.data_port.DataPort check_data_port: The port to check
:return: valid, message
"""
for data_flow in self.data_flows.values():
# Check whether the data flow connects the given port
from_port = self.get_data_port(data_flow.from_state, data_flow.from_key)
to_port = self.get_data_port(data_flow.to_state, data_flow.to_key)
if check_data_port is from_port or check_data_port is to_port:
# check if one of the data_types if type 'object'; in this case the data flow is always valid
if not (from_port.data_type is object or to_port.data_type is object):
if not type_inherits_of_type(from_port.data_type, to_port.data_type):
return False, "Connection of two non-compatible data types"
return True, "valid" | Checks the connection validity of a data port
The method is called by a child state to check the validity of a data port in case it is connected with data
flows. The data port does not belong to 'self', but to one of self.states.
If the data port is connected to a data flow, the method checks, whether these connect consistent data types
of ports.
:param rafcon.core.data_port.DataPort check_data_port: The port to check
:return: valid, message | entailment |
def _check_data_port_id(self, data_port):
"""Checks the validity of a data port id
Checks whether the id of the given data port is already used by anther data port (input, output, scoped vars)
within the state.
:param rafcon.core.data_port.DataPort data_port: The data port to be checked
:return bool validity, str message: validity is True, when the data port is valid, False else. message gives
more information especially if the data port is not valid
"""
# First check inputs and outputs
valid, message = super(ContainerState, self)._check_data_port_id(data_port)
if not valid:
return False, message
# Container state also has scoped variables
for scoped_variable_id, scoped_variable in self.scoped_variables.items():
if data_port.data_port_id == scoped_variable_id and data_port is not scoped_variable:
return False, "data port id already existing in state"
return True, message | Checks the validity of a data port id
Checks whether the id of the given data port is already used by anther data port (input, output, scoped vars)
within the state.
:param rafcon.core.data_port.DataPort data_port: The data port to be checked
:return bool validity, str message: validity is True, when the data port is valid, False else. message gives
more information especially if the data port is not valid | entailment |
def _check_data_port_name(self, data_port):
"""Checks the validity of a data port name
Checks whether the name of the given data port is already used by anther data port within the state. Names
must be unique with input data ports, output data ports and scoped variables.
:param rafcon.core.data_port.DataPort data_port: The data port to be checked
:return bool validity, str message: validity is True, when the data port is valid, False else. message gives
more information especially if the data port is not valid
"""
# First check inputs and outputs
valid, message = super(ContainerState, self)._check_data_port_name(data_port)
if not valid:
return False, message
if data_port.data_port_id in self.scoped_variables:
for scoped_variable in self.scoped_variables.values():
if data_port.name == scoped_variable.name and data_port is not scoped_variable:
return False, "scoped variable name already existing in state's scoped variables"
return True, message | Checks the validity of a data port name
Checks whether the name of the given data port is already used by anther data port within the state. Names
must be unique with input data ports, output data ports and scoped variables.
:param rafcon.core.data_port.DataPort data_port: The data port to be checked
:return bool validity, str message: validity is True, when the data port is valid, False else. message gives
more information especially if the data port is not valid | entailment |
def _check_data_flow_validity(self, check_data_flow):
"""Checks the validity of a data flow
Calls further checks to inspect the id, ports and data types.
:param rafcon.core.data_flow.DataFlow check_data_flow: The data flow to be checked
:return bool validity, str message: validity is True, when the data flow is valid, False else. message gives
more information especially if the data flow is not valid
"""
valid, message = self._check_data_flow_id(check_data_flow)
if not valid:
return False, message
valid, message = self._check_data_flow_ports(check_data_flow)
if not valid:
return False, message
return self._check_data_flow_types(check_data_flow) | Checks the validity of a data flow
Calls further checks to inspect the id, ports and data types.
:param rafcon.core.data_flow.DataFlow check_data_flow: The data flow to be checked
:return bool validity, str message: validity is True, when the data flow is valid, False else. message gives
more information especially if the data flow is not valid | entailment |
def _check_data_flow_id(self, data_flow):
"""Checks the validity of a data flow id
Checks whether the id of the given data flow is already by anther data flow used within the state.
:param rafcon.core.data_flow.DataFlow data_flow: The data flow to be checked
:return bool validity, str message: validity is True, when the data flow is valid, False else. message gives
more information especially if the data flow is not valid
"""
data_flow_id = data_flow.data_flow_id
if data_flow_id in self.data_flows and data_flow is not self.data_flows[data_flow_id]:
return False, "data_flow_id already existing"
return True, "valid" | Checks the validity of a data flow id
Checks whether the id of the given data flow is already by anther data flow used within the state.
:param rafcon.core.data_flow.DataFlow data_flow: The data flow to be checked
:return bool validity, str message: validity is True, when the data flow is valid, False else. message gives
more information especially if the data flow is not valid | entailment |
def _check_data_flow_ports(self, data_flow):
"""Checks the validity of the ports of a data flow
Checks whether the ports of a data flow are existing and whether it is allowed to connect these ports.
:param rafcon.core.data_flow.DataFlow data_flow: The data flow to be checked
:return bool validity, str message: validity is True, when the data flow is valid, False else. message gives
more information especially if the data flow is not valid
"""
from_state_id = data_flow.from_state
to_state_id = data_flow.to_state
from_data_port_id = data_flow.from_key
to_data_port_id = data_flow.to_key
# Check whether to and from port are existing
from_data_port = self.get_data_port(from_state_id, from_data_port_id)
if not from_data_port:
return False, "Data flow origin not existing -> {0}".format(data_flow)
to_data_port = self.get_data_port(to_state_id, to_data_port_id)
if not to_data_port:
return False, "Data flow target not existing -> {0}".format(data_flow)
# Data_ports without parents are not allowed to be connected twice
if not from_data_port.parent:
return False, "Source data port does not have a parent -> {0}".format(data_flow)
if not to_data_port.parent:
return False, "Target data port does not have a parent -> {0}".format(data_flow)
# Check if data ports are identical
if from_data_port is to_data_port:
return False, "Source and target data ports of data flow must not be identical -> {}".format(data_flow)
# Check, whether the origin of the data flow is valid
if from_state_id == self.state_id: # data_flow originates in container state
if from_data_port_id not in self.input_data_ports and from_data_port_id not in self.scoped_variables:
return False, "Data flow origin port must be an input port or scoped variable, when the data flow " \
"starts in the parent state -> {0}".format(data_flow)
else: # data flow originates in child state
if from_data_port_id not in from_data_port.parent.output_data_ports:
return False, "Data flow origin port must be an output port, when the data flow " \
"starts in the child state -> {0}".format(data_flow)
# Check, whether the target of a data flow is valid
if to_state_id == self.state_id: # data_flow ends in container state
if to_data_port_id not in self.output_data_ports and to_data_port_id not in self.scoped_variables:
return False, "Data flow target port must be an output port or scoped variable, when the data flow " \
"goes to the parent state -> {0}".format(data_flow)
else: # data_flow ends in child state
if to_data_port_id not in to_data_port.parent.input_data_ports:
return False, "Data flow target port must be an input port, when the data flow goes to a child state" \
" -> {0}".format(data_flow)
# Check if data flow connects two scoped variables
if isinstance(from_data_port, ScopedVariable) and isinstance(to_data_port, ScopedVariable):
return False, "Data flows must not connect two scoped variables -> {}".format(data_flow)
# Check, whether the target port is already connected
for existing_data_flow in self.data_flows.values():
to_data_port_existing = self.get_data_port(existing_data_flow.to_state, existing_data_flow.to_key)
from_data_port_existing = self.get_data_port(existing_data_flow.from_state, existing_data_flow.from_key)
if to_data_port is to_data_port_existing and data_flow is not existing_data_flow:
if from_data_port is from_data_port_existing:
return False, "Exactly the same data flow is already existing -> {0}".format(data_flow)
return True, "valid" | Checks the validity of the ports of a data flow
Checks whether the ports of a data flow are existing and whether it is allowed to connect these ports.
:param rafcon.core.data_flow.DataFlow data_flow: The data flow to be checked
:return bool validity, str message: validity is True, when the data flow is valid, False else. message gives
more information especially if the data flow is not valid | entailment |
def _check_data_flow_types(self, check_data_flow):
"""Checks the validity of the data flow connection
Checks whether the ports of a data flow have matching data types.
:param rafcon.core.data_flow.DataFlow check_data_flow: The data flow to be checked
:return bool validity, str message: validity is True, when the data flow is valid, False else. message gives
more information especially if the data flow is not valid
"""
# Check whether the data types or origin and target fit
from_data_port = self.get_data_port(check_data_flow.from_state, check_data_flow.from_key)
to_data_port = self.get_data_port(check_data_flow.to_state, check_data_flow.to_key)
# Connections from the data port type "object" are always allowed
if from_data_port.data_type is object:
return True, "valid"
if not type_inherits_of_type(from_data_port.data_type, to_data_port.data_type):
return False, "Data flow (id: {0}) with origin state \"{1}\" (from data port name: {2}) " \
"and target state \"{3}\" (to data port name: {4}) " \
"do not have matching data types (from '{5}' to '{6}')".format(
check_data_flow.data_flow_id,
from_data_port.parent.name,
from_data_port.name,
to_data_port.parent.name,
to_data_port.name,
from_data_port.data_type,
to_data_port.data_type)
return True, "valid" | Checks the validity of the data flow connection
Checks whether the ports of a data flow have matching data types.
:param rafcon.core.data_flow.DataFlow check_data_flow: The data flow to be checked
:return bool validity, str message: validity is True, when the data flow is valid, False else. message gives
more information especially if the data flow is not valid | entailment |
def _check_transition_validity(self, check_transition):
"""Checks the validity of a transition
Calls further checks to inspect the id, origin, target and connection of the transition.
:param rafcon.core.transition.Transition check_transition: The transition to be checked
:return bool validity, str message: validity is True, when the transition is valid, False else. message gives
more information especially if the transition is not valid
"""
valid, message = self._check_transition_id(check_transition)
if not valid:
return False, message
# Separate check for start transitions
if check_transition.from_state is None:
return self._check_start_transition(check_transition)
valid, message = self._check_transition_origin(check_transition)
if not valid:
return False, message
valid, message = self._check_transition_target(check_transition)
if not valid:
return False, message
return self._check_transition_connection(check_transition) | Checks the validity of a transition
Calls further checks to inspect the id, origin, target and connection of the transition.
:param rafcon.core.transition.Transition check_transition: The transition to be checked
:return bool validity, str message: validity is True, when the transition is valid, False else. message gives
more information especially if the transition is not valid | entailment |
def _check_transition_id(self, transition):
"""Checks the validity of a transition id
Checks whether the transition id is already used by another transition within the state
:param rafcon.core.transition.Transition transition: The transition to be checked
:return bool validity, str message: validity is True, when the transition is valid, False else. message gives
more information especially if the transition is not valid
"""
transition_id = transition.transition_id
if transition_id in self.transitions and transition is not self.transitions[transition_id]:
return False, "transition_id already existing"
return True, "valid" | Checks the validity of a transition id
Checks whether the transition id is already used by another transition within the state
:param rafcon.core.transition.Transition transition: The transition to be checked
:return bool validity, str message: validity is True, when the transition is valid, False else. message gives
more information especially if the transition is not valid | entailment |
def _check_start_transition(self, start_transition):
"""Checks the validity of a start transition
Checks whether the given transition is a start transition a whether it is the only one within the state.
:param rafcon.core.transition.Transition start_transition: The transition to be checked
:return bool validity, str message: validity is True, when the transition is valid, False else. message gives
more information especially if the transition is not valid
"""
for transition in self.transitions.values():
if transition.from_state is None:
if start_transition is not transition:
return False, "Only one start transition is allowed"
if start_transition.from_outcome is not None:
return False, "from_outcome must not be set in start transition"
return self._check_transition_target(start_transition) | Checks the validity of a start transition
Checks whether the given transition is a start transition a whether it is the only one within the state.
:param rafcon.core.transition.Transition start_transition: The transition to be checked
:return bool validity, str message: validity is True, when the transition is valid, False else. message gives
more information especially if the transition is not valid | entailment |
def _check_transition_target(self, transition):
"""Checks the validity of a transition target
Checks whether the transition target is valid.
:param rafcon.core.transition.Transition transition: The transition to be checked
:return bool validity, str message: validity is True, when the transition is valid, False else. message gives
more information especially if the transition is not valid
"""
to_state_id = transition.to_state
to_outcome_id = transition.to_outcome
if to_state_id == self.state_id:
if to_outcome_id not in self.outcomes:
return False, "to_outcome is not existing"
else:
if to_state_id not in self.states:
return False, "to_state is not existing"
if to_outcome_id is not None:
return False, "to_outcome must be None as transition goes to child state"
return True, "valid" | Checks the validity of a transition target
Checks whether the transition target is valid.
:param rafcon.core.transition.Transition transition: The transition to be checked
:return bool validity, str message: validity is True, when the transition is valid, False else. message gives
more information especially if the transition is not valid | entailment |
def _check_transition_origin(self, transition):
"""Checks the validity of a transition origin
Checks whether the transition origin is valid.
:param rafcon.core.transition.Transition transition: The transition to be checked
:return bool validity, str message: validity is True, when the transition is valid, False else. message gives
more information especially if the transition is not valid
"""
from_state_id = transition.from_state
from_outcome_id = transition.from_outcome
if from_state_id == self.state_id:
return False, "from_state_id of transition must not be the container state itself." \
" In the case of a start transition both the from state and the from_outcome are None."
if from_state_id != self.state_id and from_state_id not in self.states:
return False, "from_state not existing"
from_outcome = self.get_outcome(from_state_id, from_outcome_id)
if from_outcome is None:
return False, "from_outcome not existing in from_state"
return True, "valid" | Checks the validity of a transition origin
Checks whether the transition origin is valid.
:param rafcon.core.transition.Transition transition: The transition to be checked
:return bool validity, str message: validity is True, when the transition is valid, False else. message gives
more information especially if the transition is not valid | entailment |
def _check_transition_connection(self, check_transition):
"""Checks the validity of a transition connection
Checks whether the transition is allowed to connect the origin with the target.
:param rafcon.core.transition.Transition check_transition: The transition to be checked
:return bool validity, str message: validity is True, when the transition is valid, False else. message gives
more information especially if the transition is not valid
"""
from_state_id = check_transition.from_state
from_outcome_id = check_transition.from_outcome
to_state_id = check_transition.to_state
to_outcome_id = check_transition.to_outcome
# check for connected origin
for transition in self.transitions.values():
if transition.from_state == from_state_id:
if transition.from_outcome == from_outcome_id:
if check_transition is not transition:
return False, "transition origin already connected to another transition"
if from_state_id in self.states and to_state_id in self.states and to_outcome_id is not None:
return False, "no transition from one outcome to another one on the same hierarchy allowed"
return True, "valid" | Checks the validity of a transition connection
Checks whether the transition is allowed to connect the origin with the target.
:param rafcon.core.transition.Transition check_transition: The transition to be checked
:return bool validity, str message: validity is True, when the transition is valid, False else. message gives
more information especially if the transition is not valid | entailment |
def get_states_statistics(self, hierarchy_level):
"""
Returns the numer of child states
:return:
"""
number_of_all_child_states = 0
max_child_hierarchy_level = 0
for s in self.states.values():
child_hierarchy_level = 0
number_of_child_states, child_hierarchy_level = s.get_states_statistics(child_hierarchy_level)
number_of_all_child_states += number_of_child_states
if child_hierarchy_level > max_child_hierarchy_level:
max_child_hierarchy_level = child_hierarchy_level
return number_of_all_child_states + 1, hierarchy_level + max_child_hierarchy_level + 1 | Returns the numer of child states
:return: | entailment |
def get_number_of_transitions(self):
"""
Returns the numer of child states
:return:
"""
number_of_all_transitions = 0
for s in self.states.values():
number_of_all_transitions += s.get_number_of_transitions()
return number_of_all_transitions + len(self.transitions) | Returns the numer of child states
:return: | entailment |
def states(self, states):
""" Setter for _states field
See property
:param states: Dictionary of States
:raises exceptions.TypeError: if the states parameter is of wrong type
:raises exceptions.AttributeError: if the keys of the dictionary and the state_ids in the dictionary do not match
"""
if not isinstance(states, dict):
raise TypeError("states must be of type dict")
if [state_id for state_id, state in states.items() if not isinstance(state, State)]:
raise TypeError("element of container_state.states must be of type State")
if [state_id for state_id, state in states.items() if not state_id == state.state_id]:
raise AttributeError("The key of the state dictionary and the id of the state do not match")
old_states = self._states
self._states = states
for state_id, state in states.items():
try:
state.parent = self
except ValueError:
self._states = old_states
raise
# check that all old_states are no more referencing self as there parent
for old_state in old_states.values():
if old_state not in self._states.values() and old_state.parent is self:
old_state.parent = None | Setter for _states field
See property
:param states: Dictionary of States
:raises exceptions.TypeError: if the states parameter is of wrong type
:raises exceptions.AttributeError: if the keys of the dictionary and the state_ids in the dictionary do not match | entailment |
def transitions(self, transitions):
""" Setter for _transitions field
See property
:param: transitions: Dictionary transitions[transition_id] of :class:`rafcon.core.transition.Transition`
:raises exceptions.TypeError: if the transitions parameter has the wrong type
:raises exceptions.AttributeError: if the keys of the transitions dictionary and the transition_ids of the
transitions in the dictionary do not match
"""
if not isinstance(transitions, dict):
raise TypeError("transitions must be of type dict")
if [t_id for t_id, transition in transitions.items() if not isinstance(transition, Transition)]:
raise TypeError("element of transitions must be of type Transition")
if [t_id for t_id, transition in transitions.items() if not t_id == transition.transition_id]:
raise AttributeError("The key of the transition dictionary and the id of the transition do not match")
old_transitions = self._transitions
self._transitions = transitions
transition_ids_to_delete = []
for transition_id, transition in transitions.items():
try:
transition.parent = self
except (ValueError, RecoveryModeException) as e:
if type(e) is RecoveryModeException:
logger.error("Recovery error: {0}\n{1}".format(str(e), str(traceback.format_exc())))
if e.do_delete_item:
transition_ids_to_delete.append(transition.transition_id)
else:
self._transitions = old_transitions
raise
self._transitions = dict((transition_id, t) for (transition_id, t) in self._transitions.items()
if transition_id not in transition_ids_to_delete)
# check that all old_transitions are no more referencing self as there parent
for old_transition in old_transitions.values():
if old_transition not in self._transitions.values() and old_transition.parent is self:
old_transition.parent = None | Setter for _transitions field
See property
:param: transitions: Dictionary transitions[transition_id] of :class:`rafcon.core.transition.Transition`
:raises exceptions.TypeError: if the transitions parameter has the wrong type
:raises exceptions.AttributeError: if the keys of the transitions dictionary and the transition_ids of the
transitions in the dictionary do not match | entailment |
def data_flows(self, data_flows):
""" Setter for _data_flows field
See property
:param dict data_flows: Dictionary data_flows[data_flow_id] of :class:`rafcon.core.data_flow.DataFlow`
:raises exceptions.TypeError: if the data_flows parameter has the wrong type
:raises exceptions.AttributeError: if the keys of the data_flows dictionary and the data_flow_ids of the
data flows in the dictionary do not match
"""
if not isinstance(data_flows, dict):
raise TypeError("data_flows must be of type dict")
if [df_id for df_id, data_flow in data_flows.items() if not isinstance(data_flow, DataFlow)]:
raise TypeError("element of data_flows must be of type DataFlow")
if [df_id for df_id, data_flow in data_flows.items() if not df_id == data_flow.data_flow_id]:
raise AttributeError("The key of the data flow dictionary and the id of the data flow do not match")
old_data_flows = self._data_flows
self._data_flows = data_flows
data_flow_ids_to_delete = []
for data_flow_id, data_flow in data_flows.items():
try:
data_flow.parent = self
except (ValueError, RecoveryModeException) as e:
if type(e) is RecoveryModeException:
logger.error("Recovery error: {0}\n{1}".format(str(e), str(traceback.format_exc())))
if e.do_delete_item:
data_flow_ids_to_delete.append(data_flow.data_flow_id)
else:
self._data_flows = old_data_flows
raise
self._data_flows = dict((data_flow_id, d) for (data_flow_id, d) in self._data_flows.items()
if data_flow_id not in data_flow_ids_to_delete)
# check that all old_data_flows are no more referencing self as there parent
for old_data_flow in old_data_flows.values():
if old_data_flow not in self._data_flows.values() and old_data_flow.parent is self:
old_data_flow.parent = None | Setter for _data_flows field
See property
:param dict data_flows: Dictionary data_flows[data_flow_id] of :class:`rafcon.core.data_flow.DataFlow`
:raises exceptions.TypeError: if the data_flows parameter has the wrong type
:raises exceptions.AttributeError: if the keys of the data_flows dictionary and the data_flow_ids of the
data flows in the dictionary do not match | entailment |
def start_state_id(self):
""" The start state is the state to which the first transition goes to.
The setter-method creates a unique first transition to the state with the given id.
Existing first transitions are removed. If the given state id is None, the first transition is removed.
:return: The id of the start state
"""
for transition_id in self.transitions:
if self.transitions[transition_id].from_state is None:
to_state = self.transitions[transition_id].to_state
if to_state is not None:
return to_state
else:
return self.state_id
return None | The start state is the state to which the first transition goes to.
The setter-method creates a unique first transition to the state with the given id.
Existing first transitions are removed. If the given state id is None, the first transition is removed.
:return: The id of the start state | entailment |
def start_state_id(self, start_state_id, to_outcome=None):
"""Set the start state of the container state
See property
:param start_state_id: The state id of the state which should be executed first in the Container state
:raises exceptions.ValueError: if the start_state_id does not exist in
:py:attr:`rafcon.core.states.container_state.ContainerState.states`
"""
if start_state_id is not None and start_state_id not in self.states:
raise ValueError("start_state_id does not exist")
if start_state_id is None and to_outcome is not None: # this is the case if the start state is the state itself
if to_outcome not in self.outcomes:
raise ValueError("to_outcome does not exist")
if start_state_id != self.state_id:
raise ValueError("to_outcome defined but start_state_id is not state_id")
# First we remove the transition to the start state
for transition_id in self.transitions:
if self.transitions[transition_id].from_state is None:
# If the current start state is the same as the old one, we don't have to do anything
if self.transitions[transition_id].to_state == start_state_id:
return
self.remove_transition(transition_id)
break
if start_state_id is not None:
self.add_transition(None, None, start_state_id, to_outcome) | Set the start state of the container state
See property
:param start_state_id: The state id of the state which should be executed first in the Container state
:raises exceptions.ValueError: if the start_state_id does not exist in
:py:attr:`rafcon.core.states.container_state.ContainerState.states` | entailment |
def scoped_variables(self, scoped_variables):
""" Setter for _scoped_variables field
See property
:param dict scoped_variables: Dictionary scoped_variables[data_port_id] of :class:`rafcon.core.scope.ScopedVariable`
:raises exceptions.TypeError: if the scoped_variables parameter has the wrong type
:raises exceptions.AttributeError: if the keys of the scoped_variables dictionary and the ids
of the scoped variables in the dictionary do not match
"""
if not isinstance(scoped_variables, dict):
raise TypeError("scoped_variables must be of type dict")
if [sv_id for sv_id, sv in scoped_variables.items() if not isinstance(sv, ScopedVariable)]:
raise TypeError("element of scope variable must be of type ScopedVariable")
if [sv_id for sv_id, sv in scoped_variables.items() if not sv_id == sv.data_port_id]:
raise AttributeError("The key of the scope variable dictionary and "
"the id of the scope variable do not match")
old_scoped_variables = self._scoped_variables
self._scoped_variables = scoped_variables
for port_id, scoped_variable in scoped_variables.items():
try:
scoped_variable.parent = self
except ValueError:
self._scoped_variables = old_scoped_variables
raise
# check that all old_scoped_variables are no more referencing self as there parent
for old_scoped_variable in old_scoped_variables.values():
if old_scoped_variable not in self._scoped_variables.values() and old_scoped_variable.parent is self:
old_scoped_variable.parent = None | Setter for _scoped_variables field
See property
:param dict scoped_variables: Dictionary scoped_variables[data_port_id] of :class:`rafcon.core.scope.ScopedVariable`
:raises exceptions.TypeError: if the scoped_variables parameter has the wrong type
:raises exceptions.AttributeError: if the keys of the scoped_variables dictionary and the ids
of the scoped variables in the dictionary do not match | entailment |
def _execute(self, execute_inputs, execute_outputs, backward_execution=False):
"""Calls the custom execute function of the script.py of the state
"""
self._script.build_module()
outcome_item = self._script.execute(self, execute_inputs, execute_outputs, backward_execution)
# in the case of backward execution the outcome is not relevant
if backward_execution:
return
# If the state was preempted, the state must be left on the preempted outcome
if self.preempted:
return Outcome(-2, "preempted")
# Outcome id was returned
if outcome_item in self.outcomes:
return self.outcomes[outcome_item]
# Outcome name was returned
for outcome_id, outcome in self.outcomes.items():
if outcome.name == outcome_item:
return self.outcomes[outcome_id]
logger.error("Returned outcome of {0} not existing: {1}".format(self, outcome_item))
return Outcome(-1, "aborted") | Calls the custom execute function of the script.py of the state | entailment |
def run(self):
""" This defines the sequence of actions that are taken when the execution state is executed
:return:
"""
if self.is_root_state:
self.execution_history.push_call_history_item(self, CallType.EXECUTE, None, self.input_data)
logger.debug("Running {0}{1}".format(self, " (backwards)" if self.backward_execution else ""))
if self.backward_execution:
self.setup_backward_run()
else:
self.setup_run()
try:
outcome = self._execute(self.input_data, self.output_data, self.backward_execution)
self.state_execution_status = StateExecutionStatus.WAIT_FOR_NEXT_STATE
if self.backward_execution:
# outcome handling is not required as we are in backward mode and the execution order is fixed
result = self.finalize()
else:
# check output data
self.check_output_data_type()
result = self.finalize(outcome)
if self.is_root_state:
self.execution_history.push_return_history_item(self, CallType.EXECUTE, None, self.output_data)
return result
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
formatted_exc = traceback.format_exception(exc_type, exc_value, exc_traceback)
truncated_exc = []
for line in formatted_exc:
if os.path.join("rafcon", "core") not in line:
truncated_exc.append(line)
logger.error("{0} had an internal error: {1}: {2}\n{3}".format(self, type(e).__name__, e,
''.join(truncated_exc)))
# write error to the output_data of the state
self.output_data["error"] = e
self.state_execution_status = StateExecutionStatus.WAIT_FOR_NEXT_STATE
return self.finalize(Outcome(-1, "aborted")) | This defines the sequence of actions that are taken when the execution state is executed
:return: | entailment |
def register_view(self, view):
"""Called when the view was registered"""
super(StateMachineTreeController, self).register_view(view)
self.view.connect('button_press_event', self.mouse_click)
self.view_is_registered = True
self.update(with_expand=True) | Called when the view was registered | entailment |
def register_actions(self, shortcut_manager):
"""Register callback methods for triggered actions
:param rafcon.gui.shortcut_manager.ShortcutManager shortcut_manager: Shortcut Manager Object holding mappings
between shortcuts and actions.
"""
shortcut_manager.add_callback_for_action("delete", self._delete_selection)
shortcut_manager.add_callback_for_action("add", partial(self._add_new_state, state_type=StateType.EXECUTION))
shortcut_manager.add_callback_for_action("add2", partial(self._add_new_state, state_type=StateType.HIERARCHY))
shortcut_manager.add_callback_for_action("copy", self.copy_action_callback)
shortcut_manager.add_callback_for_action("cut", self.cut_action_callback)
shortcut_manager.add_callback_for_action("paste", self.paste_action_callback) | Register callback methods for triggered actions
:param rafcon.gui.shortcut_manager.ShortcutManager shortcut_manager: Shortcut Manager Object holding mappings
between shortcuts and actions. | entailment |
def register(self):
"""Change the state machine that is observed for new selected states to the selected state machine."""
self._do_selection_update = True
# relieve old model
if self.__my_selected_sm_id is not None: # no old models available
self.relieve_model(self._selected_sm_model)
# set own selected state machine id
self.__my_selected_sm_id = self.model.selected_state_machine_id
if self.__my_selected_sm_id is not None:
# observe new model
self._selected_sm_model = self.model.state_machines[self.__my_selected_sm_id]
self.observe_model(self._selected_sm_model) # for selection
self.update()
else:
self._selected_sm_model = None
self.state_row_iter_dict_by_state_path.clear()
self.tree_store.clear()
self._do_selection_update = False | Change the state machine that is observed for new selected states to the selected state machine. | entailment |
def paste_action_callback(self, *event):
"""Callback method for paste action"""
if react_to_event(self.view, self.tree_view, event):
sm_selection, _ = self.get_state_machine_selection()
# only list specific elements are cut by widget
if len(sm_selection.states) == 1:
global_clipboard.paste(sm_selection.get_selected_state(), limited=['states', 'transitions', 'data_flows'])
else:
logger.warning("Please select only one state to paste.")
return True | Callback method for paste action | entailment |
def _add_new_state(self, *event, **kwargs):
"""Triggered when shortcut keys for adding a new state are pressed, or Menu Bar "Edit, Add State" is clicked.
Adds a new state only if the the state machine tree is in focus.
"""
if react_to_event(self.view, self.view['state_machine_tree_view'], event):
state_type = StateType.EXECUTION if 'state_type' not in kwargs else kwargs['state_type']
gui_helper_state_machine.add_new_state(self._selected_sm_model, state_type)
return True | Triggered when shortcut keys for adding a new state are pressed, or Menu Bar "Edit, Add State" is clicked.
Adds a new state only if the the state machine tree is in focus. | entailment |
def selection_changed(self, widget, event=None):
"""Notify state machine about tree view selection"""
# do not forward cursor selection updates if state update is running
# TODO maybe make this generic for respective abstract controller
if self._state_which_is_updated:
return
super(StateMachineTreeController, self).selection_changed(widget, event) | Notify state machine about tree view selection | entailment |
def redo_expansion_state(self, ignore_not_existing_rows=False):
""" Considers the tree to be collapsed and expand into all tree item with the flag set True """
def set_expansion_state(state_path):
state_row_iter = self.state_row_iter_dict_by_state_path[state_path]
if state_row_iter: # may elements are missing afterwards
state_row_path = self.tree_store.get_path(state_row_iter)
self.view.expand_to_path(state_row_path)
if self.__my_selected_sm_id is not None and self.__my_selected_sm_id in self.__expansion_state:
expansion_state = self.__expansion_state[self.__my_selected_sm_id]
try:
for state_path, state_row_expanded in expansion_state.items():
if state_path in self.state_row_iter_dict_by_state_path:
if state_row_expanded:
set_expansion_state(state_path)
else:
if not ignore_not_existing_rows and self._selected_sm_model and \
self._selected_sm_model.state_machine.get_state_by_path(state_path, as_check=True):
state = self._selected_sm_model.state_machine.get_state_by_path(state_path)
if isinstance(state, LibraryState) or state.is_root_state_of_library or \
state.get_next_upper_library_root_state():
continue
logger.error("State not in StateMachineTree but in StateMachine, {0}.".format(state_path))
except (TypeError, KeyError):
logger.error("Expansion state of state machine {0} could not be restored"
"".format(self.__my_selected_sm_id)) | Considers the tree to be collapsed and expand into all tree item with the flag set True | entailment |
def update(self, changed_state_model=None, with_expand=False):
"""Checks if all states are in tree and if tree has states which were deleted
:param changed_state_model: Model that row has to be updated
:param with_expand: The expand flag for the tree
"""
if not self.view_is_registered:
return
# define initial state-model for update
if changed_state_model is None:
# reset all
parent_row_iter = None
self.state_row_iter_dict_by_state_path.clear()
self.tree_store.clear()
if self._selected_sm_model:
changed_state_model = self._selected_sm_model.root_state
else:
return
else: # pick
if changed_state_model.state.is_root_state:
parent_row_iter = self.state_row_iter_dict_by_state_path[changed_state_model.state.get_path()]
else:
if changed_state_model.state.is_root_state_of_library:
# because either lib-state or lib-state-root is in tree the next higher hierarchy state is updated
changed_upper_state_m = changed_state_model.parent.parent
else:
changed_upper_state_m = changed_state_model.parent
# TODO check the work around of the next 2 lines while refactoring -> it is a check to be more robust
while changed_upper_state_m.state.get_path() not in self.state_row_iter_dict_by_state_path:
# show Warning because because avoided method states_update
logger.warning("Take a parent state because this is not in.")
changed_upper_state_m = changed_upper_state_m.parent
parent_row_iter = self.state_row_iter_dict_by_state_path[changed_upper_state_m.state.get_path()]
# do recursive update
self.insert_and_update_recursively(parent_row_iter, changed_state_model, with_expand) | Checks if all states are in tree and if tree has states which were deleted
:param changed_state_model: Model that row has to be updated
:param with_expand: The expand flag for the tree | entailment |
def show_content(self, state_model):
"""Check state machine tree specific show content flag.
Is returning true if the upper most library state of a state model has a enabled show content flag or if there
is no library root state above this state.
:param rafcon.gui.models.abstract_state.AbstractStateModel state_model: The state model to check
"""
upper_most_lib_state_m = None
if isinstance(state_model, LibraryStateModel):
uppermost_library_root_state = state_model.state.get_uppermost_library_root_state()
if uppermost_library_root_state is None:
upper_most_lib_state_m = state_model
else:
upper_lib_state = uppermost_library_root_state.parent
upper_most_lib_state_m = self._selected_sm_model.get_state_model_by_path(upper_lib_state.get_path())
if upper_most_lib_state_m:
return upper_most_lib_state_m.show_content()
else:
return True | Check state machine tree specific show content flag.
Is returning true if the upper most library state of a state model has a enabled show content flag or if there
is no library root state above this state.
:param rafcon.gui.models.abstract_state.AbstractStateModel state_model: The state model to check | entailment |
def insert_and_update_recursively(self, parent_iter, state_model, with_expand=False):
""" Insert and/or update the handed state model in parent tree store element iterator
:param parent_iter: Parent tree store iterator the insert should be performed in
:param StateModel state_model: Model of state that has to be insert and/or updated
:param bool with_expand: Trigger to expand tree
:return:
"""
# the case handling of this method
# 0 - create - common state
# 0.1 - modify attributes of common state which is already in the list
# 1 - create - library with show content -> add library root state
# 2 - create - library without show content -> add library state
# 3 - in as library with show content -> switch library without show content, remove children + LibRootState
# 3.1 - in as library with show content -> nothing to do
# 4 - in as library without show content -> switch library with show content, add children + remove LibState
# 4.1 - in as library without show content -> nothing to do
# if state model is LibraryStateModel with enabled show content state_model becomes the library root state model
if isinstance(state_model, LibraryStateModel) and self.show_content(state_model) and state_model.state_copy_initialized:
_state_model = state_model
state_model = state_model.state_copy
else:
_state_model = state_model
if self._state_which_is_updated is None:
self._state_which_is_updated = _state_model
# TODO remove this workaround for removing LibraryStateModel or there root states by default
if isinstance(_state_model, LibraryStateModel) and _state_model.state_copy_initialized:
state_row_iter = None
if _state_model.state.get_path() in self.state_row_iter_dict_by_state_path:
state_row_iter = self.state_row_iter_dict_by_state_path[_state_model.state.get_path()]
if state_model.state.get_path() in self.state_row_iter_dict_by_state_path:
state_row_iter = self.state_row_iter_dict_by_state_path[state_model.state.get_path()]
if state_row_iter:
self.remove_tree_children(state_row_iter)
del self.state_row_iter_dict_by_state_path[self.tree_store.get_value(state_row_iter, self.STATE_PATH_STORAGE_ID)]
self.tree_store.remove(state_row_iter)
# if library root state is used instate of library state show both in type and state id
_state_id = _state_model.state.state_id
# _state_id += '' if _state_model is state_model else '/' + state_model.state.state_id TODO enable this line
_state_type = type(_state_model.state).__name__
_state_type += '' if _state_model is state_model else '/' + type(state_model.state).__name__
# check if in
state_path = state_model.state.get_path()
if state_path not in self.state_row_iter_dict_by_state_path:
# if not in -> insert it
state_row_iter = self.tree_store.insert_before(parent=parent_iter, sibling=None,
row=(state_model.state.name,
_state_id,
_state_type,
_state_model,
state_model.state.get_path()))
self.state_row_iter_dict_by_state_path[state_path] = state_row_iter
if with_expand:
parent_path = self.tree_store.get_path(state_row_iter)
self.view.expand_to_path(parent_path)
else:
# if in -> check if up to date
state_row_iter = self.state_row_iter_dict_by_state_path[state_model.state.get_path()]
self.update_tree_store_row(state_model)
# check children
# - check if ALL children are in
if isinstance(state_model, ContainerStateModel):
for child_state_id, child_state_model in state_model.states.items():
self.insert_and_update_recursively(state_row_iter, child_state_model, with_expand=False)
# - check if TOO MUCH children are in
# if state_model.state.get_library_root_state() is not None or isinstance(state_model, LibraryStateModel):
for n in reversed(range(self.tree_store.iter_n_children(state_row_iter))):
child_iter = self.tree_store.iter_nth_child(state_row_iter, n)
child_state_path = self.tree_store.get_value(child_iter, self.STATE_PATH_STORAGE_ID)
child_model = None
if self._selected_sm_model.state_machine.get_state_by_path(child_state_path, as_check=True):
child_model = self._selected_sm_model.get_state_model_by_path(child_state_path)
child_id = self.tree_store.get_value(child_iter, self.ID_STORAGE_ID)
# check if there are left over rows of old states (switch from HS or CS to S and so on)
show_content_flag = isinstance(child_model, LibraryStateModel) and self.show_content(child_model) and \
child_model.state_copy_initialized
child_is_lib_with_show_content = isinstance(child_model, LibraryStateModel) and show_content_flag
child_is_lib_without_show_content = isinstance(child_model, LibraryStateModel) and not show_content_flag
if not isinstance(state_model, ContainerStateModel) or child_model is None or \
child_id not in state_model.states and not child_is_lib_with_show_content \
and isinstance(child_model, LibraryStateModel) and child_id == child_model.state.state_copy.state_id \
or child_is_lib_without_show_content and child_id == child_model.state.state_copy.state_id or \
isinstance(_state_model, LibraryStateModel) and not self.show_content(_state_model) or \
child_model.state.is_root_state_of_library and not self.show_content(child_model.parent):
self.remove_tree_children(child_iter)
del self.state_row_iter_dict_by_state_path[self.tree_store.get_value(child_iter, self.STATE_PATH_STORAGE_ID)]
self.tree_store.remove(child_iter)
if self._state_which_is_updated is _state_model:
self._state_which_is_updated = None | Insert and/or update the handed state model in parent tree store element iterator
:param parent_iter: Parent tree store iterator the insert should be performed in
:param StateModel state_model: Model of state that has to be insert and/or updated
:param bool with_expand: Trigger to expand tree
:return: | entailment |
def get_state_machine_selection(self):
"""Getter state machine selection
:return: selection object, filtered set of selected states
:rtype: rafcon.gui.selection.Selection, set
"""
if self._selected_sm_model:
return self._selected_sm_model.selection, self._selected_sm_model.selection.states
else:
return None, set() | Getter state machine selection
:return: selection object, filtered set of selected states
:rtype: rafcon.gui.selection.Selection, set | entailment |
def _handle_double_click(self, event):
""" Double click with left mouse button focuses the state and toggles the collapse status"""
if event.get_button()[1] == 1: # Left mouse button
path_info = self.tree_view.get_path_at_pos(int(event.x), int(event.y))
if path_info: # Valid entry was clicked on
path = path_info[0]
iter = self.tree_store.get_iter(path)
state_model = self.tree_store.get_value(iter, self.MODEL_STORAGE_ID)
# Set focus to StateModel
selection = self._selected_sm_model.selection
selection.focus = state_model
# Toggle collapse status if applicable for this kind of state
if self.view.row_expanded(path):
self.view.collapse_row(path)
else:
if isinstance(state_model, ContainerStateModel) or \
isinstance(state_model, LibraryStateModel) and self.show_content(state_model):
self.view.expand_to_path(path) | Double click with left mouse button focuses the state and toggles the collapse status | entailment |
def execute(self, state, inputs=None, outputs=None, backward_execution=False):
"""Execute the user 'execute' function specified in the script
:param ExecutionState state: the state belonging to the execute function, refers to 'self'
:param dict inputs: the input data of the script
:param dict outputs: the output data of the script
:param bool backward_execution: Flag whether to run the script in backwards mode
:return: Return value of the execute script
:rtype: str | int
"""
if not outputs:
outputs = {}
if not inputs:
inputs = {}
if backward_execution:
if hasattr(self._compiled_module, "backward_execute"):
return self._compiled_module.backward_execute(
state, inputs, outputs, rafcon.core.singleton.global_variable_manager
)
else:
logger.debug("No backward execution method found for state %s" % state.name)
return None
else:
return self._compiled_module.execute(state, inputs, outputs,
rafcon.core.singleton.global_variable_manager) | Execute the user 'execute' function specified in the script
:param ExecutionState state: the state belonging to the execute function, refers to 'self'
:param dict inputs: the input data of the script
:param dict outputs: the output data of the script
:param bool backward_execution: Flag whether to run the script in backwards mode
:return: Return value of the execute script
:rtype: str | int | entailment |
def _load_script(self):
"""Loads the script from the filesystem
:raises exceptions.IOError: if the script file could not be opened
"""
script_text = filesystem.read_file(self.path, self.filename)
if not script_text:
raise IOError("Script file could not be opened or was empty: {0}"
"".format(os.path.join(self.path, self.filename)))
self.script = script_text | Loads the script from the filesystem
:raises exceptions.IOError: if the script file could not be opened | entailment |
def build_module(self):
"""Builds a temporary module from the script file
:raises exceptions.IOError: if the compilation of the script module failed
"""
try:
imp.acquire_lock()
module_name = os.path.splitext(self.filename)[0] + str(self._script_id)
# load module
tmp_module = imp.new_module(module_name)
code = compile(self.script, '%s (%s)' % (self.filename, self._script_id), 'exec')
try:
exec(code, tmp_module.__dict__)
except RuntimeError as e:
raise IOError("The compilation of the script module failed - error message: %s" % str(e))
# return the module
self.compiled_module = tmp_module
finally:
imp.release_lock() | Builds a temporary module from the script file
:raises exceptions.IOError: if the compilation of the script module failed | entailment |
def register_view(self, view):
"""Called when the View was registered"""
super(MenuBarController, self).register_view(view)
data_flow_mode = global_runtime_config.get_config_value("DATA_FLOW_MODE", False)
view["data_flow_mode"].set_active(data_flow_mode)
show_data_flows = global_runtime_config.get_config_value("SHOW_DATA_FLOWS", True)
view["show_data_flows"].set_active(show_data_flows)
show_data_values = global_runtime_config.get_config_value("SHOW_DATA_FLOW_VALUE_LABELS", True)
view["show_data_values"].set_active(show_data_values)
show_aborted_preempted = global_runtime_config.get_config_value("SHOW_ABORTED_PREEMPTED", False)
view["show_aborted_preempted"].set_active(show_aborted_preempted)
view["expert_view"].hide()
view["grid"].hide()
# use dedicated function to connect the buttons to be able to access the handler id later on
self.connect_button_to_function('new', 'activate', self.on_new_activate)
self.connect_button_to_function('open', 'activate', self.on_open_activate)
self.connect_button_to_function('save', 'activate', self.on_save_activate)
self.connect_button_to_function('save_as', 'activate', self.on_save_as_activate)
self.connect_button_to_function('save_as_copy', 'activate', self.on_save_as_copy_activate)
self.connect_button_to_function('menu_preferences', 'activate', self.on_menu_preferences_activate)
self.connect_button_to_function('refresh_all', 'activate', self.on_refresh_all_activate)
self.connect_button_to_function('refresh_libraries', 'activate', self.on_refresh_libraries_activate)
self.connect_button_to_function('bake_state_machine', 'activate', self.on_bake_state_machine_activate)
self.connect_button_to_function('quit', 'activate', self.on_quit_activate)
self.connect_button_to_function('cut', 'activate', self.on_cut_selection_activate)
self.connect_button_to_function('copy', 'activate', self.on_copy_selection_activate)
self.connect_button_to_function('paste', 'activate', self.on_paste_clipboard_activate)
self.connect_button_to_function('delete', 'activate', self.on_delete_activate)
self.connect_button_to_function('is_start_state', 'activate', self.on_toggle_is_start_state_active)
self.connect_button_to_function('add', 'activate', self.on_add_state_activate)
self.connect_button_to_function('group', 'activate', self.on_group_states_activate)
self.connect_button_to_function('ungroup', 'activate', self.on_ungroup_state_activate)
self.connect_button_to_function('substitute_state', 'activate', self.on_substitute_selected_state_activate)
self.connect_button_to_function('save_state_as', 'activate', self.on_save_selected_state_as_activate)
self.connect_button_to_function('undo', 'activate', self.on_undo_activate)
self.connect_button_to_function('redo', 'activate', self.on_redo_activate)
self.connect_button_to_function('grid', 'activate', self.on_grid_toggled)
self.connect_button_to_function('data_flow_mode', 'toggled', self.on_data_flow_mode_toggled)
self.connect_button_to_function('show_data_flows', 'toggled', self.on_show_data_flows_toggled)
self.connect_button_to_function('show_data_values', 'toggled', self.on_show_data_values_toggled)
self.connect_button_to_function('show_aborted_preempted', 'toggled', self.on_show_aborted_preempted_toggled)
self.connect_button_to_function('expert_view', 'activate', self.on_expert_view_activate)
self.connect_button_to_function('full_screen', 'toggled', self.on_full_screen_mode_toggled)
self.connect_button_to_function('start', 'activate', self.on_start_activate)
self.connect_button_to_function('start_from_selected', 'activate', self.on_start_from_selected_state_activate)
self.connect_button_to_function('run_to_selected', 'activate', self.on_run_to_selected_state_activate)
self.connect_button_to_function('pause', 'activate', self.on_pause_activate)
self.connect_button_to_function('stop', 'activate', self.on_stop_activate)
self.connect_button_to_function('step_mode', 'activate', self.on_step_mode_activate)
self.connect_button_to_function('step_into', 'activate', self.on_step_into_activate)
self.connect_button_to_function('step_over', 'activate', self.on_step_over_activate)
self.connect_button_to_function('step_out', 'activate', self.on_step_out_activate)
self.connect_button_to_function('backward_step', 'activate', self.on_backward_step_activate)
self.connect_button_to_function('about', 'activate', self.on_about_activate)
self.full_screen_window.connect('key_press_event', self.on_escape_key_press_event_leave_full_screen)
self.view['menu_edit'].connect('select', self.check_edit_menu_items_status)
self.registered_view = True
self._update_recently_opened_state_machines()
# do not move next line - here to show warning in GUI debug console
self.create_logger_warning_if_shortcuts_are_overwritten_by_menu_bar() | Called when the View was registered | entailment |
def on_config_value_changed(self, config_m, prop_name, info):
"""Callback when a config value has been changed
:param ConfigModel config_m: The config model that has been changed
:param str prop_name: Should always be 'config'
:param dict info: Information e.g. about the changed config key
"""
config_key = info['args'][1]
# config_value = info['args'][2]
if config_key == "LIBRARY_PATHS":
library_manager.refresh_libraries()
elif config_key == "SHORTCUTS":
self.refresh_shortcuts()
elif config_key == "recently_opened_state_machines":
self._update_recently_opened_state_machines() | Callback when a config value has been changed
:param ConfigModel config_m: The config model that has been changed
:param str prop_name: Should always be 'config'
:param dict info: Information e.g. about the changed config key | entailment |
def _update_recently_opened_state_machines(self):
"""Update the sub menu Open Recent in File menu
Method clean's first all menu items of the sub menu 'recent open', then insert the user menu item to clean
recent opened state machine paths and finally insert menu items for all elements in recent opened state machines
list.
"""
if not self.registered_view:
return
for item in self.view.sub_menu_open_recently.get_children():
self.view.sub_menu_open_recently.remove(item)
menu_item = gui_helper_label.create_menu_item("remove invalid paths", constants.ICON_ERASE,
global_runtime_config.clean_recently_opened_state_machines)
self.view.sub_menu_open_recently.append(menu_item)
self.view.sub_menu_open_recently.append(Gtk.SeparatorMenuItem())
for sm_path in global_runtime_config.get_config_value("recently_opened_state_machines", []):
# define label string
root_state_name = gui_helper_state_machine.get_root_state_name_of_sm_file_system_path(sm_path)
if root_state_name is None and not os.path.isdir(sm_path):
root_state_name = 'NOT_ACCESSIBLE'
label_string = "'{0}' in {1}".format(root_state_name, sm_path) if root_state_name is not None else sm_path
# define icon of menu item
is_in_libs = library_manager.is_os_path_within_library_root_paths(sm_path)
button_image = constants.SIGN_LIB if is_in_libs else constants.BUTTON_OPEN
# prepare state machine open call_back function
sm_open_function = partial(self.on_open_activate, path=sm_path)
# create and insert new menu item
menu_item = gui_helper_label.create_menu_item(label_string, button_image, sm_open_function)
self.view.sub_menu_open_recently.append(menu_item)
self.view.sub_menu_open_recently.show_all() | Update the sub menu Open Recent in File menu
Method clean's first all menu items of the sub menu 'recent open', then insert the user menu item to clean
recent opened state machine paths and finally insert menu items for all elements in recent opened state machines
list. | entailment |
def on_full_screen_activate(self, *args):
"""
function to display the currently selected state machine in full screen mode
:param args:
:return:
"""
self.sm_notebook.set_show_tabs(False)
self.main_window_view['graphical_editor_vbox'].remove(self.sm_notebook)
self.full_screen_window.add(self.sm_notebook)
position = self.main_window_view.get_top_widget().get_position()
self.full_screen_window.show()
self.full_screen_window.move(position[0], position[1])
self.full_screen_window.set_decorated(False)
self.full_screen_window.fullscreen()
self.main_window_view.get_top_widget().iconify() | function to display the currently selected state machine in full screen mode
:param args:
:return: | entailment |
def unregister_view(self):
"""import log
Unregister all registered functions to a view element
:return:
"""
for handler_id in self.handler_ids.keys():
self.view[handler_id].disconnect(self.handler_ids[handler_id]) | import log
Unregister all registered functions to a view element
:return: | entailment |
def register_actions(self, shortcut_manager):
"""Register callback methods for triggered actions
:param rafcon.gui.shortcut_manager.ShortcutManager shortcut_manager: Shortcut Manager Object holding mappings
between shortcuts and actions.
"""
self.add_callback_to_shortcut_manager('save', partial(self.call_action_callback, "on_save_activate"))
self.add_callback_to_shortcut_manager('save_as', partial(self.call_action_callback, "on_save_as_activate"))
self.add_callback_to_shortcut_manager('save_as_copy', partial(self.call_action_callback,
"on_save_as_copy_activate"))
self.add_callback_to_shortcut_manager('save_state_as', partial(self.call_action_callback,
"on_save_selected_state_as_activate"))
self.add_callback_to_shortcut_manager('substitute_state', partial(self.call_action_callback,
"on_substitute_selected_state_activate"))
self.add_callback_to_shortcut_manager('substitute_library_with_template',
partial(self.call_action_callback,
"on_substitute_library_with_template_activate"))
self.add_callback_to_shortcut_manager('open', partial(self.call_action_callback, "on_open_activate"))
self.add_callback_to_shortcut_manager('open_library_state_separately',
self.on_open_library_state_separately_activate)
self.add_callback_to_shortcut_manager('new', partial(self.call_action_callback, "on_new_activate"))
self.add_callback_to_shortcut_manager('quit', partial(self.call_action_callback, "on_quit_activate"))
self.add_callback_to_shortcut_manager('is_start_state', partial(self.call_action_callback,
"on_toggle_is_start_state_active"))
callback_function = partial(self.call_action_callback, "on_add_transitions_from_closest_sibling_state_active")
self.add_callback_to_shortcut_manager('transition_from_closest_sibling_state', callback_function)
callback_function = partial(self.call_action_callback, "on_add_transitions_to_closest_sibling_state_active")
self.add_callback_to_shortcut_manager('transition_to_closest_sibling_state', callback_function)
callback_function = partial(self.call_action_callback, "on_add_transitions_to_parent_state_active")
self.add_callback_to_shortcut_manager('transition_to_parent_state', callback_function)
self.add_callback_to_shortcut_manager('group', partial(self.call_action_callback, "on_group_states_activate"))
self.add_callback_to_shortcut_manager('ungroup', partial(self.call_action_callback,
"on_ungroup_state_activate"))
self.add_callback_to_shortcut_manager('start', partial(self.call_action_callback, "on_start_activate"))
self.add_callback_to_shortcut_manager('start_from_selected', partial(self.call_action_callback,
"on_start_from_selected_state_activate"))
self.add_callback_to_shortcut_manager('run_to_selected', partial(self.call_action_callback,
"on_run_to_selected_state_activate"))
self.add_callback_to_shortcut_manager('stop', partial(self.call_action_callback, "on_stop_activate"))
self.add_callback_to_shortcut_manager('pause', partial(self.call_action_callback, "on_pause_activate"))
self.add_callback_to_shortcut_manager('step_mode', partial(self.call_action_callback, "on_step_mode_activate"))
self.add_callback_to_shortcut_manager('step', partial(self.call_action_callback, "on_step_into_activate"))
self.add_callback_to_shortcut_manager('backward_step', partial(self.call_action_callback,
"on_backward_step_activate"))
self.add_callback_to_shortcut_manager('reload', partial(self.call_action_callback, "on_refresh_all_activate"))
self.add_callback_to_shortcut_manager('show_data_flows', self.show_data_flows_toggled_shortcut)
self.add_callback_to_shortcut_manager('show_data_values', self.show_data_values_toggled_shortcut)
self.add_callback_to_shortcut_manager('data_flow_mode', self.data_flow_mode_toggled_shortcut)
self.add_callback_to_shortcut_manager('show_aborted_preempted', self.show_aborted_preempted)
self.add_callback_to_shortcut_manager('fullscreen', self.on_toggle_full_screen_mode) | Register callback methods for triggered actions
:param rafcon.gui.shortcut_manager.ShortcutManager shortcut_manager: Shortcut Manager Object holding mappings
between shortcuts and actions. | entailment |
def add_callback_to_shortcut_manager(self, action, callback):
"""
Helper function to add an callback for an action to the shortcut manager.
:param action: the action to add a shortcut for
:param callback: the callback if the action is executed
:return:
"""
if action not in self.registered_shortcut_callbacks:
self.registered_shortcut_callbacks[action] = []
self.registered_shortcut_callbacks[action].append(callback)
self.shortcut_manager.add_callback_for_action(action, callback) | Helper function to add an callback for an action to the shortcut manager.
:param action: the action to add a shortcut for
:param callback: the callback if the action is executed
:return: | entailment |
def remove_all_callbacks(self):
"""
Remove all callbacks registered to the shortcut manager
:return:
"""
for action in self.registered_shortcut_callbacks.keys():
for callback in self.registered_shortcut_callbacks[action]:
self.shortcut_manager.remove_callback_for_action(action, callback)
# delete all registered shortcut callbacks
self.registered_shortcut_callbacks = {} | Remove all callbacks registered to the shortcut manager
:return: | entailment |
def point_left_of_line(point, line_start, line_end):
"""Determines whether a point is left of a line
:param point: Point to be checked (tuple with x any y coordinate)
:param line_start: Starting point of the line (tuple with x any y coordinate)
:param line_end: End point of the line (tuple with x any y coordinate)
:return: True if point is left of line, else False
"""
# determine sign of determinant
# ((b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x)) > 0
return ((line_end[0] - line_start[0]) * (point[1] - line_start[1]) -
(line_end[1] - line_start[1]) * (point[0] - line_start[0])) < 0 | Determines whether a point is left of a line
:param point: Point to be checked (tuple with x any y coordinate)
:param line_start: Starting point of the line (tuple with x any y coordinate)
:param line_end: End point of the line (tuple with x any y coordinate)
:return: True if point is left of line, else False | entailment |
def point_on_line(point, line_start, line_end, accuracy=50.):
"""Checks whether a point lies on a line
The function checks whether the point "point" (P) lies on the line defined by its starting point line_start (A) and
its end point line_end (B).
This is done by comparing the distance of [AB] with the sum of the distances [AP] and [PB]. If the difference is
smaller than [AB] / accuracy, the point P is assumed to be on the line. By increasing the value of accuracy (the
default is 50), the tolerance is decreased.
:param point: Point to be checked (tuple with x any y coordinate)
:param line_start: Starting point of the line (tuple with x any y coordinate)
:param line_end: End point of the line (tuple with x any y coordinate)
:param accuracy: The higher this value, the less distance is tolerated
:return: True if the point is one the line, False if not
"""
length = dist(line_start, line_end)
ds = length / float(accuracy)
if -ds < (dist(line_start, point) + dist(point, line_end) - length) < ds:
return True
return False | Checks whether a point lies on a line
The function checks whether the point "point" (P) lies on the line defined by its starting point line_start (A) and
its end point line_end (B).
This is done by comparing the distance of [AB] with the sum of the distances [AP] and [PB]. If the difference is
smaller than [AB] / accuracy, the point P is assumed to be on the line. By increasing the value of accuracy (the
default is 50), the tolerance is decreased.
:param point: Point to be checked (tuple with x any y coordinate)
:param line_start: Starting point of the line (tuple with x any y coordinate)
:param line_end: End point of the line (tuple with x any y coordinate)
:param accuracy: The higher this value, the less distance is tolerated
:return: True if the point is one the line, False if not | entailment |
def point_in_triangle(p, v1, v2, v3):
"""Checks whether a point is within the given triangle
The function checks, whether the given point p is within the triangle defined by the the three corner point v1,
v2 and v3.
This is done by checking whether the point is on all three half-planes defined by the three edges of the triangle.
:param p: The point to be checked (tuple with x any y coordinate)
:param v1: First vertex of the triangle (tuple with x any y coordinate)
:param v2: Second vertex of the triangle (tuple with x any y coordinate)
:param v3: Third vertex of the triangle (tuple with x any y coordinate)
:return: True if the point is within the triangle, False if not
"""
def _test(p1, p2, p3):
return (p1[0] - p3[0]) * (p2[1] - p3[1]) - (p2[0] - p3[0]) * (p1[1] - p3[1])
b1 = _test(p, v1, v2) < 0.0
b2 = _test(p, v2, v3) < 0.0
b3 = _test(p, v3, v1) < 0.0
return (b1 == b2) and (b2 == b3) | Checks whether a point is within the given triangle
The function checks, whether the given point p is within the triangle defined by the the three corner point v1,
v2 and v3.
This is done by checking whether the point is on all three half-planes defined by the three edges of the triangle.
:param p: The point to be checked (tuple with x any y coordinate)
:param v1: First vertex of the triangle (tuple with x any y coordinate)
:param v2: Second vertex of the triangle (tuple with x any y coordinate)
:param v3: Third vertex of the triangle (tuple with x any y coordinate)
:return: True if the point is within the triangle, False if not | entailment |
def equal(t1, t2, digit=None):
""" Compare two iterators and its elements on specific digit precision
The method assume that t1 and t2 are are list or tuple.
:param t1: First element to compare
:param t2: Second element to compare
:param int digit: Number of digits to compare
:rtype bool
:return: True if equal and False if different
"""
if not len(t1) == len(t2):
return False
for idx in range(len(t1)):
if digit is not None:
if not round(t1[idx], digit) == round(t2[idx], digit):
return False
else:
if not t1[idx] == t2[idx]:
return False
return True | Compare two iterators and its elements on specific digit precision
The method assume that t1 and t2 are are list or tuple.
:param t1: First element to compare
:param t2: Second element to compare
:param int digit: Number of digits to compare
:rtype bool
:return: True if equal and False if different | entailment |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.