use of org.drools.ancompiler.CompiledNetwork in project drools by kiegroup.
the class AlphaNodeRangeIndexingTest method assertSinks.
private void assertSinks(KieBase kbase, Class<?> factClass, int sinksLength, int sinkAdapterSize, int rangeIndexableSinksSize, int rangeIndexSize) {
final ObjectTypeNode otn = KieUtil.getObjectTypeNode(kbase, factClass);
assertNotNull(otn);
ObjectSinkPropagator objectSinkPropagator = otn.getObjectSinkPropagator();
if (this.kieBaseTestConfiguration.useAlphaNetworkCompiler()) {
objectSinkPropagator = ((CompiledNetwork) objectSinkPropagator).getOriginalSinkPropagator();
}
CompositeObjectSinkAdapter sinkAdapter = (CompositeObjectSinkAdapter) objectSinkPropagator;
ObjectSink[] sinks = sinkAdapter.getSinks();
assertEquals(sinksLength, sinks.length);
assertEquals(sinkAdapterSize, sinkAdapter.size());
if (rangeIndexableSinksSize == 0) {
assertNull(sinkAdapter.getRangeIndexableSinks());
} else {
assertEquals(rangeIndexableSinksSize, sinkAdapter.getRangeIndexableSinks().size());
}
if (rangeIndexSize == 0) {
assertNull(sinkAdapter.getRangeIndexMap());
} else {
long count = sinkAdapter.getRangeIndexMap().values().stream().flatMap(index -> index.getAllValues().stream()).count();
assertEquals(rangeIndexSize, count);
}
}
use of org.drools.ancompiler.CompiledNetwork 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