Beispiel #1
0
    def _getPyutLink(self, obj: Element):
        """

        Args:
            obj:  The GraphicLink DOM element

        Returns:
            A tuple of a source ID, destination ID, and a PyutLink object
        """
        link: Element = obj.getElementsByTagName(
            PyutXmlConstants.ELEMENT_MODEL_LINK)[0]

        pyutLink: PyutLink = PyutLink()

        pyutLink.setBidir(
            bool(link.getAttribute(PyutXmlConstants.ATTR_BIDIRECTIONAL)))

        pyutLink.destinationCardinality = link.getAttribute(
            PyutXmlConstants.ATTR_CARDINALITY_DESTINATION)
        pyutLink.sourceCardinality = link.getAttribute(
            PyutXmlConstants.ATTR_CARDINALITY_SOURCE)

        pyutLink.setName(link.getAttribute(PyutXmlConstants.ATTR_NAME))

        strLinkType: str = link.getAttribute(PyutXmlConstants.ATTR_TYPE)
        strLinkType = strLinkType.replace(PyutXmlConstants.V9_LINK_PREFIX, '')
        linkType: LinkType = LinkType.toEnum(strValue=strLinkType)
        pyutLink.setType(linkType)

        # source and destination will be reconstructed by _getOglLinks
        sourceId = int(link.getAttribute(PyutXmlConstants.ATTR_SOURCE_ID))
        destId = int(link.getAttribute(PyutXmlConstants.ATTR_DESTINATION_ID))

        return sourceId, destId, pyutLink
Beispiel #2
0
    def createLink(self,
                   src: OglClass,
                   dst: OglClass,
                   linkType: LinkType = LinkType.AGGREGATION):
        """
        Used to create links;  It is still the caller's responsibility to add the created shape to the
        appropriate diagram

        Args:
            src:        The source OglClass
            dst:        The destination OglClass
            linkType:   The type of link
        """
        sourceClass: PyutClass = cast(PyutClass, src.getPyutObject())
        destinationClass: PyutClass = cast(PyutClass, dst.getPyutObject())

        pyutLink: PyutLink = PyutLink("",
                                      linkType=linkType,
                                      source=sourceClass,
                                      destination=destinationClass)

        oglLinkFactory = getOglLinkFactory()
        oglLink = oglLinkFactory.getOglLink(src, pyutLink, dst, linkType)

        src.addLink(oglLink)
        dst.addLink(oglLink)

        # src.getPyutObject().addLink(pyutLink)       # TODO fix this
        pyutClass: PyutClass = cast(PyutClass, src.getPyutObject())
        pyutClass.addLink(pyutLink)

        return oglLink
Beispiel #3
0
    def _createInheritanceLink(self, child: OglClass,
                               parent: OglClass) -> OglLink:
        """
        Add a parent link between the child and parent objects.

        Args:
            child:  Child PyutClass
            parent: Parent PyutClass

        Returns:
            The inheritance OglLink
        """
        sourceClass: PyutClass = cast(PyutClass, child.pyutObject)
        destinationClass: PyutClass = cast(PyutClass, parent.pyutObject)
        pyutLink: PyutLink = PyutLink("",
                                      linkType=LinkType.INHERITANCE,
                                      source=sourceClass,
                                      destination=destinationClass)
        oglLink: OglLink = getOglLinkFactory().getOglLink(
            child, pyutLink, parent, LinkType.INHERITANCE)

        child.addLink(oglLink)
        parent.addLink(oglLink)

        # add it to the PyutClass
        # child.getPyutObject().addParent(parent.getPyutObject())
        childPyutClass: PyutClass = cast(PyutClass, child.pyutObject)
        parentPyutClass: PyutClass = cast(PyutClass, parent.pyutObject)

        childPyutClass.addParent(parentPyutClass)

        return oglLink
