Search in sources :

Example 1 with TransformationDictionary

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

the class KiePMMLTransformationDictionaryFactoryTest method getKiePMMLTransformationDictionaryVariableDeclaration.

@Test
public void getKiePMMLTransformationDictionaryVariableDeclaration() throws IOException {
    TransformationDictionary transformationDictionary = new TransformationDictionary();
    transformationDictionary.addDefineFunctions(getDefineFunctions());
    transformationDictionary.addDerivedFields(getDerivedFields());
    BlockStmt retrieved = KiePMMLTransformationDictionaryFactory.getKiePMMLTransformationDictionaryVariableDeclaration(transformationDictionary);
    String text = getFileContent(TEST_01_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(text);
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(KiePMMLParameterField.class, KiePMMLConstant.class, KiePMMLFieldRef.class, KiePMMLApply.class, KiePMMLDerivedField.class, KiePMMLDefineFunction.class, KiePMMLTransformationDictionary.class, Arrays.class, Collections.class);
    commonValidateCompilationWithImports(retrieved, imports);
}
Also used : TransformationDictionary(org.dmg.pmml.TransformationDictionary) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Test(org.junit.Test)

Example 2 with TransformationDictionary

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

the class KiePMMLTransformationDictionaryInstanceFactoryTest method getKiePMMLTransformationDictionary.

@Test
public void getKiePMMLTransformationDictionary() {
    final TransformationDictionary toConvert = getRandomTransformationDictionary();
    KiePMMLTransformationDictionary retrieved = KiePMMLTransformationDictionaryInstanceFactory.getKiePMMLTransformationDictionary(toConvert, Collections.emptyList());
    assertNotNull(retrieved);
    List<DerivedField> derivedFields = toConvert.getDerivedFields();
    List<KiePMMLDerivedField> derivedFieldsToVerify = retrieved.getDerivedFields();
    assertEquals(derivedFields.size(), derivedFieldsToVerify.size());
    derivedFields.forEach(derivedFieldSource -> {
        Optional<KiePMMLDerivedField> derivedFieldToVerify = derivedFieldsToVerify.stream().filter(param -> param.getName().equals(derivedFieldSource.getName().getValue())).findFirst();
        assertTrue(derivedFieldToVerify.isPresent());
        commonVerifyKiePMMLDerivedField(derivedFieldToVerify.get(), derivedFieldSource);
    });
    List<DefineFunction> defineFunctions = toConvert.getDefineFunctions();
    List<KiePMMLDefineFunction> defineFunctionsToVerify = retrieved.getDefineFunctions();
    assertEquals(defineFunctions.size(), defineFunctionsToVerify.size());
    defineFunctions.forEach(defineFunctionSource -> {
        Optional<KiePMMLDefineFunction> defineFunctionToVerify = defineFunctionsToVerify.stream().filter(param -> param.getName().equals(defineFunctionSource.getName())).findFirst();
        assertTrue(defineFunctionToVerify.isPresent());
        commonVerifyKiePMMLDefineFunction(defineFunctionToVerify.get(), defineFunctionSource);
    });
}
Also used : InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) DefineFunction(org.dmg.pmml.DefineFunction) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) List(java.util.List) TransformationDictionary(org.dmg.pmml.TransformationDictionary) DerivedField(org.dmg.pmml.DerivedField) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) PMMLModelTestUtils.getRandomTransformationDictionary(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTransformationDictionary) Optional(java.util.Optional) Collections(java.util.Collections) InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction) Assert.assertEquals(org.junit.Assert.assertEquals) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) TransformationDictionary(org.dmg.pmml.TransformationDictionary) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) PMMLModelTestUtils.getRandomTransformationDictionary(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTransformationDictionary) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) DefineFunction(org.dmg.pmml.DefineFunction) InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction) InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField) DerivedField(org.dmg.pmml.DerivedField) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) Test(org.junit.Test)

Example 3 with TransformationDictionary

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

the class KiePMMLRegressionModelFactoryTest method setup.

