Search in sources :

Example 6 with KnowledgeRequirement

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

the class DecisionServiceConverter method dmnFromNode.

@Override
@SuppressWarnings("unchecked")
public org.kie.dmn.model.api.DecisionService dmnFromNode(final Node<View<DecisionService>, ?> node, final Consumer<ComponentWidths> componentWidthsConsumer) {
    final DecisionService source = (DecisionService) DefinitionUtils.getElementDefinition(node);
    final org.kie.dmn.model.api.DecisionService ds = new org.kie.dmn.model.v1_2.TDecisionService();
    ds.setId(source.getId().getValue());
    ds.setDescription(DescriptionPropertyConverter.dmnFromWB(source.getDescription()));
    ds.setName(source.getName().getValue());
    final org.kie.dmn.model.api.InformationItem variable = InformationItemPrimaryPropertyConverter.dmnFromWB(source.getVariable(), source);
    if (variable != null) {
        variable.setParent(ds);
    }
    ds.setVariable(variable);
    final List<org.kie.dmn.model.api.DMNElementReference> existing_outputDecision = source.getOutputDecision().stream().map(DMNElementReferenceConverter::dmnFromWB).collect(Collectors.toList());
    final List<org.kie.dmn.model.api.DMNElementReference> existing_encapsulatedDecision = source.getEncapsulatedDecision().stream().map(DMNElementReferenceConverter::dmnFromWB).collect(Collectors.toList());
    final List<org.kie.dmn.model.api.DMNElementReference> existing_inputDecision = source.getInputDecision().stream().map(DMNElementReferenceConverter::dmnFromWB).collect(Collectors.toList());
    final List<org.kie.dmn.model.api.DMNElementReference> existing_inputData = source.getInputData().stream().map(DMNElementReferenceConverter::dmnFromWB).collect(Collectors.toList());
    final List<org.kie.dmn.model.api.DMNElementReference> candidate_outputDecision = new ArrayList<>();
    final List<org.kie.dmn.model.api.DMNElementReference> candidate_encapsulatedDecision = new ArrayList<>();
    final List<org.kie.dmn.model.api.DMNElementReference> candidate_inputDecision = new ArrayList<>();
    final List<org.kie.dmn.model.api.DMNElementReference> candidate_inputData = new ArrayList<>();
    final List<InputData> reqInputs = new ArrayList<>();
    final List<Decision> reqDecisions = new ArrayList<>();
    // DMN spec table 2: Requirements connection rules
    final List<Edge<?, ?>> outEdges = (List<Edge<?, ?>>) node.getOutEdges();
    for (Edge<?, ?> e : outEdges) {
        if (e.getContent() instanceof Child) {
            @SuppressWarnings("unchecked") final Node<View<?>, ?> targetNode = e.getTargetNode();
            final View<?> targetNodeView = targetNode.getContent();
            if (targetNodeView.getDefinition() instanceof DRGElement) {
                final DRGElement drgElement = (DRGElement) targetNodeView.getDefinition();
                if (drgElement instanceof Decision) {
                    final Decision decision = (Decision) drgElement;
                    final org.kie.dmn.model.api.DMNElementReference ri = new org.kie.dmn.model.v1_2.TDMNElementReference();
                    ri.setHref(new StringBuilder("#").append(decision.getId().getValue()).toString());
                    if (isOutputDecision(targetNode.getContent(), node.getContent())) {
                        candidate_outputDecision.add(ri);
                    } else {
                        candidate_encapsulatedDecision.add(ri);
                    }
                    inspectDecisionForDSReqs(targetNode, reqInputs, reqDecisions);
                } else {
                    throw new UnsupportedOperationException("wrong model definition: a DecisionService is expected to encapsulate only Decision");
                }
            }
        } else if (e.getContent() instanceof View && ((View) e.getContent()).getDefinition() instanceof KnowledgeRequirement) {
        // this was taken care by the receiving Decision or BKM.
        } else {
            throw new UnsupportedOperationException("wrong model definition.");
        }
    }
    reqInputs.stream().sorted(Comparator.comparing(x -> x.getName().getValue())).map(x -> {
        final org.kie.dmn.model.api.DMNElementReference ri = new org.kie.dmn.model.v1_2.TDMNElementReference();
        ri.setHref(new StringBuilder("#").append(x.getId().getValue()).toString());
        return ri;
    }).forEach(candidate_inputData::add);
    reqDecisions.stream().sorted(Comparator.comparing(x -> x.getName().getValue())).map(x -> {
        final org.kie.dmn.model.api.DMNElementReference ri = new org.kie.dmn.model.v1_2.TDMNElementReference();
        ri.setHref(new StringBuilder("#").append(x.getId().getValue()).toString());
        return ri;
    }).forEach(candidate_inputDecision::add);
    for (org.kie.dmn.model.api.DMNElementReference er : candidate_outputDecision) {
        candidate_inputDecision.removeIf(x -> x.getHref().equals(er.getHref()));
    }
    for (org.kie.dmn.model.api.DMNElementReference er : candidate_encapsulatedDecision) {
        candidate_inputDecision.removeIf(x -> x.getHref().equals(er.getHref()));
    }
    reconcileExistingAndCandidate(ds.getInputData(), existing_inputData, candidate_inputData);
    reconcileExistingAndCandidate(ds.getInputDecision(), existing_inputDecision, candidate_inputDecision);
    reconcileExistingAndCandidate(ds.getEncapsulatedDecision(), existing_encapsulatedDecision, candidate_encapsulatedDecision);
    reconcileExistingAndCandidate(ds.getOutputDecision(), existing_outputDecision, candidate_outputDecision);
    DMNExternalLinksToExtensionElements.loadExternalLinksIntoExtensionElements(source, ds);
    return ds;
}
Also used : DefinitionUtils(org.kie.workbench.common.stunner.core.util.DefinitionUtils) Edge(org.kie.workbench.common.stunner.core.graph.Edge) InformationItemPrimary(org.kie.workbench.common.dmn.api.definition.model.InformationItemPrimary) View(org.kie.workbench.common.stunner.core.graph.content.view.View) ArrayList(java.util.ArrayList) Id(org.kie.workbench.common.dmn.api.property.dmn.Id) Name(org.kie.workbench.common.dmn.api.property.dmn.Name) BindableAdapterUtils.getDefinitionId(org.kie.workbench.common.stunner.core.definition.adapter.binding.BindableAdapterUtils.getDefinitionId) BiConsumer(java.util.function.BiConsumer) KnowledgeRequirement(org.kie.workbench.common.dmn.api.definition.model.KnowledgeRequirement) Description(org.kie.workbench.common.dmn.api.property.dmn.Description) FactoryManager(org.kie.workbench.common.stunner.core.api.FactoryManager) HasComponentWidths(org.kie.workbench.common.dmn.api.definition.HasComponentWidths) DecisionServiceDividerLineY(org.kie.workbench.common.dmn.api.property.dmn.DecisionServiceDividerLineY) StylingSet(org.kie.workbench.common.dmn.api.property.styling.StylingSet) DecisionServiceRectangleDimensionsSet(org.kie.workbench.common.dmn.api.property.dimensions.DecisionServiceRectangleDimensionsSet) Child(org.kie.workbench.common.stunner.core.graph.content.relationship.Child) InputData(org.kie.workbench.common.dmn.api.definition.model.InputData) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) DRGElement(org.kie.workbench.common.dmn.api.definition.model.DRGElement) List(java.util.List) DecisionService(org.kie.workbench.common.dmn.api.definition.model.DecisionService) ComponentWidths(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.ComponentWidths) DMNElementReference(org.kie.workbench.common.dmn.api.definition.model.DMNElementReference) Decision(org.kie.workbench.common.dmn.api.definition.model.Decision) Comparator(java.util.Comparator) Node(org.kie.workbench.common.stunner.core.graph.Node) ArrayList(java.util.ArrayList) DecisionService(org.kie.workbench.common.dmn.api.definition.model.DecisionService) ArrayList(java.util.ArrayList) List(java.util.List) InputData(org.kie.workbench.common.dmn.api.definition.model.InputData) Child(org.kie.workbench.common.stunner.core.graph.content.relationship.Child) 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) DMNElementReference(org.kie.workbench.common.dmn.api.definition.model.DMNElementReference) Edge(org.kie.workbench.common.stunner.core.graph.Edge) DRGElement(org.kie.workbench.common.dmn.api.definition.model.DRGElement)

