def find_free_and_valid_data_flows(self, depend_to_state_id=None): # print "\n internal from %s \n\n internal to %s" % (self.free_to_port_internal, self.from_port_internal) internal_data_flows = [] if self.free_to_port_internal and self.from_port_internal: for from_state_id, elems in self.from_port_internal.iteritems(): # print "\n\nfrom_state %s and ports %s" % (from_state_id, [(elem.name, elem.data_type) for elem in elems]) for from_port in elems: for to_state_id, elems in self.free_to_port_internal.iteritems(): # print "\nto_state %s and ports %s" % (to_state_id, [(elem.name, elem.data_type) for elem in elems]) for to_port in elems: if type_helpers.type_inherits_of_type(from_port.data_type, to_port.data_type): if depend_to_state_id is None or depend_to_state_id in [from_state_id, to_state_id]: internal_data_flows.append((from_state_id, from_port.data_port_id, to_state_id, to_port.data_port_id)) # print "\n\n\n" + 60*"-" + "\n external from %s \n\n external to %s" % (self.free_to_port_external, self.from_port_external) external_data_flows = [] if self.free_to_port_external and self.from_port_external: for from_state_id, elems in self.from_port_external.iteritems(): # print "\n\nfrom_state %s and ports %s" % (from_state_id, [(elem.name, elem.data_type) for elem in elems]) for from_port in elems: for to_state_id, elems in self.free_to_port_external.iteritems(): # print "\nto_state %s and ports %s" % (to_state_id, [(elem.name, elem.data_type) for elem in elems]) for to_port in elems: if type_helpers.type_inherits_of_type(from_port.data_type, to_port.data_type): if depend_to_state_id is None or depend_to_state_id in [from_state_id, to_state_id]: external_data_flows.append((from_state_id, from_port.data_port_id, to_state_id, to_port.data_port_id)) return internal_data_flows, external_data_flows
def change_data_type(self, data_type, default_value=None): """This method changes both the data type and default value. If one of the parameters does not fit, an exception is thrown and no property is changed. Using this method ensures a consistent data type and default value and only notifies once. :param data_type: The new data type :param default_value: The new default value :return: """ old_data_type = self.data_type self.data_type = data_type if default_value is None: default_value = self.default_value if type_helpers.type_inherits_of_type(type(default_value), self._data_type): self._default_value = default_value else: if old_data_type.__name__ == "float" and data_type == "int": if self.default_value: self._default_value = int(default_value) else: self._default_value = 0 elif old_data_type.__name__ == "int" and data_type == "float": if self.default_value: self._default_value = float(default_value) else: self._default_value = 0.0 else: self._default_value = None
def add_missing_model(self, model_list_or_dict, core_elements_dict, model_name, model_class, model_key): """Adds one missing model The method will search for the first core-object out of core_object_dict not represented in the list or dict of models handed by model_list_or_dict, adds it and returns without continue to search for more objects which maybe are missing in model_list_or_dict with respect to the core_object_dict. :param model_list_or_dict: could be a list or dictionary of one model type :param core_elements_dict: dictionary of one type of core-elements (rafcon.core) :param model_name: prop_name for the core-element hold by the model, this core-element is covered by the model :param model_class: model-class of the elements that should be insert :param model_key: if model_list_or_dict is a dictionary the key is the id of the respective element (e.g. 'state_id') :return: True, is a new model was added, False else :rtype: bool """ def core_element_has_model(core_object): for model_or_key in model_list_or_dict: model = model_or_key if model_key is None else model_list_or_dict[ model_or_key] if core_object is getattr(model, model_name): return True return False if model_name == "income": self._add_model(self.income, self.state.income, IncomeModel) return for core_element in core_elements_dict.values(): if core_element_has_model(core_element): continue # get expected model and connect it to self or create a new model new_model = self._get_future_expected_model(core_element) if new_model: new_model.parent = self else: if type_helpers.type_inherits_of_type(model_class, StateModel): new_model = model_class( core_element, self, expected_future_models=self.expected_future_models) self.expected_future_models = new_model.expected_future_models # update reused models new_model.expected_future_models = set( ) # clean the field because should not be used further else: new_model = model_class(core_element, self) # insert new model into list or dict if model_key is None: model_list_or_dict.append(new_model) else: model_list_or_dict[getattr(core_element, model_key)] = new_model return True return False
def value(self, value): # check for primitive data types if value is not None and not type_helpers.type_inherits_of_type( type(value), self.value_type): raise TypeError( "Result must by of type '{0}'. Given: '{1}' with type '{2}'". format(self.value_type, value, type(value))) self._timestamp = generate_time_stamp() self._value = value
def check_value_and_type(value, data_type): """Checks if a given value is of a specific type :param value: the value to check :param data_type: the type to be checked upon :return: """ if value is not None and data_type is not type(None): if not type_inherits_of_type(data_type, type(value)): raise TypeError( "Value: '{0}' is not of data type: '{1}', value type: {2}". format(value, data_type, type(value)))
def value(self, value): # check for primitive data types if value is not None and not type_helpers.type_inherits_of_type(type(value), self.value_type): raise TypeError("Result must by of type '{0}'. Given: '{1}' with type '{2}'".format( self.value_type, value, type(value) )) # if value is not None and str(type(value).__name__) != self._value_type: # print("types", value, str(type(value).__name__), self._value_type) # #check for classes # if not isinstance(value, getattr(sys.modules[__name__], self._value_type)): # raise TypeError("result must be of type %s" % str(self._value_type)) self._timestamp = generate_time_stamp() # print("new scope data update {0}: {1} t:{2}".format(self.name+self.from_state, self._value, self._timestamp)) self._value = value
def add_if_valid(future_data_flow_parent, from_port, to_port, data_flow_list): from_state_id = from_port.parent.state_id to_state_id = to_port.parent.state_id # check that possible data flow would have consistent data types if not (type_helpers.type_inherits_of_type(from_port.data_type, to_port.data_type)): return # check that possible data flow depend to right state if not (depend_to_state_id is None or depend_to_state_id in [from_state_id, to_state_id]): return # check that possible data port is not loop if not (from_state_id != to_state_id or from_state_id == to_state_id and from_port.data_port_id != to_port.data_port_id): return # check if data flow already exists exists = any([from_state_id == df.from_state and from_port.data_port_id == df.from_key and to_state_id == df.to_state and to_port.data_port_id == df.to_key for df in future_data_flow_parent.data_flows.values()]) if exists: return # add valid data flow data_flow_list.append((from_state_id, from_port.data_port_id, to_state_id, to_port.data_port_id))