Search in sources :

Example 1 with ComponentWidths

use of org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths 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);
}
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) HashMap(java.util.HashMap) Node(org.kie.workbench.common.stunner.core.graph.Node) ArrayList(java.util.ArrayList) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) DMNElement(org.kie.workbench.common.dmn.api.definition.model.DMNElement) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) DMNDiagram(org.kie.workbench.common.dmn.api.definition.model.DMNDiagram) ViewConnector(org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector) DMNEdge(org.kie.dmn.model.api.dmndi.DMNEdge) DRGElement(org.kie.workbench.common.dmn.api.definition.model.DRGElement) DiscreteConnection(org.kie.workbench.common.stunner.core.graph.content.view.DiscreteConnection) DMNViewDefinition(org.kie.workbench.common.dmn.api.definition.DMNViewDefinition) DMNDI(org.kie.dmn.model.v1_2.dmndi.DMNDI) DiagramElement(org.kie.dmn.model.v1_2.dmndi.DiagramElement) Association(org.kie.workbench.common.dmn.api.definition.model.Association) TextAnnotation(org.kie.workbench.common.dmn.api.definition.model.TextAnnotation) QName(javax.xml.namespace.QName) Definitions(org.kie.workbench.common.dmn.api.definition.model.Definitions) ControlPoint(org.kie.workbench.common.stunner.core.graph.content.view.ControlPoint) View(org.kie.workbench.common.stunner.core.graph.content.view.View) HasComponentWidths(org.kie.workbench.common.dmn.api.definition.HasComponentWidths) ComponentWidths(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths) 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 2 with ComponentWidths

use of org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths in project kie-wb-common by kiegroup.

the class BusinessKnowledgeModelConverter method dmnFromNode.

@Override
public org.kie.dmn.model.api.BusinessKnowledgeModel dmnFromNode(final Node<View<BusinessKnowledgeModel>, ?> node, final Consumer<ComponentWidths> componentWidthsConsumer) {
    final BusinessKnowledgeModel source = (BusinessKnowledgeModel) DefinitionUtils.getElementDefinition(node);
    final org.kie.dmn.model.api.BusinessKnowledgeModel result = new org.kie.dmn.model.v1_2.TBusinessKnowledgeModel();
    result.setId(source.getId().getValue());
    result.setDescription(DescriptionPropertyConverter.dmnFromWB(source.getDescription()));
    result.setName(source.getName().getValue());
    DMNExternalLinksToExtensionElements.loadExternalLinksIntoExtensionElements(source, result);
    final org.kie.dmn.model.api.InformationItem variable = InformationItemPrimaryPropertyConverter.dmnFromWB(source.getVariable(), source);
    if (variable != null) {
        variable.setParent(result);
    }
    result.setVariable(variable);
    final org.kie.dmn.model.api.FunctionDefinition functionDefinition = FunctionDefinitionPropertyConverter.dmnFromWB(source.getEncapsulatedLogic(), componentWidthsConsumer);
    final FunctionDefinition wbFunctionDefinition = source.getEncapsulatedLogic();
    if (Objects.nonNull(wbFunctionDefinition)) {
        final String uuid = wbFunctionDefinition.getId().getValue();
        if (Objects.nonNull(uuid)) {
            final ComponentWidths componentWidths = new ComponentWidths();
            componentWidths.setDmnElementRef(new QName(uuid));
            componentWidths.setWidths(new ArrayList<>(source.getEncapsulatedLogic().getComponentWidths()));
            componentWidthsConsumer.accept(componentWidths);
        }
    }
    if (functionDefinition != null) {
        functionDefinition.setParent(result);
    }
    result.setEncapsulatedLogic(functionDefinition);
    // DMN spec table 2: Requirements connection rules
    final List<Edge<?, ?>> inEdges = (List<Edge<?, ?>>) node.getInEdges();
    for (Edge<?, ?> e : inEdges) {
        final Node<?, ?> sourceNode = e.getSourceNode();
        if (sourceNode.getContent() instanceof View<?>) {
            final View<?> view = (View<?>) sourceNode.getContent();
            if (view.getDefinition() instanceof DRGElement) {
                final DRGElement drgElement = (DRGElement) view.getDefinition();
                if (drgElement instanceof BusinessKnowledgeModel) {
                    final org.kie.dmn.model.api.KnowledgeRequirement iReq = new org.kie.dmn.model.v1_2.TKnowledgeRequirement();
                    iReq.setId(e.getUUID());
                    final org.kie.dmn.model.api.DMNElementReference ri = new org.kie.dmn.model.v1_2.TDMNElementReference();
                    ri.setHref(getHref(drgElement));
                    iReq.setRequiredKnowledge(ri);
                    result.getKnowledgeRequirement().add(iReq);
                } else if (drgElement instanceof KnowledgeSource) {
                    final org.kie.dmn.model.api.AuthorityRequirement iReq = new org.kie.dmn.model.v1_2.TAuthorityRequirement();
                    iReq.setId(e.getUUID());
                    final org.kie.dmn.model.api.DMNElementReference ri = new org.kie.dmn.model.v1_2.TDMNElementReference();
                    ri.setHref(getHref(drgElement));
                    iReq.setRequiredAuthority(ri);
                    result.getAuthorityRequirement().add(iReq);
                } else if (drgElement instanceof DecisionService) {
                    if (e.getContent() instanceof View && ((View) e.getContent()).getDefinition() instanceof KnowledgeRequirement) {
                        final org.kie.dmn.model.api.KnowledgeRequirement iReq = new org.kie.dmn.model.v1_2.TKnowledgeRequirement();
                        iReq.setId(e.getUUID());
                        final org.kie.dmn.model.api.DMNElementReference ri = new org.kie.dmn.model.v1_2.TDMNElementReference();
                        ri.setHref(getHref(drgElement));
                        iReq.setRequiredKnowledge(ri);
                        result.getKnowledgeRequirement().add(iReq);
                    } else {
                        throw new UnsupportedOperationException("wrong model definition.");
                    }
                } else {
                    throw new UnsupportedOperationException("wrong model definition.");
                }
            }
        }
    }
    return result;
}
Also used : DecisionService(org.kie.workbench.common.dmn.api.definition.model.DecisionService) KnowledgeSource(org.kie.workbench.common.dmn.api.definition.model.KnowledgeSource) FunctionDefinition(org.kie.workbench.common.dmn.api.definition.model.FunctionDefinition) ArrayList(java.util.ArrayList) List(java.util.List) QName(javax.xml.namespace.QName) View(org.kie.workbench.common.stunner.core.graph.content.view.View) KnowledgeRequirement(org.kie.workbench.common.dmn.api.definition.model.KnowledgeRequirement) BusinessKnowledgeModel(org.kie.workbench.common.dmn.api.definition.model.BusinessKnowledgeModel) HasComponentWidths(org.kie.workbench.common.dmn.api.definition.HasComponentWidths) ComponentWidths(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths) Edge(org.kie.workbench.common.stunner.core.graph.Edge) DRGElement(org.kie.workbench.common.dmn.api.definition.model.DRGElement)

