Search in sources :

Example 26 with DMNDiagram

use of org.kie.workbench.common.dmn.api.definition.model.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;
}
Also used : Edge(org.kie.workbench.common.stunner.core.graph.Edge) PointUtils.yOfBound(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.PointUtils.yOfBound) PointUtils.xOfBound(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.PointUtils.xOfBound) DoubleConsumer(java.util.function.DoubleConsumer) Bounds(org.kie.dmn.model.api.dmndi.Bounds) Map(java.util.Map) NamedElement(org.kie.workbench.common.dmn.api.definition.model.NamedElement) FactoryManager(org.kie.workbench.common.stunner.core.api.FactoryManager) Diagram(org.kie.workbench.common.stunner.core.diagram.Diagram) HasComponentWidths(org.kie.workbench.common.dmn.api.definition.HasComponentWidths) Height(org.kie.workbench.common.dmn.api.property.dimensions.Height) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) BusinessKnowledgeModel(org.kie.workbench.common.dmn.api.definition.model.BusinessKnowledgeModel) Set(java.util.Set) BusinessKnowledgeModelConverter(org.kie.workbench.common.dmn.backend.definition.v1_1.BusinessKnowledgeModelConverter) Child(org.kie.workbench.common.stunner.core.graph.content.relationship.Child) ItemDefinition(org.kie.workbench.common.dmn.api.definition.model.ItemDefinition) PointUtils.heightOfShape(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.PointUtils.heightOfShape) Stream(java.util.stream.Stream) PointUtils(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.PointUtils) QName(javax.xml.namespace.QName) PointUtils.widthOfShape(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.PointUtils.widthOfShape) DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) PointUtils.yOfShape(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.PointUtils.yOfShape) DMNDefinitionSet(org.kie.workbench.common.dmn.api.DMNDefinitionSet) DMNStyle(org.kie.dmn.model.api.dmndi.DMNStyle) Point(org.kie.dmn.model.api.dmndi.Point) Connection(org.kie.workbench.common.stunner.core.graph.content.view.Connection) Color(org.kie.dmn.model.api.dmndi.Color) ItemDefinitionPropertyConverter(org.kie.workbench.common.dmn.backend.definition.v1_1.ItemDefinitionPropertyConverter) DiagramMetadataMarshaller(org.kie.workbench.common.stunner.core.definition.service.DiagramMetadataMarshaller) ArrayList(java.util.ArrayList) FontSetPropertyConverter(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.FontSetPropertyConverter) Id(org.kie.workbench.common.dmn.api.property.dmn.Id) PointUtils.upperLeftBound(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.PointUtils.upperLeftBound) BiConsumer(java.util.function.BiConsumer) StreamSupport(java.util.stream.StreamSupport) BindableAdapterUtils(org.kie.workbench.common.stunner.core.definition.adapter.binding.BindableAdapterUtils) IOException(java.io.IOException) InputData(org.kie.workbench.common.dmn.api.definition.model.InputData) InputStreamReader(java.io.InputStreamReader) InputDataConverter(org.kie.workbench.common.dmn.backend.definition.v1_1.InputDataConverter) Association(org.kie.workbench.common.dmn.api.definition.model.Association) DecisionService(org.kie.workbench.common.dmn.api.definition.model.DecisionService) KnowledgeSourceConverter(org.kie.workbench.common.dmn.backend.definition.v1_1.KnowledgeSourceConverter) Graph(org.kie.workbench.common.stunner.core.graph.Graph) MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) TextAnnotation(org.kie.workbench.common.dmn.api.definition.model.TextAnnotation) Definitions(org.kie.workbench.common.dmn.api.definition.model.Definitions) DecisionConverter(org.kie.workbench.common.dmn.backend.definition.v1_1.DecisionConverter) DefinitionUtils(org.kie.workbench.common.stunner.core.util.DefinitionUtils) DMNDecisionServiceDividerLine(org.kie.dmn.model.api.dmndi.DMNDecisionServiceDividerLine) DMNEdge(org.kie.dmn.model.api.dmndi.DMNEdge) UUID(org.kie.workbench.common.stunner.core.util.UUID) BorderColour(org.kie.workbench.common.dmn.api.property.styling.BorderColour) EdgeImpl(org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl) TextAnnotationConverter(org.kie.workbench.common.dmn.backend.definition.v1_1.TextAnnotationConverter) DMNElement(org.kie.workbench.common.dmn.api.definition.model.DMNElement) Collectors.toMap(java.util.stream.Collectors.toMap) PointUtils.lowerRightBound(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.PointUtils.lowerRightBound) InformationRequirement(org.kie.workbench.common.dmn.api.definition.model.InformationRequirement) DiagramElement(org.kie.dmn.model.v1_2.dmndi.DiagramElement) Collections.emptyList(java.util.Collections.emptyList) DiagramMarshaller(org.kie.workbench.common.stunner.core.definition.service.DiagramMarshaller) Import(org.kie.dmn.model.api.Import) DMNDiagram(org.kie.workbench.common.dmn.api.definition.model.DMNDiagram) Collectors(java.util.stream.Collectors) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) Objects(java.util.Objects) DRGElement(org.kie.workbench.common.dmn.api.definition.model.DRGElement) List(java.util.List) DMNShape(org.kie.dmn.model.api.dmndi.DMNShape) ComponentWidths(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths) BgColour(org.kie.workbench.common.dmn.api.property.styling.BgColour) DMNDI(org.kie.dmn.model.v1_2.dmndi.DMNDI) Entry(java.util.Map.Entry) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Node(org.kie.workbench.common.stunner.core.graph.Node) PointUtils.xOfShape(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.PointUtils.xOfShape) PMMLDocumentMetadata(org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata) Width(org.kie.workbench.common.dmn.api.property.dimensions.Width) ControlPoint(org.kie.workbench.common.stunner.core.graph.content.view.ControlPoint) RectangleDimensionsSet(org.kie.workbench.common.dmn.api.property.dimensions.RectangleDimensionsSet) DefinitionsConverter(org.kie.workbench.common.dmn.backend.definition.v1_1.DefinitionsConverter) HashMap(java.util.HashMap) View(org.kie.workbench.common.stunner.core.graph.content.view.View) ColorUtils(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ColorUtils) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ComponentsWidthsExtension(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentsWidthsExtension) Bound(org.kie.workbench.common.stunner.core.graph.content.Bound) BindableAdapterUtils.getDefinitionId(org.kie.workbench.common.stunner.core.definition.adapter.binding.BindableAdapterUtils.getDefinitionId) KnowledgeRequirement(org.kie.workbench.common.dmn.api.definition.model.KnowledgeRequirement) Description(org.kie.workbench.common.dmn.api.property.dmn.Description) DMNViewDefinition(org.kie.workbench.common.dmn.api.definition.DMNViewDefinition) SimpleEntry(java.util.AbstractMap.SimpleEntry) DecisionServiceDividerLineY(org.kie.workbench.common.dmn.api.property.dmn.DecisionServiceDividerLineY) StylingSet(org.kie.workbench.common.dmn.api.property.styling.StylingSet) XMLEncoderDiagramMetadataMarshaller(org.kie.workbench.common.stunner.core.backend.service.XMLEncoderDiagramMetadataMarshaller) AssociationConverter(org.kie.workbench.common.dmn.backend.definition.v1_1.AssociationConverter) DMNMarshallerImportsHelperStandalone(org.kie.workbench.common.dmn.backend.common.DMNMarshallerImportsHelperStandalone) DecisionServiceConverter(org.kie.workbench.common.dmn.backend.definition.v1_1.DecisionServiceConverter) ViewConnector(org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector) AuthorityRequirement(org.kie.workbench.common.dmn.api.definition.model.AuthorityRequirement) Consumer(java.util.function.Consumer) DMNModelInstrumentedBase(org.kie.workbench.common.dmn.api.definition.model.DMNModelInstrumentedBase) KnowledgeSource(org.kie.workbench.common.dmn.api.definition.model.KnowledgeSource) Decision(org.kie.workbench.common.dmn.api.definition.model.Decision) DiscreteConnection(org.kie.workbench.common.stunner.core.graph.content.view.DiscreteConnection) InputStream(java.io.InputStream) Import(org.kie.dmn.model.api.Import) HashMap(java.util.HashMap) Node(org.kie.workbench.common.stunner.core.graph.Node) ArrayList(java.util.ArrayList) DecisionService(org.kie.workbench.common.dmn.api.definition.model.DecisionService) KnowledgeSource(org.kie.workbench.common.dmn.api.definition.model.KnowledgeSource) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) HashSet(java.util.HashSet) DMNDiagram(org.kie.workbench.common.dmn.api.definition.model.DMNDiagram) SimpleEntry(java.util.AbstractMap.SimpleEntry) Graph(org.kie.workbench.common.stunner.core.graph.Graph) Id(org.kie.workbench.common.dmn.api.property.dmn.Id) BindableAdapterUtils.getDefinitionId(org.kie.workbench.common.stunner.core.definition.adapter.binding.BindableAdapterUtils.getDefinitionId) Map(java.util.Map) Collectors.toMap(java.util.stream.Collectors.toMap) HashMap(java.util.HashMap) DRGElement(org.kie.workbench.common.dmn.api.definition.model.DRGElement) Description(org.kie.workbench.common.dmn.api.property.dmn.Description) PMMLDocumentMetadata(org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata) AuthorityRequirement(org.kie.workbench.common.dmn.api.definition.model.AuthorityRequirement) HasComponentWidths(org.kie.workbench.common.dmn.api.definition.HasComponentWidths) DMNShape(org.kie.dmn.model.api.dmndi.DMNShape) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) Association(org.kie.workbench.common.dmn.api.definition.model.Association) TextAnnotation(org.kie.workbench.common.dmn.api.definition.model.TextAnnotation) InputStreamReader(java.io.InputStreamReader) Definitions(org.kie.workbench.common.dmn.api.definition.model.Definitions) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Decision(org.kie.workbench.common.dmn.api.definition.model.Decision) KnowledgeRequirement(org.kie.workbench.common.dmn.api.definition.model.KnowledgeRequirement) BusinessKnowledgeModel(org.kie.workbench.common.dmn.api.definition.model.BusinessKnowledgeModel) ComponentsWidthsExtension(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentsWidthsExtension) Edge(org.kie.workbench.common.stunner.core.graph.Edge) DMNEdge(org.kie.dmn.model.api.dmndi.DMNEdge)

