/// <summary> /// The current implementation of assertObject is simple, but flexible. This /// version is not multi-threaded and doesn't use an event queue. Later on a /// multi-threaded version will be written which overrides the base /// implementation. If the user passes a specific template name, the engine /// will attempt to only propogate the fact down that template. if no /// template name is given, the engine will propogate the fact down all input /// nodes, including parent templates. /// </summary> /// <param name="data">The data.</param> /// <param name="template">The template.</param> /// <param name="statc">if set to <c>true</c> [statc].</param> /// <param name="shadow">if set to <c>true</c> [shadow].</param> public virtual void assertObject(Object data, String template, bool statc, bool shadow) { Defclass dc = null; if (template == null) { dc = engine.findDefclass(data); } else { dc = engine.findDefclassByTemplate(template); } if (dc != null) { if (statc && !StaticFacts.ContainsKey(data)) { IFact shadowfact = createFact(data, dc, template, engine.nextFactId()); // Add it to the static fact map StaticFacts.Put(data, shadowfact); assertFact(shadowfact); } else if (!DynamicFacts.ContainsKey(data)) { if (shadow) { // first Add the rule engine as a listener if (dc.JavaBean) { try { MethodInfo miHandler = typeof(Rete).GetMethod("PropertyHasChanged", BindingFlags.NonPublic | BindingFlags.Instance); Delegate d = Delegate.CreateDelegate(dc.DelegateType, this.engine, miHandler); dc.AddListenerMethod.Invoke(data, (Object[])new Object[] { d }); } catch (TargetInvocationException e) { System.Diagnostics.Trace.WriteLine(e.Message); } catch (UnauthorizedAccessException e) { System.Diagnostics.Trace.WriteLine(e.Message); } } // second, lookup the deftemplate and create the // shadow fact IFact shadowfact = createFact(data, dc, template, engine.nextFactId()); // Add it to the dynamic fact map DynamicFacts.Put(data, shadowfact); assertFact(shadowfact); } else { IFact nsfact = createNSFact(data, dc, engine.nextFactId()); DynamicFacts.Put(data, nsfact); assertFact(nsfact); } } } }
private void ResolveDynamicFacts() { AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(type => typeof(DynamicFact).IsAssignableFrom(type) && typeof(DynamicFact) != type && !type.ContainsGenericParameters) .ToList() .ForEach(type => { var instance = (DynamicFact)Activator.CreateInstance(type); if (!DynamicFacts.ContainsKey(instance.Name)) { DynamicFacts.Add(instance.Name, instance); } }); }
/// <summary> /// Modify will call retract with the old fact, followed by updating the fact /// instance and asserting the fact. /// </summary> /// <param name="data">The data.</param> public virtual void modifyObject(Object data) { if (DynamicFacts.ContainsKey(data)) { Defclass dc = (Defclass)engine.findDefclass(data); // first we retract the fact IFact ft = (IFact)DynamicFacts.RemoveWithReturn(data); String tname = ft.Deftemplate.Name; long fid = ft.FactId; retractFact(ft); // create a new fact with the same ID ft = createFact(data, dc, tname, fid); DynamicFacts.Put(data, ft); assertFact(ft); } }
/// <summary> /// Retracts the object. /// </summary> /// <param name="data">The data.</param> public virtual void retractObject(Object data) { if (StaticFacts.ContainsKey(data)) { IFact ft = (IFact)StaticFacts.RemoveWithReturn(data); retractFact(ft); // we should probably recyle the factId before we // clean the fact ft.clear(); } else if (DynamicFacts.ContainsKey(data)) { IFact ft = (IFact)DynamicFacts.RemoveWithReturn(data); retractFact(ft); // we should probably recyle the factId before we // clean the fact ft.clear(); } }
/// <summary> Clear the deffacts from the working memory. This does not include facts /// asserted using assertObject. /// </summary> public virtual void clearFacts() { if (DynamicFacts.Count > 0) { try { IEnumerator itr = DynamicFacts.Keys.GetEnumerator(); while (itr.MoveNext()) { Object obj = itr.Current; if (obj is IFact) { retractFact((IFact)obj); } } DynamicFacts.Clear(); } catch (RetractException e) { // log.Debug(e); } } if (StaticFacts.Count > 0) { try { IEnumerator itr = StaticFacts.Keys.GetEnumerator(); while (itr.MoveNext()) { Object obj = itr.Current; if (obj is IFact) { retractFact((IFact)obj); } } StaticFacts.Clear(); } catch (RetractException e) { // log.Debug(e); } } }