Search in sources :

Example 41 with DataField

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

the class KiePMMLUtil method populateMissingMiningTargetField.

/**
 * Method to populate <code>MiningSchema</code> with a n ad-hoc created target <code>MiningField</code>.
 * It also populate the given <code>List&lt;DataField&gt;</code> with the relative <code>DataField</code>.
 * This method has to be called <b>after</b> the model name has been set
 * @param model
 * @param dataFields
 */
static void populateMissingMiningTargetField(final Model model, final List<DataField> dataFields) {
    List<MiningField> miningTargetFields = getMiningTargetFields(model.getMiningSchema().getMiningFields());
    if (miningTargetFields.isEmpty()) {
        Optional<DataField> targetDataField = getTargetDataField(model);
        targetDataField.ifPresent(dataField -> {
            dataFields.add(dataField);
            MiningField targetMiningField = getTargetMiningField(dataField);
            model.getMiningSchema().addMiningFields(targetMiningField);
            correctTargetFields(targetMiningField, model.getTargets());
        });
    }
}
Also used : MiningField(org.dmg.pmml.MiningField) DataField(org.dmg.pmml.DataField)

Example 42 with DataField

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

the class KiePMMLUtil method getTargetDataField.

/**
 * Returns a model-specific <b>target</b> <code>DataField</code>
 * @param model
 * @return
 */
static Optional<DataField> getTargetDataField(final Model model) {
    DataType targetDataType = getTargetDataType(model.getMiningFunction(), model.getMathContext());
    OpType targetOpType = getTargetOpType(model.getMiningFunction());
    if (targetDataType == null || targetOpType == null) {
        return Optional.empty();
    }
    String cleanedName = model.getModelName().replaceAll("[^A-Za-z0-9]", "");
    String fieldName = String.format(TARGETFIELD_TEMPLATE, cleanedName);
    DataField toReturn = new DataField();
    toReturn.setName(FieldName.create(fieldName));
    toReturn.setOpType(targetOpType);
    toReturn.setDataType(targetDataType);
    return Optional.of(toReturn);
}
Also used : DataField(org.dmg.pmml.DataField) DataType(org.dmg.pmml.DataType) OpType(org.dmg.pmml.OpType)

Example 43 with DataField

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

the class KiePMMLSimplePredicateInstanceFactoryTest method getKiePMMLSimplePredicate.

@Test
public void getKiePMMLSimplePredicate() {
    List<Field<?>> fields = IntStream.range(0, 3).mapToObj(i -> getRandomDataField()).collect(Collectors.toList());
    final SimplePredicate toConvert = getRandomSimplePredicate((DataField) fields.get(0));
    final KiePMMLSimplePredicate retrieved = KiePMMLSimplePredicateInstanceFactory.getKiePMMLSimplePredicate(toConvert, fields);
    commonVerifyKiePMMLSimplePredicate(retrieved, toConvert, (DataField) fields.get(0));
}
Also used : IntStream(java.util.stream.IntStream) DataField(org.dmg.pmml.DataField) PMMLModelTestUtils.getRandomSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomSimplePredicate) List(java.util.List) SimplePredicate(org.dmg.pmml.SimplePredicate) Field(org.dmg.pmml.Field) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) InstanceFactoriesTestCommon.commonVerifyKiePMMLSimplePredicate(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLSimplePredicate) Test(org.junit.Test) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) Collectors(java.util.stream.Collectors) DataField(org.dmg.pmml.DataField) Field(org.dmg.pmml.Field) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) InstanceFactoriesTestCommon.commonVerifyKiePMMLSimplePredicate(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLSimplePredicate) PMMLModelTestUtils.getRandomSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomSimplePredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) InstanceFactoriesTestCommon.commonVerifyKiePMMLSimplePredicate(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLSimplePredicate) Test(org.junit.Test)

Example 44 with DataField

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

the class KiePMMLDataDictionaryASTFactoryTest method declareType.

@Test
public void declareType() {
    DataField dataField = getTypeDataField();
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
    KiePMMLDroolsType retrieved = KiePMMLDataDictionaryASTFactory.factory(fieldTypeMap).declareType(dataField);
    assertNotNull(retrieved);
    commonVerifyTypeDeclarationDescr(dataField, fieldTypeMap, retrieved);
}
Also used : KiePMMLASTTestUtils.getDottedTypeDataField(org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getDottedTypeDataField) DataField(org.dmg.pmml.DataField) KiePMMLASTTestUtils.getTypeDataField(org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getTypeDataField) HashMap(java.util.HashMap) KiePMMLDroolsType(org.kie.pmml.models.drools.ast.KiePMMLDroolsType) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 45 with DataField

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

the class DroolsModelProviderTest method getKiePMMLModelWithKnowledgeBuilder.

