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;
}
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));
}
}
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);
}
}
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();
}
}
}
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"));
}
Aggregations