Search in sources :

Example 51 with RuleDescr

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

the class RuleParserTest method testAccumulateWithNestedFrom.

@Test
public void testAccumulateWithNestedFrom() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate_with_nested_from.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final AccumulateDescr accumulate = (AccumulateDescr) out.getSource();
    PatternDescr person = (PatternDescr) accumulate.getInputPattern();
    assertEquals("Person", person.getObjectType());
    final CollectDescr collect2 = (CollectDescr) person.getSource();
    final PatternDescr people = collect2.getInputPattern();
    assertEquals("People", people.getObjectType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) CollectDescr(org.drools.compiler.lang.descr.CollectDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) Test(org.junit.Test)

Example 52 with RuleDescr

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

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

the class RuleParserTest method testAccumulateMnemonic.

@Test
public void testAccumulateMnemonic() throws Exception {
    String drl = "package org.drools.compiler\n" + "rule \"Accumulate 1\"\n" + "when\n" + "     acc( Cheese( $price : price ),\n" + "          $a1 : average( $price ) )\n" + "then\n" + "end\n";
    final PackageDescr pkg = (PackageDescr) parse("compilationUnit", drl);
    assertEquals(1, pkg.getRules().size());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Object", out.getObjectType());
    AccumulateDescr accum = (AccumulateDescr) out.getSource();
    assertTrue(accum.isExternalFunction());
    List<AccumulateFunctionCallDescr> functions = accum.getFunctions();
    assertEquals(1, functions.size());
    assertEquals("average", functions.get(0).getFunction());
    assertEquals("$a1", functions.get(0).getBind());
    assertEquals("$price", functions.get(0).getParams()[0]);
    final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
    assertEquals("Cheese", pattern.getObjectType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) AccumulateFunctionCallDescr(org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) Test(org.junit.Test)

Example 54 with RuleDescr

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

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

the class RuleParserTest method testAccumulateExternalFunction.

@Test
public void testAccumulateExternalFunction() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulateExternalFunction.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final AccumulateDescr accum = (AccumulateDescr) out.getSource();
    assertEqualsIgnoreWhitespace("$age", accum.getFunctions().get(0).getParams()[0]);
    assertEqualsIgnoreWhitespace("average", accum.getFunctions().get(0).getFunction());
    assertTrue(accum.isExternalFunction());
    final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
    assertEquals("Person", pattern.getObjectType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) Test(org.junit.Test)

Aggregations

RuleDescr (org.drools.compiler.lang.descr.RuleDescr)161 Test (org.junit.Test)143 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)103 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)100 AndDescr (org.drools.compiler.lang.descr.AndDescr)51 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)37 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)19 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)19 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)19 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)17 Cheese (org.drools.compiler.Cheese)16 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)14 OrDescr (org.drools.compiler.lang.descr.OrDescr)13 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)13 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)12 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)11 FromDescr (org.drools.compiler.lang.descr.FromDescr)11 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)10 NotDescr (org.drools.compiler.lang.descr.NotDescr)10