Search in sources :

Example 16 with DrlDumper

use of org.drools.mvel.DrlDumper in project drools by kiegroup.

the class DescrBuilderTest method testQueryParameters.

@Test
public void testQueryParameters() {
    // DROOLS-4604
    PackageDescrBuilder packBuilder = DescrFactory.newPackage().name("org.test.rules").newImport().target("org.test.event.TemporalEvent").end().newQuery().name("getTemporalEventById").parameter("String", "eventId").lhs().pattern("TemporalEvent").constraint("id == eventId").from().entryPoint("EventStream").end().end().end().end();
    String drl = new DrlDumper().dump(packBuilder.getDescr());
    assertTrue(drl.contains("query \"getTemporalEventById\" ( String eventId ) "));
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) DrlDumper(org.drools.mvel.DrlDumper) Test(org.junit.Test)

Example 17 with DrlDumper

use of org.drools.mvel.DrlDumper in project drools by kiegroup.

the class DescrBuilderTest method testConditionalBranch.

@Test
public void testConditionalBranch() {
    String expected = "packageorg.drools.mvel.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.mvel.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.mvel.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = kbase.newKieSession();
    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.mvel.compiler.Cheese) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 18 with DrlDumper

use of org.drools.mvel.DrlDumper in project drools by kiegroup.

the class DRLDumperTest method testDumpers.

@Test
public void testDumpers() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final Resource resource = new InputStreamResource(getClass().getResourceAsStream("drl/test_Dumpers.drl"));
    final PackageDescr pkg = parser.parse(resource);
    if (parser.hasErrors()) {
        for (final DroolsError error : parser.getErrors()) {
            logger.warn(error.toString());
        }
        fail(parser.getErrors().toString());
    }
    final Resource descrResource = KieServices.Factory.get().getResources().newDescrResource(pkg);
    descrResource.setSourcePath(TestConstants.TEST_RESOURCES_FOLDER + "test_Dumpers.descr");
    KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, descrResource);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Cheese brie = new Cheese("brie", 12);
    ksession.insert(brie);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertEquals("3 1", list.get(0));
    assertEquals("MAIN", list.get(1));
    assertEquals("1 1", list.get(2));
    // ---------------------------
    final DrlDumper drlDumper = new DrlDumper();
    final String drlResult = drlDumper.dump(pkg);
    System.out.println(drlResult);
    kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drlResult);
    ksession = kbase.newKieSession();
    list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(brie);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertEquals("3 1", list.get(0));
    assertEquals("MAIN", list.get(1));
    assertEquals("1 1", list.get(2));
}
Also used : DroolsError(org.drools.drl.parser.DroolsError) KieBase(org.kie.api.KieBase) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) DrlParser(org.drools.drl.parser.DrlParser) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.mvel.compiler.Cheese) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Test(org.junit.Test)

Example 19 with DrlDumper

use of org.drools.mvel.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 = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, 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.drl.ast.dsl.PackageDescrBuilder) KieBase(org.kie.api.KieBase) DrlDumper(org.drools.mvel.DrlDumper) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Aggregations

DrlDumper (org.drools.mvel.DrlDumper)19 PackageDescr (org.drools.drl.ast.descr.PackageDescr)16 Test (org.junit.Test)16 InputStreamReader (java.io.InputStreamReader)5 KieSession (org.kie.api.runtime.KieSession)5 PackageDescrBuilder (org.drools.drl.ast.dsl.PackageDescrBuilder)4 DrlParser (org.drools.drl.parser.DrlParser)4 InternalKnowledgeBase (org.drools.kiesession.rulebase.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 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)2 InputStreamResource (org.drools.core.io.impl.InputStreamResource)2 Cheese (org.drools.mvel.compiler.Cheese)2 KieBase (org.kie.api.KieBase)2 EntryPoint (org.kie.api.runtime.rule.EntryPoint)2 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1