Beispiel #4
0
    def createInterfaceLink(self, src: OglClass,
                            dst: OglClass) -> OglInterface:
        """
        Adds an OglInterface link between src and dst.

        Args:
            src:    source of the link
            dst:    destination of the link

        Returns: the created OglInterface link
        """
        sourceClass: PyutClass = cast(PyutClass, src.getPyutObject())
        destinationClass: PyutClass = cast(PyutClass, dst.getPyutObject())

        pyutLink: PyutLink = PyutLink(linkType=LinkType.INTERFACE,
                                      source=sourceClass,
                                      destination=destinationClass)
        oglInterface: OglInterface = OglInterface(srcShape=src,
                                                  pyutLink=pyutLink,
                                                  dstShape=dst)

        src.addLink(oglInterface)
        dst.addLink(oglInterface)

        self._diagram.AddShape(oglInterface)
        self.Refresh()

        return oglInterface
    def createInheritanceLink(self, child: OglClass,
                              parent: OglClass) -> OglLink:
        """
        TODO: this is a duplicate of CreateOglLinkCommandCommand._createInheritanceLink (this code adds it to the frame)

        Add a parent link between the child and parent objects.

        Args:
            child:  Child PyutClass
            parent: Parent PyutClass

        Returns:
            The inheritance OglLink
        """
        pyutLink = PyutLink("",
                            linkType=LinkType.INHERITANCE,
                            source=child.getPyutObject(),
                            destination=parent.getPyutObject())
        oglLink = getOglLinkFactory().getOglLink(child, pyutLink, parent,
                                                 LinkType.INHERITANCE)

        child.addLink(oglLink)
        parent.addLink(oglLink)

        # add it to the PyutClass
        # child.getPyutObject().addParent(parent.getPyutObject())
        childPyutClass: PyutClass = child.getPyutObject()
        parentPyutClass: PyutClass = parent.getPyutObject()

        childPyutClass.addParent(parentPyutClass)

        self._diagram.AddShape(oglLink)
        self.Refresh()

        return oglLink
Beispiel #6
0
    def _getPyutLink(self, obj):
        """
        To extract a PyutLink from an OglLink object.

        @param String obj : Name of the object.
        """
        link = obj.getElementsByTagName("Link")[0]

        aLink = PyutLink()

        aLink.setBidir(bool(link.getAttribute('bidir')))

        # aLink.setDestinationCardinality(link.getAttribute('cardDestination'))
        # aLink.setSourceCardinality(link.getAttribute('cardSrc'))
        aLink.destinationCardinality = link.getAttribute('cardDestination')
        aLink.sourceCardinality = link.getAttribute('cardSrc')

        aLink.setName(link.getAttribute('name'))
        strLinkType: str = link.getAttribute('type')
        linkType: LinkType = LinkType(int(strLinkType))
        aLink.setType(linkType)
        # source and destination will be reconstructed by _getOglLinks

        sourceId = int(link.getAttribute('sourceId'))
        destId = int(link.getAttribute('destId'))

        return sourceId, destId, aLink
Beispiel #7
0
    def _getLinks(self, obj):
        """
        To extract links form an OGL object.

        @param String obj : Name of the object.
        @since 1.0
        @author Deve Roux <*****@*****.**>
        @changed Philippe Waelti <*****@*****.**> : Refactoring campaign
        """
        allLinks = []
        for link in obj.getElementsByTagName("Link"):

            aLink = PyutLink()

            aLink.setBidir(bool(link.getAttribute('bidir')))

            # aLink.setDestinationCardinality(link.getAttribute('cardDestination'))
            # aLink.setSourceCardinality(link.getAttribute('cardSrc'))

            aLink.destinationCardinality = link.getAttribute('cardDestination')
            aLink.sourceCardinality = link.getAttribute('cardSrc')

            aLink.setName(link.getAttribute('name'))
            aLink.setType(link.getAttribute('type'))
            aLink.setDestination(link.getAttribute('destination'))

            # Backward compatibility
            if link.hasAttribute('destId'):
                destId = int(link.getAttribute('destId'))
            else:
                destId = 0

            allLinks.append([destId, aLink])

        return allLinks
