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"));
}
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
}
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;
}
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;
}
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);
}
Aggregations