Search in sources :

Example 11 with PatternDescr

use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.

the class RuleParserTest method testInOperator.

@Test
public void testInOperator() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "in_operator_test.drl");
    assertNotNull(rule);
    assertEqualsIgnoreWhitespace("consequence();", (String) rule.getConsequence());
    assertEquals("simple_rule", rule.getName());
    assertEquals(2, rule.getLhs().getDescrs().size());
    // The first pattern, with 2 restrictions on a single field (plus a
    // connective)
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Person", pattern.getObjectType());
    assertEquals(1, pattern.getConstraint().getDescrs().size());
    ExprConstraintDescr fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("age > 30 && < 40", fld.getExpression());
    // the second col, with 2 fields, the first with 2 restrictions, the
    // second field with one
    pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("Vehicle", pattern.getObjectType());
    assertEquals(2, pattern.getConstraint().getDescrs().size());
    fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("type in ( \"sedan\", \"wagon\" )", fld.getExpression());
    // now the second field
    fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(1);
    assertEquals("age < 3", fld.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 12 with PatternDescr

use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.

the class RuleParserTest method testSimpleRule.

@Test
public void testSimpleRule() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "simple_rule.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    assertEquals(22, rule.getConsequenceLine());
    assertEquals(2, rule.getConsequencePattern());
    final AndDescr lhs = rule.getLhs();
    assertNotNull(lhs);
    assertEquals(3, lhs.getDescrs().size());
    // 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());
    AndDescr fieldAnd = (AndDescr) first.getConstraint();
    ExprConstraintDescr constraint = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
    assertNotNull(constraint);
    assertEquals("a==3", constraint.getExpression());
    // Check second pattern
    final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("foo4", second.getIdentifier());
    assertEquals("Bar", second.getObjectType());
    // no constraints, only a binding
    fieldAnd = (AndDescr) second.getConstraint();
    assertEquals(1, fieldAnd.getDescrs().size());
    final ExprConstraintDescr binding = (ExprConstraintDescr) second.getConstraint().getDescrs().get(0);
    assertEquals("a4:a==4", binding.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.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 13 with PatternDescr

use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.

the class RuleParserTest method testCheckOrDescr.

@Test
public void testCheckOrDescr() throws Exception {
    final String text = "rule X when Person( eval( age == 25 ) || ( eval( name.equals( \"bob\" ) ) && eval( age == 30 ) ) ) then end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertEquals(1, pattern.getDescrs().size());
    assertEquals(pattern.getConstraint().getClass(), AndDescr.class);
    assertEquals(ExprConstraintDescr.class, pattern.getConstraint().getDescrs().get(0).getClass());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) Test(org.junit.Test)

Example 14 with PatternDescr

use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.

the class RuleParserTest method testOrCE.

@Test
public void testOrCE() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_ce.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(2, rule.getLhs().getDescrs().size());
    final PatternDescr person = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Person", person.getObjectType());
    assertEquals("$p", person.getIdentifier());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
    assertEquals(2, or.getDescrs().size());
    final PatternDescr cheese1 = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Cheese", cheese1.getObjectType());
    assertEquals("$c", cheese1.getIdentifier());
    final PatternDescr cheese2 = (PatternDescr) or.getDescrs().get(1);
    assertEquals("Cheese", cheese2.getObjectType());
    assertNull(cheese2.getIdentifier());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Example 15 with PatternDescr

use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.

the class RuleParserTest method testQualifiedClassname.

@Test
public void testQualifiedClassname() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "qualified_classname.drl");
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    final PatternDescr p = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("com.cheeseco.Cheese", p.getObjectType());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Aggregations

PatternDescr (org.drools.drl.ast.descr.PatternDescr)162 Test (org.junit.Test)131 RuleDescr (org.drools.drl.ast.descr.RuleDescr)103 PackageDescr (org.drools.drl.ast.descr.PackageDescr)84 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)65 AndDescr (org.drools.drl.ast.descr.AndDescr)61 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)18 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)18 FromDescr (org.drools.drl.ast.descr.FromDescr)16 BaseDescr (org.drools.drl.ast.descr.BaseDescr)15 NotDescr (org.drools.drl.ast.descr.NotDescr)14 Cheese (org.drools.mvel.compiler.Cheese)14 OrDescr (org.drools.drl.ast.descr.OrDescr)13 BindingDescr (org.drools.drl.ast.descr.BindingDescr)11 Element (org.w3c.dom.Element)9 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)8 InputStreamReader (java.io.InputStreamReader)7 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)7 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)7 MVELExprDescr (org.drools.drl.ast.descr.MVELExprDescr)7