use of org.kie.workbench.common.stunner.core.graph.content.view.DiscreteConnection in project kie-wb-common by kiegroup.
the class BPMNDiagramMarshallerTest method testMagnetDockers.
private void testMagnetDockers(Diagram<Graph, Metadata> diagram) throws Exception {
Node userTaskNode = (Node) findElementByContentType(diagram, UserTask.class);
Node businessRuleTaskNode = (Node) findElementByContentType(diagram, BusinessRuleTask.class);
Node scriptTaskNode = (Node) findElementByContentType(diagram, ScriptTask.class);
assertNotNull(userTaskNode);
assertNotNull(businessRuleTaskNode);
assertNotNull(scriptTaskNode);
ViewConnector userTaskInEdgeConnector = getInEdgeViewConnector(userTaskNode);
ViewConnector businessRuleTaskInEdgeConnector = getInEdgeViewConnector(businessRuleTaskNode);
ViewConnector scriptTaskInEdgeConnector = getInEdgeViewConnector(scriptTaskNode);
assertNotNull(userTaskInEdgeConnector);
assertNotNull(businessRuleTaskInEdgeConnector);
assertNotNull(scriptTaskInEdgeConnector);
ViewConnector userTaskOutEdgeConnector = getOutEdgeViewConnector(userTaskNode);
ViewConnector businessRuleTaskOutEdgeConnector = getOutEdgeViewConnector(businessRuleTaskNode);
ViewConnector scriptTaskOutEdgeConnector = getOutEdgeViewConnector(scriptTaskNode);
assertNotNull(userTaskOutEdgeConnector);
assertNotNull(businessRuleTaskOutEdgeConnector);
assertNotNull(scriptTaskOutEdgeConnector);
// userTaskInEdgeConnector is from magnet top-middle to left-middle
assertTrue(userTaskInEdgeConnector.getSourceConnection().isPresent());
assertTrue(userTaskInEdgeConnector.getTargetConnection().isPresent());
DiscreteConnection sourceConnection = (DiscreteConnection) userTaskInEdgeConnector.getSourceConnection().get();
DiscreteConnection targetConnection = (DiscreteConnection) userTaskInEdgeConnector.getTargetConnection().get();
assertEquals(20d, sourceConnection.getLocation().getX(), 0.1d);
assertEquals(0d, sourceConnection.getLocation().getY(), 0.1d);
assertEquals(0d, targetConnection.getLocation().getX(), 0.1d);
assertEquals(24d, targetConnection.getLocation().getY(), 0.1d);
// Assert both connections for userTaskInEdgeConnector are set to auto.
assertTrue(sourceConnection.isAuto());
assertTrue(targetConnection.isAuto());
// businessRuleTaskInEdgeConnector is from magnet right-middle to top-left
assertTrue(businessRuleTaskInEdgeConnector.getSourceConnection().isPresent());
assertTrue(businessRuleTaskInEdgeConnector.getTargetConnection().isPresent());
sourceConnection = (DiscreteConnection) businessRuleTaskInEdgeConnector.getSourceConnection().get();
targetConnection = (DiscreteConnection) businessRuleTaskInEdgeConnector.getTargetConnection().get();
assertEquals(40d, sourceConnection.getLocation().getX(), 0.1d);
assertEquals(20d, sourceConnection.getLocation().getY(), 0.1d);
assertEquals(0d, targetConnection.getLocation().getX(), 0.1d);
assertEquals(0d, targetConnection.getLocation().getY(), 0.1d);
// Assert both connections for businessRuleTaskInEdgeConnector are NOT set to auto.
assertFalse(sourceConnection.isAuto());
assertFalse(targetConnection.isAuto());
// scriptTaskInEdgeConnector is from magnet left-bottom to left-bottom
assertTrue(scriptTaskInEdgeConnector.getSourceConnection().isPresent());
assertTrue(scriptTaskInEdgeConnector.getTargetConnection().isPresent());
sourceConnection = (DiscreteConnection) scriptTaskInEdgeConnector.getSourceConnection().get();
targetConnection = (DiscreteConnection) scriptTaskInEdgeConnector.getTargetConnection().get();
assertEquals(0d, sourceConnection.getLocation().getX(), 0.1d);
assertEquals(40d, sourceConnection.getLocation().getY(), 0.1d);
assertEquals(0d, targetConnection.getLocation().getX(), 0.1d);
assertEquals(48d, targetConnection.getLocation().getY(), 0.1d);
// userTaskOutEdgeConnector is from magnet right-middle to left-middle
assertTrue(userTaskOutEdgeConnector.getSourceConnection().isPresent());
assertTrue(userTaskOutEdgeConnector.getTargetConnection().isPresent());
sourceConnection = (DiscreteConnection) userTaskOutEdgeConnector.getSourceConnection().get();
targetConnection = (DiscreteConnection) userTaskOutEdgeConnector.getTargetConnection().get();
assertEquals(136d, sourceConnection.getLocation().getX(), 0.1d);
assertEquals(24d, sourceConnection.getLocation().getY(), 0.1d);
assertEquals(0d, targetConnection.getLocation().getX(), 0.1d);
assertEquals(14d, targetConnection.getLocation().getY(), 0.1d);
// businessRuleTaskOutEdgeConnector is from magnet middle-bottom to middle-bottom
assertTrue(businessRuleTaskOutEdgeConnector.getSourceConnection().isPresent());
assertTrue(businessRuleTaskOutEdgeConnector.getTargetConnection().isPresent());
sourceConnection = (DiscreteConnection) businessRuleTaskOutEdgeConnector.getSourceConnection().get();
targetConnection = (DiscreteConnection) businessRuleTaskOutEdgeConnector.getTargetConnection().get();
assertEquals(68d, sourceConnection.getLocation().getX(), 0.1d);
assertEquals(48d, sourceConnection.getLocation().getY(), 0.1d);
assertEquals(14d, targetConnection.getLocation().getX(), 0.1d);
assertEquals(28d, targetConnection.getLocation().getY(), 0.1d);
// scriptTaskOutEdgeConnector is from magnet left-top to left-top
assertTrue(scriptTaskOutEdgeConnector.getSourceConnection().isPresent());
assertTrue(scriptTaskOutEdgeConnector.getTargetConnection().isPresent());
sourceConnection = (DiscreteConnection) scriptTaskOutEdgeConnector.getSourceConnection().get();
targetConnection = (DiscreteConnection) scriptTaskOutEdgeConnector.getTargetConnection().get();
assertEquals(0d, sourceConnection.getLocation().getX(), 0.1d);
assertEquals(0d, sourceConnection.getLocation().getY(), 0.1d);
assertEquals(0d, targetConnection.getLocation().getX(), 0.1d);
assertEquals(0d, targetConnection.getLocation().getY(), 0.1d);
}
use of org.kie.workbench.common.stunner.core.graph.content.view.DiscreteConnection in project kie-wb-common by kiegroup.
the class DMNMarshallerTest method testConnect.
@Test
public void testConnect() {
final DMNMarshaller dmnMarshaller = new DMNMarshaller();
final JSIDMNDiagram diagram = mock(JSIDMNDiagram.class);
final List<String> dmnDiagramElementIds = mock(List.class);
final Definitions definitionsStunnerPojo = mock(Definitions.class);
final List<JSIDMNEdge> dmnEdges = new ArrayList<>();
final Node<?, ?> node = mock(Node.class);
final List inEdges = new ArrayList<>();
final Edge edge = mock(Edge.class);
final Node sourceNode = mock(Node.class);
final View sourceView = mock(View.class);
final ViewConnector viewConnector = mock(ViewConnector.class);
final DiscreteConnection sourceConnection = mock(DiscreteConnection.class);
final DiscreteConnection targetConnection = mock(DiscreteConnection.class);
final View<?> view = mock(View.class);
inEdges.add(edge);
when(edge.getSourceNode()).thenReturn(sourceNode);
when(sourceNode.getContent()).thenReturn(sourceView);
when(node.getInEdges()).thenReturn(inEdges);
when(edge.getContent()).thenReturn(viewConnector);
when(viewConnector.getControlPoints()).thenReturn(new ControlPoint[] {});
when(sourceConnection.isAuto()).thenReturn(true);
when(targetConnection.isAuto()).thenReturn(true);
when(diagram.getName()).thenReturn("dmnEdge");
when(definitionsStunnerPojo.getDefaultNamespace()).thenReturn("org.edge");
when(viewConnector.getSourceConnection()).thenReturn(Optional.of(sourceConnection));
when(viewConnector.getTargetConnection()).thenReturn(Optional.of(targetConnection));
dmnMarshaller.connect(diagram, dmnDiagramElementIds, definitionsStunnerPojo, dmnEdges, node, view);
when(viewConnector.getSourceConnection()).thenReturn(Optional.empty());
when(viewConnector.getTargetConnection()).thenReturn(Optional.empty());
dmnMarshaller.connect(diagram, dmnDiagramElementIds, definitionsStunnerPojo, dmnEdges, node, view);
when(viewConnector.getSourceConnection()).thenReturn(Optional.of(sourceConnection));
when(viewConnector.getTargetConnection()).thenReturn(Optional.empty());
dmnMarshaller.connect(diagram, dmnDiagramElementIds, definitionsStunnerPojo, dmnEdges, node, view);
verify(sourceConnection).isAuto();
verify(targetConnection).isAuto();
}
use of org.kie.workbench.common.stunner.core.graph.content.view.DiscreteConnection in project kie-wb-common by kiegroup.
the class DMNMarshallerStandalone method marshall.
@Override
@SuppressWarnings("unchecked")
public String marshall(final Diagram<Graph, Metadata> diagram) {
final Graph<?, Node<View, ?>> g = diagram.getGraph();
final Map<String, org.kie.dmn.model.api.DRGElement> nodes = new HashMap<>();
final Map<String, org.kie.dmn.model.api.TextAnnotation> textAnnotations = new HashMap<>();
final Node<View<DMNDiagram>, ?> dmnDiagramRoot = (Node<View<DMNDiagram>, ?>) findDMNDiagramRoot(g);
final Definitions definitionsStunnerPojo = ((DMNDiagram) DefinitionUtils.getElementDefinition(dmnDiagramRoot)).getDefinitions();
cleanImportedItemDefinitions(definitionsStunnerPojo);
final org.kie.dmn.model.api.Definitions definitions = DefinitionsConverter.dmnFromWB(definitionsStunnerPojo);
if (definitions.getExtensionElements() == null) {
if (definitions instanceof org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase) {
definitions.setExtensionElements(new org.kie.dmn.model.v1_1.TDMNElement.TExtensionElements());
} else if (definitions instanceof org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase) {
definitions.setExtensionElements(new org.kie.dmn.model.v1_2.TDMNElement.TExtensionElements());
} else {
definitions.setExtensionElements(new org.kie.dmn.model.v1_2.TDMNElement.TExtensionElements());
}
}
if (definitions.getDMNDI() == null) {
definitions.setDMNDI(new DMNDI());
}
final org.kie.dmn.model.api.dmndi.DMNDiagram dmnDDDMNDiagram = new org.kie.dmn.model.v1_2.dmndi.DMNDiagram();
definitions.getDMNDI().getDMNDiagram().add(dmnDDDMNDiagram);
final List<DMNEdge> dmnEdges = new ArrayList<>();
// Convert relative positioning to absolute
for (Node<?, ?> node : g.nodes()) {
PointUtils.convertToAbsoluteBounds(node);
}
// Setup callback for marshalling ComponentWidths
if (dmnDDDMNDiagram.getExtension() == null) {
dmnDDDMNDiagram.setExtension(new DiagramElement.Extension());
}
final ComponentsWidthsExtension componentsWidthsExtension = new ComponentsWidthsExtension();
dmnDDDMNDiagram.getExtension().getAny().add(componentsWidthsExtension);
final Consumer<ComponentWidths> componentWidthsConsumer = cw -> componentsWidthsExtension.getComponentsWidths().add(cw);
// Iterate Graph processing nodes..
for (Node<?, ?> node : g.nodes()) {
if (node.getContent() instanceof View<?>) {
final View<?> view = (View<?>) node.getContent();
if (view.getDefinition() instanceof DRGElement) {
final DRGElement drgElement = (org.kie.workbench.common.dmn.api.definition.model.DRGElement) view.getDefinition();
if (!drgElement.isAllowOnlyVisualChange()) {
nodes.put(drgElement.getId().getValue(), stunnerToDMN(node, componentWidthsConsumer));
}
dmnDDDMNDiagram.getDMNDiagramElement().add(stunnerToDDExt(definitionsStunnerPojo, (View<? extends DMNElement>) view));
} else if (view.getDefinition() instanceof TextAnnotation) {
final TextAnnotation textAnnotation = (TextAnnotation) view.getDefinition();
textAnnotations.put(textAnnotation.getId().getValue(), textAnnotationConverter.dmnFromNode((Node<View<TextAnnotation>, ?>) node, componentWidthsConsumer));
dmnDDDMNDiagram.getDMNDiagramElement().add(stunnerToDDExt(definitionsStunnerPojo, (View<? extends DMNElement>) view));
final List<org.kie.dmn.model.api.Association> associations = AssociationConverter.dmnFromWB((Node<View<TextAnnotation>, ?>) node);
definitions.getArtifact().addAll(associations);
}
// DMNDI Edge management.
final List<Edge<?, ?>> inEdges = (List<Edge<?, ?>>) node.getInEdges();
for (Edge<?, ?> e : inEdges) {
if (e.getContent() instanceof ViewConnector) {
final ViewConnector connectionContent = (ViewConnector) e.getContent();
DiscreteConnection sourceConnection = (DiscreteConnection) connectionContent.getSourceConnection().orElse(null);
DiscreteConnection targetConnection = (DiscreteConnection) connectionContent.getTargetConnection().orElse(null);
if (sourceConnection != null && targetConnection != null) {
Point2D sourcePoint = sourceConnection.getLocation();
Point2D targetPoint = targetConnection.getLocation();
final Node<?, ?> sourceNode = e.getSourceNode();
final View<?> sourceView = (View<?>) sourceNode.getContent();
double xSource = xOfBound(upperLeftBound(sourceView));
double ySource = yOfBound(upperLeftBound(sourceView));
double xTarget = xOfBound(upperLeftBound(view));
double yTarget = yOfBound(upperLeftBound(view));
if (sourcePoint == null) {
// If the "connection source/target location is null" assume it's the centre of the shape.
if (sourceView.getDefinition() instanceof DMNViewDefinition) {
DMNViewDefinition dmnViewDefinition = (DMNViewDefinition) sourceView.getDefinition();
xSource += dmnViewDefinition.getDimensionsSet().getWidth().getValue() / 2;
ySource += dmnViewDefinition.getDimensionsSet().getHeight().getValue() / 2;
}
sourcePoint = Point2D.create(xSource, ySource);
} else {
// If it is non-null it is relative to the source/target shape location.
sourcePoint = Point2D.create(xSource + sourcePoint.getX(), ySource + sourcePoint.getY());
}
if (targetPoint == null) {
// If the "connection source/target location is null" assume it's the centre of the shape.
if (view.getDefinition() instanceof DMNViewDefinition) {
DMNViewDefinition dmnViewDefinition = (DMNViewDefinition) view.getDefinition();
xTarget += dmnViewDefinition.getDimensionsSet().getWidth().getValue() / 2;
yTarget += dmnViewDefinition.getDimensionsSet().getHeight().getValue() / 2;
}
targetPoint = Point2D.create(xTarget, yTarget);
} else {
// If it is non-null it is relative to the source/target shape location.
targetPoint = Point2D.create(xTarget + targetPoint.getX(), yTarget + targetPoint.getY());
}
final DMNEdge dmnEdge = new org.kie.dmn.model.v1_2.dmndi.DMNEdge();
// DMNDI edge elementRef is uuid of Stunner edge,
// with the only exception when edge contains as content a DMN Association (Association is an edge)
String uuid = e.getUUID();
if (e.getContent() instanceof View<?>) {
final View<?> edgeView = (View<?>) e.getContent();
if (edgeView.getDefinition() instanceof Association) {
uuid = ((Association) edgeView.getDefinition()).getId().getValue();
}
}
String autoConnectionId = "";
if (sourceConnection.isAuto()) {
autoConnectionId += AUTO_SOURCE_CONNECTION;
}
if (targetConnection.isAuto()) {
autoConnectionId += AUTO_TARGET_CONNECTION;
}
dmnEdge.setId("dmnedge-" + uuid + autoConnectionId);
dmnEdge.setDmnElementRef(new QName(uuid));
dmnEdge.getWaypoint().add(PointUtils.point2dToDMNDIPoint(sourcePoint));
for (ControlPoint cp : connectionContent.getControlPoints()) {
dmnEdge.getWaypoint().add(PointUtils.point2dToDMNDIPoint(cp.getLocation()));
}
dmnEdge.getWaypoint().add(PointUtils.point2dToDMNDIPoint(targetPoint));
dmnEdges.add(dmnEdge);
}
}
}
}
}
nodes.values().forEach(n -> {
n.setParent(definitions);
definitions.getDrgElement().add(n);
});
textAnnotations.values().forEach(definitions.getArtifact()::add);
// add DMNEdge last.
dmnDDDMNDiagram.getDMNDiagramElement().addAll(dmnEdges);
return marshaller.marshal(definitions);
}
use of org.kie.workbench.common.stunner.core.graph.content.view.DiscreteConnection in project kie-wb-common by kiegroup.
the class DMNMarshaller method connect.
protected void connect(final JSIDMNDiagram diagram, final List<String> dmnDiagramElementIds, final Definitions definitionsStunnerPojo, final List<JSIDMNEdge> dmnEdges, final Node<?, ?> node, final View<?> view) {
// DMNDI Edge management.
final List<Edge<?, ?>> inEdges = (List<Edge<?, ?>>) node.getInEdges();
for (Edge<?, ?> e : inEdges) {
if (e.getContent() instanceof ViewConnector) {
final ViewConnector connectionContent = (ViewConnector) e.getContent();
DiscreteConnection sourceConnection = (DiscreteConnection) connectionContent.getSourceConnection().orElse(null);
DiscreteConnection targetConnection = (DiscreteConnection) connectionContent.getTargetConnection().orElse(null);
if (sourceConnection != null && targetConnection != null) {
final Node<?, ?> sourceNode = e.getSourceNode();
final View<?> sourceView = (View<?>) sourceNode.getContent();
Point2D sourcePoint = sourceConnection.getLocation();
double xSource = xOfBound(upperLeftBound(sourceView));
double ySource = yOfBound(upperLeftBound(sourceView));
if (Objects.isNull(sourcePoint)) {
// If the "connection source/target location is null" assume it's the centre of the shape.
if (sourceView.getDefinition() instanceof DMNViewDefinition) {
DMNViewDefinition dmnViewDefinition = (DMNViewDefinition) sourceView.getDefinition();
xSource += dmnViewDefinition.getDimensionsSet().getWidth().getValue() / 2;
ySource += dmnViewDefinition.getDimensionsSet().getHeight().getValue() / 2;
}
sourcePoint = Point2D.create(xSource, ySource);
} else {
// If it is non-null it is relative to the source/target shape location.
sourcePoint = Point2D.create(xSource + sourcePoint.getX(), ySource + sourcePoint.getY());
}
Point2D targetPoint = targetConnection.getLocation();
double xTarget = xOfBound(upperLeftBound(view));
double yTarget = yOfBound(upperLeftBound(view));
if (Objects.isNull(targetPoint)) {
// If the "connection source/target location is null" assume it's the centre of the shape.
if (view.getDefinition() instanceof DMNViewDefinition) {
DMNViewDefinition dmnViewDefinition = (DMNViewDefinition) view.getDefinition();
xTarget += dmnViewDefinition.getDimensionsSet().getWidth().getValue() / 2;
yTarget += dmnViewDefinition.getDimensionsSet().getHeight().getValue() / 2;
}
targetPoint = Point2D.create(xTarget, yTarget);
} else {
// If it is non-null it is relative to the source/target shape location.
targetPoint = Point2D.create(xTarget + targetPoint.getX(), yTarget + targetPoint.getY());
}
final JSIDMNEdge dmnEdge = new JSIDMNEdge();
// DMNDI edge elementRef is uuid of Stunner edge,
// with the only exception when edge contains as content a DMN Association (Association is an edge)
final String uuid = getRawId(getUUID(e));
final String edgeId = getEdgeId(diagram, dmnDiagramElementIds, uuid, sourceConnection.isAuto(), targetConnection.isAuto());
dmnEdge.setId(edgeId);
final String namespaceURI = definitionsStunnerPojo.getDefaultNamespace();
dmnEdge.setDmnElementRef(new QName(namespaceURI, uuid, XMLConstants.DEFAULT_NS_PREFIX));
dmnEdge.addWaypoint(PointUtils.point2dToDMNDIPoint(sourcePoint));
for (ControlPoint cp : connectionContent.getControlPoints()) {
dmnEdge.addWaypoint(PointUtils.point2dToDMNDIPoint(cp.getLocation()));
}
dmnEdge.addWaypoint(PointUtils.point2dToDMNDIPoint(targetPoint));
dmnEdges.add(dmnEdge);
}
}
}
}
use of org.kie.workbench.common.stunner.core.graph.content.view.DiscreteConnection in project kie-wb-common by kiegroup.
the class StunnerLogger method log.
public static void log(final String type, final Connection connection) {
String discrete = "";
if (connection instanceof DiscreteConnection) {
final DiscreteConnection discreteConnection = (DiscreteConnection) connection;
discrete += "[index=" + discreteConnection.getMagnetIndex() + ", auto=" + discreteConnection.isAuto() + "]";
}
log(" Connection [" + type + "] at [" + connection.getLocation() + "] " + discrete);
}
Aggregations