use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.
the class FunctionGenerator method toFunction.
public static MethodDeclaration toFunction(FunctionDescr desc) {
List<Parameter> parameters = new ArrayList<>();
List<String> parameterTypes = desc.getParameterTypes();
for (int i = 0; i < parameterTypes.size(); i++) {
String type = parameterTypes.get(i);
String name = desc.getParameterNames().get(i);
parameters.add(new Parameter(parseType(type), name));
}
EnumSet<Modifier> modifiers = EnumSet.of(Modifier.PUBLIC, Modifier.STATIC);
MethodDeclaration methodDeclaration = new MethodDeclaration(modifiers, desc.getName(), parseType(desc.getReturnType()), nodeList(parameters));
methodDeclaration.setBody(DrlxParseUtil.parseBlock(desc.getBody()));
return methodDeclaration;
}
use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.
the class FlowExpressionBuilder method buildIndexedBy.
private MethodCallExpr buildIndexedBy(DrlxParseSuccess drlxParseResult, MethodCallExpr exprDSL) {
if (!hasIndex(drlxParseResult)) {
return exprDSL;
}
TypedExpression left = drlxParseResult.getLeft();
TypedExpression right = drlxParseResult.getRight();
Class<?> indexType = Stream.of(left, right).map(TypedExpression::getType).filter(Objects::nonNull).findFirst().get();
ClassExpr indexedBy_indexedClass = new ClassExpr(JavaParser.parseType(indexType.getCanonicalName()));
// not 100% accurate as the type in "nameExpr" is actually parsed if it was JavaParsers as a big chain of FieldAccessExpr
FieldAccessExpr indexedBy_constraintType = new FieldAccessExpr(new NameExpr("org.drools.model.Index.ConstraintType"), drlxParseResult.getDecodeConstraintType().toString());
LambdaExpr indexedBy_leftOperandExtractor = new LambdaExpr();
indexedBy_leftOperandExtractor.addParameter(new Parameter(new UnknownType(), "_this"));
boolean leftContainsThis = left.getExpression().toString().contains("_this");
indexedBy_leftOperandExtractor.setBody(new ExpressionStmt(leftContainsThis ? left.getExpression() : right.getExpression()));
MethodCallExpr indexedByDSL = new MethodCallExpr(exprDSL, INDEXED_BY_CALL);
indexedByDSL.addArgument(indexedBy_indexedClass);
indexedByDSL.addArgument(indexedBy_constraintType);
indexedByDSL.addArgument("" + indexIdGenerator.getFieldId(drlxParseResult.getPatternType(), left.getFieldName()));
indexedByDSL.addArgument(indexedBy_leftOperandExtractor);
Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
if (isAlphaIndex(usedDeclarations)) {
indexedByDSL.addArgument(right.getExpression());
} else {
LambdaExpr indexedBy_rightOperandExtractor = new LambdaExpr();
indexedBy_rightOperandExtractor.addParameter(new Parameter(new UnknownType(), usedDeclarations.iterator().next()));
indexedBy_rightOperandExtractor.setBody(new ExpressionStmt(!leftContainsThis ? left.getExpression() : right.getExpression()));
indexedByDSL.addArgument(indexedBy_rightOperandExtractor);
}
return indexedByDSL;
}
use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.
the class PatternExpressionBuilder method generateLambdaWithoutParameters.
public Expression generateLambdaWithoutParameters(Collection<String> usedDeclarations, Expression expr, boolean skipFirstParamAsThis) {
if (skipFirstParamAsThis && usedDeclarations.isEmpty()) {
return expr;
}
LambdaExpr lambdaExpr = new LambdaExpr();
lambdaExpr.setEnclosingParameters(true);
if (!skipFirstParamAsThis) {
lambdaExpr.addParameter(new Parameter(new UnknownType(), "_this"));
}
usedDeclarations.stream().map(s -> new Parameter(new UnknownType(), s)).forEach(lambdaExpr::addParameter);
lambdaExpr.setBody(new ExpressionStmt(expr));
return lambdaExpr;
}
use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.
the class PatternExpressionBuilder method buildIndexedBy.
private Optional<MethodCallExpr> buildIndexedBy(DrlxParseSuccess drlxParseResult) {
if (!hasIndex(drlxParseResult)) {
return Optional.empty();
}
IndexUtil.ConstraintType decodeConstraintType = drlxParseResult.getDecodeConstraintType();
TypedExpression left = drlxParseResult.getLeft();
TypedExpression right = drlxParseResult.getRight();
Class<?> indexType = Stream.of(left, right).map(TypedExpression::getType).filter(Objects::nonNull).findFirst().get();
ClassExpr indexedBy_indexedClass = new ClassExpr(JavaParser.parseType(indexType.getCanonicalName()));
// not 100% accurate as the type in "nameExpr" is actually parsed if it was JavaParsers as a big chain of FieldAccessExpr
FieldAccessExpr indexedBy_constraintType = new FieldAccessExpr(new NameExpr("org.drools.model.Index.ConstraintType"), decodeConstraintType.toString());
LambdaExpr indexedBy_leftOperandExtractor = new LambdaExpr();
indexedBy_leftOperandExtractor.addParameter(new Parameter(new UnknownType(), "_this"));
boolean leftContainsThis = left.getExpression().toString().contains("_this");
indexedBy_leftOperandExtractor.setBody(new ExpressionStmt(leftContainsThis ? left.getExpression() : right.getExpression()));
MethodCallExpr indexedByDSL = new MethodCallExpr(null, drlxParseResult.isBetaNode() ? BETA_INDEXED_BY_CALL : ALPHA_INDEXED_BY_CALL);
indexedByDSL.addArgument(indexedBy_indexedClass);
indexedByDSL.addArgument(indexedBy_constraintType);
indexedByDSL.addArgument("" + indexIdGenerator.getFieldId(drlxParseResult.getPatternType(), left.getFieldName()));
indexedByDSL.addArgument(indexedBy_leftOperandExtractor);
Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
if (isAlphaIndex(usedDeclarations)) {
indexedByDSL.addArgument(right.getExpression());
} else if (usedDeclarations.size() == 1) {
LambdaExpr indexedBy_rightOperandExtractor = new LambdaExpr();
indexedBy_rightOperandExtractor.addParameter(new Parameter(new UnknownType(), usedDeclarations.iterator().next()));
indexedBy_rightOperandExtractor.setBody(new ExpressionStmt(!leftContainsThis ? left.getExpression() : right.getExpression()));
indexedByDSL.addArgument(indexedBy_rightOperandExtractor);
}
return Optional.of(indexedByDSL);
}
Aggregations