Search in sources :

Example 1 with CompiledNetwork

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);
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) AlphaRangeIndex(org.drools.core.util.index.AlphaRangeIndex) RunWith(org.junit.runner.RunWith) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) ArrayList(java.util.ArrayList) ReleaseId(org.kie.api.builder.ReleaseId) BigDecimal(java.math.BigDecimal) CompiledNetwork(org.drools.ancompiler.CompiledNetwork) DateUtils(org.drools.core.util.DateUtils) KieServices(org.kie.api.KieServices) Assertions(org.assertj.core.api.Assertions) MyComparableHolder(org.drools.testcoverage.common.model.MyComparableHolder) Order(org.drools.testcoverage.common.model.Order) TestParametersUtil(org.drools.testcoverage.common.util.TestParametersUtil) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Parameterized(org.junit.runners.Parameterized) ObjectSink(org.drools.core.reteoo.ObjectSink) Address(org.drools.testcoverage.common.model.Address) ObjectSinkPropagator(org.drools.core.reteoo.ObjectSinkPropagator) AlphaRangeIndexThresholdOption(org.kie.internal.conf.AlphaRangeIndexThresholdOption) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test) Primitives(org.drools.testcoverage.common.model.Primitives) CompositeObjectSinkAdapter(org.drools.core.reteoo.CompositeObjectSinkAdapter) List(java.util.List) KieUtil(org.drools.testcoverage.common.util.KieUtil) KieModule(org.kie.api.builder.KieModule) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Person(org.drools.testcoverage.common.model.Person) MyComparable(org.drools.testcoverage.common.model.MyComparable) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Assert.assertEquals(org.junit.Assert.assertEquals) ObjectSinkPropagator(org.drools.core.reteoo.ObjectSinkPropagator) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) CompositeObjectSinkAdapter(org.drools.core.reteoo.CompositeObjectSinkAdapter) ObjectSink(org.drools.core.reteoo.ObjectSink)

Example 2 with CompiledNetwork

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

CompiledNetwork (org.drools.ancompiler.CompiledNetwork)2 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Assertions (org.assertj.core.api.Assertions)1 CompiledNetworkSources (org.drools.ancompiler.CompiledNetworkSources)1 ObjectTypeNodeCompiler (org.drools.ancompiler.ObjectTypeNodeCompiler)1 CompositeObjectSinkAdapter (org.drools.core.reteoo.CompositeObjectSinkAdapter)1 ObjectSink (org.drools.core.reteoo.ObjectSink)1 ObjectSinkPropagator (org.drools.core.reteoo.ObjectSinkPropagator)1 DateUtils (org.drools.core.util.DateUtils)1 AlphaRangeIndex (org.drools.core.util.index.AlphaRangeIndex)1 Address (org.drools.testcoverage.common.model.Address)1 MyComparable (org.drools.testcoverage.common.model.MyComparable)1 MyComparableHolder (org.drools.testcoverage.common.model.MyComparableHolder)1 Order (org.drools.testcoverage.common.model.Order)1 Person (org.drools.testcoverage.common.model.Person)1