Example #1
0
def test_derived():
    class A(Element):
        a: relation_many[A]

    A.a = derived("a", str, 0, "*", lambda self: ["a", "b", "c"])

    a = A()

    assert isinstance(a.a, collectionlist)
    assert a.a == ["a", "b", "c"]
Example #2
0
# 49: override Element.presentation
# defined in gaphor.core.modeling.presentation

Comment.annotatedElement = association("annotatedElement",
                                       Element,
                                       opposite="ownedComment")
Element.ownedComment = association("ownedComment",
                                   Comment,
                                   opposite="annotatedElement")
# 20: override NamedElement.qualifiedName(NamedElement.namespace): derived[List[str]]


def _namedelement_qualifiedname(self) -> List[str]:
    """
    Returns the qualified name of the element as a tuple
    """
    if self.namespace:
        return _namedelement_qualifiedname(self.namespace) + [self.name]
    else:
        return [self.name]


NamedElement.qualifiedName = derived(
    NamedElement,
    "qualifiedName",
    List[str],
    0,
    1,
    lambda obj: [_namedelement_qualifiedname(obj)],
)
    In the case where the property is one navigable end of a binary
    association with both ends navigable, this gives the other end.

    For Gaphor the property on the other end is returned regardless the
    navigability.
    """
    if self.association is not None and len(self.association.memberEnd) == 2:
        return [
            self.association.memberEnd[1]
            if self.association.memberEnd[0] is self else
            self.association.memberEnd[0]
        ]
    return [None]


uml.Property.opposite = derived("opposite", uml.Property, 0, 1,
                                property_opposite)


def property_navigability(self: uml.Property) -> List[Optional[bool]]:
    """
    Get navigability of an association end.
    If no association is related to the property, then unknown navigability
    is assumed.
    """
    assoc = self.association
    if not (assoc and self.opposite):
        return [None]  # assume unknown
    owner = self.opposite.type
    if (isinstance(owner, (uml.Class, uml.Interface))
            and isinstance(self.type, (uml.Class, uml.Interface)) and
        (self in owner.ownedAttribute) or self in assoc.navigableOwnedEnd):
Example #4
0
Heritage.isSubstitutable = attribute("isSubstitutable", int)
ObjectNode.ordering = enumeration("ordering",
                                  ("unordered", "ordered", "LIFO", "FIFO"),
                                  "FIFO")
Heritage.general = association("general", Classifier, lower=1, upper=1)
Classifier.heritage = association("heritage",
                                  Heritage,
                                  composite=True,
                                  opposite="specific")
Heritage.specific = association("specific",
                                Classifier,
                                lower=1,
                                upper=1,
                                opposite="heritage")
Classifier.general = derived("general", Classifier, 0, "*",
                             lambda self: [g.general for g in self.heritage])
DirectedRelationship.target = derivedunion(
    "target",
    Element,
    1,
    "*",
    PackageImport.importedPackage,
    PackageMerge.mergedPackage,
    Heritage.general,
    Include.addition,
    Extend.extendedCase,
    Realization.realizingClassifier,
    ElementImport.importedElement,
    Substitution.contract,
)
DirectedRelationship.source = derivedunion(