Example #1
0
        /// <summary>
        /// Saves the WorkingMemory in a rule base.
        /// </summary>
        /// <param name="adapter">The Adapter used to save the rule base.</param>
        /// <remarks>
        /// The adapter will be disposed at the end of the method's execution.
        /// </remarks>
        /// <see cref="NxBRE.InferenceEngine.IO.IRuleBaseAdapter"/>
        public void SaveRuleBase(IRuleBaseAdapter adapter)
        {
            CheckInitialized();

            if (Logger.IsInferenceEngineInformation) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Information, 0, "NxBRE Inference Engine Rule Base Saving Started, using adapter " + adapter.GetType().FullName);

            using(adapter) {
                // header
                adapter.Direction = Direction;
                adapter.Label = Label;

                //queries
                IList<Query> queries = new List<Query>();
                foreach(Query query in QB)	queries.Add(query);
                adapter.Queries = queries;

                // implications
                IList<Implication> implications = new List<Implication>();
                foreach(Implication implication in IB) implications.Add(implication);
                adapter.Implications = implications;

                // build a collection of in-memory facts
                IList<Fact> factsInWorkingMemory = new List<Fact>();
                foreach(Fact fact in WM.FB)	factsInWorkingMemory.Add(fact);

                if (adapter is IExtendedRuleBaseAdapter) {
                    // equivalents & integrity queries, assertions and retractions, if supported
                    IExtendedRuleBaseAdapter extendedAdapter = (IExtendedRuleBaseAdapter)adapter;
                    extendedAdapter.Equivalents = equivalents;
                    extendedAdapter.IntegrityQueries = integrityQueries;
                    extendedAdapter.Assertions = factsInWorkingMemory;
                    //TODO FR-1546485: write facts retractions
                }
                else {
                    // basic adapter facts output
                    adapter.Facts = factsInWorkingMemory;
                }

            }

            if (Logger.IsInferenceEngineInformation) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Information, 0, "NxBRE Inference Engine Rule Base Saving Finished");
        }
Example #2
0
        /// <summary>
        /// Saves the WorkingMemory in a rule base.
        /// </summary>
        /// <param name="adapter">The Adapter used to save the rule base.</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 SaveRuleBase(IRuleBaseAdapter adapter)
        {
            CheckInitialized();
            if (HasLogListener) ForceDispatchLog("NxBRE Inference Engine Rule Base Saving Started, using adapter " + adapter.GetType().FullName,
                                                                            LogEventImpl.INFO);

            using(adapter) {
                // header
                adapter.Direction = Direction;
                adapter.Label = Label;

                //queries
                ArrayList queries = new ArrayList();
                foreach(Query query in QB)	queries.Add(query);
                adapter.Queries = queries;

                // implications
                ArrayList implications = new ArrayList();
                foreach(Implication implication in IB) implications.Add(implication);
                adapter.Implications = implications;

                // equivalents & integrity queries if supported
                if (adapter is IExtendedRuleBaseAdapter) {
                    ((IExtendedRuleBaseAdapter)adapter).Equivalents = equivalents;
                    ((IExtendedRuleBaseAdapter)adapter).IntegrityQueries = integrityQueries;
                }

                // facts
                ArrayList facts = new ArrayList();
                foreach(Fact fact in WM.FB)	facts.Add(fact);
                adapter.Facts = facts;
            }

            if (HasLogListener) ForceDispatchLog("NxBRE Inference Engine Rule Base Saving Finished", LogEventImpl.INFO);
        }
Example #3
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>
        /// <param name="processPerformatives">Immediatly process the performative actions (assert, retract) found in the rule base.</param>
        /// <remarks>
        /// The adapter will be disposed at the end of the method's execution.
        /// </remarks>
        /// <see cref="NxBRE.InferenceEngine.IO.IRuleBaseAdapter"/>
        public void LoadRuleBase(IRuleBaseAdapter adapter, IBinder businessObjectsBinder, bool processPerformatives)
        {
            if (Logger.IsInferenceEngineInformation) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Information, 0, "NxBRE Inference Engine Rule Base Loading Started, using adapter " + adapter.GetType().FullName);

            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 (Logger.IsInferenceEngineWarning) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Warning, 0, "NxBRE interprets no-direction directive as forward chaining.");
                }
                else if (direction == "bidirectional") {
                    if (Logger.IsInferenceEngineWarning) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Warning, 0, "NxBRE interprets bidirectional as forward chaining.");
                }
                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
                IExtendedRuleBaseAdapter extendedAdapter = null;
                if (adapter is IExtendedRuleBaseAdapter) {
                    extendedAdapter = (IExtendedRuleBaseAdapter)adapter;

                    equivalents = extendedAdapter.Equivalents;
                    if (Logger.IsInferenceEngineVerbose) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, "Loaded " + equivalents.Count + " Equivalents");

                    integrityQueries = extendedAdapter.IntegrityQueries;
                    if (Logger.IsInferenceEngineVerbose) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, "Loaded " + integrityQueries.Count + " IntegrityQueries");
                }
                else {
                    equivalents = new List<Equivalent>(0);
                    integrityQueries = new List<Query>(0);
                }

                // instantiate the different storage
                ib = new ImplicationBase();
                qb = new QueryBase();

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

                // load queries
                foreach(Query query in adapter.Queries) QB.Add(query);
                if (Logger.IsInferenceEngineVerbose) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, "Loaded " + QB.Count + " Queries");

                // load implications
                foreach(Implication implication in adapter.Implications) IB.Add(implication);
                if (Logger.IsInferenceEngineVerbose) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, "Loaded " + IB.Count + " Implications\n");

                // load mutexes
                mm.AnalyzeImplications();
                if (Logger.IsInferenceEngineVerbose) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, "Loaded Mutexes\n" + mm.ToString());

                // load preconditions
                pm.AnalyzeImplications();
                if (Logger.IsInferenceEngineVerbose) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, "Loaded Preconditions\n" + pm.ToString());

                initialized = true;

                // load facts assertions
                performativeAssertions = new List<Fact>((extendedAdapter!=null)?extendedAdapter.Assertions:adapter.Facts);
                //TODO FR-1546485: load facts retractions
                if (processPerformatives) ProcessPerfomatives();
                if (Logger.IsInferenceEngineVerbose) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, "Loaded " + WM.FB.Count + " Facts");

                // finish the WM init
                WM.FinishInitialization();

            } //end: using adapter

            if (Logger.IsInferenceEngineInformation) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Information, 0, "NxBRE Inference Engine Rule Base Loading Finished");
        }
