Search in sources :

Example 36 with KiePMMLException

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

Example 37 with KiePMMLException

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());
        }
    });
}
Also used : Array(org.dmg.pmml.Array) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 38 with KiePMMLException

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;
}
Also used : ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) PMML4Result(org.kie.api.pmml.PMML4Result) PMMLModelEvaluator(org.kie.pmml.evaluator.core.executor.PMMLModelEvaluator) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 39 with KiePMMLException

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);
    }
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) HashMap(java.util.HashMap) IOException(java.io.IOException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) HasKnowledgeBuilder(org.kie.pmml.kie.dependencies.HasKnowledgeBuilder) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KiePMMLDroolsModelWithSources(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources)

Example 40 with KiePMMLException

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;
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) HashMap(java.util.HashMap) EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE(org.kie.pmml.commons.Constants.EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) HasKnowledgeBuilder(org.kie.pmml.kie.dependencies.HasKnowledgeBuilder) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

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