Example #1
0
        public void ImplicationBaseDuplicatedLabel()
        {
            ImplicationBase ib = new ImplicationBase();

            ib.Add(new Implication("impMedLow", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)));
            ib.Add(new Implication("impMedLow", 75, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)));
            Assert.Fail("Should never reach me!");
        }
Example #2
0
        public void ImplicationBase()
        {
            ImplicationBase ib  = new ImplicationBase();
            Implication     imp = new Implication("impMedLow", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3));

            ib.Add(imp);
            ib.Add(new Implication("impMedHi", 75, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)));
            Assert.AreEqual(imp, ib.Get("impMedLow"), "Get");
            Assert.IsNull(ib.Get("missing bit"), "Failed Get");
        }
Example #3
0
        public void PreconditionMissing()
        {
            ImplicationBase ib        = new ImplicationBase();
            Implication     impMedLow = new Implication("impMedLow", 25, String.Empty, "missing", imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3));

            ib.Add(impMedLow);
            ib.Add(new Implication("impMedHi", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)));
            new PreconditionManager(ib).AnalyzeImplications();
            Assert.Fail("Should never reach me!");
        }
Example #4
0
 public void MutexMissing()
 {
     Assert.Throws <BREException>(() => {
         ImplicationBase ib = new ImplicationBase();
         Implication imp    = new Implication("impMedLow", 25, "missing", String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3));
         ib.Add(imp);
         ib.Add(new Implication("impMedHi", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)));
         new MutexManager(ib).AnalyzeImplications();
         Assert.Fail("Should never reach me!");
     });
 }
Example #5
0
        public void GetPreconditionChildren()
        {
            ImplicationBase ib        = new ImplicationBase();
            Implication     impMedLow = new Implication("impMedLow", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3));

            ib.Add(impMedLow);
            Implication impMedHi = new Implication("impMedHi", 25, String.Empty, "impMedLow", imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3));

            ib.Add(impMedHi);
            new PreconditionManager(ib).AnalyzeImplications();

            IList <Implication> preconditionChildren = ib.GetPreconditionChildren(impMedLow);

            Assert.AreEqual(1, preconditionChildren.Count, "preconditionChildren size");
            Assert.IsTrue(preconditionChildren.Contains(impMedHi), "preconditionChildren content");
        }
Example #6
0
 public MutexManager(ImplicationBase ib)
     : base("Mutex", ib)
 {
 }
Example #7
0
 public PreconditionManager(ImplicationBase ib)
     : base("Precondition", ib)
 {
 }
Example #8
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 #9
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 #10
0
 protected AbstractChainManager(string type, ImplicationBase ib)
 {
     this.type = type;
     this.ib = ib;
 }
Example #11
0
 public void PreconditionMissing()
 {
     ImplicationBase ib = new ImplicationBase();
     Implication impMedLow = new Implication("impMedLow", 25, String.Empty, "missing", imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3));
     ib.Add(impMedLow);
     ib.Add(new Implication("impMedHi", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)));
     new PreconditionManager(ib).AnalyzeImplications();
     Assert.Fail("Should never reach me!");
 }
Example #12
0
 public void ImplicationBaseDuplicatedLabel()
 {
     ImplicationBase ib = new ImplicationBase();
     ib.Add(new Implication("impMedLow", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)));
     ib.Add(new Implication("impMedLow", 75, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)));
     Assert.Fail("Should never reach me!");
 }
Example #13
0
 public void ImplicationBase()
 {
     ImplicationBase ib = new ImplicationBase();
     Implication imp = new Implication("impMedLow", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3));
     ib.Add(imp);
     ib.Add(new Implication("impMedHi", 75, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)));
     Assert.AreEqual(imp, ib.Get("impMedLow"), "Get");
     Assert.IsNull(ib.Get("missing bit"), "Failed Get");
 }
Example #14
0
        public void GetPreconditionChildren()
        {
            ImplicationBase ib = new ImplicationBase();
            Implication impMedLow = new Implication("impMedLow", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3));
            ib.Add(impMedLow);
            Implication impMedHi = new Implication("impMedHi", 25, String.Empty, "impMedLow", imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3));
            ib.Add(impMedHi);
            new PreconditionManager(ib).AnalyzeImplications();

            IList<Implication> preconditionChildren = ib.GetPreconditionChildren(impMedLow);
            Assert.AreEqual(1, preconditionChildren.Count, "preconditionChildren size");
            Assert.IsTrue(preconditionChildren.Contains(impMedHi), "preconditionChildren content");
        }