use of org.drools.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class DRLExprParserTest method testDeepBinding.
@Test
public void testDeepBinding() throws Exception {
String source = "($a : a > $b : b[10].prop || 10 != 20) && $x : someMethod(10) == 20";
ConstraintConnectiveDescr result = parser.parse(source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals(ConnectiveType.AND, result.getConnective());
assertEquals(2, result.getDescrs().size());
ConstraintConnectiveDescr or = (ConstraintConnectiveDescr) result.getDescrs().get(0);
assertEquals(ConnectiveType.OR, or.getConnective());
assertEquals(2, or.getDescrs().size());
RelationalExprDescr expr = (RelationalExprDescr) or.getDescrs().get(0);
assertEquals(">", expr.getOperator());
BindingDescr leftBind = (BindingDescr) expr.getLeft();
BindingDescr rightBind = (BindingDescr) expr.getRight();
assertEquals("$a", leftBind.getVariable());
assertEquals("a", leftBind.getExpression());
assertEquals("$b", rightBind.getVariable());
assertEquals("b[10].prop", rightBind.getExpression());
expr = (RelationalExprDescr) or.getDescrs().get(1);
assertEquals("!=", expr.getOperator());
AtomicExprDescr leftExpr = (AtomicExprDescr) expr.getLeft();
AtomicExprDescr rightExpr = (AtomicExprDescr) expr.getRight();
assertEquals("10", leftExpr.getExpression());
assertEquals("20", rightExpr.getExpression());
expr = (RelationalExprDescr) result.getDescrs().get(1);
assertEquals("==", expr.getOperator());
leftBind = (BindingDescr) expr.getLeft();
rightExpr = (AtomicExprDescr) expr.getRight();
assertEquals("$x", leftBind.getVariable());
assertEquals("someMethod(10)", leftBind.getExpression());
assertEquals("20", rightExpr.getExpression());
}
use of org.drools.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class DRLExprParserTest method testBindingWithRestrictions.
@Test
public void testBindingWithRestrictions() throws Exception {
String source = "$x : property > value && < 20";
ConstraintConnectiveDescr result = parser.parse(source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals(ConnectiveType.AND, result.getConnective());
assertEquals(2, 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());
rel = (RelationalExprDescr) result.getDescrs().get(1);
assertEquals("<", rel.getOperator());
AtomicExprDescr left = (AtomicExprDescr) rel.getLeft();
assertEquals("property", left.getExpression());
right = (AtomicExprDescr) rel.getRight();
assertEquals("20", right.getExpression());
}
use of org.drools.drl.ast.descr.BindingDescr 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.drl.ast.descr.BindingDescr 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.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testEval.
@Test
public void testEval() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
pattern.addConstraint(fieldBindingDescr);
fieldBindingDescr = new BindingDescr("y", "price");
pattern.addConstraint(fieldBindingDescr);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
final EvalDescr evalDescr = new EvalDescr("( ( Integer )map.get( new Integer(x) ) ).intValue() == y");
lhs.addDescr(evalDescr);
ruleDescr.setConsequence("update(stilton);");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
final RuleImpl rule = pkg.getRule("rule-1");
final EvalCondition eval = (EvalCondition) rule.getLhs().getChildren().get(1);
final CompiledInvoker invoker = (CompiledInvoker) eval.getEvalExpression();
String s = invoker.getMethodBytecode();
}
Aggregations