Search in sources :

Example 11 with CompilerContext

use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.

the class BaseFEELCompilerTest method assertResult.

protected void assertResult(final String expression, final Map<String, Type> inputTypes, final Map<String, Object> inputValues, final Object result) {
    final CompilerContext ctx = feel.newCompilerContext();
    inputTypes.forEach(ctx::addInputVariableType);
    final CompiledExpression compiledExpression = feel.compile(expression, ctx);
    if (result == null) {
        assertThat("Evaluating: '" + expression + "'", feel.evaluate(compiledExpression, inputValues), is(nullValue()));
    } else if (result instanceof Class<?>) {
        assertThat("Evaluating: '" + expression + "'", feel.evaluate(compiledExpression, inputValues), is(instanceOf((Class<?>) result)));
    } else {
        assertThat("Evaluating: '" + expression + "'", feel.evaluate(compiledExpression, inputValues), is(result));
    }
}
Also used : CompilerContext(org.kie.dmn.feel.lang.CompilerContext) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression)

Example 12 with CompilerContext

use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.

the class DMNFEELHelper method compileFeelExpression.

public CompiledExpression compileFeelExpression(DMNCompilerContext ctx, String expression, DMNModelImpl model, DMNElement element, Msg.Message errorMsg, Object... msgParams) {
    CompilerContext feelctx = feel.newCompilerContext();
    for (Map.Entry<String, DMNType> entry : ctx.getVariables().entrySet()) {
        feelctx.addInputVariableType(entry.getKey(), ((BaseDMNTypeImpl) entry.getValue()).getFeelType());
    }
    feelctx.setFEELTypeRegistry(model.getTypeRegistry());
    CompiledExpression ce = feel.compile(expression, feelctx);
    processEvents(model, element, errorMsg, msgParams);
    return ce;
}
Also used : CompilerContext(org.kie.dmn.feel.lang.CompilerContext) HashMap(java.util.HashMap) Map(java.util.Map) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) DMNType(org.kie.dmn.api.core.DMNType)

Example 13 with CompilerContext

use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.

the class FEELImpl method evaluateUnaryTests.

@Override
public List<UnaryTest> evaluateUnaryTests(String expression, Map<String, Type> variableTypes) {
    // DMN defines a special case where, unless the expressions are unary tests
    // or ranges, they need to be converted into an equality test unary expression.
    // This way, we have to compile and check the low level AST nodes to properly
    // deal with this case
    CompilerContext ctx = newCompilerContext();
    for (Map.Entry<String, Type> e : variableTypes.entrySet()) {
        ctx.addInputVariableType(e.getKey(), e.getValue());
    }
    CompiledExpressionImpl compiledExpression = (CompiledExpressionImpl) compileExpressionList(expression, ctx);
    if (compiledExpression != null) {
        ListNode listNode = (ListNode) compiledExpression.getExpression();
        List<BaseNode> tests = new ArrayList<>();
        for (BaseNode o : listNode.getElements()) {
            if (o == null) {
                // not much we can do, so just skip it. Error was reported somewhere else
                continue;
            } else if (o instanceof UnaryTestNode || o instanceof DashNode) {
                tests.add(o);
            } else if (o instanceof RangeNode || o instanceof ListNode) {
                tests.add(new UnaryTestNode("in", o));
            } else {
                tests.add(new UnaryTestNode("=", o));
            }
        }
        listNode.setElements(tests);
        compiledExpression.setExpression(listNode);
        // now we can evaluate the expression to build the list of unary tests
        List<UnaryTest> uts = (List<UnaryTest>) evaluate(compiledExpression, FEELImpl.EMPTY_INPUT);
        return uts;
    }
    return Collections.emptyList();
}
Also used : UnaryTestNode(org.kie.dmn.feel.lang.ast.UnaryTestNode) CompilerContext(org.kie.dmn.feel.lang.CompilerContext) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) ArrayList(java.util.ArrayList) ListNode(org.kie.dmn.feel.lang.ast.ListNode) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) Type(org.kie.dmn.feel.lang.Type) ArrayList(java.util.ArrayList) List(java.util.List) DashNode(org.kie.dmn.feel.lang.ast.DashNode) HashMap(java.util.HashMap) Map(java.util.Map) RangeNode(org.kie.dmn.feel.lang.ast.RangeNode)

Example 14 with CompilerContext

use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.

the class DMNCompilerContext method toCompilerContext.

public CompilerContext toCompilerContext() {
    CompilerContext compilerContext = feelHelper.newCompilerContext();
    compilerContext.getListeners().clear();
    for (Map.Entry<String, DMNType> entry : this.getVariables().entrySet()) {
        compilerContext.addInputVariableType(entry.getKey(), ((BaseDMNTypeImpl) entry.getValue()).getFeelType());
    }
    return compilerContext;
}
Also used : CompilerContext(org.kie.dmn.feel.lang.CompilerContext) Map(java.util.Map) HashMap(java.util.HashMap) DMNType(org.kie.dmn.api.core.DMNType)

Example 15 with CompilerContext

use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.

the class DMNAlphaNetworkEvaluatorCompiler method compileDecisionTable.

