Search in sources :

Example 26 with DataDictionary

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

the class KiePMMLCompoundPredicateFactoryTest method getCompoundPredicateVariableDeclaration.

@Test
public void getCompoundPredicateVariableDeclaration() throws IOException {
    String variableName = "variableName";
    SimplePredicate simplePredicate1 = getSimplePredicate(PARAM_1, value1, operator1);
    SimplePredicate simplePredicate2 = getSimplePredicate(PARAM_2, value2, operator2);
    Array.Type arrayType = Array.Type.STRING;
    List<String> values = getStringObjects(arrayType, 4);
    SimpleSetPredicate simpleSetPredicate = getSimpleSetPredicate(values, arrayType, SimpleSetPredicate.BooleanOperator.IS_IN);
    CompoundPredicate compoundPredicate = new CompoundPredicate();
    compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.AND);
    compoundPredicate.getPredicates().add(0, simplePredicate1);
    compoundPredicate.getPredicates().add(1, simplePredicate2);
    compoundPredicate.getPredicates().add(2, simpleSetPredicate);
    DataField dataField1 = new DataField();
    dataField1.setName(simplePredicate1.getField());
    dataField1.setDataType(DataType.DOUBLE);
    DataField dataField2 = new DataField();
    dataField2.setName(simplePredicate2.getField());
    dataField2.setDataType(DataType.DOUBLE);
    DataField dataField3 = new DataField();
    dataField3.setName(simpleSetPredicate.getField());
    dataField3.setDataType(DataType.DOUBLE);
    DataDictionary dataDictionary = new DataDictionary();
    dataDictionary.addDataFields(dataField1, dataField2, dataField3);
    String booleanOperatorString = BOOLEAN_OPERATOR.class.getName() + "." + BOOLEAN_OPERATOR.byName(compoundPredicate.getBooleanOperator().value()).name();
    String valuesString = values.stream().map(valueString -> "\"" + valueString + "\"").collect(Collectors.joining(","));
    final List<Field<?>> fields = getFieldsFromDataDictionary(dataDictionary);
    BlockStmt retrieved = KiePMMLCompoundPredicateFactory.getCompoundPredicateVariableDeclaration(variableName, compoundPredicate, fields);
    String text = getFileContent(TEST_01_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, valuesString, booleanOperatorString));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(KiePMMLCompoundPredicate.class, KiePMMLSimplePredicate.class, KiePMMLSimpleSetPredicate.class, Arrays.class, Collections.class);
    commonValidateCompilationWithImports(retrieved, imports);
}
Also used : Arrays(java.util.Arrays) PMMLModelTestUtils.getSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getSimplePredicate) Field(org.dmg.pmml.Field) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicate) JavaParserUtils(org.kie.pmml.compiler.commons.utils.JavaParserUtils) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) DataType(org.dmg.pmml.DataType) KiePMMLSimpleSetPredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimpleSetPredicate) BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) KiePMMLCompoundPredicate(org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) DataDictionary(org.dmg.pmml.DataDictionary) Test(org.junit.Test) Statement(com.github.javaparser.ast.stmt.Statement) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) Collectors(java.util.stream.Collectors) Array(org.dmg.pmml.Array) FileUtils.getFileContent(org.kie.test.util.filesystem.FileUtils.getFileContent) DataField(org.dmg.pmml.DataField) List(java.util.List) SimplePredicate(org.dmg.pmml.SimplePredicate) PMMLModelTestUtils.getStringObjects(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getStringObjects) CodegenTestUtils.commonValidateCompilationWithImports(org.kie.pmml.compiler.commons.testutils.CodegenTestUtils.commonValidateCompilationWithImports) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate) Collections(java.util.Collections) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) PMMLModelTestUtils.getSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getSimplePredicate) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) KiePMMLSimpleSetPredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimpleSetPredicate) KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate) Array(org.dmg.pmml.Array) Field(org.dmg.pmml.Field) DataField(org.dmg.pmml.DataField) DataField(org.dmg.pmml.DataField) CompoundPredicate(org.dmg.pmml.CompoundPredicate) KiePMMLCompoundPredicate(org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate) Test(org.junit.Test)

Example 27 with DataDictionary

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

the class KiePMMLSimplePredicateFactoryTest method getSimplePredicateVariableDeclaration.