Example 27 with DMNDiagram

use of org.kie.workbench.common.dmn.api.definition.model.DMNDiagram in project kie-wb-common by kiegroup.

the class DMNUnmarshaller method unmarshall.

private Promise<Graph> unmarshall(final Metadata metadata, final JSITDefinitions dmnDefinitions, final Map<JSITImport, JSITDefinitions> importDefinitions, final Map<JSITImport, PMMLDocumentMetadata> pmmlDocuments) {
    final Map<String, HasComponentWidths> hasComponentWidthsMap = new HashMap<>();
    final BiConsumer<String, HasComponentWidths> hasComponentWidthsConsumer = (uuid, hcw) -> {
        if (Objects.nonNull(uuid)) {
            hasComponentWidthsMap.put(uuid, hcw);
        }
    };
    // Check before the DRG creation ('ensureDRGElementExists').
    final boolean isDMNDIPresent = Optional.ofNullable(dmnDefinitions.getDMNDI()).isPresent();
    ensureDRGElementExists(dmnDefinitions);
    final Definitions wbDefinitions = DefinitionsConverter.wbFromDMN(dmnDefinitions, importDefinitions, pmmlDocuments);
    final List<NodeEntry> nodeEntries = modelToStunnerConverter.makeNodes(dmnDefinitions, importDefinitions, isDMNDIPresent, hasComponentWidthsConsumer);
    final List<JSITDecisionService> dmnDecisionServices = getDecisionServices(nodeEntries);
    // Ensure all locations are updated to relative for Stunner
    nodeEntries.forEach(e -> PointUtils.convertToRelativeBounds(e.getNode()));
    final Map<String, Diagram> stunnerDiagramsById = new HashMap<>();
    final Map<String, DMNDiagramElement> dmnDiagramsById = new HashMap<>();
    for (final DMNDiagramElement dmnDiagramElement : wbDefinitions.getDiagramElements()) {
        final String dmnDiagramId = dmnDiagramElement.getId().getValue();
        final Diagram value = factoryManager.newDiagram(dmnDiagramId, BindableAdapterUtils.getDefinitionSetId(DMNDefinitionSet.class), metadata);
        stunnerDiagramsById.put(dmnDiagramId, value);
        dmnDiagramsById.put(dmnDiagramId, dmnDiagramElement);
    }
    final DMNDiagramsSessionState state = dmnDiagramsSession.setState(metadata, stunnerDiagramsById, dmnDiagramsById);
    nodeEntries.forEach(nodeEntry -> {
        final String diagramId = nodeEntry.getDiagramId();
        final Graph graph = stunnerDiagramsById.get(diagramId).getGraph();
        graph.addNode(nodeEntry.getNode());
    });
    final Graph drgGraph = state.getDRGDiagram().getGraph();
    loadImportedItemDefinitions(wbDefinitions, importDefinitions);
    for (final Diagram value : stunnerDiagramsById.values()) {
        final Node<?, ?> dmnDiagramRoot = DMNGraphUtils.findDMNDiagramRoot(value.getGraph());
        ((View<DMNDiagram>) dmnDiagramRoot.getContent()).getDefinition().setDefinitions(wbDefinitions);
        nodeEntries.forEach(nodeEntry -> {
            if (Objects.equals(stunnerDiagramsById.get(nodeEntry.getDiagramId()), value)) {
                connectRootWithChild(dmnDiagramRoot, nodeEntry.getNode());
            }
        });
    }
    // Only connect Nodes to the Diagram that are not referenced by DecisionServices
    final List<String> references = new ArrayList<>();
    final List<JSITDecisionService> lstDecisionServices = new ArrayList<>(dmnDecisionServices);
    for (int iDS = 0; iDS < lstDecisionServices.size(); iDS++) {
        final JSITDecisionService jsiDecisionService = Js.uncheckedCast(lstDecisionServices.get(iDS));
        final List<JSITDMNElementReference> jsiEncapsulatedDecisions = jsiDecisionService.getEncapsulatedDecision();
        if (Objects.nonNull(jsiEncapsulatedDecisions)) {
            for (int i = 0; i < jsiEncapsulatedDecisions.size(); i++) {
                final JSITDMNElementReference jsiEncapsulatedDecision = Js.uncheckedCast(jsiEncapsulatedDecisions.get(i));
                references.add(jsiEncapsulatedDecision.getHref());
            }
        }
        final List<JSITDMNElementReference> jsiOutputDecisions = jsiDecisionService.getOutputDecision();
        if (Objects.nonNull(jsiOutputDecisions)) {
            for (int i = 0; i < jsiOutputDecisions.size(); i++) {
                final JSITDMNElementReference jsiOutputDecision = Js.uncheckedCast(jsiOutputDecisions.get(i));
                references.add(jsiOutputDecision.getHref());
            }
        }
    }
    // Copy ComponentWidths information
    final List<JSITComponentsWidthsExtension> extensions = findComponentsWidthsExtensions(dmnDefinitions.getDMNDI().getDMNDiagram());
    extensions.forEach(componentsWidthsExtension -> {
        // can be imported from another diagram but the extension is not imported or present in this diagram.
        if (Objects.nonNull(componentsWidthsExtension.getComponentWidths())) {
            hasComponentWidthsMap.entrySet().forEach(es -> {
                final List<JSITComponentWidths> jsiComponentWidths = componentsWidthsExtension.getComponentWidths();
                for (int i = 0; i < jsiComponentWidths.size(); i++) {
                    final JSITComponentWidths jsiWidths = Js.uncheckedCast(jsiComponentWidths.get(i));
                    if (Objects.equals(jsiWidths.getDmnElementRef(), es.getKey())) {
                        final List<Double> widths = es.getValue().getComponentWidths();
                        if (Objects.nonNull(jsiWidths.getWidth())) {
                            widths.clear();
                            for (int w = 0; w < jsiWidths.getWidth().size(); w++) {
                                final double width = jsiWidths.getWidth().get(w).doubleValue();
                                widths.add(width);
                            }
                        }
                    }
                }
            });
        }
    });
    return promises.resolve(drgGraph);
}
Also used : JSIDMNDiagram(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmndi12.JSIDMNDiagram) Definitions(org.kie.workbench.common.dmn.api.definition.model.Definitions) Edge(org.kie.workbench.common.stunner.core.graph.Edge) UUID(org.kie.workbench.common.stunner.core.util.UUID) EdgeImpl(org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl) Map(java.util.Map) DMNDiagramElementsUtils(org.kie.workbench.common.dmn.client.marshaller.common.DMNDiagramElementsUtils) JSITDMNElementReference(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDMNElementReference) FactoryManager(org.kie.workbench.common.stunner.core.api.FactoryManager) Diagram(org.kie.workbench.common.stunner.core.diagram.Diagram) HasComponentWidths(org.kie.workbench.common.dmn.api.definition.HasComponentWidths) Child(org.kie.workbench.common.stunner.core.graph.content.relationship.Child) ItemDefinition(org.kie.workbench.common.dmn.api.definition.model.ItemDefinition) DMNDiagram(org.kie.workbench.common.dmn.api.definition.model.DMNDiagram) Collectors(java.util.stream.Collectors) JSITComponentWidths(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentWidths) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) Objects(java.util.Objects) JSITDecisionService(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDecisionService) List(java.util.List) DMNDiagramsSession(org.kie.workbench.common.dmn.client.docks.navigator.drds.DMNDiagramsSession) DMNGraphUtils(org.kie.workbench.common.dmn.client.marshaller.common.DMNGraphUtils) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) DMNDiagramElement(org.kie.workbench.common.dmn.api.definition.model.DMNDiagramElement) Node(org.kie.workbench.common.stunner.core.graph.Node) PMMLDocumentMetadata(org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata) JSIDiagramElement(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.di.JSIDiagramElement) DMNDefinitionSet(org.kie.workbench.common.dmn.api.DMNDefinitionSet) Promises(org.uberfire.client.promise.Promises) HashMap(java.util.HashMap) Promise(elemental2.promise.Promise) View(org.kie.workbench.common.stunner.core.graph.content.view.View) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Js(jsinterop.base.Js) NodeEntry(org.kie.workbench.common.dmn.client.marshaller.unmarshall.nodes.NodeEntry) DMNMarshallerImportsClientHelper(org.kie.workbench.common.dmn.client.marshaller.included.DMNMarshallerImportsClientHelper) BiConsumer(java.util.function.BiConsumer) NodeEntriesFactory(org.kie.workbench.common.dmn.client.marshaller.unmarshall.nodes.NodeEntriesFactory) JSITDefinitions(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDefinitions) BindableAdapterUtils(org.kie.workbench.common.stunner.core.definition.adapter.binding.BindableAdapterUtils) MainJs(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.MainJs) DMNDiagramsSessionState(org.kie.workbench.common.dmn.client.docks.navigator.drds.DMNDiagramsSessionState) JSITItemDefinition(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITItemDefinition) DefinitionsConverter(org.kie.workbench.common.dmn.client.marshaller.converters.DefinitionsConverter) PointUtils(org.kie.workbench.common.dmn.client.marshaller.converters.dd.PointUtils) DMNModelInstrumentedBase(org.kie.workbench.common.dmn.api.definition.model.DMNModelInstrumentedBase) Graph(org.kie.workbench.common.stunner.core.graph.Graph) JsUtils(org.kie.workbench.common.dmn.webapp.kogito.marshaller.mapper.JsUtils) ItemDefinitionPropertyConverter(org.kie.workbench.common.dmn.client.marshaller.converters.ItemDefinitionPropertyConverter) JSITImport(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITImport) JSITComponentsWidthsExtension(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentsWidthsExtension) JSITComponentsWidthsExtension(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentsWidthsExtension) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HasComponentWidths(org.kie.workbench.common.dmn.api.definition.HasComponentWidths) JSITDecisionService(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDecisionService) DMNDiagramElement(org.kie.workbench.common.dmn.api.definition.model.DMNDiagramElement) DMNDiagramsSessionState(org.kie.workbench.common.dmn.client.docks.navigator.drds.DMNDiagramsSessionState) JSITComponentWidths(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentWidths) JSIDMNDiagram(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmndi12.JSIDMNDiagram) DMNDiagram(org.kie.workbench.common.dmn.api.definition.model.DMNDiagram) Definitions(org.kie.workbench.common.dmn.api.definition.model.Definitions) JSITDefinitions(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDefinitions) JSITDMNElementReference(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDMNElementReference) DMNDefinitionSet(org.kie.workbench.common.dmn.api.DMNDefinitionSet) JSIDMNDiagram(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmndi12.JSIDMNDiagram) Diagram(org.kie.workbench.common.stunner.core.diagram.Diagram) DMNDiagram(org.kie.workbench.common.dmn.api.definition.model.DMNDiagram) Graph(org.kie.workbench.common.stunner.core.graph.Graph) NodeEntry(org.kie.workbench.common.dmn.client.marshaller.unmarshall.nodes.NodeEntry)

