public static void unlinkNotNodeOnRightInsert( NotNode notNode, BetaMemory bm, InternalWorkingMemory wm) { if (bm.getSegmentMemory().isSegmentLinked() && !notNode.isRightInputIsRiaNode() && notNode.isEmptyBetaConstraints()) { // this must be processed here, rather than initial insert, as we need to link the blocker // @TODO this could be more efficient, as it means the entire StagedLeftTuples for all // previous nodes where evaluated, needlessly. bm.unlinkNode(wm); } }
@Test public void testPopulatedSharedLiaNode() throws Exception { KnowledgeBase kbase1 = buildKnowledgeBase("r1", " A() B(1;) C() D() E()\n"); InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newStatefulKnowledgeSession()); 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 B(2)); wm.insert(new C(1)); wm.insert(new D(1)); wm.insert(new E(1)); wm.fireAllRules(); assertEquals(3, list.size()); kbase1.addKnowledgePackages(buildKnowledgePackage("r2", " a : A() B(2;) C() D() E()\n")); ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class); LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getSinkPropagator().getSinks()[0]; JoinNode bNode1 = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink(); JoinNode bNode2 = (JoinNode) liaNode.getSinkPropagator().getLastLeftTupleSink(); BetaMemory bm = (BetaMemory) wm.getNodeMemory(bNode2); SegmentMemory sm = bm.getSegmentMemory(); assertNotNull(sm.getStagedLeftTuples().getInsertFirst()); assertNotNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext()); assertNotNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext()); assertNull( sm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext()); wm.fireAllRules(); assertNull(sm.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("r2", ((Match) list.get(4)).getRule().getName()); assertEquals("r2", ((Match) list.get(5)).getRule().getName()); List results = new ArrayList(); results.add(((A) ((Match) list.get(3)).getDeclarationValue("a")).getObject()); results.add(((A) ((Match) list.get(4)).getDeclarationValue("a")).getObject()); results.add(((A) ((Match) list.get(5)).getDeclarationValue("a")).getObject()); assertTrue(results.containsAll(asList(1, 2, 3))); }
public Boolean call() throws Exception { EntryPoint ep = ksession.getEntryPoint("EP" + index); InternalWorkingMemory wm = ((NamedEntryPoint) ep).getInternalWorkingMemory(); ObjectTypeNode otn = ((NamedEntryPoint) ep) .getEntryPointNode() .getObjectTypeNodes() .values() .iterator() .next(); AlphaNode alpha = (AlphaNode) otn.getSinkPropagator().getSinks()[0]; BetaNode beta = (BetaNode) alpha.getSinkPropagator().getSinks()[0]; BetaMemory memory = (BetaMemory) wm.getNodeMemory(beta); memory.getSegmentMemory(); for (int i = 0; i < 100; i++) { Thread.sleep(RANDOM.nextInt(100)); if (fh == null) { fh = ep.insert("" + index); } else { if (RANDOM.nextInt(100) < 70) { ep.delete(fh); fh = null; } else { ep.update(fh, "" + index); } } } if (index == deleteIndex) { if (fh != null) { ep.delete(fh); fh = null; } } else if (fh == null) { fh = ep.insert("" + index); } return true; }
@Test public void testPopulatedRuleWithEvals() throws Exception { KnowledgeBase kbase1 = buildKnowledgeBase("r1", " a:A() B() eval(1==1) eval(1==1) C(1;) \n"); InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newStatefulKnowledgeSession()); 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.fireAllRules(); assertEquals(3, list.size()); kbase1.addKnowledgePackages( buildKnowledgePackage("r2", " a:A() B() eval(1==1) eval(1==1) C(2;) \n")); ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class); LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getSinkPropagator().getSinks()[0]; JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink(); EvalConditionNode e1 = (EvalConditionNode) bNode.getSinkPropagator().getFirstLeftTupleSink(); EvalConditionNode e2 = (EvalConditionNode) e1.getSinkPropagator().getFirstLeftTupleSink(); JoinNode c1Node = (JoinNode) e2.getSinkPropagator().getFirstLeftTupleSink(); JoinNode c2Node = (JoinNode) e2.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()); }