Search in sources :

Example 76 with AndDescr

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

the class RuleParserTest method testLiteralBoolAndNegativeNumbersRule.

@Test
public void testLiteralBoolAndNegativeNumbersRule() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "literal_bool_and_negative.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    assertNotNull(rule.getLhs());
    assertEqualsIgnoreWhitespace("cons();", (String) rule.getConsequence());
    final AndDescr lhs = rule.getLhs();
    assertEquals(3, lhs.getDescrs().size());
    PatternDescr pattern = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals(1, pattern.getConstraint().getDescrs().size());
    AndDescr fieldAnd = (AndDescr) pattern.getConstraint();
    ExprConstraintDescr fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
    assertEquals("bar == false", fld.getExpression());
    pattern = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals(1, pattern.getConstraint().getDescrs().size());
    fieldAnd = (AndDescr) pattern.getConstraint();
    fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
    assertEquals("boo > -42", fld.getText());
    pattern = (PatternDescr) lhs.getDescrs().get(2);
    assertEquals(1, pattern.getConstraint().getDescrs().size());
    fieldAnd = (AndDescr) pattern.getConstraint();
    fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
    assertEquals("boo > -42.42", fld.getText());
}
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 77 with AndDescr

use of org.drools.compiler.lang.descr.AndDescr 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.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 78 with AndDescr

use of org.drools.compiler.lang.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.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 79 with AndDescr

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

the class RuleParserTest method testAndOrRules.

/**
 * Test that explicit "&&", "||" works as expected
 */
@Test
public void testAndOrRules() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "and_or_rule.drl");
    assertNotNull(pkg);
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("simple_rule", rule.getName());
    // we will have 3 children under the main And node
    final AndDescr and = rule.getLhs();
    assertEquals(3, and.getDescrs().size());
    PatternDescr left = (PatternDescr) and.getDescrs().get(0);
    PatternDescr right = (PatternDescr) and.getDescrs().get(1);
    assertEquals("Person", left.getObjectType());
    assertEquals("Cheese", right.getObjectType());
    assertEquals(1, left.getConstraint().getDescrs().size());
    ExprConstraintDescr fld = (ExprConstraintDescr) left.getConstraint().getDescrs().get(0);
    assertEquals("name == \"mark\"", fld.getExpression());
    assertEquals(1, right.getConstraint().getDescrs().size());
    fld = (ExprConstraintDescr) right.getConstraint().getDescrs().get(0);
    assertEquals("type == \"stilton\"", fld.getExpression());
    // now the "||" part
    final OrDescr or = (OrDescr) and.getDescrs().get(2);
    assertEquals(2, or.getDescrs().size());
    left = (PatternDescr) or.getDescrs().get(0);
    right = (PatternDescr) or.getDescrs().get(1);
    assertEquals("Person", left.getObjectType());
    assertEquals("Cheese", right.getObjectType());
    assertEquals(1, left.getConstraint().getDescrs().size());
    fld = (ExprConstraintDescr) left.getConstraint().getDescrs().get(0);
    assertEquals("name == \"mark\"", fld.getExpression());
    assertEquals(1, right.getConstraint().getDescrs().size());
    fld = (ExprConstraintDescr) right.getConstraint().getDescrs().get(0);
    assertEquals("type == \"stilton\"", fld.getExpression());
    assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" );", (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) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

AndDescr (org.drools.compiler.lang.descr.AndDescr)79 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)54 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)52 Test (org.junit.Test)52 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)39 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)27 Cheese (org.drools.compiler.Cheese)13 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)12 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)12 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)11 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)8 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)8 OrDescr (org.drools.compiler.lang.descr.OrDescr)8 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)7 NotDescr (org.drools.compiler.lang.descr.NotDescr)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 List (java.util.List)4 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)4 QueryDescr (org.drools.compiler.lang.descr.QueryDescr)4