Search in sources :

Example 66 with Expression

use of org.kie.workbench.common.dmn.api.definition.v1_1.Expression in project drools by kiegroup.

the class DMNEvaluatorCompiler method compileFunctionDefinition.

private DMNExpressionEvaluator compileFunctionDefinition(DMNCompilerContext ctx, DMNModelImpl model, DMNBaseNode node, String functionName, FunctionDefinition expression) {
    FunctionDefinition funcDef = expression;
    String kindStr = funcDef.getAdditionalAttributes().get(FunctionDefinition.KIND_QNAME);
    FunctionDefinition.Kind kind = kindStr != null ? FunctionDefinition.Kind.determineFromString(kindStr) : FunctionDefinition.Kind.FEEL;
    if (kind == null) {
        // unknown function kind
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_INVALID_KIND, kindStr, node.getIdentifierString());
        return new DMNFunctionDefinitionEvaluator(node.getName(), funcDef);
    } else if (kind.equals(FunctionDefinition.Kind.FEEL)) {
        ctx.enterFrame();
        try {
            DMNFunctionDefinitionEvaluator func = new DMNFunctionDefinitionEvaluator(node.getName(), funcDef);
            for (InformationItem p : funcDef.getFormalParameter()) {
                DMNCompilerHelper.checkVariableName(model, p, p.getName());
                DMNType dmnType = compiler.resolveTypeRef(model, node, p, p, p.getTypeRef());
                func.addParameter(p.getName(), dmnType);
                ctx.setVariable(p.getName(), dmnType);
            }
            DMNExpressionEvaluator eval = compileExpression(ctx, model, node, functionName, funcDef.getExpression());
            if (eval instanceof DMNLiteralExpressionEvaluator && ((DMNLiteralExpressionEvaluator) eval).isFunctionDefinition()) {
                // we need to resolve the function and eliminate the indirection
                CompiledExpression fexpr = ((DMNLiteralExpressionEvaluator) eval).getExpression();
                FEELFunction feelFunction = feel.evaluateFunctionDef(ctx, fexpr, model, funcDef, Msg.FUNC_DEF_COMPILATION_ERR, functionName, node.getIdentifierString());
                DMNInvocationEvaluator invoker = new DMNInvocationEvaluator(node.getName(), node.getSource(), functionName, new Invocation(), (fctx, fname) -> feelFunction, // feel can be null as anyway is hardcoded to `feelFunction`
                null);
                for (InformationItem p : funcDef.getFormalParameter()) {
                    invoker.addParameter(p.getName(), func.getParameterType(p.getName()), (em, dr) -> new EvaluatorResultImpl(dr.getContext().get(p.getName()), EvaluatorResult.ResultType.SUCCESS));
                }
                eval = invoker;
            }
            func.setEvaluator(eval);
            return func;
        } finally {
            ctx.exitFrame();
        }
    } else if (kind.equals(FunctionDefinition.Kind.JAVA)) {
        if (funcDef.getExpression() instanceof Context) {
            // proceed
            Context context = (Context) funcDef.getExpression();
            String clazz = null;
            String method = null;
            for (ContextEntry ce : context.getContextEntry()) {
                if (ce.getVariable() != null && ce.getVariable().getName() != null && ce.getExpression() != null && ce.getExpression() instanceof LiteralExpression) {
                    if (ce.getVariable().getName().equals("class")) {
                        clazz = stripQuotes(((LiteralExpression) ce.getExpression()).getText().trim());
                    } else if (ce.getVariable().getName().equals("method signature")) {
                        method = stripQuotes(((LiteralExpression) ce.getExpression()).getText().trim());
                    }
                }
            }
            if (clazz != null && method != null) {
                String params = funcDef.getFormalParameter().stream().map(p -> p.getName()).collect(Collectors.joining(","));
                String expr = String.format("function(%s) external { java: { class: \"%s\", method signature: \"%s\" }}", params, clazz, method);
                try {
                    FEELFunction feelFunction = feel.evaluateFunctionDef(ctx, expr, model, funcDef, Msg.FUNC_DEF_COMPILATION_ERR, functionName, node.getIdentifierString());
                    if (feelFunction != null) {
                        ((BaseFEELFunction) feelFunction).setName(functionName);
                    }
                    DMNInvocationEvaluator invoker = new DMNInvocationEvaluator(node.getName(), node.getSource(), functionName, new Invocation(), (fctx, fname) -> feelFunction, // feel can be null as anyway is hardcoded to `feelFunction`
                    null);
                    DMNFunctionDefinitionEvaluator func = new DMNFunctionDefinitionEvaluator(node.getName(), funcDef);
                    for (InformationItem p : funcDef.getFormalParameter()) {
                        DMNCompilerHelper.checkVariableName(model, p, p.getName());
                        DMNType dmnType = compiler.resolveTypeRef(model, node, p, p, p.getTypeRef());
                        func.addParameter(p.getName(), dmnType);
                        invoker.addParameter(p.getName(), dmnType, (em, dr) -> new EvaluatorResultImpl(dr.getContext().get(p.getName()), EvaluatorResult.ResultType.SUCCESS));
                    }
                    func.setEvaluator(invoker);
                    return func;
                } catch (Throwable e) {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, expression, model, e, null, Msg.FUNC_DEF_COMPILATION_ERR, functionName, node.getIdentifierString(), "Exception raised: " + e.getClass().getSimpleName());
                }
            } else {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, expression, model, null, null, Msg.FUNC_DEF_MISSING_ENTRY, functionName, node.getIdentifierString());
            }
        } else {
            // error, java function definitions require a context
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_BODY_NOT_CONTEXT, node.getIdentifierString());
        }
    } else if (kind.equals(FunctionDefinition.Kind.PMML)) {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, funcDef, model, null, null, Msg.FUNC_DEF_PMML_NOT_SUPPORTED, node.getIdentifierString());
    } else {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_INVALID_KIND, kindStr, node.getIdentifierString());
    }
    return new DMNFunctionDefinitionEvaluator(node.getName(), funcDef);
}
Also used : InformationItem(org.kie.dmn.model.v1_1.InformationItem) DMNMessage(org.kie.dmn.api.core.DMNMessage) ContextEntry(org.kie.dmn.model.v1_1.ContextEntry) OutputClause(org.kie.dmn.model.v1_1.OutputClause) FunctionDefinition(org.kie.dmn.model.v1_1.FunctionDefinition) LoggerFactory(org.slf4j.LoggerFactory) LiteralExpression(org.kie.dmn.model.v1_1.LiteralExpression) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) DTDecisionRule(org.kie.dmn.feel.runtime.decisiontables.DTDecisionRule) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) Binding(org.kie.dmn.model.v1_1.Binding) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) DMNModelInstrumentedBase(org.kie.dmn.model.v1_1.DMNModelInstrumentedBase) BaseDMNTypeImpl(org.kie.dmn.core.impl.BaseDMNTypeImpl) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) DecisionTable(org.kie.dmn.model.v1_1.DecisionTable) DMNRelationEvaluator(org.kie.dmn.core.ast.DMNRelationEvaluator) Collectors(java.util.stream.Collectors) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) Context(org.kie.dmn.model.v1_1.Context) List(java.util.List) DMNDTExpressionEvaluator(org.kie.dmn.core.ast.DMNDTExpressionEvaluator) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Invocation(org.kie.dmn.model.v1_1.Invocation) Optional(java.util.Optional) QName(javax.xml.namespace.QName) DMNLiteralExpressionEvaluator(org.kie.dmn.core.ast.DMNLiteralExpressionEvaluator) BusinessKnowledgeModel(org.kie.dmn.model.v1_1.BusinessKnowledgeModel) DMNElement(org.kie.dmn.model.v1_1.DMNElement) FEEL(org.kie.dmn.feel.FEEL) MsgUtil(org.kie.dmn.core.util.MsgUtil) DMNType(org.kie.dmn.api.core.DMNType) DMNContextEvaluator(org.kie.dmn.core.ast.DMNContextEvaluator) DTOutputClause(org.kie.dmn.feel.runtime.decisiontables.DTOutputClause) EvaluatorResultImpl(org.kie.dmn.core.ast.EvaluatorResultImpl) DTInputClause(org.kie.dmn.feel.runtime.decisiontables.DTInputClause) ArrayList(java.util.ArrayList) HitPolicy(org.kie.dmn.model.v1_1.HitPolicy) Relation(org.kie.dmn.model.v1_1.Relation) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) DMNBaseNode(org.kie.dmn.core.ast.DMNBaseNode) DecisionRule(org.kie.dmn.model.v1_1.DecisionRule) UnaryTests(org.kie.dmn.model.v1_1.UnaryTests) Expression(org.kie.dmn.model.v1_1.Expression) DMNInvocationEvaluator(org.kie.dmn.core.ast.DMNInvocationEvaluator) Logger(org.slf4j.Logger) DMNListEvaluator(org.kie.dmn.core.ast.DMNListEvaluator) DTInvokerFunction(org.kie.dmn.feel.runtime.functions.DTInvokerFunction) Decision(org.kie.dmn.model.v1_1.Decision) Collectors.toList(java.util.stream.Collectors.toList) DMNFunctionDefinitionEvaluator(org.kie.dmn.core.ast.DMNFunctionDefinitionEvaluator) InputClause(org.kie.dmn.model.v1_1.InputClause) Msg(org.kie.dmn.core.util.Msg) DecisionTableImpl(org.kie.dmn.feel.runtime.decisiontables.DecisionTableImpl) Collections(java.util.Collections) BaseFEELFunction(org.kie.dmn.feel.runtime.functions.BaseFEELFunction) Context(org.kie.dmn.model.v1_1.Context) BaseFEELFunction(org.kie.dmn.feel.runtime.functions.BaseFEELFunction) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) BaseFEELFunction(org.kie.dmn.feel.runtime.functions.BaseFEELFunction) Invocation(org.kie.dmn.model.v1_1.Invocation) DMNLiteralExpressionEvaluator(org.kie.dmn.core.ast.DMNLiteralExpressionEvaluator) LiteralExpression(org.kie.dmn.model.v1_1.LiteralExpression) InformationItem(org.kie.dmn.model.v1_1.InformationItem) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) ContextEntry(org.kie.dmn.model.v1_1.ContextEntry) DMNFunctionDefinitionEvaluator(org.kie.dmn.core.ast.DMNFunctionDefinitionEvaluator) FunctionDefinition(org.kie.dmn.model.v1_1.FunctionDefinition) EvaluatorResultImpl(org.kie.dmn.core.ast.EvaluatorResultImpl) DMNInvocationEvaluator(org.kie.dmn.core.ast.DMNInvocationEvaluator) DMNType(org.kie.dmn.api.core.DMNType)

