public void testPropogateNoMatch() { Console.WriteLine("testPropogateNoMatch"); // first create a rule engine instance Rete engine = new Rete(); NotJoin nj = new NotJoin(engine.nextNodeId()); HashedEqBNode bn2 = new HashedEqBNode(engine.nextNodeId()); Assert.IsNotNull(nj); // create a defclass Defclass dc = new Defclass(typeof(TestBean2)); // create deftemplate Deftemplate dtemp = dc.createDeftemplate("testBean2"); Assert.IsNotNull(dtemp); Binding[] binds = new Binding[1]; Binding b1 = new Binding(); b1.LeftIndex = (0); b1.IsObjectVar = (false); b1.LeftRow = (0); b1.RightIndex = (0); b1.VarName = ("var1"); binds[0] = b1; Binding[] binds2 = new Binding[1]; Binding b2 = new Binding(); b2.LeftIndex = (1); b2.IsObjectVar = (false); b2.LeftRow = (0); b2.RightIndex = (1); b2.VarName = ("var2"); binds2[0] = b2; // set the binding nj.Bindings = (binds); bn2.Bindings = (binds2); // now add the second Not to the first try { nj.addSuccessorNode(bn2, engine, engine.WorkingMemory); } catch (AssertException e) { Console.WriteLine(e.Message); } int count = 10; ArrayList data = new ArrayList(); for (int idx = 0; idx < count; idx++) { TestBean2 bean = new TestBean2(); bean.Attr1 = ("random" + idx); bean.Attr2 = (101 + idx); short s = 10001; bean.Attr3 = (s); long l = 10101018 + idx; bean.Attr4 = (l); bean.Attr5 = (1010101); bean.Attr6 = (1001.1001); IFact fact = dtemp.createFact(bean, dc, engine.nextFactId()); data.Add(fact); } IEnumerator itr = data.GetEnumerator(); while (itr.MoveNext()) { try { IFact f1 = (IFact)itr.Current; nj.assertLeft(new Index(new IFact[] { f1 }), engine, engine.WorkingMemory); nj.assertRight(f1, engine, engine.WorkingMemory); } catch (AssertException e) { Console.WriteLine(e.Message); } } IGenericMap <IFact, IFact> rbmem = (IGenericMap <IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(nj); Assert.AreEqual(count, rbmem.Count); IGenericMap <Object, Object> lbmem = (IGenericMap <Object, Object>)engine.WorkingMemory.getBetaLeftMemory(nj); Assert.AreEqual(count, lbmem.Count); // now check the BetaMemory has matches Console.WriteLine(nj.toPPString()); IEnumerator mitr = lbmem.Values.GetEnumerator(); while (mitr.MoveNext()) { IBetaMemory btm = (IBetaMemory)mitr.Current; Assert.AreEqual(0, btm.matchCount()); Console.WriteLine("match count=" + btm.matchCount() + " - " + btm.toPPString()); } IGenericMap <Object, Object> lbmem2 = (IGenericMap <Object, Object>)engine.WorkingMemory.getBetaLeftMemory(bn2); Assert.AreEqual(count, lbmem2.Count); Console.WriteLine(bn2.toPPString()); IEnumerator mitr2 = lbmem2.Values.GetEnumerator(); engine.close(); // TODO need to update the test to check the match count // by getting the right memory }
protected internal virtual void printBetaNodes(BaseJoin bjoin, bool detailed, int betaTotal) { if (bjoin is HashedEqBNode || bjoin is HashedEqNJoin) { IGenericMap <Object, Object> bm = (IGenericMap <Object, Object>)betaLeftMemories.Get(bjoin); // we iterate over the keys in the HashMap IEnumerator bitr = bm.Keys.GetEnumerator(); while (bitr.MoveNext()) { Index indx = (Index)bm.Get(bitr.Current); if (detailed) { engine.writeMessage(bjoin.toPPString(), Constants.DEFAULT_OUTPUT); HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)getBetaRightMemory(bjoin); EqHashIndex eqinx = new EqHashIndex(NodeUtils.getLeftValues(bjoin.binds, indx.Facts)); // Add to the total count betaTotal += rightmem.count(eqinx); engine.writeMessage(" count=" + betaTotal + " - " + indx.toPPString() + ": ", Constants.DEFAULT_OUTPUT); IEnumerator ritr = rightmem.iterator(eqinx); if (ritr != null) { StringBuilder buf = new StringBuilder(); while (ritr.MoveNext()) { buf.Append(((IFact)ritr.Current).FactId + ","); } engine.writeMessage(buf.ToString(), Constants.DEFAULT_OUTPUT); } engine.writeMessage(Constants.LINEBREAK, Constants.DEFAULT_OUTPUT); } } } else if (bjoin is HashedNotEqNJoin || bjoin is HashedNotEqBNode) { IGenericMap <Object, Object> bm = (IGenericMap <Object, Object>)betaLeftMemories.Get(bjoin); // we iterate over the keys in the HashMap IEnumerator bitr = bm.Keys.GetEnumerator(); while (bitr.MoveNext()) { Index indx = (Index)bm.Get(bitr.Current); if (detailed) { engine.writeMessage(bjoin.toPPString(), Constants.DEFAULT_OUTPUT); HashedNeqAlphaMemory rightmem = (HashedNeqAlphaMemory)getBetaRightMemory(bjoin); EqHashIndex eqinx = new EqHashIndex(NodeUtils.getLeftValues(bjoin.binds, indx.Facts)); // Add to the total count betaTotal += rightmem.count(eqinx); engine.writeMessage(" count=" + betaTotal + " - " + indx.toPPString() + ": ", Constants.DEFAULT_OUTPUT); IEnumerator ritr = rightmem.iterator(eqinx); if (ritr != null) { StringBuilder buf = new StringBuilder(); while (ritr.MoveNext()) { buf.Append(((IFact)ritr.Current).FactId + ","); } engine.writeMessage(buf.ToString(), Constants.DEFAULT_OUTPUT); } engine.writeMessage(Constants.LINEBREAK, Constants.DEFAULT_OUTPUT); } } } else if (bjoin is ExistJoin) { ExistJoin henj = (ExistJoin)bjoin; IGenericMap <Object, Object> bm = (IGenericMap <Object, Object>)betaLeftMemories.Get(henj); // we iterate over the keys in the HashMap IEnumerator bitr = bm.Keys.GetEnumerator(); while (bitr.MoveNext()) { Index indx = (Index)bm.Get(bitr.Current); if (detailed) { engine.writeMessage(bjoin.toPPString(), Constants.DEFAULT_OUTPUT); HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)getBetaRightMemory(henj); EqHashIndex eqinx = new EqHashIndex(NodeUtils.getLeftValues(henj.binds, indx.Facts)); // Add to the total count betaTotal += rightmem.count(eqinx); engine.writeMessage(" count=" + betaTotal + " - " + indx.toPPString() + ": ", Constants.DEFAULT_OUTPUT); IEnumerator ritr = rightmem.iterator(eqinx); if (ritr != null) { StringBuilder buf = new StringBuilder(); while (ritr.MoveNext()) { buf.Append(((IFact)ritr.Current).FactId + ","); } engine.writeMessage(buf.ToString(), Constants.DEFAULT_OUTPUT); } engine.writeMessage(Constants.LINEBREAK, Constants.DEFAULT_OUTPUT); } } } else if (bjoin is NotJoin) { NotJoin nj = (NotJoin)bjoin; IGenericMap <Object, Object> bm = (IGenericMap <Object, Object>)getBetaLeftMemory(bjoin); IEnumerator bitr = bm.Keys.GetEnumerator(); while (bitr.MoveNext()) { Index indx = (Index)bitr.Current; IBetaMemory bmem = (IBetaMemory)bm.Get(indx); engine.writeMessage(bmem.toPPString()); } } else if (bjoin is TemporalEqNode) { TemporalEqNode ten = (TemporalEqNode)bjoin; } else { IGenericMap <Object, Object> bm = (IGenericMap <Object, Object>)betaLeftMemories.Get(bjoin); // we iterate over the keys in the HashMap IEnumerator bitr = bm.Keys.GetEnumerator(); while (bitr.MoveNext()) { Index indx = (Index)bm.Get(bitr.Current); Object rightmem = betaRightMemories.Get(bjoin); if (detailed) { if (rightmem is HashedAlphaMemoryImpl) { HashedAlphaMemoryImpl hami = (HashedAlphaMemoryImpl)rightmem; engine.writeMessage(bjoin.toPPString() + " count=" + hami.size() + " - " + indx.toPPString() + Constants.LINEBREAK); } else { IGenericMap <Object, Object> rmap = (IGenericMap <Object, Object>)rightmem; engine.writeMessage(bjoin.toPPString() + " count=" + rmap.Count + " - " + indx.toPPString() + Constants.LINEBREAK); } } if (rightmem is HashedAlphaMemoryImpl) { betaTotal += ((HashedAlphaMemoryImpl)rightmem).size(); } else { betaTotal += ((IGenericMap <IFact, IFact>)rightmem).Count; } } } }
public void testMatch() { // first create a rule engine instance Rete engine = new Rete(); NotJoin bn = new NotJoin(engine.nextNodeId()); Assert.IsNotNull(bn); // create a defclass Defclass dc = new Defclass(typeof(TestBean2)); // create deftemplate Deftemplate dtemp = dc.createDeftemplate("testBean2"); Assert.IsNotNull(dtemp); Binding[] binds = new Binding[1]; Binding b1 = new Binding(); b1.LeftIndex = (0); b1.IsObjectVar = (false); b1.LeftRow = (0); b1.RightIndex = (0); b1.VarName = ("var1"); binds[0] = b1; // set the binding bn.Bindings = (binds); int count = 10; ArrayList data = new ArrayList(); for (int idx = 0; idx < count; idx++) { TestBean2 bean = new TestBean2(); bean.Attr1 = ("random"); bean.Attr2 = (101); short s = 10001; bean.Attr3 = (s); long l = 10101018; bean.Attr4 = (l); bean.Attr5 = (1010101); bean.Attr6 = (1001.1001); IFact fact = dtemp.createFact(bean, dc, engine.nextFactId()); data.Add(fact); } IEnumerator itr = data.GetEnumerator(); while (itr.MoveNext()) { try { IFact f1 = (IFact)itr.Current; bn.assertLeft(new Index(new IFact[] { f1 }), engine, engine.WorkingMemory); bn.assertRight(f1, engine, engine.WorkingMemory); } catch (AssertException e) { Console.WriteLine(e.Message); } } IGenericMap <IFact, IFact> rbmem = (IGenericMap <IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(bn); Assert.AreEqual(count, rbmem.Count); IGenericMap <Object, Object> lbmem = (IGenericMap <Object, Object>)engine.WorkingMemory.getBetaLeftMemory(bn); Assert.AreEqual(count, lbmem.Count); // now check the BetaMemory has matches Console.WriteLine(bn.toPPString()); IEnumerator mitr = lbmem.Values.GetEnumerator(); while (mitr.MoveNext()) { IBetaMemory btm = (IBetaMemory)mitr.Current; Assert.AreEqual(9, btm.matchCount()); Console.WriteLine("match count=" + btm.matchCount() + " - " + btm.toPPString()); } engine.close(); }