use of org.drools.compiler.kie.util.KieJarChangeSet in project drools by kiegroup.
the class KieContainerImpl method update.
private Results update(final InternalKieModule currentKM, final ReleaseId newReleaseId) {
final InternalKieModule newKM = (InternalKieModule) kr.getKieModule(newReleaseId);
if (newKM == null) {
return null;
}
final KieJarChangeSet cs = currentKM.getChanges(newKM);
List<String> modifiedClassNames = getModifiedClasses(cs);
final boolean modifyingUsedClass = isModifyingUsedClass(modifiedClassNames, getClassLoader());
final List<Class<?>> modifiedClasses = reinitModifiedClasses(newKM, modifiedClassNames, getClassLoader());
final List<String> unchangedResources = getUnchangedResources(newKM, cs);
Map<String, KieBaseModel> currentKieBaseModels = ((KieModuleKieProject) kProject).updateToModule(newKM);
final ResultsImpl results = new ResultsImpl();
List<String> kbasesToRemove = new ArrayList<String>();
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();
KieBaseUpdateContext context = new KieBaseUpdateContext(kProject, kBase, currentKM, newReleaseId, newKM, cs, modifiedClasses, modifyingUsedClass, unchangedResources, results, newKieBaseModel, currentKieBaseModel);
kBase.enqueueModification(currentKM.createKieBaseUpdater(context));
}
}
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.kie.util.KieJarChangeSet in project drools by kiegroup.
the class ChangeSetBuilderTest method testRemoved.
@Test
public void testRemoved() {
String drl1 = "package org.drools\n" + "rule R1 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
String drl2 = "package org.drools\n" + "rule R2 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
InternalKieModule kieJar1 = createKieJar(drl1, drl2);
InternalKieModule kieJar2 = createKieJar(drl1);
KieJarChangeSet changes = ChangeSetBuilder.build(kieJar1, kieJar2);
String removedFile = (String) kieJar1.getFileNames().toArray()[1];
assertThat(changes.getChanges().size(), is(1));
ResourceChangeSet cs = changes.getChanges().get(removedFile);
assertThat(cs, not(nullValue()));
assertThat(cs.getChangeType(), is(ChangeType.REMOVED));
}
use of org.drools.compiler.kie.util.KieJarChangeSet in project drools by kiegroup.
the class ChangeSetBuilderTest method testModified.
@Test
public void testModified() {
String drl1 = "package org.drools\n" + "rule R1 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
String drl2 = "package org.drools\n" + "rule R2 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
String drl3 = "package org.drools\n" + "rule R3 when\n" + " $m : Message( message == \"Good bye World\" )\n" + "then\n" + "end\n";
InternalKieModule kieJar1 = createKieJar(drl1, drl2);
InternalKieModule kieJar2 = createKieJar(drl1, drl3);
KieJarChangeSet changes = ChangeSetBuilder.build(kieJar1, kieJar2);
String modifiedFile = (String) kieJar2.getFileNames().toArray()[1];
assertThat(changes.getChanges().size(), is(1));
ResourceChangeSet cs = changes.getChanges().get(modifiedFile);
assertThat(cs, not(nullValue()));
assertThat(cs.getChangeType(), is(ChangeType.UPDATED));
assertThat(cs.getChanges().size(), is(2));
assertThat(cs.getChanges().get(1), is(new ResourceChange(ChangeType.ADDED, Type.RULE, "R3")));
assertThat(cs.getChanges().get(0), is(new ResourceChange(ChangeType.REMOVED, Type.RULE, "R2")));
// ChangeSetBuilder builder = new ChangeSetBuilder();
// System.out.println( builder.toProperties( changes ) );
}
use of org.drools.compiler.kie.util.KieJarChangeSet in project drools by kiegroup.
the class ChangeSetBuilderTest method testModified2.
@Test
public void testModified2() {
String drl1 = "package org.drools\n" + "rule \"Rule 1\" when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule \"An updated rule\" when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule \"A removed rule\" when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
String drl1_5 = "package org.drools\n" + "rule \"Rule 1\" when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule \"An updated rule\" when\n" + " $m : Message( message == \"Good Bye World\" )\n" + "then\n" + "end\n" + "rule \"An added rule\" when\n" + " $m : Message( message == \"Good Bye World\" )\n" + "then\n" + "end\n";
String drl2 = "package org.drools\n" + "rule \"This is the name of rule 3\" when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
String drl3 = "package org.drools\n" + "rule \"Another dumb rule\" when\n" + " $m : Message( message == \"Good bye World\" )\n" + "then\n" + "end\n";
InternalKieModule kieJar1 = createKieJar(drl1, drl2);
InternalKieModule kieJar2 = createKieJar(drl1_5, null, drl3);
KieJarChangeSet changes = ChangeSetBuilder.build(kieJar1, kieJar2);
// System.out.println( builder.toProperties( changes ) );
String modifiedFile = (String) kieJar2.getFileNames().toArray()[0];
String addedFile = (String) kieJar2.getFileNames().toArray()[1];
String removedFile = (String) kieJar1.getFileNames().toArray()[1];
assertThat(changes.getChanges().size(), is(3));
ResourceChangeSet cs = changes.getChanges().get(removedFile);
assertThat(cs, not(nullValue()));
assertThat(cs.getChangeType(), is(ChangeType.REMOVED));
assertThat(cs.getChanges().size(), is(0));
cs = changes.getChanges().get(addedFile);
assertThat(cs, not(nullValue()));
assertThat(cs.getChangeType(), is(ChangeType.ADDED));
assertThat(cs.getChanges().size(), is(0));
cs = changes.getChanges().get(modifiedFile);
assertThat(cs, not(nullValue()));
assertThat(cs.getChangeType(), is(ChangeType.UPDATED));
// assertThat( cs.getChanges().size(), is(3) );
// assertThat( cs.getChanges().get( 0 ), is( new ResourceChange(ChangeType.ADDED, Type.RULE, "An added rule") ) );
// assertThat( cs.getChanges().get( 1 ), is( new ResourceChange(ChangeType.REMOVED, Type.RULE, "A removed rule") ) );
// assertThat( cs.getChanges().get( 2 ), is( new ResourceChange(ChangeType.UPDATED, Type.RULE, "An updated rule") ) );
}
use of org.drools.compiler.kie.util.KieJarChangeSet 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;
}
Aggregations