Example 67 with Expression

use of org.kie.workbench.common.dmn.api.definition.v1_1.Expression in project drools by kiegroup.

the class DMNEvaluatorCompiler method compileRelation.

private DMNExpressionEvaluator compileRelation(DMNCompilerContext ctx, DMNModelImpl model, DMNBaseNode node, String relationName, Relation expression) {
    Relation relationDef = expression;
    DMNRelationEvaluator relationEval = new DMNRelationEvaluator(node.getName(), node.getSource(), relationDef);
    for (InformationItem col : relationDef.getColumn()) {
        DMNCompilerHelper.checkVariableName(model, col, col.getName());
        relationEval.addColumn(col.getName());
    }
    for (org.kie.dmn.model.v1_1.List row : relationDef.getRow()) {
        java.util.List<DMNExpressionEvaluator> values = new ArrayList<>();
        for (Expression expr : row.getExpression()) {
            values.add(compileExpression(ctx, model, node, relationName, expr));
        }
        relationEval.addRow(values);
    }
    return relationEval;
}
Also used : Relation(org.kie.dmn.model.v1_1.Relation) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) LiteralExpression(org.kie.dmn.model.v1_1.LiteralExpression) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Expression(org.kie.dmn.model.v1_1.Expression) DMNRelationEvaluator(org.kie.dmn.core.ast.DMNRelationEvaluator) ArrayList(java.util.ArrayList) InformationItem(org.kie.dmn.model.v1_1.InformationItem)

