Search in sources :

Example 26 with FunctionDefinition

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

the class BusinessKnowledgeModelConverter method dmnFromNode.

@Override
@SuppressWarnings("unchecked")
public JSITBusinessKnowledgeModel dmnFromNode(final Node<View<BusinessKnowledgeModel>, ?> node, final Consumer<JSITComponentWidths> componentWidthsConsumer) {
    final BusinessKnowledgeModel source = (BusinessKnowledgeModel) DefinitionUtils.getElementDefinition(node);
    final JSITBusinessKnowledgeModel result = new JSITBusinessKnowledgeModel();
    result.setId(source.getId().getValue());
    final Optional<String> description = Optional.ofNullable(DescriptionPropertyConverter.dmnFromWB(source.getDescription()));
    description.ifPresent(result::setDescription);
    result.setName(source.getName().getValue());
    // Add because it is present in the original JSON when unmarshalling
    if (Objects.isNull(result.getKnowledgeRequirement())) {
        result.setKnowledgeRequirement(new ArrayList<>());
    }
    // Add because it is present in the original JSON when unmarshalling
    if (Objects.isNull(result.getAuthorityRequirement())) {
        result.setAuthorityRequirement(new ArrayList<>());
    }
    DMNExternalLinksToExtensionElements.loadExternalLinksIntoExtensionElements(source, result);
    final JSITInformationItem variable = InformationItemPrimaryPropertyConverter.dmnFromWB(source.getVariable(), source);
    result.setVariable(variable);
    final JSITFunctionDefinition 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 JSITComponentWidths componentWidths = new JSITComponentWidths();
            componentWidths.setDmnElementRef(uuid);
            source.getEncapsulatedLogic().getComponentWidths().stream().filter(Objects::nonNull).forEach(w -> componentWidths.addWidth(new Float(w)));
            componentWidthsConsumer.accept(componentWidths);
        }
    }
    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 JSITKnowledgeRequirement iReq = new JSITKnowledgeRequirement();
                    iReq.setId(getRawId(e.getUUID()));
                    final JSITDMNElementReference ri = new JSITDMNElementReference();
                    ri.setHref(getHref(drgElement));
                    iReq.setRequiredKnowledge(ri);
                    result.addKnowledgeRequirement(iReq);
                } else if (drgElement instanceof KnowledgeSource) {
                    final JSITAuthorityRequirement iReq = new JSITAuthorityRequirement();
                    iReq.setId(getRawId(e.getUUID()));
                    final JSITDMNElementReference ri = new JSITDMNElementReference();
                    ri.setHref(getHref(drgElement));
                    iReq.setRequiredAuthority(ri);
                    result.addAuthorityRequirement(iReq);
                } else if (drgElement instanceof DecisionService) {
                    if (e.getContent() instanceof View && ((View) e.getContent()).getDefinition() instanceof KnowledgeRequirement) {
                        final JSITKnowledgeRequirement iReq = new JSITKnowledgeRequirement();
                        iReq.setId(getRawId(e.getUUID()));
                        final JSITDMNElementReference ri = new JSITDMNElementReference();
                        ri.setHref(getHref(drgElement));
                        iReq.setRequiredKnowledge(ri);
                        result.addKnowledgeRequirement(iReq);
                    } else {
                        throw new UnsupportedOperationException("wrong model definition.");
                    }
                } else {
                    throw new UnsupportedOperationException("wrong model definition.");
                }
            }
        }
    }
    return result;
}
Also used : JSITInformationItem(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITInformationItem) DecisionService(org.kie.workbench.common.dmn.api.definition.model.DecisionService) JSITBusinessKnowledgeModel(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITBusinessKnowledgeModel) KnowledgeSource(org.kie.workbench.common.dmn.api.definition.model.KnowledgeSource) JSITAuthorityRequirement(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITAuthorityRequirement) FunctionDefinition(org.kie.workbench.common.dmn.api.definition.model.FunctionDefinition) JSITFunctionDefinition(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITFunctionDefinition) ArrayList(java.util.ArrayList) List(java.util.List) JSITComponentWidths(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentWidths) JSITFunctionDefinition(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITFunctionDefinition) JSITDMNElementReference(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDMNElementReference) View(org.kie.workbench.common.stunner.core.graph.content.view.View) KnowledgeRequirement(org.kie.workbench.common.dmn.api.definition.model.KnowledgeRequirement) JSITKnowledgeRequirement(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITKnowledgeRequirement) JSITKnowledgeRequirement(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITKnowledgeRequirement) BusinessKnowledgeModel(org.kie.workbench.common.dmn.api.definition.model.BusinessKnowledgeModel) JSITBusinessKnowledgeModel(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITBusinessKnowledgeModel) Edge(org.kie.workbench.common.stunner.core.graph.Edge) DRGElement(org.kie.workbench.common.dmn.api.definition.model.DRGElement)

Example 27 with FunctionDefinition

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

the class BusinessKnowledgeModelConverter method nodeFromDMN.

