use of org.drools.core.spi.PropagationContext in project drools by kiegroup.
the class TruthMaintenanceSystem method delete.
public void delete(FactHandle fh) {
if (fh == null) {
return;
}
InternalFactHandle ifh = (InternalFactHandle) fh;
// This will clear out the logical entries for the FH. However the FH and EqualityKey remain, if it's stated
// Update the equality key, which maintains a list of stated FactHandles
final EqualityKey key = ifh.getEqualityKey();
if (key.getLogicalFactHandle() != fh) {
throw new IllegalArgumentException("The FactHandle did not originate from TMS : " + fh);
}
InternalWorkingMemory wm = ep.getInternalWorkingMemory();
final PropagationContext propagationContext = ep.getPctxFactory().createPropagationContext(wm.getNextPropagationIdCounter(), PropagationContext.Type.DELETION, null, null, ifh, ep.getEntryPoint());
TruthMaintenanceSystemHelper.removeLogicalDependencies(ifh, propagationContext);
}
use of org.drools.core.spi.PropagationContext in project drools by kiegroup.
the class AddRemoveRule method insertFacts.
private static void insertFacts(PathEndNodes endNodes, InternalWorkingMemory[] wms) {
Set<LeftTupleNode> visited = new HashSet<LeftTupleNode>();
for (PathEndNode endNode : endNodes.subjectEndNodes) {
LeftTupleNode[] nodes = endNode.getPathNodes();
for (int i = 0; i < nodes.length; i++) {
LeftTupleNode node = nodes[i];
if (NodeTypeEnums.isBetaNode(node) && node.getAssociationsSize() == 1) {
if (!visited.add(node)) {
// this is to avoid rentering a path, and processing nodes twice. This can happen for nested subnetworks.
continue;
}
BetaNode bn = (BetaNode) node;
if (!bn.isRightInputIsRiaNode()) {
for (int j = 0; j < wms.length; j++) {
PropagationContextFactory pctxFactory = wms[j].getKnowledgeBase().getConfiguration().getComponentFactory().getPropagationContextFactory();
final PropagationContext pctx = pctxFactory.createPropagationContext(wms[j].getNextPropagationIdCounter(), PropagationContext.Type.RULE_ADDITION, null, null, null);
bn.getRightInput().updateSink(bn, pctx, wms[j]);
}
}
}
}
}
}
use of org.drools.core.spi.PropagationContext in project drools by kiegroup.
the class PhreakFromNode method doLeftInserts.
public void doLeftInserts(FromNode fromNode, FromMemory fm, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
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.getInsertFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
PropagationContext propagationContext = leftTuple.getPropagationContext();
Map<Object, RightTuple> matches = null;
boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(fromNode, leftTuple);
if (useLeftMemory) {
fm.getBetaMemory().getLeftTupleMemory().add(leftTuple);
matches = new LinkedHashMap<Object, RightTuple>();
leftTuple.setContextObject(matches);
}
betaConstraints.updateFromTuple(context, wm, leftTuple);
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 = fromNode.createRightTuple(leftTuple, propagationContext, wm, object);
checkConstraintsAndPropagate(sink, leftTuple, rightTuple, alphaConstraints, betaConstraints, propagationContext, wm, fm, context, useLeftMemory, trgLeftTuples, null);
if (useLeftMemory) {
fromNode.addToCreatedHandlesMap(matches, rightTuple);
}
}
leftTuple.clearStaged();
leftTuple = next;
}
betaConstraints.resetTuple(context);
}
use of org.drools.core.spi.PropagationContext in project drools by kiegroup.
the class AlphaNodeTest method testLiteralConstraintAssertObjectWithoutMemory.
@Test
public void testLiteralConstraintAssertObjectWithoutMemory() throws Exception {
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase);
buildContext.setRule(new RuleImpl("test"));
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final RuleImpl rule = new RuleImpl("test-rule");
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
final MockObjectSource source = new MockObjectSource(buildContext.getNextId());
final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
// With Memory
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, // no memory
buildContext);
final MockObjectSink sink = new MockObjectSink();
alphaNode.addObjectSink(sink);
final Cheese cheddar = new Cheese("cheddar", 5);
final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
// check sink is empty
assertLength(0, sink.getAsserted());
// object should assert as it passes text
alphaNode.assertObject(f0, context, ksession);
assertEquals(1, sink.getAsserted().size());
Object[] list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
final Cheese stilton = new Cheese("stilton", 6);
final DefaultFactHandle f1 = new DefaultFactHandle(1, stilton);
// object should NOT assert as it does not pass test
alphaNode.assertObject(f1, context, ksession);
assertLength(1, sink.getAsserted());
list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
}
use of org.drools.core.spi.PropagationContext in project drools by kiegroup.
the class SlidingTimeWindow method expireFacts.
public void expireFacts(final Object context, final PropagationContext pctx, final InternalWorkingMemory workingMemory) {
TimerService clock = workingMemory.getTimerService();
long currentTime = clock.getCurrentTime();
SlidingTimeWindowContext queue = (SlidingTimeWindowContext) context;
EventFactHandle handle = queue.peek();
while (handle != null && isExpired(currentTime, handle)) {
queue.setExpiringHandle(handle);
queue.remove();
if (handle.isValid()) {
// if not expired yet, expire it
final PropagationContext expiresPctx = createPropagationContextForFact(workingMemory, handle, PropagationContext.Type.EXPIRATION);
ObjectTypeNode.doRetractObject(handle, expiresPctx, workingMemory);
}
queue.setExpiringHandle(null);
handle = queue.peek();
}
// update next expiration time
updateNextExpiration(handle, workingMemory, queue, nodeId);
}
Aggregations