use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLTextIndexNormalizationFactory method getTextIndexNormalizationVariableDeclaration.
static BlockStmt getTextIndexNormalizationVariableDeclaration(final String variableName, final TextIndexNormalization textIndexNormalization) {
if (textIndexNormalization.getInlineTable() == null && textIndexNormalization.getTableLocator() != null) {
throw new UnsupportedOperationException("TableLocator not supported, yet");
}
final MethodDeclaration methodDeclaration = TEXTINDEXNORMALIZATION_TEMPLATE.getMethodsByName(GETKIEPMMLTEXTINDEXNORMALIZATION).get(0).clone();
final BlockStmt textIndexNormalizationBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(textIndexNormalizationBody, TEXTINDEXNORMALIZATION).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TEXTINDEXNORMALIZATION, textIndexNormalizationBody)));
variableDeclarator.setName(variableName);
String inlineTableVariableName = String.format("%s_InlineTable", variableName);
final BlockStmt toReturn = new BlockStmt();
BlockStmt toAdd = getInlineTableVariableDeclaration(inlineTableVariableName, textIndexNormalization.getInlineTable());
toAdd.getStatements().forEach(toReturn::addStatement);
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TEXTINDEXNORMALIZATION, textIndexNormalizationBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final StringLiteralExpr nameExpr = new StringLiteralExpr(variableName);
builder.setArgument(0, nameExpr);
getChainedMethodCallExprFrom("withInField", initializer).setArgument(0, getExpressionForObject(textIndexNormalization.getInField()));
getChainedMethodCallExprFrom("withOutField", initializer).setArgument(0, getExpressionForObject(textIndexNormalization.getOutField()));
getChainedMethodCallExprFrom("withKiePMMLInlineTable", initializer).setArgument(0, new NameExpr(inlineTableVariableName));
getChainedMethodCallExprFrom("withRegexField", initializer).setArgument(0, getExpressionForObject(textIndexNormalization.getRegexField()));
getChainedMethodCallExprFrom("withRecursive", initializer).setArgument(0, getExpressionForObject(textIndexNormalization.isRecursive()));
BooleanLiteralExpr isCaseSensitiveExpression = textIndexNormalization.isCaseSensitive() != null ? (BooleanLiteralExpr) getExpressionForObject(textIndexNormalization.isCaseSensitive()) : new BooleanLiteralExpr(false);
getChainedMethodCallExprFrom("withIsCaseSensitive", initializer).setArgument(0, isCaseSensitiveExpression);
getChainedMethodCallExprFrom("withMaxLevenshteinDistance", initializer).setArgument(0, getExpressionForObject(textIndexNormalization.getMaxLevenshteinDistance()));
Expression wordSeparatorCharacterREExpression;
if (textIndexNormalization.getWordSeparatorCharacterRE() != null) {
String wordSeparatorCharacterRE = StringEscapeUtils.escapeJava(textIndexNormalization.getWordSeparatorCharacterRE());
wordSeparatorCharacterREExpression = new StringLiteralExpr(wordSeparatorCharacterRE);
} else {
wordSeparatorCharacterREExpression = new NullLiteralExpr();
}
getChainedMethodCallExprFrom("withWordSeparatorCharacterRE", initializer).setArgument(0, wordSeparatorCharacterREExpression);
BooleanLiteralExpr tokenizeExpression = textIndexNormalization.isTokenize() != null ? (BooleanLiteralExpr) getExpressionForObject(textIndexNormalization.isTokenize()) : new BooleanLiteralExpr(false);
getChainedMethodCallExprFrom("withTokenize", initializer).setArgument(0, tokenizeExpression);
textIndexNormalizationBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLTruePredicateFactory method getTruePredicateVariableDeclaration.
static BlockStmt getTruePredicateVariableDeclaration(final String variableName, final True truePredicate) {
final MethodDeclaration methodDeclaration = TRUEPREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLTRUEPREDICATE).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, TRUEPREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TRUEPREDICATE, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TRUEPREDICATE, toReturn))).asObjectCreationExpr();
final StringLiteralExpr nameExpr = new StringLiteralExpr(variableName);
objectCreationExpr.getArguments().set(0, nameExpr);
return toReturn;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLFieldColumnPairFactory method getFieldColumnPairVariableDeclaration.
static BlockStmt getFieldColumnPairVariableDeclaration(final String variableName, final FieldColumnPair fieldColumnPair) {
final MethodDeclaration methodDeclaration = FIELDCOLUMNPAIR_TEMPLATE.getMethodsByName(GETKIEPMMLFIELDCOLUMNPAIR).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, FIELDCOLUMNPAIR).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, FIELDCOLUMNPAIR, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, FIELDCOLUMNPAIR, toReturn))).asObjectCreationExpr();
objectCreationExpr.getArguments().set(0, new StringLiteralExpr(fieldColumnPair.getField().getValue()));
objectCreationExpr.getArguments().set(2, new StringLiteralExpr(fieldColumnPair.getColumn()));
return toReturn;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLInlineTableFactory method getInlineTableVariableDeclaration.
static BlockStmt getInlineTableVariableDeclaration(final String variableName, final InlineTable inlineTable) {
final MethodDeclaration methodDeclaration = INLINETABLE_TEMPLATE.getMethodsByName(GETKIEPMMLINLINETABLE).get(0).clone();
final BlockStmt inlineTableBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(inlineTableBody, INLINETABLE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, INLINETABLE, inlineTableBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
for (Row row : inlineTable.getRows()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getRowVariableDeclaration(nestedVariableName, row);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, INLINETABLE, toReturn))).asObjectCreationExpr();
objectCreationExpr.getArguments().set(0, new StringLiteralExpr(variableName));
objectCreationExpr.getArguments().get(2).asMethodCallExpr().setArguments(arguments);
inlineTableBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLLocalTransformationsFactory method getKiePMMLLocalTransformationsVariableDeclaration.
/**
* @param localTransformations
* @return
*/
static BlockStmt getKiePMMLLocalTransformationsVariableDeclaration(final LocalTransformations localTransformations) {
final MethodDeclaration methodDeclaration = LOCAL_TRANSFORMATIONS_TEMPLATE.getMethodsByName(GETKIEPMMLLOCALTRANSFORMATIONS).get(0).clone();
final BlockStmt transformationDictionaryBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(transformationDictionaryBody, LOCAL_TRANSFORMATIONS).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, LOCAL_TRANSFORMATIONS, transformationDictionaryBody)));
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, LOCAL_TRANSFORMATIONS, methodDeclaration))).asMethodCallExpr();
final BlockStmt toReturn = new BlockStmt();
if (localTransformations.hasDerivedFields()) {
NodeList<Expression> derivedFields = addDerivedField(toReturn, localTransformations.getDerivedFields());
getChainedMethodCallExprFrom("withDerivedFields", initializer).setArguments(derivedFields);
}
transformationDictionaryBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
Aggregations