/// <summary> Method takes a list of Slots and creates a deffact from it. /// /// </summary> /// <param name="">data /// </param> /// <param name="">id /// </param> /// <returns> /// /// </returns> public virtual IFact createFact(IList data, long id) { Slot[] values = cloneAllSlots(); IEnumerator itr = data.GetEnumerator(); while (itr.MoveNext()) { Slot s = (Slot)itr.Current; for (int idx = 0; idx < values.Length; idx++) { if (values[idx].Name.Equals(s.Name)) { if (s.Value == null) { values[idx].Value = Constants.NIL_SYMBOL; } else if (values[idx].ValueType == Constants.STRING_TYPE && !(s.Value is BoundParam)) { values[idx].Value = s.Value.ToString(); } else { values[idx].Value = s.Value; } } } } Deffact newfact = new Deffact(this, null, values, id); // we call this to create the string used to map the fact. newfact.equalityIndex(); return(newfact); }
public virtual void assertFact(IFact fact) { Deffact f = (Deffact)fact; if (!containsFact(f)) { deffactMap.Put(fact.equalityIndex(), f); f.setFactId = engine; if (profileAssert_Renamed_Field) { assertFactWProfile(f); } else { if (watchFact_Renamed_Field) { engine.writeMessage("==> " + fact.toFactString() + Constants.LINEBREAK, "t"); } root.assertObject(f, engine, this); } } else { f.resetID((Deffact)deffactMap.Get(fact.equalityIndex())); } }
/// <summary> the current implementation only compares the values, since the slot /// names are equal. It would be a waste of time to compare the slot /// names. The exception to the case is when a deftemplate is changed. /// Since that feature isn't supported yet, it's currently not an issue. /// Even if updating deftemplates is added in the future, the deffacts /// need to be updated. If the deffacts weren't updated, it could lead /// to NullPointerExceptions. /// </summary> /// <param name="">fact /// </param> /// <returns> /// /// </returns> public virtual bool slotEquals(Deffact fact) { bool eq = true; Slot[] cslots = fact.slots; for (int idx = 0; idx < slots.Length; idx++) { if (!slots[idx].Value.Equals(cslots[idx].Value)) { eq = false; break; } } return(eq); }
/// <summary> Convienance method for cloning a fact. If a slot's value is a BoundParam, /// the cloned fact uses the value of the BoundParam. /// </summary> /// <returns> /// /// </returns> public virtual Deffact cloneFact() { Deffact newfact = new Deffact(deftemplate, objInstance, deftemplate.cloneAllSlots(), -1); Slot[] slts = newfact.slots; for (int idx = 0; idx < slts.Length; idx++) { // probably need to revisit this and make sure if (slots[idx] is MultiSlot) { // it's multislot so we have to replace the bound values // correctly MultiSlot ms = (MultiSlot)slots[idx]; Object[] sval = (Object[])ms.Value; Object[] mval = new Object[sval.Length]; for (int mdx = 0; mdx < mval.Length; mdx++) { Object v = sval[mdx]; if (v is BoundParam) { mval[mdx] = ((BoundParam)v).Value; } else { mval[mdx] = v; } } slts[idx].Value = mval; } else if (slots[idx].Value is BoundParam) { if (slts[idx].ValueType == Constants.STRING_TYPE) { slts[idx].Value = ((BoundParam)slots[idx].Value).Value.ToString(); } else { slts[idx].Value = ((BoundParam)slots[idx].Value).Value; } } else { slts[idx].Value = slots[idx].Value; } } return(newfact); }
public virtual void retractFact(IFact fact) { Deffact f = (Deffact)fact; deffactMap.Remove(f.equalityIndex()); if (profileRetract_Renamed_Field) { retractFactWProfile(f); } else { if (watchFact_Renamed_Field) { engine.writeMessage("<== " + fact.toFactString() + Constants.LINEBREAK, "t"); } root.retractObject(f, engine, this); } }
/// <summary> Method will create a Fact from the given object instance /// /// </summary> /// <param name="">data /// </param> /// <returns> /// /// </returns> public virtual IFact createFact(Object data, Defclass clazz, long id) { // first we clone the slots Slot[] values = cloneAllSlots(); // now we set the values for (int idx = 0; idx < values.Length; idx++) { Object val = clazz.getSlotValue(idx, data); if (val == null) { values[idx].Value = Constants.NIL_SYMBOL; } else { values[idx].Value = val; } } Deffact newfact = new Deffact(this, data, values, id); return(newfact); }
/// ----- helper methods that are not defined in WorkingMemory interface ----- /// protected internal virtual void assertFactWProfile(Deffact fact) { ProfileStats.startAssert(); root.assertObject(fact, engine, this); ProfileStats.endAssert(); }
/// <summary> /// Modify retracts the old fact and asserts the new fact. Unlike assertFact, /// modifyFact will not check to see if the fact already exists. This is /// because the old fact would already be unique. /// </summary> /// <param name="old">The old.</param> /// <param name="newfact">The newfact.</param> public virtual void modifyFact(Deffact old, Deffact newfact) { retractFact(old); assertFact(newfact); }
/// <summary> /// Retract a fact directly /// </summary> /// <param name="fact">The fact.</param> public virtual void retractFact(Deffact fact) { workingMem.retractFact(fact); }
/// <summary> /// This method is explicitly used to assert facts. /// </summary> /// <param name="fact">The fact.</param> public virtual void assertFact(Deffact fact) { workingMem.assertFact(fact); }
/// <summary> this is used to reset the id, in the event an user tries to /// assert the same fact again, we reset the id to the existing one. /// </summary> /// <param name="">fact /// /// </param> protected internal virtual void resetID(Deffact fact) { id = fact.id; }
/// <summary> /// </summary> public EqualityIndex(Deffact facts) { fact = facts; calculateHash(); }
public virtual IFact createFact(Object[] data, long id) { Slot[] values = cloneAllSlots(); List<Object> bslots = new List<Object>(); bool hasbinding = false; for (int idz = 0; idz < data.Length; idz++) { Slot s = (Slot) data[idz]; for (int idx = 0; idx < values.Length; idx++) { if (values[idx].Name.Equals(s.Name)) { if (s is MultiSlot) { // since the value is multislot, we have to // check for boundparams MultiSlot ms = (MultiSlot) s; Object[] mvals = (Object[]) ms.Value; for (int mdx = 0; mdx < mvals.Length; mdx++) { if (mvals[mdx] is BoundParam) { bslots.Add((MultiSlot) ms.Clone()); hasbinding = true; break; } } values[idx].Value = s.Value; } else { if (s.Value == null) { values[idx].Value = Constants.NIL_SYMBOL; } else if (values[idx].ValueType == Constants.STRING_TYPE && !(s.Value is BoundParam)) { values[idx].Value = s.Value.ToString(); } else if (s.Value is BoundParam) { values[idx].Value = s.Value; bslots.Add((Slot) s.Clone()); hasbinding = true; } else { values[idx].Value = s.Value; } } break; } } } Deffact newfact = new Deffact(this, null, values, id); if (hasbinding) { Slot[] slts2 = new Slot[bslots.Count]; bslots.CopyTo(slts2,0); newfact.boundSlots = slts2; newfact.hasBinding_Renamed_Field = true; } // we call this to create the string used to map the fact. newfact.equalityIndex(); return newfact; }
/// <summary> Method takes a list of Slots and creates a deffact from it. /// /// </summary> /// <param name="">data /// </param> /// <param name="">id /// </param> /// <returns> /// /// </returns> public virtual IFact createFact(IList data, long id) { Slot[] values = cloneAllSlots(); IEnumerator itr = data.GetEnumerator(); while (itr.MoveNext()) { Slot s = (Slot) itr.Current; for (int idx = 0; idx < values.Length; idx++) { if (values[idx].Name.Equals(s.Name)) { if (s.Value == null) { values[idx].Value = Constants.NIL_SYMBOL; } else if (values[idx].ValueType == Constants.STRING_TYPE && !(s.Value is BoundParam)) { values[idx].Value = s.Value.ToString(); } else { values[idx].Value = s.Value; } } } } Deffact newfact = new Deffact(this, null, values, id); // we call this to create the string used to map the fact. newfact.equalityIndex(); return newfact; }
public virtual bool containsFact(Deffact fact) { return deffactMap.ContainsKey(fact.equalityIndex()); }
/// <summary> /// Retracts the fact W profile. /// </summary> /// <param name="fact">The fact.</param> protected internal virtual void retractFactWProfile(Deffact fact) { ProfileStats.startRetract(); root.retractObject(fact, engine, this); ProfileStats.endRetract(); }
public virtual bool containsFact(Deffact fact) { return(deffactMap.ContainsKey(fact.equalityIndex())); }
/// <summary> Convienance method for cloning a fact. If a slot's value is a BoundParam, /// the cloned fact uses the value of the BoundParam. /// </summary> /// <returns> /// /// </returns> public virtual Deffact cloneFact() { Deffact newfact = new Deffact(deftemplate, objInstance, deftemplate.cloneAllSlots(), - 1); Slot[] slts = newfact.slots; for (int idx = 0; idx < slts.Length; idx++) { // probably need to revisit this and make sure if (slots[idx] is MultiSlot) { // it's multislot so we have to replace the bound values // correctly MultiSlot ms = (MultiSlot) slots[idx]; Object[] sval = (Object[]) ms.Value; Object[] mval = new Object[sval.Length]; for (int mdx = 0; mdx < mval.Length; mdx++) { Object v = sval[mdx]; if (v is BoundParam) { mval[mdx] = ((BoundParam) v).Value; } else { mval[mdx] = v; } } slts[idx].Value = mval; } else if (slots[idx].Value is BoundParam) { if (slts[idx].ValueType == Constants.STRING_TYPE) { slts[idx].Value = ((BoundParam) slots[idx].Value).Value.ToString(); } else { slts[idx].Value = ((BoundParam) slots[idx].Value).Value; } } else { slts[idx].Value = slots[idx].Value; } } return newfact; }
/// <summary> the current implementation only compares the values, since the slot /// names are equal. It would be a waste of time to compare the slot /// names. The exception to the case is when a deftemplate is changed. /// Since that feature isn't supported yet, it's currently not an issue. /// Even if updating deftemplates is added in the future, the deffacts /// need to be updated. If the deffacts weren't updated, it could lead /// to NullPointerExceptions. /// </summary> /// <param name="">fact /// </param> /// <returns> /// /// </returns> public virtual bool slotEquals(Deffact fact) { bool eq = true; Slot[] cslots = fact.slots; for (int idx = 0; idx < slots.Length; idx++) { if (!slots[idx].Value.Equals(cslots[idx].Value)) { eq = false; break; } } return eq; }
/// <summary> Method will create a Fact from the given object instance /// /// </summary> /// <param name="">data /// </param> /// <returns> /// /// </returns> public virtual IFact createFact(Object data, Defclass clazz, long id) { // first we clone the slots Slot[] values = cloneAllSlots(); // now we set the values for (int idx = 0; idx < values.Length; idx++) { Object val = clazz.getSlotValue(idx, data); if (val == null) { values[idx].Value = Constants.NIL_SYMBOL; } else { values[idx].Value = val; } } Deffact newfact = new Deffact(this, data, values, id); return newfact; }
public virtual IFact createFact(Object[] data, long id) { Slot[] values = cloneAllSlots(); List <Object> bslots = new List <Object>(); bool hasbinding = false; for (int idz = 0; idz < data.Length; idz++) { Slot s = (Slot)data[idz]; for (int idx = 0; idx < values.Length; idx++) { if (values[idx].Name.Equals(s.Name)) { if (s is MultiSlot) { // since the value is multislot, we have to // check for boundparams MultiSlot ms = (MultiSlot)s; Object[] mvals = (Object[])ms.Value; for (int mdx = 0; mdx < mvals.Length; mdx++) { if (mvals[mdx] is BoundParam) { bslots.Add((MultiSlot)ms.Clone()); hasbinding = true; break; } } values[idx].Value = s.Value; } else { if (s.Value == null) { values[idx].Value = Constants.NIL_SYMBOL; } else if (values[idx].ValueType == Constants.STRING_TYPE && !(s.Value is BoundParam)) { values[idx].Value = s.Value.ToString(); } else if (s.Value is BoundParam) { values[idx].Value = s.Value; bslots.Add((Slot)s.Clone()); hasbinding = true; } else { values[idx].Value = s.Value; } } break; } } } Deffact newfact = new Deffact(this, null, values, id); if (hasbinding) { Slot[] slts2 = new Slot[bslots.Count]; bslots.CopyTo(slts2, 0); newfact.boundSlots = slts2; newfact.hasBinding_Renamed_Field = true; } // we call this to create the string used to map the fact. newfact.equalityIndex(); return(newfact); }