Search in sources :

Example 11 with DrlDumper

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);
}
Also used : DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper)

Example 12 with DrlDumper

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);
}
Also used : InputStreamReader(java.io.InputStreamReader) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 13 with DrlDumper

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);
}
Also used : InputStreamReader(java.io.InputStreamReader) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 14 with DrlDumper

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);
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) DrlDumper(org.drools.mvel.DrlDumper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 15 with DrlDumper

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)"));
}
Also used : PackageDescr(org.drools.drl.ast.descr.PackageDescr) LinkedList(java.util.LinkedList) DrlDumper(org.drools.mvel.DrlDumper) 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