Search in sources :

Example 26 with KiePMMLModel

use of org.kie.pmml.commons.model.KiePMMLModel in project drools by kiegroup.

the class PMMLCompilerService method getKiePMMLModelsFromResourceWithSources.

/**
 * @param kbuilderImpl
 * @param resource
 * @return
 */
public static List<KiePMMLModel> getKiePMMLModelsFromResourceWithSources(KnowledgeBuilderImpl kbuilderImpl, Resource resource) {
    PMMLCompiler pmmlCompiler = kbuilderImpl.getCachedOrCreate(PMML_COMPILER_CACHE_KEY, PMMLCompilerService::getCompiler);
    String[] classNamePackageName = getFactoryClassNamePackageName(resource);
    String factoryClassName = classNamePackageName[0];
    String packageName = classNamePackageName[1];
    try {
        final List<KiePMMLModel> toReturn = pmmlCompiler.getKiePMMLModelsWithSources(factoryClassName, packageName, resource.getInputStream(), getFileName(resource.getSourcePath()), new HasKnowledgeBuilderImpl(kbuilderImpl));
        populateWithPMMLRuleMappers(toReturn, resource);
        return toReturn;
    } catch (IOException e) {
        throw new ExternalException("ExternalException", e);
    }
}
Also used : KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) HasKnowledgeBuilderImpl(org.kie.pmml.evaluator.assembler.implementations.HasKnowledgeBuilderImpl) PMMLCompiler(org.kie.pmml.compiler.executor.PMMLCompiler) IOException(java.io.IOException) ExternalException(org.kie.pmml.api.exceptions.ExternalException)

Example 27 with KiePMMLModel

use of org.kie.pmml.commons.model.KiePMMLModel in project drools by kiegroup.

the class PostProcess method updateTargetValueType.

/**
 * Verify that the returned value has the required type as defined inside <code>DataDictionary/MiningSchema</code>
 * @param model
 * @param toUpdate
 */
static void updateTargetValueType(final KiePMMLModel model, final PMML4Result toUpdate) {
    DATA_TYPE dataType = model.getMiningFields().stream().filter(miningField -> model.getTargetField().equals(miningField.getName())).map(MiningField::getDataType).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find DATA_TYPE for " + model.getTargetField()));
    Object prediction = toUpdate.getResultVariables().get(model.getTargetField());
    if (prediction != null) {
        Object convertedPrediction = dataType.getActualValue(prediction);
        toUpdate.getResultVariables().put(model.getTargetField(), convertedPrediction);
    }
}
Also used : OK(org.kie.pmml.api.enums.ResultCode.OK) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) PMML4Result(org.kie.api.pmml.PMML4Result) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) ArrayList(java.util.ArrayList) MiningField(org.kie.pmml.api.models.MiningField) LinkedHashMap(java.util.LinkedHashMap) PMMLContext(org.kie.pmml.api.runtime.PMMLContext) AbstractMap(java.util.AbstractMap) List(java.util.List) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) Map(java.util.Map) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) MiningField(org.kie.pmml.api.models.MiningField) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE)

Example 28 with KiePMMLModel

use of org.kie.pmml.commons.model.KiePMMLModel in project drools by kiegroup.

the class PMMLRuntimeInternalImplTest method getKiePMMLModelMock.

private KiePMMLModel getKiePMMLModelMock() {
    KiePMMLModel toReturn = mock(KiePMMLModel.class);
    String targetFieldName = "targetFieldName";
    MiningField miningFieldMock = mock(MiningField.class);
    when(miningFieldMock.getName()).thenReturn(targetFieldName);
    when(miningFieldMock.getDataType()).thenReturn(DATA_TYPE.FLOAT);
    when(toReturn.getName()).thenReturn(MODEL_NAME);
    when(toReturn.getMiningFields()).thenReturn(Collections.singletonList(miningFieldMock));
    when(toReturn.getTargetField()).thenReturn(targetFieldName);
    when(toReturn.getPmmlMODEL()).thenReturn(PMML_MODEL.TEST_MODEL);
    return toReturn;
}
Also used : MiningField(org.kie.pmml.api.models.MiningField) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel)

Example 29 with KiePMMLModel

use of org.kie.pmml.commons.model.KiePMMLModel in project drools by kiegroup.

the class KiePMMLSegmentFactoryTest method getSegmentSourcesMapCompiled.

