Search in sources :

Example 1 with DrlDumper

use of org.drools.compiler.lang.DrlDumper in project drools by kiegroup.

the class DescrBuilderTest method testDumperDuration.

@Test
public void testDumperDuration() {
    PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").attribute("duration").value("int: 0 3600000; repeat-limit = 6").end().lhs().end().rhs("").end().end().getDescr();
    String drl = new DrlDumper().dump(pkg);
    assertTrue(drl.contains("duration (int: 0 3600000; repeat-limit = 6)"));
}
Also used : PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) Test(org.junit.Test)

Example 2 with DrlDumper

use of org.drools.compiler.lang.DrlDumper in project drools by kiegroup.

the class DescrBuilderTest method testConditionalBranch.

@Test
public void testConditionalBranch() {
    String expected = "packageorg.drools.compiler\n" + "rule \"test\"\n" + "when\n" + "    Cheese( type == \"stilton\" )  \n" + "    if ( price < 10 ) do[c1] \n" + "    Cheese( type == \"cheddar\" )  \n" + "then\n" + "// do something\n" + "then[c1]\n" + "// do something else\n" + "end";
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.compiler").newRule().name("test").lhs().pattern("Cheese").constraint("type == \"stilton\"").end().conditionalBranch().condition().constraint("price < 10").end().consequence().name("c1").end().end().pattern("Cheese").constraint("type == \"cheddar\"").end().end().rhs("// do something").namedRhs("c1", "// do something else").end().getDescr();
    assertEquals(1, pkg.getRules().size());
    String drl = new DrlDumper().dump(pkg);
    Assertions.assertThat(expected).isEqualToIgnoringWhitespace(drl);
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = createKnowledgeSession(kbase);
    Cheese stilton = new Cheese("stilton", 5);
    Cheese cheddar = new Cheese("cheddar", 7);
    Cheese brie = new Cheese("brie", 5);
    ksession.insert(stilton);
    ksession.insert(cheddar);
    ksession.insert(brie);
    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 3 with DrlDumper

use of org.drools.compiler.lang.DrlDumper 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.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) Test(org.junit.Test)

Example 4 with DrlDumper

use of org.drools.compiler.lang.DrlDumper in project drools by kiegroup.

the class ExtendsTest method testExtendsDump.

@Test
public void testExtendsDump() {
    PackageDescrBuilder pkgd = DescrFactory.newPackage();
    pkgd.name("org.test").newDeclare().type().name("Foo").newField("id").type("int").end().end().newDeclare().type().name("Bar").superType("Foo").newField("val").type("int").initialValue("42").end().end();
    String drl = new DrlDumper().dump(pkgd.getDescr());
    KieBase kb = loadKnowledgeBaseFromString(drl);
    FactType bar = kb.getFactType("org.test", "Bar");
    try {
        Object x = bar.newInstance();
        assertEquals(42, bar.get(x, "val"));
        bar.set(x, "id", 1);
        assertEquals(1, bar.get(x, "id"));
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : PackageDescrBuilder(org.drools.compiler.lang.api.PackageDescrBuilder) KieBase(org.kie.api.KieBase) DrlDumper(org.drools.compiler.lang.DrlDumper) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 5 with DrlDumper

use of org.drools.compiler.lang.DrlDumper 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.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Aggregations

DrlDumper (org.drools.compiler.lang.DrlDumper)16 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)14 Test (org.junit.Test)14 InputStreamReader (java.io.InputStreamReader)5 KieSession (org.kie.api.runtime.KieSession)5 DrlParser (org.drools.compiler.compiler.DrlParser)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)4 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)3 KiePackage (org.kie.api.definition.KiePackage)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Cheese (org.drools.compiler.Cheese)2 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)2 KieBase (org.kie.api.KieBase)2 EntryPoint (org.kie.api.runtime.rule.EntryPoint)2 StringReader (java.io.StringReader)1 StockTick (org.drools.compiler.StockTick)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 DroolsError (org.drools.compiler.compiler.DroolsError)1