use of org.drools.mvel.DrlDumper in project drools by kiegroup.
the class DumperTest method checkRoundtrip.
private void checkRoundtrip(String drl) throws DroolsParserException {
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.mvel.DrlDumper in project drools by kiegroup.
the class XmlPackageReaderTest method testParseLhs.
@Test
public void testParseLhs() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseLhs.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
String expected = StringUtils.readFileAsString(new InputStreamReader(getClass().getResourceAsStream("test_ParseLhs.drl")));
String expectedWithoutHeader = removeLicenseHeader(expected);
String actual = new DrlDumper().dump(packageDescr);
Assertions.assertThat(expectedWithoutHeader).isEqualToIgnoringWhitespace(actual);
}
use of org.drools.mvel.DrlDumper in project drools by kiegroup.
the class XmlPackageReaderTest method testParseRule.
@Test
public void testParseRule() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseRule.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
String expected = StringUtils.readFileAsString(new InputStreamReader(getClass().getResourceAsStream("test_ParseRule.drl")));
// remove license header as that one is not stored in the XML
String expectedWithoutHeader = removeLicenseHeader(expected);
System.out.println(expectedWithoutHeader);
String actual = new DrlDumper().dump(packageDescr);
Assertions.assertThat(expectedWithoutHeader).isEqualToIgnoringWhitespace(actual);
}
use of org.drools.mvel.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.mvel.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.mvel.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)"));
}
Aggregations