Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DROOLS-7501] avoid using TMS components when not strictly necessary #5393

Merged
merged 3 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ InternalMatch createAgendaItem(RuleTerminalNodeLeftTuple rtnLeftTuple,
void evaluateQueriesForRule(RuleAgendaItem item);

KnowledgeHelper getKnowledgeHelper();
void resetKnowledgeHelper();

void executeTask(ExecutableEntry executableEntry);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 }
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ int evaluateAndFire( AgendaFilter filter,
InternalAgendaGroup group );

KnowledgeHelper getKnowledgeHelper();

void resetKnowledgeHelper();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -262,6 +262,11 @@ public KnowledgeHelper getKnowledgeHelper() {
return ruleEvaluator.getKnowledgeHelper();
}

@Override
public void resetKnowledgeHelper() {
ruleEvaluator.resetKnowledgeHelper();
}

@Override
public void executeTask(ExecutableEntry executableEntry) {
executableEntry.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static AgendaComponentFactory get() {
return AgendaComponentFactory.Holder.INSTANCE;
}

public class AgendaComponentFactoryImpl implements AgendaComponentFactory {
class AgendaComponentFactoryImpl implements AgendaComponentFactory {

public AgendaComponentFactoryImpl() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,10 @@ public KnowledgeHelper getKnowledgeHelper() {
return ruleEvaluator.getKnowledgeHelper();
}

public void resetKnowledgeHelper() {
ruleEvaluator.resetKnowledgeHelper();
}

@Override
public void addPropagation(PropagationEntry propagationEntry) {
propagationList.addEntry( propagationEntry );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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());
}

Expand All @@ -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) {
Expand Down
Loading