use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLModelFactoryUtils method getObjectCreationExprFromInterval.
static ObjectCreationExpr getObjectCreationExprFromInterval(Interval source) {
ObjectCreationExpr toReturn = new ObjectCreationExpr();
toReturn.setType(Interval.class.getCanonicalName());
NodeList<Expression> arguments = new NodeList<>();
if (source.getLeftMargin() != null) {
arguments.add(new NameExpr(source.getLeftMargin().toString()));
} else {
arguments.add(new NullLiteralExpr());
}
if (source.getRightMargin() != null) {
arguments.add(new NameExpr(source.getRightMargin().toString()));
} else {
arguments.add(new NullLiteralExpr());
}
toReturn.setArguments(arguments);
return toReturn;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLMapValuesFactory method getMapValuesVariableDeclaration.
static BlockStmt getMapValuesVariableDeclaration(final String variableName, final MapValues mapValues) {
if (mapValues.getInlineTable() == null && mapValues.getTableLocator() != null) {
throw new UnsupportedOperationException("TableLocator not supported, yet");
}
final MethodDeclaration methodDeclaration = MAPVALUES_TEMPLATE.getMethodsByName(GETKIEPMMLMAPVALUES).get(0).clone();
final BlockStmt mapValuesBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(mapValuesBody, MAPVALUES).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, MAPVALUES, mapValuesBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
if (mapValues.hasFieldColumnPairs()) {
for (FieldColumnPair fieldColumnPair : mapValues.getFieldColumnPairs()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getFieldColumnPairVariableDeclaration(nestedVariableName, fieldColumnPair);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
}
String inlineTableVariableName = String.format("%s_InlineTable", variableName);
BlockStmt toAdd = getInlineTableVariableDeclaration(inlineTableVariableName, mapValues.getInlineTable());
toAdd.getStatements().forEach(toReturn::addStatement);
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, MAPVALUES, toReturn))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final StringLiteralExpr nameExpr = new StringLiteralExpr(variableName);
final StringLiteralExpr outputColumnExpr = new StringLiteralExpr(mapValues.getOutputColumn());
builder.setArgument(0, nameExpr);
builder.setArgument(2, outputColumnExpr);
final Expression dataTypeExpression = getExpressionForDataType(mapValues.getDataType());
getChainedMethodCallExprFrom("withDefaultValue", initializer).setArgument(0, getExpressionForObject(mapValues.getDefaultValue()));
getChainedMethodCallExprFrom("withMapMissingTo", initializer).setArgument(0, getExpressionForObject(mapValues.getMapMissingTo()));
getChainedMethodCallExprFrom("withDataType", initializer).setArgument(0, dataTypeExpression);
getChainedMethodCallExprFrom("withKiePMMLInlineTable", initializer).setArgument(0, new NameExpr(inlineTableVariableName));
getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
mapValuesBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method setKiePMMLModelConstructor.
@Test
public void setKiePMMLModelConstructor() {
String generatedClassName = "generatedClassName";
String name = "newName";
List<MiningField> miningFields = IntStream.range(0, 3).mapToObj(i -> ModelUtils.convertToKieMiningField(getRandomMiningField(), getRandomDataField())).collect(Collectors.toList());
List<OutputField> outputFields = IntStream.range(0, 2).mapToObj(i -> ModelUtils.convertToKieOutputField(getRandomOutputField(), getRandomDataField())).collect(Collectors.toList());
List<TargetField> targetFields = IntStream.range(0, 2).mapToObj(i -> ModelUtils.convertToKieTargetField(getRandomTarget())).collect(Collectors.toList());
KiePMMLModelFactoryUtils.setKiePMMLModelConstructor(generatedClassName, constructorDeclaration, name, miningFields, outputFields, targetFields);
commonVerifySuperInvocation(generatedClassName, name);
List<MethodCallExpr> retrieved = getMethodCallExprList(constructorDeclaration.getBody(), miningFields.size(), "miningFields", "add");
MethodCallExpr addMethodCall = retrieved.get(0);
NodeList<Expression> arguments = addMethodCall.getArguments();
commonVerifyMiningFieldsObjectCreation(arguments, miningFields);
retrieved = getMethodCallExprList(constructorDeclaration.getBody(), outputFields.size(), "outputFields", "add");
addMethodCall = retrieved.get(0);
arguments = addMethodCall.getArguments();
commonVerifyOutputFieldsObjectCreation(arguments, outputFields);
retrieved = getMethodCallExprList(constructorDeclaration.getBody(), outputFields.size(), "kiePMMLTargets", "add");
addMethodCall = retrieved.get(0);
arguments = addMethodCall.getArguments();
commonVerifyKiePMMLTargetFieldsMethodCallExpr(arguments, targetFields);
}
use of com.github.javaparser.ast.NodeList 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 com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLFactoryFactory method populateKiePmmlFields.
static void populateKiePmmlFields(final FieldDeclaration toPopulate, Map<String, Boolean> generatedClassesModelTypeMap) {
final VariableDeclarator variable = toPopulate.getVariable(0);
Set<Expression> methodCallExpressions = generatedClassesModelTypeMap.entrySet().stream().map(entry -> getInstantiationExpression(entry.getKey(), entry.getValue())).collect(Collectors.toSet());
NodeList<Expression> expressions = NodeList.nodeList(methodCallExpressions);
MethodCallExpr initializer = new MethodCallExpr(new NameExpr("Arrays"), "asList", expressions);
variable.setInitializer(initializer);
}
Aggregations