use of org.kie.workbench.common.dmn.api.definition.model.DMNDiagram in project kie-wb-common by kiegroup.
the class DMNDiagramFactoryImplTest method testDefaultNameSpaces.
@Test
@SuppressWarnings("unchecked")
public void testDefaultNameSpaces() {
final Diagram diagram = factory.build(NAME, metadata, graph);
// We can safely get the first object on the iterator as we know the graph only contains one node
final Node<View, Edge> root = (Node<View, Edge>) diagram.getGraph().nodes().iterator().next();
final DMNDiagram dmnDiagram = (DMNDiagram) DefinitionUtils.getElementDefinition(root);
final Definitions dmnDefinitions = dmnDiagram.getDefinitions();
final Map<String, String> dmnDefaultNameSpaces = dmnDefinitions.getNsContext();
assertTrue(dmnDefaultNameSpaces.containsKey(DMNModelInstrumentedBase.Namespace.FEEL.getPrefix()));
assertEquals(DMNModelInstrumentedBase.Namespace.FEEL.getUri(), dmnDefaultNameSpaces.get(DMNModelInstrumentedBase.Namespace.FEEL.getPrefix()));
assertTrue(dmnDefaultNameSpaces.containsKey(DMNModelInstrumentedBase.Namespace.DMN.getPrefix()));
assertEquals(DMNModelInstrumentedBase.Namespace.DMN.getUri(), dmnDefaultNameSpaces.get(DMNModelInstrumentedBase.Namespace.DMN.getPrefix()));
assertTrue(dmnDefaultNameSpaces.containsKey(DMNModelInstrumentedBase.Namespace.KIE.getPrefix()));
assertEquals(DMNModelInstrumentedBase.Namespace.KIE.getUri(), dmnDefaultNameSpaces.get(DMNModelInstrumentedBase.Namespace.KIE.getPrefix()));
assertTrue(dmnDefaultNameSpaces.containsKey(DMNModelInstrumentedBase.Namespace.DEFAULT.getPrefix()));
assertEquals(dmnDefinitions.getNamespace().getValue(), dmnDefaultNameSpaces.get(DMNModelInstrumentedBase.Namespace.DEFAULT.getPrefix()));
assertTrue(dmnDefaultNameSpaces.containsKey(DMNModelInstrumentedBase.Namespace.DMNDI.getPrefix()));
assertEquals(DMNModelInstrumentedBase.Namespace.DMNDI.getUri(), dmnDefaultNameSpaces.get(DMNModelInstrumentedBase.Namespace.DMNDI.getPrefix()));
assertTrue(dmnDefaultNameSpaces.containsKey(DMNModelInstrumentedBase.Namespace.DI.getPrefix()));
assertEquals(DMNModelInstrumentedBase.Namespace.DI.getUri(), dmnDefaultNameSpaces.get(DMNModelInstrumentedBase.Namespace.DI.getPrefix()));
assertTrue(dmnDefaultNameSpaces.containsKey(DMNModelInstrumentedBase.Namespace.DC.getPrefix()));
assertEquals(DMNModelInstrumentedBase.Namespace.DC.getUri(), dmnDefaultNameSpaces.get(DMNModelInstrumentedBase.Namespace.DC.getPrefix()));
}
use of org.kie.workbench.common.dmn.api.definition.model.DMNDiagram in project kie-wb-common by kiegroup.
the class DMNMarshaller method marshall.
public JSITDefinitions marshall() {
final Map<String, JSITDRGElement> nodes = new HashMap<>();
final Map<String, JSITTextAnnotation> textAnnotations = new HashMap<>();
final Node<View<DMNDiagram>, ?> dmnDiagramRoot = (Node<View<DMNDiagram>, ?>) DMNGraphUtils.findDMNDiagramRoot(dmnDiagramsSession.getDRGDiagram().getGraph());
final Definitions definitionsStunnerPojo = ((DMNDiagram) getElementDefinition(dmnDiagramRoot)).getDefinitions();
final List<String> dmnDiagramElementIds = new ArrayList<>();
final JSITDefinitions definitions = DefinitionsConverter.dmnFromWB(definitionsStunnerPojo, true);
if (Objects.isNull(definitions.getExtensionElements())) {
JSITDMNElement.JSIExtensionElements jsiExtensionElements = new JSITDMNElement.JSIExtensionElements();
definitions.setExtensionElements(jsiExtensionElements);
}
final JsArrayLike<JSIDMNDiagram> dmnDiagrams = definitions.getDMNDI().getNativeDMNDiagram();
for (int i = 0; i < dmnDiagrams.getLength(); i++) {
JSIDMNDiagram diagram = Js.uncheckedCast(dmnDiagrams.getAt(i));
final String elementDiagramId = diagram.getId();
final List<JSIDMNEdge> dmnEdges = new ArrayList<>();
final List<Node> diagramNodes = getNodeStream(dmnDiagramsSession.getDiagram(elementDiagramId));
// Setup callback for marshalling ComponentWidths
if (Objects.isNull(diagram.getExtension())) {
diagram.setExtension(new JSIDiagramElement.JSIExtension());
}
final JSITComponentsWidthsExtension componentsWidthsExtension = new JSITComponentsWidthsExtension();
final JSIDiagramElement.JSIExtension extension = diagram.getExtension();
JSITComponentsWidthsExtension wrappedComponentsWidthsExtension = WrapperUtils.getWrappedJSITComponentsWidthsExtension(componentsWidthsExtension);
extension.addAny(wrappedComponentsWidthsExtension);
final Consumer<JSITComponentWidths> componentWidthsConsumer = (cw) -> componentsWidthsExtension.addComponentWidths(cw);
// Convert relative positioning to absolute
for (final Node<?, ?> node : diagramNodes) {
PointUtils.convertToAbsoluteBounds(node);
}
// Iterate Graph processing nodes..
for (final Node<?, ?> node : diagramNodes) {
if (!(node.getContent() instanceof View<?>)) {
continue;
}
final View<?> view = (View<?>) node.getContent();
final Object viewDefinition = view.getDefinition();
if (!(viewDefinition instanceof HasContentDefinitionId)) {
continue;
}
final HasContentDefinitionId hasContentDefinitionId = (HasContentDefinitionId) viewDefinition;
final String nodeDiagramId = hasContentDefinitionId.getDiagramId();
if (!Objects.equals(nodeDiagramId, elementDiagramId)) {
continue;
}
if (viewDefinition instanceof DRGElement) {
final DRGElement drgElement = (DRGElement) viewDefinition;
if (!drgElement.isAllowOnlyVisualChange()) {
if (nodes.containsKey(drgElement.getId().getValue())) {
final JSITDRGElement currentValue = nodes.get(drgElement.getId().getValue());
mergeNodeRequirements(stunnerToDMN(withIncludedModels(node, definitionsStunnerPojo), componentWidthsConsumer), currentValue);
} else {
nodes.put(drgElement.getId().getValue(), stunnerToDMN(withIncludedModels(node, definitionsStunnerPojo), componentWidthsConsumer));
}
}
final String namespaceURI = definitionsStunnerPojo.getDefaultNamespace();
diagram.addDMNDiagramElement(WrapperUtils.getWrappedJSIDMNShape(diagram, dmnDiagramElementIds, definitionsStunnerPojo, (View<? extends DMNElement>) view, namespaceURI));
}
if (viewDefinition instanceof TextAnnotation) {
final TextAnnotation textAnnotation = (TextAnnotation) viewDefinition;
if (!textAnnotation.isAllowOnlyVisualChange()) {
textAnnotations.put(textAnnotation.getId().getValue(), textAnnotationConverter.dmnFromNode((Node<View<TextAnnotation>, ?>) node, componentWidthsConsumer));
}
final String namespaceURI = definitionsStunnerPojo.getDefaultNamespace();
diagram.addDMNDiagramElement(WrapperUtils.getWrappedJSIDMNShape(diagram, dmnDiagramElementIds, definitionsStunnerPojo, (View<? extends DMNElement>) view, namespaceURI));
final List<JSITAssociation> associations = AssociationConverter.dmnFromWB((Node<View<TextAnnotation>, ?>) node);
forEach(associations, association -> {
final JSITAssociation wrappedJSITAssociation = WrapperUtils.getWrappedJSITAssociation(Js.uncheckedCast(association));
definitions.addArtifact(wrappedJSITAssociation);
});
}
connect(diagram, dmnDiagramElementIds, definitionsStunnerPojo, dmnEdges, node, view);
}
nodes.values().forEach(node -> {
addNodeToDefinitionsIfNotPresent(node, definitions);
});
textAnnotations.values().forEach(text -> {
final boolean exists = anyMatch(definitions.getArtifact(), artifact -> Objects.equals(artifact.getId(), text.getId()));
if (!exists) {
definitions.addArtifact(WrapperUtils.getWrappedJSITTextAnnotation(text));
}
});
forEach(dmnEdges, dmnEdge -> {
final boolean exists = anyMatch(diagram.getDMNDiagramElement(), diagramElement -> {
if (JSIDMNEdge.instanceOf(diagramElement)) {
final JSIDMNEdge jsidmnEdge = Js.uncheckedCast(diagramElement);
return Objects.equals(jsidmnEdge.getDmnElementRef(), dmnEdge.getDmnElementRef());
}
return false;
});
if (!exists) {
diagram.addDMNDiagramElement(WrapperUtils.getWrappedJSIDMNEdge(Js.uncheckedCast(dmnEdge)));
}
});
// Convert absolute positioning to relative
for (final Node<?, ?> node : diagramNodes) {
PointUtils.convertToRelativeBounds(node);
}
}
;
return definitions;
}
use of org.kie.workbench.common.dmn.api.definition.model.DMNDiagram in project kie-wb-common by kiegroup.
the class DMNMarshallerTest method testWithIncludedModelsWhenNodeAlreadyHasImports.
@Test
public void testWithIncludedModelsWhenNodeAlreadyHasImports() {
final DMNMarshaller dmnMarshaller = spy(new DMNMarshaller());
final Node node = mock(Node.class);
final Definition nodeDefinition = mock(Definition.class);
final DRGElement drgElement = mock(DRGElement.class);
final Definitions definitionsStunnerPojo = mock(Definitions.class);
final Import import1 = mock(Import.class);
final Import import2 = mock(Import.class);
final List<Import> diagramImports = new ArrayList<>(asList(import1, import2));
final DMNDiagram nodeDiagram = mock(DMNDiagram.class);
final Definitions nodeDiagramDefinitions = mock(Definitions.class);
final List<Import> nodeDiagramImports = new ArrayList<>(asList(import1, import2));
when(node.getContent()).thenReturn(nodeDefinition);
when(nodeDefinition.getDefinition()).thenReturn(drgElement);
when(definitionsStunnerPojo.getImport()).thenReturn(diagramImports);
when(drgElement.getParent()).thenReturn(nodeDiagram);
when(nodeDiagram.getDefinitions()).thenReturn(nodeDiagramDefinitions);
when(nodeDiagramDefinitions.getImport()).thenReturn(nodeDiagramImports);
dmnMarshaller.withIncludedModels(node, definitionsStunnerPojo);
assertEquals(2, nodeDiagramImports.size());
assertTrue(nodeDiagramImports.contains(import1));
assertTrue(nodeDiagramImports.contains(import2));
}
use of org.kie.workbench.common.dmn.api.definition.model.DMNDiagram in project kie-wb-common by kiegroup.
the class DMNMarshallerStandaloneTest method testStunnerConstellationButtonCausingPoint2DbeingNull.
/**
* DROOLS-3184: If the "connection source/target location is null" assume it's the centre of the shape.
* [source/target location is null] If the connection was created from the Toolbox (i.e. add a InputData and then the Decision from it using the Decision toolbox icon).
* <p>
* This test re-create by hard-code the behavior of the Stunner framework "Toolbox" by instrumenting API calls to achieve the same behavior.
*/
@SuppressWarnings("unchecked")
@Test
public void testStunnerConstellationButtonCausingPoint2DbeingNull() throws IOException {
final BiConsumer<String, HasComponentWidths> hasComponentWidthsConsumer = (uuid, hcw) -> {
/*NOP*/
};
Diagram diagram = createDiagram();
Graph g = diagram.getGraph();
Node diagramRoot = DMNMarshallerStandalone.findDMNDiagramRoot(g);
testAugmentWithNSPrefixes(((DMNDiagram) ((View<?>) diagramRoot.getContent()).getDefinition()).getDefinitions());
org.kie.dmn.model.api.InputData dmnInputData = new TInputData();
dmnInputData.setId("inputDataID");
dmnInputData.setName(dmnInputData.getId());
Node inputDataNode = new InputDataConverter(api.getFactoryManager()).nodeFromDMN(dmnInputData, hasComponentWidthsConsumer);
org.kie.dmn.model.api.Decision dmnDecision = new TDecision();
dmnDecision.setId("decisionID");
dmnDecision.setName(dmnDecision.getId());
Node decisionNode = new DecisionConverter(api.getFactoryManager()).nodeFromDMN(dmnDecision, hasComponentWidthsConsumer);
g.addNode(inputDataNode);
g.addNode(decisionNode);
View content = (View) decisionNode.getContent();
content.setBounds(org.kie.workbench.common.stunner.core.graph.content.Bounds.create(200, 200, 300, 250));
final String irID = "irID";
Edge myEdge = api.getFactoryManager().newElement(irID, DMNMarshallerStandalone.INFO_REQ_ID).asEdge();
myEdge.setSourceNode(inputDataNode);
myEdge.setTargetNode(decisionNode);
inputDataNode.getOutEdges().add(myEdge);
decisionNode.getInEdges().add(myEdge);
ViewConnector connectionContent = (ViewConnector) myEdge.getContent();
// DROOLS-3184: If the "connection source/target location is null" assume it's the centre of the shape.
// keep Stunner behavior of constellation button
connectionContent.setSourceConnection(MagnetConnection.Builder.atCenter(inputDataNode).setLocation(null).setAuto(true));
connectionContent.setTargetConnection(MagnetConnection.Builder.atCenter(decisionNode).setLocation(null).setAuto(true));
DMNMarshallerStandalone.connectRootWithChild(diagramRoot, inputDataNode);
DMNMarshallerStandalone.connectRootWithChild(diagramRoot, decisionNode);
DMNMarshallerStandalone m = getDMNMarshaller();
String output = m.marshall(diagram);
LOG.debug(output);
Definitions dmnDefinitions = DMNMarshallerFactory.newDefaultMarshaller().unmarshal(output);
DMNEdge dmndiEdge = findEdgeByDMNI(dmnDefinitions.getDMNDI().getDMNDiagram().get(0), irID);
assertThat(dmndiEdge.getWaypoint()).hasSize(2);
Point wpSource = dmndiEdge.getWaypoint().get(0);
assertThat(wpSource.getX()).isEqualByComparingTo(50d);
assertThat(wpSource.getY()).isEqualByComparingTo(25d);
Point wpTarget = dmndiEdge.getWaypoint().get(1);
assertThat(wpTarget.getX()).isEqualByComparingTo(250d);
assertThat(wpTarget.getY()).isEqualByComparingTo(225d);
}
use of org.kie.workbench.common.dmn.api.definition.model.DMNDiagram in project kie-wb-common by kiegroup.
the class DMNMarshallerStandaloneTest method testAssociationEdgeDMNDI.
/**
* DROOLS-2569 [DMN Designer] Marshalling of magnet positions -- Association DMN Edge DMNDI serialization.
* This test re-create by hard-code the graph to simulate the behavior of the Stunner framework programmatically.
*/
@SuppressWarnings("unchecked")
@Test
public void testAssociationEdgeDMNDI() throws IOException {
final BiConsumer<String, HasComponentWidths> hasComponentWidthsConsumer = (uuid, hcw) -> {
/*NOP*/
};
Diagram diagram = createDiagram();
Graph g = diagram.getGraph();
Node diagramRoot = DMNMarshallerStandalone.findDMNDiagramRoot(g);
testAugmentWithNSPrefixes(((DMNDiagram) ((View<?>) diagramRoot.getContent()).getDefinition()).getDefinitions());
org.kie.dmn.model.api.InputData dmnInputData = new TInputData();
dmnInputData.setId("inputDataID");
dmnInputData.setName(dmnInputData.getId());
Node inputDataNode = new InputDataConverter(api.getFactoryManager()).nodeFromDMN(dmnInputData, hasComponentWidthsConsumer);
org.kie.dmn.model.api.TextAnnotation dmnTextAnnotation = new TTextAnnotation();
dmnTextAnnotation.setId("textAnnotationID");
Node textAnnotationNode = new TextAnnotationConverter(api.getFactoryManager()).nodeFromDMN(dmnTextAnnotation, hasComponentWidthsConsumer);
g.addNode(inputDataNode);
g.addNode(textAnnotationNode);
View content = (View) textAnnotationNode.getContent();
content.setBounds(org.kie.workbench.common.stunner.core.graph.content.Bounds.create(200, 200, 300, 250));
final String edgeID = "edgeID";
final String associationID = "associationID";
Edge myEdge = api.getFactoryManager().newElement(edgeID, DMNMarshallerStandalone.ASSOCIATION_ID).asEdge();
final View<?> edgeView = (View<?>) myEdge.getContent();
((Association) edgeView.getDefinition()).setId(new Id(associationID));
myEdge.setSourceNode(inputDataNode);
myEdge.setTargetNode(textAnnotationNode);
inputDataNode.getOutEdges().add(myEdge);
textAnnotationNode.getInEdges().add(myEdge);
ViewConnector connectionContent = (ViewConnector) myEdge.getContent();
connectionContent.setSourceConnection(MagnetConnection.Builder.atCenter(inputDataNode));
connectionContent.setTargetConnection(MagnetConnection.Builder.atCenter(textAnnotationNode));
DMNMarshallerStandalone.connectRootWithChild(diagramRoot, inputDataNode);
DMNMarshallerStandalone.connectRootWithChild(diagramRoot, textAnnotationNode);
DMNMarshallerStandalone m = getDMNMarshaller();
String output = m.marshall(diagram);
LOG.debug(output);
Definitions dmnDefinitions = DMNMarshallerFactory.newDefaultMarshaller().unmarshal(output);
assertThat(dmnDefinitions.getDMNDI().getDMNDiagram().get(0).getDMNDiagramElement().stream().filter(DMNEdge.class::isInstance).count()).isEqualTo(1);
DMNEdge dmndiEdge = findEdgeByDMNI(dmnDefinitions.getDMNDI().getDMNDiagram().get(0), associationID);
assertThat(dmndiEdge.getWaypoint()).hasSize(2);
}
Aggregations