@Test
public void getKiePMMLModelWithKnowledgeBuilder() {
    KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
    final CommonCompilationDTO<Scorecard> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, scorecard, new HasKnowledgeBuilderMock(knowledgeBuilder));
    KiePMMLDroolsModel retrieved = droolsModelProvider.getKiePMMLModel(compilationDTO);
    assertNotNull(retrieved);
    assertTrue(retrieved instanceof KiePMMLDroolsModelTest);
    KiePMMLDroolsModelTest retrievedTest = (KiePMMLDroolsModelTest) retrieved;
    final List<DataField> originalDataFields = pmml.getDataDictionary().getDataFields();
    final List<DataField> retrievedDataFields = retrievedTest.dataDictionary.getDataFields();
    assertEquals(originalDataFields.size(), retrievedTest.dataDictionary.getDataFields().size());
    originalDataFields.forEach(dataField -> {
        Optional<DataField> optRet = retrievedDataFields.stream().filter(retrievedDataField -> dataField.getName().equals(retrievedDataField.getName())).findFirst();
        assertTrue(optRet.isPresent());
        assertEquals(dataField.getDataType(), optRet.get().getDataType());
    });
    assertEquals(pmml.getTransformationDictionary(), retrievedTest.transformationDictionary);
    assertEquals(scorecard, retrievedTest.model);
    String expectedPackageName = getSanitizedPackageName(PACKAGE_NAME);
    assertEquals(expectedPackageName, retrievedTest.getKModulePackageName());
    assertEquals(PACKAGE_NAME, retrievedTest.getName());
    PackageDescr packageDescr = knowledgeBuilder.getPackageDescrs("packagename").get(0);
    assertTrue(packageDescr instanceof CompositePackageDescr);
}
Also used : KiePMMLDroolsModel(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModel) BeforeClass(org.junit.BeforeClass) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) DroolsCompilationDTO(org.kie.pmml.models.drools.dto.DroolsCompilationDTO) Scorecard(org.dmg.pmml.scorecard.Scorecard) KiePMMLDroolsType(org.kie.pmml.models.drools.ast.KiePMMLDroolsType) HashMap(java.util.HashMap) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Field(org.dmg.pmml.Field) TransformationDictionary(org.dmg.pmml.TransformationDictionary) PMML_MODEL(org.kie.pmml.api.enums.PMML_MODEL) DerivedField(org.dmg.pmml.DerivedField) Map(java.util.Map) KiePMMLModelUtils.getSanitizedPackageName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedPackageName) PMML(org.dmg.pmml.PMML) PACKAGE_NAME(org.kie.pmml.commons.Constants.PACKAGE_NAME) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) DataDictionary(org.dmg.pmml.DataDictionary) Test(org.junit.Test) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) CommonTestingUtils.getFieldsFromDataDictionaryAndTransformationDictionaryAndLocalTransformations(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionaryAndTransformationDictionaryAndLocalTransformations) GeneratedFile(org.drools.modelcompiler.builder.GeneratedFile) HasKnowledgeBuilderMock(org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock) DataField(org.dmg.pmml.DataField) TestUtils(org.kie.pmml.compiler.api.testutils.TestUtils) List(java.util.List) CommonCompilationDTO(org.kie.pmml.compiler.api.dto.CommonCompilationDTO) Optional(java.util.Optional) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) KiePMMLDroolsModelWithSources(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) Collections(java.util.Collections) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) Assert.assertEquals(org.junit.Assert.assertEquals) KiePMMLModelUtils.getSanitizedClassName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedClassName) HasKnowledgeBuilderMock(org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock) KiePMMLDroolsModel(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModel) DataField(org.dmg.pmml.DataField) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Scorecard(org.dmg.pmml.scorecard.Scorecard) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) Test(org.junit.Test)

Aggregations

DataField (org.dmg.pmml.DataField)109 Test (org.junit.Test)51 DataDictionary (org.dmg.pmml.DataDictionary)42 MiningField (org.dmg.pmml.MiningField)42 MiningSchema (org.dmg.pmml.MiningSchema)30 PMMLModelTestUtils.getRandomDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField)28 RegressionModel (org.dmg.pmml.regression.RegressionModel)27 CommonTestingUtils.getFieldsFromDataDictionary (org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary)27 Model (org.dmg.pmml.Model)24 PMMLModelTestUtils.getDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField)22 OutputField (org.dmg.pmml.OutputField)19 PMMLModelTestUtils.getRandomMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField)19 List (java.util.List)18 DataType (org.dmg.pmml.DataType)18 PMMLModelTestUtils.getMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField)18 FieldName (org.dmg.pmml.FieldName)17 PMML (org.dmg.pmml.PMML)17 Collectors (java.util.stream.Collectors)16 ArrayList (java.util.ArrayList)15 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)15