Search in sources :

Example 26 with AtomicExprDescr

use of org.drools.compiler.lang.descr.AtomicExprDescr in project drools by kiegroup.

the class DRLExprParserTest method testSimpleExpression.

@Test
public void testSimpleExpression() throws Exception {
    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.compiler.lang.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) RelationalExprDescr(org.drools.compiler.lang.descr.RelationalExprDescr) Test(org.junit.Test)

Example 27 with AtomicExprDescr

use of org.drools.compiler.lang.descr.AtomicExprDescr in project drools by kiegroup.

the class MVELDumperTest method testProcessNullSafeDereferencing.

@Test
public void testProcessNullSafeDereferencing() throws Exception {
    String expr = "field1!.field2";
    String expectedNullCheck = "field1 != null";
    String expectedExpr = "field1.field2";
    AtomicExprDescr atomicExpr = new AtomicExprDescr(expr);
    ConstraintConnectiveDescr ccd = new ConstraintConnectiveDescr();
    String[] nullCheckAndExpr = dumper.processImplicitConstraints(expr, atomicExpr, ccd, ccd.getDescrs().indexOf(atomicExpr), null);
    assertEquals(expectedNullCheck, ccd.getDescrs().get(0).toString());
    assertEquals(expectedExpr, nullCheckAndExpr[1]);
    assertEquals(expectedExpr, atomicExpr.getRewrittenExpression());
    expr = "field1!.field2!.field3";
    String expectedNullCheck1 = "field1 != null";
    String expectedNullCheck2 = "field1.field2 != null";
    expectedExpr = "field1.field2.field3";
    atomicExpr = new AtomicExprDescr(expr);
    ccd = new ConstraintConnectiveDescr();
    nullCheckAndExpr = dumper.processImplicitConstraints(expr, atomicExpr, ccd, ccd.getDescrs().indexOf(atomicExpr), null);
    assertEquals(expectedNullCheck1, ccd.getDescrs().get(0).toString());
    assertEquals(expectedNullCheck2, ccd.getDescrs().get(1).toString());
    assertEquals(expectedExpr, nullCheckAndExpr[1]);
    assertEquals(expectedExpr, atomicExpr.getRewrittenExpression());
}
Also used : AtomicExprDescr(org.drools.compiler.lang.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) Test(org.junit.Test)

Example 28 with AtomicExprDescr

use of org.drools.compiler.lang.descr.AtomicExprDescr in project drools by kiegroup.

the class MVELDumperTest 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.compiler.lang.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) Test(org.junit.Test)

Example 29 with AtomicExprDescr

use of org.drools.compiler.lang.descr.AtomicExprDescr in project drools by kiegroup.

the class MVELDumperTest method testProcessImplicitConstraints.

@Test
public void testProcessImplicitConstraints() throws Exception {
    String expr = "field1#Class!.field2";
    String expectedConstraints = "field1 instanceof Class";
    String expectedExpr = "((Class)field1).field2";
    AtomicExprDescr atomicExpr = new AtomicExprDescr(expr);
    ConstraintConnectiveDescr ccd = new ConstraintConnectiveDescr();
    String[] constraintsAndExpr = dumper.processImplicitConstraints(expr, atomicExpr, ccd, ccd.getDescrs().indexOf(atomicExpr), null);
    assertEquals(expectedConstraints, ccd.getDescrs().get(0).toString());
    assertEquals(expectedExpr, constraintsAndExpr[1]);
    assertEquals(expectedExpr, atomicExpr.getRewrittenExpression());
    expr = "field1!.field2#Class.field3";
    String expectedConstraints1 = "field1 != null";
    String expectedConstraints2 = "field1.field2 instanceof Class";
    expectedExpr = "((Class)field1.field2).field3";
    atomicExpr = new AtomicExprDescr(expr);
    ccd = new ConstraintConnectiveDescr();
    constraintsAndExpr = dumper.processImplicitConstraints(expr, atomicExpr, ccd, ccd.getDescrs().indexOf(atomicExpr), null);
    assertEquals(expectedConstraints1, ccd.getDescrs().get(0).toString());
    assertEquals(expectedConstraints2, ccd.getDescrs().get(1).toString());
    assertEquals(expectedExpr, constraintsAndExpr[1]);
    assertEquals(expectedExpr, atomicExpr.getRewrittenExpression());
    expr = "field1#Class.field2!.field3";
    expectedConstraints1 = "field1 instanceof Class";
    expectedConstraints2 = "((Class)field1).field2 != null";
    expectedExpr = "((Class)field1).field2.field3";
    atomicExpr = new AtomicExprDescr(expr);
    ccd = new ConstraintConnectiveDescr();
    constraintsAndExpr = dumper.processImplicitConstraints(expr, atomicExpr, ccd, ccd.getDescrs().indexOf(atomicExpr), null);
    assertEquals(expectedConstraints1, ccd.getDescrs().get(0).toString());
    assertEquals(expectedConstraints2, ccd.getDescrs().get(1).toString());
    assertEquals(expectedExpr, constraintsAndExpr[1]);
    assertEquals(expectedExpr, atomicExpr.getRewrittenExpression());
}
Also used : AtomicExprDescr(org.drools.compiler.lang.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) Test(org.junit.Test)

Aggregations

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