Example #4
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);
        }
Example #5
0
        /// <summary>
        /// Saves the WorkingMemory in a rule base.
        /// </summary>
        /// <param name="adapter">The Adapter used to save the rule base.</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 SaveRuleBase(IRuleBaseAdapter adapter)
        {
            CheckInitialized();
            if (HasLogListener)
            {
                ForceDispatchLog("NxBRE Inference Engine Rule Base Saving Started, using adapter " + adapter.GetType().FullName,
                                 LogEventImpl.INFO);
            }

            using (adapter) {
                // header
                adapter.Direction = Direction;
                adapter.Label     = Label;

                //queries
                ArrayList queries = new ArrayList();
                foreach (Query query in QB)
                {
                    queries.Add(query);
                }
                adapter.Queries = queries;

                // implications
                ArrayList implications = new ArrayList();
                foreach (Implication implication in IB)
                {
                    implications.Add(implication);
                }
                adapter.Implications = implications;

                // equivalents & integrity queries if supported
                if (adapter is IExtendedRuleBaseAdapter)
                {
                    ((IExtendedRuleBaseAdapter)adapter).Equivalents      = equivalents;
                    ((IExtendedRuleBaseAdapter)adapter).IntegrityQueries = integrityQueries;
                }

                // facts
                ArrayList facts = new ArrayList();
                foreach (Fact fact in WM.FB)
                {
                    facts.Add(fact);
                }
                adapter.Facts = facts;
            }

            if (HasLogListener)
            {
                ForceDispatchLog("NxBRE Inference Engine Rule Base Saving Finished", LogEventImpl.INFO);
            }
        }
Example #6
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);
            }
        }
Example #7
0
		/// <summary>
		/// Save facts of the current working memory. Current implications, facts and queries
		/// remain unchanged.
		/// </summary>
		/// <remarks>
		/// The adapter will be disposed at the end of the method's execution.
		/// </remarks>
		/// <param name="adapter">The Adapter used to save the fact base.</param>
		public void SaveFacts(IRuleBaseAdapter adapter) {
			CheckInitialized();
			if (HasLogListener) ForceDispatchLog("NxBRE Inference Engine Facts Saving Started, using adapter " + adapter.GetType().FullName,
			            													LogEventImpl.INFO);
			using(adapter) {
				// header
				adapter.Direction = Direction;
				adapter.Label = Label;
		
				// facts
				ArrayList facts = new ArrayList();
				foreach(Fact fact in WM.FB)	facts.Add(fact);
				adapter.Facts = facts;
			} //end: using adapter
			
			if (HasLogListener) ForceDispatchLog("NxBRE Inference Engine Facts Saving Finished", LogEventImpl.INFO);
		}
Example #8
0
		/// <summary>
		/// Load facts in the current working memory. Current implications, facts and queries
		/// remain unchanged.
		/// </summary>
		/// <remarks>
		/// The adapter will be disposed at the end of the method's execution.
		/// </remarks>
		/// <param name="adapter">The Adapter used to read the fact base.</param>
		public void LoadFacts(IRuleBaseAdapter adapter) {
			CheckInitialized();
			if (HasLogListener) ForceDispatchLog("NxBRE Inference Engine Facts Loading Started, using adapter " + adapter.GetType().FullName,
			            													LogEventImpl.INFO);
			
			using(adapter) {
				// sets the eventual Binder
				if (Binder != null) adapter.Binder = Binder;

				// load facts
				int initialFactsCount = WM.FB.Count;
				foreach(Fact fact in adapter.Facts) Assert(fact);
				if (HasLogListener) ForceDispatchLog("Added " + (WM.FB.Count - initialFactsCount) + " new Facts", LogEventImpl.DEBUG);
				
			} //end: using adapter
			if (HasLogListener) ForceDispatchLog("NxBRE Inference Engine Facts Loading Finished", LogEventImpl.INFO);
		}