Search in sources :

Example 11 with FunctionDefinition

use of org.kie.dmn.model.api.FunctionDefinition in project drools by kiegroup.

the class DMNContextEvaluator method evaluate.

@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
    DMNResultImpl result = (DMNResultImpl) dmnr;
    // when this evaluator is executed, it should either return a Map of key/value pairs
    // where keys are the name of the entries and values are the result of the evaluations
    // OR if a default result is implemented, it should return the result instead
    Map<String, Object> results = new HashMap<>();
    DMNContext previousContext = result.getContext();
    DMNContext dmnContext = previousContext.clone();
    result.setContext(dmnContext);
    try {
        Set<String> visitedNames = new LinkedHashSet<>();
        for (ContextEntryDef ed : entries) {
            try {
                String entryVarId = getEntryVarId(ed);
                String entryExprId = getEntryExprId(ed);
                DMNRuntimeEventManagerUtils.fireBeforeEvaluateContextEntry(eventManager, name, ed.getName(), entryVarId, entryExprId, result);
                if (visitedNames.contains(ed.getName())) {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, null, null, Msg.DUPLICATE_CONTEXT_ENTRY, ed.getName());
                    return new EvaluatorResultImpl(results, ResultType.FAILURE);
                }
                EvaluatorResult er = ed.getEvaluator().evaluate(eventManager, result);
                if (er.getResultType() == ResultType.SUCCESS) {
                    Object value = er.getResult();
                    if (!ed.getType().isCollection() && value instanceof Collection && ((Collection) value).size() == 1) {
                        // spec defines that "a=[a]", i.e., singleton collections should be treated as the single element
                        // and vice-versa
                        value = ((Collection) value).toArray()[0];
                    }
                    if (((DMNRuntimeImpl) eventManager.getRuntime()).performRuntimeTypeCheck(result.getModel())) {
                        if (!(ed.getContextEntry().getExpression() instanceof FunctionDefinition)) {
                            // checking directly the result type...
                            if (ed.getType() != null && !ed.getType().isAssignableValue(value)) {
                                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, null, null, Msg.ERROR_EVAL_NODE_RESULT_WRONG_TYPE, ed.getName(), ed.getType(), value);
                                return new EvaluatorResultImpl(results, ResultType.FAILURE);
                            }
                        } else {
                        // TODO ...will need calculation/inference of function return type.
                        }
                    }
                    results.put(ed.getName(), value);
                    dmnContext.set(ed.getName(), value);
                    visitedNames.add(ed.getName());
                    DMNRuntimeEventManagerUtils.fireAfterEvaluateContextEntry(eventManager, name, ed.getName(), entryVarId, entryExprId, value, result);
                } else {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, null, null, Msg.ERR_EVAL_CTX_ENTRY_ON_CTX, ed.getName(), name);
                    DMNRuntimeEventManagerUtils.fireAfterEvaluateContextEntry(eventManager, name, ed.getName(), entryVarId, entryExprId, er.getResult(), result);
                    return new EvaluatorResultImpl(results, ResultType.FAILURE);
                }
            } catch (Exception e) {
                logger.error("Error invoking expression for node '" + name + "'.", e);
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, e, null, Msg.ERR_EVAL_CTX_ENTRY_ON_CTX_MSG, ed.getName(), name, e.getLocalizedMessage());
                String entryVarId = getEntryVarId(ed);
                String entryExprId = getEntryExprId(ed);
                DMNRuntimeEventManagerUtils.fireAfterEvaluateContextEntry(eventManager, name, ed.getName(), entryVarId, entryExprId, null, result);
                return new EvaluatorResultImpl(results, ResultType.FAILURE);
            }
        }
    } finally {
        result.setContext(previousContext);
    }
    if (results.containsKey(RESULT_ENTRY)) {
        return new EvaluatorResultImpl(results.get(RESULT_ENTRY), ResultType.SUCCESS);
    } else {
        return new EvaluatorResultImpl(results, ResultType.SUCCESS);
    }
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) LinkedHashSet(java.util.LinkedHashSet) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) HashMap(java.util.HashMap) DMNRuntimeImpl(org.kie.dmn.core.impl.DMNRuntimeImpl) DMNContext(org.kie.dmn.api.core.DMNContext) Collection(java.util.Collection) FunctionDefinition(org.kie.dmn.model.api.FunctionDefinition)

Example 12 with FunctionDefinition

use of org.kie.dmn.model.api.FunctionDefinition in project drools by kiegroup.

the class FunctionDefinitionConverter method writeAttributes.

