Search in sources :

Example 1 with BindingDescr

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());
}
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 2 with BindingDescr

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());
}
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 3 with BindingDescr

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());
}
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 4 with BindingDescr

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());
}
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 5 with BindingDescr

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();
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) BindingDescr(org.drools.drl.ast.descr.BindingDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) Cheese(org.drools.mvel.compiler.Cheese) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) EvalCondition(org.drools.core.rule.EvalCondition) CompiledInvoker(org.drools.core.spi.CompiledInvoker) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) EvalDescr(org.drools.drl.ast.descr.EvalDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

BindingDescr (org.drools.drl.ast.descr.BindingDescr)42 ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)17 Test (org.junit.Test)17 BaseDescr (org.drools.drl.ast.descr.BaseDescr)13 PatternDescr (org.drools.drl.ast.descr.PatternDescr)11 RuleDescr (org.drools.drl.ast.descr.RuleDescr)11 AtomicExprDescr (org.drools.drl.ast.descr.AtomicExprDescr)10 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)10 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)9 AndDescr (org.drools.drl.ast.descr.AndDescr)8 Cheese (org.drools.mvel.compiler.Cheese)8 PackageDescr (org.drools.drl.ast.descr.PackageDescr)7 RelationalExprDescr (org.drools.drl.ast.descr.RelationalExprDescr)7 DumperContext (org.drools.compiler.lang.DumperContext)6 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)6 ClassObjectType (org.drools.core.base.ClassObjectType)5 Declaration (org.drools.core.rule.Declaration)5 TypeDeclaration (org.drools.core.rule.TypeDeclaration)5 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 PredicateConstraint (org.drools.core.rule.PredicateConstraint)4