Beispiel #8
0
    def testLegacyValidLinkType(self):

        legacyPyutLink: PyutLink = PyutLink(name='ValidLegacyPyutLink')
        legacyValue: LinkType = LinkType.COMPOSITION

        legacyPyutLink.setType(legacyValue)

        expectedLinkType: LinkType = LinkType.COMPOSITION
        actualLinkType: LinkType = legacyPyutLink.getType()

        self.assertEqual(expectedLinkType, actualLinkType,
                         'Incorrect  valid legacy type support')
Beispiel #9
0
    def testLegacyInvalidLinkType(self):

        legacyPyutLink: PyutLink = PyutLink(name='InvalidLegacyPyutLink')
        legacyValue: int = LinkType.NOTELINK.value + 99

        # noinspection PyTypeChecker
        legacyPyutLink.setType(legacyValue)

        expectedLinkType: LinkType = LinkType.INHERITANCE
        actualLinkType: LinkType = legacyPyutLink.getType()

        self.assertEqual(expectedLinkType, actualLinkType,
                         'Incorrect legacy invalid type support')
Beispiel #10
0
    def testLegacyLinkType(self):

        legacyPyutLink: PyutLink = PyutLink(name='LegacyPyutLink')
        legacyValue: int = LinkType.NOTELINK.value

        # noinspection PyTypeChecker
        legacyPyutLink.setType(legacyValue)

        expectedLinkType: LinkType = LinkType.NOTELINK
        actualLinkType: LinkType = legacyPyutLink.getType()

        self.assertEqual(expectedLinkType, actualLinkType,
                         'Incorrect legacy support')
Beispiel #11
0
    def __createMockLink(self, src: MagicMock, dest: MagicMock) -> MagicMock:
        """
        pyutLink = PyutLink("", linkType=linkType, source=src.getPyutObject(), destination=dst.getPyutObject())

        oglLink = oglLinkFactory.getOglLink(src, pyutLink, dst, linkType)

        src.addLink(oglLink)
        dst.addLink(oglLink)

        src.getPyutObject().addLink(pyutLink)

        Args:
            src:    Mock OglClass
            dest:   Mock OglClass

        Returns:
            Mocked OglLink
        """
        oglLink: MagicMock = MagicMock(spec=OglLink)
        linkId = next(self._linkIDGenerator)
        oglLink.GetID.return_value = linkId

        mockSourceAnchor: MagicMock = MagicMock(spec=AnchorPoint)
        mockDestinationAnchor: MagicMock = MagicMock(spec=AnchorPoint)

        mockSourceAnchor.GetPosition.return_value = (22, 44)
        mockDestinationAnchor.GetPosition.return_value = (1024, 450)

        oglLink.sourceAnchor.return_value = mockSourceAnchor
        oglLink.destinationAnchor.return_value = mockDestinationAnchor

        oglLink.getSourceShape.return_value = src
        oglLink.getDestinationShape.return_value = dest
        #
        # PyutLink object simple enough so create real one
        pyutLink: PyutLink = PyutLink("",
                                      linkType=LinkType.INHERITANCE,
                                      source=src.getPyutObject(),
                                      destination=dest.getPyutObject())

        src.getLinks.return_value = [oglLink]
        dest.getLinks.return_value = [oglLink]

        mockPyutClass = src.getPyutObject()
        mockPyutClass.getLinks.return_value = [pyutLink]

        return oglLink
