From b18aedf084babad27198d7479ba45291309550cb Mon Sep 17 00:00:00 2001 From: Mario Fusco Date: Mon, 17 Jul 2023 18:27:15 +0200 Subject: [PATCH] [DROOLS-7501] avoid using TMS components when not strictly necessary (#5393) * [DROOLS-7501] avoid using TMS components when not strictly necessary * wip * wip --- .../core/common/ActivationsManager.java | 1 + .../drools/core/common/InternalAgenda.java | 6 +- .../org/drools/core/common/ReteEvaluator.java | 13 ++-- .../drools/core/concurrent/RuleEvaluator.java | 2 + .../concurrent/SequentialRuleEvaluator.java | 8 ++- .../core/impl/ActivationsManagerImpl.java | 29 ++++---- .../core/reteoo/AbstractTerminalNode.java | 2 +- .../core/reteoo/AgendaComponentFactory.java | 2 +- .../agenda/CompositeDefaultAgenda.java | 5 ++ .../kiesession/agenda/DefaultAgenda.java | 4 ++ .../consequence/DefaultKnowledgeHelper.java | 66 ++++++++++------- .../StatefulKnowledgeSessionForRHS.java | 44 +++++++----- .../session/StatefulKnowledgeSessionImpl.java | 71 +++++++++++-------- .../impl/sessions/RuleUnitExecutorImpl.java | 44 +++++++----- .../protobuf/ProtobufInputMarshaller.java | 17 ++--- .../protobuf/TruthMaintenanceTest.java | 37 ++++------ ...nanceSystemKnowledgeHelperFactoryImpl.java | 3 +- .../main/java/org/drools/util/ObjectPool.java | 3 +- 18 files changed, 213 insertions(+), 144 deletions(-) diff --git a/drools-core/src/main/java/org/drools/core/common/ActivationsManager.java b/drools-core/src/main/java/org/drools/core/common/ActivationsManager.java index addaae76518..413fd4b9970 100644 --- a/drools-core/src/main/java/org/drools/core/common/ActivationsManager.java +++ b/drools-core/src/main/java/org/drools/core/common/ActivationsManager.java @@ -75,6 +75,7 @@ InternalMatch createAgendaItem(RuleTerminalNodeLeftTuple rtnLeftTuple, void evaluateQueriesForRule(RuleAgendaItem item); KnowledgeHelper getKnowledgeHelper(); + void resetKnowledgeHelper(); void executeTask(ExecutableEntry executableEntry); diff --git a/drools-core/src/main/java/org/drools/core/common/InternalAgenda.java b/drools-core/src/main/java/org/drools/core/common/InternalAgenda.java index 02fb2c9c880..cf3d6db5ebd 100644 --- a/drools-core/src/main/java/org/drools/core/common/InternalAgenda.java +++ b/drools-core/src/main/java/org/drools/core/common/InternalAgenda.java @@ -16,13 +16,13 @@ package org.drools.core.common; -import java.util.Iterator; -import java.util.Map; - import org.drools.core.phreak.PropagationEntry; import org.kie.api.runtime.rule.Agenda; import org.kie.api.runtime.rule.AgendaFilter; +import java.util.Iterator; +import java.util.Map; + public interface InternalAgenda extends Agenda, ActivationsManager { /** diff --git a/drools-core/src/main/java/org/drools/core/common/ReteEvaluator.java b/drools-core/src/main/java/org/drools/core/common/ReteEvaluator.java index bcad264fc93..aa88baa1415 100644 --- a/drools-core/src/main/java/org/drools/core/common/ReteEvaluator.java +++ b/drools-core/src/main/java/org/drools/core/common/ReteEvaluator.java @@ -16,11 +16,8 @@ package org.drools.core.common; -import java.util.Collection; -import java.util.Collections; -import java.util.function.Consumer; - import org.drools.base.base.ValueResolver; +import org.drools.base.rule.EntryPointId; import org.drools.core.RuleSessionConfiguration; import org.drools.core.SessionConfiguration; import org.drools.core.WorkingMemoryEntryPoint; @@ -31,7 +28,6 @@ import org.drools.core.phreak.PropagationEntry; import org.drools.core.reteoo.ObjectTypeConf; import org.drools.core.reteoo.RuntimeComponentFactory; -import org.drools.base.rule.EntryPointId; import org.drools.core.rule.accessor.FactHandleFactory; import org.drools.core.rule.consequence.KnowledgeHelper; import org.drools.core.time.TimerService; @@ -43,6 +39,10 @@ import org.kie.api.runtime.rule.QueryResults; import org.kie.api.time.SessionClock; +import java.util.Collection; +import java.util.Collections; +import java.util.function.Consumer; + public interface ReteEvaluator extends ValueResolver { enum InternalOperationType{ FIRE, INSERT, UPDATE, DELETE, SET_GLOBAL } @@ -123,6 +123,9 @@ default boolean isSequential() { default void startOperation(InternalOperationType operationType) { } default void endOperation(InternalOperationType operationType) { } + boolean isTMSEnabled(); + void enableTMS(); + default KnowledgeHelper createKnowledgeHelper() { return RuntimeComponentFactory.get().createKnowledgeHelper(this); } diff --git a/drools-core/src/main/java/org/drools/core/concurrent/RuleEvaluator.java b/drools-core/src/main/java/org/drools/core/concurrent/RuleEvaluator.java index b943abae63e..ea794108826 100644 --- a/drools-core/src/main/java/org/drools/core/concurrent/RuleEvaluator.java +++ b/drools-core/src/main/java/org/drools/core/concurrent/RuleEvaluator.java @@ -27,4 +27,6 @@ int evaluateAndFire( AgendaFilter filter, InternalAgendaGroup group ); KnowledgeHelper getKnowledgeHelper(); + + void resetKnowledgeHelper(); } diff --git a/drools-core/src/main/java/org/drools/core/concurrent/SequentialRuleEvaluator.java b/drools-core/src/main/java/org/drools/core/concurrent/SequentialRuleEvaluator.java index 5a26374bbb2..4ca0b9fc044 100644 --- a/drools-core/src/main/java/org/drools/core/concurrent/SequentialRuleEvaluator.java +++ b/drools-core/src/main/java/org/drools/core/concurrent/SequentialRuleEvaluator.java @@ -26,7 +26,7 @@ public class SequentialRuleEvaluator extends AbstractRuleEvaluator implements Ru private final boolean sequential; - private final KnowledgeHelper knowledgeHelper; + private KnowledgeHelper knowledgeHelper; public SequentialRuleEvaluator( ActivationsManager activationsManager ) { super(activationsManager); @@ -43,7 +43,13 @@ public int evaluateAndFire( AgendaFilter filter, return item != null ? internalEvaluateAndFire( filter, fireCount, fireLimit, item ) : 0; } + @Override public KnowledgeHelper getKnowledgeHelper() { return knowledgeHelper; } + + @Override + public void resetKnowledgeHelper() { + knowledgeHelper = newKnowledgeHelper(); + } } diff --git a/drools-core/src/main/java/org/drools/core/impl/ActivationsManagerImpl.java b/drools-core/src/main/java/org/drools/core/impl/ActivationsManagerImpl.java index 8f54a53de3a..f2adb090e20 100644 --- a/drools-core/src/main/java/org/drools/core/impl/ActivationsManagerImpl.java +++ b/drools-core/src/main/java/org/drools/core/impl/ActivationsManagerImpl.java @@ -16,25 +16,21 @@ package org.drools.core.impl; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - +import org.drools.base.definitions.rule.impl.QueryImpl; +import org.drools.base.definitions.rule.impl.RuleImpl; import org.drools.core.common.ActivationGroupImpl; import org.drools.core.common.ActivationGroupNode; import org.drools.core.common.ActivationsFilter; import org.drools.core.common.ActivationsManager; import org.drools.core.common.AgendaGroupsManager; +import org.drools.core.common.InternalActivationGroup; import org.drools.core.common.InternalAgendaGroup; import org.drools.core.common.InternalFactHandle; import org.drools.core.common.InternalWorkingMemoryEntryPoint; +import org.drools.core.common.PropagationContext; import org.drools.core.common.ReteEvaluator; import org.drools.core.concurrent.RuleEvaluator; import org.drools.core.concurrent.SequentialRuleEvaluator; -import org.drools.base.definitions.rule.impl.RuleImpl; import org.drools.core.event.AgendaEventSupport; import org.drools.core.phreak.ExecutableEntry; import org.drools.core.phreak.PropagationEntry; @@ -47,17 +43,21 @@ import org.drools.core.reteoo.PathMemory; import org.drools.core.reteoo.RuleTerminalNodeLeftTuple; import org.drools.core.reteoo.TerminalNode; -import org.drools.base.definitions.rule.impl.QueryImpl; +import org.drools.core.reteoo.Tuple; import org.drools.core.rule.consequence.InternalMatch; -import org.drools.core.common.InternalActivationGroup; import org.drools.core.rule.consequence.KnowledgeHelper; -import org.drools.core.common.PropagationContext; -import org.drools.core.reteoo.Tuple; import org.drools.util.StringUtils; import org.kie.api.conf.EventProcessingOption; import org.kie.api.event.rule.MatchCancelledCause; import org.kie.api.runtime.rule.AgendaFilter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + public class ActivationsManagerImpl implements ActivationsManager { private final ReteEvaluator reteEvaluator; @@ -262,6 +262,11 @@ public KnowledgeHelper getKnowledgeHelper() { return ruleEvaluator.getKnowledgeHelper(); } + @Override + public void resetKnowledgeHelper() { + ruleEvaluator.resetKnowledgeHelper(); + } + @Override public void executeTask(ExecutableEntry executableEntry) { executableEntry.execute(); diff --git a/drools-core/src/main/java/org/drools/core/reteoo/AbstractTerminalNode.java b/drools-core/src/main/java/org/drools/core/reteoo/AbstractTerminalNode.java index 41eca410c84..f0cdf1219d5 100644 --- a/drools-core/src/main/java/org/drools/core/reteoo/AbstractTerminalNode.java +++ b/drools-core/src/main/java/org/drools/core/reteoo/AbstractTerminalNode.java @@ -255,7 +255,7 @@ public static PathMemory initPathMemory( PathEndNode pathEndNode, PathMemory pme } public LeftTuple createPeer(LeftTuple original) { - RuleTerminalNodeLeftTuple peer = (RuleTerminalNodeLeftTuple) AgendaComponentFactory.get().createTerminalTuple(); + AbstractLeftTuple peer = (AbstractLeftTuple) AgendaComponentFactory.get().createTerminalTuple(); peer.initPeer((AbstractLeftTuple) original, this); original.setPeer( peer ); return peer; diff --git a/drools-core/src/main/java/org/drools/core/reteoo/AgendaComponentFactory.java b/drools-core/src/main/java/org/drools/core/reteoo/AgendaComponentFactory.java index 15a419f6362..6c1bc344e53 100644 --- a/drools-core/src/main/java/org/drools/core/reteoo/AgendaComponentFactory.java +++ b/drools-core/src/main/java/org/drools/core/reteoo/AgendaComponentFactory.java @@ -46,7 +46,7 @@ static AgendaComponentFactory get() { return AgendaComponentFactory.Holder.INSTANCE; } - public class AgendaComponentFactoryImpl implements AgendaComponentFactory { + class AgendaComponentFactoryImpl implements AgendaComponentFactory { public AgendaComponentFactoryImpl() { } diff --git a/drools-kiesession/src/main/java/org/drools/kiesession/agenda/CompositeDefaultAgenda.java b/drools-kiesession/src/main/java/org/drools/kiesession/agenda/CompositeDefaultAgenda.java index 8f05cc5218d..840176b9184 100644 --- a/drools-kiesession/src/main/java/org/drools/kiesession/agenda/CompositeDefaultAgenda.java +++ b/drools-kiesession/src/main/java/org/drools/kiesession/agenda/CompositeDefaultAgenda.java @@ -494,6 +494,11 @@ public KnowledgeHelper getKnowledgeHelper() { throw new UnsupportedOperationException( "This method has to be called on the single partitioned agendas" ); } + @Override + public void resetKnowledgeHelper() { + throw new UnsupportedOperationException( "This method has to be called on the single partitioned agendas" ); + } + @Override public boolean isParallelAgenda() { return true; diff --git a/drools-kiesession/src/main/java/org/drools/kiesession/agenda/DefaultAgenda.java b/drools-kiesession/src/main/java/org/drools/kiesession/agenda/DefaultAgenda.java index 266b2d74784..6508493feca 100644 --- a/drools-kiesession/src/main/java/org/drools/kiesession/agenda/DefaultAgenda.java +++ b/drools-kiesession/src/main/java/org/drools/kiesession/agenda/DefaultAgenda.java @@ -893,6 +893,10 @@ public KnowledgeHelper getKnowledgeHelper() { return ruleEvaluator.getKnowledgeHelper(); } + public void resetKnowledgeHelper() { + ruleEvaluator.resetKnowledgeHelper(); + } + @Override public void addPropagation(PropagationEntry propagationEntry) { propagationList.addEntry( propagationEntry ); diff --git a/drools-kiesession/src/main/java/org/drools/kiesession/consequence/DefaultKnowledgeHelper.java b/drools-kiesession/src/main/java/org/drools/kiesession/consequence/DefaultKnowledgeHelper.java index 6b6135e5f1e..42972682b27 100644 --- a/drools-kiesession/src/main/java/org/drools/kiesession/consequence/DefaultKnowledgeHelper.java +++ b/drools-kiesession/src/main/java/org/drools/kiesession/consequence/DefaultKnowledgeHelper.java @@ -16,33 +16,25 @@ package org.drools.kiesession.consequence; -import java.io.Externalizable; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - +import org.drools.base.beliefsystem.Mode; +import org.drools.base.definitions.rule.impl.RuleImpl; +import org.drools.base.factmodel.traits.CoreWrapper; +import org.drools.base.factmodel.traits.Thing; +import org.drools.base.factmodel.traits.TraitableBean; +import org.drools.base.rule.Declaration; import org.drools.core.RuleBaseConfiguration; import org.drools.core.WorkingMemory; -import org.drools.base.beliefsystem.Mode; import org.drools.core.common.InternalFactHandle; import org.drools.core.common.InternalRuleFlowGroup; import org.drools.core.common.InternalWorkingMemoryEntryPoint; import org.drools.core.common.ReteEvaluator; import org.drools.core.common.TruthMaintenanceSystemFactory; -import org.drools.base.definitions.rule.impl.RuleImpl; -import org.drools.base.factmodel.traits.CoreWrapper; -import org.drools.base.factmodel.traits.Thing; -import org.drools.base.factmodel.traits.TraitableBean; +import org.drools.core.process.AbstractProcessContext; import org.drools.core.reteoo.LeftTuple; import org.drools.core.reteoo.RuleTerminalNode; -import org.drools.base.rule.Declaration; -import org.drools.core.process.AbstractProcessContext; +import org.drools.core.reteoo.Tuple; import org.drools.core.rule.consequence.InternalMatch; import org.drools.core.rule.consequence.KnowledgeHelper; -import org.drools.core.reteoo.Tuple; import org.drools.core.util.bitmask.BitMask; import org.drools.kiesession.rulebase.InternalKnowledgeBase; import org.drools.kiesession.session.StatefulKnowledgeSessionImpl; @@ -57,6 +49,14 @@ import org.kie.api.runtime.rule.FactHandle; import org.kie.api.runtime.rule.Match; +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; + import static org.drools.base.reteoo.PropertySpecificUtil.allSetButTraitBitMask; import static org.drools.base.reteoo.PropertySpecificUtil.onlyTraitBitSetMask; @@ -70,6 +70,8 @@ public class DefaultKnowledgeHelper implements KnowledgeHelper, Externalizable { protected ReteEvaluator reteEvaluator; private StatefulKnowledgeSessionForRHS wrappedEvaluator; + private KnowledgeHelper tmsKnowledgeHelper; + public DefaultKnowledgeHelper() { } public DefaultKnowledgeHelper(ReteEvaluator reteEvaluator) { @@ -104,11 +106,11 @@ public void reset() { } public void blockMatch(Match act) { - TruthMaintenanceSystemFactory.throwExceptionForMissingTms(); + executeOnTMS().blockMatch(act); } public void unblockAllMatches(Match act) { - TruthMaintenanceSystemFactory.throwExceptionForMissingTms(); + executeOnTMS().unblockAllMatches(act); } public FactHandle insertAsync( final Object object ) { @@ -120,7 +122,7 @@ public FactHandle insert(final Object object) { } public FactHandle insert(final Object object, final boolean dynamic) { - return (InternalFactHandle) ((InternalWorkingMemoryEntryPoint) this.reteEvaluator.getDefaultEntryPoint()) + return ((InternalWorkingMemoryEntryPoint) this.reteEvaluator.getDefaultEntryPoint()) .insert(object, dynamic, this.internalMatch.getRule(), this.internalMatch.getTuple().getTupleSink()); } @@ -141,23 +143,33 @@ public FactHandle insertLogical(final Object object) { @Override public FactHandle insertLogical(Object object, Object value) { - TruthMaintenanceSystemFactory.throwExceptionForMissingTms(); - return null; + return executeOnTMS().insertLogical(object, value); } @Override public FactHandle insertLogical(EntryPoint ep, Object object) { - TruthMaintenanceSystemFactory.throwExceptionForMissingTms(); - return null; + return executeOnTMS().insertLogical(ep, object); } - public InternalFactHandle bolster( final Object object ) { + public FactHandle bolster( final Object object ) { return bolster( object, null ); } - public InternalFactHandle bolster( final Object object, final Object value ) { - TruthMaintenanceSystemFactory.throwExceptionForMissingTms(); - return null; + public FactHandle bolster( final Object object, final Object value ) { + return executeOnTMS().bolster(object, value); + } + + private KnowledgeHelper executeOnTMS() { + if (!TruthMaintenanceSystemFactory.present()) { + TruthMaintenanceSystemFactory.throwExceptionForMissingTms(); + } + if (tmsKnowledgeHelper != null) { + return tmsKnowledgeHelper; + } + reteEvaluator.enableTMS(); + tmsKnowledgeHelper = reteEvaluator.createKnowledgeHelper(); + tmsKnowledgeHelper.setActivation(internalMatch); + return tmsKnowledgeHelper; } public void cancelMatch(Match act) { diff --git a/drools-kiesession/src/main/java/org/drools/kiesession/consequence/StatefulKnowledgeSessionForRHS.java b/drools-kiesession/src/main/java/org/drools/kiesession/consequence/StatefulKnowledgeSessionForRHS.java index 84b61b23bd9..ca599accd2f 100644 --- a/drools-kiesession/src/main/java/org/drools/kiesession/consequence/StatefulKnowledgeSessionForRHS.java +++ b/drools-kiesession/src/main/java/org/drools/kiesession/consequence/StatefulKnowledgeSessionForRHS.java @@ -1,18 +1,15 @@ package org.drools.kiesession.consequence; -import java.io.Externalizable; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.Collection; -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.locks.Lock; - +import org.drools.base.RuleBase; +import org.drools.base.beliefsystem.Mode; +import org.drools.base.definitions.rule.impl.RuleImpl; +import org.drools.base.factmodel.traits.Thing; +import org.drools.base.factmodel.traits.TraitableBean; +import org.drools.base.rule.EntryPointId; +import org.drools.base.rule.accessor.GlobalResolver; import org.drools.core.RuleSessionConfiguration; import org.drools.core.SessionConfiguration; import org.drools.core.WorkingMemoryEntryPoint; -import org.drools.base.beliefsystem.Mode; import org.drools.core.common.ActivationsManager; import org.drools.core.common.EndOperationListener; import org.drools.core.common.EventSupport; @@ -26,27 +23,21 @@ import org.drools.core.common.ObjectStore; import org.drools.core.common.ObjectTypeConfigurationRegistry; import org.drools.core.common.ReteEvaluator; -import org.drools.base.definitions.rule.impl.RuleImpl; import org.drools.core.event.AgendaEventSupport; import org.drools.core.event.RuleEventListenerSupport; import org.drools.core.event.RuleRuntimeEventSupport; -import org.drools.base.factmodel.traits.Thing; -import org.drools.base.factmodel.traits.TraitableBean; import org.drools.core.phreak.PropagationEntry; import org.drools.core.reteoo.EntryPointNode; import org.drools.core.reteoo.TerminalNode; -import org.drools.base.rule.EntryPointId; +import org.drools.core.rule.accessor.FactHandleFactory; import org.drools.core.rule.consequence.InternalMatch; import org.drools.core.runtime.process.InternalProcessRuntime; -import org.drools.core.rule.accessor.FactHandleFactory; -import org.drools.base.rule.accessor.GlobalResolver; import org.drools.core.time.TimerService; import org.drools.core.util.bitmask.BitMask; import org.drools.kiesession.rulebase.InternalKnowledgeBase; import org.drools.kiesession.session.StatefulKnowledgeSessionImpl; import org.kie.api.KieBase; import org.kie.api.command.Command; -import org.drools.base.RuleBase; import org.kie.api.event.kiebase.KieBaseEventListener; import org.kie.api.event.process.ProcessEventListener; import org.kie.api.event.rule.AgendaEventListener; @@ -72,6 +63,15 @@ import org.kie.internal.event.rule.RuleEventListener; import org.kie.internal.process.CorrelationKey; +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.locks.Lock; + /** * Wrapper of ReteEvaluator so to intercept call from RHS internal Drools execution and proxy or delegate method call as appropriate. */ @@ -268,6 +268,16 @@ public void addEventListener(KieBaseEventListener listener) { delegate.addEventListener(listener); } + @Override + public void enableTMS() { + delegate.enableTMS(); + } + + @Override + public boolean isTMSEnabled() { + return delegate.isTMSEnabled(); + } + public FactHandle insert(Object object) { return delegate.insert(object); } diff --git a/drools-kiesession/src/main/java/org/drools/kiesession/session/StatefulKnowledgeSessionImpl.java b/drools-kiesession/src/main/java/org/drools/kiesession/session/StatefulKnowledgeSessionImpl.java index d9536eba62e..e110c8ab35e 100644 --- a/drools-kiesession/src/main/java/org/drools/kiesession/session/StatefulKnowledgeSessionImpl.java +++ b/drools-kiesession/src/main/java/org/drools/kiesession/session/StatefulKnowledgeSessionImpl.java @@ -16,27 +16,14 @@ package org.drools.kiesession.session; -import java.io.ByteArrayOutputStream; -import java.io.Externalizable; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; -import java.util.function.Consumer; - +import org.drools.base.RuleBase; +import org.drools.base.beliefsystem.Mode; +import org.drools.base.definitions.rule.impl.RuleImpl; +import org.drools.base.factmodel.traits.Thing; +import org.drools.base.factmodel.traits.TraitableBean; +import org.drools.base.rule.Declaration; +import org.drools.base.rule.EntryPointId; +import org.drools.base.rule.accessor.GlobalResolver; import org.drools.core.FlowSessionConfiguration; import org.drools.core.QueryResultsImpl; import org.drools.core.RuleBaseConfiguration; @@ -49,7 +36,6 @@ import org.drools.core.base.NonCloningQueryViewListener; import org.drools.core.base.QueryRowWithSubruleIndex; import org.drools.core.base.StandardQueryViewChangedEventListener; -import org.drools.base.beliefsystem.Mode; import org.drools.core.common.ActivationsManager; import org.drools.core.common.ConcurrentNodeMemories; import org.drools.core.common.EndOperationListener; @@ -67,12 +53,9 @@ import org.drools.core.common.PropagationContext; import org.drools.core.common.PropagationContextFactory; import org.drools.core.common.ReteEvaluator; -import org.drools.base.definitions.rule.impl.RuleImpl; import org.drools.core.event.AgendaEventSupport; import org.drools.core.event.RuleEventListenerSupport; import org.drools.core.event.RuleRuntimeEventSupport; -import org.drools.base.factmodel.traits.Thing; -import org.drools.base.factmodel.traits.TraitableBean; import org.drools.core.impl.AbstractRuntime; import org.drools.core.impl.EnvironmentFactory; import org.drools.core.management.DroolsManagementAgent; @@ -89,10 +72,7 @@ import org.drools.core.reteoo.RuntimeComponentFactory; import org.drools.core.reteoo.SegmentMemory; import org.drools.core.reteoo.TerminalNode; -import org.drools.base.rule.Declaration; -import org.drools.base.rule.EntryPointId; import org.drools.core.rule.accessor.FactHandleFactory; -import org.drools.base.rule.accessor.GlobalResolver; import org.drools.core.rule.consequence.InternalMatch; import org.drools.core.runtime.process.InternalProcessRuntime; import org.drools.core.runtime.rule.impl.LiveQueryImpl; @@ -105,7 +85,6 @@ import org.kie.api.command.BatchExecutionCommand; import org.kie.api.command.Command; import org.kie.api.conf.MBeansOption; -import org.drools.base.RuleBase; import org.kie.api.event.KieRuntimeEventManager; import org.kie.api.event.kiebase.KieBaseEventListener; import org.kie.api.event.process.ProcessEventListener; @@ -139,6 +118,27 @@ import org.kie.internal.process.CorrelationKey; import org.kie.internal.runtime.StatefulKnowledgeSession; +import java.io.ByteArrayOutputStream; +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Consumer; + import static java.util.stream.Collectors.toList; import static org.drools.base.base.ClassObjectType.InitialFact_ObjectType; import static org.drools.base.reteoo.PropertySpecificUtil.allSetButTraitBitMask; @@ -243,6 +243,8 @@ public class StatefulKnowledgeSessionImpl extends AbstractRuntime private Consumer workingMemoryActionListener; + private boolean tmsEnabled; + // ------------------------------------------------------------ // Constructors // ------------------------------------------------------------ @@ -1204,6 +1206,17 @@ public FactHandle insertAsync(final Object object) { return entryPointsManager.getDefaultEntryPoint().insertAsync( object ); } + @Override + public void enableTMS() { + tmsEnabled = true; + agenda.resetKnowledgeHelper(); + } + + @Override + public boolean isTMSEnabled() { + return tmsEnabled; + } + /** * @see org.drools.core.WorkingMemory */ diff --git a/drools-ruleunits/drools-ruleunits-impl/src/main/java/org/drools/ruleunits/impl/sessions/RuleUnitExecutorImpl.java b/drools-ruleunits/drools-ruleunits-impl/src/main/java/org/drools/ruleunits/impl/sessions/RuleUnitExecutorImpl.java index 889bcd13fde..2b74ea184ff 100644 --- a/drools-ruleunits/drools-ruleunits-impl/src/main/java/org/drools/ruleunits/impl/sessions/RuleUnitExecutorImpl.java +++ b/drools-ruleunits/drools-ruleunits-impl/src/main/java/org/drools/ruleunits/impl/sessions/RuleUnitExecutorImpl.java @@ -16,15 +16,12 @@ package org.drools.ruleunits.impl.sessions; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicLong; - +import org.drools.base.beliefsystem.Mode; +import org.drools.base.definitions.rule.impl.RuleImpl; +import org.drools.base.factmodel.traits.Thing; +import org.drools.base.factmodel.traits.TraitableBean; +import org.drools.base.rule.Declaration; +import org.drools.base.rule.accessor.GlobalResolver; import org.drools.core.EntryPointsManager; import org.drools.core.QueryResultsImpl; import org.drools.core.RuleSessionConfiguration; @@ -36,7 +33,6 @@ import org.drools.core.base.MapGlobalResolver; import org.drools.core.base.NonCloningQueryViewListener; import org.drools.core.base.QueryRowWithSubruleIndex; -import org.drools.base.beliefsystem.Mode; import org.drools.core.common.ActivationsManager; import org.drools.core.common.ConcurrentNodeMemories; import org.drools.core.common.InternalFactHandle; @@ -48,12 +44,9 @@ import org.drools.core.common.PropagationContext; import org.drools.core.common.PropagationContextFactory; import org.drools.core.common.ReteEvaluator; -import org.drools.base.definitions.rule.impl.RuleImpl; import org.drools.core.event.AgendaEventSupport; import org.drools.core.event.RuleEventListenerSupport; import org.drools.core.event.RuleRuntimeEventSupport; -import org.drools.base.factmodel.traits.Thing; -import org.drools.base.factmodel.traits.TraitableBean; import org.drools.core.impl.ActivationsManagerImpl; import org.drools.core.impl.InternalRuleBase; import org.drools.core.phreak.PropagationEntry; @@ -61,9 +54,7 @@ import org.drools.core.reteoo.RuntimeComponentFactory; import org.drools.core.reteoo.TerminalNode; import org.drools.core.reteoo.Tuple; -import org.drools.base.rule.Declaration; import org.drools.core.rule.accessor.FactHandleFactory; -import org.drools.base.rule.accessor.GlobalResolver; import org.drools.core.rule.consequence.InternalMatch; import org.drools.core.rule.consequence.KnowledgeHelper; import org.drools.core.time.TimerService; @@ -84,6 +75,15 @@ import org.kie.api.runtime.rule.QueryResults; import org.kie.api.time.SessionClock; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicLong; + import static org.drools.base.base.ClassObjectType.InitialFact_ObjectType; public class RuleUnitExecutorImpl implements ReteEvaluator { @@ -112,6 +112,8 @@ public class RuleUnitExecutorImpl implements ReteEvaluator { private RuleUnits ruleUnits; + private boolean tmsEnabled; + public RuleUnitExecutorImpl(InternalRuleBase knowledgeBase) { this(knowledgeBase, knowledgeBase.getSessionConfiguration().as(SessionConfiguration.KEY)); } @@ -338,6 +340,16 @@ public void setRuleUnits(RuleUnits ruleUnits) { this.ruleUnits = ruleUnits; } + @Override + public void enableTMS() { + tmsEnabled = true; + } + + @Override + public boolean isTMSEnabled() { + return tmsEnabled; + } + @Override public KnowledgeHelper createKnowledgeHelper() { return new RuleUnitKnowledgeHelper((DefaultKnowledgeHelper) ReteEvaluator.super.createKnowledgeHelper(), this); @@ -641,7 +653,7 @@ public FactHandle insertLogical(EntryPoint ep, Object object) { } @Override - public InternalFactHandle bolster(Object object) { + public FactHandle bolster(Object object) { return knowledgeHelper.bolster(object); } diff --git a/drools-serialization-protobuf/src/main/java/org/drools/serialization/protobuf/ProtobufInputMarshaller.java b/drools-serialization-protobuf/src/main/java/org/drools/serialization/protobuf/ProtobufInputMarshaller.java index c9576221734..7766a2d08d5 100644 --- a/drools-serialization-protobuf/src/main/java/org/drools/serialization/protobuf/ProtobufInputMarshaller.java +++ b/drools-serialization-protobuf/src/main/java/org/drools/serialization/protobuf/ProtobufInputMarshaller.java @@ -260,10 +260,7 @@ public static StatefulKnowledgeSessionImpl readSession( ProtobufMessages.Knowled context.getFilter().fireRNEAs( context.getWorkingMemory() ); - readTruthMaintenanceSystem( context, - wmep, - _ep, - pctxs ); + readTruthMaintenanceSystem( session, context, wmep, _ep, pctxs ); context.getWorkingMemory().getFactHandleFactory().stopRecycleIds(); } @@ -543,7 +540,8 @@ public static InternalFactHandle readFactHandle( ProtobufMarshallerReaderContext return handle; } - public static void readTruthMaintenanceSystem( ProtobufMarshallerReaderContext context, + public static void readTruthMaintenanceSystem( StatefulKnowledgeSessionImpl session, + ProtobufMarshallerReaderContext context, EntryPoint wmep, ProtobufMessages.EntryPoint _ep, List pctxs) throws IOException, @@ -552,8 +550,8 @@ public static void readTruthMaintenanceSystem( ProtobufMarshallerReaderContext c boolean wasOTCSerialized = _ep.getOtcCount() > 0; // if 0, then the OTC was not serialized (older versions of drools) Set tmsEnabled = new HashSet<>(); - for( ObjectTypeConfiguration _otc : _ep.getOtcList() ) { - if( _otc.getTmsEnabled() ) { + for ( ObjectTypeConfiguration _otc : _ep.getOtcList() ) { + if ( _otc.getTmsEnabled() ) { tmsEnabled.add( _otc.getType() ); } } @@ -561,7 +559,7 @@ public static void readTruthMaintenanceSystem( ProtobufMarshallerReaderContext c ProtobufMessages.TruthMaintenanceSystem _tms = _ep.getTms(); for ( ProtobufMessages.EqualityKey _key : _tms.getKeyList() ) { - InternalFactHandle handle = (InternalFactHandle) context.getHandles().get( _key.getHandleId() ); + InternalFactHandle handle = context.getHandles().get( _key.getHandleId() ); // ObjectTypeConf state is not marshalled, so it needs to be re-determined ObjectTypeConf typeConf = context.getWorkingMemory().getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf( handle.getEntryPointId(), @@ -595,6 +593,9 @@ public static void readTruthMaintenanceSystem( ProtobufMarshallerReaderContext c readBeliefSet( context, tms, _key ); } + if ( tms.getEqualityKeysSize() != 0 ) { + session.enableTMS(); + } } private static void readBeliefSet( MarshallerReaderContext context, diff --git a/drools-serialization-protobuf/src/test/java/org/drools/serialization/protobuf/TruthMaintenanceTest.java b/drools-serialization-protobuf/src/test/java/org/drools/serialization/protobuf/TruthMaintenanceTest.java index f4244df8066..4271831e8a4 100644 --- a/drools-serialization-protobuf/src/test/java/org/drools/serialization/protobuf/TruthMaintenanceTest.java +++ b/drools-serialization-protobuf/src/test/java/org/drools/serialization/protobuf/TruthMaintenanceTest.java @@ -14,12 +14,6 @@ package org.drools.serialization.protobuf; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import org.drools.core.ClassObjectFilter; import org.drools.core.common.EqualityKey; import org.drools.core.common.InternalAgenda; @@ -46,6 +40,12 @@ import org.kie.internal.builder.KnowledgeBuilderFactory; import org.kie.internal.io.ResourceFactory; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; import static org.drools.serialization.protobuf.SerializationHelper.getSerialisedStatefulKnowledgeSession; @@ -702,20 +702,16 @@ public void testLogicalInsertionsModifySameRuleGivesDifferentLogicalInsertion() kbase.addPackages( pkgs ); KieSession session = createKnowledgeSession(kbase); try { - Sensor sensor1 = new Sensor( 100, - 0 ); + Sensor sensor1 = new Sensor( 100, 0 ); FactHandle sensor1Handle = session.insert( sensor1 ); - Sensor sensor2 = new Sensor( 200, - 0 ); + Sensor sensor2 = new Sensor( 200, 0 ); FactHandle sensor2Handle = session.insert( sensor2 ); - Sensor sensor3 = new Sensor( 200, - 0 ); + Sensor sensor3 = new Sensor( 200, 0 ); FactHandle sensor3Handle = session.insert( sensor3 ); session.fireAllRules(); - session = getSerialisedStatefulKnowledgeSession( session, - true ); + session = getSerialisedStatefulKnowledgeSession( session, true ); List temperatureList = new ArrayList( session.getObjects( new ClassObjectFilter( Integer.class ) ) ); assertThat(temperatureList.contains(Integer.valueOf(100))).isTrue(); @@ -726,8 +722,7 @@ public void testLogicalInsertionsModifySameRuleGivesDifferentLogicalInsertion() sensor1Handle = getFactHandle( sensor1Handle, session ); session.update( sensor1Handle, sensor1 ); - session = getSerialisedStatefulKnowledgeSession( session, - true ); + session = getSerialisedStatefulKnowledgeSession( session, true ); session.fireAllRules(); temperatureList = new ArrayList( session.getObjects( new ClassObjectFilter( Integer.class ) ) ); @@ -744,20 +739,18 @@ public InternalFactHandle getFactHandle(FactHandle factHandle, StatefulKnowledgeSessionImpl session) { Map handles = new HashMap<>(); for ( FactHandle fh : session.getFactHandles() ) { - handles.put( ((InternalFactHandle) fh).getId(), - fh ); + handles.put( fh.getId(), fh ); } - return (InternalFactHandle) handles.get( ((InternalFactHandle) factHandle).getId() ); + return (InternalFactHandle) handles.get( factHandle.getId() ); } public InternalFactHandle getFactHandle(FactHandle factHandle, KieSession ksession) { Map handles = new HashMap<>(); for ( FactHandle fh : ksession.getFactHandles() ) { - handles.put( ((InternalFactHandle) fh).getId(), - fh ); + handles.put( fh.getId(), fh ); } - return (InternalFactHandle) handles.get( ((InternalFactHandle) factHandle).getId() ); + return (InternalFactHandle) handles.get( factHandle.getId() ); } } diff --git a/drools-tms/src/main/java/org/drools/tms/TruthMaintenanceSystemKnowledgeHelperFactoryImpl.java b/drools-tms/src/main/java/org/drools/tms/TruthMaintenanceSystemKnowledgeHelperFactoryImpl.java index 0dd11f2a779..9d10617368b 100644 --- a/drools-tms/src/main/java/org/drools/tms/TruthMaintenanceSystemKnowledgeHelperFactoryImpl.java +++ b/drools-tms/src/main/java/org/drools/tms/TruthMaintenanceSystemKnowledgeHelperFactoryImpl.java @@ -16,12 +16,13 @@ import org.drools.core.common.ReteEvaluator; import org.drools.core.rule.consequence.KnowledgeHelper; +import org.drools.kiesession.consequence.DefaultKnowledgeHelper; import org.drools.kiesession.factory.KnowledgeHelperFactory; public class TruthMaintenanceSystemKnowledgeHelperFactoryImpl implements KnowledgeHelperFactory { @Override public KnowledgeHelper createKnowledgeHelper(ReteEvaluator reteEvaluator) { - return new TruthMaintenanceSystemKnowledgeHelper( reteEvaluator ); + return reteEvaluator.isTMSEnabled() ? new TruthMaintenanceSystemKnowledgeHelper( reteEvaluator ) : new DefaultKnowledgeHelper( reteEvaluator ); } } diff --git a/drools-util/src/main/java/org/drools/util/ObjectPool.java b/drools-util/src/main/java/org/drools/util/ObjectPool.java index 157d466176c..ddb81ae96ea 100644 --- a/drools-util/src/main/java/org/drools/util/ObjectPool.java +++ b/drools-util/src/main/java/org/drools/util/ObjectPool.java @@ -51,7 +51,8 @@ public LockFreeObjectPool(Supplier factory, Consumer destroyer) { @Override public T borrow() { - return pool.isEmpty() ? factory.get() : pool.poll(); + T t = pool.poll(); + return t != null ? t : factory.get(); } @Override