use of org.kie.api.io.Resource in project drools by kiegroup.
the class KieBaseUpdater method updateResourcesIncrementally.
private int updateResourcesIncrementally(KnowledgeBuilderImpl kbuilder, CompositeKnowledgeBuilder ckbuilder) {
int fileCount = ctx.modifiedClasses.size();
for (ResourceChangeSet rcs : ctx.cs.getChanges().values()) {
if (rcs.getChangeType() != ChangeType.REMOVED) {
String resourceName = rcs.getResourceName();
if (!resourceName.endsWith(".properties") && isFileInKBase(ctx.newKM, ctx.newKieBaseModel, resourceName)) {
List<ResourceChange> changes = rcs.getChanges();
if (!changes.isEmpty()) {
// we need to deal with individual parts of the resource
fileCount += AbstractKieModule.updateResource(ckbuilder, ctx.newKM, resourceName, rcs) ? 1 : 0;
} else {
// the whole resource has to handled
if (rcs.getChangeType() == ChangeType.UPDATED) {
Resource resource = ctx.currentKM.getResource(resourceName);
kbuilder.removeObjectsGeneratedFromResource(resource);
}
fileCount += ctx.newKM.addResourceToCompiler(ckbuilder, ctx.newKieBaseModel, resourceName, rcs) ? 1 : 0;
}
}
}
for (ResourceChangeSet.RuleLoadOrder loadOrder : rcs.getLoadOrder()) {
KnowledgePackageImpl pkg = (KnowledgePackageImpl) ctx.kBase.getKiePackage(loadOrder.getPkgName());
if (pkg != null) {
RuleImpl rule = pkg.getRule(loadOrder.getRuleName());
if (rule != null) {
// rule can be null, if it didn't exist before
rule.setLoadOrder(loadOrder.getLoadOrder());
}
}
}
}
return fileCount;
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class KieBuilderImpl method buildPMMLPojos.
private void buildPMMLPojos(List<String> pmmlFileNames, KieProject kProject) {
PMMLCompiler compiler = PMMLCompilerFactory.getPMMLCompiler();
System.out.println("!!!!! PMMLCompiler version = " + compiler.getCompilerVersion() + " !!!!!");
KieFileSystem javaSource = KieServices.Factory.get().newKieFileSystem();
Map<String, String> javaSources = new HashMap<>();
for (String fname : pmmlFileNames) {
Map<String, String> modelSources = null;
Resource pmmlContent = ResourceFactory.newByteArrayResource(srcMfs.getBytes(fname));
if (pmmlContent != null) {
try {
modelSources = compiler.getJavaClasses(pmmlContent.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
modelSources = compiler.getJavaClasses(fname);
}
if (modelSources != null && !modelSources.isEmpty()) {
javaSources.putAll(modelSources);
}
}
for (String key : javaSources.keySet()) {
String javaCode = javaSources.get(key);
if (javaCode != null && !javaCode.trim().isEmpty()) {
Resource res = ResourceFactory.newByteArrayResource(javaCode.getBytes()).setResourceType(ResourceType.JAVA);
String sourcePath = key.replaceAll("\\.", "/") + ".java";
res.setSourcePath(sourcePath);
javaSource.write(res);
}
}
ResourceReader src = ((KieFileSystemImpl) javaSource).asMemoryFileSystem();
List<String> javaFileNames = getJavaFileNames(src);
if (javaFileNames != null && !javaFileNames.isEmpty()) {
ClassLoader classLoader = kProject.getClassLoader();
KnowledgeBuilderConfigurationImpl kconf = new KnowledgeBuilderConfigurationImpl(classLoader);
JavaDialectConfiguration javaConf = (JavaDialectConfiguration) kconf.getDialectConfiguration("java");
compileJavaClasses(javaConf, classLoader, javaFileNames, JAVA_ROOT, src);
}
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class KieRepositoryImpl method addKieModule.
public KieModule addKieModule(Resource resource, Resource... dependencies) {
log.info("Adding KieModule from resource: " + resource);
KieModule kModule = getKieModule(resource);
if (dependencies != null && dependencies.length > 0) {
for (Resource depRes : dependencies) {
InternalKieModule depKModule = (InternalKieModule) getKieModule(depRes);
((InternalKieModule) kModule).addKieDependency(depKModule);
log.info("Adding KieModule dependency from resource: " + resource);
}
}
addKieModule(kModule);
return kModule;
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class KieBuilderTest method testMultipleKBaseWithDrlError.
@Test
public void testMultipleKBaseWithDrlError() {
// RHBRMS-2651
final String drl = "package org.drools.compiler;\n" + "rule \"test\"\n" + " when\n" + " Smurf\n" + " then\n" + "end";
final KieServices ks = KieServices.Factory.get();
final KieModuleModel kproj = ks.newKieModuleModel();
kproj.newKieBaseModel("kbase1").newKieSessionModel("ksession1").setDefault(true);
kproj.newKieBaseModel("kbase2").newKieSessionModel("ksession2");
final ReleaseId releaseId = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
final KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).writeKModuleXML(kproj.toXML());
final Resource drlResource = ResourceFactory.newByteArrayResource(drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
kfs.write("src/main/resources/org/drools/compiler/drl1.drl", drlResource);
final KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
final List<org.kie.api.builder.Message> messages = kb.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR);
assertEquals(4, messages.size());
assertTrue(messages.get(0).toString().contains("kbase1"));
assertTrue(messages.get(1).toString().contains("kbase1"));
assertTrue(messages.get(2).toString().contains("kbase2"));
assertTrue(messages.get(3).toString().contains("kbase2"));
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class KieBuilderTest method testStatelessSessionDeclarativeChannelRegistration.
@Test
public void testStatelessSessionDeclarativeChannelRegistration() {
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\" type=\"stateless\">\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());
StatelessKieSession statelessKieSession = kieContainer.newStatelessKieSession();
assertEquals(1, statelessKieSession.getChannels().size());
assertTrue(statelessKieSession.getChannels().keySet().contains("testChannel"));
}
Aggregations