Beispiel #12
0
    def _createLink(self,
                    src,
                    dst,
                    linkType: LinkType = LinkType.INHERITANCE,
                    srcPos=None,
                    dstPos=None):
        """
        Add a link between src and dst without adding it to the frame.

        Args:
            src:        Source of the link
            dst:        Destination of the link
            linkType:   Type of the link
            srcPos:     Position on source
            dstPos:     Position destination

        Returns:    The created link
        """
        if linkType == LinkType.INHERITANCE:
            return self._createInheritanceLink(src, dst)
        elif linkType == LinkType.SD_MESSAGE:
            return self._createSDMessage(src=src,
                                         dest=dst,
                                         srcPos=srcPos,
                                         destPos=dstPos)
        pyutLink = PyutLink("",
                            linkType=linkType,
                            source=src.getPyutObject(),
                            destination=dst.getPyutObject())

        # Call the factory to create OGL Link
        oglLinkFactory = getOglLinkFactory()
        # oglLink = oglLinkFactory.getOglLink(src, pyutLink, dst, linkType)
        oglLink = oglLinkFactory.getOglLink(srcShape=src,
                                            pyutLink=pyutLink,
                                            destShape=dst,
                                            linkType=linkType)

        src.addLink(oglLink)  # add it to the source OglShape
        dst.addLink(oglLink)  # add it to the destination OglShape

        src.getPyutObject().addLink(pyutLink)  # add it to the PyutClass

        return oglLink
Beispiel #13
0
    def __init__(self, srcShape, pyutLink, dstShape, srcPos=None, dstPos=None):
        """

        Args:
            srcShape:   Source shape
            pyutLink:   Conceptual links associated with the graphical links.
            dstShape:   Destination shape
            srcPos:     Position of source      Override location of input source
            dstPos:     Position of destination Override location of input destination
        """
        self._srcShape = srcShape
        self._destShape = dstShape

        self.clsLogger.debug(
            f'Input Override positions - srcPos: {srcPos} dstPos: {dstPos}')
        if srcPos is None and dstPos is None:
            srcX, srcY = self._srcShape.GetPosition()
            dstX, dstY = self._destShape.GetPosition()

            orient = getOrient(srcX, srcY, dstX, dstY)

            sw, sh = self._srcShape.GetSize()
            dw, dh = self._destShape.GetSize()
            if orient == AttachmentPoint.NORTH:
                srcX, srcY = sw / 2, 0
                dstX, dstY = dw / 2, dh
            elif orient == AttachmentPoint.SOUTH:
                srcX, srcY = sw / 2, sh
                dstX, dstY = dw / 2, 0
            elif orient == AttachmentPoint.EAST:
                srcX, srcY = sw, sh / 2
                dstX, dstY = 0, dh / 2
            elif orient == AttachmentPoint.WEST:
                srcX, srcY = 0, sh / 2
                dstX, dstY = dw, dh / 2

            # ============== Avoid over-lining; Added by C.Dutoit ================
            # lstAnchorsPoints = [anchor.GetRelativePosition() for anchor in srcShape.GetAnchors()]
            # while (srcX, srcY) in lstAnchorsPoints:
            #     self.clsLogger.warning(f'Over-lining in source shape')
            #     if orient == PyutAttachmentPoint.NORTH or orient == PyutAttachmentPoint.SOUTH:
            #         srcX += 10
            #     else:
            #         srcY += 10
            #
            # lstAnchorsPoints = [anchor.GetRelativePosition() for anchor in dstShape.GetAnchors()]
            # while (dstX, dstY) in lstAnchorsPoints:
            #     from org.pyut.ogl.OglClass import OglClass
            #     dstShape: OglClass = cast(OglClass, dstShape)
            #     self.clsLogger.warning(f'Over-lining in destination shape: {dstShape.getPyutObject}')
            #     if orient == PyutAttachmentPoint.NORTH or orient == PyutAttachmentPoint.SOUTH:
            #         dstX += 10
            #     else:
            #         dstY += 10

            # =========== end avoid over-lining-Added by C.Dutoit ================
        else:
            # Use provided position
            (srcX, srcY) = srcPos
            (dstX, dstY) = dstPos

        srcAnchor: AnchorPoint = self._srcShape.AddAnchor(srcX, srcY)
        dstAnchor: AnchorPoint = self._destShape.AddAnchor(dstX, dstY)
        srcAnchor.SetPosition(srcX, srcY)
        dstAnchor.SetPosition(dstX, dstY)
        srcAnchor.SetVisible(False)
        dstAnchor.SetVisible(False)
        self.clsLogger.debug(
            f'src anchor pos: {srcAnchor.GetPosition()} dst anchor pos {dstAnchor.GetPosition()}'
        )
        srcAnchor.SetDraggable(True)
        dstAnchor.SetDraggable(True)
        # Init
        LineShape.__init__(self, srcAnchor, dstAnchor)
        # Set up painting colors
        self.SetPen(BLACK_PEN)
        # Keep reference to the PyutLink for mouse events, in order
        # to can find back the corresponding link
        if pyutLink is not None:
            self._link = pyutLink
        else:
            self._link = PyutLink()