Example 3 with ComponentWidths

use of org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths in project kie-wb-common by kiegroup.

the class ExpressionPropertyConverter method dmnFromWB.

public static org.kie.dmn.model.api.Expression dmnFromWB(final Expression wb, final Consumer<ComponentWidths> componentWidthsConsumer) {
    if (Objects.isNull(wb)) {
        return null;
    }
    final String uuid = wb.getId().getValue();
    if (Objects.nonNull(uuid)) {
        final ComponentWidths componentWidths = new ComponentWidths();
        componentWidths.setDmnElementRef(new QName(uuid));
        componentWidths.setWidths(new ArrayList<>(wb.getComponentWidths()));
        componentWidthsConsumer.accept(componentWidths);
    }
    if (wb instanceof IsLiteralExpression) {
        return LiteralExpressionPropertyConverter.dmnFromWB((IsLiteralExpression) wb);
    } else if (wb instanceof Context) {
        return ContextPropertyConverter.dmnFromWB((Context) wb, componentWidthsConsumer);
    } else if (wb instanceof Relation) {
        return RelationPropertyConverter.dmnFromWB((Relation) wb, componentWidthsConsumer);
    } else if (wb instanceof List) {
        return ListPropertyConverter.dmnFromWB((List) wb, componentWidthsConsumer);
    } else if (wb instanceof Invocation) {
        return InvocationPropertyConverter.dmnFromWB((Invocation) wb, componentWidthsConsumer);
    } else if (wb instanceof FunctionDefinition) {
        return FunctionDefinitionPropertyConverter.dmnFromWB((FunctionDefinition) wb, componentWidthsConsumer);
    } else if (wb instanceof DecisionTable) {
        return DecisionTablePropertyConverter.dmnFromWB((DecisionTable) wb);
    }
    return null;
}
Also used : Context(org.kie.workbench.common.dmn.api.definition.model.Context) DecisionTable(org.kie.workbench.common.dmn.api.definition.model.DecisionTable) Relation(org.kie.workbench.common.dmn.api.definition.model.Relation) IsLiteralExpression(org.kie.workbench.common.dmn.api.definition.model.IsLiteralExpression) Invocation(org.kie.workbench.common.dmn.api.definition.model.Invocation) QName(javax.xml.namespace.QName) List(org.kie.workbench.common.dmn.api.definition.model.List) ArrayList(java.util.ArrayList) FunctionDefinition(org.kie.workbench.common.dmn.api.definition.model.FunctionDefinition) HasComponentWidths(org.kie.workbench.common.dmn.api.definition.HasComponentWidths) ComponentWidths(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths)

