コード例 #1
0
 def __init__(
     self,
     entity_name: LocatableString,
     implementations: List[LocatableString],
     select: ExpressionStatement,
     inherit: bool = False,
     comment: LocatableString = None,
 ) -> None:
     DefinitionStatement.__init__(self)
     self.entity = entity_name
     self.entity_location = entity_name.get_location()
     self.implementations = implementations
     self.anchors = [
         TypeReferenceAnchor(x.namespace, x) for x in implementations
     ]
     self.anchors.append(
         TypeReferenceAnchor(entity_name.namespace, entity_name))
     self.anchors.extend(select.get_anchors())
     self.location = entity_name.get_location()
     if inherit and (not isinstance(select, Literal)
                     or select.value is not True):
         raise RuntimeException(
             self, "Conditional implementation with parents not allowed")
     self.select = select
     self.inherit: bool = inherit
     if comment is not None:
         self.comment = str(comment)
     else:
         self.comment = None
コード例 #2
0
ファイル: define.py プロジェクト: inmanta/inmanta-core
    def __init__(
        self,
        namespace: Namespace,
        name: LocatableString,
        target_type: LocatableString,
        statements: BasicBlock,
        comment: LocatableString,
    ):
        TypeDefinitionStatement.__init__(self, namespace, str(name))
        self.name = str(name)
        if "-" in self.name:
            inmanta_warnings.warn(HyphenDeprecationWarning(name))

        self.block = statements
        self.entity = target_type

        self.comment = None
        if comment is not None:
            self.comment = str(comment)

        self.location = name.get_location()

        self.type = Implementation(str(self.name), self.block, self.namespace, str(target_type), self.comment)
        self.type.location = name.get_location()
        self.anchors = [TypeReferenceAnchor(namespace, target_type)]
        self.anchors.extend(statements.get_anchors())
コード例 #3
0
ファイル: define.py プロジェクト: inmanta/inmanta-core
    def __init__(
        self,
        namespace: Namespace,
        lname: LocatableString,
        comment: Optional[LocatableString],
        parents: List[LocatableString],
        attributes: List[DefineAttribute],
    ) -> None:
        name = str(lname)
        TypeDefinitionStatement.__init__(self, namespace, name)
        if "-" in name:
            inmanta_warnings.warn(HyphenDeprecationWarning(lname))

        self.anchors = [TypeReferenceAnchor(namespace, x) for x in parents]

        self.name = name
        self.attributes = attributes
        if comment is not None:
            self.comment = str(comment)
        else:
            self.comment = None

        self.parents = parents

        if len(self.parents) == 0 and not (self.name == "Entity" and self.namespace.name == "std"):
            dummy_location: Range = Range("__internal__", 1, 1, 1, 1)
            self.parents.append(LocatableString("std::Entity", dummy_location, -1, namespace))

        self.type = Entity(self.name, namespace, self.comment)
        self.type.location = lname.location
コード例 #4
0
ファイル: define.py プロジェクト: inmanta/inmanta-core
 def __init__(self, entity_type: LocatableString, attributes: List[LocatableString]):
     DefinitionStatement.__init__(self)
     self.type = entity_type
     self.attributes = [str(a) for a in attributes]
     self.anchors.append(TypeReferenceAnchor(entity_type.namespace, entity_type))
     self.anchors.extend(
         [AttributeReferenceAnchor(x.get_location(), entity_type.namespace, entity_type, str(x)) for x in attributes]
     )
コード例 #5
0
    def __init__(self,
                 left: Relationside,
                 right: Relationside,
                 annotations: List[ExpressionStatement] = []) -> None:
        DefinitionStatement.__init__(self)
        # for later evaluation
        self.annotation_expression = [(ResultVariable(), exp)
                                      for exp in annotations]
        # for access to results
        self.annotations = [exp[0] for exp in self.annotation_expression]

        self.anchors.extend((y for x in annotations for y in x.get_anchors()))
        self.anchors.append(TypeReferenceAnchor(left[0].namespace, left[0]))
        self.anchors.append(TypeReferenceAnchor(right[0].namespace, right[0]))

        self.left: Relationside = left
        self.right: Relationside = right

        self.comment = None
コード例 #6
0
ファイル: assign.py プロジェクト: inmanta/inmanta-core
 def __init__(
     self,
     index_type: LocatableString,
     query: typing.List[typing.Tuple[LocatableString, ExpressionStatement]],
     wrapped_query: typing.List["WrappedKwargs"],
 ) -> None:
     ReferenceStatement.__init__(self, list(chain((v for (_, v) in query), wrapped_query)))
     self.index_type = index_type
     self.anchors.append(TypeReferenceAnchor(index_type.namespace, index_type))
     self.query = [(str(n), e) for n, e in query]
     self.wrapped_query: typing.List["WrappedKwargs"] = wrapped_query