Beispiel #14
0
    def getOglLinks(self, xmlOglLinks: NodeList,
                    oglClasses: OglObjects) -> OglLinks:
        """
        Extract the link for the OglClasses

        Args:
            xmlOglLinks:    A DOM node list of links
            oglClasses:  The OglClasses

        Returns:
            The OglLinks list
        """
        oglLinks: OglLinks = cast(OglLinks, [])

        for xmlOglLink in xmlOglLinks:
            # src and dst anchor position
            xmlLink: Element = cast(Element, xmlOglLink)

            sx = PyutUtils.secureFloat(
                xmlLink.getAttribute(
                    PyutXmlConstants.ATTR_LINK_SOURCE_ANCHOR_X))
            sy = PyutUtils.secureFloat(
                xmlLink.getAttribute(
                    PyutXmlConstants.ATTR_LINK_SOURCE_ANCHOR_Y))
            dx = PyutUtils.secureFloat(
                xmlLink.getAttribute(
                    PyutXmlConstants.ATTR_LINK_DESTINATION_ANCHOR_X))
            dy = PyutUtils.secureFloat(
                xmlLink.getAttribute(
                    PyutXmlConstants.ATTR_LINK_DESTINATION_ANCHOR_Y))

            spline: bool = PyutUtils.secureBoolean(
                xmlLink.getAttribute(PyutXmlConstants.ATTR_SPLINE))

            # get the associated PyutLink
            srcId, dstId, assocPyutLink = self._getPyutLink(xmlLink)

            try:
                src: OglClass = oglClasses[srcId]
                dst: OglClass = oglClasses[dstId]
            except KeyError as ke:
                self.logger.error(
                    f'Developer Error -- srcId: {srcId} - dstId: {dstId}  error: {ke}'
                )
                continue

            linkType: LinkType = assocPyutLink.getType()
            pyutLink: PyutLink = PyutLink(
                name=assocPyutLink.getName(),
                linkType=linkType,
                cardSrc=assocPyutLink.sourceCardinality,
                cardDest=assocPyutLink.destinationCardinality,
                source=src.getPyutObject(),
                destination=dst.getPyutObject())

            oglLinkFactory = getOglLinkFactory()
            oglLink = oglLinkFactory.getOglLink(src, pyutLink, dst, linkType)
            src.addLink(oglLink)
            dst.addLink(oglLink)

            oglLinks.append(oglLink)

            oglLink.SetSpline(spline)

            # put the anchors at the right position
            srcAnchor = oglLink.GetSource()
            dstAnchor = oglLink.GetDestination()
            srcAnchor.SetPosition(sx, sy)
            dstAnchor.SetPosition(dx, dy)

            # add the control points to the line
            line = srcAnchor.GetLines()[0]  # only 1 line per anchor in pyut
            parent = line.GetSource().GetParent()
            selfLink = parent is line.GetDestination().GetParent()

            controlPoints: ControlPoints = self._generateControlPoints(xmlLink)
            for controlPoint in controlPoints:
                line.AddControl(controlPoint)
                if selfLink:
                    x, y = controlPoint.GetPosition()
                    controlPoint.SetParent(parent)
                    controlPoint.SetPosition(x, y)

            if isinstance(oglLink, OglAssociation):
                self.__furtherCustomizeAssociationLink(xmlLink, oglLink)
            self._reconstituteLinkDataModel(oglLink)

        return oglLinks
