use of com.github.javaparser.ast.expr.MethodReferenceExpr in project drools by kiegroup.
the class LegacyAccumulate method build.
public void build() {
Pattern pattern = (Pattern) new PatternBuilder().build(ruleBuildContext, basePattern);
Accumulate accumulate = (Accumulate) pattern.getSource();
final Set<String> imports = ruleBuildContext.getPkg().getImports().keySet();
final String packageName = ruleBuildContext.getPkg().getName();
if (context.getLegacyAccumulateCounter() == 0) {
GeneratedClassWithPackage generatedClassWithPackage = createAllAccumulateClass(imports, packageName);
packageModel.addGeneratedAccumulateClasses(generatedClassWithPackage);
} else {
for (GeneratedClassWithPackage c : packageModel.getGeneratedAccumulateClasses()) {
String ruleClassName = StringUtils.ucFirst(context.getRuleDescr().getClassName());
if (ruleClassName.equals(c.getClassName())) {
for (String m : ruleBuildContext.getMethods()) {
c.getGeneratedClass().addMember(parseBodyDeclaration(m));
}
break;
}
}
}
GeneratedClassWithPackage invokerGenerated = createInvokerClass(imports, packageName);
packageModel.addGeneratedAccumulateClasses(invokerGenerated);
final String generatedClassName = invokerGenerated.getGeneratedClass().getName().asString();
String typeWithPackage = String.format("%s.%s", packageName, generatedClassName);
Expression accExpr = new MethodReferenceExpr(new NameExpr(typeWithPackage), new NodeList<Type>(), "new");
MethodCallExpr accFunctionCall = createDslTopLevelMethod(ACC_FUNCTION_CALL, nodeList(accExpr));
if (accumulate.getRequiredDeclarations().length > 0) {
accFunctionCall = new MethodCallExpr(accFunctionCall, ACC_WITH_EXTERNAL_DECLRS_CALL);
for (Declaration requiredDeclaration : accumulate.getRequiredDeclarations()) {
accFunctionCall.addArgument(context.getVar(requiredDeclaration.getIdentifier()));
}
}
final String identifier;
if (basePattern.getIdentifier() != null) {
identifier = basePattern.getIdentifier();
} else {
// As we don't have a bind we need a unique name. Let's use the same as the generated invoker
identifier = generatedClassName;
context.addDeclaration(generatedClassName, Object.class);
}
Expression bindingVariable = context.getVarExpr(identifier);
accFunctionCall = new MethodCallExpr(accFunctionCall, BIND_AS_CALL, nodeList(bindingVariable));
context.addExpression(accFunctionCall);
context.increaseLegacyAccumulateCounter();
}
use of com.github.javaparser.ast.expr.MethodReferenceExpr in project drools by kiegroup.
the class CommonCodegenUtils method addMapPopulation.
/**
* For every entry in the given map, add
* <pre>
* (<i>mapName</i>).put(<i>entry_key<i/>, this::<i>entry_value_ref</i>>);
* </pre>
* e.g.
* <pre>
* MAP_NAME.put("KEY_0", this::METHOD_015);
* MAP_NAME.put("KEY_3", this::METHOD_33);
* MAP_NAME.put("KEY_2", this::METHOD_219);
* MAP_NAME.put("KEY_4", this::METHOD_46);
* </pre>
* inside the given <code>BlockStmt</code>
* @param toAdd
* @param body
* @param mapName
*/
public static void addMapPopulation(final Map<String, MethodDeclaration> toAdd, final BlockStmt body, final String mapName) {
Map<String, Expression> toAddExpr = toAdd.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> {
MethodReferenceExpr methodReferenceExpr = new MethodReferenceExpr();
methodReferenceExpr.setScope(new ThisExpr());
methodReferenceExpr.setIdentifier(entry.getValue().getNameAsString());
return methodReferenceExpr;
}));
addMapPopulationExpressions(toAddExpr, body, mapName);
}
use of com.github.javaparser.ast.expr.MethodReferenceExpr in project drools by kiegroup.
the class KiePMMLNodeFactory method mergeNodeReferences.
/**
* Adjust the <b>evaluateNode(?)</b> references to the ones declared in the given
* <code>JavaParserDTO.nodeTemplate</code>
*
* @param toPopulate
* @param nestedNodeNamesDTO
* @param evaluateNodeInitializer
*/
static void mergeNodeReferences(final JavaParserDTO toPopulate, final NodeNamesDTO nestedNodeNamesDTO, final MethodCallExpr evaluateNodeInitializer) {
final NodeList<Expression> evaluateNodeReferences = evaluateNodeInitializer.getArguments();
final String expectedReference = String.format(PACKAGE_CLASS_TEMPLATE, toPopulate.packageName, nestedNodeNamesDTO.nodeClassName);
Optional<MethodReferenceExpr> found = Optional.empty();
for (Expression expression : evaluateNodeReferences) {
if (expectedReference.equals(expression.asMethodReferenceExpr().getScope().toString())) {
found = Optional.of(expression.asMethodReferenceExpr());
break;
}
}
final MethodReferenceExpr evaluateNodeReference = found.orElseThrow(() -> new KiePMMLException(String.format(MISSING_METHOD_REFERENCE_TEMPLATE, expectedReference, evaluateNodeInitializer)));
String identifier = EVALUATE_NODE + nestedNodeNamesDTO.nodeClassName;
evaluateNodeReference.setScope(new NameExpr(toPopulate.nodeClassName));
evaluateNodeReference.setIdentifier(identifier);
}
use of com.github.javaparser.ast.expr.MethodReferenceExpr in project drools by kiegroup.
the class KiePMMLNodeFactoryTest method mergeNodeReferences.
@Test
public void mergeNodeReferences() {
KiePMMLNodeFactory.NodeNamesDTO nodeNamesDTO = new KiePMMLNodeFactory.NodeNamesDTO(nodeRoot, createNodeClassName(), null, 1.0);
KiePMMLNodeFactory.JavaParserDTO toPopulate = new KiePMMLNodeFactory.JavaParserDTO(nodeNamesDTO, PACKAGE_NAME);
Node nestedNode = nodeRoot.getNodes().get(0);
// Creating evaluateNodeInitializer
String nestedNodeClassName = nodeNamesDTO.childrenNodes.get(nestedNode);
String fullNestedNodeClassName = String.format(PACKAGE_CLASS_TEMPLATE, PACKAGE_NAME, nestedNodeClassName);
final NodeList<Expression> methodReferenceExprs = NodeList.nodeList(KiePMMLNodeFactory.getEvaluateNodeMethodReference(fullNestedNodeClassName));
MethodReferenceExpr evaluateNodeReference = new MethodReferenceExpr();
evaluateNodeReference.setScope(new NameExpr(fullNestedNodeClassName));
evaluateNodeReference.setIdentifier(EVALUATE_NODE);
MethodCallExpr evaluateNodeInitializer = new MethodCallExpr();
evaluateNodeInitializer.setScope(new TypeExpr(parseClassOrInterfaceType(Arrays.class.getName())));
evaluateNodeInitializer.setName(AS_LIST);
evaluateNodeInitializer.setArguments(methodReferenceExprs);
//
KiePMMLNodeFactory.NodeNamesDTO nestedNodeNamesDTO = new KiePMMLNodeFactory.NodeNamesDTO(nestedNode, nodeNamesDTO.getNestedNodeClassName(nestedNode), nodeNamesDTO.nodeClassName, nodeNamesDTO.missingValuePenalty);
KiePMMLNodeFactory.mergeNodeReferences(toPopulate, nestedNodeNamesDTO, evaluateNodeInitializer);
MethodReferenceExpr retrieved = evaluateNodeInitializer.getArguments().get(0).asMethodReferenceExpr();
String expected = toPopulate.nodeClassName;
assertEquals(expected, retrieved.getScope().asNameExpr().toString());
expected = EVALUATE_NODE + nestedNodeClassName;
assertEquals(expected, retrieved.getIdentifier());
}
use of com.github.javaparser.ast.expr.MethodReferenceExpr in project drools by kiegroup.
the class KiePMMLTreeModelFactory method setConstructor.
static void setConstructor(final TreeCompilationDTO compilationDTO, final ClassOrInterfaceDeclaration modelTemplate, final String fullNodeClassName) {
KiePMMLModelFactoryUtils.init(compilationDTO, modelTemplate);
final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
final BlockStmt body = constructorDeclaration.getBody();
// set predicate function
MethodReferenceExpr nodeReference = new MethodReferenceExpr();
nodeReference.setScope(new NameExpr(fullNodeClassName));
nodeReference.setIdentifier("evaluateNode");
CommonCodegenUtils.setAssignExpressionValue(body, "nodeFunction", nodeReference);
}
Aggregations