Search in sources :

Example 1 with InternalResource

use of org.drools.core.io.internal.InternalResource in project drools by kiegroup.

the class XmlBifParser method loadBif.

public static Bif loadBif(Resource resource, KnowledgeBuilderErrors errors) {
    InputStream is = null;
    try {
        is = resource.getInputStream();
    } catch (IOException e) {
        errors.add(new ParserError(resource, "Exception opening Stream:\n" + e.toString(), 0, 0));
        return null;
    }
    try {
        String encoding = resource instanceof InternalResource ? ((InternalResource) resource).getEncoding() : null;
        XStream xstream = encoding != null ? createTrustingXStream(new DomDriver(encoding)) : createTrustingXStream();
        initXStream(xstream);
        Bif bif = (Bif) xstream.fromXML(is);
        return bif;
    } catch (Exception e) {
        errors.add(new BayesNetworkAssemblerError(resource, "Unable to parse opening Stream:\n" + e.toString()));
        return null;
    }
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) ParserError(org.drools.compiler.compiler.ParserError) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) XStreamUtils.createTrustingXStream(org.kie.soup.commons.xstream.XStreamUtils.createTrustingXStream) BayesNetworkAssemblerError(org.drools.beliefs.bayes.assembler.BayesNetworkAssemblerError) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with InternalResource

use of org.drools.core.io.internal.InternalResource in project drools by kiegroup.

the class DrlParser method parse.

public PackageDescr parse(final boolean isEditor, final Resource resource, final InputStream is) throws DroolsParserException, IOException {
    this.resource = resource;
    String encoding = resource instanceof InternalResource ? ((InternalResource) resource).getEncoding() : null;
    lexer = buildLexer(is, encoding, languageLevel);
    DRLParser parser = buildParser(lexer, languageLevel);
    return compile(isEditor, parser);
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) DRLParser(org.drools.compiler.lang.DRLParser)

Example 3 with InternalResource

use of org.drools.core.io.internal.InternalResource in project drools by kiegroup.

the class XmlChangeSetReaderTest method testResourceAttributes.

@Test
public void testResourceAttributes() throws Exception {
    SemanticModules semanticModules = new SemanticModules();
    semanticModules.addSemanticModule(new ChangeSetSemanticModule());
    XmlChangeSetReader changeSetReader = new XmlChangeSetReader(semanticModules);
    changeSetReader.setClassLoader(XmlChangeSetReaderTest.class.getClassLoader(), null);
    ChangeSet changeSet = changeSetReader.read(XmlChangeSetReaderTest.class.getClassLoader().getResourceAsStream("org/drools/core/xml/test-change-set.xml"));
    Assert.assertNotNull(changeSet);
    Collection<Resource> resourcesAdded = changeSet.getResourcesAdded();
    Assert.assertNotNull(resourcesAdded);
    Assert.assertEquals(4, resourcesAdded.size());
    InternalResource resource1 = null;
    InternalResource resource2 = null;
    InternalResource resource3 = null;
    InternalResource secureResource = null;
    for (Resource r : resourcesAdded) {
        InternalResource resource = (InternalResource) r;
        if (resource.getSourcePath() != null && resource.getSourcePath().equals("resource1")) {
            resource1 = resource;
        } else if (resource.getSourcePath() != null && resource.getSourcePath().equals("secureResource")) {
            secureResource = resource;
        } else if (resource.getSourcePath() == null && resource.getDescription() == null) {
            resource3 = resource;
        } else if (resource.getSourcePath() == null) {
            resource2 = resource;
        }
    }
    Assert.assertNotNull(resource1);
    Assert.assertNotNull(resource2);
    Assert.assertNotNull(resource3);
    Assert.assertNotNull(secureResource);
    Assert.assertNull(resource1.getDescription());
    Assert.assertEquals("another description", resource2.getDescription());
    Assert.assertEquals("some useful description", secureResource.getDescription());
    Assert.assertEquals(2, changeSetReader.getParser().getAttrs().getLength());
    Assert.assertEquals("DRL", changeSetReader.getParser().getAttrs().getValue("type"));
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) Resource(org.kie.api.io.Resource) InternalResource(org.drools.core.io.internal.InternalResource) ChangeSet(org.kie.internal.ChangeSet) Test(org.junit.Test)

Example 4 with InternalResource

use of org.drools.core.io.internal.InternalResource in project drools by kiegroup.

the class KnowledgeBuilderImpl method registerBuildResource.

