Search in sources :

Example 76 with KiePMMLException

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);
    }
}
Also used : KiePMMLClusteringModel(org.kie.pmml.models.clustering.model.KiePMMLClusteringModel) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 77 with KiePMMLException

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;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) HashMap(java.util.HashMap) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 78 with KiePMMLException

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);
    }
}
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 79 with KiePMMLException

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);
    }
}
Also used : KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) PMMLListener(org.kie.pmml.api.runtime.PMMLListener) Test(org.junit.Test)

Example 80 with KiePMMLException

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;
}
Also used : KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) CompoundPredicate(org.dmg.pmml.CompoundPredicate) KiePMMLASTFactoryUtils.getConstraintEntriesFromAndOrCompoundPredicate(org.kie.pmml.models.drools.utils.KiePMMLASTFactoryUtils.getConstraintEntriesFromAndOrCompoundPredicate) KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate(org.kie.pmml.models.drools.utils.KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)

Aggregations

KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)109 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)49 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)40 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)38 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)37 Expression (com.github.javaparser.ast.expr.Expression)33 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)33 NameExpr (com.github.javaparser.ast.expr.NameExpr)32 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)26 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)20 KiePMMLInternalException (org.kie.pmml.api.exceptions.KiePMMLInternalException)18 CompilationUnit (com.github.javaparser.ast.CompilationUnit)17 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)17 NodeList (com.github.javaparser.ast.NodeList)13 HashMap (java.util.HashMap)12 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)10 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)9 ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)9 Test (org.junit.Test)7 IOException (java.io.IOException)6