use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.
the class KieContainerImpl method update.
private Results update(InternalKieModule currentKM, InternalKieModule newKM) {
final KieJarChangeSet cs = currentKM.getChanges(newKM);
List<String> modifiedClassNames = getModifiedClasses(cs);
final boolean modifyingUsedClass = isModifyingUsedClass(modifiedClassNames, getClassLoader()) || isModifyingUsedFunction(cs);
final Collection<Class<?>> modifiedClasses = reinitModifiedClasses(newKM, modifiedClassNames, getClassLoader(), modifyingUsedClass);
final Collection<String> unchangedResources = getUnchangedResources(newKM, cs);
Map<String, KieBaseModel> currentKieBaseModels = ((KieModuleKieProject) kProject).updateToModule(newKM);
final ResultsImpl results = new ResultsImpl();
currentKM.updateKieModule(newKM);
List<String> kbasesToRemove = new ArrayList<>();
for (Entry<String, KieBase> kBaseEntry : kBases.entrySet()) {
String kbaseName = kBaseEntry.getKey();
KieBaseModelImpl newKieBaseModel = (KieBaseModelImpl) kProject.getKieBaseModel(kbaseName);
KieBaseModelImpl currentKieBaseModel = (KieBaseModelImpl) currentKieBaseModels.get(kbaseName);
// if a kbase no longer exists, just remove it from the cache
if (newKieBaseModel == null) {
// have to save for later removal to avoid iteration errors
kbasesToRemove.add(kbaseName);
} else {
final InternalKnowledgeBase kBase = (InternalKnowledgeBase) kBaseEntry.getValue();
// share Knowledge Builder among updater as it's computationally expensive to create this
KnowledgeBuilderConfigurationImpl builderConfiguration = (KnowledgeBuilderConfigurationImpl) newKM.createBuilderConfiguration(newKieBaseModel, kBase.getRootClassLoader());
InternalKnowledgeBuilder kbuilder = (InternalKnowledgeBuilder) KnowledgeBuilderFactory.newKnowledgeBuilder(kBase, builderConfiguration);
KieBaseUpdaterImplContext context = new KieBaseUpdaterImplContext(kProject, kBase, currentKM, newKM, cs, modifiedClasses, modifyingUsedClass, unchangedResources, results, newKieBaseModel, currentKieBaseModel, kbuilder);
// Multiple updaters are required to be merged together in a single Runnable
// to avoid a deadlock while using .fireUntilHalt()
// see IncrementalCompilationTest.testMultipleIncrementalCompilationsWithFireUntilHalt
// with multiple updaters (such as Alpha NetworkCompilerUpdater)
CompositeRunnable compositeUpdater = new CompositeRunnable();
KieBaseUpdater kieBaseUpdater = currentKM.createKieBaseUpdater(context);
compositeUpdater.add(kieBaseUpdater);
KieBaseUpdaterOptions kieBaseUpdaterOptions = new KieBaseUpdaterOptions(new KieBaseUpdaterOptions.OptionEntry(AlphaNetworkCompilerOption.class, builderConfiguration.getAlphaNetworkCompilerOption()));
KieBaseUpdaters updaters = KieService.load(KieBaseUpdaters.class);
updaters.getChildren().stream().map(kbu -> kbu.create(new KieBaseUpdatersContext(kieBaseUpdaterOptions, context.kBase.getRete(), context.kBase.getRootClassLoader()))).forEach(compositeUpdater::add);
kBase.enqueueModification(compositeUpdater);
}
}
for (String kbaseToRemove : kbasesToRemove) {
kBases.remove(kbaseToRemove);
}
// remove sessions that no longer exist
this.kSessions.entrySet().removeIf(ksession -> kProject.getKieSessionModel(ksession.getKey()) == null);
this.statelessKSessions.entrySet().removeIf(ksession -> kProject.getKieSessionModel(ksession.getKey()) == null);
return results;
}
use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.
the class KieContainerImpl method getKieBase.
public KieBase getKieBase(String kBaseName) {
KieBase kBase = kBases.get(kBaseName);
if (kBase == null) {
KieBaseModelImpl kBaseModel = getKieBaseModelImpl(kBaseName);
synchronized (kBaseModel) {
kBase = kBases.get(kBaseName);
if (kBase == null) {
BuildContext buildContext = new BuildContext();
kBase = createKieBase(kBaseModel, kProject, buildContext, null);
if (kBase == null) {
// build error, throw runtime exception
throw new RuntimeException("Error while creating KieBase" + buildContext.getMessages().filterMessages(Level.ERROR));
}
kBases.put(kBaseName, kBase);
}
}
}
return kBase;
}
use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.
the class AbstractKieProject method verify.
private void verify(String[] kBaseNames, BuildContext buildContext) {
for (String modelName : kBaseNames) {
KieBaseModelImpl kieBaseModel = (KieBaseModelImpl) kBaseModels.get(modelName);
if (kieBaseModel == null) {
throw new RuntimeException("Unknown KieBase. Cannot find a KieBase named: " + modelName);
}
buildKnowledgePackages(kieBaseModel, buildContext);
}
}
use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.
the class KieBuilderTest method testKieModuleDependencies.
@Test
public void testKieModuleDependencies() throws ClassNotFoundException, InterruptedException, IOException {
KieServices ks = KieServices.Factory.get();
String namespace1 = "org.kie.test1";
ReleaseId releaseId1 = KieServices.Factory.get().newReleaseId(namespace1, "memory", "1.0-SNAPSHOT");
KieModuleModel kProj1 = createKieProject(namespace1);
KieFileSystem kfs1 = KieServices.Factory.get().newKieFileSystem();
generateAll(kfs1, namespace1, releaseId1, kProj1);
KieBuilder kb1 = createKieBuilder(kfs1);
kb1.buildAll();
if (kb1.getResults().hasMessages(Level.ERROR)) {
fail("Unable to build KieJar\n" + kb1.getResults().toString());
}
KieRepository kr = ks.getRepository();
KieModule kModule1 = kr.getKieModule(releaseId1);
assertNotNull(kModule1);
String namespace2 = "org.kie.test2";
ReleaseId releaseId2 = KieServices.Factory.get().newReleaseId(namespace2, "memory", "1.0-SNAPSHOT");
KieModuleModel kProj2 = createKieProject(namespace2);
KieBaseModelImpl kieBase2 = (KieBaseModelImpl) kProj2.getKieBaseModels().get(namespace2);
kieBase2.addInclude(namespace1);
KieFileSystem kfs2 = KieServices.Factory.get().newKieFileSystem();
generateAll(kfs2, namespace2, releaseId2, kProj2);
KieBuilder kb2 = createKieBuilder(kfs2);
kb2.setDependencies(kModule1);
kb2.buildAll();
if (kb2.getResults().hasMessages(Level.ERROR)) {
fail("Unable to build KieJar\n" + kb2.getResults().toString());
}
KieModule kModule2 = kr.getKieModule(releaseId2);
assertNotNull(kModule2);
KieContainer kContainer = ks.newKieContainer(releaseId2);
KieBase kBase = kContainer.getKieBase(namespace2);
KieSession kSession = kBase.newKieSession();
List list = new ArrayList();
kSession.setGlobal("list", list);
kSession.fireAllRules();
assertEquals(2, list.size());
if ("org.kie.test1.Message".equals(list.get(0).getClass().getName())) {
assertEquals("org.kie.test2.Message", list.get(1).getClass().getName());
} else {
assertEquals("org.kie.test2.Message", list.get(0).getClass().getName());
assertEquals("org.kie.test1.Message", list.get(1).getClass().getName());
}
}
use of org.drools.compiler.kproject.models.KieBaseModelImpl 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);
}
}
}
Aggregations