Search in sources :

Example 11 with ExprConstraintDescr

use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.

the class RuleParserTest method testLhsSemicolonDelim.

@Test
public void testLhsSemicolonDelim() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "lhs_semicolon_delim.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    final AndDescr lhs = rule.getLhs();
    assertNotNull(lhs);
    assertEquals(3, lhs.getDescrs().size());
    // System.err.println( lhs.getDescrs() );
    // Check first pattern
    final PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("foo3", first.getIdentifier());
    assertEquals("Bar", first.getObjectType());
    assertEquals(1, first.getConstraint().getDescrs().size());
    // LiteralDescr constraint = (LiteralDescr) first.getDescrs().get( 0 );
    AndDescr and = (AndDescr) first.getConstraint();
    ExprConstraintDescr fld = (ExprConstraintDescr) and.getDescrs().get(0);
    assertNotNull(fld);
    assertEquals("a==3", fld.getExpression());
    // Check second pattern
    final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("foo4", second.getIdentifier());
    assertEquals("Bar", second.getObjectType());
    assertEquals(1, second.getDescrs().size());
    final ExprConstraintDescr fieldBindingDescr = (ExprConstraintDescr) second.getDescrs().get(0);
    assertEquals("a4:a==4", fieldBindingDescr.getExpression());
    // Check third pattern
    final PatternDescr third = (PatternDescr) lhs.getDescrs().get(2);
    assertNull(third.getIdentifier());
    assertEquals("Baz", third.getObjectType());
    assertEqualsIgnoreWhitespace("if ( a == b ) { " + "  assert( foo3 );" + "} else {" + "  retract( foo4 );" + "}" + "  System.out.println( a4 );", (String) rule.getConsequence());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 12 with ExprConstraintDescr

use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.

the class RuleParserTest method testWithPredicate.

@Test
public void testWithPredicate() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "with_predicate.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final PatternDescr col = (PatternDescr) rule.getLhs().getDescrs().get(0);
    AndDescr and = (AndDescr) col.getConstraint();
    assertEquals(2, and.getDescrs().size());
    final ExprConstraintDescr field = (ExprConstraintDescr) col.getDescrs().get(0);
    final ExprConstraintDescr pred = (ExprConstraintDescr) and.getDescrs().get(1);
    assertEquals("$age2:age", field.getExpression());
    assertEqualsIgnoreWhitespace("$age2 == $age1+2", pred.getExpression());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 13 with ExprConstraintDescr

use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.

the class RuleParserTest method testUnificationBinding.

@Test
public void testUnificationBinding() throws Exception {
    final String text = "rule X when $p := Person( $name := name, $loc : location ) then end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertEquals("$p", pattern.getIdentifier());
    assertTrue(pattern.isUnification());
    assertEquals(2, pattern.getDescrs().size());
    ExprConstraintDescr bindingDescr = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("$name := name", bindingDescr.getExpression());
    bindingDescr = (ExprConstraintDescr) pattern.getDescrs().get(1);
    assertEquals("$loc : location", bindingDescr.getExpression());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 14 with ExprConstraintDescr

use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.

the class RuleParserTest method testConstraintAndConnective.

@Test
public void testConstraintAndConnective() throws Exception {
    final String text = "rule X when Person( age < 42 && location==\"atlanta\") then end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertEquals(1, pattern.getDescrs().size());
    ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("age < 42 && location==\"atlanta\"", fcd.getExpression());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 15 with ExprConstraintDescr

use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.

the class RuleParserTest method testInfinityLiteral.

@Test
public void testInfinityLiteral() throws Exception {
    final String text = "rule \"infinity\"\n" + "when\n" + "    StockTick( this after[-*,*] $another )\n" + "then\n" + "end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertEquals("StockTick", pattern.getObjectType());
    ExprConstraintDescr constr = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("this after[-*,*] $another", constr.getText());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)65 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)57 Test (org.junit.Test)49 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)37 AndDescr (org.drools.compiler.lang.descr.AndDescr)27 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)23 Cheese (org.drools.compiler.Cheese)9 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)9 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)5 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)5 List (java.util.List)4 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)4 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)4 Element (org.w3c.dom.Element)4 ArrayList (java.util.ArrayList)3 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)3 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)3 QueryDescr (org.drools.compiler.lang.descr.QueryDescr)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 Declaration (org.drools.core.rule.Declaration)3