use of org.drools.drl.ast.descr.RelationalExprDescr in project drools by kiegroup.
the class DRLExprParserTest method testDeepBinding.
@Test
public void testDeepBinding() throws Exception {
String source = "($a : a > $b : b[10].prop || 10 != 20) && $x : someMethod(10) == 20";
ConstraintConnectiveDescr result = parser.parse(source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals(ConnectiveType.AND, result.getConnective());
assertEquals(2, result.getDescrs().size());
ConstraintConnectiveDescr or = (ConstraintConnectiveDescr) result.getDescrs().get(0);
assertEquals(ConnectiveType.OR, or.getConnective());
assertEquals(2, or.getDescrs().size());
RelationalExprDescr expr = (RelationalExprDescr) or.getDescrs().get(0);
assertEquals(">", expr.getOperator());
BindingDescr leftBind = (BindingDescr) expr.getLeft();
BindingDescr rightBind = (BindingDescr) expr.getRight();
assertEquals("$a", leftBind.getVariable());
assertEquals("a", leftBind.getExpression());
assertEquals("$b", rightBind.getVariable());
assertEquals("b[10].prop", rightBind.getExpression());
expr = (RelationalExprDescr) or.getDescrs().get(1);
assertEquals("!=", expr.getOperator());
AtomicExprDescr leftExpr = (AtomicExprDescr) expr.getLeft();
AtomicExprDescr rightExpr = (AtomicExprDescr) expr.getRight();
assertEquals("10", leftExpr.getExpression());
assertEquals("20", rightExpr.getExpression());
expr = (RelationalExprDescr) result.getDescrs().get(1);
assertEquals("==", expr.getOperator());
leftBind = (BindingDescr) expr.getLeft();
rightExpr = (AtomicExprDescr) expr.getRight();
assertEquals("$x", leftBind.getVariable());
assertEquals("someMethod(10)", leftBind.getExpression());
assertEquals("20", rightExpr.getExpression());
}
use of org.drools.drl.ast.descr.RelationalExprDescr in project drools by kiegroup.
the class DRLExprParserTest method testNestedExpression.
@Test(timeout = 10000L)
public void testNestedExpression() throws Exception {
// DROOLS-982
String source = "(((((((((((((((((((((((((((((((((((((((((((((((((( a > b ))))))))))))))))))))))))))))))))))))))))))))))))))";
ConstraintConnectiveDescr result = parser.parse(source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals(ConnectiveType.AND, result.getConnective());
assertEquals(1, result.getDescrs().size());
RelationalExprDescr expr = (RelationalExprDescr) result.getDescrs().get(0);
assertEquals(">", expr.getOperator());
AtomicExprDescr left = (AtomicExprDescr) expr.getLeft();
AtomicExprDescr right = (AtomicExprDescr) expr.getRight();
assertEquals("a", left.getExpression());
assertEquals("b", right.getExpression());
}
use of org.drools.drl.ast.descr.RelationalExprDescr in project drools by kiegroup.
the class DRLExprParserTest method testBindingWithRestrictions.
@Test
public void testBindingWithRestrictions() throws Exception {
String source = "$x : property > value && < 20";
ConstraintConnectiveDescr result = parser.parse(source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals(ConnectiveType.AND, result.getConnective());
assertEquals(2, result.getDescrs().size());
RelationalExprDescr rel = (RelationalExprDescr) result.getDescrs().get(0);
assertEquals(">", rel.getOperator());
BindingDescr bind = (BindingDescr) rel.getLeft();
assertEquals("$x", bind.getVariable());
assertEquals("property", bind.getExpression());
AtomicExprDescr right = (AtomicExprDescr) rel.getRight();
assertEquals("value", right.getExpression());
rel = (RelationalExprDescr) result.getDescrs().get(1);
assertEquals("<", rel.getOperator());
AtomicExprDescr left = (AtomicExprDescr) rel.getLeft();
assertEquals("property", left.getExpression());
right = (AtomicExprDescr) rel.getRight();
assertEquals("20", right.getExpression());
}
use of org.drools.drl.ast.descr.RelationalExprDescr in project drools by kiegroup.
the class DRLExprParserTest method testConnective2.
@Test
public void testConnective2() throws Exception {
String source = "(a > b || 10 != 20) && someMethod(10) == 20";
ConstraintConnectiveDescr result = parser.parse(source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals(ConnectiveType.AND, result.getConnective());
assertEquals(2, result.getDescrs().size());
ConstraintConnectiveDescr or = (ConstraintConnectiveDescr) result.getDescrs().get(0);
assertEquals(ConnectiveType.OR, or.getConnective());
assertEquals(2, or.getDescrs().size());
RelationalExprDescr expr = (RelationalExprDescr) or.getDescrs().get(0);
assertEquals(">", expr.getOperator());
AtomicExprDescr left = (AtomicExprDescr) expr.getLeft();
AtomicExprDescr right = (AtomicExprDescr) expr.getRight();
assertEquals("a", left.getExpression());
assertEquals("b", right.getExpression());
expr = (RelationalExprDescr) or.getDescrs().get(1);
assertEquals("!=", expr.getOperator());
left = (AtomicExprDescr) expr.getLeft();
right = (AtomicExprDescr) expr.getRight();
assertEquals("10", left.getExpression());
assertEquals("20", right.getExpression());
expr = (RelationalExprDescr) result.getDescrs().get(1);
assertEquals("==", expr.getOperator());
left = (AtomicExprDescr) expr.getLeft();
right = (AtomicExprDescr) expr.getRight();
assertEquals("someMethod(10)", left.getExpression());
assertEquals("20", right.getExpression());
}
use of org.drools.drl.ast.descr.RelationalExprDescr in project drools by kiegroup.
the class PatternBuilder method rewriteCompositeExpressions.
private String rewriteCompositeExpressions(RuleBuildContext context, Pattern pattern, ConstraintConnectiveDescr d) {
int i = 0;
StringBuilder sb = new StringBuilder();
for (BaseDescr subDescr : d.getDescrs()) {
if (subDescr instanceof BindingDescr) {
continue;
}
if (i++ > 0) {
sb.append(" ").append(d.getConnective().getConnective()).append(" ");
}
String normalizedExpr;
if (subDescr instanceof RelationalExprDescr && isSimpleExpr((RelationalExprDescr) subDescr)) {
RelationalExprDescr relDescr = (RelationalExprDescr) subDescr;
if (relDescr.getExpression() != null) {
normalizedExpr = normalizeExpression(context, pattern, relDescr, relDescr.getExpression());
} else {
i--;
normalizedExpr = "";
}
} else if (subDescr instanceof ConstraintConnectiveDescr) {
String rewrittenExpr = rewriteCompositeExpressions(context, pattern, (ConstraintConnectiveDescr) subDescr);
if (rewrittenExpr == null) {
return null;
}
normalizedExpr = "(" + rewrittenExpr + ")";
} else if (subDescr instanceof AtomicExprDescr) {
normalizedExpr = ((AtomicExprDescr) subDescr).getRewrittenExpression();
} else {
return null;
}
sb.append(normalizedExpr);
}
return sb.toString();
}
Aggregations