Search in sources :

Example 1 with ObjectTypeNodeCompiler

use of org.drools.ancompiler.ObjectTypeNodeCompiler 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)

Example 2 with ObjectTypeNodeCompiler

use of org.drools.ancompiler.ObjectTypeNodeCompiler in project drools by kiegroup.

the class DMNAlphaNetworkEvaluatorCompiler method createAlphaNetworkCompiler.

private ObjectTypeNodeCompiler createAlphaNetworkCompiler(ObjectTypeNode firstObjectTypeNodeOfRete) {
    ANCConfiguration ancConfiguration = new ANCConfiguration();
    ancConfiguration.setDisableContextEntry(true);
    ancConfiguration.setPrettyPrint(true);
    ancConfiguration.setEnableModifyObject(false);
    ObjectTypeNodeCompiler objectTypeNodeCompiler = new ObjectTypeNodeCompiler(ancConfiguration, firstObjectTypeNodeOfRete, true);
    VariableDeclarator variableDeclarator = new VariableDeclarator(StaticJavaParser.parseType(AlphaNetworkEvaluationContext.class.getCanonicalName()), "ctx");
    objectTypeNodeCompiler.addAdditionalFields(new FieldDeclaration(NodeList.nodeList(), NodeList.nodeList(variableDeclarator)));
    return objectTypeNodeCompiler;
}
Also used : ANCConfiguration(org.drools.ancompiler.ANCConfiguration) ObjectTypeNodeCompiler(org.drools.ancompiler.ObjectTypeNodeCompiler) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Aggregations

ObjectTypeNodeCompiler (org.drools.ancompiler.ObjectTypeNodeCompiler)2 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 HashMap (java.util.HashMap)1 ANCConfiguration (org.drools.ancompiler.ANCConfiguration)1 CompiledNetwork (org.drools.ancompiler.CompiledNetwork)1 CompiledNetworkSources (org.drools.ancompiler.CompiledNetworkSources)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1 DMNCompilerContext (org.kie.dmn.core.compiler.DMNCompilerContext)1 DMNFEELHelper (org.kie.dmn.core.compiler.DMNFEELHelper)1 OutputClausesWithType (org.kie.dmn.core.compiler.alphanetbased.evaluator.OutputClausesWithType)1 CompilerContext (org.kie.dmn.feel.lang.CompilerContext)1 Type (org.kie.dmn.feel.lang.Type)1