use of org.drools.core.spi.PropagationContext in project drools by kiegroup.
the class AddRemoveRule method insertLiaFacts.
private static void insertLiaFacts(LeftTupleNode startNode, InternalWorkingMemory wm) {
// rule added with no sharing
PropagationContextFactory pctxFactory = wm.getKnowledgeBase().getConfiguration().getComponentFactory().getPropagationContextFactory();
final PropagationContext pctx = pctxFactory.createPropagationContext(wm.getNextPropagationIdCounter(), PropagationContext.Type.RULE_ADDITION, null, null, null);
LeftInputAdapterNode lian = (LeftInputAdapterNode) startNode;
RightTupleSinkAdapter liaAdapter = new RightTupleSinkAdapter(lian);
lian.getObjectSource().updateSink(liaAdapter, pctx, wm);
}
use of org.drools.core.spi.PropagationContext in project drools by kiegroup.
the class PhreakFromNode method doLeftUpdates.
public void doLeftUpdates(FromNode fromNode, FromMemory fm, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
BetaMemory bm = fm.getBetaMemory();
ContextEntry[] context = bm.getContext();
BetaConstraints betaConstraints = fromNode.getBetaConstraints();
AlphaNodeFieldConstraint[] alphaConstraints = fromNode.getAlphaConstraints();
DataProvider dataProvider = fromNode.getDataProvider();
Class<?> resultClass = fromNode.getResultClass();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
PropagationContext propagationContext = leftTuple.getPropagationContext();
final Map<Object, RightTuple> previousMatches = (Map<Object, RightTuple>) leftTuple.getContextObject();
final Map<Object, RightTuple> newMatches = new HashMap<Object, RightTuple>();
leftTuple.setContextObject(newMatches);
betaConstraints.updateFromTuple(context, wm, leftTuple);
FastIterator rightIt = LinkedList.fastIterator;
for (final java.util.Iterator<?> it = dataProvider.getResults(leftTuple, wm, propagationContext, fm.providerContext); it.hasNext(); ) {
final Object object = it.next();
if ((object == null) || !resultClass.isAssignableFrom(object.getClass())) {
// skip anything if it not assignable
continue;
}
RightTuple rightTuple = previousMatches.remove(object);
if (rightTuple == null) {
// new match, propagate assert
rightTuple = fromNode.createRightTuple(leftTuple, propagationContext, wm, object);
} else {
// previous match, so reevaluate and propagate modify
if (rightIt.next(rightTuple) != null) {
// handle the odd case where more than one object has the same hashcode/equals value
previousMatches.put(object, (RightTuple) rightIt.next(rightTuple));
rightTuple.setNext(null);
}
}
checkConstraintsAndPropagate(sink, leftTuple, rightTuple, alphaConstraints, betaConstraints, propagationContext, wm, fm, context, true, trgLeftTuples, stagedLeftTuples);
fromNode.addToCreatedHandlesMap(newMatches, rightTuple);
}
for (RightTuple rightTuple : previousMatches.values()) {
for (RightTuple current = rightTuple; current != null; current = (RightTuple) rightIt.next(current)) {
deleteChildLeftTuple(propagationContext, trgLeftTuples, stagedLeftTuples, current.getFirstChild());
}
}
leftTuple.clearStaged();
leftTuple = next;
}
betaConstraints.resetTuple(context);
}
use of org.drools.core.spi.PropagationContext in project drools by kiegroup.
the class ProtobufInputMarshaller method readSession.
public static StatefulKnowledgeSessionImpl readSession(ProtobufMessages.KnowledgeSession _session, StatefulKnowledgeSessionImpl session, InternalAgenda agenda, MarshallerReaderContext context) throws IOException, ClassNotFoundException {
GlobalResolver globalResolver = (GlobalResolver) context.env.get(EnvironmentName.GLOBALS);
if (globalResolver != null) {
session.setGlobalResolver(globalResolver);
}
if (session.getTimerService() instanceof PseudoClockScheduler) {
PseudoClockScheduler clock = (PseudoClockScheduler) session.getTimerService();
clock.advanceTime(_session.getTime(), TimeUnit.MILLISECONDS);
}
// RuleFlowGroups need to reference the session
// for ( InternalAgendaGroup group : agenda.getAgendaGroupsMap().values() ) {
// ((RuleFlowGroupImpl) group).setWorkingMemory( session );
// }
context.wm = session;
// need to read node memories before reading the fact handles
// because this data is required during fact propagation
readNodeMemories(context, _session.getRuleData());
List<PropagationContext> pctxs = new ArrayList<PropagationContext>();
if (_session.getRuleData().hasInitialFact()) {
session.setInitialFactHandle(session.initInitialFact(context.kBase, context));
context.handles.put(session.getInitialFactHandle().getId(), session.getInitialFactHandle());
}
for (ProtobufMessages.EntryPoint _ep : _session.getRuleData().getEntryPointList()) {
EntryPoint wmep = ((StatefulKnowledgeSessionImpl) context.wm).getEntryPointMap().get(_ep.getEntryPointId());
readFactHandles(context, _ep, ((WorkingMemoryEntryPoint) wmep).getObjectStore(), pctxs);
context.filter.fireRNEAs(context.wm);
readTruthMaintenanceSystem(context, wmep, _ep, pctxs);
}
cleanReaderContexts(pctxs);
readActionQueue(context, _session.getRuleData());
if (processMarshaller != null) {
if (_session.hasProcessData()) {
context.parameterObject = _session.getProcessData();
processMarshaller.readProcessInstances(context);
context.parameterObject = _session.getProcessData();
processMarshaller.readWorkItems(context);
// This actually does ALL timers, due to backwards compatability issues
// It will read in old JBPM binaries, but always write to the new binary format.
context.parameterObject = _session.getProcessData();
processMarshaller.readProcessTimers(context);
}
} else {
if (_session.hasProcessData()) {
throw new IllegalStateException("No process marshaller, unable to unmarshall process data.");
}
}
if (_session.hasTimers()) {
for (ProtobufMessages.Timers.Timer _timer : _session.getTimers().getTimerList()) {
readTimer(context, _timer);
}
}
// need to process any eventual left over timer node timers
if (!context.timerNodeSchedulers.isEmpty()) {
for (Map<TupleKey, Scheduler> schedulers : context.timerNodeSchedulers.values()) {
for (Scheduler scheduler : schedulers.values()) {
scheduler.schedule(scheduler.getTrigger());
}
}
context.timerNodeSchedulers.clear();
}
// remove the activations filter
agenda.setActivationsFilter(null);
return session;
}
use of org.drools.core.spi.PropagationContext in project drools by kiegroup.
the class ProtobufInputMarshaller method assertHandleIntoOTN.
private static void assertHandleIntoOTN(MarshallerReaderContext context, InternalWorkingMemory wm, InternalFactHandle handle, List<PropagationContext> pctxs) {
Object object = handle.getObject();
WorkingMemoryEntryPoint ep = handle.getEntryPoint();
ObjectTypeConf typeConf = ep.getObjectTypeConfigurationRegistry().getObjectTypeConf(ep.getEntryPoint(), object);
PropagationContextFactory pctxFactory = wm.getKnowledgeBase().getConfiguration().getComponentFactory().getPropagationContextFactory();
PropagationContext propagationContext = pctxFactory.createPropagationContext(wm.getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, null, null, handle, ep.getEntryPoint(), context);
// keeping this list for a later cleanup is necessary because of the lazy propagations that might occur
pctxs.add(propagationContext);
ep.getEntryPointNode().assertObject(handle, propagationContext, typeConf, wm);
wm.flushPropagations();
}
use of org.drools.core.spi.PropagationContext in project drools by kiegroup.
the class NamedEntryPoint method insert.
public FactHandle insert(final Object object, final boolean dynamic, final RuleImpl rule, final TerminalNode terminalNode) {
if (object == null) {
// you cannot assert a null object
return null;
}
try {
this.wm.startOperation();
ObjectTypeConf typeConf = getObjectTypeConfigurationRegistry().getObjectTypeConf(this.entryPoint, object);
final PropagationContext propagationContext = this.pctxFactory.createPropagationContext(this.wm.getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, rule, terminalNode, null, entryPoint);
if (this.wm.isSequential()) {
InternalFactHandle handle = createHandle(object, typeConf);
propagationContext.setFactHandle(handle);
insert(handle, object, rule, typeConf, propagationContext);
return handle;
}
InternalFactHandle handle;
try {
this.lock.lock();
// check if the object already exists in the WM
handle = this.objectStore.getHandleForObject(object);
if (!typeConf.isTMSEnabled()) {
// TMS not enabled for this object type
if (handle != null) {
return handle;
}
handle = createHandle(object, typeConf);
} else {
TruthMaintenanceSystem tms = getTruthMaintenanceSystem();
EqualityKey key;
if (handle != null && handle.getEqualityKey().getStatus() == EqualityKey.STATED) {
// it's already stated, so just return the handle
return handle;
} else {
key = tms.get(object);
}
if (key != null && key.getStatus() == EqualityKey.JUSTIFIED) {
// The justified set needs to be staged, before we can continue with the stated insert
BeliefSet bs = handle.getEqualityKey().getBeliefSet();
// staging will set it's status to stated
bs.getBeliefSystem().stage(propagationContext, bs);
}
handle = createHandle(object, // we know the handle is null
typeConf);
if (key == null) {
key = new EqualityKey(handle, EqualityKey.STATED);
tms.put(key);
} else {
key.addFactHandle(handle);
}
handle.setEqualityKey(key);
}
propagationContext.setFactHandle(handle);
// @propertyChangeSupport
if (dynamic || typeConf.isDynamic()) {
addPropertyChangeListener(handle, dynamic);
}
insert(handle, object, rule, typeConf, propagationContext);
} finally {
this.lock.unlock();
}
return handle;
} finally {
this.wm.endOperation();
}
}
Aggregations