@BeforeClass
public static void setup() {
    Random random = new Random();
    Set<String> fieldNames = new HashSet<>();
    regressionTables = IntStream.range(0, 3).mapToObj(i -> {
        List<CategoricalPredictor> categoricalPredictors = new ArrayList<>();
        List<NumericPredictor> numericPredictors = new ArrayList<>();
        List<PredictorTerm> predictorTerms = new ArrayList<>();
        IntStream.range(0, 3).forEach(j -> {
            String catFieldName = "CatPred-" + j;
            String numFieldName = "NumPred-" + j;
            categoricalPredictors.add(getCategoricalPredictor(catFieldName, random.nextDouble(), random.nextDouble()));
            numericPredictors.add(getNumericPredictor(numFieldName, random.nextInt(), random.nextDouble()));
            predictorTerms.add(getPredictorTerm("PredTerm-" + j, random.nextDouble(), Arrays.asList(catFieldName, numFieldName)));
            fieldNames.add(catFieldName);
            fieldNames.add(numFieldName);
        });
        return getRegressionTable(categoricalPredictors, numericPredictors, predictorTerms, tableIntercept + random.nextDouble(), tableTargetCategory + "-" + i);
    }).collect(Collectors.toList());
    dataFields = new ArrayList<>();
    miningFields = new ArrayList<>();
    fieldNames.forEach(fieldName -> {
        dataFields.add(getDataField(fieldName, OpType.CATEGORICAL, DataType.STRING));
        miningFields.add(getMiningField(fieldName, MiningField.UsageType.ACTIVE));
    });
    targetMiningField = miningFields.get(0);
    targetMiningField.setUsageType(MiningField.UsageType.TARGET);
    dataDictionary = getDataDictionary(dataFields);
    transformationDictionary = new TransformationDictionary();
    miningSchema = getMiningSchema(miningFields);
    regressionModel = getRegressionModel(modelName, MiningFunction.REGRESSION, miningSchema, regressionTables);
    COMPILATION_UNIT = getFromFileName(KIE_PMML_REGRESSION_MODEL_TEMPLATE_JAVA);
    MODEL_TEMPLATE = COMPILATION_UNIT.getClassByName(KIE_PMML_REGRESSION_MODEL_TEMPLATE).get();
    pmml = new PMML();
    pmml.setDataDictionary(dataDictionary);
    pmml.setTransformationDictionary(transformationDictionary);
    pmml.addModels(regressionModel);
}
Also used : PredictorTerm(org.dmg.pmml.regression.PredictorTerm) PMMLModelTestUtils.getPredictorTerm(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getPredictorTerm) TransformationDictionary(org.dmg.pmml.TransformationDictionary) ArrayList(java.util.ArrayList) NumericPredictor(org.dmg.pmml.regression.NumericPredictor) PMMLModelTestUtils.getNumericPredictor(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getNumericPredictor) CategoricalPredictor(org.dmg.pmml.regression.CategoricalPredictor) PMMLModelTestUtils.getCategoricalPredictor(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getCategoricalPredictor) Random(java.util.Random) PMML(org.dmg.pmml.PMML) HashSet(java.util.HashSet) BeforeClass(org.junit.BeforeClass)

Example 4 with TransformationDictionary

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

the class PMMLModelTestUtils method getRandomTransformationDictionary.

public static TransformationDictionary getRandomTransformationDictionary() {
    final TransformationDictionary toReturn = new TransformationDictionary();
    IntStream.range(0, 3).forEach(i -> {
        toReturn.addDerivedFields(getDerivedField("DerivedField-" + i));
        toReturn.addDefineFunctions(getDefineFunction("DefineFunction-" + i));
    });
    return toReturn;
}
Also used : TransformationDictionary(org.dmg.pmml.TransformationDictionary)

Aggregations

TransformationDictionary (org.dmg.pmml.TransformationDictionary)4 Test (org.junit.Test)2 KiePMMLTransformationDictionary (org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 Statement (com.github.javaparser.ast.stmt.Statement)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Random (java.util.Random)1 DefineFunction (org.dmg.pmml.DefineFunction)1 DerivedField (org.dmg.pmml.DerivedField)1 PMML (org.dmg.pmml.PMML)1 CategoricalPredictor (org.dmg.pmml.regression.CategoricalPredictor)1 NumericPredictor (org.dmg.pmml.regression.NumericPredictor)1 PredictorTerm (org.dmg.pmml.regression.PredictorTerm)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertTrue (org.junit.Assert.assertTrue)1