@Test
public void getSegmentSourcesMapCompiled() throws Exception {
    final Segment segment = MINING_MODEL.getSegmentation().getSegments().get(0);
    final List<KiePMMLModel> nestedModels = new ArrayList<>();
    final String expectedNestedModelGeneratedClass = getExpectedNestedModelClass(segment);
    final HasKnowledgeBuilderMock hasKnowledgeBuilderMock = new HasKnowledgeBuilderMock(KNOWLEDGE_BUILDER);
    try {
        hasKnowledgeBuilderMock.getClassLoader().loadClass(expectedNestedModelGeneratedClass);
        fail("Expecting class not found: " + expectedNestedModelGeneratedClass);
    } catch (Exception e) {
        assertTrue(e instanceof ClassNotFoundException);
    }
    final CommonCompilationDTO<MiningModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, MINING_MODEL, hasKnowledgeBuilderMock);
    final MiningModelCompilationDTO compilationDTO = MiningModelCompilationDTO.fromCompilationDTO(source);
    final SegmentCompilationDTO segmentCompilationDTO = SegmentCompilationDTO.fromGeneratedPackageNameAndFields(compilationDTO, segment, compilationDTO.getFields());
    final Map<String, String> retrieved = KiePMMLSegmentFactory.getSegmentSourcesMapCompiled(segmentCompilationDTO, nestedModels);
    commonEvaluateNestedModels(nestedModels);
    commonEvaluateMap(retrieved, segment);
    hasKnowledgeBuilderMock.getClassLoader().loadClass(expectedNestedModelGeneratedClass);
}
Also used : HasKnowledgeBuilderMock(org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock) ArrayList(java.util.ArrayList) SegmentCompilationDTO(org.kie.pmml.models.mining.compiler.dto.SegmentCompilationDTO) Segment(org.dmg.pmml.mining.Segment) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) MiningModel(org.dmg.pmml.mining.MiningModel) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) MiningModelCompilationDTO(org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO) Test(org.junit.Test)

Example 30 with KiePMMLModel

use of org.kie.pmml.commons.model.KiePMMLModel in project drools by kiegroup.

the class KiePMMLSegmentFactoryTest method getSegmentSourcesMap.

@Test
public void getSegmentSourcesMap() {
    final Segment segment = MINING_MODEL.getSegmentation().getSegments().get(0);
    final List<KiePMMLModel> nestedModels = new ArrayList<>();
    final CommonCompilationDTO<MiningModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, MINING_MODEL, new HasKnowledgeBuilderMock(KNOWLEDGE_BUILDER));
    final MiningModelCompilationDTO compilationDTO = MiningModelCompilationDTO.fromCompilationDTO(source);
    final SegmentCompilationDTO segmentCompilationDTO = SegmentCompilationDTO.fromGeneratedPackageNameAndFields(compilationDTO, segment, compilationDTO.getFields());
    final Map<String, String> retrieved = KiePMMLSegmentFactory.getSegmentSourcesMap(segmentCompilationDTO, nestedModels);
    commonEvaluateNestedModels(nestedModels);
    commonEvaluateMap(retrieved, segment);
}
Also used : HasKnowledgeBuilderMock(org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock) MiningModel(org.dmg.pmml.mining.MiningModel) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) ArrayList(java.util.ArrayList) SegmentCompilationDTO(org.kie.pmml.models.mining.compiler.dto.SegmentCompilationDTO) MiningModelCompilationDTO(org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO) Segment(org.dmg.pmml.mining.Segment) Test(org.junit.Test)

Aggregations

KiePMMLModel (org.kie.pmml.commons.model.KiePMMLModel)37 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)17 MiningModel (org.dmg.pmml.mining.MiningModel)9 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)9 KiePMMLTestingModel (org.kie.pmml.commons.testingutility.KiePMMLTestingModel)8 CommonCompilationDTO (org.kie.pmml.compiler.api.dto.CommonCompilationDTO)7 HasClassLoaderMock (org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock)7 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 HasKnowledgeBuilderMock (org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock)6 MiningModelCompilationDTO (org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO)6 HasSourcesMap (org.kie.pmml.commons.model.HasSourcesMap)5 File (java.io.File)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 MINING_FUNCTION (org.kie.pmml.api.enums.MINING_FUNCTION)4