Exemple #1
0
        /// <summary>
        /// Schedule all implications that are listening the facts in the fact base
        /// except if no new fact of the listening type where asserted in the previous iteration.
        /// </summary>
        /// <param name="positiveImplications">Null if it is the first iteration, else en ArrayList of positive implications of current iteration.</param>
        /// <param name="IB">The current ImplicationBase.</param>
        public void Schedule(ArrayList positiveImplications, ImplicationBase IB)
        {
            if (positiveImplications == null)
            {
                // schedule all implications
                foreach (Implication implication in IB)
                {
                    Schedule(implication);
                }
            }
            else
            {
                foreach (Implication positiveImplication in positiveImplications)
                {
                    if (positiveImplication.Action != ImplicationAction.Retract)
                    {
                        // for positive implications, schedule only the implications
                        // relevant to the newly asserted facts.
                        ArrayList listeningImplications = IB.GetListeningImplications(positiveImplication.Deduction.Type);
                        if (listeningImplications != null)
                        {
                            foreach (Implication implication in listeningImplications)
                            {
                                Schedule(implication);
                            }
                        }
                    }
                    else
                    {
                        // for negative implications, schedule only the implications
                        // that can potentially assert a fact of same type that was retracted
                        foreach (Implication implication in IB)
                        {
                            if (implication.Deduction.Type == positiveImplication.Deduction.Type)
                            {
                                Schedule(implication);
                            }
                        }
                    }

                    // schedule implications potentially pre-condition unlocked
                    foreach (Implication implication in IB.GetPreconditionChildren(positiveImplication))
                    {
                        Schedule(implication);
                    }
                }
            }
        }
		protected AbstractChainManager(string type, ImplicationBase ib) {
			this.type = type;
			this.ib = ib;
		}
		public PreconditionManager(ImplicationBase ib):base("Precondition", ib) {}
Exemple #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);
        }
Exemple #5
0
		public MutexManager(ImplicationBase ib):base("Mutex", ib) {}
Exemple #6
0
 public MutexManager(ImplicationBase ib) : base("Mutex", ib)
 {
 }
Exemple #7
0
 public PreconditionManager(ImplicationBase ib) : base("Precondition", ib)
 {
 }
Exemple #8
0
        /// <summary>
        /// Schedule all implications that are listening the facts in the fact base
        /// except if no new fact of the listening type where asserted in the previous iteration.
        /// </summary>
        /// <param name="positiveImplications">Null if it is the first iteration, else en ArrayList of positive implications of current iteration.</param>
        /// <param name="IB">The current ImplicationBase.</param>
        public void Schedule(ArrayList positiveImplications, ImplicationBase IB)
        {
            if (positiveImplications == null) {
                // schedule all implications
                foreach(Implication implication in IB) Schedule(implication);
            }
            else {
                foreach(Implication positiveImplication in positiveImplications) {
                    if (positiveImplication.Action != ImplicationAction.Retract) {
                        // for positive implications, schedule only the implications
                        // relevant to the newly asserted facts.
                        ArrayList listeningImplications = IB.GetListeningImplications(positiveImplication.Deduction.Type);
                        if (listeningImplications != null)
                            foreach(Implication implication in listeningImplications)
                                Schedule(implication);
                    }
                    else {
                        // for negative implications, schedule only the implications
                        // that can potentially assert a fact of same type that was retracted
                        foreach(Implication implication in IB)
                            if (implication.Deduction.Type == positiveImplication.Deduction.Type)
                                Schedule(implication);
                    }

                    // schedule implications potentially pre-condition unlocked
                    foreach(Implication implication in IB.GetPreconditionChildren(positiveImplication))
                        Schedule(implication);
                }
            }
        }
Exemple #9
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!");
 }
Exemple #10
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!");
 }
Exemple #11
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");
 }
Exemple #12
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();

            ArrayList preconditionChildren = ib.GetPreconditionChildren(impMedLow);
            Assert.AreEqual(1, preconditionChildren.Count, "preconditionChildren size");
            Assert.IsTrue(preconditionChildren.Contains(impMedHi), "preconditionChildren content");
        }
Exemple #13
0
 protected AbstractChainManager(string type, ImplicationBase ib)
 {
     this.type = type;
     this.ib   = ib;
 }