use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class OOPathExprGenerator method toPatternExpr.
private void toPatternExpr(String bindingId, List<DrlxParseResult> list) {
MethodCallExpr patternExpr = new MethodCallExpr(null, "pattern");
patternExpr.addArgument(toVar(bindingId));
for (DrlxParseResult drlx : list) {
if (drlx instanceof DrlxParseSuccess) {
MethodCallExpr expr = (MethodCallExpr) expressionBuilder.buildExpressionWithIndexing(((DrlxParseSuccess) drlx));
expr.setScope(patternExpr);
patternExpr = expr;
}
}
context.addExpression(patternExpr);
}
use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class OOPathExprGenerator method toFlowExpr.
private void toFlowExpr(String bindingId, List<DrlxParseResult> list) {
final List<DrlxParseSuccess> value = list.stream().filter(r -> r instanceof DrlxParseSuccess).map(DrlxParseSuccess.class::cast).collect(Collectors.toList());
if (value.size() == 1) {
context.addExpression(expressionBuilder.buildExpressionWithIndexing(value.get(0)));
} else {
final MethodCallExpr andDSL = new MethodCallExpr(null, "and");
value.forEach(e -> {
final Expression expression = expressionBuilder.buildExpressionWithIndexing(e);
andDSL.addArgument(expression);
});
context.addExpression(andDSL);
}
}
use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class OOPathExprGenerator method visit.
public void visit(Class<?> originalClass, String originalBind, DrlxParseSuccess patternParseResult) {
final OOPathExpr ooPathExpr = (OOPathExpr) patternParseResult.getExpr();
Class<?> previousClass = originalClass;
String previousBind = originalBind;
Map<String, List<DrlxParseResult>> ooPathConditionExpressions = new LinkedHashMap<>();
for (Iterator<OOPathChunk> iterator = ooPathExpr.getChunks().iterator(); iterator.hasNext(); ) {
OOPathChunk chunk = iterator.next();
final String fieldName = chunk.getField().toString();
final TypedExpression callExpr = DrlxParseUtil.nameExprToMethodCallExpr(fieldName, previousClass, null);
Class<?> fieldType = (chunk.getInlineCast() != null) ? DrlxParseUtil.getClassFromContext(context.getTypeResolver(), chunk.getInlineCast().toString()) : callExpr.getType();
if (Iterable.class.isAssignableFrom(fieldType)) {
fieldType = extractGenericType(previousClass, ((MethodCallExpr) callExpr.getExpression()).getName().toString());
}
final String chunkKey = originalBind + fieldName;
final String bindingId;
if (!iterator.hasNext() && patternParseResult.getExprBinding() != null) {
bindingId = patternParseResult.getExprBinding();
context.removeDeclarationById(bindingId);
} else {
bindingId = context.getOOPathId(fieldType, chunkKey);
}
final Expression accessorLambda = generateLambdaWithoutParameters(Collections.emptySortedSet(), prepend(new NameExpr("_this"), callExpr.getExpression()));
final MethodCallExpr reactiveFrom = new MethodCallExpr(null, "reactiveFrom");
reactiveFrom.addArgument(new NameExpr(toVar(previousBind)));
reactiveFrom.addArgument(accessorLambda);
DeclarationSpec newDeclaration = new DeclarationSpec(bindingId, fieldType, reactiveFrom);
context.addDeclaration(newDeclaration);
context.addOOPathDeclaration(newDeclaration);
final List<Expression> conditions = chunk.getConditions();
if (!conditions.isEmpty()) {
Class<?> finalFieldType = fieldType;
final List<DrlxParseResult> conditionParseResult = conditions.stream().map((Expression c) -> new ConstraintParser(context, packageModel).drlxParse(finalFieldType, bindingId, c.toString())).collect(Collectors.toList());
ooPathConditionExpressions.put(bindingId, conditionParseResult);
} else {
if (context.isPatternDSL()) {
ooPathConditionExpressions.put(bindingId, Collections.emptyList());
} else {
final DrlxParseSuccess drlxParseResult = new DrlxParseSuccess(fieldType, "", bindingId, new BooleanLiteralExpr(true), fieldType);
ooPathConditionExpressions.put(bindingId, Collections.singletonList(drlxParseResult));
}
}
previousBind = bindingId;
previousClass = fieldType;
}
ooPathConditionExpressions.forEach(context.isPatternDSL() ? this::toPatternExpr : this::toFlowExpr);
}
use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class QueryGenerator method processQuery.
public static void processQuery(KnowledgeBuilderImpl kbuilder, PackageModel packageModel, QueryDescr queryDescr) {
String queryDefVariableName = toQueryDef(queryDescr.getName());
RuleContext context = packageModel.getQueryDefWithType().get(queryDefVariableName).getContext();
new ModelGeneratorVisitor(context, packageModel).visit(queryDescr.getLhs());
final Type queryType = JavaParser.parseType(Query.class.getCanonicalName());
MethodDeclaration queryMethod = new MethodDeclaration(EnumSet.of(Modifier.PRIVATE), queryType, "query_" + toId(queryDescr.getName()));
BlockStmt queryBody = new BlockStmt();
ModelGenerator.createVariables(kbuilder, queryBody, packageModel, context);
queryMethod.setBody(queryBody);
String queryBuildVarName = toId(queryDescr.getName()) + "_build";
VariableDeclarationExpr queryBuildVar = new VariableDeclarationExpr(queryType, queryBuildVarName);
MethodCallExpr buildCall = new MethodCallExpr(new NameExpr(queryDefVariableName), BUILD_CALL);
context.getExpressions().forEach(buildCall::addArgument);
AssignExpr queryBuildAssign = new AssignExpr(queryBuildVar, buildCall, AssignExpr.Operator.ASSIGN);
queryBody.addStatement(queryBuildAssign);
queryBody.addStatement(new ReturnStmt(queryBuildVarName));
packageModel.putQueryMethod(queryMethod);
}
use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class WindowReferenceGenerator method addField.
private void addField(KnowledgeBuilderImpl kbuilder, PackageModel packageModel, WindowDeclarationDescr descr) {
final String windowName = toVar(descr.getName());
final MethodCallExpr initializer = new MethodCallExpr(null, "window");
final PatternDescr pattern = descr.getPattern();
ParsedBehavior behavior = pattern.getBehaviors().stream().map(this::parseTypeFromBehavior).findFirst().orElseThrow(RuntimeException::new);
final WindowDefinition.Type windowType = behavior.windowType;
initializer.addArgument(new NameExpr(windowType.getDeclaringClass().getCanonicalName() + "." + windowType.toString()));
initializer.addArgument(new IntegerLiteralExpr(behavior.duration.getValue()));
final TimeUnit timeUnit = behavior.duration.getTimeUnit();
initializer.addArgument(new NameExpr(timeUnit.getDeclaringClass().getCanonicalName() + "." + timeUnit.name()));
final Class<?> initClass = DrlxParseUtil.getClassFromContext(typeResolver, pattern.getObjectType());
final Type initType = JavaParser.parseType(initClass.getCanonicalName());
initializer.addArgument(new ClassExpr(initType));
parseConditions(kbuilder, packageModel, pattern, initClass).forEach(initializer::addArgument);
packageModel.addAllWindowReferences(windowName, initializer);
}
Aggregations