Search in sources :

Example 6 with Interval

use of org.dmg.pmml.Interval in project drools by kiegroup.

the class KiePMMLDiscretizeBinFactoryTest method getDiscretizeBinVariableDeclaration.

@Test
public void getDiscretizeBinVariableDeclaration() throws IOException {
    String variableName = "variableName";
    double leftMargin = 45.32;
    Interval interval = new Interval();
    interval.setLeftMargin(leftMargin);
    interval.setRightMargin(null);
    interval.setClosure(Interval.Closure.CLOSED_OPEN);
    String binValue = "binValue";
    DiscretizeBin discretizeBin = new DiscretizeBin();
    discretizeBin.setBinValue(binValue);
    discretizeBin.setInterval(interval);
    BlockStmt retrieved = KiePMMLDiscretizeBinFactory.getDiscretizeBinVariableDeclaration(variableName, discretizeBin);
    String closureString = CLOSURE.class.getName() + "." + CLOSURE.byName(interval.getClosure().value()).name();
    String text = getFileContent(TEST_01_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, leftMargin, closureString, binValue));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(Collections.class, KiePMMLDiscretizeBin.class, KiePMMLInterval.class);
    commonValidateCompilationWithImports(retrieved, imports);
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLDiscretizeBin(org.kie.pmml.commons.model.expressions.KiePMMLDiscretizeBin) DiscretizeBin(org.dmg.pmml.DiscretizeBin) Interval(org.dmg.pmml.Interval) KiePMMLInterval(org.kie.pmml.commons.model.expressions.KiePMMLInterval) Test(org.junit.Test)

Example 7 with Interval

use of org.dmg.pmml.Interval in project drools by kiegroup.

the class KiePMMLIntervalFactoryTest method getIntervalVariableDeclaration.

@Test
public void getIntervalVariableDeclaration() throws IOException {
    String variableName = "variableName";
    double leftMargin = 45.32;
    Interval interval = new Interval();
    interval.setLeftMargin(leftMargin);
    interval.setRightMargin(null);
    interval.setClosure(Interval.Closure.CLOSED_OPEN);
    BlockStmt retrieved = KiePMMLIntervalFactory.getIntervalVariableDeclaration(variableName, interval);
    String closureString = CLOSURE.class.getName() + "." + CLOSURE.byName(interval.getClosure().value()).name();
    String text = getFileContent(TEST_01_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, leftMargin, closureString));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(Collections.class, KiePMMLInterval.class);
    commonValidateCompilationWithImports(retrieved, imports);
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Interval(org.dmg.pmml.Interval) KiePMMLInterval(org.kie.pmml.commons.model.expressions.KiePMMLInterval) Test(org.junit.Test)

Example 8 with Interval

use of org.dmg.pmml.Interval in project drools by kiegroup.

the class KiePMMLDiscretizeFactoryTest method getInterval.

private static Interval getInterval(Number leftMargin, Number rightMargin, Interval.Closure closure) {
    Interval toReturn = new Interval();
    toReturn.setLeftMargin(leftMargin);
    toReturn.setRightMargin(rightMargin);
    toReturn.setClosure(closure);
    return toReturn;
}
Also used : Interval(org.dmg.pmml.Interval) KiePMMLInterval(org.kie.pmml.commons.model.expressions.KiePMMLInterval)

Example 9 with Interval

use of org.dmg.pmml.Interval 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)

Example 10 with Interval

use of org.dmg.pmml.Interval in project drools by kiegroup.

the class PMMLModelTestUtils method getRandomInterval.

public static Interval getRandomInterval() {
    Random random = new Random();
    Number leftMargin = random.nextInt(100) / 10;
    Number rightMargin = leftMargin.doubleValue() + random.nextInt(50) / 10;
    Interval toReturn = new Interval();
    toReturn.setLeftMargin(leftMargin);
    toReturn.setRightMargin(rightMargin);
    toReturn.setClosure(getRandomClosure());
    return toReturn;
}
Also used : Random(java.util.Random) Interval(org.dmg.pmml.Interval)

Aggregations

Interval (org.dmg.pmml.Interval)14 DiscretizeBin (org.dmg.pmml.DiscretizeBin)6 ArrayList (java.util.ArrayList)5 Discretize (org.dmg.pmml.Discretize)4 Test (org.junit.Test)4 KiePMMLInterval (org.kie.pmml.commons.model.expressions.KiePMMLInterval)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 Statement (com.github.javaparser.ast.stmt.Statement)2 HashMap (java.util.HashMap)2 DerivedField (org.dmg.pmml.DerivedField)2 Value (org.dmg.pmml.Value)2 InputStream (java.io.InputStream)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Random (java.util.Random)1 Collectors (java.util.stream.Collectors)1 Bucketizer (org.apache.spark.ml.feature.Bucketizer)1 ContStats (org.dmg.pmml.ContStats)1