@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
    super.writeAttributes(writer, parent);
    FunctionDefinition fd = (FunctionDefinition) parent;
    writer.addAttribute(KIND, fd.getKind().value());
}
Also used : TFunctionDefinition(org.kie.dmn.model.v1_4.TFunctionDefinition) FunctionDefinition(org.kie.dmn.model.api.FunctionDefinition)

Example 13 with FunctionDefinition

use of org.kie.dmn.model.api.FunctionDefinition in project drools by kiegroup.

the class FunctionDefinitionConverter method assignAttributes.

@Override
protected void assignAttributes(HierarchicalStreamReader reader, Object parent) {
    super.assignAttributes(reader, parent);
    FunctionDefinition i = (FunctionDefinition) parent;
    String kind = reader.getAttribute(KIND);
    if (kind != null) {
        i.setKind(FunctionKind.fromValue(kind));
    }
}
Also used : TFunctionDefinition(org.kie.dmn.model.v1_4.TFunctionDefinition) FunctionDefinition(org.kie.dmn.model.api.FunctionDefinition)

Example 14 with FunctionDefinition

use of org.kie.dmn.model.api.FunctionDefinition in project drools by kiegroup.

the class FunctionDefinitionConverter method writeAttributes.

@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
    super.writeAttributes(writer, parent);
    FunctionDefinition fd = (FunctionDefinition) parent;
    writer.addAttribute(KIND, fd.getKind().value());
}
Also used : TFunctionDefinition(org.kie.dmn.model.v1_3.TFunctionDefinition) FunctionDefinition(org.kie.dmn.model.api.FunctionDefinition)

Example 15 with FunctionDefinition

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

the class ContextPropertyConverter method wbFromDMN.

public static Context wbFromDMN(final org.kie.dmn.model.api.Context dmn, final BiConsumer<String, HasComponentWidths> hasComponentWidthsConsumer) {
    final Id id = new Id(dmn.getId());
    final Description description = DescriptionPropertyConverter.wbFromDMN(dmn.getDescription());
    final QName typeRef = QNamePropertyConverter.wbFromDMN(dmn.getTypeRef(), dmn);
    final Context result = new Context(id, description, typeRef);
    for (org.kie.dmn.model.api.ContextEntry ce : dmn.getContextEntry()) {
        final ContextEntry ceConverted = ContextEntryPropertyConverter.wbFromDMN(ce, hasComponentWidthsConsumer);
        if (ceConverted != null) {
            ceConverted.setParent(result);
        }
        result.getContextEntry().add(ceConverted);
    }
    // No need to append a _default_ row if the Context is part of a JAVA or PMML FunctionDefinition
    if (dmn.getParent() instanceof FunctionDefinition) {
        final FunctionDefinition functionDefinition = (FunctionDefinition) dmn.getParent();
        if (!functionDefinition.getKind().equals(FunctionKind.FEEL)) {
            return result;
        }
    }
    // The UI requires a ContextEntry for the _default_ result even if none has been defined
    final int contextEntriesCount = result.getContextEntry().size();
    if (contextEntriesCount == 0) {
        result.getContextEntry().add(new ContextEntry());
    } else if (!Objects.isNull(result.getContextEntry().get(contextEntriesCount - 1).getVariable())) {
        result.getContextEntry().add(new ContextEntry());
    }
    return result;
}
Also used : Context(org.kie.workbench.common.dmn.api.definition.model.Context) Description(org.kie.workbench.common.dmn.api.property.dmn.Description) QName(org.kie.workbench.common.dmn.api.property.dmn.QName) FunctionDefinition(org.kie.dmn.model.api.FunctionDefinition) Id(org.kie.workbench.common.dmn.api.property.dmn.Id) ContextEntry(org.kie.workbench.common.dmn.api.definition.model.ContextEntry)

Aggregations

FunctionDefinition (org.kie.dmn.model.api.FunctionDefinition)15 InformationItem (org.kie.dmn.model.api.InformationItem)6 DMNExpressionEvaluator (org.kie.dmn.core.api.DMNExpressionEvaluator)3 EvaluatorResult (org.kie.dmn.core.api.EvaluatorResult)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 UUID (java.util.UUID)2 Collectors (java.util.stream.Collectors)2 Collectors.toList (java.util.stream.Collectors.toList)2 QName (javax.xml.namespace.QName)2 Resource (org.kie.api.io.Resource)2 DMNMessage (org.kie.dmn.api.core.DMNMessage)2 DMNType (org.kie.dmn.api.core.DMNType)2 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)2 DMNNode (org.kie.dmn.api.core.ast.DMNNode)2 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)2