Ejemplo n.º 1
0
 def create_assign(self, node):
     """
         Create an assignment
     """
     rhs = self._handle_node(node.children[1])
     var = self._handle_node(node.children[0])
     
     if isinstance(var, AttributeVariable):
         stmt = SetAttribute(var.instance, var.attribute, rhs)
     else:
         stmt = Assign(var, rhs)
 
     stmt.line = var.line
     return stmt
Ejemplo n.º 2
0
    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)