use of org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentsWidthsExtension 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.dmn.backend.definition.v1_1.dd.ComponentsWidthsExtension 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