Search in sources :

Example 1 with PackageDescr

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

the class NestingTest method testNesting.

@Test
public void testNesting() throws Exception {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "\n" + "dialect \"mvel\"\n" + "\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + Address.class.getCanonicalName() + ";\n" + "\n" + "rule \"test something\"\n" + "\n" + "    when\n" + "        p: Person( name==\"Michael\",\n" + "                                (addresses[1].street == \"Low\" &&\n" + "                                addresses[0].street == \"High\"  )\n" + "                                )\n" + "    then\n" + "        p.name = \"goober\";\n" + "        System.out.println(p.name);\n" + "        insert(new Address(\"Latona\"));\n" + "end";
    final Person p = new Person();
    p.setName("Michael");
    final Address add1 = new Address();
    add1.setStreet("High");
    final Address add2 = new Address();
    add2.setStreet("Low");
    final List<Address> l = new ArrayList<>();
    l.add(add1);
    l.add(add2);
    p.setAddresses(l);
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final PackageDescr desc = parser.parse(new StringReader(drl));
    final List packageAttrs = desc.getAttributes();
    assertEquals(1, desc.getRules().size());
    assertEquals(1, packageAttrs.size());
    final RuleDescr rule = desc.getRules().get(0);
    final Map<String, AttributeDescr> ruleAttrs = rule.getAttributes();
    assertEquals(1, ruleAttrs.size());
    assertEquals("mvel", ruleAttrs.get("dialect").getValue());
    assertEquals("dialect", ruleAttrs.get("dialect").getName());
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("nesting-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        ksession.insert(p);
        ksession.fireAllRules();
    } finally {
        ksession.dispose();
    }
}
Also used : Address(org.drools.testcoverage.common.model.Address) ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) RuleDescr(org.drools.drl.ast.descr.RuleDescr) DrlParser(org.drools.drl.parser.DrlParser) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Person(org.drools.testcoverage.common.model.Person) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 2 with PackageDescr

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

the class DumperTest method testAccumulateWithoutConstraint.

@Test
public void testAccumulateWithoutConstraint() throws Exception {
    // DROOLS-5872
    String expectedDrl = "package example \n" + "\n" + "import java.math.BigDecimal\n" + "\n" + "rule \"Test Rule\"\n" + "when\n" + "    accumulate(     \n" + "    $target : example.RuleTest.Fact(  )  ,\n" + "        $cnt : count(  ) \n" + "         ) \n" + "then\n" + "System.out.println($cnt);\n" + "\n" + "end";
    PackageDescr packageDescr = DescrFactory.newPackage().name("example").newImport().target("java.math.BigDecimal").end().newRule().name("Test Rule").lhs().accumulate().source().pattern().type("example.RuleTest.Fact").id("$target", false).end().end().function("count", "$cnt", false).end().end().rhs("System.out.println($cnt);").end().end().getDescr();
    String drl = new DrlDumper().dump(packageDescr);
    Assertions.assertThat(drl).isEqualToIgnoringWhitespace(expectedDrl);
}
Also used : PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) Test(org.junit.Test)

Example 3 with PackageDescr

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

the class XmlPackageReaderTest method testParseSimpleRule.

@Test
public void testParseSimpleRule() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_SimpleRule1.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.List", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.mvel.compiler.Person", ((ImportDescr) imports.get(1)).getTarget());
    RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules().get(0);
    assertEquals("simple_rule1", ruleDescr.getName());
    AndDescr lhs = ruleDescr.getLhs();
    PatternDescr patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Person", patternDescr.getObjectType());
    ExprConstraintDescr expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("name == \"darth\"", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(1);
    assertEquals("simple_rule2", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("age == 35 || == -3.5", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(2);
    assertEquals("simple_rule3", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("age == 35 || (!= 7.0 && != -70)", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(3);
    assertEquals("simple_rule3", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("name == $s", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(4);
    assertEquals("simple_rule4", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("(name == $s) || (age == 35 || (!= 7.0 && != -70))", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(5);
    assertEquals("simple_rule5", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("(name == $s) || ((age != 34) && (age != 37) && (name != \"yoda\"))", expr.getExpression());
}
Also used : InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 4 with PackageDescr

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

the class XmlPackageReaderTest method testParseExists.

@Test
public void testParseExists() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseExists.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
    Object existdescr = obj.getLhs().getDescrs().get(0);
    assertTrue(existdescr instanceof ExistsDescr);
    Object patternDescriptor = ((ExistsDescr) existdescr).getDescrs().get(0);
    assertTrue(patternDescriptor instanceof PatternDescr);
    assertEquals(((PatternDescr) patternDescriptor).getObjectType(), "Person");
    Object notDescr = obj.getLhs().getDescrs().get(1);
    assertEquals(notDescr.getClass().getName(), NotDescr.class.getName());
    existdescr = ((NotDescr) notDescr).getDescrs().get(0);
    patternDescriptor = ((ExistsDescr) existdescr).getDescrs().get(0);
    assertTrue(patternDescriptor instanceof PatternDescr);
    assertEquals(((PatternDescr) patternDescriptor).getObjectType(), "Cheese");
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 5 with PackageDescr

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

the class XmlPackageReaderTest method testParseFrom.

@Test
public void testParseFrom() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseFrom.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
    PatternDescr patterndescr = (PatternDescr) obj.getLhs().getDescrs().get(0);
    FromDescr from = (FromDescr) patterndescr.getSource();
    MVELExprDescr accessordescriptor = (MVELExprDescr) from.getDataSource();
    assertEquals("cheesery.getCheeses(i+4)", accessordescriptor.getExpression());
    assertEquals(patterndescr.getObjectType(), "Cheese");
    assertEquals(patterndescr.getIdentifier(), "cheese");
}
Also used : InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FromDescr(org.drools.drl.ast.descr.FromDescr) MVELExprDescr(org.drools.drl.ast.descr.MVELExprDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Aggregations

PackageDescr (org.drools.drl.ast.descr.PackageDescr)262 Test (org.junit.Test)210 RuleDescr (org.drools.drl.ast.descr.RuleDescr)104 PatternDescr (org.drools.drl.ast.descr.PatternDescr)85 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)52 AndDescr (org.drools.drl.ast.descr.AndDescr)39 DrlParser (org.drools.drl.parser.DrlParser)39 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)25 InputStreamReader (java.io.InputStreamReader)23 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)23 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)23 KieSession (org.kie.api.runtime.KieSession)19 List (java.util.List)18 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)17 KiePackage (org.kie.api.definition.KiePackage)17 DrlDumper (org.drools.mvel.DrlDumper)16 Cheese (org.drools.mvel.compiler.Cheese)16 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)15