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