use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.
the class PhreakAccumulateNode method doNode.
public void doNode(AccumulateNode accNode, LeftTupleSink sink, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
BetaMemory bm = am.getBetaMemory();
TupleSets<RightTuple> srcRightTuples = bm.getStagedRightTuples().takeAll();
// order of left and right operations is to minimise wasted of innefficient joins.
// We need to collect which leftTuple where updated, so that we can
// add their result tuple to the real target tuples later
TupleSets<LeftTuple> tempLeftTuples = new TupleSetsImpl<LeftTuple>();
if (srcLeftTuples.getDeleteFirst() != null) {
// use the real target here, as dealing direct with left tuples
doLeftDeletes(accNode, am, wm, srcLeftTuples, trgLeftTuples, stagedLeftTuples);
}
if (srcRightTuples.getDeleteFirst() != null) {
doRightDeletes(accNode, am, wm, srcRightTuples, tempLeftTuples);
}
if (srcRightTuples.getUpdateFirst() != null) {
RuleNetworkEvaluator.doUpdatesReorderRightMemory(bm, srcRightTuples);
doRightUpdates(accNode, am, wm, srcRightTuples, tempLeftTuples);
}
if (srcLeftTuples.getUpdateFirst() != null) {
RuleNetworkEvaluator.doUpdatesReorderLeftMemory(bm, srcLeftTuples);
doLeftUpdates(accNode, am, wm, srcLeftTuples, tempLeftTuples);
}
if (srcRightTuples.getInsertFirst() != null) {
doRightInserts(accNode, am, wm, srcRightTuples, tempLeftTuples);
}
if (srcLeftTuples.getInsertFirst() != null) {
doLeftInserts(accNode, am, wm, srcLeftTuples, tempLeftTuples);
}
Accumulate accumulate = accNode.getAccumulate();
// LeftTuple retracts are already on the trgLeftTuples
for (LeftTuple leftTuple = tempLeftTuples.getInsertFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
evaluateResultConstraints(accNode, sink, accumulate, leftTuple, leftTuple.getPropagationContext(), wm, am, (AccumulateContext) leftTuple.getContextObject(), trgLeftTuples, stagedLeftTuples);
leftTuple.clearStaged();
leftTuple = next;
}
for (LeftTuple leftTuple = tempLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
evaluateResultConstraints(accNode, sink, accumulate, leftTuple, leftTuple.getPropagationContext(), wm, am, (AccumulateContext) leftTuple.getContextObject(), trgLeftTuples, stagedLeftTuples);
leftTuple.clearStaged();
leftTuple = next;
}
srcRightTuples.resetAll();
srcLeftTuples.resetAll();
}
use of org.drools.core.reteoo.BetaMemory 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.reteoo.BetaMemory in project drools by kiegroup.
the class BaseBetaConstraintsTest method checkBetaConstraints.
protected void checkBetaConstraints(BetaNodeFieldConstraint[] constraints, Class cls, short betaNodeType) {
RuleBaseConfiguration config = new RuleBaseConfiguration();
int depth = config.getCompositeKeyDepth();
BetaConstraints betaConstraints;
try {
betaConstraints = (BetaConstraints) cls.getConstructor(new Class[] { BetaNodeFieldConstraint[].class, RuleBaseConfiguration.class }).newInstance(constraints, config);
} catch (Exception e) {
throw new RuntimeException("could not invoke constructor for " + cls.getName());
}
betaConstraints.initIndexes(depth, betaNodeType);
// BetaConstraints betaConstraints = new DefaultBetaConstraints(constraints, config );
constraints = betaConstraints.getConstraints();
List<Integer> list = new ArrayList<Integer>();
// get indexed positions
for (int i = 0; i < constraints.length && list.size() < depth; i++) {
if (((IndexableConstraint) constraints[i]).isIndexable(betaNodeType)) {
list.add(i);
}
}
// convert to array
int[] indexedPositions = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
indexedPositions[i] = i;
}
assertEquals((indexedPositions.length > 0), betaConstraints.isIndexed());
assertEquals(indexedPositions.length, betaConstraints.getIndexCount());
BetaMemory betaMemory = betaConstraints.createBetaMemory(config, NodeTypeEnums.JoinNode);
if (indexedPositions.length > 0) {
if (((IndexableConstraint) constraints[indexedPositions[0]]).getConstraintType() == ConstraintType.EQUAL) {
TupleIndexHashTable tupleHashTable = (TupleIndexHashTable) betaMemory.getLeftTupleMemory();
assertTrue(tupleHashTable.isIndexed());
Index index = tupleHashTable.getIndex();
for (int i = 0; i < indexedPositions.length; i++) {
checkSameConstraintForIndex((IndexableConstraint) constraints[indexedPositions[i]], index.getFieldIndex(i));
}
TupleIndexHashTable factHashTable = (TupleIndexHashTable) betaMemory.getRightTupleMemory();
assertTrue(factHashTable.isIndexed());
index = factHashTable.getIndex();
for (int i = 0; i < indexedPositions.length; i++) {
checkSameConstraintForIndex((IndexableConstraint) constraints[indexedPositions[i]], index.getFieldIndex(i));
}
} else {
}
} else {
TupleList tupleHashTable = (TupleList) betaMemory.getLeftTupleMemory();
assertFalse(tupleHashTable.isIndexed());
TupleList factHashTable = (TupleList) betaMemory.getRightTupleMemory();
assertFalse(factHashTable.isIndexed());
}
}
use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.
the class SegmentUtilities method processBetaNode.
private static long processBetaNode(BetaNode betaNode, InternalWorkingMemory wm, SegmentMemory smem, long nodePosMask, long allLinkedTestMask, boolean updateNodeBit) {
BetaMemory bm = NodeTypeEnums.AccumulateNode == betaNode.getType() ? ((AccumulateMemory) smem.createNodeMemory(betaNode, wm)).getBetaMemory() : (BetaMemory) smem.createNodeMemory(betaNode, wm);
// this must be set first, to avoid recursion as sub networks can be initialised multiple ways
// and bm.getSegmentMemory == null check can be used to avoid recursion.
bm.setSegmentMemory(smem);
if (betaNode.isRightInputIsRiaNode()) {
RightInputAdapterNode riaNode = createRiaSegmentMemory(betaNode, wm);
RiaNodeMemory riaMem = wm.getNodeMemory(riaNode);
bm.setRiaRuleMemory(riaMem.getRiaPathMemory());
if (updateNodeBit && canBeDisabled(betaNode) && riaMem.getRiaPathMemory().getAllLinkedMaskTest() > 0) {
// only ria's with reactive subnetworks can be disabled and thus need checking
allLinkedTestMask = allLinkedTestMask | nodePosMask;
}
} else if (updateNodeBit && canBeDisabled(betaNode)) {
allLinkedTestMask = allLinkedTestMask | nodePosMask;
}
bm.setNodePosMaskBit(nodePosMask);
if (NodeTypeEnums.NotNode == betaNode.getType()) {
// not nodes start up linked in
smem.linkNodeWithoutRuleNotify(bm.getNodePosMaskBit());
}
return allLinkedTestMask;
}
use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.
the class AddRuleTest method testPopulatedRuleMidwayShare.
@Test
public void testPopulatedRuleMidwayShare() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " a : A() B() C(1;) D() E()\n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
wm.insert(new A(1));
wm.insert(new A(2));
wm.insert(new A(3));
wm.insert(new B(1));
wm.insert(new C(1));
wm.insert(new C(2));
wm.insert(new D(1));
wm.insert(new E(1));
wm.fireAllRules();
assertEquals(3, list.size());
kbase1.addPackages(buildKnowledgePackage("r2", " a : A() B() C(2;) D() E()\n"));
ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
JoinNode c1Node = (JoinNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
JoinNode c2Node = (JoinNode) bNode.getSinkPropagator().getLastLeftTupleSink();
LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
SegmentMemory sm = lm.getSegmentMemory();
BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
assertSame(sm.getFirst(), c1Mem.getSegmentMemory());
assertEquals(3, c1Mem.getLeftTupleMemory().size());
assertEquals(1, c1Mem.getRightTupleMemory().size());
BetaMemory c2Mem = (BetaMemory) wm.getNodeMemory(c2Node);
SegmentMemory c2Smem = sm.getFirst().getNext();
assertSame(c2Smem, c2Mem.getSegmentMemory());
assertEquals(0, c2Mem.getLeftTupleMemory().size());
assertEquals(0, c2Mem.getRightTupleMemory().size());
assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst());
assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext());
assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
assertNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
wm.fireAllRules();
assertEquals(3, c2Mem.getLeftTupleMemory().size());
assertEquals(1, c2Mem.getRightTupleMemory().size());
assertNull(c2Smem.getStagedLeftTuples().getInsertFirst());
assertEquals(6, list.size());
assertEquals("r1", ((Match) list.get(0)).getRule().getName());
assertEquals("r1", ((Match) list.get(1)).getRule().getName());
assertEquals("r1", ((Match) list.get(2)).getRule().getName());
assertEquals("r2", ((Match) list.get(3)).getRule().getName());
assertEquals(3, ((A) ((Match) list.get(3)).getDeclarationValue("a")).getObject());
assertEquals("r2", ((Match) list.get(4)).getRule().getName());
assertEquals(2, ((A) ((Match) list.get(4)).getDeclarationValue("a")).getObject());
assertEquals("r2", ((Match) list.get(5)).getRule().getName());
assertEquals(1, ((A) ((Match) list.get(5)).getDeclarationValue("a")).getObject());
}
Aggregations