use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLClusteringModelFactory method getKiePMMLClusteringModel.
public static KiePMMLClusteringModel getKiePMMLClusteringModel(final CompilationDTO<ClusteringModel> compilationDTO) {
logger.trace("getKiePMMLClusteringModel {}", compilationDTO);
Map<String, String> sourcesMap = getKiePMMLClusteringModelSourcesMap(compilationDTO);
try {
Class<?> clusteringModelClass = compilationDTO.compileAndLoadClass(sourcesMap);
return (KiePMMLClusteringModel) clusteringModelClass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new KiePMMLException(e);
}
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLClusteringModelFactory method getKiePMMLClusteringModelSourcesMap.
public static Map<String, String> getKiePMMLClusteringModelSourcesMap(final CompilationDTO<ClusteringModel> compilationDTO) {
logger.trace("getKiePMMLClusteringModelSourcesMap {}", compilationDTO);
String simpleClassName = compilationDTO.getSimpleClassName();
CompilationUnit compilationUnit = JavaParserUtils.getKiePMMLModelCompilationUnit(simpleClassName, compilationDTO.getPackageName(), KIE_PMML_CLUSTERING_MODEL_TEMPLATE_JAVA, KIE_PMML_CLUSTERING_MODEL_TEMPLATE);
ClassOrInterfaceDeclaration modelTemplate = compilationUnit.getClassByName(simpleClassName).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + simpleClassName));
setConstructor(compilationDTO, modelTemplate);
Map<String, String> sourcesMap = new HashMap<>();
sourcesMap.put(getFullClassName(compilationUnit), compilationUnit.toString());
return sourcesMap;
}
use of org.kie.pmml.api.exceptions.KiePMMLException 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);
}
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class PMMLRuntimeInternalImplTest method addPMMLListener.
@Test
public void addPMMLListener() {
try {
pmmlRuntime.evaluate(MODEL_NAME, pmmlContextMock);
verify(pmmlContextMock, never()).addPMMLListener(any());
} catch (KiePMMLException e) {
commonManageException(e);
}
try {
reset(pmmlContextMock);
PMMLListener listener = getPMMLListener(new ArrayList<>());
pmmlRuntime.addPMMLListener(listener);
pmmlRuntime.evaluate(MODEL_NAME, pmmlContextMock);
verify(pmmlContextMock).addPMMLListener(listener);
} catch (KiePMMLException e) {
commonManageException(e);
}
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLCompoundPredicateASTFactory method getBuilderForCompoundPredicateAndOrXor.
/**
* Method to be invoked when <b>compoundPredicate.getBooleanOperator()</b> is <code>AND</code>, <code>OR</code> or
* <XOR>XOR</XOR>. Throws exception otherwise
* @param statusToSet
*/
private KiePMMLDroolsRule.Builder getBuilderForCompoundPredicateAndOrXor(final String statusToSet) {
logger.trace("getBuilderForCompoundPredicateAndOrXor {}", statusToSet);
CompoundPredicate compoundPredicate = (CompoundPredicate) predicateASTFactoryData.getPredicate();
if (!CompoundPredicate.BooleanOperator.AND.equals(compoundPredicate.getBooleanOperator()) && !CompoundPredicate.BooleanOperator.OR.equals((compoundPredicate.getBooleanOperator())) && !CompoundPredicate.BooleanOperator.XOR.equals((compoundPredicate.getBooleanOperator()))) {
throw new KiePMMLException(String.format("getBuilderForCompoundPredicateAndOrXor invoked with %s CompoundPredicate", compoundPredicate.getBooleanOperator()));
}
String statusConstraint = StringUtils.isEmpty(predicateASTFactoryData.getParentPath()) ? KiePMMLAbstractModelASTFactory.STATUS_NULL : String.format(STATUS_PATTERN, predicateASTFactoryData.getParentPath());
List<KiePMMLFieldOperatorValue> constraints;
KiePMMLDroolsRule.Builder toReturn = KiePMMLDroolsRule.builder(predicateASTFactoryData.getCurrentRule(), statusToSet, predicateASTFactoryData.getOutputFields()).withStatusConstraint(statusConstraint);
switch(compoundPredicate.getBooleanOperator()) {
case AND:
constraints = getConstraintEntriesFromAndOrCompoundPredicate(compoundPredicate, predicateASTFactoryData.getFieldTypeMap());
toReturn = toReturn.withAndConstraints(constraints);
break;
case OR:
constraints = getConstraintEntriesFromAndOrCompoundPredicate(compoundPredicate, predicateASTFactoryData.getFieldTypeMap());
toReturn = toReturn.withOrConstraints(constraints);
break;
case XOR:
constraints = getConstraintEntriesFromXOrCompoundPredicate(compoundPredicate, predicateASTFactoryData.getFieldTypeMap());
toReturn = toReturn.withXorConstraints(constraints);
break;
default:
throw new IllegalStateException(String.format("CompoundPredicate.booleanOperator should never be %s at this point", compoundPredicate.getBooleanOperator()));
}
return toReturn;
}
Aggregations