Search in sources :

Example 46 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class HasKnowledgeBuilderMock method compileAndLoadClass.

@Override
public Class<?> compileAndLoadClass(Map<String, String> sourcesMap, String fullClassName) {
    ClassLoader classLoader = getClassLoader();
    if (!(classLoader instanceof ProjectClassLoader)) {
        throw new IllegalStateException("Expected ProjectClassLoader, received " + classLoader.getClass().getName());
    }
    ProjectClassLoader projectClassLoader = (ProjectClassLoader) classLoader;
    final Map<String, byte[]> byteCode = KieMemoryCompiler.compileNoLoad(sourcesMap, projectClassLoader);
    byteCode.forEach(projectClassLoader::defineClass);
    try {
        return projectClassLoader.loadClass(fullClassName);
    } catch (Exception e) {
        throw new KiePMMLException(e);
    }
}
Also used : ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 47 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class KiePMMLScorecardModelFactory method getKiePMMLScorecardModelSourcesMap.

public static Map<String, String> getKiePMMLScorecardModelSourcesMap(final DroolsCompilationDTO<Scorecard> compilationDTO) {
    logger.trace("getKiePMMLScorecardModelSourcesMap {} {} {}", compilationDTO.getFields(), compilationDTO.getModel(), compilationDTO.getPackageName());
    CompilationUnit cloneCU = getKiePMMLModelCompilationUnit(compilationDTO, KIE_PMML_SCORECARD_MODEL_TEMPLATE_JAVA, KIE_PMML_SCORECARD_MODEL_TEMPLATE);
    String className = compilationDTO.getSimpleClassName();
    ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
    setConstructor(compilationDTO, modelTemplate);
    Map<String, String> toReturn = new HashMap<>();
    String fullClassName = compilationDTO.getPackageCanonicalClassName();
    toReturn.put(fullClassName, cloneCU.toString());
    return toReturn;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) KiePMMLDroolsModelFactoryUtils.getKiePMMLModelCompilationUnit(org.kie.pmml.models.drools.utils.KiePMMLDroolsModelFactoryUtils.getKiePMMLModelCompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) HashMap(java.util.HashMap) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 48 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class KiePMMLTreeModelFactory method getKiePMMLTreeModelSourcesMap.

public static Map<String, String> getKiePMMLTreeModelSourcesMap(final TreeCompilationDTO compilationDTO) {
    logger.trace("getKiePMMLTreeModelSourcesMap {} {} {}", compilationDTO.getFields(), compilationDTO.getModel(), compilationDTO.getPackageName());
    String className = compilationDTO.getSimpleClassName();
    String packageName = compilationDTO.getPackageName();
    CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, packageName, KIE_PMML_TREE_MODEL_TEMPLATE_JAVA, KIE_PMML_TREE_MODEL_TEMPLATE);
    ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
    final Double missingValuePenalty = compilationDTO.getMissingValuePenalty();
    final KiePMMLNodeFactory.NodeNamesDTO nodeNamesDTO = new KiePMMLNodeFactory.NodeNamesDTO(compilationDTO.getNode(), createNodeClassName(), null, missingValuePenalty);
    String fullNodeClassName = packageName + "." + nodeNamesDTO.nodeClassName;
    Map<String, String> toReturn = getKiePMMLNodeSourcesMap(nodeNamesDTO, compilationDTO.getFields(), packageName);
    setConstructor(compilationDTO, modelTemplate, fullNodeClassName);
    String fullClassName = packageName + "." + className;
    toReturn.put(fullClassName, cloneCU.toString());
    return toReturn;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 49 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class KiePMMLUtil method populateMissingOutputFieldDataType.

/**
 * Method to populate the <b>dataType</b> property of <code>OutputField</code>s.
 * Such property was optional until 4.4.1 spec
 * @param toPopulate
 * @param miningFields
 * @param dataFields
 */
static void populateMissingOutputFieldDataType(List<OutputField> toPopulate, List<MiningField> miningFields, List<DataField> dataFields) {
    // partial implementation to fix missing "dataType" inside OutputField; "dataType" became mandatory only in 4.4.1 version
    List<MiningField> targetFields = getMiningTargetFields(miningFields);
    toPopulate.stream().filter(outputField -> outputField.getDataType() == null).forEach(outputField -> {
        MiningField referencedField = null;
        if (outputField.getTargetField() != null) {
            referencedField = targetFields.stream().filter(targetField -> outputField.getTargetField().equals(targetField.getName())).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find a target field for OutputField " + outputField.getName().getValue()));
        }
        if (referencedField == null && (outputField.getResultFeature() == null || outputField.getResultFeature().equals(ResultFeature.PREDICTED_VALUE))) {
            // default predictedValue
            referencedField = targetFields.stream().findFirst().orElse(// It is allowed to not have any "target" field inside MiningSchema
            null);
        }
        if (referencedField == null && ResultFeature.PROBABILITY.equals(outputField.getResultFeature())) {
            // we set the "dataType" to "double" because outputField is a "probability", we may return
            outputField.setDataType(DataType.DOUBLE);
            return;
        }
        if (referencedField != null) {
            FieldName targetFieldName = referencedField.getName();
            DataField dataField = dataFields.stream().filter(df -> df.getName().equals(targetFieldName)).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find a DataField field for " + "MiningField " + targetFieldName.toString()));
            outputField.setDataType(dataField.getDataType());
        }
    });
}
Also used : PMML(org.dmg.pmml.PMML) Model(org.dmg.pmml.Model) OutputField(org.dmg.pmml.OutputField) Targets(org.dmg.pmml.Targets) DataType(org.dmg.pmml.DataType) ResultFeature(org.dmg.pmml.ResultFeature) MiningSchema(org.dmg.pmml.MiningSchema) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) Target(org.dmg.pmml.Target) DataField(org.dmg.pmml.DataField) FieldName(org.dmg.pmml.FieldName) OpType(org.dmg.pmml.OpType) List(java.util.List) Segment(org.dmg.pmml.mining.Segment) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) MiningFunction(org.dmg.pmml.MiningFunction) MiningField(org.dmg.pmml.MiningField) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) MathContext(org.dmg.pmml.MathContext) InputStream(java.io.InputStream) MiningModel(org.dmg.pmml.mining.MiningModel) MiningField(org.dmg.pmml.MiningField) DataField(org.dmg.pmml.DataField) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) FieldName(org.dmg.pmml.FieldName)

Example 50 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class TestingModelImplementationProvider method getKiePMMLTestModelSourcesMap.

private Map<String, String> getKiePMMLTestModelSourcesMap(final CompilationDTO<TestModel> compilationDTO) {
    String className = compilationDTO.getSimpleClassName();
    CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, compilationDTO.getPackageName(), KIE_PMML_TEST_MODEL_TEMPLATE_JAVA, KIE_PMML_TEST_MODEL_TEMPLATE);
    ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
    String modelName = compilationDTO.getModelName();
    final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
    setConstructor(className, constructorDeclaration, modelName);
    Map<String, String> toReturn = new HashMap<>();
    toReturn.put(getFullClassName(cloneCU), cloneCU.toString());
    return toReturn;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) HashMap(java.util.HashMap) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException)

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