use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class BoxedParameters method getBoxedParametersWithUnboxedAssignment.
// Types in the executable model are promoted to boxed to type check the Java DSL.
// We add such promoted types as _<PARAMETER_NAME> (with the underscore prefix)
// and then we downcast to the original unboxed type in the body of the function (methodBody)
public NodeList<Parameter> getBoxedParametersWithUnboxedAssignment(Collection<String> declarationUsedInRHS, BlockStmt methodBody) {
NodeList<Parameter> parameters = NodeList.nodeList();
for (String parameterName : declarationUsedInRHS) {
DeclarationSpec declaration = context.getDeclarationByIdWithException(parameterName);
Parameter boxedParameter;
Type boxedType = declaration.getBoxedType();
if (declaration.isBoxed()) {
String boxedParameterName = "_" + parameterName;
boxedParameter = new Parameter(boxedType, boxedParameterName);
Expression unboxedTypeDowncast = new VariableDeclarationExpr(new VariableDeclarator(declaration.getRawType(), parameterName, new NameExpr(boxedParameterName)));
methodBody.addStatement(0, unboxedTypeDowncast);
} else {
boxedParameter = new Parameter(boxedType, parameterName);
}
parameters.add(boxedParameter);
}
return parameters;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class DMNAlphaNetworkEvaluatorCompiler method createAlphaNetworkCompiler.
private ObjectTypeNodeCompiler createAlphaNetworkCompiler(ObjectTypeNode firstObjectTypeNodeOfRete) {
ANCConfiguration ancConfiguration = new ANCConfiguration();
ancConfiguration.setDisableContextEntry(true);
ancConfiguration.setPrettyPrint(true);
ancConfiguration.setEnableModifyObject(false);
ObjectTypeNodeCompiler objectTypeNodeCompiler = new ObjectTypeNodeCompiler(ancConfiguration, firstObjectTypeNodeOfRete, true);
VariableDeclarator variableDeclarator = new VariableDeclarator(StaticJavaParser.parseType(AlphaNetworkEvaluationContext.class.getCanonicalName()), "ctx");
objectTypeNodeCompiler.addAdditionalFields(new FieldDeclaration(NodeList.nodeList(), NodeList.nodeList(variableDeclarator)));
return objectTypeNodeCompiler;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLCompoundPredicateFactory method getCompoundPredicateVariableDeclaration.
static BlockStmt getCompoundPredicateVariableDeclaration(final String variableName, final CompoundPredicate compoundPredicate, final List<Field<?>> fields) {
final MethodDeclaration methodDeclaration = COMPOUND_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLCOMPOUNDPREDICATE).get(0).clone();
final BlockStmt compoundPredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(compoundPredicateBody, COMPOUND_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, COMPOUND_PREDICATE, compoundPredicateBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
for (Predicate predicate : compoundPredicate.getPredicates()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getKiePMMLPredicate(nestedVariableName, predicate, fields);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
final BOOLEAN_OPERATOR booleanOperator = BOOLEAN_OPERATOR.byName(compoundPredicate.getBooleanOperator().value());
final NameExpr booleanOperatorExpr = new NameExpr(BOOLEAN_OPERATOR.class.getName() + "." + booleanOperator.name());
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COMPOUND_PREDICATE, compoundPredicateBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
builder.setArgument(1, booleanOperatorExpr);
getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
compoundPredicateBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLOutputFieldFactory method getOutputFieldVariableDeclaration.
static BlockStmt getOutputFieldVariableDeclaration(final String variableName, final OutputField outputField) {
final MethodDeclaration methodDeclaration = OUTPUTFIELD_TEMPLATE.getMethodsByName(GETKIEPMMLOUTPUTFIELD).get(0).clone();
final BlockStmt outputFieldBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(outputFieldBody, OUTPUTFIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, OUTPUTFIELD, outputFieldBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
final Expression expressionExpr;
if (outputField.getExpression() != null) {
String nestedVariableName = String.format("%s_Expression", variableName);
BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, outputField.getExpression());
toAdd.getStatements().forEach(toReturn::addStatement);
expressionExpr = new NameExpr(nestedVariableName);
} else {
expressionExpr = new NullLiteralExpr();
}
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, OUTPUTFIELD, toReturn))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final StringLiteralExpr nameExpr = new StringLiteralExpr(outputField.getName().getValue());
final RESULT_FEATURE resultFeature = RESULT_FEATURE.byName(outputField.getResultFeature().value());
final NameExpr resultFeatureExpr = new NameExpr(RESULT_FEATURE.class.getName() + "." + resultFeature.name());
final Expression targetFieldExpr = outputField.getTargetField() != null ? getExpressionForObject(outputField.getTargetField().getValue()) : new NullLiteralExpr();
final Expression valueExpr = outputField.getValue() != null ? getExpressionForObject(outputField.getValue()) : new NullLiteralExpr();
final Expression dataTypeExpression = getExpressionForDataType(outputField.getDataType());
final Expression rankExpr = outputField.getRank() != null ? getExpressionForObject(outputField.getRank()) : new NullLiteralExpr();
builder.setArgument(0, nameExpr);
getChainedMethodCallExprFrom("withResultFeature", initializer).setArgument(0, resultFeatureExpr);
getChainedMethodCallExprFrom("withTargetField", initializer).setArgument(0, targetFieldExpr);
getChainedMethodCallExprFrom("withValue", initializer).setArgument(0, valueExpr);
getChainedMethodCallExprFrom("withDataType", initializer).setArgument(0, dataTypeExpression);
getChainedMethodCallExprFrom("withRank", initializer).setArgument(0, rankExpr);
getChainedMethodCallExprFrom("withKiePMMLExpression", initializer).setArgument(0, expressionExpr);
outputFieldBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLSimplePredicateFactory method getSimplePredicateVariableDeclaration.
static BlockStmt getSimplePredicateVariableDeclaration(final String variableName, final SimplePredicate simplePredicate, final List<Field<?>> fields) {
final MethodDeclaration methodDeclaration = SIMPLE_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLSIMPLEPREDICATE).get(0).clone();
final BlockStmt simplePredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(simplePredicateBody, SIMPLE_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, SIMPLE_PREDICATE, simplePredicateBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
final OPERATOR operator = OPERATOR.byName(simplePredicate.getOperator().value());
final NameExpr operatorExpr = new NameExpr(OPERATOR.class.getName() + "." + operator.name());
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, SIMPLE_PREDICATE, simplePredicateBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
builder.setArgument(0, new StringLiteralExpr(simplePredicate.getField().getValue()));
builder.setArgument(2, operatorExpr);
DataType dataType = getDataType(fields, simplePredicate.getField().getValue());
Object actualValue = DATA_TYPE.byName(dataType.value()).getActualValue(simplePredicate.getValue());
getChainedMethodCallExprFrom("withValue", initializer).setArgument(0, getExpressionForObject(actualValue));
simplePredicateBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
Aggregations