Exemple #1
0
 public void QueryBaseIndexTooHigh()
 {
     QueryBase qb = new QueryBase();
     qb.Add(new Query("get spending",
                           new AtomGroup(AtomGroup.LogicalOperator.And, new Atom("spending",
                                               new Variable("customer"),
                                               new Individual("min(5000,EUR)"),
                                               new Individual("previous year")))));
     qb.Get(2);
     Assert.Fail("Should never reach me!");
 }
Exemple #2
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 #3
0
        public void QueryBase()
        {
            QueryBase qb = new QueryBase();
            Query query1 = new Query("get spending",
                                          new AtomGroup(AtomGroup.LogicalOperator.And, new Atom("spending",
                                                              new Variable("customer"),
                                                              new Individual("min(5000,EUR)"),
                                                              new Individual("previous year"))));
            qb.Add(query1);
            Assert.AreEqual(1, qb.Count, "(1) QueriesCount");

            Query getQ1 = qb.Get("get spending");
            Assert.AreEqual(query1, getQ1, "Get Query Is Equal");
            Assert.IsTrue(((Atom)query1.AtomGroup.Members[0]).IsIntersecting((Atom)getQ1.AtomGroup.Members[0]), "Get Query Is Similar");

            Assert.IsNull(qb.Get("find me if you can"), "Missing query");

            Query query2 = new Query("get earning",
                                          new AtomGroup(AtomGroup.LogicalOperator.And, new Atom("earning",
                                                              new Variable("customer"),
                                                              new Individual("min(99999,EUR)"),
                                                              new Individual("previous year"))));
            qb.Add(query2);
            Assert.AreEqual(2, qb.Count, "(2) QueriesCount");

            qb.Remove(qb.Get(0));
            Assert.AreEqual(1, qb.Count, "(3) QueriesCount");
            Query getQ2 = qb.Get(0);
            Assert.AreEqual(query2, getQ2, "(3) Get Query Is Equal");
            Assert.IsTrue(((Atom)query2.AtomGroup.Members[0]).IsIntersecting((Atom)getQ2.AtomGroup.Members[0]), "(3) Get Query Is Similar");

            qb.Add(new Query("to be killed",
                                  new AtomGroup(AtomGroup.LogicalOperator.And, new Atom("victim",
                                                      new Variable("test"),
                                                      new Individual("whatsoever")))));
            Assert.AreEqual(2, qb.Count, "(4) QueriesCount");

            qb.Remove(qb.Get("to be killed"));
            Assert.AreEqual(1, qb.Count, "(5) QueriesCount");

            qb.Remove(query2);
            Assert.AreEqual(0, qb.Count, "(6) QueriesCount");
        }