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());
}
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());
}
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);
}
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);
}
}
}
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());
}
Aggregations