Search in sources :

Example 6 with DrlDumper

use of org.drools.compiler.lang.DrlDumper in project drools by kiegroup.

the class DumperTestHelper method DrlFile.

public static void DrlFile(String filename) throws Exception {
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final PackageDescr pkgOriginal = parser.parse(new InputStreamReader(DumperTestHelper.class.getResourceAsStream(filename)));
    final DrlDumper dumper = new DrlDumper();
    String result1 = dumper.dump(pkgOriginal);
    final PackageDescr pkgDerivated = parser.parse(new StringReader(result1));
    String result2 = dumper.dump(pkgDerivated);
    System.out.println(result1);
    Assertions.assertThat(result1).isEqualToIgnoringWhitespace(result2);
}
Also used : InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper)

Example 7 with DrlDumper

use of org.drools.compiler.lang.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("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());
    }
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(pkg));
    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 = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(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));
}
Also used : DroolsError(org.drools.compiler.compiler.DroolsError) KieBase(org.kie.api.KieBase) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) DrlParser(org.drools.compiler.compiler.DrlParser) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Test(org.junit.Test)

Example 8 with DrlDumper

use of org.drools.compiler.lang.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.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 9 with DrlDumper

use of org.drools.compiler.lang.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.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 10 with DrlDumper

use of org.drools.compiler.lang.DrlDumper in project drools by kiegroup.

the class DumperTestHelper method dump.

public static String dump(String filename) throws Exception {
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final PackageDescr pkgOriginal = parser.parse(new InputStreamReader(DumperTestHelper.class.getResourceAsStream(filename)));
    final DrlDumper dumper = new DrlDumper();
    return dumper.dump(pkgOriginal);
}
Also used : InputStreamReader(java.io.InputStreamReader) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper)

Aggregations

DrlDumper (org.drools.compiler.lang.DrlDumper)16 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)14 Test (org.junit.Test)14 InputStreamReader (java.io.InputStreamReader)5 KieSession (org.kie.api.runtime.KieSession)5 DrlParser (org.drools.compiler.compiler.DrlParser)4 InternalKnowledgeBase (org.drools.core.impl.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 Cheese (org.drools.compiler.Cheese)2 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)2 KieBase (org.kie.api.KieBase)2 EntryPoint (org.kie.api.runtime.rule.EntryPoint)2 StringReader (java.io.StringReader)1 StockTick (org.drools.compiler.StockTick)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 DroolsError (org.drools.compiler.compiler.DroolsError)1