Example 4 with ComponentWidths

use of org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths in project kie-wb-common by kiegroup.

the class BusinessKnowledgeModelConverterTest method testDMNFromWB.

@Test
public void testDMNFromWB() {
    final BusinessKnowledgeModel wb = new BusinessKnowledgeModel();
    final LiteralExpression literalExpression = new LiteralExpression();
    final InformationItemPrimary informationItem = new InformationItemPrimary();
    final FunctionDefinition functionDefinition = new FunctionDefinition();
    literalExpression.getComponentWidths().set(0, 200.0);
    literalExpression.getId().setValue(EXPRESSION_UUID);
    functionDefinition.getComponentWidths().set(0, 200.0);
    functionDefinition.setExpression(literalExpression);
    functionDefinition.getId().setValue(FUNCTION_DEFINITION_UUID);
    wb.getId().setValue(DECISION_UUID);
    wb.getName().setValue(DECISION_NAME);
    wb.getDescription().setValue(DECISION_DESCRIPTION);
    wb.setVariable(informationItem);
    wb.setEncapsulatedLogic(functionDefinition);
    final Node<View<BusinessKnowledgeModel>, ?> node = new NodeImpl<>(UUID.uuid());
    final View<BusinessKnowledgeModel> view = new ViewImpl<>(wb, Bounds.create());
    node.setContent(view);
    final org.kie.dmn.model.api.BusinessKnowledgeModel dmn = converter.dmnFromNode(node, componentWidthsConsumer);
    assertThat(dmn).isNotNull();
    assertThat(dmn.getId()).isNotNull();
    assertThat(dmn.getId()).isEqualTo(DECISION_UUID);
    assertThat(dmn.getName()).isNotNull();
    assertThat(dmn.getName()).isEqualTo(DECISION_NAME);
    assertThat(dmn.getDescription()).isNotNull();
    assertThat(dmn.getDescription()).isEqualTo(DECISION_DESCRIPTION);
    assertThat(dmn.getVariable()).isNotNull();
    assertThat(dmn.getVariable().getName()).isEqualTo(DECISION_NAME);
    assertThat(dmn.getEncapsulatedLogic()).isNotNull();
    assertThat(dmn.getEncapsulatedLogic().getExpression()).isNotNull();
    assertThat(dmn.getEncapsulatedLogic().getExpression().getId()).isEqualTo(EXPRESSION_UUID);
    verify(componentWidthsConsumer, times(2)).accept(componentWidthsCaptor.capture());
    final List<ComponentWidths> componentWidths = componentWidthsCaptor.getAllValues();
    assertThat(componentWidths).isNotNull();
    assertThat(componentWidths).hasSize(2);
    final ComponentWidths componentWidths0 = componentWidths.get(0);
    assertThat(componentWidths0.getDmnElementRef().getLocalPart()).isEqualTo(EXPRESSION_UUID);
    assertThat(componentWidths0.getWidths().size()).isEqualTo(literalExpression.getRequiredComponentWidthCount());
    assertThat(componentWidths0.getWidths().get(0)).isEqualTo(200.0);
    final ComponentWidths componentWidths1 = componentWidths.get(1);
    assertThat(componentWidths1.getDmnElementRef().getLocalPart()).isEqualTo(FUNCTION_DEFINITION_UUID);
    assertThat(componentWidths1.getWidths().size()).isEqualTo(functionDefinition.getRequiredComponentWidthCount());
    assertThat(componentWidths1.getWidths().get(0)).isEqualTo(200.0);
}
Also used : InformationItemPrimary(org.kie.workbench.common.dmn.api.definition.model.InformationItemPrimary) NodeImpl(org.kie.workbench.common.stunner.core.graph.impl.NodeImpl) TLiteralExpression(org.kie.dmn.model.v1_2.TLiteralExpression) LiteralExpression(org.kie.workbench.common.dmn.api.definition.model.LiteralExpression) View(org.kie.workbench.common.stunner.core.graph.content.view.View) TBusinessKnowledgeModel(org.kie.dmn.model.v1_2.TBusinessKnowledgeModel) BusinessKnowledgeModel(org.kie.workbench.common.dmn.api.definition.model.BusinessKnowledgeModel) ViewImpl(org.kie.workbench.common.stunner.core.graph.content.view.ViewImpl) FunctionDefinition(org.kie.workbench.common.dmn.api.definition.model.FunctionDefinition) TFunctionDefinition(org.kie.dmn.model.v1_2.TFunctionDefinition) HasComponentWidths(org.kie.workbench.common.dmn.api.definition.HasComponentWidths) ComponentWidths(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths) Test(org.junit.Test)

