use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLTextIndexFactory method getTextIndexVariableDeclaration.
static BlockStmt getTextIndexVariableDeclaration(final String variableName, final TextIndex textIndex) {
final MethodDeclaration methodDeclaration = TEXTINDEX_TEMPLATE.getMethodsByName(GETKIEPMMLTEXTINDEX).get(0).clone();
final BlockStmt textIndexBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(textIndexBody, TEXTINDEX).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TEXTINDEX, textIndexBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
String expressionVariableName = String.format("%s_Expression", variableName);
final BlockStmt expressionBlockStatement = getKiePMMLExpressionBlockStmt(expressionVariableName, textIndex.getExpression());
expressionBlockStatement.getStatements().forEach(toReturn::addStatement);
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
if (textIndex.hasTextIndexNormalizations()) {
for (TextIndexNormalization textIndexNormalization : textIndex.getTextIndexNormalizations()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getTextIndexNormalizationVariableDeclaration(nestedVariableName, textIndexNormalization);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
}
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TEXTINDEX, toReturn))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final StringLiteralExpr nameExpr = new StringLiteralExpr(textIndex.getTextField().getValue());
final NameExpr expressionExpr = new NameExpr(expressionVariableName);
builder.setArgument(0, nameExpr);
builder.setArgument(2, expressionExpr);
Expression localTermWeightsExpression;
if (textIndex.getLocalTermWeights() != null) {
final LOCAL_TERM_WEIGHTS localTermWeights = LOCAL_TERM_WEIGHTS.byName(textIndex.getLocalTermWeights().value());
localTermWeightsExpression = new NameExpr(LOCAL_TERM_WEIGHTS.class.getName() + "." + localTermWeights.name());
} else {
localTermWeightsExpression = new NullLiteralExpr();
}
getChainedMethodCallExprFrom("withLocalTermWeights", initializer).setArgument(0, localTermWeightsExpression);
getChainedMethodCallExprFrom("withIsCaseSensitive", initializer).setArgument(0, getExpressionForObject(textIndex.isCaseSensitive()));
getChainedMethodCallExprFrom("withMaxLevenshteinDistance", initializer).setArgument(0, getExpressionForObject(textIndex.getMaxLevenshteinDistance()));
Expression countHitsExpression;
if (textIndex.getCountHits() != null) {
final COUNT_HITS countHits = COUNT_HITS.byName(textIndex.getCountHits().value());
countHitsExpression = new NameExpr(COUNT_HITS.class.getName() + "." + countHits.name());
} else {
countHitsExpression = new NullLiteralExpr();
}
getChainedMethodCallExprFrom("withCountHits", initializer).setArgument(0, countHitsExpression);
Expression wordSeparatorCharacterREExpression;
if (textIndex.getWordSeparatorCharacterRE() != null) {
String wordSeparatorCharacterRE = StringEscapeUtils.escapeJava(textIndex.getWordSeparatorCharacterRE());
wordSeparatorCharacterREExpression = new StringLiteralExpr(wordSeparatorCharacterRE);
} else {
wordSeparatorCharacterREExpression = new NullLiteralExpr();
}
getChainedMethodCallExprFrom("withWordSeparatorCharacterRE", initializer).setArgument(0, wordSeparatorCharacterREExpression);
getChainedMethodCallExprFrom("withTokenize", initializer).setArgument(0, getExpressionForObject(textIndex.isTokenize()));
getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
textIndexBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLSimpleSetPredicateFactory method getSimpleSetPredicateVariableDeclaration.
static BlockStmt getSimpleSetPredicateVariableDeclaration(final String variableName, final SimpleSetPredicate simpleSetPredicate) {
final MethodDeclaration methodDeclaration = SIMPLESET_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLSIMPLESETPREDICATE).get(0).clone();
final BlockStmt simpleSetPredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(simpleSetPredicateBody, SIMPLESET_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, SIMPLESET_PREDICATE, simpleSetPredicateBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
final NodeList<Expression> arguments = new NodeList<>();
List<Object> values = getObjectsFromArray(simpleSetPredicate.getArray());
for (Object value : values) {
arguments.add(getExpressionForObject(value));
}
final ARRAY_TYPE arrayType = ARRAY_TYPE.byName(simpleSetPredicate.getArray().getType().value());
final NameExpr arrayTypeExpr = new NameExpr(ARRAY_TYPE.class.getName() + "." + arrayType.name());
final IN_NOTIN inNotIn = IN_NOTIN.byName(simpleSetPredicate.getBooleanOperator().value());
final NameExpr inNotInExpr = new NameExpr(IN_NOTIN.class.getName() + "." + inNotIn.name());
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, SIMPLESET_PREDICATE, simpleSetPredicateBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
builder.setArgument(0, new StringLiteralExpr(simpleSetPredicate.getField().getValue()));
builder.setArgument(2, arrayTypeExpr);
builder.setArgument(3, inNotInExpr);
getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
simpleSetPredicateBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLTargetValueFactory method getKiePMMLTargetValueVariableInitializer.
static MethodCallExpr getKiePMMLTargetValueVariableInitializer(final TargetValue targetValueField) {
final MethodDeclaration methodDeclaration = TARGETVALUE_TEMPLATE.getMethodsByName(GETKIEPMMLTARGETVALUE).get(0).clone();
final BlockStmt targetValueBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(targetValueBody, TARGETVALUE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TARGETVALUE, targetValueBody)));
variableDeclarator.setName(targetValueField.getName());
final ObjectCreationExpr targetValueFieldInstance = TargetValueFactory.getTargetValueVariableInitializer(targetValueField);
final MethodCallExpr toReturn = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TARGETVALUE, targetValueBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", toReturn);
final StringLiteralExpr nameExpr = new StringLiteralExpr(targetValueField.getName());
builder.setArgument(0, nameExpr);
builder.setArgument(2, targetValueFieldInstance);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class TargetValueFactory method getTargetValueVariableInitializer.
static ObjectCreationExpr getTargetValueVariableInitializer(final TargetValue targetValueField) {
final MethodDeclaration methodDeclaration = TARGETVALUE_TEMPLATE.getMethodsByName(GETTARGETVALUE).get(0).clone();
final BlockStmt targetValueBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(targetValueBody, TARGETVALUE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TARGETVALUE, targetValueBody)));
final ObjectCreationExpr toReturn = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TARGETVALUE, targetValueBody))).asObjectCreationExpr();
toReturn.setArgument(0, getExpressionForObject(targetValueField.getValue()));
toReturn.setArgument(1, getExpressionForObject(targetValueField.getDisplayValue()));
toReturn.setArgument(2, getExpressionForObject(targetValueField.getPriorProbability()));
toReturn.setArgument(3, getExpressionForObject(targetValueField.getDefaultValue()));
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class PMMLCompilerService method addPMMLRuleMapper.
static void addPMMLRuleMapper(final KiePMMLModel kiePMMLModel, final List<String> generatedRuleMappers, final String sourcePath) {
if (!(kiePMMLModel instanceof HasSourcesMap)) {
String errorMessage = String.format("Expecting HasSourcesMap instance, retrieved %s inside %s", kiePMMLModel.getClass().getName(), sourcePath);
throw new KiePMMLException(errorMessage);
}
if (kiePMMLModel instanceof HasRule) {
String pkgUUID = ((HasRule) kiePMMLModel).getPkgUUID();
String rulesFileName = kiePMMLModel.getKModulePackageName() + "." + RULES_FILE_NAME + pkgUUID;
String pmmlRuleMapper = kiePMMLModel.getKModulePackageName() + "." + KIE_PMML_RULE_MAPPER_CLASS_NAME;
String ruleMapperSource = PMMLRuleMapperFactory.getPMMLRuleMapperSource(rulesFileName);
((HasRule) kiePMMLModel).addSourceMap(pmmlRuleMapper, ruleMapperSource);
generatedRuleMappers.add(pmmlRuleMapper);
}
if (kiePMMLModel instanceof HasNestedModels) {
for (KiePMMLModel nestedModel : ((HasNestedModels) kiePMMLModel).getNestedModels()) {
addPMMLRuleMapper(nestedModel, generatedRuleMappers, sourcePath);
}
}
}
Aggregations