Search in sources :

Example 1 with ConstraintConnectiveDescr

use of org.drools.drl.ast.descr.ConstraintConnectiveDescr 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 ConstraintConnectiveDescr

use of org.drools.drl.ast.descr.ConstraintConnectiveDescr 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 ConstraintConnectiveDescr

use of org.drools.drl.ast.descr.ConstraintConnectiveDescr 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 ConstraintConnectiveDescr

use of org.drools.drl.ast.descr.ConstraintConnectiveDescr 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 ConstraintConnectiveDescr

use of org.drools.drl.ast.descr.ConstraintConnectiveDescr 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

ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)60 Test (org.junit.Test)29 BaseDescr (org.drools.drl.ast.descr.BaseDescr)23 BindingDescr (org.drools.drl.ast.descr.BindingDescr)17 AtomicExprDescr (org.drools.drl.ast.descr.AtomicExprDescr)15 RelationalExprDescr (org.drools.drl.ast.descr.RelationalExprDescr)12 AnnotationDescr (org.drools.drl.ast.descr.AnnotationDescr)8 DumperContext (org.drools.compiler.lang.DumperContext)7 DrlExprParser (org.drools.drl.parser.DrlExprParser)6 PredicateConstraint (org.drools.core.rule.PredicateConstraint)4 NegConstraint (org.drools.core.rule.constraint.NegConstraint)4 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)4 Constraint (org.drools.core.spi.Constraint)4 ArrayList (java.util.ArrayList)3 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)3 Declaration (org.drools.core.rule.Declaration)3 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)3 DroolsParserException (org.drools.drl.parser.DroolsParserException)3 ClassObjectType (org.drools.core.base.ClassObjectType)2 QueryArgument (org.drools.core.rule.QueryArgument)2