Example 28 with DMNDiagram

use of org.kie.workbench.common.dmn.api.definition.model.DMNDiagram in project kie-wb-common by kiegroup.

the class DMNUnmarshaller method connectRootWithChild.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void connectRootWithChild(final Node dmnDiagramRoot, final Node child) {
    final String uuid = UUID.uuid();
    final Edge<Child, Node> edge = new EdgeImpl<>(uuid);
    edge.setContent(new Child());
    connectEdge(edge, dmnDiagramRoot, child);
    final Definitions definitions = ((DMNDiagram) ((View) dmnDiagramRoot.getContent()).getDefinition()).getDefinitions();
    final DMNModelInstrumentedBase childDRG = (DMNModelInstrumentedBase) ((View) child.getContent()).getDefinition();
    childDRG.setParent(definitions);
}
Also used : JSIDMNDiagram(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmndi12.JSIDMNDiagram) DMNDiagram(org.kie.workbench.common.dmn.api.definition.model.DMNDiagram) Node(org.kie.workbench.common.stunner.core.graph.Node) Definitions(org.kie.workbench.common.dmn.api.definition.model.Definitions) JSITDefinitions(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDefinitions) DMNModelInstrumentedBase(org.kie.workbench.common.dmn.api.definition.model.DMNModelInstrumentedBase) EdgeImpl(org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl) Child(org.kie.workbench.common.stunner.core.graph.content.relationship.Child)