Example 68 with Expression

use of org.kie.workbench.common.dmn.api.definition.v1_1.Expression in project drools by kiegroup.

the class ExpressionConverter method writeAttributes.

@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
    super.writeAttributes(writer, parent);
    Expression e = (Expression) parent;
    if (e.getTypeRef() != null)
        writer.addAttribute(TYPE_REF, MarshallingUtils.formatQName(e.getTypeRef()));
}
Also used : Expression(org.kie.dmn.model.v1_1.Expression)

Example 69 with Expression

use of org.kie.workbench.common.dmn.api.definition.v1_1.Expression in project oozie by apache.

the class CoordInputLogicEvaluatorUtil method validateInputLogic.

/**
 * Validate input logic.
 *
 * @throws JDOMException the JDOM exception
 * @throws CommandException in case of error
 */
public void validateInputLogic() throws JDOMException, CommandException {
    JexlEngine jexl = new OozieJexlEngine();
    String expression = CoordUtils.getInputLogic(coordAction.getActionXml().toString());
    if (StringUtils.isEmpty(expression)) {
        return;
    }
    Expression e = jexl.createExpression(expression);
    JexlContext jc = new OozieJexlParser(jexl, new CoordInputLogicBuilder(new CoordInputLogicEvaluatorPhaseValidate(coordAction)));
    try {
        Object result = e.evaluate(jc);
        log.debug("Input logic expression is [{0}] and evaluate result is [{1}]", expression, result);
    } catch (RuntimeException re) {
        throw new CommandException(ErrorCode.E1028, re.getCause().getMessage());
    }
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) CommandException(org.apache.oozie.command.CommandException)

