def _reconstituteLinkDataModel(self, oglLink: OglLink): """ Updates one the following lists in a PyutLinkedObject: ._parents for Inheritance links ._links for all other link types Args: oglLink: An OglLink """ srcShape: OglClass = oglLink.getSourceShape() destShape: OglClass = oglLink.getDestinationShape() self.logger.debug( f'source ID: {srcShape.GetID()} - destination ID: {destShape.GetID()}' ) pyutLink: PyutLink = oglLink.getPyutObject() if pyutLink.getType() == LinkType.INHERITANCE: childPyutClass: PyutClass = srcShape.getPyutObject() parentPyutClass: PyutClass = destShape.getPyutObject() childPyutClass.addParent(parentPyutClass) else: srcPyutClass: PyutClass = srcShape.getPyutObject() srcPyutClass.addLink(pyutLink)
def oglLinkToXml(self, oglLink: OglLink, xmlDoc: Document): """ Export an OgLink to a minidom element Args: oglLink: OglLink to convert xmlDoc: xml document Returns: A new minidom element """ root = xmlDoc.createElement(PyutXmlConstants.ELEMENT_GRAPHIC_LINK) # save source and destination anchor points x, y = oglLink.GetSource().GetModel().GetPosition() simpleX, simpleY = self.__getSimpleCoordinates(x, y) root.setAttribute(PyutXmlConstants.ATTR_LINK_SOURCE_ANCHOR_X, simpleX) root.setAttribute(PyutXmlConstants.ATTR_LINK_SOURCE_ANCHOR_Y, simpleY) x, y = oglLink.GetDestination().GetModel().GetPosition() simpleX, simpleY = self.__getSimpleCoordinates(x, y) root.setAttribute(PyutXmlConstants.ATTR_LINK_DESTINATION_ANCHOR_X, simpleX) root.setAttribute(PyutXmlConstants.ATTR_LINK_DESTINATION_ANCHOR_Y, simpleY) root.setAttribute(PyutXmlConstants.ATTR_SPLINE, str(oglLink.GetSpline())) if isinstance(oglLink, OglAssociation): center: OglAssociationLabel = oglLink.centerLabel src: OglAssociationLabel = oglLink.sourceCardinality dst: OglAssociationLabel = oglLink.destinationCardinality assocLabels = { PyutXmlConstants.ELEMENT_ASSOC_CENTER_LABEL: center, PyutXmlConstants.ELEMENT_ASSOC_SOURCE_LABEL: src, PyutXmlConstants.ELEMENT_ASSOC_DESTINATION_LABEL: dst } for eltName in assocLabels: elt: Element = self.__createAssocLabelElement( eltName, xmlDoc, assocLabels[eltName]) root.appendChild(elt) # save control points (not anchors!) for x, y in oglLink.GetSegments()[1:-1]: item = xmlDoc.createElement( PyutXmlConstants.ELEMENT_MODEL_CONTROL_POINT) item.setAttribute(PyutXmlConstants.ATTR_X, str(x)) item.setAttribute(PyutXmlConstants.ATTR_Y, str(y)) root.appendChild(item) # adding the data layer object root.appendChild(self._pyutLinkToXml(oglLink.getPyutObject(), xmlDoc)) return root
def _OglLink2xml(self, oglLink: OglLink, xmlDoc: Document): """ """ root = xmlDoc.createElement('GraphicLink') # Append OGL object base # save src and dst anchor points x, y = oglLink.GetSource().GetModel().GetPosition() root.setAttribute('srcX', str(x)) root.setAttribute('srcY', str(y)) x, y = oglLink.GetDestination().GetModel().GetPosition() root.setAttribute('dstX', str(x)) root.setAttribute('dstY', str(y)) root.setAttribute('spline', str(oglLink.GetSpline())) if isinstance(oglLink, OglAssociation): center: OglAssociationLabel = oglLink.centerLabel src: OglAssociationLabel = oglLink.sourceCardinality dst: OglAssociationLabel = oglLink._destinationCardinality label = xmlDoc.createElement("LabelCenter") root.appendChild(label) label.setAttribute("x", str(center.oglPosition.x)) label.setAttribute("y", str(center.oglPosition.y)) label = xmlDoc.createElement("LabelSrc") root.appendChild(label) label.setAttribute("x", str(src.oglPosition.x)) label.setAttribute("y", str(src.oglPosition.y)) label = xmlDoc.createElement("LabelDst") root.appendChild(label) label.setAttribute("x", str(dst.oglPosition.x)) label.setAttribute("y", str(dst.oglPosition.y)) # save control points (not anchors!) for x, y in oglLink.GetSegments()[1:-1]: item = xmlDoc.createElement('ControlPoint') item.setAttribute('x', str(x)) item.setAttribute('y', str(y)) root.appendChild(item) # adding the data layer object root.appendChild(self._PyutLink2xml(oglLink.getPyutObject(), xmlDoc)) return root
def testExceptionRaised(self): """ https://ongspxm.github.io/blog/2016/11/assertraises-testing-for-errors-in-unittest/ """ from org.pyut.ogl.IllegalOperationException import IllegalOperationException mockSourceShape: MagicMock = self._createMockShape((100, 100), (10, 100)) mockDestinationShape: MagicMock = self._createMockShape((500, 500), (10, 100)) mockPyutLink: MagicMock = MagicMock() badOglLink: OglLink = OglLink(srcShape=mockSourceShape, pyutLink=mockPyutLink, dstShape=mockDestinationShape) # cream the source shape badOglLink._srcShape = None self.assertRaises(IllegalOperationException, lambda: self._raiseException(badOglLink)) badOglLink._srcShape = self._createMockShape((100, 100), (10, 100)) # cream the destination shape badOglLink._destShape = None self.assertRaises(IllegalOperationException, lambda: self._raiseException(badOglLink))
def __generateUniqueEdge(self, oglLink: OglLink, gml: str) -> str: srcOglId: int = oglLink.getSourceShape().GetID() destOglId: int = oglLink.getDestinationShape().GetID() gml = ( f'{gml}' f'{GMLExporter.singleTab}{GMLExporter.EDGE_TOKEN} {GMLExporter.START_TOKEN}\n' f'{GMLExporter.doubleTab}{GMLExporter.ID_TOKEN} {oglLink.GetID()}\n' f'{GMLExporter.doubleTab}{GMLExporter.SOURCE_ID_TOKEN} {srcOglId}\n' f'{GMLExporter.doubleTab}{GMLExporter.TARGET_ID_TOKEN} {destOglId}\n' f'{self.__generateEdgeGraphicsSection(oglLink=oglLink)}' f'{GMLExporter.singleTab}{GMLExporter.END_TOKEN}\n' ) return gml
def Draw(self, dc: DC, withChildren: bool = True): """ Called for the content drawing of links. Args: dc: Device context withChildren: draw the children or not """ self.updateLabels() OglLink.Draw(self, dc, withChildren)
def testBasicComputeLinkLength(self): mockSourceShape: MagicMock = self._createMockShape(self.MOCK_SOURCE_POSITION, (10, 100)) mockDestinationShape: MagicMock = self._createMockShape(self.MOCK_DESTINATION_POSITION, (10, 100)) mockPyutLink: MagicMock = MagicMock() oglLink: OglLink = OglLink(srcShape=mockSourceShape, pyutLink=mockPyutLink, dstShape=mockDestinationShape) actualLength: float = oglLink._computeLinkLength(self.MOCK_SOURCE_POSITION, self.MOCK_DESTINATION_POSITION) expectedLength: float = 565.685 self.assertAlmostEqual(expectedLength, actualLength, places=2)
def Draw(self, dc: DC, withChildren: bool = False): """ Called to draw the link content. We are going to draw all of our stuff, cardinality, Link name, etc. Args: dc: Device context withChildren: draw the children or not """ OglLink.Draw(self, dc, withChildren) sp: Tuple[int, int] = self._srcAnchor.GetPosition() dp: Tuple[int, int] = self._dstAnchor.GetPosition() oglSp: OglPosition = OglPosition(x=sp[0], y=sp[1]) oglDp: OglPosition = OglPosition(x=dp[0], y=dp[1]) self._drawSourceCardinality(dc=dc, sp=oglSp, dp=oglDp) self._drawCenterLabel(dc=dc, sp=oglSp, dp=oglDp) self._drawDestinationCardinality(dc=dc, sp=oglSp, dp=oglDp)
def __generateEdgeGraphicsSection(self, oglLink: OglLink) -> str: srcAnchor: AnchorPoint = oglLink.sourceAnchor destAnchor: AnchorPoint = oglLink.destinationAnchor controlPoints: List[ControlPoint] = oglLink.GetControlPoints() edgeGml: str = ( f'{GMLExporter.doubleTab}{GMLExporter.GRAPHICS_TOKEN} {GMLExporter.START_TOKEN}\n' f'{GMLExporter.tripleTab}type "line"\n' f'{GMLExporter.tripleTab}arrow "last"\n' f'{GMLExporter.tripleTab}{GMLExporter.LINE_DEFINITION_TOKEN} {GMLExporter.START_TOKEN}\n' f'{self.__generatePoint(srcAnchor)}' f'{self.__generatePoints(controlPoints)}' f'{self.__generatePoint(destAnchor)}' f'{GMLExporter.tripleTab}{GMLExporter.END_TOKEN}\n' f'{GMLExporter.doubleTab}{GMLExporter.END_TOKEN}\n' ) return edgeGml
def testFindClosestControlPoint(self): mockSourceShape: MagicMock = self._createMockShape(self.MOCK_SOURCE_POSITION, (10, 100)) mockDestinationShape: MagicMock = self._createMockShape(self.MOCK_DESTINATION_POSITION, (10, 100)) mockPyutLink: MagicMock = MagicMock() oglLink: OglLink = OglLink(srcShape=mockSourceShape, pyutLink=mockPyutLink, dstShape=mockDestinationShape) pointsToAdd: ControlPoints = self._createControlPoints() for cp in pointsToAdd: oglLink.AddControl(cp) self.logger.debug(f'{len(oglLink._controls)=}') expectedControlPoint: ControlPoint = pointsToAdd[0] closestPoint: ControlPoint = oglLink._findClosestControlPoint(clickPoint=(100, 151)) self.logger.debug(f'{closestPoint=}') self.assertEqual(expectedControlPoint, closestPoint, 'Found incorrect control point')
def _raiseException(self, badOglLink: OglLink): badOglLink._computeDxDy(srcPosition=self.MOCK_SOURCE_POSITION, destPosition=self.MOCK_DESTINATION_POSITION)