use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class AddRemoveRule method deleteLeftTuple.
private static void deleteLeftTuple(LeftTuple removingLt, LeftTuple removingLt2, LeftTuple prevLt) {
// only the first LT in a peer chain is hooked into left and right parents or the FH.
// If the first LT is being remove, those hooks need to be shifted to the next peer,
// or nulled if there is no next peer.
// When there is a subnetwork, it needs to shift to the peer of the next lt.
// if it is not the first LT in the peer chain, leftParent and rightParent are null.
// And the previous peer will need to point to the peer after removingLt, or removingLt2 if it exists.
// is this the first LT in a peer chain chain
boolean isFirstLt = prevLt == null;
// if there is a subnetwork, skip to the peer after that
LeftTuple nextPeerLt = (removingLt2 == null) ? removingLt.getPeer() : removingLt2.getPeer();
if (!isFirstLt) {
// This LT is not the first tuple in a peer chain. So just correct the peer chain linked list
prevLt.setPeer(nextPeerLt);
} else {
if (nextPeerLt == null) {
removingLt.unlinkFromLeftParent();
removingLt.unlinkFromRightParent();
return;
}
InternalFactHandle fh = removingLt.getFactHandle();
// This is the first LT in a peer chain. Only this LT is hooked into the left and right parent LT and RT and
// if it's the root (form the lian) it will be hooked itno the FH.
LeftTuple leftPrevious = removingLt.getHandlePrevious();
LeftTuple leftNext = removingLt.getHandleNext();
LeftTuple rightPrevious = removingLt.getRightParentPrevious();
LeftTuple rightNext = removingLt.getRightParentNext();
LeftTuple leftParent = removingLt.getLeftParent();
RightTuple rightParent = removingLt.getRightParent();
// This tuple is the first peer and thus is linked into the left parent LT.
nextPeerLt.setFactHandle(removingLt.getFactHandle());
// correct the linked list
if (leftPrevious != null) {
nextPeerLt.setHandlePrevious(leftPrevious);
leftPrevious.setHandleNext(nextPeerLt);
}
if (leftNext != null) {
nextPeerLt.setHandleNext(leftNext);
leftNext.setHandlePrevious(nextPeerLt);
}
// correct the linked list
if (rightPrevious != null) {
nextPeerLt.setRightParentPrevious(rightPrevious);
rightPrevious.setRightParentNext(nextPeerLt);
}
if (rightNext != null) {
nextPeerLt.setRightParentNext(rightNext);
rightNext.setRightParentPrevious(nextPeerLt);
}
// correct the parent's first/last references
if (leftParent != null) {
nextPeerLt.setLeftParent(leftParent);
if (leftParent.getFirstChild() == removingLt) {
leftParent.setFirstChild(nextPeerLt);
}
if (leftParent.getLastChild() == removingLt) {
leftParent.setLastChild(nextPeerLt);
}
} else {
// is the LT for the LIAN, if so we need to process the FH too
fh.removeLeftTuple(removingLt);
}
if (rightParent != null) {
// This tuple is the first peer and thus is linked into the right parent RT.
nextPeerLt.setRightParent(rightParent);
// if nextLT is null, it's ok for parent's reference to be null
if (rightParent.getFirstChild() == removingLt) {
// if next peer exists, set it to this
rightParent.setFirstChild(nextPeerLt);
}
if (rightParent.getLastChild() == removingLt) {
rightParent.setLastChild(nextPeerLt);
}
}
}
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class PhreakAccumulateNode method reaccumulateForLeftTuple.
private static void reaccumulateForLeftTuple(final AccumulateNode accNode, final Accumulate accumulate, final LeftTuple leftTuple, final InternalWorkingMemory wm, final AccumulateMemory am, final AccumulateContext accctx) {
accumulate.init(am.workingMemoryContext, accctx.context, leftTuple, wm);
for (LeftTuple childMatch = leftTuple.getFirstChild(); childMatch != null; childMatch = childMatch.getHandleNext()) {
RightTuple rightTuple = childMatch.getRightParent();
InternalFactHandle childHandle = rightTuple.getFactHandle();
LeftTuple tuple = leftTuple;
if (accNode.isUnwrapRightObject()) {
// if there is a subnetwork, handle must be unwrapped
tuple = (LeftTuple) rightTuple;
childHandle = rightTuple.getFactHandleForEvaluation();
}
accumulate.accumulate(am.workingMemoryContext, accctx.context, tuple, childHandle, wm);
}
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class PhreakAccumulateNode method removeMatch.
/**
* Removes a match between left and right tuple
*/
private static void removeMatch(final AccumulateNode accNode, final Accumulate accumulate, final RightTuple rightTuple, final LeftTuple match, final InternalWorkingMemory wm, final AccumulateMemory am, final AccumulateContext accctx, final boolean reaccumulate) {
// save the matching tuple
LeftTuple leftTuple = match.getLeftParent();
// removing link between left and right
match.unlinkFromLeftParent();
match.unlinkFromRightParent();
// if there is a subnetwork, we need to unwrap the object from inside the tuple
InternalFactHandle handle = rightTuple.getFactHandle();
LeftTuple tuple = leftTuple;
if (accNode.isUnwrapRightObject()) {
tuple = (LeftTuple) rightTuple;
handle = rightTuple.getFactHandleForEvaluation();
}
if (accumulate.supportsReverse()) {
// just reverse this single match
accumulate.reverse(am.workingMemoryContext, accctx.context, tuple, handle, wm);
} else {
// otherwise need to recalculate all matches for the given leftTuple
if (reaccumulate) {
reaccumulateForLeftTuple(accNode, accumulate, leftTuple, wm, am, accctx);
}
}
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class InputMarshaller method readFactHandle.
public static InternalFactHandle readFactHandle(MarshallerReaderContext context) throws IOException, ClassNotFoundException {
int type = context.stream.readInt();
int id = context.stream.readInt();
long recency = context.stream.readLong();
long startTimeStamp = 0;
long duration = 0;
boolean expired = false;
long activationsCount = 0;
if (type == 2) {
startTimeStamp = context.stream.readLong();
duration = context.stream.readLong();
expired = context.stream.readBoolean();
activationsCount = context.stream.readLong();
}
int strategyIndex = context.stream.readInt();
Object object = null;
ObjectMarshallingStrategy strategy = null;
// This is the old way of de/serializing strategy objects
if (strategyIndex >= 0) {
strategy = context.resolverStrategyFactory.getStrategy(strategyIndex);
} else // This is the new way
if (strategyIndex == -2) {
String strategyClassName = context.stream.readUTF();
if (!StringUtils.isEmpty(strategyClassName)) {
strategy = context.resolverStrategyFactory.getStrategyObject(strategyClassName);
if (strategy == null) {
throw new IllegalStateException("No strategy of type " + strategyClassName + " available.");
}
}
}
// If either way retrieves a strategy, use it
if (strategy != null) {
object = strategy.read(context.stream);
}
EntryPoint entryPoint = null;
if (context.readBoolean()) {
String entryPointId = context.readUTF();
if (entryPointId != null && !entryPointId.equals("")) {
entryPoint = ((RuleRuntime) context.wm).getEntryPoint(entryPointId);
}
}
EntryPointId confEP;
if (entryPoint != null) {
confEP = ((NamedEntryPoint) entryPoint).getEntryPoint();
} else {
confEP = context.wm.getEntryPoint();
}
ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(confEP, object);
InternalFactHandle handle = null;
switch(type) {
case 0:
{
handle = new DefaultFactHandle(id, object, recency, (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
break;
}
case 1:
{
handle = new QueryElementFactHandle(object, id, recency);
break;
}
case 2:
{
handle = new EventFactHandle(id, object, recency, startTimeStamp, duration, (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
((EventFactHandle) handle).setExpired(expired);
((EventFactHandle) handle).setActivationsCount(activationsCount);
break;
}
default:
{
throw new IllegalStateException("Unable to marshal FactHandle, as type does not exist:" + type);
}
}
return handle;
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class ProtobufInputMarshaller method readFactHandle.
public static InternalFactHandle readFactHandle(MarshallerReaderContext context, EntryPoint entryPoint, FactHandle _handle) throws IOException, ClassNotFoundException {
Object object = null;
ObjectMarshallingStrategy strategy = null;
if (_handle.hasStrategyIndex()) {
strategy = context.usedStrategies.get(_handle.getStrategyIndex());
object = strategy.unmarshal(context.strategyContexts.get(strategy), context, _handle.getObject().toByteArray(), (context.kBase == null) ? null : context.kBase.getRootClassLoader());
}
EntryPointId confEP;
if (entryPoint != null) {
confEP = ((NamedEntryPoint) entryPoint).getEntryPoint();
} else {
confEP = context.wm.getEntryPoint();
}
ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(confEP, object);
InternalFactHandle handle = null;
switch(_handle.getType()) {
case FACT:
{
handle = new DefaultFactHandle(_handle.getId(), object, _handle.getRecency(), (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
break;
}
case QUERY:
{
handle = new QueryElementFactHandle(object, _handle.getId(), _handle.getRecency());
break;
}
case EVENT:
{
handle = new EventFactHandle(_handle.getId(), object, _handle.getRecency(), _handle.getTimestamp(), _handle.getDuration(), (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
((EventFactHandle) handle).setExpired(_handle.getIsExpired());
((EventFactHandle) handle).setOtnCount(_handle.getOtnCount());
// ((EventFactHandle) handle).setActivationsCount( _handle.getActivationsCount() );
break;
}
default:
{
throw new IllegalStateException("Unable to marshal FactHandle, as type does not exist:" + _handle.getType());
}
}
return handle;
}
Aggregations