Example 5 with ComponentWidths

use of org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths in project kie-wb-common by kiegroup.

the class DecisionConverterTest method testDMNFromWB.

@Test
public void testDMNFromWB() {
    final Decision wb = new Decision();
    final LiteralExpression literalExpression = new LiteralExpression();
    final InformationItemPrimary informationItem = new InformationItemPrimary();
    literalExpression.getComponentWidths().set(0, 200.0);
    literalExpression.getId().setValue(EXPRESSION_UUID);
    wb.getId().setValue(DECISION_UUID);
    wb.getName().setValue(DECISION_NAME);
    wb.getDescription().setValue(DECISION_DESCRIPTION);
    wb.setVariable(informationItem);
    wb.setExpression(literalExpression);
    final Node<View<Decision>, ?> node = new NodeImpl<>(UUID.uuid());
    final View<Decision> view = new ViewImpl<>(wb, Bounds.create());
    node.setContent(view);
    final org.kie.dmn.model.api.Decision dmn = converter.dmnFromNode(node, componentWidthsConsumer);
    assertThat(dmn).isNotNull();
    assertThat(dmn.getId()).isNotNull();
    assertThat(dmn.getId()).isEqualTo(DECISION_UUID);
    assertThat(dmn.getName()).isNotNull();
    assertThat(dmn.getName()).isEqualTo(DECISION_NAME);
    assertThat(dmn.getDescription()).isNotNull();
    assertThat(dmn.getDescription()).isEqualTo(DECISION_DESCRIPTION);
    assertThat(dmn.getVariable()).isNotNull();
    assertThat(dmn.getVariable().getName()).isEqualTo(DECISION_NAME);
    assertThat(dmn.getExpression()).isNotNull();
    assertThat(dmn.getExpression().getId()).isEqualTo(EXPRESSION_UUID);
    verify(componentWidthsConsumer).accept(componentWidthsCaptor.capture());
    final ComponentWidths componentWidths = componentWidthsCaptor.getValue();
    assertThat(componentWidths).isNotNull();
    assertThat(componentWidths.getDmnElementRef().getLocalPart()).isEqualTo(EXPRESSION_UUID);
    assertThat(componentWidths.getWidths().size()).isEqualTo(literalExpression.getRequiredComponentWidthCount());
    assertThat(componentWidths.getWidths().get(0)).isEqualTo(200.0);
}
Also used : InformationItemPrimary(org.kie.workbench.common.dmn.api.definition.model.InformationItemPrimary) NodeImpl(org.kie.workbench.common.stunner.core.graph.impl.NodeImpl) TLiteralExpression(org.kie.dmn.model.v1_2.TLiteralExpression) LiteralExpression(org.kie.workbench.common.dmn.api.definition.model.LiteralExpression) View(org.kie.workbench.common.stunner.core.graph.content.view.View) TDecision(org.kie.dmn.model.v1_2.TDecision) Decision(org.kie.workbench.common.dmn.api.definition.model.Decision) ViewImpl(org.kie.workbench.common.stunner.core.graph.content.view.ViewImpl) HasComponentWidths(org.kie.workbench.common.dmn.api.definition.HasComponentWidths) ComponentWidths(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths) Test(org.junit.Test)

Aggregations

HasComponentWidths (org.kie.workbench.common.dmn.api.definition.HasComponentWidths)16 ComponentWidths (org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths)16 Test (org.junit.Test)10 TLiteralExpression (org.kie.dmn.model.v1_2.TLiteralExpression)9 LiteralExpression (org.kie.workbench.common.dmn.api.definition.model.LiteralExpression)9 View (org.kie.workbench.common.stunner.core.graph.content.view.View)6 ArrayList (java.util.ArrayList)5 List (java.util.List)4 DRGElement (org.kie.workbench.common.dmn.api.definition.model.DRGElement)4 Decision (org.kie.workbench.common.dmn.api.definition.model.Decision)4 DecisionService (org.kie.workbench.common.dmn.api.definition.model.DecisionService)4 FunctionDefinition (org.kie.workbench.common.dmn.api.definition.model.FunctionDefinition)4 KnowledgeRequirement (org.kie.workbench.common.dmn.api.definition.model.KnowledgeRequirement)4 Edge (org.kie.workbench.common.stunner.core.graph.Edge)4 BiConsumer (java.util.function.BiConsumer)3 Consumer (java.util.function.Consumer)3 Collectors (java.util.stream.Collectors)3 QName (javax.xml.namespace.QName)3 BusinessKnowledgeModel (org.kie.workbench.common.dmn.api.definition.model.BusinessKnowledgeModel)3 IOException (java.io.IOException)2