def _generate_actions(self, ref, value_ref): """ Generate actions for the given value_ref """ actions = [] definition = ref.value.__class__.__definition__ attr = definition.get_attribute(self.attribute_name) if attr is None: raise Exception("Attribute '%s' is not defined for entity '%s'" % (self.attribute_name, definition.name)) if hasattr(attr, "end"): # this side if attr.low == 1 and attr.high == 1: actions.append(("set", AttributeVariable.create(ref, self.attribute_name))) else: actions.append(("add", AttributeVariable.create(ref, self.attribute_name))) # the other side if attr.end.low == 1 and attr.end.high == 1: actions.append(("set", AttributeVariable.create(value_ref, attr.end.name))) else: actions.append(("add", AttributeVariable.create(value_ref, attr.end.name))) else: # there is only this side actions.append(("set", AttributeVariable.create(ref, self.attribute_name))) return actions
def evaluate(self, state, local_scope): """ Retrieve the attribute value """ instance_ref = state.get_ref("instance") value = getattr(instance_ref.value, self.attribute_name) state.graph.add_alias(AttributeVariable.create(instance_ref, self.attribute_name), value) return value
def _pre_process_action(self, actions, op, obj): """ Do some preprocessing on actions """ if hasattr(obj, "instance") and hasattr(obj.instance, "instance"): # unpack nested attributes instance = Variable(obj.instance.value) actions.append((op, AttributeVariable.create(instance, obj.attribute))) else: actions.append((op, obj))
def create_var_ref(self, node): """ Create a variable reference """ to_list = [str(x.text) for x in node.children[0].children] ref = Reference(str(node.children[1].text), to_list) ref.line = node.children[1].line if len(node.children[2].children) > 0: var = ref for attr in node.children[2].children: var = AttributeVariable.create(var, str(attr.text)) var.line = attr.line return var else: return ref
def actions(self, state): """ @see DynamicStatement#actions """ type_class = state.get_type("classtype") object_ref = state.get_result_reference() actions = [("add", Variable(type_class)), ("set", object_ref)] if isinstance(type_class, Default): type_class = type_class.get_entity() for attribute_name in type_class.get_all_attribute_names(): # Set a attributes with low multiplicity == 0 -> set [] attribute_obj = type_class.get_attribute(attribute_name) if hasattr(attribute_obj, "low") and attribute_obj.low == 0: actions.append(("add", AttributeVariable.create(object_ref, attribute_name))) return actions
def evaluate_statement(self, statement): """ Evaluate the given statement """ try: statement.evaluate() return True except UnsetException as exception: # This statement tried to access an attribute that was not set. Add # a "get" action for this variable. attr_var = AttributeVariable.create(Variable(exception.instance), exception.attribute) self._graph.add_actions(statement, [("get", attr_var)]) except Exception as exception: self.show_exception(statement, exception) return False
def create_string_format(self, format_string, variables): """ Create a string interpolation statement """ _vars = [] for var_str in variables: var_parts = var_str[1].split(".") ref = Reference(var_parts[0], []) if len(var_parts) > 1: var = ref for attr in var_parts[1:]: var = AttributeVariable.create(var, attr) _vars.append((var, var_str[0])) else: _vars.append((ref, var_str[0])) return StringFormat(format_string, _vars)
def actions(self, state): """ @see DynamicStatement#actions """ instance_ref = state.get_ref("instance") return [("get", AttributeVariable.create(instance_ref, self.attribute_name))]
def new_statements(self, state): """ Add any arguments that need to be validated to the graph """ attributes = set() # Set the value from the constructor object_ref = state.get_result_reference() for name, value in self.__attributes.items(): # set the attributes passed with the constructor stmt = SetAttribute(object_ref, name, value) self.copy_location(stmt) stmt.namespace = self.namespace state.add_statement(stmt) attributes.add(name) # Set values defined in default constructors type_class = state.get_type("classtype") if isinstance(type_class, Default): default = type_class type_class = type_class.get_entity() # set default values for attribute_name in type_class.get_all_attribute_names(): attribute = type_class.get_attribute(attribute_name) if attribute.name not in attributes: try: value = default.get_default(attribute.name) stmt = SetAttribute(object_ref, attribute.name, value) self.copy_location(stmt) stmt.namespace = self.namespace state.add_statement(stmt) attributes.add(attribute.name) except AttributeError: pass # Set default values if they have not been set yet for name, value in type_class.get_default_values().items(): if name not in attributes and value is not None: stmt = SetAttribute(object_ref, name, value) self.copy_location(stmt) stmt.namespace = self.namespace state.add_statement(stmt) # Make values of attributes available in subscopes by defining # variables with matching names in the subscope object_id = Scope.object_to_name(state) namespace = Namespace(object_id, state.namespace) scope = Scope.get_or_create_scope(state.graph, namespace.to_path()) added_variables = set() for attribute in type_class.get_all_attribute_names(): if attribute in added_variables: continue var = AttributeVariable.create(object_ref, attribute) self.copy_location(var) added_variables.add(attribute) scope.add_variable(attribute, var) # Set a attributes with low multiplicity == 0 -> set [] attribute_obj = type_class.get_attribute(attribute) if hasattr(attribute_obj, "low") and attribute_obj.low == 0: value = Variable(QList()) stmt = SetAttribute(object_ref, attribute_obj.name, value) self.copy_location(stmt) stmt.namespace = self.namespace state.add_statement(stmt) # set the self variable scope.add_variable("self", object_ref) # now check that all variables that have indexes on them, are already # defined and add the instance to the index for index in type_class._index_def: for attr in index: if attr not in attributes: raise Exception("%s is part of an index and should be set in the constructor." % attr)