Beispiel #1
0
        /// <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);
                    }
                }
            }
        }