use of org.drools.compiler.lang.DrlDumper in project drools by kiegroup.
the class DumperTest method testRoundTripDRLAccumulate.
@Test
public void testRoundTripDRLAccumulate() throws Exception {
// RHDM-254
String drl = "package org.test\n" + "\n" + "rule \"last flown date\"\n" + "when\n" + " $customer : Profile( $ceid : id )\n" + " accumulate(\n" + " Flight( status == \"Flown\", $dptDate: departureDate.time ) from $customer.flights,\n" + " $cnt : count( $dptDate );\n" + " $cnt > 0 )\n" + "then\n" + "end";
DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
final PackageDescr pkgOriginal = parser.parse(false, drl);
final DrlDumper dumper = new DrlDumper();
String out = dumper.dump(pkgOriginal);
Assertions.assertThat(drl).isEqualToIgnoringWhitespace(out);
}
use of org.drools.compiler.lang.DrlDumper in project drools by kiegroup.
the class DescrBuilderTest method testBehaviorForSlidingWindow.
@Test
public void testBehaviorForSlidingWindow() throws InstantiationException, IllegalAccessException {
// DROOLS-852
List<String> myParams = new LinkedList<String>();
myParams.add("5s");
PackageDescr pkg = DescrFactory.newPackage().name("org.drools").newRule().name("from rule").lhs().not().pattern().type("StockTick").constraint("price > 10").behavior().type("window", "time").parameters(myParams).end().end().end().end().rhs("//System.out.println(s);").end().getDescr();
String drl = new DrlDumper().dump(pkg);
assertTrue(drl.contains("window:time(5s)"));
}
use of org.drools.compiler.lang.DrlDumper in project drools by kiegroup.
the class DescrBuilderTest method testDumperFromPkg.
@Test
public void testDumperFromPkg() {
// DROOLS-109
PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().and().or().pattern().id("$x", false).type("Integer").constraint("this > 10").end().pattern().id("$x", false).type("Integer").constraint("this < 20").end().end().pattern().type("Integer").constraint("this == $x").constraint("this == 42").end().end().end().rhs("").end().end().getDescr();
String drl = new DrlDumper().dump(pkg);
System.out.println(drl);
KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
System.err.println(knowledgeBuilder.getErrors());
assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
KieSession knowledgeSession = kbase.newKieSession();
KiePackage rebuiltPkg = knowledgeBuilder.getPackage("org.test");
org.kie.api.definition.rule.Rule rule = rebuiltPkg.getRules().iterator().next();
RuleImpl r = ((RuleImpl) rule);
assertEquals(2, r.getLhs().getChildren().size());
Iterator<RuleConditionElement> iter = r.getLhs().getChildren().iterator();
RuleConditionElement arg1 = iter.next();
assertTrue(arg1 instanceof GroupElement && ((GroupElement) arg1).getType() == GroupElement.Type.OR);
assertEquals(2, ((GroupElement) arg1).getChildren().size());
RuleConditionElement arg2 = iter.next();
assertTrue(arg2 instanceof Pattern);
}
use of org.drools.compiler.lang.DrlDumper in project drools by kiegroup.
the class DescrBuilderTest method testAccumulate.
@Test
public void testAccumulate() throws InstantiationException, IllegalAccessException {
PackageDescrBuilder packBuilder = DescrFactory.newPackage().newGlobal().identifier("list").type(List.class.getName()).end().name("org.drools.compiler").newRule().name("r1").lhs().pattern().id("$tot", true).type(Double.class.getName()).end().accumulate().source().pattern().id("$i", false).type(Integer.class.getName()).end().end().function("sum", "$tot", true, "$i").constraint("$tot > 15").end().end().rhs("list.add( $tot );").end().newRule().name("r2").attribute("dialect", "mvel").lhs().pattern().id("$tot", true).type(Double.class.getName()).end().accumulate().source().pattern().id("$i", false).type(Integer.class.getName()).end().end().function("sum", "$tot", true, "$i").constraint("$tot > 15").end().end().rhs("list.add( $tot * 2 );").end();
String drl = new DrlDumper().dump(packBuilder.getDescr());
System.out.println(drl);
KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
System.err.println(knowledgeBuilder.getErrors());
assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
KieSession knowledgeSession = kbase.newKieSession();
List list = new ArrayList();
knowledgeSession.setGlobal("list", list);
knowledgeSession.insert(3);
knowledgeSession.insert(39);
knowledgeSession.insert(24.0);
knowledgeSession.insert(42.0);
knowledgeSession.fireAllRules();
assertEquals(Arrays.asList(42.0, 84.0), list);
}
use of org.drools.compiler.lang.DrlDumper in project drools by kiegroup.
the class DescrBuilderTest method testRule.
@Test
public void testRule() throws InstantiationException, IllegalAccessException {
PackageDescrBuilder packBuilder = DescrFactory.newPackage().name("org.drools.compiler").newRule().name("r1").lhs().and().or().pattern("StockTick").constraint("price > 100").end().pattern("StockTick").constraint("price < 10").end().end().pattern("StockTick").constraint("company == \"RHT\"").end().end().end().rhs(" System.out.println(\"foo\");\n").end();
PackageDescr pkg = packBuilder.getDescr();
String drl = new DrlDumper().dump(packBuilder.getDescr());
System.out.println(drl);
KiePackage kpkg = compilePkgDescr(pkg);
assertEquals("org.drools.compiler", kpkg.getName());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Collections.singletonList(kpkg));
KieSession ksession = createKnowledgeSession(kbase);
ksession.insert(new StockTick(1, "RHT", 80, 1));
int rules = ksession.fireAllRules();
assertEquals(0, rules);
ksession = kbase.newKieSession();
ksession.insert(new StockTick(2, "RHT", 150, 1));
rules = ksession.fireAllRules();
assertEquals(1, rules);
}
Aggregations