@Override
protected DMNExpressionEvaluator compileDecisionTable(DMNCompilerContext dmnCompilerContext, DMNModelImpl dmnModelImpl, DMNBaseNode dmnBaseNode, String decisionTableName, DecisionTable decisionTable) {
    DMNFEELHelper feelHelper = dmnCompilerContext.getFeelHelper();
    CompilerContext compilerContext = dmnCompilerContext.toCompilerContext();
    // Parse every cell in the Decision Table
    TableCell.TableCellFactory tableCellFactory = new TableCell.TableCellFactory(feelHelper, compilerContext);
    TableCellParser tableCellParser = new TableCellParser(tableCellFactory);
    DTQNameToTypeResolver resolver = new DTQNameToTypeResolver(compiler, dmnModelImpl, dmnBaseNode.getSource(), decisionTable);
    TableCells tableCells = tableCellParser.parseCells(decisionTable, resolver, decisionTableName);
    GeneratedSources allGeneratedSources = new GeneratedSources();
    DMNAlphaNetworkCompiler dmnAlphaNetworkCompiler = new DMNAlphaNetworkCompiler();
    GeneratedSources generatedSources = dmnAlphaNetworkCompiler.generateSourceCode(decisionTable, tableCells, decisionTableName, allGeneratedSources);
    ReteBuilderContext reteBuilderContext = new ReteBuilderContext();
    ObjectTypeNode firstObjectTypeNodeOfRete = tableCells.createRete(reteBuilderContext);
    // Compile FEEL unary tests to Java source code with row,column index.
    Map<String, String> feelTestClasses = tableCells.createFEELSourceClasses();
    allGeneratedSources.putAllGeneratedFEELTestClasses(feelTestClasses);
    // Generate the ANC
    ObjectTypeNodeCompiler objectTypeNodeCompiler = createAlphaNetworkCompiler(firstObjectTypeNodeOfRete);
    CompiledNetworkSources compiledNetworkSource = objectTypeNodeCompiler.generateSource();
    generatedSources.addNewSourceClasses(compiledNetworkSource.getAllGeneratedSources());
    // Look at target/generated-sources
    generatedSources.dumpGeneratedClasses();
    // Compile everything
    Map<String, Class<?>> compiledClasses = KieMemoryCompiler.compile(generatedSources.getAllGeneratedSources(), this.getClass().getClassLoader());
    Class<?> compiledNetworkClass = compiledClasses.get(compiledNetworkSource.getName());
    CompiledNetwork compiledAlphaNetwork = compiledNetworkSource.createInstanceAndSet(compiledNetworkClass);
    Results results = new Results();
    AlphaNetworkEvaluationContext evaluationContext = new AlphaNetworkEvaluationContext(results);
    DMNAlphaNetworkEvaluator dmnCompiledAlphaNetworkEvaluator = generatedSources.newInstanceOfAlphaNetwork(compiledClasses, compiledAlphaNetwork, evaluationContext);
    // FeelDecisionTable is used at runtime to evaluate Hit Policy / Output values
    // TODO DT-ANC probably need to have all the types in here
    Map<String, Type> variableTypes = new HashMap<>();
    OutputClausesWithType outputClausesWithType = new OutputClausesWithType(dmnModelImpl, decisionTable);
    List<OutputClausesWithType.OutputClauseWithType> outputs = outputClausesWithType.inferTypeForOutputClauses(decisionTable.getOutput());
    FeelDecisionTable feelDecisionTable = new FeelDecisionTable(decisionTableName, outputs, feelHelper, variableTypes, dmnModelImpl.getTypeRegistry().unknown());
    return new DMNAlphaNetworkEvaluatorImpl(dmnCompiledAlphaNetworkEvaluator, feelHelper, decisionTableName, feelDecisionTable, dmnBaseNode, results);
}
Also used : DMNFEELHelper(org.kie.dmn.core.compiler.DMNFEELHelper) CompiledNetworkSources(org.drools.ancompiler.CompiledNetworkSources) HashMap(java.util.HashMap) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) CompiledNetwork(org.drools.ancompiler.CompiledNetwork) ObjectTypeNodeCompiler(org.drools.ancompiler.ObjectTypeNodeCompiler) CompilerContext(org.kie.dmn.feel.lang.CompilerContext) DMNCompilerContext(org.kie.dmn.core.compiler.DMNCompilerContext) OutputClausesWithType(org.kie.dmn.core.compiler.alphanetbased.evaluator.OutputClausesWithType) Type(org.kie.dmn.feel.lang.Type) OutputClausesWithType(org.kie.dmn.core.compiler.alphanetbased.evaluator.OutputClausesWithType)

Aggregations

CompilerContext (org.kie.dmn.feel.lang.CompilerContext)21 Test (org.junit.Test)13 CompiledExpression (org.kie.dmn.feel.lang.CompiledExpression)12 HashMap (java.util.HashMap)10 FEELEvent (org.kie.dmn.api.feel.runtime.events.FEELEvent)6 FEELEventListener (org.kie.dmn.api.feel.runtime.events.FEELEventListener)6 FEEL (org.kie.dmn.feel.FEEL)5 MapBackedType (org.kie.dmn.feel.lang.impl.MapBackedType)5 Map (java.util.Map)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DMNType (org.kie.dmn.api.core.DMNType)2 Type (org.kie.dmn.feel.lang.Type)2 ASTNode (org.kie.dmn.feel.lang.ast.ASTNode)2 FunctionInvocationNode (org.kie.dmn.feel.lang.ast.FunctionInvocationNode)2 GenListType (org.kie.dmn.feel.lang.types.GenListType)2 CompiledNetwork (org.drools.ancompiler.CompiledNetwork)1 CompiledNetworkSources (org.drools.ancompiler.CompiledNetworkSources)1 ObjectTypeNodeCompiler (org.drools.ancompiler.ObjectTypeNodeCompiler)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1