Search in sources :

Example 1 with DMNFEELHelper

use of org.kie.dmn.core.compiler.DMNFEELHelper 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 DMNFEELHelper

use of org.kie.dmn.core.compiler.DMNFEELHelper in project drools by kiegroup.

the class DMNImportPMMLInfo method from.

public static Either<Exception, DMNImportPMMLInfo> from(InputStream is, DMNCompilerConfigurationImpl cc, DMNModelImpl model, Import i) {
    try {
        final PMML pmml = org.jpmml.model.PMMLUtil.unmarshal(is);
        PMMLHeaderInfo h = PMMLInfo.pmmlToHeaderInfo(pmml, pmml.getHeader());
        for (DataField df : pmml.getDataDictionary().getDataFields()) {
            String dfName = df.getName().getValue();
            BuiltInType ft = getBuiltInTypeByDataType(df.getDataType());
            List<FEELProfile> helperFEELProfiles = cc.getFeelProfiles();
            DMNFEELHelper feel = new DMNFEELHelper(cc.getRootClassLoader(), helperFEELProfiles);
            List<UnaryTest> av = new ArrayList<>();
            if (df.getValues() != null && !df.getValues().isEmpty() && ft != BuiltInType.UNKNOWN) {
                final BuiltInType feelType = ft;
                String lov = df.getValues().stream().map(Value::getValue).map(o -> feelType == BuiltInType.STRING ? "\"" + o.toString() + "\"" : o.toString()).collect(Collectors.joining(","));
                av = feel.evaluateUnaryTests(lov, Collections.emptyMap());
            } else if (df.getIntervals() != null && !df.getIntervals().isEmpty() && ft != BuiltInType.UNKNOWN) {
                for (Interval interval : df.getIntervals()) {
                    String utString = null;
                    switch(interval.getClosure()) {
                        case CLOSED_CLOSED:
                            utString = new StringBuilder("[").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append("]").toString();
                            break;
                        case CLOSED_OPEN:
                            utString = new StringBuilder("[").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append(")").toString();
                            break;
                        case OPEN_CLOSED:
                            utString = new StringBuilder("(").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append("]").toString();
                            break;
                        case OPEN_OPEN:
                            utString = new StringBuilder("(").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append(")").toString();
                            break;
                    }
                    List<UnaryTest> ut = feel.evaluateUnaryTests(utString, Collections.emptyMap());
                    av.addAll(ut);
                }
            }
            DMNType type = new SimpleTypeImpl(i.getNamespace(), dfName, null, false, av, model.getTypeRegistry().resolveType(model.getDefinitions().getURIFEEL(), ft.getName()), ft);
            model.getTypeRegistry().registerType(type);
        }
        pmml.getModels().stream().forEach(m -> registerOutputFieldType(m, model, i));
        List<DMNPMMLModelInfo> models = pmml.getModels().stream().map(m -> PMMLInfo.pmmlToModelInfo(m)).map(proto -> DMNPMMLModelInfo.from(proto, model, i)).collect(Collectors.toList());
        DMNImportPMMLInfo info = new DMNImportPMMLInfo(i, models, h);
        return Either.ofRight(info);
    } catch (Throwable e) {
        return Either.ofLeft(new Exception("Unable to process DMNImportPMMLInfo", e));
    }
}
Also used : DMNType(org.kie.dmn.api.core.DMNType) Interval(org.dmg.pmml.Interval) Model(org.dmg.pmml.Model) OutputField(org.dmg.pmml.OutputField) FEELProfile(org.kie.dmn.feel.lang.FEELProfile) LoggerFactory(org.slf4j.LoggerFactory) Either(org.kie.dmn.feel.util.Either) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) Value(org.dmg.pmml.Value) Map(java.util.Map) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) PMML(org.dmg.pmml.PMML) DMNCompilerConfigurationImpl(org.kie.dmn.core.compiler.DMNCompilerConfigurationImpl) Logger(org.slf4j.Logger) Collection(java.util.Collection) DataType(org.dmg.pmml.DataType) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) Import(org.kie.dmn.model.api.Import) Collectors(java.util.stream.Collectors) DataField(org.dmg.pmml.DataField) List(java.util.List) DMNFEELHelper(org.kie.dmn.core.compiler.DMNFEELHelper) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) Collections(java.util.Collections) InputStream(java.io.InputStream) DMNFEELHelper(org.kie.dmn.core.compiler.DMNFEELHelper) FEELProfile(org.kie.dmn.feel.lang.FEELProfile) ArrayList(java.util.ArrayList) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) DataField(org.dmg.pmml.DataField) SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) Value(org.dmg.pmml.Value) PMML(org.dmg.pmml.PMML) ArrayList(java.util.ArrayList) List(java.util.List) Interval(org.dmg.pmml.Interval) DMNType(org.kie.dmn.api.core.DMNType)

Aggregations

HashMap (java.util.HashMap)2 DMNFEELHelper (org.kie.dmn.core.compiler.DMNFEELHelper)2 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 DataField (org.dmg.pmml.DataField)1 DataType (org.dmg.pmml.DataType)1 Interval (org.dmg.pmml.Interval)1 Model (org.dmg.pmml.Model)1 OutputField (org.dmg.pmml.OutputField)1 PMML (org.dmg.pmml.PMML)1 Value (org.dmg.pmml.Value)1 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