Search in sources :

Example 76 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class AbstractKieCiTest method deployJar.

public static KieModule deployJar(KieServices ks, byte[] jar) {
    // Deploy jar into the repository
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    return km;
}
Also used : Resource(org.kie.api.io.Resource) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule)

Example 77 with Resource

use of org.kie.api.io.Resource 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 78 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KnowledgeBuilderImpl method validateUniqueRuleNames.

protected void validateUniqueRuleNames(final PackageDescr packageDescr) {
    final Set<String> names = new HashSet<String>();
    PackageRegistry packageRegistry = this.pkgRegistryMap.get(packageDescr.getNamespace());
    InternalKnowledgePackage pkg = null;
    if (packageRegistry != null) {
        pkg = packageRegistry.getPackage();
    }
    for (final RuleDescr rule : packageDescr.getRules()) {
        validateRule(packageDescr, rule);
        final String name = rule.getName();
        if (names.contains(name)) {
            addBuilderResult(new ParserError(rule.getResource(), "Duplicate rule name: " + name, rule.getLine(), rule.getColumn(), packageDescr.getNamespace()));
        }
        if (pkg != null) {
            RuleImpl duplicatedRule = pkg.getRule(name);
            if (duplicatedRule != null) {
                Resource resource = rule.getResource();
                Resource duplicatedResource = duplicatedRule.getResource();
                if (resource == null || duplicatedResource == null || duplicatedResource.getSourcePath() == null || duplicatedResource.getSourcePath().equals(resource.getSourcePath())) {
                    addBuilderResult(new DuplicateRule(rule, packageDescr, this.configuration));
                } else {
                    addBuilderResult(new ParserError(rule.getResource(), "Duplicate rule name: " + name, rule.getLine(), rule.getColumn(), packageDescr.getNamespace()));
                }
            }
        }
        names.add(name);
    }
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) DuplicateRule(org.drools.compiler.compiler.DuplicateRule) ParserError(org.drools.compiler.compiler.ParserError) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) 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) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) HashSet(java.util.HashSet) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 79 with Resource

use of org.kie.api.io.Resource 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)

Example 80 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieBuilderTest method testDeclarativeChannelRegistration.

@Test
public void testDeclarativeChannelRegistration() {
    final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\">\n" + "       <channels>\n" + "         <channel name=\"testChannel\" type=\"org.drools.compiler.integrationtests.KieBuilderTest$MockChannel\" />\n" + "       </channels>\n" + "    </ksession>" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1);
    KieContainer kieContainer = ks.newKieContainer(km.getReleaseId());
    KieSession kieSession = kieContainer.newKieSession();
    assertEquals(1, kieSession.getChannels().size());
    assertTrue(kieSession.getChannels().keySet().contains("testChannel"));
}
Also used : Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

Resource (org.kie.api.io.Resource)162 Test (org.junit.Test)100 KieSession (org.kie.api.runtime.KieSession)50 KieServices (org.kie.api.KieServices)43 KieBase (org.kie.api.KieBase)42 ArrayList (java.util.ArrayList)30 KieContainer (org.kie.api.runtime.KieContainer)27 StringReader (java.io.StringReader)23 ReleaseId (org.kie.api.builder.ReleaseId)23 KieBuilder (org.kie.api.builder.KieBuilder)20 KieModule (org.kie.api.builder.KieModule)20 KieFileSystem (org.kie.api.builder.KieFileSystem)19 ClassPathResource (org.drools.core.io.impl.ClassPathResource)18 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)17 FactType (org.kie.api.definition.type.FactType)17 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)14 IOException (java.io.IOException)13 PMMLResource (org.drools.compiler.compiler.PMMLResource)11