use of org.kie.dmn.model.v1_2.dmndi.DMNDiagram in project drools by kiegroup.
the class DMNModelInstrumentedBaseConverter method writeAttributes.
@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
KieDMNModelInstrumentedBase mib = (KieDMNModelInstrumentedBase) parent;
CustomStaxWriter staxWriter = ((CustomStaxWriter) writer.underlyingWriter());
for (Entry<String, String> kv : mib.getNsContext().entrySet()) {
try {
if (KieDMNModelInstrumentedBase.URI_DMN.equals(kv.getValue())) {
// skip as that is the default namespace xmlns<:prefix>=DMN is handled by the stax driver.
} else {
staxWriter.writeNamespace(kv.getKey(), kv.getValue());
}
} catch (Exception e) {
LOG.warn("The XML driver writer failed to manage writing namespace, namespaces prefixes could be wrong in the resulting file.", e);
}
}
for (Entry<QName, String> kv : mib.getAdditionalAttributes().entrySet()) {
staxWriter.addAttribute(kv.getKey().getPrefix() + ":" + kv.getKey().getLocalPart(), kv.getValue());
}
if (parent instanceof TDefinitions) {
TDefinitions tDefinitions = (TDefinitions) parent;
String dmndiPrefix = tDefinitions.getPrefixForNamespaceURI(KieDMNModelInstrumentedBase.URI_DMNDI).orElse("dmndi");
String diPrefix = tDefinitions.getPrefixForNamespaceURI(KieDMNModelInstrumentedBase.URI_DI).orElse("di");
String dcPrefix = tDefinitions.getPrefixForNamespaceURI(KieDMNModelInstrumentedBase.URI_DC).orElse("dc");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNDI", dmndiPrefix), "DMNDI");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNDiagram", dmndiPrefix), "DMNDiagram");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNStyle", dmndiPrefix), "style");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNStyle", dmndiPrefix), "DMNStyle");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNShape", dmndiPrefix), "DMNShape");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNEdge", dmndiPrefix), "DMNEdge");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNDecisionServiceDividerLine", dmndiPrefix), "DMNDecisionServiceDividerLine");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNLabel", dmndiPrefix), "DMNLabel");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, DMNLabelConverter.TEXT, dmndiPrefix), DMNLabelConverter.TEXT);
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "Size", dmndiPrefix), "Size");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "FillColor", dmndiPrefix), "FillColor");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "StrokeColor", dmndiPrefix), "StrokeColor");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "FontColor", dmndiPrefix), "FontColor");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DI, "waypoint", diPrefix), "waypoint");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DI, "extension", diPrefix), "extension");
staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DC, "Bounds", dcPrefix), "Bounds");
}
}
use of org.kie.dmn.model.v1_2.dmndi.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.dmn.model.v1_2.dmndi.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);
}
use of org.kie.dmn.model.v1_2.dmndi.DMNDiagram in project kie-wb-common by kiegroup.
the class DMNMarshallerStandalone method unmarshall.
@Override
@SuppressWarnings("unchecked")
public Graph unmarshall(final Metadata metadata, final InputStream input) throws IOException {
final Map<String, HasComponentWidths> hasComponentWidthsMap = new HashMap<>();
final BiConsumer<String, HasComponentWidths> hasComponentWidthsConsumer = (uuid, hcw) -> {
if (Objects.nonNull(uuid)) {
hasComponentWidthsMap.put(uuid, hcw);
}
};
final org.kie.dmn.model.api.Definitions dmnXml = marshaller.unmarshal(new InputStreamReader(input));
final List<org.kie.dmn.model.api.DRGElement> diagramDrgElements = dmnXml.getDrgElement();
final Optional<org.kie.dmn.model.api.dmndi.DMNDiagram> dmnDDDiagram = findDMNDiagram(dmnXml);
// Get external DMN model information
final Map<Import, org.kie.dmn.model.api.Definitions> importDefinitions = dmnMarshallerImportsHelper.getImportDefinitions(metadata, dmnXml.getImport());
// Get external PMML model information
final Map<Import, PMMLDocumentMetadata> pmmlDocuments = dmnMarshallerImportsHelper.getPMMLDocuments(metadata, dmnXml.getImport());
// Map external DRGElements
final List<DMNShape> dmnShapes = dmnDDDiagram.map(this::getUniqueDMNShapes).orElse(emptyList());
final List<org.kie.dmn.model.api.DRGElement> importedDrgElements = getImportedDrgElementsByShape(dmnShapes, importDefinitions);
// Group DRGElements
final List<org.kie.dmn.model.api.DRGElement> drgElements = new ArrayList<>();
drgElements.addAll(diagramDrgElements);
drgElements.addAll(importedDrgElements);
// Remove DRGElements that doesn't have any local or imported shape.
removeDrgElementsWithoutShape(drgElements, dmnShapes);
final Map<String, Entry<org.kie.dmn.model.api.DRGElement, Node>> elems = drgElements.stream().collect(toMap(org.kie.dmn.model.api.DRGElement::getId, dmn -> new SimpleEntry<>(dmn, dmnToStunner(dmn, hasComponentWidthsConsumer, importedDrgElements))));
final Set<org.kie.dmn.model.api.DecisionService> dmnDecisionServices = new HashSet<>();
// Stunner rely on relative positioning for Edge connections, so need to cycle on DMNShape first.
for (Entry<org.kie.dmn.model.api.DRGElement, Node> kv : elems.values()) {
ddExtAugmentStunner(dmnDDDiagram, kv.getValue());
}
// Setup Node Relationships and Connections all based on absolute positioning
for (Entry<org.kie.dmn.model.api.DRGElement, Node> kv : elems.values()) {
final org.kie.dmn.model.api.DRGElement elem = kv.getKey();
final Node currentNode = kv.getValue();
// For imported nodes, we don't have its connections
if (isImportedDRGElement(importedDrgElements, elem)) {
continue;
}
// DMN spec table 2: Requirements connection rules
if (elem instanceof org.kie.dmn.model.api.Decision) {
final org.kie.dmn.model.api.Decision decision = (org.kie.dmn.model.api.Decision) elem;
for (org.kie.dmn.model.api.InformationRequirement ir : decision.getInformationRequirement()) {
connectEdgeToNodes(INFO_REQ_ID, ir, ir.getRequiredInput(), elems, dmnXml, currentNode);
connectEdgeToNodes(INFO_REQ_ID, ir, ir.getRequiredDecision(), elems, dmnXml, currentNode);
}
for (org.kie.dmn.model.api.KnowledgeRequirement kr : decision.getKnowledgeRequirement()) {
connectEdgeToNodes(KNOWLEDGE_REQ_ID, kr, kr.getRequiredKnowledge(), elems, dmnXml, currentNode);
}
for (org.kie.dmn.model.api.AuthorityRequirement ar : decision.getAuthorityRequirement()) {
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredAuthority(), elems, dmnXml, currentNode);
}
} else if (elem instanceof org.kie.dmn.model.api.BusinessKnowledgeModel) {
final org.kie.dmn.model.api.BusinessKnowledgeModel bkm = (org.kie.dmn.model.api.BusinessKnowledgeModel) elem;
for (org.kie.dmn.model.api.KnowledgeRequirement kr : bkm.getKnowledgeRequirement()) {
connectEdgeToNodes(KNOWLEDGE_REQ_ID, kr, kr.getRequiredKnowledge(), elems, dmnXml, currentNode);
}
for (org.kie.dmn.model.api.AuthorityRequirement ar : bkm.getAuthorityRequirement()) {
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredAuthority(), elems, dmnXml, currentNode);
}
} else if (elem instanceof org.kie.dmn.model.api.KnowledgeSource) {
final org.kie.dmn.model.api.KnowledgeSource ks = (org.kie.dmn.model.api.KnowledgeSource) elem;
for (org.kie.dmn.model.api.AuthorityRequirement ar : ks.getAuthorityRequirement()) {
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredInput(), elems, dmnXml, currentNode);
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredDecision(), elems, dmnXml, currentNode);
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredAuthority(), elems, dmnXml, currentNode);
}
} else if (elem instanceof org.kie.dmn.model.api.DecisionService) {
final org.kie.dmn.model.api.DecisionService ds = (org.kie.dmn.model.api.DecisionService) elem;
dmnDecisionServices.add(ds);
for (org.kie.dmn.model.api.DMNElementReference er : ds.getEncapsulatedDecision()) {
final String reqInputID = getId(er);
final Node requiredNode = getRequiredNode(elems, reqInputID);
if (Objects.nonNull(requiredNode)) {
connectDSChildEdge(currentNode, requiredNode);
}
}
for (org.kie.dmn.model.api.DMNElementReference er : ds.getOutputDecision()) {
final String reqInputID = getId(er);
final Node requiredNode = getRequiredNode(elems, reqInputID);
if (Objects.nonNull(requiredNode)) {
connectDSChildEdge(currentNode, requiredNode);
}
}
}
}
final Map<String, Node<View<TextAnnotation>, ?>> textAnnotations = dmnXml.getArtifact().stream().filter(org.kie.dmn.model.api.TextAnnotation.class::isInstance).map(org.kie.dmn.model.api.TextAnnotation.class::cast).collect(Collectors.toMap(org.kie.dmn.model.api.TextAnnotation::getId, dmn -> textAnnotationConverter.nodeFromDMN(dmn, hasComponentWidthsConsumer)));
textAnnotations.values().forEach(n -> ddExtAugmentStunner(dmnDDDiagram, n));
final List<org.kie.dmn.model.api.Association> associations = dmnXml.getArtifact().stream().filter(org.kie.dmn.model.api.Association.class::isInstance).map(org.kie.dmn.model.api.Association.class::cast).collect(Collectors.toList());
for (org.kie.dmn.model.api.Association a : associations) {
final String sourceId = getId(a.getSourceRef());
final Node sourceNode = Optional.ofNullable(elems.get(sourceId)).map(Entry::getValue).orElse(textAnnotations.get(sourceId));
final String targetId = getId(a.getTargetRef());
final Node targetNode = Optional.ofNullable(elems.get(targetId)).map(Entry::getValue).orElse(textAnnotations.get(targetId));
@SuppressWarnings("unchecked") final Edge<View<Association>, ?> myEdge = (Edge<View<Association>, ?>) factoryManager.newElement(idOfDMNorWBUUID(a), ASSOCIATION_ID).asEdge();
final Id id = new Id(a.getId());
final Description description = new Description(a.getDescription());
final Association definition = new Association(id, description);
myEdge.getContent().setDefinition(definition);
connectEdge(myEdge, sourceNode, targetNode);
setConnectionMagnets(myEdge, a.getId(), dmnXml);
}
// Ensure all locations are updated to relative for Stunner
for (Entry<org.kie.dmn.model.api.DRGElement, Node> kv : elems.values()) {
PointUtils.convertToRelativeBounds(kv.getValue());
}
final Graph graph = factoryManager.newDiagram("prova", BindableAdapterUtils.getDefinitionSetId(DMNDefinitionSet.class), metadata).getGraph();
elems.values().stream().map(Map.Entry::getValue).forEach(graph::addNode);
textAnnotations.values().forEach(graph::addNode);
final Node<?, ?> dmnDiagramRoot = findDMNDiagramRoot(graph);
final Definitions definitionsStunnerPojo = DefinitionsConverter.wbFromDMN(dmnXml, importDefinitions, pmmlDocuments);
loadImportedItemDefinitions(definitionsStunnerPojo, importDefinitions);
((View<DMNDiagram>) dmnDiagramRoot.getContent()).getDefinition().setDefinitions(definitionsStunnerPojo);
// Only connect Nodes to the Diagram that are not referenced by DecisionServices
final List<String> references = new ArrayList<>();
dmnDecisionServices.forEach(ds -> references.addAll(ds.getEncapsulatedDecision().stream().map(org.kie.dmn.model.api.DMNElementReference::getHref).collect(Collectors.toList())));
dmnDecisionServices.forEach(ds -> references.addAll(ds.getOutputDecision().stream().map(org.kie.dmn.model.api.DMNElementReference::getHref).collect(Collectors.toList())));
final Map<org.kie.dmn.model.api.DRGElement, Node> elemsToConnectToRoot = elems.values().stream().filter(elem -> !references.contains("#" + elem.getKey().getId())).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
elemsToConnectToRoot.values().stream().forEach(node -> connectRootWithChild(dmnDiagramRoot, node));
textAnnotations.values().stream().forEach(node -> connectRootWithChild(dmnDiagramRoot, node));
// Copy ComponentWidths information
final Optional<ComponentsWidthsExtension> extension = findComponentsWidthsExtension(dmnDDDiagram);
extension.ifPresent(componentsWidthsExtension -> {
// can be imported from another diagram but the extension is not imported or present in this diagram.
if (componentsWidthsExtension.getComponentsWidths() != null) {
hasComponentWidthsMap.forEach((uuid, hasComponentWidths) -> componentsWidthsExtension.getComponentsWidths().stream().filter(componentWidths -> componentWidths.getDmnElementRef().getLocalPart().equals(uuid)).findFirst().ifPresent(componentWidths -> {
final List<Double> widths = hasComponentWidths.getComponentWidths();
widths.clear();
widths.addAll(componentWidths.getWidths());
}));
}
});
return graph;
}
Aggregations