/// <summary> Current implemenation will return the index of the activation, if /// it is in the Creshendo.rete.util.LinkedList. If activation isn't in the list, the method /// returns -1. /// </summary> public virtual int indexOf(Object activation) { int index = -1; LinkedActivation la = first; LinkedActivation match = null; while (la != null) { index++; if (la == activation) { match = la; break; } else { la = la.Next; } } if (match != null) { return(index); } else { return(-1); } }
public override void addActivation(IActivation act) { if (act is LinkedActivation) { LinkedActivation newact = (LinkedActivation)act; if (lazy) { if (count == 0) { first = newact; last = newact; } else { last.Next = newact; last = newact; } count++; } else { if (count > 0) { quickSort(newact); } else if (count == 0) { first = newact; last = newact; } count++; } } }
/// <summary> /// </summary> /// <param name="inx"></param> /// <param name="engine"></param> /// <param name="mem"></param> public override void assertFacts(Index inx, Rete engine, IWorkingMemory mem) { LinkedActivation act = new LinkedActivation(theRule, inx); act.TerminalNode = this; // fire the activation immediately engine.fireActivation(act); }
/// <summary> /// the sort method uses binary search to find the correct insertion /// point for the new activation. It's much faster than the brute /// force method. /// </summary> /// <param name="newact">The newact.</param> public virtual void quickSort(LinkedActivation newact) { if (stratey.compare(newact, last) >= 0) { // the new activation has a higher salience than the last, which means // it should become the bottom activation last.Next = newact; last = newact; } else if (stratey.compare(newact, first) < 0) { // the new activation has a salience lower than the first, which means // it should become the top activation newact.Next = first; first = newact; } else { // this means the new activation goes in the middle some where int counter = count / 2; LinkedActivation cur = goUp(counter, last); bool added = false; while (!added) { if (counter <= 1) { // Add the activation if (stratey.compare(newact, cur) < 0) { // if the new activation is lower sailence than the current, // we Add it before the current (aka above) newact.Previous = cur.Previous; newact.Next = cur; } else { // the new activation is higher salience than the current // therefore we Add it after (aka below) newact.Next = cur.Next; newact.Previous = cur; } added = true; } else if (stratey.compare(newact, cur) >= 0) { // the new activation is of greater salience down half again counter = counter / 2; cur = goDown(counter, cur); } else { // the new activation is of lower salience, up half again counter = counter / 2; cur = goUp(counter, cur); } } } }
public override IActivation nextActivation() { if (lazy) { if (count == 0) { return(null); } else { LinkedActivation left = last; LinkedActivation right = last.Previous; while (right != null) { if (stratey.compare(left, right) < 1) { left = right; } right = right.Previous; } if (left == first) { first = left.Next; } else if (left == last) { last = left.Previous; } left.remove(); count--; return(left); } } else { if (count > 1) { LinkedActivation r = last; last = r.Previous; count--; r.remove(); return(r); } else if (count == 1) { LinkedActivation r = last; last = null; first = null; count--; return(r); } else { return(null); } } }
/// <summary> /// method will loop for the given count and return the item before it. /// for example: /// 1 /// 2 /// 3 /// 4 /// 5 /// 6 /// If I pass a count of 2 and item #6. it will return #4. /// </summary> /// <param name="count">The count.</param> /// <param name="start">The start.</param> /// <returns></returns> protected internal virtual LinkedActivation goUp(int count, LinkedActivation start) { LinkedActivation rt = start; for (int idx = 0; idx < count; idx++) { rt = rt.Previous; } return(rt); }
/// <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> /// method will loop for the given count and return the item after it. /// for example: /// 1 /// 2 /// 3 /// 4 /// 5 /// 6 /// If I pass a count of 2 and item #1. it will return #3. /// </summary> /// <param name="count">The count.</param> /// <param name="start">The start.</param> /// <returns></returns> protected internal virtual LinkedActivation goDown(int count, LinkedActivation start) { LinkedActivation rt = start; for (int idx = 0; idx < count; idx++) { rt = rt.Next; } return(rt); }
/// <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> Iterate over the Creshendo.rete.util.LinkedList and null the references to previous /// and Current in the LinkedActivation /// </summary> public override void clear() { while (first != null) { LinkedActivation la = first; first = la.Next; la.remove(); } last = null; count = 0; }
/// <param name="">facts /// </param> /// <param name="">engine /// /// </param> public override void assertFacts(Index inx, Rete engine, IWorkingMemory mem) { long time = (DateTime.Now.Ticks - 621355968000000000) / 10000; if (theRule.ExpirationDate > 0 && time > theRule.EffectiveDate && time < theRule.ExpirationDate) { LinkedActivation act = new LinkedActivation(theRule, inx); act.TerminalNode = this; // fire the activation immediately engine.fireActivation(act); } }
/// <summary> The implementation checks to see if the rule is active before it tries to /// retract the fact. It checks in the following order. /// 1. is the expiration date greater than zero /// 2. is the current time > the effective date /// 3. is the current time the expiration date /// </summary> /// <param name="">facts /// </param> /// <param name="">engine /// /// </param> public override void retractFacts(Index inx, Rete engine, IWorkingMemory mem) { long time = (DateTime.Now.Ticks - 621355968000000000) / 10000; if (theRule.ExpirationDate > 0 && time > theRule.EffectiveDate && time < theRule.ExpirationDate) { IGenericMap <Object, Object> tmem = (IGenericMap <Object, Object>)mem.getTerminalMemory(this); LinkedActivation act = (LinkedActivation)tmem.RemoveWithReturn(inx); if (act != null) { engine.Agenda.removeActivation(act); } } }
/// <summary> The implementation checks to see if the rule is active before it tries to /// assert the fact. It checks in the following order. /// 1. is the expiration date greater than zero /// 2. is the current time > the effective date /// 3. is the current time the expiration date /// </summary> /// <param name="">facts /// </param> /// <param name="">engine /// /// </param> public override void assertFacts(Index inx, Rete engine, IWorkingMemory mem) { long time = (DateTime.Now.Ticks - 621355968000000000) / 10000; if (theRule.ExpirationDate > 0 && time > theRule.EffectiveDate && time < theRule.ExpirationDate) { LinkedActivation act = new LinkedActivation(theRule, inx); act.TerminalNode = this; IGenericMap <Object, Object> tmem = (IGenericMap <Object, Object>)mem.getTerminalMemory(this); tmem.Put(act.Index, act); // Add the activation to the current module's activation list. engine.Agenda.addActivation(act); } }
/// <summary> /// Remove the Activation from the list and set the previous /// and Current activation correctly. There's basically 3 cases /// we have to handle. /// 1. first /// 2. last /// 3. somewhere in between /// The current implementation will first set the previous /// and Current. Once they are correctly set, it will set /// the references to those LinkedActivation to null. /// </summary> public void remove() { if (prev != null && next != null) { prev.Next = next; } else if (prev != null && next == null) { prev.Next = null; } else if (prev == null && next != null) { next.Previous = null; } prev = null; next = null; }
public virtual Object set(int index, Object activation) { if (index < count && activation != null) { LinkedActivation act = first; for (int idx = 0; idx <= count; idx++) { act = act.Next; } // now we are at the index point LinkedActivation pre = act.Previous; LinkedActivation nxt = act.Next; pre.Next = (LinkedActivation)activation; nxt.Previous = (LinkedActivation)activation; act.remove(); } return(activation); }
/// <summary> removeActivation will check to see if the activation is /// the first or last before removing it. /// </summary> public override IActivation removeActivation(IActivation act) { if (act is LinkedActivation) { LinkedActivation lact = (LinkedActivation)act; if (first == lact) { first = lact.Next; } if (last == lact) { last = lact.Previous; } count--; lact.remove(); } return(act); }
/// <summary> the current implementation iterates over the LinkedActivations /// from the start until it finds a match. If it doesn't find a /// match, the method returns false. /// </summary> public virtual bool contains(Object o) { bool contain = false; LinkedActivation act = first; while (act != null) { if (o == act) { contain = true; break; } else { act = act.Next; } } return(contain); }
/// <summary> method will clone the list and make a copy of the activations /// </summary> public override IActivationList cloneActivationList() { LinkedActivationList la = new LinkedActivationList(); la.count = count; la.first = first.cloneActivation(); la.lazy = lazy; la.stratey = stratey; LinkedActivation current = first; LinkedActivation newcurr = la.first; while (current != null) { newcurr.Next = current.Next.cloneActivation(); current = current.Next; newcurr = newcurr.Next; } return(la); }
/// <summary> Method will call checkFacts() first to make sure none of the facts have /// expired. An activation is only created if the facts are valid. /// </summary> /// <param name="">facts /// </param> /// <param name="">engine /// /// </param> public override void assertFacts(Index inx, Rete engine, IWorkingMemory mem) { // first check the facts and make sure they didn't expire if (checkFacts(inx, engine, mem)) { LinkedActivation act = new LinkedActivation(theRule, inx); act.TerminalNode = this; if (temporal) { engine.fireActivation(act); } else { 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> /// method doesn't apply for no agenda terminal node /// </summary> /// <param name="mem"></param> /// <param name="activation"></param> public override void removeActivation(IWorkingMemory mem, LinkedActivation activation) { }
/// <summary> /// Remove the LinkedActivation from TerminalNode2. This is necessary /// when the activation is fired and the actions executed. /// </summary> /// <param name="mem">The mem.</param> /// <param name="activation">The activation.</param> public virtual void removeActivation(IWorkingMemory mem, LinkedActivation activation) { IGenericMap <Object, Object> tmem = (IGenericMap <Object, Object>)mem.getTerminalMemory(this); tmem.Remove(activation.Index); }
/// <summary> /// Clones the activation. /// </summary> /// <returns></returns> public LinkedActivation cloneActivation() { LinkedActivation la = new LinkedActivation(theRule, index); return(la); }