Search in sources :

Example 1 with KieJarChangeSet

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;
}
Also used : KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) ArrayList(java.util.ArrayList) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieBase(org.kie.api.KieBase) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 2 with KieJarChangeSet

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));
}
Also used : KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 3 with KieJarChangeSet

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 ) );
}
Also used : KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) ResourceChange(org.kie.internal.builder.ResourceChange) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 4 with KieJarChangeSet

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") ) );
}
Also used : KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 5 with KieJarChangeSet

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;
}
Also used : ResourceChange(org.kie.internal.builder.ResourceChange) ChangeType(org.kie.internal.builder.ChangeType) LoggerFactory(org.slf4j.LoggerFactory) SessionConfigurationImpl(org.drools.core.SessionConfigurationImpl) ReleaseId(org.kie.api.builder.ReleaseId) InjectionHelper.wireSessionComponents(org.drools.compiler.kie.util.InjectionHelper.wireSessionComponents) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Map(java.util.Map) StatefulSessionPool(org.drools.kiesession.session.StatefulSessionPool) KieSession(org.kie.api.runtime.KieSession) KieLoggers(org.kie.api.logger.KieLoggers) KieRuntimeEventManager(org.kie.api.event.KieRuntimeEventManager) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) Collection(java.util.Collection) MBeansOption(org.kie.api.conf.MBeansOption) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Results(org.kie.api.builder.Results) UUID(java.util.UUID) ObjectName(javax.management.ObjectName) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) DroolsManagementAgent(org.drools.core.management.DroolsManagementAgent) Environment(org.kie.api.runtime.Environment) List(java.util.List) KieModule(org.kie.api.builder.KieModule) KieSessionModel(org.kie.api.builder.model.KieSessionModel) MaterializedLambda(org.drools.compiler.kie.builder.MaterializedLambda) Entry(java.util.Map.Entry) FileLoggerModel(org.kie.api.builder.model.FileLoggerModel) KieContainerMonitor(org.drools.compiler.management.KieContainerMonitor) RuntimeComponentFactory(org.drools.core.reteoo.RuntimeComponentFactory) ClassUtils.convertResourceToClassName(org.drools.core.util.ClassUtils.convertResourceToClassName) StatelessKnowledgeSessionImpl(org.drools.kiesession.session.StatelessKnowledgeSessionImpl) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) Drools.isJndiAvailable(org.drools.core.util.Drools.isJndiAvailable) ResourceType(org.kie.api.io.ResourceType) AlphaNetworkCompilerOption(org.kie.internal.builder.conf.AlphaNetworkCompilerOption) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) KieRepository(org.kie.api.builder.KieRepository) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) HashSet(java.util.HashSet) Level(org.kie.api.builder.Message.Level) SessionConfiguration(org.drools.core.SessionConfiguration) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) KieServices(org.kie.api.KieServices) Message(org.kie.api.builder.Message) KieBase(org.kie.api.KieBase) KieSessionModelImpl(org.drools.compiler.kproject.models.KieSessionModelImpl) RuleBase(org.drools.core.impl.RuleBase) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalKieContainer(org.drools.core.impl.InternalKieContainer) Logger(org.slf4j.Logger) ClassUtils(org.drools.core.util.ClassUtils) Calendar(org.kie.api.time.Calendar) KieService(org.kie.api.internal.utils.KieService) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) CBSKey(org.drools.core.management.DroolsManagementAgent.CBSKey) KieContainerSessionsPool(org.kie.api.runtime.KieContainerSessionsPool) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) Collectors.toList(java.util.stream.Collectors.toList) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) Collections(java.util.Collections) InputStream(java.io.InputStream) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ArrayList(java.util.ArrayList) AlphaNetworkCompilerOption(org.kie.internal.builder.conf.AlphaNetworkCompilerOption) KieBase(org.kie.api.KieBase) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieBaseModel(org.kie.api.builder.model.KieBaseModel)

Aggregations

KieJarChangeSet (org.drools.compiler.kie.util.KieJarChangeSet)9 ResourceChangeSet (org.kie.internal.builder.ResourceChangeSet)7 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 KieBaseModel (org.kie.api.builder.model.KieBaseModel)3 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 KieBaseModelImpl (org.drools.compiler.kproject.models.KieBaseModelImpl)2 KieBase (org.kie.api.KieBase)2 ResourceChange (org.kie.internal.builder.ResourceChange)2 InputStream (java.io.InputStream)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 UUID (java.util.UUID)1