@Override
public Node<View<BusinessKnowledgeModel>, ?> nodeFromDMN(final NodeEntry nodeEntry) {
    final JSITBusinessKnowledgeModel dmn = Js.uncheckedCast(nodeEntry.getDmnElement());
    @SuppressWarnings("unchecked") final Node<View<BusinessKnowledgeModel>, ?> node = (Node<View<BusinessKnowledgeModel>, ?>) factoryManager.newElement(nodeEntry.getId(), getDefinitionId(BusinessKnowledgeModel.class)).asNode();
    final Id id = IdPropertyConverter.wbFromDMN(dmn.getId());
    final Description description = DescriptionPropertyConverter.wbFromDMN(dmn.getDescription());
    final Name name = new Name(dmn.getName());
    final InformationItemPrimary informationItem = InformationItemPrimaryPropertyConverter.wbFromDMN(dmn.getVariable(), dmn);
    final JSITFunctionDefinition dmnFunctionDefinition = dmn.getEncapsulatedLogic();
    final FunctionDefinition functionDefinition = FunctionDefinitionPropertyConverter.wbFromDMN(dmnFunctionDefinition, nodeEntry.getComponentWidthsConsumer());
    final BusinessKnowledgeModel bkm = new BusinessKnowledgeModel(id, description, name, informationItem, functionDefinition, new StylingSet(), new GeneralRectangleDimensionsSet());
    bkm.setDiagramId(nodeEntry.getDiagramId());
    node.getContent().setDefinition(bkm);
    if (Objects.nonNull(informationItem)) {
        informationItem.setParent(bkm);
    }
    if (Objects.nonNull(functionDefinition)) {
        functionDefinition.setParent(bkm);
    }
    if (Objects.nonNull(dmnFunctionDefinition)) {
        nodeEntry.getComponentWidthsConsumer().accept(dmnFunctionDefinition.getId(), functionDefinition);
    }
    DMNExternalLinksToExtensionElements.loadExternalLinksFromExtensionElements(dmn, bkm);
    return node;
}
Also used : InformationItemPrimary(org.kie.workbench.common.dmn.api.definition.model.InformationItemPrimary) JSITFunctionDefinition(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITFunctionDefinition) Description(org.kie.workbench.common.dmn.api.property.dmn.Description) Node(org.kie.workbench.common.stunner.core.graph.Node) GeneralRectangleDimensionsSet(org.kie.workbench.common.dmn.api.property.dimensions.GeneralRectangleDimensionsSet) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Name(org.kie.workbench.common.dmn.api.property.dmn.Name) JSITBusinessKnowledgeModel(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITBusinessKnowledgeModel) BusinessKnowledgeModel(org.kie.workbench.common.dmn.api.definition.model.BusinessKnowledgeModel) JSITBusinessKnowledgeModel(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITBusinessKnowledgeModel) StylingSet(org.kie.workbench.common.dmn.api.property.styling.StylingSet) FunctionDefinition(org.kie.workbench.common.dmn.api.definition.model.FunctionDefinition) JSITFunctionDefinition(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITFunctionDefinition) Id(org.kie.workbench.common.dmn.api.property.dmn.Id) IdUtils.getRawId(org.kie.workbench.common.dmn.client.marshaller.common.IdUtils.getRawId) BindableAdapterUtils.getDefinitionId(org.kie.workbench.common.stunner.core.definition.adapter.binding.BindableAdapterUtils.getDefinitionId)

Example 28 with FunctionDefinition

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

the class RegisterNodeCommandTest method testExecuteWithBusinessKnowledgeModelNode.

@Test
public void testExecuteWithBusinessKnowledgeModelNode() {
    when(candidateContent.getDefinition()).thenReturn(businessKnowledgeModel);
    assertThat(command.execute(graphCommandExecutionContext).getType()).isEqualTo(CommandResult.Type.INFO);
    final FunctionDefinition encapsulatedLogic = businessKnowledgeModel.getEncapsulatedLogic();
    final Expression expression = encapsulatedLogic.getExpression();
    assertThat(expression).isInstanceOf(LiteralExpression.class);
    assertThat(expression.getParent()).isEqualTo(encapsulatedLogic);
    assertThat(KindUtilities.getKind(encapsulatedLogic)).isEqualTo(FunctionDefinition.Kind.FEEL);
}
Also used : LiteralExpression(org.kie.workbench.common.dmn.api.definition.model.LiteralExpression) Expression(org.kie.workbench.common.dmn.api.definition.model.Expression) FunctionDefinition(org.kie.workbench.common.dmn.api.definition.model.FunctionDefinition) Test(org.junit.Test) AbstractGraphCommandTest(org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommandTest)

Example 29 with FunctionDefinition

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

the class RegisterNodeCommand method setupEncapsulatedLogic.

private void setupEncapsulatedLogic(final BusinessKnowledgeModel businessKnowledgeModel) {
    final LiteralExpression le = new LiteralExpression();
    final FunctionDefinition function = new FunctionDefinition();
    KindUtilities.setKind(function, FunctionDefinition.Kind.FEEL);
    function.setExpression(le);
    le.setParent(function);
    businessKnowledgeModel.setEncapsulatedLogic(function);
    function.setParent(businessKnowledgeModel);
}
Also used : LiteralExpression(org.kie.workbench.common.dmn.api.definition.model.LiteralExpression) FunctionDefinition(org.kie.workbench.common.dmn.api.definition.model.FunctionDefinition)

