Example #1
0
        /// <summary>
        /// Loads a rule base. The working memory is reset (all facts are lost).
        /// </summary>
        /// <param name="adapter">The Adapter used to read the rule base.</param>
        /// <param name="businessObjectsBinder">The business object binder that the engine must use.</param>
        /// <remarks>
        /// The adapter will be disposed at the end of the method's execution.
        /// </remarks>
        /// <see cref="org.nxbre.ie.adapters.IRuleBaseAdapter"/>
        public void LoadRuleBase(IRuleBaseAdapter adapter, IBinder businessObjectsBinder)
        {
            if (HasLogListener)
            {
                ForceDispatchLog("NxBRE Inference Engine Rule Base Loading Started, using adapter " + adapter.GetType().FullName,
                                 LogEventImpl.INFO);
            }

            using (adapter) {
                // reset the WM
                WM.PrepareInitialization();

                // sets the Binder
                Binder = businessObjectsBinder;

                // and pass it to the adapter if needed
                if (Binder != null)
                {
                    adapter.Binder = Binder;
                }

                // currently only forward chaining is supported
                direction = adapter.Direction;
                if (direction == "backward")
                {
                    throw new BREException("NxBRE does not support backward chaining");
                }
                else if (direction == String.Empty)
                {
                    if (HasLogListener)
                    {
                        ForceDispatchLog("NxBRE interprets no-direction directive as forward chaining.", LogEventImpl.WARN);
                    }
                    else if (direction == "bidirectional")
                    {
                        if (HasLogListener)
                        {
                            ForceDispatchLog("NxBRE interprets bidirectional as forward chaining.", LogEventImpl.WARN);
                        }
                        else if (direction != "forward")
                        {
                            throw new BREException("NxBRE does not support direction: " + direction);
                        }
                    }
                }

                // sets the label
                label = adapter.Label;

                // load the Equivalents and IntegrityQueries if the adapter supports it
                if (adapter is IExtendedRuleBaseAdapter)
                {
                    equivalents = ((IExtendedRuleBaseAdapter)adapter).Equivalents;
                    if (HasLogListener)
                    {
                        ForceDispatchLog("Loaded " + equivalents.Count + " Equivalents", LogEventImpl.DEBUG);
                    }

                    integrityQueries = ((IExtendedRuleBaseAdapter)adapter).IntegrityQueries;
                    foreach (Query integrityQuery in integrityQueries)
                    {
                        WM.FB.RegisterAtoms(integrityQuery.AtomGroup.AllAtoms);
                    }

                    if (HasLogListener)
                    {
                        ForceDispatchLog("Loaded " + integrityQueries.Count + " IntegrityQueries", LogEventImpl.DEBUG);
                    }
                }
                else
                {
                    equivalents      = new ArrayList();
                    integrityQueries = equivalents;
                }

                // instantiate the implication base and the query base
                ib = new ImplicationBase();
                qb = new QueryBase();

                // instantiate the related managers
                mm          = new MutexManager(IB);
                pm          = new PreconditionManager(IB);
                initialized = true;

                // load queries
                foreach (Query query in adapter.Queries)
                {
                    QB.Add(query);
                    WM.FB.RegisterAtoms(query.AtomGroup.AllAtoms);
                }
                if (HasLogListener)
                {
                    ForceDispatchLog("Loaded " + QB.Count + " Queries", LogEventImpl.DEBUG);
                }

                // load implications
                foreach (Implication implication in adapter.Implications)
                {
                    IB.Add(implication);
                    int nbRA = WM.FB.RegisterAtoms(implication.AtomGroup.AllAtoms);
                    if (HasLogListener)
                    {
                        ForceDispatchLog("Registered: " + nbRA + " body atoms", LogEventImpl.DEBUG);
                    }

                    // modifying implication must run searches based on their deduction, so must register the atom
                    if (implication.Action == ImplicationAction.Modify)
                    {
                        nbRA = WM.FB.RegisterAtoms(implication.Deduction);
                        if (HasLogListener)
                        {
                            ForceDispatchLog("Registered: " + nbRA + " head atoms", LogEventImpl.DEBUG);
                        }
                    }
                }
                if (HasLogListener)
                {
                    ForceDispatchLog("Loaded " + IB.Count + " Implications\n", LogEventImpl.DEBUG);
                }

                // load mutexes
                mm.AnalyzeImplications();
                if (HasLogListener)
                {
                    ForceDispatchLog("Loaded Mutexes\n" + mm.ToString(), LogEventImpl.DEBUG);
                }

                // load preconditions
                pm.AnalyzeImplications();
                if (HasLogListener)
                {
                    ForceDispatchLog("Loaded Preconditions\n" + pm.ToString(), LogEventImpl.DEBUG);
                }

                // load facts
                foreach (Fact fact in adapter.Facts)
                {
                    Assert(fact);
                }
                if (HasLogListener)
                {
                    ForceDispatchLog("Loaded " + WM.FB.Count + " Facts", LogEventImpl.DEBUG);
                }

                // finish the WM init
                WM.FinishInitialization();
            }             //end: using adapter
            if (HasLogListener)
            {
                ForceDispatchLog("NxBRE Inference Engine Rule Base Loading Finished", LogEventImpl.INFO);
            }
        }