@Test
public void getSimplePredicateVariableDeclaration() throws IOException {
    String variableName = "variableName";
    final SimplePredicate simplePredicate = new SimplePredicate();
    simplePredicate.setField(FieldName.create("CUSTOM_FIELD"));
    simplePredicate.setValue("235.435");
    simplePredicate.setOperator(SimplePredicate.Operator.EQUAL);
    String operatorString = OPERATOR.class.getName() + "." + OPERATOR.byName(simplePredicate.getOperator().value());
    DataField dataField = new DataField();
    dataField.setName(simplePredicate.getField());
    dataField.setDataType(DataType.DOUBLE);
    DataDictionary dataDictionary = new DataDictionary();
    dataDictionary.addDataFields(dataField);
    BlockStmt retrieved = KiePMMLSimplePredicateFactory.getSimplePredicateVariableDeclaration(variableName, simplePredicate, getFieldsFromDataDictionary(dataDictionary));
    String text = getFileContent(TEST_01_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, simplePredicate.getField().getValue(), operatorString, simplePredicate.getValue()));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(KiePMMLSimplePredicate.class, Collections.class);
    commonValidateCompilationWithImports(retrieved, imports);
}
Also used : DataField(org.dmg.pmml.DataField) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) Test(org.junit.Test)

Example 28 with DataDictionary

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

the class ModelUtilsTest method getOpTypeByTargetsNotFound.

@Test(expected = KiePMMLInternalException.class)
public void getOpTypeByTargetsNotFound() {
    final Model model = new RegressionModel();
    final DataDictionary dataDictionary = new DataDictionary();
    final MiningSchema miningSchema = new MiningSchema();
    final Targets targets = new Targets();
    IntStream.range(0, 3).forEach(i -> {
        String fieldName = "field" + i;
        final DataField dataField = getRandomDataField();
        dataField.setName(FieldName.create(fieldName));
        dataDictionary.addDataFields(dataField);
        final MiningField miningField = getRandomMiningField();
        miningField.setName(dataField.getName());
        miningSchema.addMiningFields(miningField);
        final Target targetField = getRandomTarget();
        targetField.setField(dataField.getName());
        targets.addTargets(targetField);
    });
    model.setMiningSchema(miningSchema);
    model.setTargets(targets);
    ModelUtils.getOpType(getFieldsFromDataDictionary(dataDictionary), model, "NOT_EXISTING");
}
Also used : PMMLModelTestUtils.getMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField) MiningField(org.dmg.pmml.MiningField) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) Target(org.dmg.pmml.Target) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) PMMLModelTestUtils.getTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getTarget) MiningSchema(org.dmg.pmml.MiningSchema) DataField(org.dmg.pmml.DataField) PMMLModelTestUtils.getDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) Model(org.dmg.pmml.Model) RegressionModel(org.dmg.pmml.regression.RegressionModel) Targets(org.dmg.pmml.Targets) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) RegressionModel(org.dmg.pmml.regression.RegressionModel) Test(org.junit.Test)

Example 29 with DataDictionary

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

the class ModelUtilsTest method getTargetFieldsWithTargetFieldsWithoutOptType.

