Search in sources :

Example 6 with XmlPackageReader

use of org.drools.compiler.compiler.xml.XmlPackageReader 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);
}
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 7 with XmlPackageReader

use of org.drools.compiler.compiler.xml.XmlPackageReader in project drools by kiegroup.

the class DumperTestHelper method XmlFile.

public static void XmlFile(String filename) throws Exception {
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    XmlPackageReader xmlPackageReader = new XmlPackageReader(conf.getSemanticModules());
    xmlPackageReader.getParser().setClassLoader(DumperTestHelper.class.getClassLoader());
    xmlPackageReader.read(new InputStreamReader(DumperTestHelper.class.getResourceAsStream(filename)));
    final PackageDescr pkgOriginal = xmlPackageReader.getPackageDescr();
    final XmlDumper dumper = new XmlDumper();
    final String result = dumper.dump(pkgOriginal);
    String buffer = readFile(filename);
    System.out.println(buffer);
    System.out.println(result);
    Assertions.assertThat(buffer).isEqualToIgnoringWhitespace(result);
    assertNotNull(result);
}
Also used : InputStreamReader(java.io.InputStreamReader) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) XmlDumper(org.drools.compiler.compiler.xml.XmlDumper) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader)

Example 8 with XmlPackageReader

use of org.drools.compiler.compiler.xml.XmlPackageReader in project drools by kiegroup.

the class KnowledgeBuilderImpl method addPackageFromXml.

/**
 * Load a rule package from XML source.
 *
 * @param reader
 * @throws DroolsParserException
 * @throws IOException
 */
public void addPackageFromXml(final Reader reader) throws DroolsParserException, IOException {
    this.resource = new ReaderResource(reader, ResourceType.XDRL);
    final XmlPackageReader xmlReader = new XmlPackageReader(this.configuration.getSemanticModules());
    xmlReader.getParser().setClassLoader(this.rootClassLoader);
    try {
        xmlReader.read(reader);
    } catch (final SAXException e) {
        throw new DroolsParserException(e.toString(), e.getCause());
    }
    addPackage(xmlReader.getPackageDescr());
    this.resource = null;
}
Also used : ReaderResource(org.drools.core.io.impl.ReaderResource) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) SAXException(org.xml.sax.SAXException)

Example 9 with XmlPackageReader

use of org.drools.compiler.compiler.xml.XmlPackageReader in project drools by kiegroup.

the class KnowledgeBuilderImpl method xmlToPackageDescr.

PackageDescr xmlToPackageDescr(Resource resource) throws DroolsParserException, IOException {
    final XmlPackageReader xmlReader = new XmlPackageReader(this.configuration.getSemanticModules());
    xmlReader.getParser().setClassLoader(this.rootClassLoader);
    Reader reader = null;
    try {
        reader = resource.getReader();
        xmlReader.read(reader);
    } catch (final SAXException e) {
        throw new DroolsParserException(e.toString(), e.getCause());
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return xmlReader.getPackageDescr();
}
Also used : XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Reader(java.io.Reader) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) StringReader(java.io.StringReader) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) SAXException(org.xml.sax.SAXException)

Example 10 with XmlPackageReader

use of org.drools.compiler.compiler.xml.XmlPackageReader in project drools by kiegroup.

the class XmlPackageReaderTest method testParseGlobal.

@Test
public void testParseGlobal() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseGlobal.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
    final List globals = packageDescr.getGlobals();
    assertEquals(2, globals.size());
    final GlobalDescr x = (GlobalDescr) globals.get(0);
    final GlobalDescr yada = (GlobalDescr) globals.get(1);
    assertEquals("com.sample.X", x.getType());
    assertEquals("x", x.getIdentifier());
    assertEquals("com.sample.Yada", yada.getType());
    assertEquals("yada", yada.getIdentifier());
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) InputStreamReader(java.io.InputStreamReader) List(java.util.List) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Aggregations

XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)19 InputStreamReader (java.io.InputStreamReader)16 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)16 Test (org.junit.Test)15 List (java.util.List)8 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)7 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)7 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)4 DrlDumper (org.drools.compiler.lang.DrlDumper)3 AndDescr (org.drools.compiler.lang.descr.AndDescr)3 FunctionDescr (org.drools.compiler.lang.descr.FunctionDescr)3 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)2 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)2 SAXException (org.xml.sax.SAXException)2 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 XmlDumper (org.drools.compiler.compiler.xml.XmlDumper)1 ExistsDescr (org.drools.compiler.lang.descr.ExistsDescr)1 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)1