Search in sources :

Example 1 with DumperContext

use of org.drools.compiler.lang.DumperContext in project drools by kiegroup.

the class DescrDumperTest method testDumpBindingsWithRestriction.

@Test
public void testDumpBindingsWithRestriction() throws Exception {
    String input = "$x : age > 10 && < 20 || > 30";
    String expected = "( age > 10 && age < 20 || age > 30 )";
    ConstraintConnectiveDescr descr = parse(input);
    DumperContext ctx = new DumperContext();
    String result = dumper.dump(descr, ctx);
    assertEquals(expected, result);
    assertEquals(1, ctx.getBindings().size());
    BindingDescr bind = ctx.getBindings().get(0);
    assertEquals("$x", bind.getVariable());
    assertEquals("age", bind.getExpression());
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) DumperContext(org.drools.compiler.lang.DumperContext) Test(org.junit.Test)

Example 2 with DumperContext

use of org.drools.compiler.lang.DumperContext in project drools by kiegroup.

the class DescrDumperTest method testDumpBindings2.

@Test
public void testDumpBindings2() throws Exception {
    String input = "( $a : a > $b : b[10].prop || 10 != 20 ) && $x : someMethod(10) == 20";
    String expected = "( a > b[10].prop || 10 != 20 ) && someMethod(10) == 20";
    ConstraintConnectiveDescr descr = parse(input);
    DumperContext ctx = new DumperContext();
    String result = dumper.dump(descr, ctx);
    assertEquals(expected, result);
    assertEquals(3, ctx.getBindings().size());
    BindingDescr bind = ctx.getBindings().get(0);
    assertEquals("$a", bind.getVariable());
    assertEquals("a", bind.getExpression());
    bind = ctx.getBindings().get(1);
    assertEquals("$b", bind.getVariable());
    assertEquals("b[10].prop", bind.getExpression());
    bind = ctx.getBindings().get(2);
    assertEquals("$x", bind.getVariable());
    assertEquals("someMethod(10)", bind.getExpression());
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) DumperContext(org.drools.compiler.lang.DumperContext) Test(org.junit.Test)

Example 3 with DumperContext

use of org.drools.compiler.lang.DumperContext in project drools by kiegroup.

the class PatternBuilder method build.

private void build(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, Pattern pattern, BaseDescr original, String expr) {
    ConstraintConnectiveDescr result = parseExpression(context, patternDescr, original, expr);
    if (result == null) {
        return;
    }
    result.copyLocation(original);
    DumperContext mvelCtx = new DumperContext().setRuleContext(context);
    List<Constraint> constraints = build(context, patternDescr, xpathStartDeclaration, pattern, result, mvelCtx);
    pattern.addConstraints(constraints);
}
Also used : 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) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) DumperContext(org.drools.compiler.lang.DumperContext)

Example 4 with DumperContext

use of org.drools.compiler.lang.DumperContext in project drools by kiegroup.

the class PatternBuilder method processConstraintsAndBinds.

/**
 * Process all constraints and bindings on this pattern
 */
private void processConstraintsAndBinds(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, Pattern pattern) {
    DumperContext mvelCtx = new DumperContext().setRuleContext(context);
    for (BaseDescr b : patternDescr.getDescrs()) {
        String expression;
        boolean isPositional = false;
        if (b instanceof BindingDescr) {
            BindingDescr bind = (BindingDescr) b;
            expression = bind.getVariable() + (bind.isUnification() ? " := " : " : ") + bind.getExpression();
        } else if (b instanceof ExprConstraintDescr) {
            ExprConstraintDescr descr = (ExprConstraintDescr) b;
            expression = descr.getExpression();
            isPositional = descr.getType() == ExprConstraintDescr.Type.POSITIONAL;
        } else {
            expression = b.getText();
        }
        ConstraintConnectiveDescr result = parseExpression(context, patternDescr, b, expression);
        if (result == null) {
            return;
        }
        result.setNegated(b.isNegated());
        isPositional &= !(result.getDescrs().size() == 1 && result.getDescrs().get(0) instanceof BindingDescr);
        if (isPositional) {
            processPositional(context, patternDescr, xpathStartDeclaration, pattern, (ExprConstraintDescr) b);
        } else {
            // need to build the actual constraint
            List<Constraint> constraints = build(context, patternDescr, xpathStartDeclaration, pattern, result, mvelCtx);
            pattern.addConstraints(constraints);
        }
    }
    TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
    if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
        for (String field : lookAheadFieldsOfIdentifier(context.getRuleDescr(), patternDescr)) {
            addFieldToPatternWatchlist(pattern, typeDeclaration, field);
        }
    }
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) 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) BaseDescr(org.drools.drl.ast.descr.BaseDescr) DumperContext(org.drools.compiler.lang.DumperContext) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr)

Example 5 with DumperContext

use of org.drools.compiler.lang.DumperContext in project drools by kiegroup.

the class DescrDumperTest method testDumpBindingsComplexOp2.

@Test
public void testDumpBindingsComplexOp2() throws Exception {
    String input = "$x : age not in (10, 20, $someVal)";
    String expected = "age != 10 && age != 20 && age != $someVal";
    ConstraintConnectiveDescr descr = parse(input);
    DumperContext ctx = new DumperContext();
    String result = dumper.dump(descr, ctx);
    assertEquals(expected, result);
    assertEquals(1, ctx.getBindings().size());
    BindingDescr bind = ctx.getBindings().get(0);
    assertEquals("$x", bind.getVariable());
    assertEquals("age", bind.getExpression());
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) DumperContext(org.drools.compiler.lang.DumperContext) Test(org.junit.Test)

Aggregations

DumperContext (org.drools.compiler.lang.DumperContext)8 ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)7 BindingDescr (org.drools.drl.ast.descr.BindingDescr)6 Test (org.junit.Test)5 PredicateConstraint (org.drools.core.rule.PredicateConstraint)2 NegConstraint (org.drools.core.rule.constraint.NegConstraint)2 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)2 Constraint (org.drools.core.spi.Constraint)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 BaseDescr (org.drools.drl.ast.descr.BaseDescr)1 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)1 DroolsParserException (org.drools.drl.parser.DroolsParserException)1