Search in sources :

Example 36 with PackageDescr

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

the class RuleParserTest method testExpanderMultipleConstraints.

@Test
public void testExpanderMultipleConstraints() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final PackageDescr pkg = parser.parse(this.getReader("expander_multiple_constraints.dslr"), this.getReader("multiple_constraints.dsl"));
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(2, rule.getLhs().getDescrs().size());
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Person", pattern.getObjectType());
    assertEquals(2, pattern.getConstraint().getDescrs().size());
    assertEquals("age < 42", ((ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0)).getExpression());
    assertEquals("location==atlanta", ((ExprConstraintDescr) pattern.getConstraint().getDescrs().get(1)).getExpression());
    pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("Bar", pattern.getObjectType());
    assertNotNull((String) rule.getConsequence());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 37 with PackageDescr

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

the class RuleParserTest method testTypeWithMetaData.

@Test
public void testTypeWithMetaData() throws Exception {
    PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "type_with_meta.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final List<TypeDeclarationDescr> declarations = pkg.getTypeDeclarations();
    assertEquals(3, declarations.size());
}
Also used : TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 38 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr 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.compiler.lang.descr.NotDescr) ExistsDescr(org.drools.compiler.lang.descr.ExistsDescr) 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 39 with PackageDescr

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

the class RuleParserTest method testFromWithInlineListIndex.

@Test
public void testFromWithInlineListIndex() throws Exception {
    String source = "rule XYZ \n" + " when \n" + " o: Order( ) \n" + " Number( ) from [1, 2, 3][1] \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][1]", ((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 40 with PackageDescr

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

the class RuleParserTest method testSimpleConstraint.

@Test
public void testSimpleConstraint() throws Exception {
    String source = "package com.sample  rule test  when  Cheese( type == 'stilton', price > 10 )  then  end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertEquals("com.sample", pkg.getName());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("test", rule.getName());
    assertEquals(1, rule.getLhs().getDescrs().size());
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    AndDescr constraint = (AndDescr) pattern.getConstraint();
    assertEquals(2, constraint.getDescrs().size());
    assertEquals("type == \"stilton\"", constraint.getDescrs().get(0).toString());
    assertEquals("price > 10", constraint.getDescrs().get(1).toString());
}
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)

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