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 ) "));
}
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);
}
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));
}
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());
}
}
Aggregations