Search in sources :

Example 26 with AtomicExprDescr

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

the class DRLExprParserTest method testAndConnective.

@Test
public void testAndConnective() throws Exception {
    String source = "a > b && 10 != 20";
    ConstraintConnectiveDescr result = parser.parse(source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals(ConnectiveType.AND, result.getConnective());
    assertEquals(2, 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());
    expr = (RelationalExprDescr) result.getDescrs().get(1);
    assertEquals("!=", expr.getOperator());
    left = (AtomicExprDescr) expr.getLeft();
    right = (AtomicExprDescr) expr.getRight();
    assertEquals("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 27 with AtomicExprDescr

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

the class DRLExprParserTest method testBindingConstraint.

@Test
public void testBindingConstraint() throws Exception {
    String source = "$x : property > value";
    ConstraintConnectiveDescr result = parser.parse(source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals(ConnectiveType.AND, result.getConnective());
    assertEquals(1, 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());
}
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 28 with AtomicExprDescr

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

Example 29 with AtomicExprDescr

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

the class DescrDumper method processInlineCast.

private String[] processInlineCast(String expr, AtomicExprDescr atomicExpr, ConstraintConnectiveDescr ccd, DumperContext context, int sharpPos, int parentIdx, int childIdx) {
    // convert "field1#Class.field2" in ["field1 instanceof Class && ", "((Class)field1).field2"]
    String field1 = expr.substring(0, sharpPos).trim();
    int sharpPos2 = expr.indexOf('#', sharpPos + 1);
    String part2 = sharpPos2 < 0 ? expr.substring(sharpPos + 1).trim() : expr.substring(sharpPos + 1, sharpPos2).trim();
    String[] classAndField = splitInClassAndField(part2, context);
    BaseDescr desc = parentIdx >= 0 ? ccd.getDescrs().get(parentIdx) : null;
    if (classAndField == null) {
        return new String[] { "", expr };
    } else if (desc instanceof AtomicExprDescr && classAndField.length == 1) {
        return new String[] { "", field1 + " instanceof " + classAndField[0] };
    }
    String className = classAndField[0];
    String castedExpression = classAndField.length == 1 ? "((" + className + ")" + field1 + ")" : "((" + className + ")" + field1 + ")." + classAndField[1] + (sharpPos2 > 0 ? expr.substring(sharpPos2) : "");
    RelationalExprDescr check = new RelationalExprDescr("instanceof", false, null, new AtomicExprDescr(field1), new AtomicExprDescr(className));
    if (ccd.getConnective() == ConnectiveType.AND || ccd.getConnective() == ConnectiveType.INC_AND) {
        ccd.getDescrs().add(childIdx, check);
    } else {
        if (desc instanceof ConstraintConnectiveDescr) {
            ((ConstraintConnectiveDescr) desc).getDescrs().add(childIdx, check);
        } else {
            ConstraintConnectiveDescr localAnd = new ConstraintConnectiveDescr(ConnectiveType.AND);
            BaseDescr original = ccd.getDescrs().remove(parentIdx);
            localAnd.getDescrs().add(check);
            localAnd.getDescrs().add(original);
            ccd.getDescrs().add(parentIdx, localAnd);
        }
    }
    atomicExpr.setRewrittenExpression(castedExpression);
    String innerCheck = check + " && ";
    return new String[] { innerCheck, castedExpression };
}
Also used : BaseDescr(org.drools.drl.ast.descr.BaseDescr) AtomicExprDescr(org.drools.drl.ast.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) RelationalExprDescr(org.drools.drl.ast.descr.RelationalExprDescr)

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