Search in sources :

Example 6 with PackageDescr

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

the class XmlPackageReaderTest method testParseRhs.

@Test
public void testParseRhs() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseRhs.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.HashMap", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.mvel.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
    final List globals = packageDescr.getGlobals();
    assertEquals(2, globals.size());
    final GlobalDescr x = (GlobalDescr) globals.get(0);
    final GlobalDescr yada = (GlobalDescr) globals.get(1);
    assertEquals("com.sample.X", x.getType());
    assertEquals("x", x.getIdentifier());
    assertEquals("com.sample.Yada", yada.getType());
    assertEquals("yada", yada.getIdentifier());
    final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get(0);
    final List names = functionDescr.getParameterNames();
    assertEquals("foo", names.get(0));
    assertEquals("bada", names.get(1));
    final List types = functionDescr.getParameterTypes();
    assertEquals("Bar", types.get(0));
    assertEquals("Bing", types.get(1));
    assertEquals("System.out.println(\"hello world\");", functionDescr.getText().trim());
    final RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules().get(0);
    assertEquals("my rule", ruleDescr.getName());
    final String consequence = (String) ruleDescr.getConsequence();
    assertNotNull(consequence);
    assertEquals("System.out.println( \"hello\" );", consequence.trim());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) InputStreamReader(java.io.InputStreamReader) RuleDescr(org.drools.drl.ast.descr.RuleDescr) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 7 with PackageDescr

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

the class XmlPackageReaderTest method testParseCollect.

@Test
public void testParseCollect() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseCollect.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    String expected = StringUtils.readFileAsString(new InputStreamReader(getClass().getResourceAsStream("test_ParseCollect.drl")));
    String expectedWithoutHeader = removeLicenseHeader(expected);
    String actual = new DrlDumper().dump(packageDescr);
    Assertions.assertThat(expectedWithoutHeader).isEqualToIgnoringWhitespace(actual);
}
Also used : InputStreamReader(java.io.InputStreamReader) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 8 with PackageDescr

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

the class XmlPackageReaderTest method testAccumulateMultiPattern.

@Test
public void testAccumulateMultiPattern() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseAccumulate.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    RuleDescr obj = (RuleDescr) packageDescr.getRules().get(1);
    Object patternobj = obj.getLhs().getDescrs().get(0);
    assertTrue(patternobj instanceof PatternDescr);
    final PatternDescr patterncheese = (PatternDescr) patternobj;
    assertEquals(patterncheese.getIdentifier(), "cheese");
    assertEquals(patterncheese.getObjectType(), "Cheese");
    AccumulateDescr accumulatedescr = (AccumulateDescr) patterncheese.getSource();
    assertEquals("total += $cheese.getPrice();", accumulatedescr.getActionCode());
    assertEquals("int total = 0;", accumulatedescr.getInitCode());
    assertEquals("new Integer( total ) );", accumulatedescr.getResultCode());
    AndDescr anddescr = (AndDescr) accumulatedescr.getInput();
    List descrlist = anddescr.getDescrs();
    PatternDescr[] listpattern = (PatternDescr[]) descrlist.toArray(new PatternDescr[descrlist.size()]);
    assertEquals(listpattern[0].getObjectType(), "Milk");
    assertEquals(listpattern[1].getObjectType(), "Cup");
}
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) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 9 with PackageDescr

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

the class DescrBuilderTest method testDumperTimer.

@Test
public void testDumperTimer() {
    PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").attribute("timer").value("cron:0/5 * * * * ?").end().lhs().end().rhs("").end().end().getDescr();
    String drl = new DrlDumper().dump(pkg);
    assertTrue(drl.contains("timer (cron:0/5 * * * * ?)"));
}
Also used : PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) Test(org.junit.Test)

Example 10 with PackageDescr

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

the class DescrBuilderTest method testDumperPositional.

@Test
public void testDumperPositional() {
    PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().pattern().type("Integer").constraint("this > 10", true).constraint("this > 11", true).constraint("this > 12", false).constraint("this > 13", false).end().end().rhs("").end().end().getDescr();
    String drl = new DrlDumper().dump(pkg);
    assertTrue(drl.contains("Integer( this > 10, this > 11; this > 12, this > 13 )"));
}
Also used : PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) 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