use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.
the class KieCompilationCacheTest method testHelloWorldWithPackagesAnd2KieBases.
@Test
public void testHelloWorldWithPackagesAnd2KieBases() throws Exception {
String drl1 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R11 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R12 when\n" + " $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
String drl2 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R21 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R22 when\n" + " $m : Message( message == \"Aloha Earth\" )\n" + "then\n" + "end\n";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0-SNAPSHOT");
KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).write("src/main/resources/KBase1/org/pkg2/r2.drl", drl2).writeKModuleXML(createKieProjectWithPackagesAnd2KieBases(ks).toXML());
ks.newKieBuilder(kfs).buildAll();
InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
byte[] jar = kieModule.getBytes();
MemoryFileSystem mfs = MemoryFileSystem.readFromJar(jar);
File file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase1"));
assertNotNull(file);
file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase2"));
assertNotNull(file);
Resource jarRes = ks.getResources().newByteArrayResource(jar);
KieModule km = ks.getRepository().addKieModule(jarRes);
KieSession ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
ksession.insert(new Message("Hello World"));
assertEquals(1, ksession.fireAllRules());
ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
ksession.insert(new Message("Hi Universe"));
assertEquals(1, ksession.fireAllRules());
ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
ksession.insert(new Message("Aloha Earth"));
assertEquals(0, ksession.fireAllRules());
ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
ksession.insert(new Message("Hello World"));
assertEquals(1, ksession.fireAllRules());
ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
ksession.insert(new Message("Hi Universe"));
assertEquals(0, ksession.fireAllRules());
ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
ksession.insert(new Message("Aloha Earth"));
assertEquals(1, ksession.fireAllRules());
}
use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.
the class CommonTestMethodBase method createJar.
public static byte[] createJar(KieServices ks, String kmoduleContent, Predicate<String> classFilter, ReleaseId releaseId, Resource... resources) {
KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).writeKModuleXML(kmoduleContent);
for (int i = 0; i < resources.length; i++) {
if (resources[i] != null) {
kfs.write(resources[i]);
}
}
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
((InternalKieBuilder) kieBuilder).buildAll(classFilter);
Results results = kieBuilder.getResults();
if (results.hasMessages(Message.Level.ERROR)) {
throw new IllegalStateException(results.getMessages(Message.Level.ERROR).toString());
}
InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
byte[] jar = kieModule.getBytes();
return jar;
}
use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.
the class KieBuilderTest method testSetPomModelReuse.
@Test
public void testSetPomModelReuse() throws IOException {
String namespace = "org.kie.test";
ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "pomModelReuse", "1.0-SNAPSHOT");
String pom = KieBuilderImpl.generatePomXml(releaseId);
KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
kfs.writePomXML(pom);
// Create a KieBuilder instance
KieBuilder kieBuilder1 = createKieBuilder(kfs);
kieBuilder1.buildAll();
// Get PomModel to re-use in second KieBuilder instance
PomModel pomModel = ((KieBuilderImpl) kieBuilder1).getPomModel();
kfs.writePomXML(pom);
// Create another KieBuilder instance with the same KieFileSystem, setting PomModel
KieBuilder kieBuilder2 = createKieBuilder(kfs);
((KieBuilderImpl) kieBuilder2).setPomModel(pomModel);
kieBuilder2.buildAll();
// Read pom.xml from first KieBuilder's KieModule
InternalKieModule kieModule1 = (InternalKieModule) ((KieBuilderImpl) kieBuilder1).getKieModuleIgnoringErrors();
final Reader reader1 = kieModule1.getResource("META-INF/maven/org.kie.test/pomModelReuse/pom.xml").getReader();
int charCode;
String readPom1 = "";
while ((charCode = reader1.read()) != -1) {
readPom1 = readPom1 + (char) charCode;
}
reader1.close();
assertEquals(pom, readPom1);
// Read pom.xml from second KieBuilder's KieModule
InternalKieModule kieModule2 = (InternalKieModule) ((KieBuilderImpl) kieBuilder2).getKieModuleIgnoringErrors();
final Reader reader2 = kieModule2.getResource("META-INF/maven/org.kie.test/pomModelReuse/pom.xml").getReader();
String readPom2 = "";
while ((charCode = reader2.read()) != -1) {
readPom2 = readPom2 + (char) charCode;
}
reader1.close();
assertEquals(pom, readPom2);
}
use of org.drools.compiler.kie.builder.impl.InternalKieModule in project jbpm by kiegroup.
the class AbstractCaseServicesBaseTest method createKieJar.
protected InternalKieModule createKieJar(KieServices ks, ReleaseId releaseId, List<String> resources, Map<String, String> extraResources) {
KieFileSystem kfs = createKieFileSystemWithKProject(ks);
kfs.writePomXML(getPom(releaseId));
DeploymentDescriptor customDescriptor = createDeploymentDescriptor();
if (extraResources == null) {
extraResources = new HashMap<String, String>();
}
extraResources.put("src/main/resources/" + DeploymentDescriptor.META_INF_LOCATION, customDescriptor.toXml());
for (String resource : resources) {
kfs.write("src/main/resources/KBase-test/" + resource, ResourceFactory.newClassPathResource(resource));
}
if (extraResources != null) {
for (Map.Entry<String, String> entry : extraResources.entrySet()) {
kfs.write(entry.getKey(), ResourceFactory.newByteArrayResource(entry.getValue().getBytes()));
}
}
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
if (!kieBuilder.buildAll().getResults().getMessages().isEmpty()) {
for (Message message : kieBuilder.buildAll().getResults().getMessages()) {
logger.error("Error Message: ({}) {}", message.getPath(), message.getText());
}
throw new RuntimeException("There are errors builing the package, please check your knowledge assets!");
}
return (InternalKieModule) kieBuilder.getKieModule();
}
use of org.drools.compiler.kie.builder.impl.InternalKieModule in project jbpm by kiegroup.
the class RuntimeEnvironmentBuilder method getDefault.
/**
* Provides default configuration of <code>RuntimeEnvironmentBuilder</code> that is based on:
* <ul>
* <li>DefaultRuntimeEnvironment</li>
* </ul>
* This one is tailored to works smoothly with kjars as the notion of kbase and ksessions
* @param releaseId <code>ReleaseId</code> that described the kjar
* @param kbaseName name of the kbase defined in kmodule.xml stored in kjar
* @param ksessionName name of the ksession define in kmodule.xml stored in kjar
* @return new instance of <code>RuntimeEnvironmentBuilder</code> that is already preconfigured with defaults
*
* @see DefaultRuntimeEnvironment
*/
public static RuntimeEnvironmentBuilder getDefault(ReleaseId releaseId, String kbaseName, String ksessionName) {
KieMavenRepository repository = KieMavenRepository.getKieMavenRepository();
repository.resolveArtifact(releaseId.toExternalForm());
KieServices ks = KieServices.Factory.get();
KieContainer kieContainer = ks.newKieContainer(releaseId);
DeploymentDescriptorManager descriptorManager = new DeploymentDescriptorManager();
List<DeploymentDescriptor> descriptorHierarchy = descriptorManager.getDeploymentDescriptorHierarchy(kieContainer);
DeploymentDescriptorMerger merger = new DeploymentDescriptorMerger();
DeploymentDescriptor descriptor = merger.merge(descriptorHierarchy, MergeMode.MERGE_COLLECTIONS);
if (StringUtils.isEmpty(kbaseName)) {
KieBaseModel defaultKBaseModel = ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
if (defaultKBaseModel != null) {
kbaseName = defaultKBaseModel.getName();
} else {
kbaseName = DEFAULT_KBASE_NAME;
}
}
InternalKieModule module = (InternalKieModule) ((KieContainerImpl) kieContainer).getKieModuleForKBase(kbaseName);
if (module == null) {
throw new IllegalStateException("Cannot find kbase, either it does not exist or there are multiple default kbases in kmodule.xml");
}
KieBase kbase = kieContainer.getKieBase(kbaseName);
RuntimeEnvironmentBuilder builder = null;
if (descriptor.getPersistenceMode() == PersistenceMode.NONE) {
builder = getDefaultInMemory();
} else {
builder = getDefault();
}
Map<String, Object> contaxtParams = new HashMap<String, Object>();
contaxtParams.put("classLoader", kieContainer.getClassLoader());
// populate various properties of the builder
if (descriptor.getPersistenceUnit() != null) {
EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate(descriptor.getPersistenceUnit());
builder.entityManagerFactory(emf);
contaxtParams.put("entityManagerFactory", emf);
}
// process object models that are globally configured (environment entries, session configuration)
for (NamedObjectModel model : descriptor.getEnvironmentEntries()) {
Object entry = getInstanceFromModel(model, kieContainer, contaxtParams);
builder.addEnvironmentEntry(model.getName(), entry);
}
for (NamedObjectModel model : descriptor.getConfiguration()) {
Object entry = getInstanceFromModel(model, kieContainer, contaxtParams);
builder.addConfiguration(model.getName(), (String) entry);
}
ObjectMarshallingStrategy[] mStrategies = new ObjectMarshallingStrategy[descriptor.getMarshallingStrategies().size() + 1];
int index = 0;
for (ObjectModel model : descriptor.getMarshallingStrategies()) {
Object strategy = getInstanceFromModel(model, kieContainer, contaxtParams);
mStrategies[index] = (ObjectMarshallingStrategy) strategy;
index++;
}
// lastly add the main default strategy
mStrategies[index] = new SerializablePlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT);
builder.addEnvironmentEntry(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, mStrategies);
builder.addEnvironmentEntry("KieDeploymentDescriptor", descriptor).knowledgeBase(kbase).classLoader(kieContainer.getClassLoader()).registerableItemsFactory(new KModuleRegisterableItemsFactory(kieContainer, ksessionName));
return builder;
}
Aggregations