use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class AccumulateInline method addAccumulateClassInitializationToMethod.
private void addAccumulateClassInitializationToMethod(MethodCallExpr accumulateDSL, String identifier) {
this.packageModel.addGeneratedPOJO(accumulateInlineClass);
final MethodCallExpr functionDSL = createDslTopLevelMethod(ACC_FUNCTION_CALL);
functionDSL.addArgument(new MethodReferenceExpr(new NameExpr(accumulateInlineClassName), new NodeList<>(), "new"));
functionDSL.addArgument(context.getVarExpr(identifier));
final String bindingId = this.basePattern.getIdentifier();
final MethodCallExpr asDSL = new MethodCallExpr(functionDSL, BIND_AS_CALL);
asDSL.addArgument(context.getVarExpr(bindingId));
accumulateDSL.addArgument(asDSL);
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class GeneratedMvelParserBase method generateLambda.
/**
* Workaround for rather complex ambiguity that lambda's create
*/
Expression generateLambda(Expression ret, Statement lambdaBody) {
if (ret instanceof EnclosedExpr) {
Expression inner = ((EnclosedExpr) ret).getInner();
SimpleName id = ((NameExpr) inner).getName();
NodeList<Parameter> params = add(new NodeList<>(), new Parameter(ret.getTokenRange().orElse(null), new NodeList<>(), new NodeList<>(), new UnknownType(), false, new NodeList<>(), id));
ret = new LambdaExpr(range(ret, lambdaBody), params, lambdaBody, true);
} else if (ret instanceof NameExpr) {
SimpleName id = ((NameExpr) ret).getName();
NodeList<Parameter> params = add(new NodeList<>(), new Parameter(ret.getTokenRange().orElse(null), new NodeList<>(), new NodeList<>(), new UnknownType(), false, new NodeList<>(), id));
ret = new LambdaExpr(range(ret, lambdaBody), params, lambdaBody, false);
} else if (ret instanceof LambdaExpr) {
((LambdaExpr) ret).setBody(lambdaBody);
propagateRangeGrowthOnRight(ret, lambdaBody);
} else if (ret instanceof CastExpr) {
CastExpr castExpr = (CastExpr) ret;
Expression inner = generateLambda(castExpr.getExpression(), lambdaBody);
castExpr.setExpression(inner);
} else {
addProblem("Failed to parse lambda expression! Please create an issue at https://github.com/javaparser/javaparser/issues");
}
return ret;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method createIntervalsExpression.
@Test
public void createIntervalsExpression() {
List<Interval> intervals = IntStream.range(0, 3).mapToObj(i -> {
int leftMargin = new Random().nextInt(40);
int rightMargin = leftMargin + 13;
return new Interval(leftMargin, rightMargin);
}).collect(Collectors.toList());
Expression retrieved = KiePMMLModelFactoryUtils.createIntervalsExpression(intervals);
assertNotNull(retrieved);
assertTrue(retrieved instanceof MethodCallExpr);
MethodCallExpr mtdExp = (MethodCallExpr) retrieved;
String expected = "java.util.Arrays";
assertEquals(expected, mtdExp.getScope().get().asNameExpr().toString());
expected = "asList";
assertEquals(expected, mtdExp.getName().asString());
NodeList<Expression> arguments = mtdExp.getArguments();
assertEquals(intervals.size(), arguments.size());
arguments.forEach(argument -> {
assertTrue(argument instanceof ObjectCreationExpr);
ObjectCreationExpr objCrt = (ObjectCreationExpr) argument;
assertEquals(Interval.class.getCanonicalName(), objCrt.getType().asString());
Optional<Interval> intervalOpt = intervals.stream().filter(interval -> String.valueOf(interval.getLeftMargin()).equals(objCrt.getArgument(0).asNameExpr().toString()) && String.valueOf(interval.getRightMargin()).equals(objCrt.getArgument(1).asNameExpr().toString())).findFirst();
assertTrue(intervalOpt.isPresent());
});
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class DMNAlphaNetworkCompiler method initPropertyNames.
private void initPropertyNames(List<InputClause> input) {
NodeList<Expression> propertyNamesArray = input.stream().map(inputClause -> inputClause.getInputExpression().getText()).map(StringLiteralExpr::new).collect(Collectors.toCollection(NodeList::new));
ArrayCreationExpr array = new ArrayCreationExpr().setElementType(new ArrayType(parseType(String.class.getCanonicalName()))).setInitializer(new ArrayInitializerExpr(propertyNamesArray));
template.findAll(StringLiteralExpr.class, n -> n.asString().equals("PROPERTY_NAMES")).forEach(r -> r.replace(array));
}
use of com.github.javaparser.ast.NodeList 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;
}
Aggregations