Example 29 with DMNDiagram

use of org.kie.workbench.common.dmn.api.definition.model.DMNDiagram in project kie-wb-common by kiegroup.

the class DMNMarshallerTest method testWithIncludedModelsWhenNodeParentIsDMNDiagram.

@Test
public void testWithIncludedModelsWhenNodeParentIsDMNDiagram() {
    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<>();
    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));
}
Also used : JSIDMNDiagram(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmndi12.JSIDMNDiagram) DMNDiagram(org.kie.workbench.common.dmn.api.definition.model.DMNDiagram) Import(org.kie.workbench.common.dmn.api.definition.model.Import) Node(org.kie.workbench.common.stunner.core.graph.Node) Definitions(org.kie.workbench.common.dmn.api.definition.model.Definitions) JSITDefinitions(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDefinitions) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) ArrayList(java.util.ArrayList) JSITDRGElement(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDRGElement) DRGElement(org.kie.workbench.common.dmn.api.definition.model.DRGElement) Test(org.junit.Test)

Example 30 with DMNDiagram

use of org.kie.workbench.common.dmn.api.definition.model.DMNDiagram in project kie-wb-common by kiegroup.

the class DRDContextMenuServiceTest method testRemoveFromCurrentDRD.

@Test
public void testRemoveFromCurrentDRD() {
    final String nodeUUID = "UUID";
    final Node node = mock(Node.class);
    final Diagram diagram = mock(Diagram.class);
    final DMNDiagramElement dmnDiagram = mock(DMNDiagramElement.class);
    when(node.getUUID()).thenReturn(nodeUUID);
    when(dmnDiagramsSession.getCurrentDiagram()).thenReturn(Optional.of(diagram));
    when(dmnDiagramsSession.getCurrentDMNDiagramElement()).thenReturn(Optional.of(dmnDiagram));
    when(diagram.getGraph()).thenReturn(graph);
    drdContextMenuService.removeFromCurrentDRD(singleton(node));
    verify(graph, times(1)).removeNode(nodeUUID);
    verify(selectedEvent, times(1)).fire(Mockito.<DMNDiagramSelected>any());
}
Also used : Node(org.kie.workbench.common.stunner.core.graph.Node) DMNDiagramElement(org.kie.workbench.common.dmn.api.definition.model.DMNDiagramElement) Diagram(org.kie.workbench.common.stunner.core.diagram.Diagram) DMNDiagram(org.kie.workbench.common.dmn.api.definition.model.DMNDiagram) Test(org.junit.Test)

