use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class QueryInRHSCepTest method withInsertBeforeQueryCloudTest.
@Test
public void withInsertBeforeQueryCloudTest() {
String drl = "package org.drools.compiler.integrationtests\n" + "import " + SolicitFirePojo.class.getCanonicalName() + "\n" + "import " + QueryItemPojo.class.getCanonicalName() + "\n" + "global java.util.List myGlobal \n" + "query \"myQuery\"\n" + " $r : QueryItemPojo()\n" + "end\n" + "rule \"drools-usage/WLHxG8S\"\n" + " no-loop\n" + " when\n" + " SolicitFirePojo()\n" + " then\n" + " insert(new QueryItemPojo());\n" + " myGlobal.add(drools.getKieRuntime().getQueryResults(\"myQuery\"));\n" + " end\n";
System.out.println(drl);
final KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
KieModuleModel kmodule = ks.newKieModuleModel();
KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase").setDefault(true).setEventProcessingMode(EventProcessingOption.CLOUD);
baseModel.newKieSessionModel("defaultKSession").setDefault(true);
kfs.writeKModuleXML(kmodule.toXML());
kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/compiler/integrationtests/" + this.getClass().getName() + ".drl"));
assertTrue(ks.newKieBuilder(kfs).buildAll().getResults().getMessages().isEmpty());
ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
myGlobal = new ArrayList<>();
ksession.setGlobal("myGlobal", myGlobal);
ksession.insert(new QueryItemPojo());
ksession.insert(new SolicitFirePojo());
int fired = ksession.fireAllRules();
assertEquals(1, fired);
assertEquals(1, myGlobal.size());
// notice 1 is manually inserted, 1 get inserted from rule's RHS, for a total of 2.
assertEquals(2, ((QueryResults) myGlobal.get(0)).size());
}
use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class RuleUnit method createKieProjectWithPackages.
private KieModuleModel createKieProjectWithPackages(KieServices ks) {
KieModuleModel kproj = ks.newKieModuleModel();
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setEventProcessingMode(EventProcessingOption.STREAM).addPackage("*").setDefault(true);
kieBaseModel1.newKieSessionModel("KSession1").setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
return kproj;
}
use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class AbstractKieProject method indexKieModule.
private void indexKieModule(Map<String, InternalKieModule> kJarFromKBaseName, InternalKieModule kJar, boolean isMainModule) {
boolean defaultKieBaseFromMain = false;
boolean defaultKieSessionFromMain = false;
boolean defaultStatelessKieSessionFromMain = false;
KieModuleModel kieProject = kJar.getKieModuleModel();
for (KieBaseModel kieBaseModel : kieProject.getKieBaseModels().values()) {
if (kieBaseModel.isDefault()) {
if (defaultKieBase == null || (isMainModule && !defaultKieBaseFromMain)) {
defaultKieBase = kieBaseModel;
defaultKieBaseFromMain = isMainModule;
} else {
defaultKieBase = null;
log.warn("Found more than one default KieBase: disabling all. KieBases will be accessible only by name");
}
}
kBaseModels.put(kieBaseModel.getName(), kieBaseModel);
// should already be set, but just in case
((KieBaseModelImpl) kieBaseModel).setKModule(kieProject);
kJarFromKBaseName.put(kieBaseModel.getName(), kJar);
for (KieSessionModel kieSessionModel : kieBaseModel.getKieSessionModels().values()) {
if (kieSessionModel.isDefault()) {
if (kieSessionModel.getType() == KieSessionModel.KieSessionType.STATEFUL) {
if (defaultKieSession == null || (isMainModule && !defaultKieSessionFromMain)) {
defaultKieSession = kieSessionModel;
defaultKieSessionFromMain = isMainModule;
} else {
defaultKieSession = null;
log.warn("Found more than one default KieSession: disabling all. KieSessions will be accessible only by name");
}
} else {
if (defaultStatelessKieSession == null || (isMainModule && !defaultStatelessKieSessionFromMain)) {
defaultStatelessKieSession = kieSessionModel;
defaultStatelessKieSessionFromMain = isMainModule;
} else {
defaultStatelessKieSession = null;
log.warn("Found more than one default StatelessKieSession: disabling all. StatelessKieSessions will be accessible only by name");
}
}
}
// should already be set, but just in case
((KieSessionModelImpl) kieSessionModel).setKBase(kieBaseModel);
kSessionModels.put(kieSessionModel.getName(), kieSessionModel);
}
}
}
use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class ClasspathKieProject method fetchKModule.
private static InternalKieModule fetchKModule(URL url, String fixedURL) {
if (url.getPath().endsWith("-spring.xml")) {
// the entire kmodule creation is happening in the kie-spring module,
// hence we force a null return
fetchKModuleFromSpring(url);
return null;
}
KieModuleModel kieProject = KieModuleModelImpl.fromXML(url);
setDefaultsforEmptyKieModule(kieProject);
String pomProperties = getPomProperties(fixedURL);
if (pomProperties == null) {
log.warn("Cannot find maven pom properties for this project. Using the container's default ReleaseId");
}
ReleaseId releaseId = pomProperties != null ? ReleaseIdImpl.fromPropertiesString(pomProperties) : KieServices.Factory.get().getRepository().getDefaultReleaseId();
String rootPath = fixedURL;
if (rootPath.lastIndexOf(':') > 0) {
rootPath = IoUtils.asSystemSpecificPath(rootPath, rootPath.lastIndexOf(':'));
}
return createInternalKieModule(kieProject, releaseId, rootPath);
}
use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class KieRepositoryImpl method getKieModule.
public KieModule getKieModule(Resource resource) {
InternalResource res = (InternalResource) resource;
try {
KieModule kModule;
// find kmodule.xml
if (res.hasURL()) {
String urlPath = res.getURL().toExternalForm();
if (res.isDirectory()) {
if (!urlPath.endsWith("/")) {
urlPath = urlPath + "/";
}
urlPath = urlPath + KieModuleModelImpl.KMODULE_JAR_PATH;
} else {
urlPath = "jar:" + urlPath + "!/" + KieModuleModelImpl.KMODULE_JAR_PATH;
}
kModule = ClasspathKieProject.fetchKModule(new URL(urlPath));
log.debug("Fetched KieModule from resource: " + resource);
} else {
// might be a byte[] resource
MemoryFileSystem mfs = MemoryFileSystem.readFromJar(res.getInputStream());
byte[] bytes = mfs.getBytes(KieModuleModelImpl.KMODULE_JAR_PATH);
KieModuleModel kieProject = KieModuleModelImpl.fromXML(new ByteArrayInputStream(bytes));
setDefaultsforEmptyKieModule(kieProject);
String pomProperties = mfs.findPomProperties();
ReleaseId releaseId = ReleaseIdImpl.fromPropertiesString(pomProperties);
kModule = new MemoryKieModule(releaseId, kieProject, mfs);
}
return kModule;
} catch (Exception e) {
throw new RuntimeException("Unable to fetch module from resource: " + res, e);
}
}
Aggregations