Search in sources :

Example 1 with KiePMMLException

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

the class AbstractRegressionBenchmark method setupModel.

protected void setupModel() throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(KieServices.get().getResources().newClassPathResource(fileName).setResourceType(ResourceType.PMML));
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    final ReleaseId relId = kieBuilder.getKieModule().getReleaseId();
    Results res = kieBuilder.getResults();
    KieBase kbase = ks.newKieContainer(relId).getKieBase();
    KieSession session = kbase.newKieSession();
    pmmlRuntime = session.getKieRuntime(PMMLRuntimeInternal.class);
    model = pmmlRuntime.getKiePMMLModel(modelName).orElseThrow(() -> new KiePMMLException("Failed to retrieve the model"));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KieBase(org.kie.api.KieBase) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) PMMLRuntimeInternal(org.kie.pmml.evaluator.api.executor.PMMLRuntimeInternal) KieBuilder(org.kie.api.builder.KieBuilder)

Example 2 with KiePMMLException

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

the class KiePMMLTextIndex method evaluateAugmentedNormalizedTermFrequency.

static double evaluateAugmentedNormalizedTermFrequency(int calculatedLevenshteinDistance, List<String> texts) {
    Map<String, Long> wordFrequencies = texts.stream().collect(Collectors.groupingBy(Function.identity(), counting()));
    int maxFrequency = wordFrequencies.values().stream().max(Comparator.comparingLong(f -> f)).map(Long::intValue).orElseThrow(() -> new KiePMMLException("Failed to find most frequent word!"));
    int binaryEvaluation = evaluateBinary(calculatedLevenshteinDistance);
    // cast for
    return 0.5 * (binaryEvaluation + (calculatedLevenshteinDistance / (double) maxFrequency));
// java:S2184
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Collectors.counting(java.util.stream.Collectors.counting) LevenshteinDistance(org.apache.commons.text.similarity.LevenshteinDistance) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ExpressionsUtils.getFromPossibleSources(org.kie.pmml.commons.model.expressions.ExpressionsUtils.getFromPossibleSources) List(java.util.List) TreeMap(java.util.TreeMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LOCAL_TERM_WEIGHTS(org.kie.pmml.api.enums.LOCAL_TERM_WEIGHTS) Map(java.util.Map) StringJoiner(java.util.StringJoiner) KiePMMLExtension(org.kie.pmml.commons.model.KiePMMLExtension) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) COUNT_HITS(org.kie.pmml.api.enums.COUNT_HITS) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) AbstractKiePMMLComponent(org.kie.pmml.commons.model.abstracts.AbstractKiePMMLComponent) Collections(java.util.Collections) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) SortedMap(java.util.SortedMap) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 3 with KiePMMLException

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

the class KiePMMLDefineFunctionFactory method getDefineFunctionVariableDeclaration.

static BlockStmt getDefineFunctionVariableDeclaration(final DefineFunction defineFunction) {
    final MethodDeclaration methodDeclaration = DEFINE_FUNCTION_TEMPLATE.getMethodsByName(GETKIEPMMLDEFINEFUNCTION).get(0).clone();
    final BlockStmt defineFunctionBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(defineFunctionBody, DEFINE_FUNCTION).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, DEFINE_FUNCTION, defineFunctionBody)));
    variableDeclarator.setName(defineFunction.getName());
    final BlockStmt toReturn = new BlockStmt();
    int counter = 0;
    final NodeList<Expression> parameterFieldArguments = new NodeList<>();
    for (ParameterField parameterField : defineFunction.getParameterFields()) {
        String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, defineFunction.getName(), counter);
        parameterFieldArguments.add(new NameExpr(nestedVariableName));
        BlockStmt toAdd = getParameterFieldVariableDeclaration(nestedVariableName, parameterField);
        toAdd.getStatements().forEach(toReturn::addStatement);
        counter++;
    }
    String kiePMMLExpression = String.format("%s_Expression", defineFunction.getName());
    BlockStmt toAdd = getKiePMMLExpressionBlockStmt(kiePMMLExpression, defineFunction.getExpression());
    toAdd.getStatements().forEach(toReturn::addStatement);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, DEFINE_FUNCTION, toReturn))).asObjectCreationExpr();
    final StringLiteralExpr nameExpr = new StringLiteralExpr(defineFunction.getName());
    objectCreationExpr.getArguments().set(0, nameExpr);
    final Expression dataTypeExpression = getExpressionForDataType(defineFunction.getDataType());
    final Expression opTypeExpression = getExpressionForOpType(defineFunction.getOpType());
    objectCreationExpr.getArguments().set(2, dataTypeExpression);
    objectCreationExpr.getArguments().set(3, opTypeExpression);
    objectCreationExpr.getArguments().get(4).asMethodCallExpr().setArguments(parameterFieldArguments);
    objectCreationExpr.getArguments().set(5, new NameExpr(kiePMMLExpression));
    defineFunctionBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ParameterField(org.dmg.pmml.ParameterField)

Example 4 with KiePMMLException

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

the class KiePMMLDiscretizeBinFactory method getDiscretizeBinVariableDeclaration.

static BlockStmt getDiscretizeBinVariableDeclaration(final String variableName, final DiscretizeBin discretizeBin) {
    final MethodDeclaration methodDeclaration = DISCRETIZE_BIN_TEMPLATE.getMethodsByName(GETKIEPMMLDISCRETIZE_BIN).get(0).clone();
    final BlockStmt discretizeBinBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(discretizeBinBody, DISCRETIZE_BIN).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, DISCRETIZE_BIN, discretizeBinBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    String nestedVariableName = String.format("%s_Interval", variableName);
    BlockStmt toAdd = getIntervalVariableDeclaration(nestedVariableName, discretizeBin.getInterval());
    toAdd.getStatements().forEach(toReturn::addStatement);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, DISCRETIZE_BIN, discretizeBinBody))).asObjectCreationExpr();
    final Expression nameExpr = new StringLiteralExpr(variableName);
    final Expression binValueExpr = getExpressionForObject(discretizeBin.getBinValue());
    final NameExpr intervalExpr = new NameExpr(nestedVariableName);
    objectCreationExpr.getArguments().set(0, nameExpr);
    objectCreationExpr.getArguments().set(2, binValueExpr);
    objectCreationExpr.getArguments().set(3, intervalExpr);
    discretizeBinBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 5 with KiePMMLException

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

the class CommonCodegenUtils method setVariableDeclaratorValue.

/**
 * Set the value of the variable with the given <b>variableDeclaratorName</b> in the given <code>BlockStmt</code>
 * It throws <code>KiePMMLException</code> if variable is not found
 * @param body
 * @param variableDeclaratorName
 * @param value
 * @throws <code>KiePMMLException</code> if <code>VariableDeclarator</code> with given <b>variableDeclaratorName</b> is not
 * found
 */
public static void setVariableDeclaratorValue(final BlockStmt body, final String variableDeclaratorName, final Expression value) {
    VariableDeclarator variableDeclarator = getVariableDeclarator(body, variableDeclaratorName).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, variableDeclaratorName, body)));
    variableDeclarator.setInitializer(value);
}
Also used : KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

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