Aggregations

DMNDiagram (org.kie.workbench.common.dmn.api.definition.model.DMNDiagram)33 Node (org.kie.workbench.common.stunner.core.graph.Node)25 Definitions (org.kie.workbench.common.dmn.api.definition.model.Definitions)19 Diagram (org.kie.workbench.common.stunner.core.diagram.Diagram)17 Test (org.junit.Test)16 View (org.kie.workbench.common.stunner.core.graph.content.view.View)16 Edge (org.kie.workbench.common.stunner.core.graph.Edge)13 Graph (org.kie.workbench.common.stunner.core.graph.Graph)12 DMNModelInstrumentedBase (org.kie.workbench.common.dmn.api.definition.model.DMNModelInstrumentedBase)10 Decision (org.kie.workbench.common.dmn.api.definition.model.Decision)10 ArrayList (java.util.ArrayList)9 Definition (org.kie.workbench.common.stunner.core.graph.content.definition.Definition)8 HashMap (java.util.HashMap)7 Optional (java.util.Optional)7 DMNDefinitionSet (org.kie.workbench.common.dmn.api.DMNDefinitionSet)7 DMNDiagramElement (org.kie.workbench.common.dmn.api.definition.model.DMNDiagramElement)7 Name (org.kie.workbench.common.dmn.api.property.dmn.Name)7 Child (org.kie.workbench.common.stunner.core.graph.content.relationship.Child)7 ViewConnector (org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector)7 EdgeImpl (org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl)7