Search in sources :

Example 6 with AtomicExprDescr

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

the class DescrDumperTest 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.drl.ast.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) Test(org.junit.Test)

Example 7 with AtomicExprDescr

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

the class PatternBuilder method processAtomicExpression.

protected Constraint processAtomicExpression(RuleBuildContext context, Pattern pattern, BaseDescr d, String expr, Map<String, OperatorDescr> aliases) {
    if (d instanceof AtomicExprDescr) {
        Matcher m = evalRegexp.matcher(((AtomicExprDescr) d).getExpression());
        if (m.find()) {
            // MVELDumper already stripped the eval
            // this will build the eval using the specified dialect
            PredicateDescr pdescr = new PredicateDescr(context.getRuleDescr().getResource(), expr);
            pdescr.copyLocation(d);
            return buildEval(context, pattern, pdescr, aliases, expr, true);
        }
    }
    return null;
}
Also used : Matcher(java.util.regex.Matcher) AtomicExprDescr(org.drools.drl.ast.descr.AtomicExprDescr) PredicateDescr(org.drools.drl.ast.descr.PredicateDescr)

Example 8 with AtomicExprDescr

use of org.drools.drl.ast.descr.AtomicExprDescr 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();
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) AtomicExprDescr(org.drools.drl.ast.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) RelationalExprDescr(org.drools.drl.ast.descr.RelationalExprDescr)

Example 9 with AtomicExprDescr

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

the class DescrDumper method processBinding.

private void processBinding(StringBuilder sbuilder, BindingDescr bind, ConstraintConnectiveDescr parent, boolean isInsideRelCons, DumperContext context) {
    String expr = bind.getExpression().trim();
    AtomicExprDescr atomicExpr = new AtomicExprDescr(expr);
    atomicExpr.setResource(parent.getResource());
    String[] constrAndExpr = processImplicitConstraints(expr, atomicExpr, parent, parent.getDescrs().indexOf(bind), context);
    if (isInsideRelCons) {
        sbuilder.append(constrAndExpr[0]).append(constrAndExpr[1]);
    } else if (constrAndExpr[0].length() > 4) {
        sbuilder.append(constrAndExpr[0], 0, constrAndExpr[0].length() - 4);
    }
    if (bind.getExpression().equals(bind.getBindingField())) {
        bind.setExpressionAndBindingField(constrAndExpr[1]);
    } else {
        bind.setExpression(constrAndExpr[1]);
    }
    context.addBinding(bind);
}
Also used : AtomicExprDescr(org.drools.drl.ast.descr.AtomicExprDescr)

Example 10 with AtomicExprDescr

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

the class DescrDumper method processNullSafeDereferencing.

private String[] processNullSafeDereferencing(String expr, AtomicExprDescr atomicExpr, ConstraintConnectiveDescr ccd, int nullSafePos, int parentIdx, int childIdx) {
    // convert "field1!.field2" in ["field1 != null && ", "field1.field2"]
    String field1 = expr.substring(0, nullSafePos).trim();
    expr = field1 + "." + expr.substring(nullSafePos + 2).trim();
    RelationalExprDescr check = new RelationalExprDescr("!=", false, null, new AtomicExprDescr(getPreconditionsToAppend(field1)), new AtomicExprDescr("null"));
    if (ccd.getConnective() == ConnectiveType.AND || ccd.getConnective() == ConnectiveType.INC_AND) {
        ccd.getDescrs().add(childIdx, check);
    } else {
        BaseDescr desc = ccd.getDescrs().get(parentIdx);
        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);
        }
    }
    String innerCheck = check + " && ";
    String[] nullCheckAndExpr = new String[] { innerCheck, expr };
    atomicExpr.setRewrittenExpression(expr);
    return nullCheckAndExpr;
}
Also used : AtomicExprDescr(org.drools.drl.ast.descr.AtomicExprDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) 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