Search in sources :

Example 71 with AndDescr

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

the class RuleParserTest method testRestrictionsMultiple.

@Test
public void testRestrictionsMultiple() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "restrictions_test.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    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());
    AndDescr and = (AndDescr) pattern.getConstraint();
    ExprConstraintDescr fld = (ExprConstraintDescr) and.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());
    and = (AndDescr) pattern.getConstraint();
    fld = (ExprConstraintDescr) and.getDescrs().get(0);
    assertEquals("type == \"sedan\" || == \"wagon\"", fld.getExpression());
    // now the second field
    fld = (ExprConstraintDescr) and.getDescrs().get(1);
    assertEquals("age < 3", fld.getExpression());
}
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 72 with AndDescr

use of org.drools.drl.ast.descr.AndDescr 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.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 73 with AndDescr

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

the class RuleParserTest method testLineNumberInAST.

@Test
public void testLineNumberInAST() throws Exception {
    // also see testSimpleExpander to see how this works with an expander
    // (should be the same).
    final RuleDescr rule = (RuleDescr) parseResource("rule", "simple_rule.drl");
    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());
    // Check second pattern
    final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("foo4", second.getIdentifier());
    assertEquals("Bar", second.getObjectType());
    final PatternDescr third = (PatternDescr) lhs.getDescrs().get(2);
    assertEquals("Baz", third.getObjectType());
    assertEquals(19, first.getLine());
    assertEquals(20, second.getLine());
    assertEquals(21, third.getLine());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) Test(org.junit.Test)

Example 74 with AndDescr

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

the class RuleParserTest method testNotExistWithBrackets.

@Test
public void testNotExistWithBrackets() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "not_exist_with_brackets.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    final AndDescr lhs = rule.getLhs();
    assertEquals(2, lhs.getDescrs().size());
    final NotDescr not = (NotDescr) lhs.getDescrs().get(0);
    assertEquals(1, not.getDescrs().size());
    final PatternDescr pattern = (PatternDescr) not.getDescrs().get(0);
    assertEquals("Cheese", pattern.getObjectType());
    final ExistsDescr ex = (ExistsDescr) lhs.getDescrs().get(1);
    assertEquals(1, ex.getDescrs().size());
    final PatternDescr exPattern = (PatternDescr) ex.getDescrs().get(0);
    assertEquals("Foo", exPattern.getObjectType());
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 75 with AndDescr

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

the class RuleParserTest method testOrNesting.

@Test
public void testOrNesting() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_nesting.drl");
    assertNotNull(pkg);
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("simple_rule", rule.getName());
    assertEquals(1, rule.getLhs().getDescrs().size());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(2, or.getDescrs().size());
    final PatternDescr first = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Person", first.getObjectType());
    final AndDescr and = (AndDescr) or.getDescrs().get(1);
    assertEquals(2, and.getDescrs().size());
    final PatternDescr left = (PatternDescr) and.getDescrs().get(0);
    assertEquals("Person", left.getObjectType());
    final PatternDescr right = (PatternDescr) and.getDescrs().get(1);
    assertEquals("Cheese", right.getObjectType());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) 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)

Aggregations

AndDescr (org.drools.drl.ast.descr.AndDescr)88 Test (org.junit.Test)61 PatternDescr (org.drools.drl.ast.descr.PatternDescr)60 RuleDescr (org.drools.drl.ast.descr.RuleDescr)54 PackageDescr (org.drools.drl.ast.descr.PackageDescr)39 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)34 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)18 BaseDescr (org.drools.drl.ast.descr.BaseDescr)18 Cheese (org.drools.mvel.compiler.Cheese)13 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)11 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)10 OrDescr (org.drools.drl.ast.descr.OrDescr)10 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)9 BindingDescr (org.drools.drl.ast.descr.BindingDescr)8 NotDescr (org.drools.drl.ast.descr.NotDescr)8 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 QueryDescr (org.drools.drl.ast.descr.QueryDescr)6 RuleDescrBuilder (org.drools.drl.ast.dsl.RuleDescrBuilder)5 FieldConstraintDescr (org.drools.drl.ast.descr.FieldConstraintDescr)4 LiteralRestrictionDescr (org.drools.drl.ast.descr.LiteralRestrictionDescr)4