Search in sources :

Example 41 with PackageDescr

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

the class RuleParserTest method testMultipleRules.

@Test
public void testMultipleRules() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "multiple_rules.drl");
    final List<RuleDescr> rules = pkg.getRules();
    assertEquals(2, rules.size());
    final RuleDescr rule0 = rules.get(0);
    assertEquals("Like Stilton", rule0.getName());
    final RuleDescr rule1 = rules.get(1);
    assertEquals("Like Cheddar", rule1.getName());
    // checkout the first rule
    AndDescr lhs = rule1.getLhs();
    assertNotNull(lhs);
    assertEquals(1, lhs.getDescrs().size());
    assertEqualsIgnoreWhitespace("System.out.println(\"I like \" + t);", (String) rule0.getConsequence());
    // Check first pattern
    PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Cheese", first.getObjectType());
    // checkout the second rule
    lhs = rule1.getLhs();
    assertNotNull(lhs);
    assertEquals(1, lhs.getDescrs().size());
    assertEqualsIgnoreWhitespace("System.out.println(\"I like \" + t);", (String) rule1.getConsequence());
    // Check first pattern
    first = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Cheese", first.getObjectType());
}
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) Test(org.junit.Test)

Example 42 with PackageDescr

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

the class RuleParserTest method testFromWithInlineListMethod.

@Test
public void testFromWithInlineListMethod() throws Exception {
    String source = "rule XYZ \n" + " when \n" + " o: Order( ) \n" + " Number( ) from [1, 2, 3].sublist(1, 2) \n" + " then \n" + " System.err.println(\"Invalid customer id found!\"); \n" + " o.addError(\"Invalid customer id\"); \n" + "end \n";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("XYZ", rule.getName());
    assertFalse(parser.hasErrors());
    PatternDescr number = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("[1, 2, 3].sublist(1, 2)", ((FromDescr) number.getSource()).getDataSource().toString());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FromDescr(org.drools.compiler.lang.descr.FromDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 43 with PackageDescr

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

the class RuleParserTest method testRuleMetadata.

@Test
public void testRuleMetadata() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "Rule_with_Metadata.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    // @fooAttribute(barValue)
    // @fooAtt2(barVal2)
    RuleDescr rule = pkg.getRules().get(0);
    assertTrue(rule.getAnnotationNames().contains("fooMeta1"));
    assertEquals("barVal1", rule.getAnnotation("fooMeta1").getValue());
    assertTrue(rule.getAnnotationNames().contains("fooMeta2"));
    assertEquals("barVal2", rule.getAnnotation("fooMeta2").getValue());
    assertEqualsIgnoreWhitespace("System.out.println(\"Consequence\");", (String) rule.getConsequence());
}
Also used : RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 44 with PackageDescr

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

the class RuleParserTest method testPackageWithError2.

@Test
public void testPackageWithError2() throws Exception {
    final String source = "package 12 12312 231";
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final PackageDescr pkg = parser.parse(true, new StringReader(source));
    assertTrue(parser.hasErrors());
    assertEquals("", pkg.getName());
}
Also used : StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 45 with PackageDescr

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

the class RuleParserTest method testEmptyPattern.

@Test
public void testEmptyPattern() throws Exception {
    PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "test_EmptyPattern.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals(1, pkg.getRules().size());
    final RuleDescr ruleDescr = (RuleDescr) pkg.getRules().get(0);
    assertEquals("simple rule", ruleDescr.getName());
    assertNotNull(ruleDescr.getLhs());
    assertEquals(1, ruleDescr.getLhs().getDescrs().size());
    final PatternDescr patternDescr = (PatternDescr) ruleDescr.getLhs().getDescrs().get(0);
    assertEquals(0, // this
    patternDescr.getConstraint().getDescrs().size());
    assertEquals("Cheese", patternDescr.getObjectType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Aggregations

PackageDescr (org.drools.compiler.lang.descr.PackageDescr)243 Test (org.junit.Test)194 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)85 DrlParser (org.drools.compiler.compiler.DrlParser)46 AndDescr (org.drools.compiler.lang.descr.AndDescr)39 InputStreamReader (java.io.InputStreamReader)27 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)27 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)24 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)23 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)19 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)19 KiePackage (org.kie.api.definition.KiePackage)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 Cheese (org.drools.compiler.Cheese)16 KieSession (org.kie.api.runtime.KieSession)16 List (java.util.List)15 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)15 DrlDumper (org.drools.compiler.lang.DrlDumper)14