/// <summary> /// if there are zero matches for the NotEqHashIndex2, the method /// return true. If there are matches, the method returns false. /// False means there's 1 or more matches /// </summary> /// <param name="index">The index.</param> /// <returns></returns> public virtual bool zeroMatch(NotEqHashIndex index) { IGenericMap <Object, Object> matches = (IGenericMap <Object, Object>)memory.Get(index); int idz = 0; if (matches != null) { IEnumerator itr = matches.Keys.GetEnumerator(); while (itr.MoveNext()) { Object key = itr.Current; // if the key doesn't match the subindex, Add it to the // counter. if (!index.SubIndex.Equals(key)) { IGenericMap <Object, Object> submatch = (IGenericMap <Object, Object>)matches.Get(key); idz += submatch.Count; } if (idz > 0) { break; } } return(false); } else { return(true); } }
/// <summary> return an List with all the facts /// </summary> /// <returns> /// /// </returns> public override Object[] iterateAll() { Object[] facts = new Object[counter]; IEnumerator itr = memory.Keys.GetEnumerator(); int idx = 0; while (itr.MoveNext()) { IGenericMap <Object, Object> matches = (IGenericMap <Object, Object>)memory.Get(itr.Current); IEnumerator itr2 = matches.Keys.GetEnumerator(); while (itr2.MoveNext()) { IGenericMap <Object, Object> submatch = (IGenericMap <Object, Object>)matches.Get(itr2.Current); IEnumerator itr3 = submatch.Values.GetEnumerator(); while (itr3.MoveNext()) { facts[idx] = itr3.Current; idx++; } } } Object[] trim = new Object[idx]; Array.Copy(facts, 0, trim, 0, idx); facts = null; return(trim); }
/// <summary> /// assertLeft takes an array of facts. Since the Current join may be /// joining against one or more objects, we need to pass all /// previously matched facts. /// </summary> /// <param name="linx">The linx.</param> /// <param name="engine">The engine.</param> /// <param name="mem">The mem.</param> public override void assertLeft(Index linx, Rete engine, IWorkingMemory mem) { IGenericMap <Object, Object> leftmem = mem.getBetaLeftMemory(this); leftmem.Put(linx, linx); //foreach(IFact rfcts in ((IGenericMap<IFact, IFact>)mem.getBetaRightMemory(this)).Values) //{ // propogateAssert(linx.add(rfcts), engine, mem); //} IGenericMap <IFact, IFact> rightmem = (IGenericMap <IFact, IFact>)mem.getBetaRightMemory(this); foreach (IFact rfcts in rightmem.Values) { propogateAssert(linx.add(rfcts), engine, mem); } //IEnumerator itr = rightmem.Values.GetEnumerator(); //while (itr.MoveNext()) //{ // IFact rfcts = (IFact)itr.Current; // // now we propogate // propogateAssert(linx.add(rfcts), engine, mem); //} }
/// <summary> Clear will Clear the lists /// </summary> public override void clear(IWorkingMemory mem) { IGenericMap <Object, Object> leftmem = mem.getBetaLeftMemory(this); IGenericMap <Object, Object> rightmem = (IGenericMap <Object, Object>)mem.getBetaRightMemory(this); // first we iterate over the list for each fact // and Clear it. foreach (object item in leftmem.Keys) { ((IBetaMemory)leftmem.Get(item)).clear(); } /* * IEnumerator itr = leftmem.Keys.GetEnumerator(); * // first we iterate over the list for each fact * // and Clear it. * while (itr.MoveNext()) * { * IBetaMemory bmem = (IBetaMemory) leftmem.Get(itr.Current); * bmem.clear(); * } */ // now that we've cleared the list for each fact, we // can Clear the Creshendo.rete.util.Map. leftmem.Clear(); rightmem.Clear(); }
/// <summary> NotJoin has to have a special addSuccessorNode since it needs /// to just propogate the left facts if it has zero matches. /// </summary> public override void addSuccessorNode(TerminalNode node, Rete engine, IWorkingMemory mem) { if (addNode(node)) { // first, we Get the memory for this node IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this); // now we iterate over the entry set IEnumerator itr = leftmem.Values.GetEnumerator(); while (itr.MoveNext()) { Object omem = itr.Current; if (omem is IBetaMemory) { IBetaMemory bmem = (IBetaMemory)omem; EqHashIndex inx = new EqHashIndex(NodeUtils.getLeftValues(binds, bmem.LeftFacts)); HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)mem.getBetaRightMemory(this); // we don't bother adding the right fact to the left, since // the right side is already Hashed if (rightmem.count(inx) == 0) { node.assertFacts(bmem.Index, engine, mem); } } } } }
/// <summary> addPartialMatch stores the fact with the factId as the /// key. /// </summary> public virtual int addPartialMatch(NotEqHashIndex index, IFact fact) { IGenericMap <Object, Object> matches = (IGenericMap <Object, Object>)memory.Get(index); int count = 0; if (matches == null) { count = addNewPartialMatch(index, fact); } else { IGenericMap <object, object> submatch = (IGenericMap <object, object>)matches.Get(index.SubIndex); if (submatch == null) { submatch = CollectionFactory.newHashMap(); submatch.Put(fact, fact); matches.Put(index.SubIndex, submatch); count = matches.Count; } else { submatch.Put(fact, fact); count = submatch.Count; } } counter++; return(count); }
/// <summary> /// Get an enumerable to iterate over neighbors of the given positon. /// </summary> /// <param name="map">The map from which the coordinates are sourced.</param> /// <param name="position">The position from which we want to pick neighbors.</param> /// <return>An enumerable to iterate over the neighbor positions.</return> public static IEnumerable <Vector> GetNeighbors(this IGenericMap map, Vector position) { Contract.Requires(map != null); Vector result = position; if (position.X > 0) { result.X = position.X - 1; yield return(result); } if (position.X + 1 < map.SizeX) { result.X = position.X + 1; yield return(result); } result.X = position.X; if (position.Y > 0) { result.Y = position.Y - 1; yield return(result); } if (position.Y + 1 < map.SizeY) { result.Y = position.Y + 1; yield return(result); } }
private void InitBlock() { asserts = new List <Object>(); retracts = new List <Object>(); profiles = new List <Object>(); nodeFilter = new GenericHashMap <object, object>(); }
/// <summary> Retracting from the left is different than retractRight for couple /// of reasons. /// <ul> /// <li> NotJoin will only propogate the facts from the left</li> /// <li> NotJoin never needs to merge the left and right</li> /// </ul> /// </summary> /// <param name="">factInstance /// </param> /// <param name="">engine /// /// </param> public override void retractLeft(Index linx, Rete engine, IWorkingMemory mem) { IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this); leftmem.Remove(linx); propogateRetract(linx, engine, mem); }
/// <summary> /// copies all mappings from the specified map to this map /// will replace any mappings that had matching keys /// </summary> /// <param name="t"></param> public void putAll(IGenericMap <K, V> t) { // Expand enough to hold t's elements without resizing. int n = t.Count; if (n == 0) { return; } if (n >= threshold) { n = (int)(n / loadFactor + 1); if (n > MAXIMUM_CAPACITY) { n = MAXIMUM_CAPACITY; } int capacity = table.Length; while (capacity < n) { capacity <<= 1; } Resize(capacity); } foreach (IHashMapEntry <K, V> entry in t) { Put(entry.Key, entry.Value); } }
/// <summary> assertLeft takes an array of facts. Since the Current join may be /// joining against one or more objects, we need to pass all /// previously matched facts. /// </summary> /// <param name="">factInstance /// </param> /// <param name="">engine /// /// </param> public override void assertLeft(Index linx, Rete engine, IWorkingMemory mem) { IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this); IBetaMemory bmem = new BetaMemoryImpl(linx); leftmem.Put(linx, bmem); IGenericMap <Object, Object> rightmem = (IGenericMap <Object, Object>)mem.getBetaRightMemory(this); IEnumerator itr = rightmem.Keys.GetEnumerator(); if (itr != null) { while (itr.MoveNext()) { IFact vl = (IFact)itr.Current; // we have to evaluate the function if (vl != null && evaluate(linx.Facts, vl, engine)) { bmem.addMatch(vl); } } } if (bmem.matchCount() > 0) { propogateAssert(linx, engine, mem); } }
/// <summary> return an List with all the facts /// </summary> /// <returns> /// /// </returns> public virtual Object[] iterateAll() { Object[] all = new Object[counter]; int idx = 0; foreach (object key in memory.Keys) { IGenericMap <Object, Object> f = (IGenericMap <Object, Object>)memory[key]; foreach (Object val in f.Values) { all[idx] = val; idx++; } } return(all); //Object[] all = new Object[counter]; //IEnumerator itr = memory.keySet().GetEnumerator(); //int idx = 0; //while (itr.MoveNext()) //{ // Map f = (Map) memory[itr.Current()]; // IEnumerator itr2 = f.Values.GetEnumerator(); // while (itr2.MoveNext()) // { // all[idx] = itr2.Current(); // idx++; // } //} //return all; }
/// <summary> /// it's unlikely 2 rules are identical, except for the name. The implementation /// gets the current memory and propogates, but I wonder how much sense this /// makes in a real production environment. An user really shouldn't be deploying /// identical rules with different rule name. /// </summary> /// <param name="node">The node.</param> /// <param name="engine">The engine.</param> /// <param name="mem">The mem.</param> public virtual void addSuccessorNode(TerminalNode node, Rete engine, IWorkingMemory mem) { if (addNode(node)) { // first, we Get the memory for this node IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this); // now we iterate over the entry set IEnumerator itr = leftmem.Values.GetEnumerator(); while (itr.MoveNext()) { Object omem = itr.Current; if (omem is IBetaMemory) { IBetaMemory bmem = (IBetaMemory)omem; Index left = bmem.Index; // iterate over the matches IGenericMap <Object, Object> rightmem = (IGenericMap <Object, Object>)mem.getBetaRightMemory(this); IEnumerator ritr = rightmem.Keys.GetEnumerator(); while (ritr.MoveNext()) { IFact rfcts = (IFact)ritr.Current; // merge the left and right fact into a new Array node.assertFacts(left.add(rfcts), engine, mem); } } } } }
/// <summary> Assert from the right side is always going to be from an Alpha node. /// /// </summary> /// <param name="">factInstance /// </param> /// <param name="">engine /// /// </param> public override void assertRight(IFact rfact, Rete engine, IWorkingMemory mem) { // Get the memory for the node HashedNeqAlphaMemory rightmem = (HashedNeqAlphaMemory)mem.getBetaRightMemory(this); NotEqHashIndex inx = new NotEqHashIndex(NodeUtils.getRightBindValues(binds, rfact)); rightmem.addPartialMatch(inx, rfact); // now that we've added the facts to the list, we // proceed with evaluating the fact // else we compare the fact to all facts in the left IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this); // since there may be key collisions, we iterate over the // values of the HashMap. If we used keySet to iterate, // we could encounter a ClassCastException in the case of // key collision. IEnumerator itr = leftmem.Values.GetEnumerator(); while (itr.MoveNext()) { Index linx = (Index)itr.Current; if (evaluate(linx.Facts, rfact)) { // now we propogate propogateAssert(linx.add(rfact), engine, mem); } } }
/* static DefaultRuleCompiler() { FUNCTION_NOT_FOUND = Messages.getString("CompilerProperties_function_not_found"); INVALID_FUNCTION = Messages.getString("CompilerProperties_invalid_function"); ASSERT_ON_PROPOGATE = Messages.getString("CompilerProperties_assert_on_Add"); } */ public DefaultRuleCompiler(Rete engine, IGenericMap<object, object> inputNodes) { InitBlock(); this.engine = engine; this.inputnodes = inputNodes; tval = new TemplateValidation(engine); }
/* * static DefaultRuleCompiler() * { * FUNCTION_NOT_FOUND = Messages.getString("CompilerProperties_function_not_found"); * INVALID_FUNCTION = Messages.getString("CompilerProperties_invalid_function"); * ASSERT_ON_PROPOGATE = Messages.getString("CompilerProperties_assert_on_Add"); * } */ public DefaultRuleCompiler(Rete engine, IGenericMap <object, object> inputNodes) { InitBlock(); this.engine = engine; this.inputnodes = inputNodes; tval = new TemplateValidation(engine); }
/// <summary> Assert from the right side is always going to be from an Alpha node. /// /// </summary> /// <param name="">factInstance /// </param> /// <param name="">engine /// /// </param> public override void assertRight(IFact rfact, Rete engine, IWorkingMemory mem) { // Get the memory for the node HashedNeqAlphaMemory rightmem = (HashedNeqAlphaMemory)mem.getBetaRightMemory(this); NotEqHashIndex inx = new NotEqHashIndex(NodeUtils.getRightBindValues(binds, rfact)); rightmem.addPartialMatch(inx, rfact); bool zm = rightmem.zeroMatch(inx); IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this); IEnumerator itr = leftmem.Values.GetEnumerator(); while (itr.MoveNext()) { Index linx = (Index)itr.Current; if (evaluate(linx.Facts, rfact)) { if (!zm) { try { propogateRetract(linx, engine, mem); } catch (RetractException e) { throw new AssertException("NotJion - " + e.Message); } } } } }
/// <summary> Retract from the right works in the following order. /// 1. Remove the fact from the right memory /// 2. check which left memory matched /// 3. propogate the retract /// </summary> /// <param name="">factInstance /// </param> /// <param name="">engine /// /// </param> public override void retractRight(IFact rfact, Rete engine, IWorkingMemory mem) { NotEqHashIndex inx = new NotEqHashIndex(NodeUtils.getRightBindValues(binds, rfact)); HashedNeqAlphaMemory rightmem = (HashedNeqAlphaMemory)mem.getBetaRightMemory(this); // first we Remove the fact from the right rightmem.removePartialMatch(inx, rfact); bool zm = rightmem.zeroMatch(inx); // now we see the left memory matched and Remove it also IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this); IEnumerator itr = leftmem.Values.GetEnumerator(); while (itr.MoveNext()) { Index linx = (Index)itr.Current; if (evaluate(linx.Facts, rfact)) { if (zm) { try { propogateAssert(linx, engine, mem); } catch (AssertException e) { throw new RetractException("NotJion - " + e.Message); } } } } }
public virtual void addNewPartialMatch(IHashIndex index, IFact fact) { IGenericMap <object, object> matches = CollectionFactory.newMap(); matches.Put(fact, fact); memory.Put(index, matches); }
/// <summary> /// assertLeft takes an array of facts. Since the Current join may be /// joining against one or more objects, we need to pass all /// previously matched facts. /// </summary> /// <param name="linx">The linx.</param> /// <param name="engine">The engine.</param> /// <param name="mem">The mem.</param> public override void assertLeft(Index linx, Rete engine, IWorkingMemory mem) { IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this); // we create a new list for storing the matches. // any fact that isn't in the list will be evaluated. IBetaMemory bmem = new BetaMemoryImpl(linx); leftmem.Put(bmem.Index, bmem); IGenericMap <IFact, IFact> rightmem = (IGenericMap <IFact, IFact>)mem.getBetaRightMemory(this); int prevCount = bmem.matchCount(); IEnumerator itr = rightmem.Values.GetEnumerator(); while (itr.MoveNext()) { IFact rfcts = (IFact)itr.Current; if (evaluate(linx.Facts, rfcts, engine)) { // it matched, so we Add it to the beta memory bmem.addMatch(rfcts); } } // since the Fact[] is entering the left for the first time, // if there are no matches, we merged the facts propogate. if (bmem.matchCount() == 0) { propogateAssert(linx, engine, mem); } }
public virtual void clear() { IEnumerator amitr = alphaMemories.Values.GetEnumerator(); while (amitr.MoveNext()) { IAlphaMemory am = (IAlphaMemory)amitr.Current; am.clear(); } alphaMemories.Clear(); // aggressivley Clear the memories IEnumerator blitr = betaLeftMemories.Values.GetEnumerator(); while (blitr.MoveNext()) { Object bval = blitr.Current; if (bval is IGenericMap <Object, Object> ) { IGenericMap <Object, Object> lmem = (IGenericMap <Object, Object>)bval; // now iterate over the betamemories IEnumerator bmitr = lmem.Keys.GetEnumerator(); while (bmitr.MoveNext()) { Index indx = (Index)bmitr.Current; indx.clear(); } lmem.Clear(); } } betaLeftMemories.Clear(); IEnumerator britr = betaRightMemories.Values.GetEnumerator(); while (britr.MoveNext()) { Object val = britr.Current; if (val is HashedAlphaMemoryImpl) { ((HashedAlphaMemoryImpl)val).clear(); } else if (val is TemporalHashedAlphaMem) { ((TemporalHashedAlphaMem)val).clear(); } else { IGenericMap <IFact, IFact> mem = (IGenericMap <IFact, IFact>)val; mem.Clear(); } } betaRightMemories.Clear(); terminalMemories.Clear(); root.clear(); focusStack.Clear(); //contexts.Clear(); agenda.clear(); main.clear(); currentModule.clear(); addModule(main); }
private void InitBlock() { bindValues = new GenericHashMap <string, Object>(); bindings = new GenericHashMap <string, Binding>(); direction = Constants.FORWARD_CHAINING; }
/// <summary> /// Subclasses need to implement Clear and make sure all /// memories are cleared properly. /// </summary> /// <param name="mem">The mem.</param> public virtual void clear(IWorkingMemory mem) { IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this); leftmem.Clear(); HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)mem.getBetaRightMemory(this); rightmem.clear(); }
/// <summary> /// The terminal nodes doesn't have a memory, so the method /// does nothing. /// </summary> /// <param name="mem"></param> public override void clear(IWorkingMemory mem) { IGenericMap <Object, Object> tmem = (IGenericMap <Object, Object>)mem.getTerminalMemory(this); if (tmem != null) { tmem.Clear(); } }
public virtual int addNewPartialMatch(NotEqHashIndex index, IFact fact) { IGenericMap <object, object> matches = CollectionFactory.newHashMap(); IGenericMap <object, object> submatch = CollectionFactory.newHashMap(); submatch.Put(fact, fact); matches.Put(index.SubIndex, submatch); memory.Put(index, matches); return(1); }
/// <summary> /// Retracts the facts. /// </summary> /// <param name="inx">The inx.</param> /// <param name="engine">The engine.</param> /// <param name="mem">The mem.</param> public override void retractFacts(Index inx, Rete engine, IWorkingMemory mem) { IGenericMap <Object, Object> tmem = (IGenericMap <Object, Object>)mem.getTerminalMemory(this); LinkedActivation act = (LinkedActivation)tmem.RemoveWithReturn(inx); if (act != null) { engine.Agenda.removeActivation(act); } }
/// <summary> /// </summary> public override void retractLeft(Index linx, Rete engine, IWorkingMemory mem) { IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this); if (leftmem.ContainsKey(linx)) { // the memory Contains the key, so we retract and propogate leftmem.Remove(linx); propogateRetract(linx, engine, mem); } }
/// <summary> /// Asserts the facts. /// </summary> /// <param name="inx">The inx.</param> /// <param name="engine">The engine.</param> /// <param name="mem">The mem.</param> public override void assertFacts(Index inx, Rete engine, IWorkingMemory mem) { LinkedActivation act = new LinkedActivation(theRule, inx); act.TerminalNode = this; IGenericMap <Object, Object> tmem = (IGenericMap <Object, Object>)mem.getTerminalMemory(this); tmem.Put(inx, act); // Add the activation to the current module's activation list. engine.Agenda.addActivation(act); }
/// <summary> TODO - not implemented yet /// </summary> public virtual void printWorkingMemory(IGenericMap <Object, Object> filter) { if (filter != null && filter.Count > 0) { // not implemented yet } else { printWorkingMemory(true, false); } }
public void testAssertLeftOne() { // 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); TestBean2 bean = new TestBean2(); bean.Attr1 = ("random1"); bean.Attr2 = (101); short s = 10001; bean.Attr3 = (s); long l = 10101018; bean.Attr4 = (l); bean.Attr5 = (1010101); bean.Attr6 = (1001.1001); IFact f1 = dtemp.createFact(bean, dc, engine.nextFactId()); try { bn.assertLeft(new Index(new IFact[] { f1 }), engine, engine.WorkingMemory); IGenericMap <Object, Object> bmem = (IGenericMap <Object, Object>)engine.WorkingMemory.getBetaLeftMemory(bn); Assert.AreEqual(1, bmem.Count); } catch (AssertException e) { Console.WriteLine(e.Message); } engine.close(); }
/// <summary> Remove a partial match from the memory /// </summary> public virtual int removePartialMatch(IHashIndex index, IFact fact) { IGenericMap <Object, Object> list = (IGenericMap <Object, Object>)memory[index]; list.Remove(fact); if (list.Count == 0) { memory.Remove(index); } counter--; return(list.Count); }
private void InitBlock() { asserts = new List<Object>(); retracts = new List<Object>(); profiles = new List<Object>(); nodeFilter = new GenericHashMap<object, object>(); }
private void InitBlock() { variables = CollectionFactory.newHashMap(); }
private void InitBlock() { modules = CollectionFactory.localMap(); }
private void InitBlock() { entries = CollectionFactory.localMap(); successor2 = new List<Object>(); }
private void InitBlock() { rules = CollectionFactory.localMap(); deftemplates = CollectionFactory.localMap(); }
private void InitBlock() { alphaMemories = CollectionFactory.newMap(); betaLeftMemories = new GenericHashMap<object, IGenericMap<object, object>>(); betaRightMemories = CollectionFactory.newMap(); terminalMemories = CollectionFactory.newMap(); staticFacts = CollectionFactory.localMap(); dynamicFacts = CollectionFactory.localMap(); deffactMap = CollectionFactory.localMap(); defglobals = new DefglobalMap(); initialFacts = new List<Object>(); modules = CollectionFactory.localMap(); focusStack = new List<Object>(); scopes = new Stack(); }
/// <summary> TODO - not implemented yet /// </summary> public virtual void printWorkingMemory(IGenericMap<Object, Object> filter) { if (filter != null && filter.Count > 0) { // not implemented yet } else { printWorkingMemory(true, false); } }
/// <summary> /// </summary> public AlphaMemoryImpl(String name) { memory = CollectionFactory.newAlphaMemoryMap(name); }
private void InitBlock() { defclass = CollectionFactory.localMap(); templateToDefclass = CollectionFactory.localMap(); functions = CollectionFactory.localMap(); outputStreams = CollectionFactory.localMap(); listeners = new List<Object>(); functionGroups = new List<Object>(); //log = new Log4netLogger(typeof (Rete)); router = new MessageRouter(this); initFact = new InitialFact(); deffunctions = new DeffunctionGroup(); root = new RootNode(); rulesFired = CollectionFactory.localMap(); }
private void InitBlock() { entries = CollectionFactory.localMap(); }
private void InitBlock() { methods = CollectionFactory.localMap(); }
private void InitBlock() { matches = CollectionFactory.newMap(); }
private void InitBlock() { bindValues = new GenericHashMap<string, Object>(); bindings = new GenericHashMap<string, Binding>(); direction = Constants.FORWARD_CHAINING; }