use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class ModelImplementationProvider method getKiePMMLModelWithSourcesCompiled.
/**
* Method provided only to have <b>drools</b> models working when invoked by a <code>MiningModel</code>
* Default implementation provided for <b>not-drools</b> models.
* @param compilationDTO
* @return
* @throws KiePMMLInternalException
*/
default KiePMMLModelWithSources getKiePMMLModelWithSourcesCompiled(final CompilationDTO<T> compilationDTO) {
KiePMMLModelWithSources toReturn = getKiePMMLModelWithSources(compilationDTO);
final Map<String, String> sourcesMap = ((HasSourcesMap) toReturn).getSourcesMap();
try {
compilationDTO.compileAndLoadClass(sourcesMap);
} catch (Exception e) {
throw new KiePMMLException(e);
}
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class InstanceFactoriesTestCommon method commonVerifyKiePMMLSimpleSetPredicate.
static void commonVerifyKiePMMLSimpleSetPredicate(KiePMMLSimpleSetPredicate toVerify, SimpleSetPredicate source) {
assertNotNull(toVerify);
assertEquals(source.getField().getValue(), toVerify.getName());
Array array = source.getArray();
assertEquals(array.getType().value(), toVerify.getArrayType().getName());
assertEquals(source.getBooleanOperator().value(), toVerify.getInNotIn().getName());
assertEquals(array.getN().intValue(), toVerify.getValues().size());
String stringValue = (String) array.getValue();
String[] valuesArray = stringValue.split(" ");
IntStream.range(0, array.getN()).forEach(i -> {
switch(array.getType()) {
case INT:
assertEquals(Integer.valueOf(valuesArray[i]), toVerify.getValues().get(i));
break;
case STRING:
assertEquals(valuesArray[i], toVerify.getValues().get(i));
break;
case REAL:
assertEquals(Double.valueOf(valuesArray[i]), toVerify.getValues().get(i));
break;
default:
throw new KiePMMLException("Unknown Array " + array.getType());
}
});
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class PMMLRuntimeInternalImpl method evaluate.
@SuppressWarnings({ "unchecked", "rawtypes" })
protected PMML4Result evaluate(final KiePMMLModel model, final PMMLContext context) {
if (logger.isDebugEnabled()) {
logger.debug("evaluate {} {}", model, context);
}
pmmlListeners.forEach(context::addPMMLListener);
addStep(() -> getStep(START, model, context.getRequestData()), context);
final ProcessingDTO processingDTO = preProcess(model, context);
addStep(() -> getStep(PRE_EVALUATION, model, context.getRequestData()), context);
PMMLModelEvaluator executor = getFromPMMLModelType(model.getPmmlMODEL()).orElseThrow(() -> new KiePMMLException(String.format("PMMLModelEvaluator not found for model %s", model.getPmmlMODEL())));
PMML4Result toReturn = executor.evaluate(knowledgeBase, model, context);
addStep(() -> getStep(POST_EVALUATION, model, context.getRequestData()), context);
postProcess(toReturn, model, context, processingDTO);
addStep(() -> getStep(END, model, context.getRequestData()), context);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class DroolsModelProvider method getKiePMMLModelWithSourcesCompiled.
@Override
public KiePMMLModelWithSources getKiePMMLModelWithSourcesCompiled(final CompilationDTO<T> compilationDTO) {
logger.trace("getKiePMMLModelWithSourcesCompiled {} {} {}", compilationDTO.getPackageName(), compilationDTO.getFields(), compilationDTO.getModel());
if (!(compilationDTO.getHasClassloader() instanceof HasKnowledgeBuilder)) {
throw new KiePMMLException(String.format(EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE, compilationDTO.getHasClassloader().getClass().getName()));
}
try {
HasKnowledgeBuilder hasKnowledgeBuilder = (HasKnowledgeBuilder) compilationDTO.getHasClassloader();
KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) hasKnowledgeBuilder.getKnowledgeBuilder();
final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
KiePMMLDroolsAST kiePMMLDroolsAST = getKiePMMLDroolsASTCommon(compilationDTO.getFields(), compilationDTO.getModel(), fieldTypeMap);
final DroolsCompilationDTO<T> droolsCompilationDTO = DroolsCompilationDTO.fromCompilationDTO(compilationDTO, fieldTypeMap);
Map<String, String> sourcesMap = getKiePMMLDroolsModelSourcesMap(droolsCompilationDTO);
compilationDTO.compileAndLoadClass(sourcesMap);
PackageDescr packageDescr = getPackageDescr(kiePMMLDroolsAST, compilationDTO.getPackageName());
String pkgUUID = getPkgUUID(knowledgeBuilder.getReleaseId(), compilationDTO.getPackageName());
packageDescr.setPreferredPkgUUID(pkgUUID);
Map<String, String> rulesSourceMap = Collections.unmodifiableMap(getRulesSourceMap(packageDescr));
KiePMMLDroolsModelWithSources toReturn = new KiePMMLDroolsModelWithSources(compilationDTO.getModelName(), compilationDTO.getPackageName(), compilationDTO.getKieMiningFields(), compilationDTO.getKieOutputFields(), compilationDTO.getKieTargetFields(), sourcesMap, pkgUUID, rulesSourceMap);
// Needed to compile Rules from PackageDescr
CompositePackageDescr compositePackageDescr = new CompositePackageDescr(null, packageDescr);
knowledgeBuilder.buildPackages(Collections.singletonList(compositePackageDescr));
return toReturn;
} catch (Exception e) {
throw new KiePMMLException(e);
}
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class DroolsModelProvider method getKiePMMLModel.
@Override
public E getKiePMMLModel(final CompilationDTO<T> compilationDTO) {
logger.trace("getKiePMMLModel {} {} {}", compilationDTO.getPackageName(), compilationDTO.getFields(), compilationDTO.getModel());
if (!(compilationDTO.getHasClassloader() instanceof HasKnowledgeBuilder)) {
throw new KiePMMLException(String.format(EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE, compilationDTO.getHasClassloader().getClass().getName()));
}
HasKnowledgeBuilder hasKnowledgeBuilder = (HasKnowledgeBuilder) compilationDTO.getHasClassloader();
KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) hasKnowledgeBuilder.getKnowledgeBuilder();
final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
KiePMMLDroolsAST kiePMMLDroolsAST = getKiePMMLDroolsASTCommon(compilationDTO.getFields(), compilationDTO.getModel(), fieldTypeMap);
final DroolsCompilationDTO<T> droolsCompilationDTO = DroolsCompilationDTO.fromCompilationDTO(compilationDTO, fieldTypeMap);
E toReturn = getKiePMMLDroolsModel(droolsCompilationDTO);
PackageDescr packageDescr = getPackageDescr(kiePMMLDroolsAST, toReturn.getKModulePackageName());
// Needed to compile Rules from PackageDescr
CompositePackageDescr compositePackageDescr = new CompositePackageDescr(null, packageDescr);
knowledgeBuilder.buildPackages(Collections.singletonList(compositePackageDescr));
return toReturn;
}
Aggregations