use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class AccumulateVisitor method getRootNodeName.
private String getRootNodeName(DrlxParseUtil.RemoveRootNodeResult methodCallWithoutRootNode) {
final Expression rootNode = methodCallWithoutRootNode.getRootNode().orElseThrow(UnsupportedOperationException::new);
final String rootNodeName;
if (rootNode instanceof NameExpr) {
rootNodeName = ((NameExpr) rootNode).getName().asString();
} else {
throw new AccumulateParsingFailedException("Root node of expression should be a declaration");
}
return rootNodeName;
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class FromVisitor method fromFieldOrName.
private Optional<Expression> fromFieldOrName(String expression) {
Optional<String> optContainsBinding = DrlxParseUtil.findBindingIdFromDotExpression(expression);
final String bindingId = optContainsBinding.orElse(expression);
final DrlxExpression drlxExpression = DrlxParseUtil.parseExpression(expression);
final Expression parsedExpression = drlxExpression.getExpr();
Optional<TypedExpression> staticField = parsedExpression instanceof FieldAccessExpr ? ExpressionTyper.tryParseAsConstantField(context.getTypeResolver(), ((FieldAccessExpr) parsedExpression).getScope(), ((FieldAccessExpr) parsedExpression).getNameAsString()) : Optional.empty();
if (staticField.isPresent()) {
return of(createSupplier(parsedExpression));
}
if (packageModel.hasEntryPoint(bindingId)) {
return of(createEntryPointCall(bindingId));
}
if (contextHasDeclaration(bindingId)) {
return of(createFromCall(expression, bindingId, optContainsBinding.isPresent(), null));
}
return of(createUnitDataCall(bindingId));
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class FromVisitor method createArg.
private Expression createArg(String expression, String bindingId, MethodCallExpr fromCall) {
if (bindingId != null) {
DeclarationSpec declarationSpec = context.getDeclarationById(bindingId).orElseThrow(RuntimeException::new);
Class<?> clazz = declarationSpec.getDeclarationClass();
DrlxParseResult drlxParseResult = ConstraintParser.withoutVariableValidationConstraintParser(context, packageModel).drlxParse(clazz, bindingId, expression);
return drlxParseResult.acceptWithReturnValue(drlxParseSuccess -> {
SingleDrlxParseSuccess singleResult = (SingleDrlxParseSuccess) drlxParseResult;
if (!isCompatibleWithFromReturnType(patternType, singleResult.getExprRawClass())) {
context.addCompilationError(new InvalidExpressionErrorResult("Pattern of type: '" + patternType.getCanonicalName() + "' on rule '" + context.getRuleName() + "' is not compatible with type " + singleResult.getExprRawClass().getCanonicalName() + " returned by source"));
}
Expression parsedExpression = drlxParseSuccess.getExpr();
Expression newExpr = generateLambdaWithoutParameters(singleResult.getUsedDeclarations(), parsedExpression, singleResult.isSkipThisAsParam(), ofNullable(singleResult.getPatternType()), context);
if (newExpr instanceof LambdaExpr) {
context.getPackageModel().registerLambdaReturnType((LambdaExpr) newExpr, singleResult.getExprType());
}
addArgumentWithPreexistingCheck(fromCall, singleResult.getUsedDeclarations());
return newExpr;
});
}
return null;
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class SingleDrlxParseSuccess method combineWith.
@Override
public DrlxParseResult combineWith(DrlxParseResult other, BinaryExpr.Operator operator) {
if (!other.isSuccess()) {
return other;
}
SingleDrlxParseSuccess otherDrlx = (SingleDrlxParseSuccess) other;
Collection<String> newUsedDeclarations = new LinkedHashSet<>();
newUsedDeclarations.addAll(this.usedDeclarations);
newUsedDeclarations.addAll(otherDrlx.usedDeclarations);
Collection<String> newUsedDeclarationsOnLeft = null;
if (this.usedDeclarationsOnLeft != null && otherDrlx.usedDeclarationsOnLeft != null) {
newUsedDeclarationsOnLeft = new LinkedHashSet<>();
newUsedDeclarationsOnLeft.addAll(this.usedDeclarationsOnLeft);
newUsedDeclarationsOnLeft.addAll(otherDrlx.usedDeclarationsOnLeft);
}
Set<String> newReactOnProperties = new HashSet<>();
newReactOnProperties.addAll(this.reactOnProperties);
newReactOnProperties.addAll(otherDrlx.reactOnProperties);
List<Expression> newNullSafeExpressions = new ArrayList<>();
if (operator == BinaryExpr.Operator.OR) {
// NullSafeExpressions are combined here because the order is complex
this.expr = combinePredicatesWithAnd(this.expr, this.nullSafeExpressions);
otherDrlx.expr = combinePredicatesWithAnd(otherDrlx.expr, otherDrlx.nullSafeExpressions);
// Also combine implicitCast earlier than null-check
this.expr = combinePredicatesWithAnd(this.expr, StreamUtils.optionalToList(this.implicitCastExpression));
otherDrlx.expr = combinePredicatesWithAnd(otherDrlx.expr, StreamUtils.optionalToList(otherDrlx.implicitCastExpression));
} else {
// NullSafeExpressions will be added by PatternDSL.addNullSafeExpr
newNullSafeExpressions.addAll(this.nullSafeExpressions);
newNullSafeExpressions.addAll(otherDrlx.nullSafeExpressions);
}
return new SingleDrlxParseSuccess(patternType, patternBinding, new EnclosedExpr(new BinaryExpr(expr, otherDrlx.expr, operator)), exprType).setDecodeConstraintType(Index.ConstraintType.UNKNOWN).setUsedDeclarations(newUsedDeclarations).setUsedDeclarationsOnLeft(newUsedDeclarationsOnLeft).setUnification(this.isUnification() || otherDrlx.isUnification()).setCombined(true).setReactOnProperties(newReactOnProperties).setBetaConstraint(betaConstraint).setLeft(new TypedExpression(this.expr, left != null ? left.getType() : boolean.class)).setRight(new TypedExpression(otherDrlx.expr, right != null ? right.getType() : boolean.class)).setBoundExpr(left).setIsPredicate(this.isPredicate && otherDrlx.isPredicate).setNullSafeExpressions(newNullSafeExpressions).setExprBinding(// only left exprBinding
this.exprBinding);
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class AbstractExpressionBuilder method processExpression.
public void processExpression(MultipleDrlxParseSuccess drlxParseResult) {
if (drlxParseResult.isPredicate()) {
Expression dslExpr = buildExpressionWithIndexing(drlxParseResult);
context.addExpression(dslExpr);
}
}
Aggregations