use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLAttributeFactory method getAttributeVariableDeclaration.
static BlockStmt getAttributeVariableDeclaration(final String variableName, final Attribute attribute, final List<Field<?>> fields) {
final MethodDeclaration methodDeclaration = ATTRIBUTE_TEMPLATE.getMethodsByName(GETKIEPMMLATTRIBUTE).get(0).clone();
final BlockStmt attributeBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(attributeBody, ATTRIBUTE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, ATTRIBUTE, attributeBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
String predicateVariableName = String.format("%s_Predicate", variableName);
BlockStmt toAdd = getKiePMMLPredicate(predicateVariableName, attribute.getPredicate(), fields);
toAdd.getStatements().forEach(toReturn::addStatement);
final Expression complexPartialScoreExpression;
if (attribute.getComplexPartialScore() != null) {
String complexPartialScoreVariableName = String.format("%s_ComplexPartialScore", variableName);
toAdd = getComplexPartialScoreVariableDeclaration(complexPartialScoreVariableName, attribute.getComplexPartialScore());
toAdd.getStatements().forEach(toReturn::addStatement);
complexPartialScoreExpression = new NameExpr(complexPartialScoreVariableName);
} else {
complexPartialScoreExpression = new NullLiteralExpr();
}
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, ATTRIBUTE, attributeBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
builder.setArgument(0, new StringLiteralExpr(variableName));
builder.setArgument(2, new NameExpr(predicateVariableName));
getChainedMethodCallExprFrom("withPartialScore", initializer).setArgument(0, getExpressionForObject(attribute.getPartialScore()));
getChainedMethodCallExprFrom("withComplexPartialScore", initializer).setArgument(0, complexPartialScoreExpression);
attributeBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class CompiledFEELSupport method compiledErrorUnaryTest.
public static DirectCompilerResult compiledErrorUnaryTest(String msg) {
LambdaExpr initializer = new LambdaExpr();
initializer.setEnclosingParameters(true);
initializer.addParameter(new Parameter(new UnknownType(), "feelExprCtx"));
initializer.addParameter(new Parameter(new UnknownType(), "left"));
Statement lambdaBody = new BlockStmt(new NodeList<>(new ExpressionStmt(compiledErrorExpression(msg)), new ReturnStmt(new BooleanLiteralExpr(false))));
initializer.setBody(lambdaBody);
String constantName = "UT_EMPTY";
VariableDeclarator vd = new VariableDeclarator(parseClassOrInterfaceType(UnaryTest.class.getCanonicalName()), constantName);
vd.setInitializer(initializer);
FieldDeclaration fd = new FieldDeclaration();
fd.setModifier(Modifier.publicModifier().getKeyword(), true);
fd.setModifier(Modifier.staticModifier().getKeyword(), true);
fd.setModifier(Modifier.finalModifier().getKeyword(), true);
fd.addVariable(vd);
fd.setJavadocComment(" FEEL unary test: - ");
MethodCallExpr list = new MethodCallExpr(compiledFeelSemanticMappingsFQN(), "list", new NodeList<>(new NameExpr(constantName)));
DirectCompilerResult directCompilerResult = DirectCompilerResult.of(list, BuiltInType.LIST);
directCompilerResult.addFieldDeclaration(fd);
return directCompilerResult;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLModelFactoryUtils method initStaticGetter.
/**
* Populate the given <code>ClassOrInterfaceDeclaration</code>' <b>staticGetter</b> with the <b>common</b>
* parameters needed to
* instantiate a <code>KiePMMLModel</code>
* @param compilationDTO
* @param modelTemplate
* @return
*/
public static void initStaticGetter(final CompilationDTO<? extends Model> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
final MethodDeclaration staticGetterMethod = modelTemplate.getMethodsByName(GET_MODEL).get(0);
final BlockStmt staticGetterBody = staticGetterMethod.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, staticGetterMethod)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(staticGetterBody, TO_RETURN).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TO_RETURN, staticGetterBody)));
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TO_RETURN, staticGetterBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final String name = compilationDTO.getModelName();
final Expression miningFunctionExpression;
if (compilationDTO.getMINING_FUNCTION() != null) {
MINING_FUNCTION miningFunction = compilationDTO.getMINING_FUNCTION();
miningFunctionExpression = new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name());
} else {
miningFunctionExpression = new NullLiteralExpr();
}
builder.setArgument(0, new StringLiteralExpr(name));
builder.setArgument(1, miningFunctionExpression);
String targetFieldName = compilationDTO.getTargetFieldName();
final Expression targetFieldExpression;
if (targetFieldName != null) {
targetFieldExpression = new StringLiteralExpr(targetFieldName);
} else {
targetFieldExpression = new NullLiteralExpr();
}
getChainedMethodCallExprFrom("withTargetField", initializer).setArgument(0, targetFieldExpression);
//
populateGetCreatedMiningFieldsMethod(modelTemplate, compilationDTO.getKieMiningFields());
populateGetCreatedOutputFieldsMethod(modelTemplate, compilationDTO.getKieOutputFields());
populateGetCreatedKiePMMLMiningFieldsMethod(modelTemplate, compilationDTO.getMiningSchema().getMiningFields(), compilationDTO.getFields());
if (compilationDTO.getOutput() != null) {
populateGetCreatedKiePMMLOutputFieldsMethod(modelTemplate, compilationDTO.getOutput().getOutputFields());
}
if (compilationDTO.getKieTargetFields() != null) {
populateGetCreatedKiePMMLTargetsMethod(modelTemplate, compilationDTO.getKieTargetFields());
}
populateGetCreatedTransformationDictionaryMethod(modelTemplate, compilationDTO.getTransformationDictionary());
populateGetCreatedLocalTransformationsMethod(modelTemplate, compilationDTO.getLocalTransformations());
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLDerivedFieldFactory method getDerivedFieldVariableDeclaration.
static BlockStmt getDerivedFieldVariableDeclaration(final String variableName, final DerivedField derivedField) {
final MethodDeclaration methodDeclaration = DERIVED_FIELD_TEMPLATE.getMethodsByName(GETKIEPMMLDERIVEDFIELD).get(0).clone();
final BlockStmt derivedFieldBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(derivedFieldBody, DERIVED_FIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, DERIVED_FIELD, derivedFieldBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, 0);
BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, derivedField.getExpression());
toAdd.getStatements().forEach(toReturn::addStatement);
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, DERIVED_FIELD, derivedFieldBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final Expression dataTypeExpression = getExpressionForDataType(derivedField.getDataType());
final Expression opTypeExpression = getExpressionForOpType(derivedField.getOpType());
builder.setArgument(0, new StringLiteralExpr(derivedField.getName().getValue()));
builder.setArgument(2, dataTypeExpression);
builder.setArgument(3, opTypeExpression);
builder.setArgument(4, new NameExpr(nestedVariableName));
getChainedMethodCallExprFrom("withDisplayName", initializer).setArgument(0, getExpressionForObject(derivedField.getDisplayName()));
derivedFieldBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLFieldRefFactory method getFieldRefVariableDeclaration.
static BlockStmt getFieldRefVariableDeclaration(final String variableName, final FieldRef fieldRef) {
final MethodDeclaration methodDeclaration = FIELDREF_TEMPLATE.getMethodsByName(GETKIEPMMLFIELDREF).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, FIELD_REF).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, FIELD_REF, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, FIELD_REF, toReturn))).asObjectCreationExpr();
final StringLiteralExpr nameExpr = new StringLiteralExpr(fieldRef.getField().getValue());
final Expression mapMissingToExpr = getExpressionForObject(fieldRef.getMapMissingTo());
objectCreationExpr.getArguments().set(0, nameExpr);
objectCreationExpr.getArguments().set(2, mapMissingToExpr);
return toReturn;
}
Aggregations