Beispiel #15
0
    def _getOglLinks(self, xmlOglLinks, dicoOglObjects, dicoLink, dicoFather,
                     umlFrame):
        """
        To extract the links from an OGL object.
        """
        def secure_float(floatX):
            if floatX is not None:
                return float(floatX)
            return 0.0

        def secure_spline_int(splineX):
            if splineX is None:
                return 0
            elif splineX == "_DeprecatedNonBool: False" or splineX == "False":
                return 0
            elif splineX == "_DeprecatedNonBool: True" or splineX == "True":
                return 1
            else:
                return int(splineX)

        for link in xmlOglLinks:
            # src and dst anchor position
            sx = secure_float(link.getAttribute("srcX"))
            sy = secure_float(link.getAttribute("srcY"))
            dx = secure_float(link.getAttribute("dstX"))
            dy = secure_float(link.getAttribute("dstY"))
            spline = secure_spline_int(link.getAttribute("spline"))

            # create a list of ControlPoints
            ctrlpts = []
            for ctrlpt in link.getElementsByTagName("ControlPoint"):
                x = secure_float(ctrlpt.getAttribute("x"))
                y = secure_float(ctrlpt.getAttribute("y"))
                ctrlpts.append(ControlPoint(x, y))

            # get the associated PyutLink
            srcId, dstId, pyutLink = self._getPyutLink(link)

            # CD 20060218
            src = dicoOglObjects[srcId]
            dst = dicoOglObjects[dstId]
            linkType = pyutLink.getType()
            pyutLink = PyutLink("",
                                linkType=linkType,
                                source=src.getPyutObject(),
                                destination=dst.getPyutObject())

            oglLinkFactory = getOglLinkFactory()
            oglLink = oglLinkFactory.getOglLink(src, pyutLink, dst, linkType)
            src.addLink(oglLink)
            dst.addLink(oglLink)
            umlFrame.GetDiagram().AddShape(oglLink, withModelUpdate=False)

            # create the OglLink
            oglLink.SetSpline(spline)

            # give it the PyutLink
            newPyutLink = pyutLink

            # copy the good information from the read link
            newPyutLink.setBidir(pyutLink.getBidir())
            # newPyutLink.setDestinationCardinality(pyutLink.getDestinationCardinality())
            # newPyutLink.setSourceCardinality(pyutLink.getSourceCardinality())
            newPyutLink.destinationCardinality = pyutLink.destinationCardinality
            newPyutLink.sourceCardinality = pyutLink.sourceCardinality

            newPyutLink.setName(pyutLink.getName())

            # put the anchors at the right position
            srcAnchor = oglLink.GetSource()
            dstAnchor = oglLink.GetDestination()
            srcAnchor.SetPosition(sx, sy)
            dstAnchor.SetPosition(dx, dy)

            # add the control points to the line
            line = srcAnchor.GetLines()[0]  # only 1 line per anchor in pyut
            parent = line.GetSource().GetParent()
            selfLink = parent is line.GetDestination().GetParent()

            for ctrl in ctrlpts:
                line.AddControl(ctrl)
                if selfLink:
                    x, y = ctrl.GetPosition()
                    ctrl.SetParent(parent)
                    ctrl.SetPosition(x, y)

            if isinstance(oglLink, OglAssociation):
                # center = oglLink.getLabels()[CENTER]
                # src = oglLink.getLabels()[SRC_CARD]
                # dst = oglLink.getLabels()[DEST_CARD]

                label = link.getElementsByTagName("LabelCenter")[0]
                x = float(label.getAttribute("x"))
                y = float(label.getAttribute("y"))
                # center.SetPosition(x, y)
                oglLink.centerLabel.oglPosition.x = x
                oglLink.centerLabel.oglPosition.y = y

                label = link.getElementsByTagName("LabelSrc")[0]
                x = float(label.getAttribute("x"))
                y = float(label.getAttribute("y"))
                # src.SetPosition(x, y)
                oglLink.sourceCardinality.oglPosition.x = x
                oglLink.sourceCardinality.oglPosition.y = y

                label = link.getElementsByTagName("LabelDst")[0]
                x = float(label.getAttribute("x"))
                y = float(label.getAttribute("y"))
                dst.SetPosition(x, y)