public void registerBuildResource(final Resource resource, ResourceType type) {
    InternalResource ires = (InternalResource) resource;
    if (ires.getResourceType() == null) {
        ires.setResourceType(type);
    } else if (ires.getResourceType() != type) {
        addBuilderResult(new ResourceTypeDeclarationWarning(resource, ires.getResourceType(), type));
    }
    if (ResourceType.CHANGE_SET == type) {
        try {
            ChangeSet changeSet = parseChangeSet(resource);
            List<Resource> resources = new ArrayList<Resource>();
            resources.add(resource);
            for (Resource addedRes : changeSet.getResourcesAdded()) {
                resources.add(addedRes);
            }
            for (Resource modifiedRes : changeSet.getResourcesModified()) {
                resources.add(modifiedRes);
            }
            for (Resource removedRes : changeSet.getResourcesRemoved()) {
                resources.add(removedRes);
            }
            buildResources.push(resources);
        } catch (Exception e) {
            results.add(new DroolsError() {

                public String getMessage() {
                    return "Unable to register changeset resource " + resource;
                }

                public int[] getLines() {
                    return new int[0];
                }
            });
        }
    } else {
        buildResources.push(Collections.singletonList(resource));
    }
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) DroolsError(org.drools.compiler.compiler.DroolsError) BaseResource(org.drools.core.io.impl.BaseResource) DescrResource(org.drools.core.io.impl.DescrResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Resource(org.kie.api.io.Resource) ReaderResource(org.drools.core.io.impl.ReaderResource) PMMLResource(org.drools.compiler.compiler.PMMLResource) InternalResource(org.drools.core.io.internal.InternalResource) ArrayList(java.util.ArrayList) ResourceTypeDeclarationWarning(org.drools.compiler.compiler.ResourceTypeDeclarationWarning) ChangeSet(org.kie.internal.ChangeSet) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ExpanderException(org.drools.compiler.lang.ExpanderException) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) SAXException(org.xml.sax.SAXException)

Example 5 with InternalResource

use of org.drools.core.io.internal.InternalResource in project drools by kiegroup.

the class KnowledgeBuilderImpl method addPackageFromChangeSet.

void addPackageFromChangeSet(Resource resource) throws SAXException, IOException {
    XmlChangeSetReader reader = new XmlChangeSetReader(this.configuration.getSemanticModules());
    if (resource instanceof ClassPathResource) {
        reader.setClassLoader(((ClassPathResource) resource).getClassLoader(), ((ClassPathResource) resource).getClazz());
    } else {
        reader.setClassLoader(this.configuration.getClassLoader(), null);
    }
    Reader resourceReader = null;
    try {
        resourceReader = resource.getReader();
        ChangeSet changeSet = reader.read(resourceReader);
        if (changeSet == null) {
        // @TODO should log an error
        }
        for (Resource nestedResource : changeSet.getResourcesAdded()) {
            InternalResource iNestedResourceResource = (InternalResource) nestedResource;
            if (iNestedResourceResource.isDirectory()) {
                for (Resource childResource : iNestedResourceResource.listResources()) {
                    if (((InternalResource) childResource).isDirectory()) {
                        // ignore sub directories
                        continue;
                    }
                    ((InternalResource) childResource).setResourceType(iNestedResourceResource.getResourceType());
                    addKnowledgeResource(childResource, iNestedResourceResource.getResourceType(), iNestedResourceResource.getConfiguration());
                }
            } else {
                addKnowledgeResource(iNestedResourceResource, iNestedResourceResource.getResourceType(), iNestedResourceResource.getConfiguration());
            }
        }
    } finally {
        if (resourceReader != null) {
            resourceReader.close();
        }
    }
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) BaseResource(org.drools.core.io.impl.BaseResource) DescrResource(org.drools.core.io.impl.DescrResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Resource(org.kie.api.io.Resource) ReaderResource(org.drools.core.io.impl.ReaderResource) PMMLResource(org.drools.compiler.compiler.PMMLResource) InternalResource(org.drools.core.io.internal.InternalResource) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Reader(java.io.Reader) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) StringReader(java.io.StringReader) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) ChangeSet(org.kie.internal.ChangeSet) ClassPathResource(org.drools.core.io.impl.ClassPathResource)

Aggregations

InternalResource (org.drools.core.io.internal.InternalResource)8 IOException (java.io.IOException)3 ClassPathResource (org.drools.core.io.impl.ClassPathResource)3 Resource (org.kie.api.io.Resource)3 ChangeSet (org.kie.internal.ChangeSet)3 PMMLResource (org.drools.compiler.compiler.PMMLResource)2 BaseResource (org.drools.core.io.impl.BaseResource)2 DescrResource (org.drools.core.io.impl.DescrResource)2 ReaderResource (org.drools.core.io.impl.ReaderResource)2 XStream (com.thoughtworks.xstream.XStream)1 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 ExecutionException (java.util.concurrent.ExecutionException)1