Example 30 with FunctionDefinition

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

the class FunctionDefinitionPropertyConverter method wbFromDMN.

public static FunctionDefinition wbFromDMN(final JSITFunctionDefinition dmn, final BiConsumer<String, HasComponentWidths> hasComponentWidthsConsumer) {
    if (Objects.isNull(dmn)) {
        return null;
    }
    final Id id = IdPropertyConverter.wbFromDMN(dmn.getId());
    final Description description = DescriptionPropertyConverter.wbFromDMN(dmn.getDescription());
    final QName typeRef = QNamePropertyConverter.wbFromDMN(dmn.getTypeRef());
    Expression expression = null;
    final JSITExpression jsiWrapped = dmn.getExpression();
    if (Objects.nonNull(jsiWrapped)) {
        final JSITExpression jsiExpression = Js.uncheckedCast(JsUtils.getUnwrappedElement(jsiWrapped));
        expression = ExpressionPropertyConverter.wbFromDMN(jsiExpression, Js.uncheckedCast(dmn), hasComponentWidthsConsumer);
    }
    final FunctionDefinition result = new FunctionDefinition(id, description, typeRef, expression);
    if (Objects.nonNull(expression)) {
        expression.setParent(result);
    }
    // JSITFunctionKind is a String JSO so convert into the real type
    final String sKind = Js.uncheckedCast(dmn.getKind());
    final Kind kind = Kind.fromValue(sKind);
    switch(kind) {
        case FEEL:
            result.setKind(Kind.FEEL);
            break;
        case JAVA:
            result.setKind(Kind.JAVA);
            break;
        case PMML:
            result.setKind(Kind.PMML);
            convertPMMLFunctionExpression(result, hasComponentWidthsConsumer);
            break;
        default:
            result.setKind(Kind.FEEL);
            break;
    }
    final List<JSITInformationItem> jsiInformationItems = dmn.getFormalParameter();
    for (int i = 0; i < jsiInformationItems.size(); i++) {
        final JSITInformationItem jsiInformationItem = Js.uncheckedCast(jsiInformationItems.get(i));
        final InformationItem iiConverted = InformationItemPropertyConverter.wbFromDMN(jsiInformationItem);
        if (Objects.nonNull(iiConverted)) {
            iiConverted.setParent(result);
        }
        result.getFormalParameter().add(iiConverted);
    }
    return result;
}
Also used : Description(org.kie.workbench.common.dmn.api.property.dmn.Description) JSITInformationItem(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITInformationItem) LiteralExpression(org.kie.workbench.common.dmn.api.definition.model.LiteralExpression) JSITExpression(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITExpression) Expression(org.kie.workbench.common.dmn.api.definition.model.Expression) QName(org.kie.workbench.common.dmn.api.property.dmn.QName) JSITFunctionKind(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITFunctionKind) Kind(org.kie.workbench.common.dmn.api.definition.model.FunctionDefinition.Kind) FunctionDefinition(org.kie.workbench.common.dmn.api.definition.model.FunctionDefinition) JSITFunctionDefinition(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITFunctionDefinition) JSITInformationItem(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITInformationItem) InformationItem(org.kie.workbench.common.dmn.api.definition.model.InformationItem) Id(org.kie.workbench.common.dmn.api.property.dmn.Id) JSITExpression(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITExpression)

Aggregations

FunctionDefinition (org.kie.workbench.common.dmn.api.definition.model.FunctionDefinition)40 Test (org.junit.Test)12 BusinessKnowledgeModel (org.kie.workbench.common.dmn.api.definition.model.BusinessKnowledgeModel)12 LiteralExpression (org.kie.workbench.common.dmn.api.definition.model.LiteralExpression)12 Context (org.kie.workbench.common.dmn.api.definition.model.Context)11 ArrayList (java.util.ArrayList)8 InformationItem (org.kie.workbench.common.dmn.api.definition.model.InformationItem)8 Id (org.kie.workbench.common.dmn.api.property.dmn.Id)8 Before (org.junit.Before)7 Expression (org.kie.workbench.common.dmn.api.definition.model.Expression)7 View (org.kie.workbench.common.stunner.core.graph.content.view.View)7 TFunctionDefinition (org.kie.dmn.model.v1_2.TFunctionDefinition)5 HasComponentWidths (org.kie.workbench.common.dmn.api.definition.HasComponentWidths)5 ContextEntry (org.kie.workbench.common.dmn.api.definition.model.ContextEntry)5 DRGElement (org.kie.workbench.common.dmn.api.definition.model.DRGElement)5 IsLiteralExpression (org.kie.workbench.common.dmn.api.definition.model.IsLiteralExpression)5 Name (org.kie.workbench.common.dmn.api.property.dmn.Name)5 List (java.util.List)4 QName (javax.xml.namespace.QName)4 TLiteralExpression (org.kie.dmn.model.v1_2.TLiteralExpression)4