Search in sources :

Example 1 with AtomicExprDescr

use of org.drools.drl.ast.descr.AtomicExprDescr 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());
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) AtomicExprDescr(org.drools.drl.ast.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) RelationalExprDescr(org.drools.drl.ast.descr.RelationalExprDescr) Test(org.junit.Test)

Example 2 with AtomicExprDescr

use of org.drools.drl.ast.descr.AtomicExprDescr 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());
}
Also used : AtomicExprDescr(org.drools.drl.ast.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) RelationalExprDescr(org.drools.drl.ast.descr.RelationalExprDescr) Test(org.junit.Test)

Example 3 with AtomicExprDescr

use of org.drools.drl.ast.descr.AtomicExprDescr 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());
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) AtomicExprDescr(org.drools.drl.ast.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) RelationalExprDescr(org.drools.drl.ast.descr.RelationalExprDescr) Test(org.junit.Test)

Example 4 with AtomicExprDescr

use of org.drools.drl.ast.descr.AtomicExprDescr 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());
}
Also used : AtomicExprDescr(org.drools.drl.ast.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) RelationalExprDescr(org.drools.drl.ast.descr.RelationalExprDescr) Test(org.junit.Test)

Example 5 with AtomicExprDescr

use of org.drools.drl.ast.descr.AtomicExprDescr in project drools by kiegroup.

the class DescrDumperTest method testProcessInlineCast.

@Test
public void testProcessInlineCast() throws Exception {
    String expr = "field1#Class.field2";
    String expectedInstanceof = "field1 instanceof Class";
    String expectedcasted = "((Class)field1).field2";
    AtomicExprDescr atomicExpr = new AtomicExprDescr(expr);
    ConstraintConnectiveDescr ccd = new ConstraintConnectiveDescr();
    ccd.addDescr(atomicExpr);
    String[] instanceofAndCastedExpr = dumper.processImplicitConstraints(expr, atomicExpr, ccd, ccd.getDescrs().indexOf(atomicExpr), null);
    assertEquals(2, ccd.getDescrs().size());
    assertEquals(expectedInstanceof, ccd.getDescrs().get(0).toString());
    assertEquals(expectedcasted, atomicExpr.getRewrittenExpression());
    expr = "field1#Class1.field2#Class2.field3";
    String expectedInstanceof1 = "field1 instanceof Class1";
    String expectedInstanceof2 = "((Class1)field1).field2 instanceof Class2";
    expectedcasted = "((Class2)((Class1)field1).field2).field3";
    atomicExpr = new AtomicExprDescr(expr);
    ccd = new ConstraintConnectiveDescr();
    instanceofAndCastedExpr = dumper.processImplicitConstraints(expr, atomicExpr, ccd, ccd.getDescrs().indexOf(atomicExpr), null);
    assertEquals(expectedInstanceof1, ccd.getDescrs().get(0).toString());
    assertEquals(expectedInstanceof2, ccd.getDescrs().get(1).toString());
    assertEquals(expectedcasted, instanceofAndCastedExpr[1]);
    assertEquals(expectedcasted, atomicExpr.getRewrittenExpression());
}
Also used : AtomicExprDescr(org.drools.drl.ast.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) Test(org.junit.Test)

Aggregations

AtomicExprDescr (org.drools.drl.ast.descr.AtomicExprDescr)29 BaseDescr (org.drools.drl.ast.descr.BaseDescr)17 RelationalExprDescr (org.drools.drl.ast.descr.RelationalExprDescr)16 ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)15 BindingDescr (org.drools.drl.ast.descr.BindingDescr)10 Test (org.junit.Test)10 Matcher (java.util.regex.Matcher)1 PredicateConstraint (org.drools.core.rule.PredicateConstraint)1 NegConstraint (org.drools.core.rule.constraint.NegConstraint)1 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)1 Constraint (org.drools.core.spi.Constraint)1 PredicateDescr (org.drools.drl.ast.descr.PredicateDescr)1