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));
}
}
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;
}
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();
}
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;
}
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);
}
Aggregations