idx
int64
0
63k
question
stringlengths
53
5.28k
target
stringlengths
5
805
40,600
def run_to_states ( self ) : 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
40,601
def recursively_preempt_states ( self ) : super ( ContainerState , self ) . recursively_preempt_states ( ) 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 .
40,602
def recursively_pause_states ( self ) : 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 .
40,603
def recursively_resume_states ( self ) : 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 .
40,604
def handle_no_transition ( self , state ) : transition = None while not transition : 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 execution_signal = state_machine_execution_engine . handle_execution_mode ( self ) if execution_signal is StateMachineExecutionStatus . STOPPED : 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 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 .
40,605
def handle_no_start_state ( self ) : start_state = self . get_start_state ( set_final_outcome = True ) while not start_state : execution_signal = state_machine_execution_engine . handle_execution_mode ( self ) if execution_signal is StateMachineExecutionStatus . STOPPED : 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
40,606
def add_state ( self , state , storage_load = False ) : assert isinstance ( state , State ) while state . state_id == self . state_id or state . state_id in self . states : state . change_state_id ( ) 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 .
40,607
def remove_state ( self , state_id , recursive = True , force = False , destroy = True ) : 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 ) 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 : self . states [ state_id ] . destroy ( recursive ) self . states [ state_id ] . parent = None return self . states . pop ( state_id )
Remove a state from the container state .
40,608
def change_state_type ( self , state , new_state_class ) : 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
40,609
def set_start_state ( self , 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
40,610
def get_start_state ( self , set_final_outcome = False ) : 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 if self . start_state_id == self . state_id : if set_final_outcome : for transition_id in self . transitions : 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
40,611
def check_transition_id ( self , transition_id ) : 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
40,612
def check_if_outcome_already_connected ( self , from_state_id , from_outcome ) : 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
40,613
def create_transition ( self , from_state_id , from_outcome , to_state_id , to_outcome , transition_id ) : 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 ] 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 : 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 : 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 ) self . _transitions_cv . acquire ( ) self . _transitions_cv . notify_all ( ) self . _transitions_cv . release ( ) return transition_id
Creates a new transition .
40,614
def add_transition ( self , from_state_id , from_outcome , to_state_id , to_outcome , transition_id = None ) : transition_id = self . check_transition_id ( transition_id ) 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 self . _transitions_cv . acquire ( ) self . _transitions_cv . notify_all ( ) self . _transitions_cv . release ( ) return transition_id
Adds a transition to the container state
40,615
def get_transition_for_outcome ( self , state , outcome ) : 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 .
40,616
def remove_transition ( self , transition_id , destroy = True ) : 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
40,617
def remove_outcome_hook ( self , outcome_id ) : 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
40,618
def check_data_flow_id ( self , data_flow_id ) : 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
40,619
def add_data_flow ( self , from_state_id , from_data_port_id , to_state_id , to_data_port_id , data_flow_id = None ) : 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
40,620
def remove_data_flow ( self , data_flow_id , destroy = True ) : 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
40,621
def remove_data_flows_with_data_port_id ( self , data_port_id ) : 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 ) 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
40,622
def get_scoped_variable_from_name ( self , name ) : 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
40,623
def add_scoped_variable ( self , name , data_type = None , default_value = None , scoped_variable_id = None ) : if scoped_variable_id is None : 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 ) 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
40,624
def remove_scoped_variable ( self , scoped_variable_id , destroy = True ) : if scoped_variable_id not in self . _scoped_variables : raise AttributeError ( "A scoped variable with id %s does not exist" % str ( scoped_variable_id ) ) if destroy : self . remove_data_flows_with_data_port_id ( scoped_variable_id ) self . _scoped_variables [ scoped_variable_id ] . parent = None return self . _scoped_variables . pop ( scoped_variable_id )
Remove a scoped variable from the container state
40,625
def get_data_port ( self , state_id , port_id ) : 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
40,626
def get_inputs_for_state ( self , 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 ( ) : 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 : 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 .
40,627
def add_input_data_to_scoped_data ( self , dictionary ) : 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 ) 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
40,628
def add_state_execution_output_to_scoped_data ( self , dictionary , state ) : 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
40,629
def add_default_values_of_scoped_variables_to_scoped_data ( self ) : 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
40,630
def update_scoped_variables_with_output_dictionary ( self , dictionary , state ) : for key , value in dictionary . items ( ) : output_data_port_key = None 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 : if data_flow . to_key in self . scoped_variables . keys ( ) : 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 .
40,631
def change_state_id ( self , state_id = None ) : old_state_id = self . state_id super ( ContainerState , self ) . change_state_id ( state_id ) 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 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 .
40,632
def get_state_for_transition ( self , transition ) : if not isinstance ( transition , Transition ) : raise TypeError ( "transition must be of type Transition" ) 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
40,633
def write_output_data ( self , specific_output_dictionary = None ) : 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 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 .
40,634
def check_data_port_connection ( self , check_data_port ) : for data_flow in self . data_flows . values ( ) : 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 : 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
40,635
def _check_data_flow_validity ( self , check_data_flow ) : 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
40,636
def _check_data_flow_id ( self , data_flow ) : 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
40,637
def _check_data_flow_ports ( self , data_flow ) : 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 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 ) 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 ) 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 ) if from_state_id == self . state_id : 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 : 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 ) if to_state_id == self . state_id : 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 : 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 ) 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 ) 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
40,638
def _check_data_flow_types ( self , check_data_flow ) : 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 ) 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
40,639
def _check_transition_validity ( self , check_transition ) : valid , message = self . _check_transition_id ( check_transition ) if not valid : return False , message 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
40,640
def _check_transition_id ( self , transition ) : 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
40,641
def _check_start_transition ( self , start_transition ) : 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
40,642
def _check_transition_target ( self , transition ) : 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
40,643
def _check_transition_origin ( self , transition ) : 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
40,644
def _check_transition_connection ( self , check_transition ) : 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 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
40,645
def states ( self , states ) : 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 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
40,646
def transitions ( self , transitions ) : 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 ) 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
40,647
def data_flows ( self , data_flows ) : 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 ) 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
40,648
def start_state_id ( self ) : 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 .
40,649
def start_state_id ( self , start_state_id , to_outcome = None ) : 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 : 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" ) for transition_id in self . transitions : if self . transitions [ transition_id ] . from_state is None : 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
40,650
def scoped_variables ( self , scoped_variables ) : 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 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
40,651
def _execute ( self , execute_inputs , execute_outputs , backward_execution = False ) : self . _script . build_module ( ) outcome_item = self . _script . execute ( self , execute_inputs , execute_outputs , backward_execution ) if backward_execution : return if self . preempted : return Outcome ( - 2 , "preempted" ) if outcome_item in self . outcomes : return self . outcomes [ outcome_item ] 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
40,652
def run ( self ) : 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 : result = self . finalize ( ) else : 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 ) ) ) 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
40,653
def register_view ( self , view ) : 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
40,654
def register ( self ) : self . _do_selection_update = True if self . __my_selected_sm_id is not None : self . relieve_model ( self . _selected_sm_model ) self . __my_selected_sm_id = self . model . selected_state_machine_id if self . __my_selected_sm_id is not None : self . _selected_sm_model = self . model . state_machines [ self . __my_selected_sm_id ] self . observe_model ( self . _selected_sm_model ) 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 .
40,655
def selection_changed ( self , widget , event = None ) : if self . _state_which_is_updated : return super ( StateMachineTreeController , self ) . selection_changed ( widget , event )
Notify state machine about tree view selection
40,656
def redo_expansion_state ( self , ignore_not_existing_rows = False ) : def set_expansion_state ( state_path ) : state_row_iter = self . state_row_iter_dict_by_state_path [ state_path ] if state_row_iter : 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
40,657
def update ( self , changed_state_model = None , with_expand = False ) : if not self . view_is_registered : return if changed_state_model is None : 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 : 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 : changed_upper_state_m = changed_state_model . parent . parent else : changed_upper_state_m = changed_state_model . parent while changed_upper_state_m . state . get_path ( ) not in self . state_row_iter_dict_by_state_path : 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 ( ) ] 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
40,658
def show_content ( self , state_model ) : 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 .
40,659
def get_state_machine_selection ( self ) : 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
40,660
def execute ( self , state , inputs = None , outputs = None , backward_execution = False ) : 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
40,661
def _load_script ( self ) : 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
40,662
def build_module ( self ) : try : imp . acquire_lock ( ) module_name = os . path . splitext ( self . filename ) [ 0 ] + str ( self . _script_id ) 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 ) ) self . compiled_module = tmp_module finally : imp . release_lock ( )
Builds a temporary module from the script file
40,663
def _update_recently_opened_state_machines ( self ) : 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" , [ ] ) : 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 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 sm_open_function = partial ( self . on_open_activate , path = sm_path ) 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
40,664
def point_left_of_line ( point , line_start , line_end ) : 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
40,665
def point_on_line ( point , line_start , line_end , accuracy = 50. ) : 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
40,666
def point_in_triangle ( p , v1 , v2 , v3 ) : 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
40,667
def equal ( t1 , t2 , digit = None ) : 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
40,668
def cal_dist_between_2_coord_frame_aligned_boxes ( box1_pos , box1_size , box2_pos , box2_size ) : box1_x_min , box1_y_min = box1_pos box1_x_max , box1_y_max = ( box1_pos [ 0 ] + box1_size [ 0 ] , box1_pos [ 1 ] + box1_size [ 1 ] ) box2_x_min , box2_y_min = box2_pos box2_x_max , box2_y_max = ( box2_pos [ 0 ] + box2_size [ 0 ] , box2_pos [ 1 ] + box2_size [ 1 ] ) if box2_x_max < box1_x_min and box2_y_max < box1_y_min : distance = sqrt ( ( box1_x_min - box2_x_max ) ** 2 + ( box1_y_min - box2_y_max ) ** 2 ) elif box2_x_min > box1_x_max and box2_y_max < box1_y_min : distance = sqrt ( ( box2_x_min - box1_x_max ) ** 2 + ( box1_y_min - box2_y_max ) ** 2 ) elif box2_x_max < box1_x_min and box2_y_min > box1_y_max : distance = sqrt ( ( box1_x_min - box2_x_max ) ** 2 + ( box2_y_min - box1_y_max ) ** 2 ) elif box2_x_min > box1_x_max and box2_y_min > box1_y_max : distance = sqrt ( ( box2_x_min - box1_x_max ) ** 2 + ( box2_y_min - box1_y_max ) ** 2 ) elif box2_y_max < box1_y_min : distance = box1_y_min - box2_y_max elif box2_x_max < box1_x_min : distance = box1_x_min - box2_x_max elif box2_x_min > box1_x_max : distance = box2_x_min - box1_x_max elif box2_y_min > box1_y_max : distance = box2_y_min - box1_y_max else : distance = 0. return distance
Calculate Euclidean distance between two boxes those edges are parallel to the coordinate axis
40,669
def get_history_item_for_tree_iter ( self , child_tree_iter ) : history_item = self . history_tree_store [ child_tree_iter ] [ self . HISTORY_ITEM_STORAGE_ID ] if history_item is None : if self . history_tree_store . iter_n_children ( child_tree_iter ) > 0 : child_iter = self . history_tree_store . iter_nth_child ( child_tree_iter , 0 ) history_item = self . history_tree_store [ child_iter ] [ self . HISTORY_ITEM_STORAGE_ID ] else : logger . debug ( "In a dummy history should be respective real call element." ) return history_item
Hands history item for tree iter and compensate if tree item is a dummy item
40,670
def _store_expansion_state ( self ) : def store_tree_expansion ( child_tree_iter , expansion_state ) : tree_item_path = self . history_tree_store . get_path ( child_tree_iter ) history_item = self . get_history_item_for_tree_iter ( child_tree_iter ) if tree_item_path is not None : if history_item not in expansion_state : expansion_state [ history_item ] = self . history_tree . row_expanded ( tree_item_path ) for n in range ( self . history_tree_store . iter_n_children ( child_tree_iter ) ) : child_iter = self . history_tree_store . iter_nth_child ( child_tree_iter , n ) store_tree_expansion ( child_iter , expansion_state ) root_iter = self . history_tree_store . get_iter_first ( ) if not root_iter : return current_expansion_state = { } if not self . get_history_item_for_tree_iter ( root_iter ) . state_reference : return state_machine = self . get_history_item_for_tree_iter ( root_iter ) . state_reference . get_state_machine ( ) self . _expansion_state [ state_machine . state_machine_id ] = current_expansion_state while root_iter : store_tree_expansion ( root_iter , current_expansion_state ) root_iter = self . history_tree_store . iter_next ( root_iter )
Iter recursively all tree items and store expansion state
40,671
def _restore_expansion_state ( self ) : def restore_tree_expansion ( child_tree_iter , expansion_state ) : tree_item_path = self . history_tree_store . get_path ( child_tree_iter ) history_item = self . get_history_item_for_tree_iter ( child_tree_iter ) if tree_item_path and history_item in expansion_state : if expansion_state [ history_item ] : self . history_tree . expand_to_path ( tree_item_path ) for n in range ( self . history_tree_store . iter_n_children ( child_tree_iter ) ) : child_iter = self . history_tree_store . iter_nth_child ( child_tree_iter , n ) restore_tree_expansion ( child_iter , expansion_state ) root_iter = self . history_tree_store . get_iter_first ( ) if not root_iter : return state_machine = self . get_history_item_for_tree_iter ( root_iter ) . state_reference . get_state_machine ( ) if state_machine . state_machine_id not in self . _expansion_state : return while root_iter : restore_tree_expansion ( root_iter , self . _expansion_state [ state_machine . state_machine_id ] ) root_iter = self . history_tree_store . iter_next ( root_iter )
Iter recursively all tree items and restore expansion state
40,672
def notification_selected_sm_changed ( self , model , prop_name , info ) : selected_state_machine_id = self . model . selected_state_machine_id if selected_state_machine_id is None : return self . update ( )
If a new state machine is selected make sure expansion state is stored and tree updated
40,673
def notification_sm_changed ( self , model , prop_name , info ) : for state_machine_id in list ( self . _expansion_state . keys ( ) ) : if state_machine_id not in self . model . state_machines : del self . _expansion_state [ state_machine_id ]
Remove references to non - existing state machines
40,674
def execution_history_focus ( self , model , prop_name , info ) : if state_machine_execution_engine . status . execution_mode in [ StateMachineExecutionStatus . STARTED , StateMachineExecutionStatus . STOPPED , StateMachineExecutionStatus . FINISHED ] : if self . parent is not None and hasattr ( self . parent , "focus_notebook_page_of_controller" ) : self . parent . focus_notebook_page_of_controller ( self ) if state_machine_execution_engine . status . execution_mode is not StateMachineExecutionStatus . STARTED : if not self . model . selected_state_machine_id == self . model . state_machine_manager . active_state_machine_id : pass else : self . update ( )
Arranges to put execution - history widget page to become top page in notebook when execution starts and stops and resets the boolean of modification_history_was_focused to False each time this notification are observed .
40,675
def clean_history ( self , widget , event = None ) : self . history_tree_store . clear ( ) selected_sm_m = self . model . get_selected_state_machine_model ( ) if selected_sm_m : if state_machine_execution_engine . finished_or_stopped ( ) : selected_sm_m . state_machine . destroy_execution_histories ( ) self . update ( )
Triggered when the Clean History button is clicked .
40,676
def insert_history_item ( self , parent , history_item , description , dummy = False ) : if not history_item . state_reference : logger . error ( "This must never happen! Current history_item is {}" . format ( history_item ) ) return None content = None if global_gui_config . get_config_value ( "SHOW_PATH_NAMES_IN_EXECUTION_HISTORY" , False ) : content = ( history_item . state_reference . name + " - " + history_item . state_reference . get_path ( ) + " - " + description , None if dummy else history_item , None if dummy else self . TOOL_TIP_TEXT ) else : content = ( history_item . state_reference . name + " - " + description , None if dummy else history_item , None if dummy else self . TOOL_TIP_TEXT ) tree_item = self . history_tree_store . insert_before ( parent , None , content ) return tree_item
Enters a single history item into the tree store
40,677
def insert_execution_history ( self , parent , execution_history , is_root = False ) : current_parent = parent execution_history_iterator = iter ( execution_history ) for history_item in execution_history_iterator : if isinstance ( history_item , ConcurrencyItem ) : self . insert_concurrent_execution_histories ( current_parent , history_item . execution_histories ) elif isinstance ( history_item , CallItem ) : tree_item = self . insert_history_item ( current_parent , history_item , "Enter" if is_root else "Call" ) if not tree_item : return if history_item . call_type is CallType . EXECUTE : next_history_item = history_item . next if next_history_item and next_history_item . call_type is CallType . CONTAINER : current_parent = tree_item self . insert_history_item ( current_parent , next_history_item , "Enter" ) try : next ( execution_history_iterator ) except StopIteration as e : return else : if current_parent is None : logger . error ( "Invalid execution history: current_parent is None" ) return if history_item . call_type is CallType . EXECUTE : self . insert_history_item ( current_parent , history_item , "Return" ) else : self . insert_history_item ( current_parent , history_item , "Exit" ) current_parent = self . history_tree_store . iter_parent ( current_parent ) is_root = False
Insert a list of history items into a the tree store
40,678
def insert_concurrent_execution_histories ( self , parent , concurrent_execution_histories ) : for execution_history in concurrent_execution_histories : if len ( execution_history ) >= 1 : first_history_item = execution_history [ 0 ] tree_item = self . insert_history_item ( parent , first_history_item , "Concurrency Branch" , dummy = True ) self . insert_execution_history ( tree_item , execution_history )
Adds the child execution histories of a concurrency state .
40,679
def select_open_state_machine_of_selected_library_element ( self ) : ( model , row_path ) = self . view . get_selection ( ) . get_selected ( ) if row_path : physical_library_path = model [ row_path ] [ self . ITEM_STORAGE_ID ] smm = gui_singletons . state_machine_manager_model . state_machine_manager sm = smm . get_open_state_machine_of_file_system_path ( physical_library_path ) if sm : gui_singletons . state_machine_manager_model . selected_state_machine_id = sm . state_machine_id
Select respective state machine of selected library in state machine manager if already open
40,680
def menu_item_remove_libraries_or_root_clicked ( self , menu_item ) : menu_item_text = self . get_menu_item_text ( menu_item ) logger . info ( "Delete item '{0}' pressed." . format ( menu_item_text ) ) model , path = self . view . get_selection ( ) . get_selected ( ) if path : tree_m_row = self . tree_store [ path ] library_os_path , library_path , library_name , item_key = self . extract_library_properties_from_selected_row ( ) library_file_system_path = library_os_path if "root" in menu_item_text : button_texts = [ menu_item_text + "from tree and config" , "Cancel" ] partial_message = "This will remove the library root from your configuration (config.yaml)." else : button_texts = [ menu_item_text , "Cancel" ] partial_message = "This folder will be removed from hard drive! You really wanna do that?" message_string = "You choose to {2} with " "\n\nlibrary tree path: {0}" "\n\nphysical path: {1}.\n\n\n" "{3}" "" . format ( os . path . join ( self . convert_if_human_readable ( tree_m_row [ self . LIB_PATH_STORAGE_ID ] ) , item_key ) , library_file_system_path , menu_item_text . lower ( ) , partial_message ) width = 8 * len ( "physical path: " + library_file_system_path ) dialog = RAFCONButtonDialog ( message_string , button_texts , message_type = Gtk . MessageType . QUESTION , parent = self . get_root_window ( ) , width = min ( width , 1400 ) ) response_id = dialog . run ( ) dialog . destroy ( ) if response_id == 1 : if "root" in menu_item_text : logger . info ( "Remove library root key '{0}' from config." . format ( item_key ) ) from rafcon . gui . singleton import global_config library_paths = global_config . get_config_value ( 'LIBRARY_PATHS' ) del library_paths [ tree_m_row [ self . LIB_KEY_STORAGE_ID ] ] global_config . save_configuration ( ) self . model . library_manager . refresh_libraries ( ) elif "libraries" in menu_item_text : logger . debug ( "Remove of all libraries in {} is triggered." . format ( library_os_path ) ) import shutil shutil . rmtree ( library_os_path ) self . model . library_manager . refresh_libraries ( ) else : logger . debug ( "Remove of Library {} is triggered." . format ( library_os_path ) ) self . model . library_manager . remove_library_from_file_system ( library_path , library_name ) elif response_id in [ 2 , - 4 ] : pass else : logger . warning ( "Response id: {} is not considered" . format ( response_id ) ) return True return False
Removes library from hard drive after request second confirmation
40,681
def extract_library_properties_from_selected_row ( self ) : ( model , row ) = self . view . get_selection ( ) . get_selected ( ) tree_item_key = model [ row ] [ self . ID_STORAGE_ID ] library_item = model [ row ] [ self . ITEM_STORAGE_ID ] library_path = model [ row ] [ self . LIB_PATH_STORAGE_ID ] if isinstance ( library_item , dict ) : os_path = model [ row ] [ self . OS_PATH_STORAGE_ID ] return os_path , None , None , tree_item_key assert isinstance ( library_item , string_types ) library_os_path = library_item library_name = library_os_path . split ( os . path . sep ) [ - 1 ] return library_os_path , library_path , library_name , tree_item_key
Extracts properties library_os_path library_path library_name and tree_item_key from tree store row
40,682
def _get_selected_library_state ( self ) : library_os_path , library_path , library_name , item_key = self . extract_library_properties_from_selected_row ( ) if library_path is None : return None logger . debug ( "Link library state '{0}' (with library tree path: {2} and file system path: {1}) into state " "machine." . format ( str ( item_key ) , library_os_path , self . convert_if_human_readable ( str ( library_path ) ) + "/" + str ( item_key ) ) ) library_name = library_os_path . split ( os . path . sep ) [ - 1 ] return LibraryState ( library_path , library_name , "0.1" , format_folder_name_human_readable ( library_name ) )
Returns the LibraryState which was selected in the LibraryTree
40,683
def start ( self , execution_history , backward_execution = False , generate_run_id = True ) : self . execution_history = execution_history if generate_run_id : self . _run_id = run_id_generator ( ) self . backward_execution = copy . copy ( backward_execution ) self . thread = threading . Thread ( target = self . run ) self . thread . start ( )
Starts the execution of the state in a new thread .
40,684
def join ( self ) : if self . thread : self . thread . join ( ) self . thread = None else : logger . debug ( "Cannot join {0}, as the state hasn't been started, yet or is already finished!" . format ( self ) )
Waits until the state finished execution .
40,685
def recursively_preempt_states ( self ) : self . preempted = True self . paused = False self . started = False
Preempt the state
40,686
def get_default_input_values_for_state ( self , state ) : from rafcon . core . states . library_state import LibraryState result_dict = { } for input_port_key , value in state . input_data_ports . items ( ) : if isinstance ( state , LibraryState ) : if state . use_runtime_value_input_data_ports [ input_port_key ] : default = state . input_data_port_runtime_values [ input_port_key ] else : default = value . default_value else : default = value . default_value if isinstance ( default , string_types ) and len ( default ) > 0 and default [ 0 ] == '$' : from rafcon . core . singleton import global_variable_manager as gvm var_name = default [ 1 : ] if not gvm . variable_exist ( var_name ) : logger . error ( "The global variable '{0}' does not exist" . format ( var_name ) ) global_value = None else : global_value = gvm . get_variable ( var_name ) result_dict [ value . name ] = global_value else : result_dict [ value . name ] = copy . copy ( default ) return result_dict
Computes the default input values for a state
40,687
def create_output_dictionary_for_state ( state ) : from rafcon . core . states . library_state import LibraryState result_dict = { } for key , data_port in state . output_data_ports . items ( ) : if isinstance ( state , LibraryState ) and state . use_runtime_value_output_data_ports [ key ] : result_dict [ data_port . name ] = copy . copy ( state . output_data_port_runtime_values [ key ] ) else : result_dict [ data_port . name ] = copy . copy ( data_port . default_value ) return result_dict
Return empty output dictionary for a state
40,688
def add_input_data_port ( self , name , data_type = None , default_value = None , data_port_id = None ) : if data_port_id is None : data_port_id = generate_data_port_id ( self . get_data_port_ids ( ) ) self . _input_data_ports [ data_port_id ] = InputDataPort ( name , data_type , default_value , data_port_id , self ) valid , message = self . _check_data_port_name ( self . _input_data_ports [ data_port_id ] ) if not valid : self . _input_data_ports [ data_port_id ] . parent = None del self . _input_data_ports [ data_port_id ] raise ValueError ( message ) return data_port_id
Add a new input data port to the state .
40,689
def remove_input_data_port ( self , data_port_id , force = False , destroy = True ) : if data_port_id in self . _input_data_ports : if destroy : self . remove_data_flows_with_data_port_id ( data_port_id ) self . _input_data_ports [ data_port_id ] . parent = None return self . _input_data_ports . pop ( data_port_id ) else : raise AttributeError ( "input data port with name %s does not exit" , data_port_id )
Remove an input data port from the state
40,690
def remove_data_flows_with_data_port_id ( self , data_port_id ) : if not self . is_root_state : 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 )
Remove all data flows whose from_key or to_key equals the passed data_port_id
40,691
def add_output_data_port ( self , name , data_type , default_value = None , data_port_id = None ) : if data_port_id is None : data_port_id = generate_data_port_id ( self . get_data_port_ids ( ) ) self . _output_data_ports [ data_port_id ] = OutputDataPort ( name , data_type , default_value , data_port_id , self ) valid , message = self . _check_data_port_name ( self . _output_data_ports [ data_port_id ] ) if not valid : self . _output_data_ports [ data_port_id ] . parent = None del self . _output_data_ports [ data_port_id ] raise ValueError ( message ) return data_port_id
Add a new output data port to the state
40,692
def remove_output_data_port ( self , data_port_id , force = False , destroy = True ) : if data_port_id in self . _output_data_ports : if destroy : self . remove_data_flows_with_data_port_id ( data_port_id ) self . _output_data_ports [ data_port_id ] . parent = None return self . _output_data_ports . pop ( data_port_id ) else : raise AttributeError ( "output data port with name %s does not exit" , data_port_id )
Remove an output data port from the state
40,693
def get_io_data_port_id_from_name_and_type ( self , name , data_port_type ) : if data_port_type is InputDataPort : for ip_id , output_port in self . input_data_ports . items ( ) : if output_port . name == name : return ip_id raise AttributeError ( "Name '{0}' is not in input_data_ports" . format ( name ) ) elif data_port_type is OutputDataPort : for op_id , output_port in self . output_data_ports . items ( ) : if output_port . name == name : return op_id if name == "error" : return raise AttributeError ( "Name '{0}' is not in output_data_ports" . format ( name ) )
Returns the data_port_id of a data_port with a certain name and data port type
40,694
def get_path ( self , appendix = None , by_name = False ) : if by_name : state_identifier = self . name else : state_identifier = self . state_id if not self . is_root_state : if appendix is None : return self . parent . get_path ( state_identifier , by_name ) else : return self . parent . get_path ( state_identifier + PATH_SEPARATOR + appendix , by_name ) else : if appendix is None : return state_identifier else : return state_identifier + PATH_SEPARATOR + appendix
Recursively create the path of the state .
40,695
def get_storage_path ( self , appendix = None ) : state_identifier = storage . get_storage_id_for_state ( self ) if not self . is_root_state : if appendix is None : return self . parent . get_storage_path ( state_identifier ) else : return self . parent . get_storage_path ( state_identifier + PATH_SEPARATOR + appendix ) else : if appendix is None : return state_identifier else : return state_identifier + PATH_SEPARATOR + appendix
Recursively create the storage path of the state .
40,696
def get_state_machine ( self ) : if self . parent : if self . is_root_state : return self . parent else : return self . parent . get_state_machine ( ) return None
Get a reference of the state_machine the state belongs to
40,697
def file_system_path ( self , file_system_path ) : if not isinstance ( file_system_path , string_types ) : raise TypeError ( "file_system_path must be a string" ) self . _file_system_path = file_system_path
Setter for file_system_path attribute of state
40,698
def add_outcome ( self , name , outcome_id = None ) : if outcome_id is None : outcome_id = generate_outcome_id ( list ( self . outcomes . keys ( ) ) ) if name in self . _outcomes : logger . error ( "Two outcomes cannot have the same names" ) return if outcome_id in self . outcomes : logger . error ( "Two outcomes cannot have the same outcome_ids" ) return outcome = Outcome ( outcome_id , name , self ) self . _outcomes [ outcome_id ] = outcome return outcome_id
Add a new outcome to the state
40,699
def remove_outcome ( self , outcome_id , force = False , destroy = True ) : if outcome_id not in self . outcomes : raise AttributeError ( "There is no outcome_id %s" % str ( outcome_id ) ) if not force : if outcome_id == - 1 or outcome_id == - 2 : raise AttributeError ( "You cannot remove the outcomes with id -1 or -2 as a state must always be able" "to return aborted or preempted" ) self . remove_outcome_hook ( outcome_id ) if destroy and not self . is_root_state : for transition_id , transition in self . parent . transitions . items ( ) : if transition . from_outcome == outcome_id and transition . from_state == self . state_id : self . parent . remove_transition ( transition_id ) break self . _outcomes [ outcome_id ] . parent = None return self . _outcomes . pop ( outcome_id )
Remove an outcome from the state