use of org.drools.mvel.parser.ast.expr.PointFreeExpr in project drools by kiegroup.
the class DroolsMvelParserTest method testDotFreeExprWithArgs.
@Test
public void testDotFreeExprWithArgs() {
String expr = "this after[5,8] $a";
Expression expression = parseExpression(parser, expr).getExpr();
assertTrue(expression instanceof PointFreeExpr);
assertFalse(((PointFreeExpr) expression).isNegated());
// please note the parsed expression once normalized would take the time unit for milliseconds.
assertEquals("this after[5ms,8ms] $a", printNode(expression));
}
use of org.drools.mvel.parser.ast.expr.PointFreeExpr in project drools by kiegroup.
the class DroolsMvelParserTest method testDotFreeExpr.
@Test
public void testDotFreeExpr() {
String expr = "this after $a";
Expression expression = parseExpression(parser, expr).getExpr();
assertTrue(expression instanceof PointFreeExpr);
assertEquals(expr, printNode(expression));
}
use of org.drools.mvel.parser.ast.expr.PointFreeExpr in project drools by kiegroup.
the class ConstraintParser method parseBinaryExpr.
private DrlxParseResult parseBinaryExpr(BinaryExpr binaryExpr, Class<?> patternType, String bindingId, ConstraintExpression constraint, Expression drlxExpr, boolean hasBind, boolean isPositional, boolean isEnclosed) {
BinaryExpr.Operator operator = binaryExpr.getOperator();
boolean isOrBinary = operator == BinaryExpr.Operator.OR;
if (isLogicalOperator(operator) && isCombinable(binaryExpr)) {
DrlxParseResult leftResult = compileToJavaRecursive(patternType, bindingId, constraint, binaryExpr.getLeft(), hasBind, isPositional);
Expression rightExpr = binaryExpr.getRight() instanceof HalfPointFreeExpr ? completeHalfExpr(((PointFreeExpr) binaryExpr.getLeft()).getLeft(), (HalfPointFreeExpr) binaryExpr.getRight()) : binaryExpr.getRight();
DrlxParseResult rightResult = compileToJavaRecursive(patternType, bindingId, constraint, rightExpr, hasBind, isPositional);
return isMultipleResult(leftResult, operator, rightResult) ? createMultipleDrlxParseSuccess(operator, (DrlxParseSuccess) leftResult, (DrlxParseSuccess) rightResult) : leftResult.combineWith(rightResult, operator);
}
final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
final ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, isPositional, expressionTyperContext);
TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(binaryExpr.getLeft());
Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
if (!optLeft.isPresent()) {
return new DrlxParseFail();
}
TypedExpression left = optLeft.get();
List<String> usedDeclarationsOnLeft = hasBind ? new ArrayList<>(expressionTyperContext.getUsedDeclarations()) : null;
List<Expression> leftPrefixExpressions = new ArrayList<>();
if (isOrBinary) {
leftPrefixExpressions.addAll(expressionTyperContext.getNullSafeExpressions());
expressionTyperContext.getNullSafeExpressions().clear();
leftPrefixExpressions.addAll(expressionTyperContext.getPrefixExpresssions());
expressionTyperContext.getPrefixExpresssions().clear();
}
List<Expression> rightPrefixExpresssions = new ArrayList<>();
TypedExpression right;
if (constraint.isNameClashingUnification()) {
String name = constraint.getUnificationField();
right = new TypedExpression(new NameExpr(name), left.getType());
expressionTyperContext.addUsedDeclarations(name);
} else {
TypedExpressionResult rightExpressionResult = expressionTyper.toTypedExpression(binaryExpr.getRight());
Optional<TypedExpression> optRight = rightExpressionResult.getTypedExpression();
if (!optRight.isPresent()) {
return new DrlxParseFail(new ParseExpressionErrorResult(drlxExpr));
}
right = optRight.get();
if (isOrBinary) {
rightPrefixExpresssions.addAll(expressionTyperContext.getNullSafeExpressions());
expressionTyperContext.getNullSafeExpressions().clear();
rightPrefixExpresssions.addAll(expressionTyperContext.getPrefixExpresssions());
expressionTyperContext.getPrefixExpresssions().clear();
}
}
boolean equalityExpr = operator == EQUALS || operator == NOT_EQUALS;
CoercedExpression.CoercedExpressionResult coerced;
try {
coerced = new CoercedExpression(left, right, equalityExpr).coerce();
} catch (CoercedExpression.CoercedExpressionException e) {
return new DrlxParseFail(e.getInvalidExpressionErrorResult());
}
left = coerced.getCoercedLeft();
right = getCoercedRightExpression(packageModel, coerced);
Expression combo;
boolean arithmeticExpr = asList(PLUS, MINUS, MULTIPLY, DIVIDE, REMAINDER).contains(operator);
boolean isBetaConstraint = right.getExpression() != null && hasNonGlobalDeclaration(expressionTyperContext);
boolean requiresSplit = operator == BinaryExpr.Operator.AND && binaryExpr.getRight() instanceof HalfBinaryExpr && !isBetaConstraint;
if (equalityExpr) {
combo = getEqualityExpression(left, right, operator).expression;
} else if (arithmeticExpr && (left.isBigDecimal())) {
ConstraintCompiler constraintCompiler = createConstraintCompiler(this.context, of(patternType));
CompiledExpressionResult compiledExpressionResult = constraintCompiler.compileExpression(binaryExpr);
combo = compiledExpressionResult.getExpression();
} else {
if (left.getExpression() == null || right.getExpression() == null) {
return new DrlxParseFail(new ParseExpressionErrorResult(drlxExpr));
}
// This special comparisons
SpecialComparisonResult specialComparisonResult = handleSpecialComparisonCases(expressionTyper, operator, left, right);
combo = specialComparisonResult.expression;
left = requiresSplit ? left : specialComparisonResult.coercedLeft;
right = requiresSplit ? right : specialComparisonResult.coercedRight;
}
if (isOrBinary) {
// NullSafeExpressions are combined here because the order is complex
combo = combineExpressions(leftPrefixExpressions, rightPrefixExpresssions, combo);
} else {
// NullSafeExpressions will be added later by PatternDSL.addNullSafeExpr() which will be separated AlphaNodes
combo = combineExpressions(leftTypedExpressionResult, combo);
}
boolean isPredicate = isPredicateBooleanExpression(binaryExpr);
if (isEnclosed && !isPredicate) {
combo = new EnclosedExpr(combo);
}
Index.ConstraintType constraintType = DrlxParseUtil.toConstraintType(operator);
if (isForallSelfJoinConstraint(left, right, constraintType)) {
constraintType = Index.ConstraintType.FORALL_SELF_JOIN;
}
return new SingleDrlxParseSuccess(patternType, bindingId, combo, isBooleanOperator(operator) ? boolean.class : left.getType()).setDecodeConstraintType(constraintType).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setUsedDeclarationsOnLeft(usedDeclarationsOnLeft).setUnification(constraint.isUnification()).setReactOnProperties(expressionTyperContext.getReactOnProperties()).setLeft(left).setRight(right).setBetaConstraint(isBetaConstraint).setRequiresSplit(requiresSplit).setIsPredicate(isPredicate).setImplicitCastExpression(leftTypedExpressionResult.getInlineCastExpression()).setNullSafeExpressions(// This would be empty if NullSafeExpressions were combined earlier
leftTypedExpressionResult.getNullSafeExpressions());
}
use of org.drools.mvel.parser.ast.expr.PointFreeExpr in project drools by kiegroup.
the class DroolsMvelParserTest method testMvelSquareOperator.
private void testMvelSquareOperator(String wholeExpression, String operator, String left, String right, boolean isNegated) {
String expr = wholeExpression;
Expression expression = parseExpression(parser, expr).getExpr();
assertThat(expression, instanceOf(PointFreeExpr.class));
assertEquals(wholeExpression, printNode(expression));
PointFreeExpr e = (PointFreeExpr) expression;
assertEquals(operator, e.getOperator().asString());
assertEquals(left, toString(e.getLeft()));
assertEquals(right, toString(e.getRight().get(0)));
assertEquals(isNegated, e.isNegated());
}
use of org.drools.mvel.parser.ast.expr.PointFreeExpr in project drools by kiegroup.
the class DroolsMvelParserTest method testDotFreeExprWithArgsNegated.
@Test
public void testDotFreeExprWithArgsNegated() {
String expr = "this not after[5,8] $a";
Expression expression = parseExpression(parser, expr).getExpr();
assertThat(expression, instanceOf(PointFreeExpr.class));
assertTrue(((PointFreeExpr) expression).isNegated());
// please note the parsed expression once normalized would take the time unit for milliseconds.
assertEquals("this not after[5ms,8ms] $a", printNode(expression));
}
Aggregations