コード例 #7
0
ファイル: define.py プロジェクト: inmanta/inmanta-core
 def __init__(
     self, namespace: Namespace, name: LocatableString, basetype: LocatableString, expression: ExpressionStatement
 ) -> None:
     TypeDefinitionStatement.__init__(self, namespace, str(name))
     self.set_location(name.get_location())
     self.basetype = basetype
     self.anchors.append(TypeReferenceAnchor(namespace, basetype))
     self.anchors.extend(expression.get_anchors())
     self.set_expression(expression)
     self.type = ConstraintType(self.namespace, str(name))
     self.type.location = name.get_location()
     self.comment = None
     if self.name in TYPES:
         inmanta_warnings.warn(CompilerRuntimeWarning(self, "Trying to override a built-in type: %s" % self.name))
     if "-" in self.name:
         inmanta_warnings.warn(HyphenDeprecationWarning(name))
コード例 #8
0
ファイル: generator.py プロジェクト: inmanta/inmanta-core
    def __init__(
        self,
        class_type: LocatableString,
        attributes: List[Tuple[LocatableString, ExpressionStatement]],
        wrapped_kwargs: List["WrappedKwargs"],
        location: Location,
        namespace: Namespace,
    ) -> None:
        super().__init__()
        self.class_type = class_type
        self.__attributes = {}  # type: Dict[str,ExpressionStatement]
        self.__wrapped_kwarg_attributes: List[WrappedKwargs] = wrapped_kwargs
        self.location = location
        self.namespace = namespace
        self.anchors.append(TypeReferenceAnchor(namespace, class_type))
        for a in attributes:
            self.add_attribute(a[0], a[1])
        self.type: Optional["EntityLike"] = None
        self.required_kwargs: List[str] = []

        self._direct_attributes = {}  # type: Dict[str,ExpressionStatement]
        self._indirect_attributes = {}  # type: Dict[str,ExpressionStatement]
コード例 #9
0
 def __init__(
     self,
     name: LocatableString,
     arguments: List[ExpressionStatement],
     kwargs: List[Tuple[LocatableString, ExpressionStatement]],
     wrapped_kwargs: List[WrappedKwargs],
     location: Location,
     namespace: Namespace,
 ) -> None:
     ReferenceStatement.__init__(self, list(chain(arguments, (v for _, v in kwargs), wrapped_kwargs)))
     self.name: LocatableString = name
     self.arguments: List[ExpressionStatement] = arguments
     self.wrapped_kwargs: List[WrappedKwargs] = wrapped_kwargs
     self.location: Location = location
     self.namespace: Namespace = namespace
     self.anchors = [TypeReferenceAnchor(self.namespace, self.name)]
     self.kwargs: Dict[str, ExpressionStatement] = {}
     for loc_name, expr in kwargs:
         arg_name: str = str(loc_name)
         if arg_name in self.kwargs:
             raise RuntimeException(self, "Keyword argument %s repeated in function call %s()" % (arg_name, self.name))
         self.kwargs[arg_name] = expr
     self.function: Optional[Function] = None
コード例 #10
0
ファイル: define.py プロジェクト: inmanta/inmanta-core
    def evaluate(self) -> None:
        """
        Evaluate this statement.
        """
        try:
            entity_type = self.type
            entity_type.comment = self.comment

            add_attributes: Dict[str, Attribute] = {}
            attribute: DefineAttribute
            for attribute in self.attributes:
                attr_type: Type = attribute.type.get_type(self.namespace)
                if not isinstance(attr_type, (Type, type)):
                    raise TypingException(self, "Attributes can only be a type. Entities need to be defined as relations.")

                name = str(attribute.name)
                attr_obj = Attribute(
                    entity_type,
                    attribute.type.get_basetype(self.namespace),
                    name,
                    attribute.get_location(),
                    attribute.type.multi,
                    attribute.type.nullable,
                )
                self.anchors.append(TypeReferenceAnchor(self.namespace, attribute.type.basetype))

                if name in add_attributes:
                    raise DuplicateException(attr_obj, add_attributes[name], "Same attribute defined twice in one entity")

                add_attributes[name] = attr_obj

                if attribute.default is not None or attribute.remove_default:
                    entity_type.add_default_value(name, attribute)

            if len({str(p) for p in self.parents}) != len(self.parents):
                raise TypingException(self, "same parent defined twice")
            for parent in self.parents:
                parent_type = self.namespace.get_type(parent)
                if parent_type is self.type:
                    raise TypingException(self, "Entity can not be its own parent (%s) " % parent)
                if not isinstance(parent_type, Entity):
                    raise TypingException(
                        self,
                        "Parents of an entity need to be entities. "
                        "Default constructors are not supported. %s is not an entity" % parent,
                    )

                entity_type.parent_entities.append(parent_type)
                parent_type.child_entities.append(entity_type)

            for parent_type in entity_type.get_all_parent_entities():
                for attr_name, other_attr in parent_type.attributes.items():
                    if attr_name not in add_attributes:
                        add_attributes[attr_name] = other_attr
                    else:
                        # allow compatible attributes
                        my_attr = add_attributes[attr_name]

                        if my_attr.type == other_attr.type:
                            add_attributes[attr_name] = other_attr
                        else:
                            raise DuplicateException(my_attr, other_attr, "Incompatible attributes")
            # verify all attribute compatibility
        except TypeNotFoundException as e:
            e.set_statement(self)
            raise e