use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.
the class ModelGenerator method processRule.
private static void processRule(PackageDescr packageDescr, RuleContext context) {
PackageModel packageModel = context.getPackageModel();
RuleDescr ruleDescr = context.getRuleDescr();
context.addGlobalDeclarations();
context.setDialectFromAttributes(ruleDescr.getAttributes().values());
for (Entry<String, Object> kv : ruleDescr.getNamedConsequences().entrySet()) {
context.addNamedConsequence(kv.getKey(), kv.getValue().toString());
}
RuleUnitDescription ruleUnitDescr = context.getRuleUnitDescr();
BlockStmt ruleVariablesBlock = context.getRuleVariablesBlock();
new ModelGeneratorVisitor(context, packageModel).visit(getExtendedLhs(packageDescr, ruleDescr));
if (context.hasCompilationError()) {
return;
}
final String ruleMethodName = "rule_" + toId(ruleDescr.getName());
MethodDeclaration ruleMethod = new MethodDeclaration(NodeList.nodeList(Modifier.publicModifier(), Modifier.staticModifier()), toClassOrInterfaceType(Rule.class), ruleMethodName);
ruleMethod.setJavadocComment(" Rule name: " + ruleDescr.getName() + " ");
VariableDeclarationExpr ruleVar = new VariableDeclarationExpr(toClassOrInterfaceType(Rule.class), "rule");
MethodCallExpr ruleCall = createDslTopLevelMethod(RULE_CALL);
if (!ruleDescr.getNamespace().isEmpty()) {
ruleCall.addArgument(toStringLiteral(ruleDescr.getNamespace()));
}
ruleCall.addArgument(toStringLiteral(ruleDescr.getName()));
MethodCallExpr buildCallScope = ruleUnitDescr != null ? new MethodCallExpr(ruleCall, UNIT_CALL).addArgument(new ClassExpr(toClassOrInterfaceType(ruleUnitDescr.getCanonicalName()))) : ruleCall;
for (MethodCallExpr attributeExpr : ruleAttributes(context, ruleDescr)) {
attributeExpr.setScope(buildCallScope);
buildCallScope = attributeExpr;
}
for (MethodCallExpr metaAttributeExpr : ruleMetaAttributes(context, ruleDescr)) {
metaAttributeExpr.setScope(buildCallScope);
buildCallScope = metaAttributeExpr;
}
MethodCallExpr buildCall = new MethodCallExpr(buildCallScope, BUILD_CALL, NodeList.nodeList(context.getExpressions()));
createVariables(ruleVariablesBlock, packageModel, context);
ruleMethod.setBody(ruleVariablesBlock);
MethodCallExpr executeCall = new Consequence(context).createCall(ruleDescr, ruleDescr.getConsequence().toString(), ruleVariablesBlock, false);
buildCall.addArgument(executeCall);
ruleVariablesBlock.addStatement(new AssignExpr(ruleVar, buildCall, AssignExpr.Operator.ASSIGN));
ruleVariablesBlock.addStatement(new ReturnStmt("rule"));
packageModel.putRuleMethod(ruleUnitDescr != null ? ruleUnitDescr.getSimpleName() : DEFAULT_RULE_UNIT, ruleMethod, context.getRuleIndex());
}
use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.
the class CommonCodegenUtils method getFilteredKiePMMLNameValueExpression.
/**
* Returns
* <pre>
* Optional<KiePMMLNameValue> kiePMMLNameValue = (<i>kiePMMLNameValueListParam</i>)
* .stream()
* .filter((KiePMMLNameValue kpmmlnv) -> Objects.equals("(<i>fieldNameToRef</i>)", kpmmlnv.getName()))
* .findFirst();
* </pre>
* <p>
* expression, where <b>kiePMMLNameValueListParam</b> is the name of the
* <code>List<KiePMMLNameValue></code> parameter, and
* <b>fieldNameToRef</b> is the name of the field to find, in the containing method
* @param kiePMMLNameValueListParam
* @param fieldNameToRef
* @param stringLiteralComparison if <code>true</code>, equals comparison is made on the String, e.g Objects
* .equals("(<i>fieldNameToRef</i>)", kpmmlnv.getName())),
* otherwise, is done on object reference, e.g Objects.equals((<i>fieldNameToRef</i>), kpmmlnv.getName())). In
* this latter case, a <i>fieldNameToRef</i> variable is
* expected to exists
* @return
*/
public static ExpressionStmt getFilteredKiePMMLNameValueExpression(final String kiePMMLNameValueListParam, final String fieldNameToRef, boolean stringLiteralComparison) {
// kpmmlnv.getName()
MethodCallExpr argumentBodyExpressionArgument2 = new MethodCallExpr("getName");
argumentBodyExpressionArgument2.setScope(new NameExpr(LAMBDA_PARAMETER_NAME));
// Objects.equals(fieldNameToRef, kpmmlnv.getName())
MethodCallExpr argumentBodyExpression = new MethodCallExpr("equals");
Expression equalsComparisonExpression;
if (stringLiteralComparison) {
equalsComparisonExpression = new StringLiteralExpr(fieldNameToRef);
} else {
equalsComparisonExpression = new NameExpr(fieldNameToRef);
}
argumentBodyExpression.setArguments(NodeList.nodeList(equalsComparisonExpression, argumentBodyExpressionArgument2));
argumentBodyExpression.setScope(new NameExpr(Objects.class.getName()));
ExpressionStmt argumentBody = new ExpressionStmt(argumentBodyExpression);
// (KiePMMLNameValue kpmmlnv) -> Objects.equals(fieldNameToRef, kpmmlnv.getName())
Parameter argumentParameter = new Parameter(parseClassOrInterfaceType(KiePMMLNameValue.class.getName()), LAMBDA_PARAMETER_NAME);
LambdaExpr argument = new LambdaExpr();
//
argument.setEnclosingParameters(true).setParameters(NodeList.nodeList(argumentParameter));
// (KiePMMLNameValue kpmmlnv) ->
// Objects.equals(fieldNameToRef, kpmmlnv.getName())
argument.setBody(argumentBody);
// kiePMMLNameValueListParam.stream()
MethodCallExpr initializerScopeScope = new MethodCallExpr("stream");
initializerScopeScope.setScope(new NameExpr(kiePMMLNameValueListParam));
// kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue kpmmlnv) -> Objects.equals(fieldNameToRef,
// kpmmlnv.getName()))
MethodCallExpr initializerScope = new MethodCallExpr("filter");
initializerScope.setScope(initializerScopeScope);
initializerScope.setArguments(NodeList.nodeList(argument));
// kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue kpmmlnv) -> Objects.equals(fieldNameToRef,
// kpmmlnv.getName())).findFirst()
MethodCallExpr initializer = new MethodCallExpr("findFirst");
initializer.setScope(initializerScope);
// Optional<KiePMMLNameValue> kiePMMLNameValue
VariableDeclarator variableDeclarator = new VariableDeclarator(getTypedClassOrInterfaceTypeByTypeNames(Optional.class.getName(), Collections.singletonList(KiePMMLNameValue.class.getName())), OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME);
// Optional<KiePMMLNameValue> kiePMMLNameValue = kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue
// kpmmlnv) -> Objects.equals(fieldNameToRef, kpmmlnv.getName())).findFirst()
variableDeclarator.setInitializer(initializer);
//
VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(NodeList.nodeList(variableDeclarator));
ExpressionStmt toReturn = new ExpressionStmt();
toReturn.setExpression(variableDeclarationExpr);
return toReturn;
}
use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.
the class CommonCodegenUtilsTest method getVariableDeclarator.
@Test
public void getVariableDeclarator() {
final String variableName = "variableName";
final BlockStmt body = new BlockStmt();
assertFalse(CommonCodegenUtils.getVariableDeclarator(body, variableName).isPresent());
final VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(parseClassOrInterfaceType("String"), variableName);
body.addStatement(variableDeclarationExpr);
Optional<VariableDeclarator> retrieved = CommonCodegenUtils.getVariableDeclarator(body, variableName);
assertTrue(retrieved.isPresent());
VariableDeclarator variableDeclarator = retrieved.get();
assertEquals(variableName, variableDeclarator.getName().asString());
}
use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.
the class KiePMMLNodeFactoryTest method populateEvaluateNodeWithNodeFunctions.
@Test
public void populateEvaluateNodeWithNodeFunctions() {
final BlockStmt toPopulate = new BlockStmt();
final VariableDeclarator variableDeclarator = new VariableDeclarator();
variableDeclarator.setType("Object");
variableDeclarator.setName(NODE_FUNCTIONS);
toPopulate.addStatement(new VariableDeclarationExpr(variableDeclarator));
assertFalse(variableDeclarator.getInitializer().isPresent());
// empty list
List<String> nestedNodesFullClasses = Collections.emptyList();
KiePMMLNodeFactory.populateEvaluateNodeWithNodeFunctions(toPopulate, nestedNodesFullClasses);
commonVerifyEvaluateNodeWithNodeFunctions(variableDeclarator, nestedNodesFullClasses);
// populated list
nestedNodesFullClasses = IntStream.range(0, 2).mapToObj(i -> "full.node.NodeClassName" + i).collect(Collectors.toList());
KiePMMLNodeFactory.populateEvaluateNodeWithNodeFunctions(toPopulate, nestedNodesFullClasses);
commonVerifyEvaluateNodeWithNodeFunctions(variableDeclarator, nestedNodesFullClasses);
}
use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project checker-framework by typetools.
the class DoubleJavaParserVisitor method visit.
@Override
public void visit(final VariableDeclarationExpr node1, final Node other) {
VariableDeclarationExpr node2 = (VariableDeclarationExpr) other;
defaultAction(node1, node2);
visitLists(node1.getModifiers(), node2.getModifiers());
visitLists(node1.getVariables(), node2.getVariables());
}
Aggregations