Aggregations

Expression (org.kie.workbench.common.dmn.api.definition.v1_1.Expression)19 BaseExpressionGrid (org.kie.workbench.common.dmn.client.widgets.grid.BaseExpressionGrid)15 Expression (org.apache.commons.jexl2.Expression)14 GridCellTuple (org.kie.workbench.common.dmn.client.widgets.grid.model.GridCellTuple)13 JexlContext (org.apache.commons.jexl2.JexlContext)12 Test (org.junit.Test)12 HasExpression (org.kie.workbench.common.dmn.api.definition.HasExpression)12 LiteralExpression (org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression)11 HasName (org.kie.workbench.common.dmn.api.definition.HasName)10 JexlEngine (org.apache.commons.jexl2.JexlEngine)9 InformationItem (org.kie.workbench.common.dmn.api.definition.v1_1.InformationItem)9 BaseGridCellValue (org.uberfire.ext.wires.core.grids.client.model.impl.BaseGridCellValue)9 GridWidget (org.uberfire.ext.wires.core.grids.client.widget.grid.GridWidget)9 MapContext (org.apache.commons.jexl2.MapContext)8 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)7 Expression (org.eclipse.xtext.resource.bug385636.Expression)7 Optional (java.util.Optional)6 Before (org.junit.Before)6 Decision (org.kie.workbench.common.dmn.api.definition.v1_1.Decision)6 Name (org.kie.workbench.common.dmn.api.property.dmn.Name)6