@Test
public void getTargetFieldsWithTargetFieldsWithoutOptType() {
    final Model model = new RegressionModel();
    final DataDictionary dataDictionary = new DataDictionary();
    final MiningSchema miningSchema = new MiningSchema();
    IntStream.range(0, 3).forEach(i -> {
        final String fieldName = "fieldName-" + i;
        final DataField dataField = getDataField(fieldName, OpType.CATEGORICAL, DataType.STRING);
        dataDictionary.addDataFields(dataField);
        final MiningField miningField = getMiningField(fieldName, MiningField.UsageType.PREDICTED);
        miningField.setOpType(null);
        miningSchema.addMiningFields(miningField);
    });
    model.setMiningSchema(miningSchema);
    List<KiePMMLNameOpType> retrieved = ModelUtils.getTargetFields(getFieldsFromDataDictionary(dataDictionary), model);
    assertNotNull(retrieved);
    assertEquals(miningSchema.getMiningFields().size(), retrieved.size());
    retrieved.forEach(kiePMMLNameOpType -> {
        assertTrue(miningSchema.getMiningFields().stream().anyMatch(fld -> kiePMMLNameOpType.getName().equals(fld.getName().getValue())));
        Optional<DataField> optionalDataField = dataDictionary.getDataFields().stream().filter(fld -> kiePMMLNameOpType.getName().equals(fld.getName().getValue())).findFirst();
        assertTrue(optionalDataField.isPresent());
        DataField dataField = optionalDataField.get();
        OP_TYPE expected = OP_TYPE.byName(dataField.getOpType().value());
        assertEquals(expected, kiePMMLNameOpType.getOpType());
    });
}
Also used : KiePMMLNameOpType(org.kie.pmml.commons.model.tuples.KiePMMLNameOpType) RESULT_FEATURE(org.kie.pmml.api.enums.RESULT_FEATURE) ModelUtils.getPrefixedName(org.kie.pmml.compiler.api.utils.ModelUtils.getPrefixedName) Arrays(java.util.Arrays) Date(java.util.Date) Model(org.dmg.pmml.Model) MiningSchema(org.dmg.pmml.MiningSchema) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) Row(org.dmg.pmml.Row) FieldName(org.dmg.pmml.FieldName) FIELD_USAGE_TYPE(org.kie.pmml.api.enums.FIELD_USAGE_TYPE) OpType(org.dmg.pmml.OpType) Map(java.util.Map) InputCell(org.jpmml.model.inlinetable.InputCell) PMMLModelTestUtils.getDataTypes(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataTypes) PMMLModelTestUtils.getMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField) RegressionModel(org.dmg.pmml.regression.RegressionModel) Targets(org.dmg.pmml.Targets) DataType(org.dmg.pmml.DataType) PMMLModelTestUtils.getRandomRow(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomRow) Collectors(java.util.stream.Collectors) PMMLModelTestUtils.getParameterFields(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getParameterFields) DataField(org.dmg.pmml.DataField) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) PMMLModelTestUtils.getArray(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getArray) PMMLModelTestUtils.getRandomDataType(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataType) ParameterField(org.dmg.pmml.ParameterField) IntStream(java.util.stream.IntStream) OutputField(org.dmg.pmml.OutputField) PMMLModelTestUtils.getDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Field(org.dmg.pmml.Field) DerivedField(org.dmg.pmml.DerivedField) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLNameOpType(org.kie.pmml.commons.model.tuples.KiePMMLNameOpType) OutputCell(org.jpmml.model.inlinetable.OutputCell) MiningField(org.dmg.pmml.MiningField) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) DataDictionary(org.dmg.pmml.DataDictionary) Test(org.junit.Test) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) Target(org.dmg.pmml.Target) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) Array(org.dmg.pmml.Array) PMMLModelTestUtils.getRandomOutputField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOutputField) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) Assert.assertNull(org.junit.Assert.assertNull) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) PMMLModelTestUtils.getTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getTarget) PMMLModelTestUtils.getRandomRowWithCells(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomRowWithCells) Assert.assertEquals(org.junit.Assert.assertEquals) PMMLModelTestUtils.getMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField) MiningField(org.dmg.pmml.MiningField) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) MiningSchema(org.dmg.pmml.MiningSchema) DataField(org.dmg.pmml.DataField) PMMLModelTestUtils.getDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) Model(org.dmg.pmml.Model) RegressionModel(org.dmg.pmml.regression.RegressionModel) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) RegressionModel(org.dmg.pmml.regression.RegressionModel) Test(org.junit.Test)

Example 30 with DataDictionary

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

the class ModelUtilsTest method getOpTypeByDataFieldsNotFound.

@Test(expected = KiePMMLInternalException.class)
public void getOpTypeByDataFieldsNotFound() {
    final Model model = new RegressionModel();
    final DataDictionary dataDictionary = new DataDictionary();
    IntStream.range(0, 3).forEach(i -> {
        String fieldName = "field" + i;
        final DataField dataField = getRandomDataField();
        dataField.setName(FieldName.create(fieldName));
        dataDictionary.addDataFields(dataField);
    });
    ModelUtils.getOpType(getFieldsFromDataDictionary(dataDictionary), model, "NOT_EXISTING");
}
Also used : DataField(org.dmg.pmml.DataField) PMMLModelTestUtils.getDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) Model(org.dmg.pmml.Model) RegressionModel(org.dmg.pmml.regression.RegressionModel) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) RegressionModel(org.dmg.pmml.regression.RegressionModel) Test(org.junit.Test)

Aggregations

DataDictionary (org.dmg.pmml.DataDictionary)48 DataField (org.dmg.pmml.DataField)41 Test (org.junit.Test)41 CommonTestingUtils.getFieldsFromDataDictionary (org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary)30 MiningSchema (org.dmg.pmml.MiningSchema)28 MiningField (org.dmg.pmml.MiningField)27 RegressionModel (org.dmg.pmml.regression.RegressionModel)27 PMMLModelTestUtils.getDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField)21 PMMLModelTestUtils.getRandomDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField)21 Model (org.dmg.pmml.Model)19 PMMLModelTestUtils.getMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField)17 PMMLModelTestUtils.getRandomMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField)17 PMML (org.dmg.pmml.PMML)12 OutputField (org.dmg.pmml.OutputField)11 Collectors (java.util.stream.Collectors)10 Assert.assertTrue (org.junit.Assert.assertTrue)10 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)10 OP_TYPE (org.kie.pmml.api.enums.OP_TYPE)10 HasClassLoaderMock (org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock)10 Arrays (java.util.Arrays)9