Aggregations

List (java.util.List)6 DRGElement (org.kie.workbench.common.dmn.api.definition.model.DRGElement)6 DecisionService (org.kie.workbench.common.dmn.api.definition.model.DecisionService)6 KnowledgeRequirement (org.kie.workbench.common.dmn.api.definition.model.KnowledgeRequirement)6 Edge (org.kie.workbench.common.stunner.core.graph.Edge)6 View (org.kie.workbench.common.stunner.core.graph.content.view.View)6 ArrayList (java.util.ArrayList)5 BusinessKnowledgeModel (org.kie.workbench.common.dmn.api.definition.model.BusinessKnowledgeModel)4 Decision (org.kie.workbench.common.dmn.api.definition.model.Decision)4 InputData (org.kie.workbench.common.dmn.api.definition.model.InputData)4 KnowledgeSource (org.kie.workbench.common.dmn.api.definition.model.KnowledgeSource)4 Child (org.kie.workbench.common.stunner.core.graph.content.relationship.Child)4 Id (org.kie.workbench.common.dmn.api.property.dmn.Id)3 JSITDMNElementReference (org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDMNElementReference)3 JSITInformationItem (org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITInformationItem)3 BindableAdapterUtils.getDefinitionId (org.kie.workbench.common.stunner.core.definition.adapter.binding.BindableAdapterUtils.getDefinitionId)3 Comparator (java.util.Comparator)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2 HasComponentWidths (org.kie.workbench.common.dmn.api.definition.HasComponentWidths)2