Esempio n. 1
0
        public void AreIntersecting()
        {
            ArrayList a1 = new ArrayList();

            a1.Add("a");
            a1.Add(1);
            a1.Add(3.14);
            ArrayList a2 = new ArrayList();

            a2.Add("b");
            a2.Add(1);
            a2.Add(6.28);
            ArrayList a3 = new ArrayList();

            a3.Add("c");
            a3.Add(2);
            a3.Add(9.42);

            Assert.IsTrue(Misc.AreIntersecting(a1, a2), "Intersecting");
            Assert.IsFalse(Misc.AreIntersecting(a1, a3), "Not-Intersecting");
        }
Esempio n. 2
0
        private void InferUntilNoNewFact(ArrayList positiveImplications)
        {
            long iniTime;

            if (HasLogListener)
            {
                ForceDispatchLog("(Starting) " +
                                 ((WM.Type == WorkingMemoryTypes.Global)?"Global":"Isolated") +
                                 "Working Memory contains: " +
                                 WM.FB.Count + " facts, " +
                                 IB.Count + " implications, " +
                                 QB.Count + " queries.",
                                 LogEventImpl.DEBUG);
            }

            iniTime = DateTime.Now.Ticks;
            ArrayList iterationPositiveImplications = null;
            bool      iterate = true;
            Agenda    agenda  = new Agenda();

            // Loop as long as there are new deduction made
            while (iterate)
            {
                if (iteration >= IterationLimit)
                {
                    throw new BREException("Maximum limit of iterations reached: " + IterationLimit);
                }

                iterate = false;
                iteration++;

                // Schedule all implications matching the existing facts
                agenda.Schedule(iterationPositiveImplications, IB);
                agenda.PrepareExecution();

                if (HasLogListener)
                {
                    ForceDispatchLog("Iteration #" +
                                     iteration +
                                     ": " +
                                     agenda.Count + " implications in agenda, with " +
                                     positiveImplications.Count + " positive.",
                                     LogEventImpl.DEBUG);
                }

                iterationPositiveImplications = new ArrayList();

                while (agenda.HasMoreToExecute)
                {
                    Implication firedImplication = agenda.NextToExecute;

                    // check if this implication is worth processing:
                    // first: see if it is not mutexed by a previously positive implication
                    if ((firedImplication.MutexChain != null) &&
                        (!positiveImplications.Contains(firedImplication)) &&
                        (Misc.AreIntersecting(firedImplication.MutexChain, positiveImplications)))
                    {
                        if (HasLogListener)
                        {
                            ForceDispatchLog("Mutexed: " + firedImplication.Label, LogEventImpl.DEBUG);
                        }
                        firedImplication = null;
                    }

                    // second: see if it is not pre-condition disabled by a previously negative implication
                    if ((firedImplication != null) &&
                        (firedImplication.PreconditionImplication != null) &&
                        (!positiveImplications.Contains(firedImplication.PreconditionImplication)))
                    {
                        if (HasLogListener)
                        {
                            ForceDispatchLog("Negative Precondition: " + firedImplication.Label, LogEventImpl.DEBUG);
                        }
                        firedImplication = null;
                    }

                    if (firedImplication != null)
                    {
                        WM.FB.ModifiedFlag = false;

                        int resultsCount = RunImplication(firedImplication);

                        if (HasLogListener)
                        {
                            ForceDispatchLog("Fired Implication: " +
                                             firedImplication.ToString() +
                                             " returned: " +
                                             resultsCount,
                                             LogEventImpl.DEBUG);
                        }

                        // if processor has been positive, i.e an implication deducted at least one fact
                        if (resultsCount > 0)
                        {
                            positiveImplications.Add(firedImplication);
                        }

                        // if the fact base has been anyhow modified
                        if (WM.FB.ModifiedFlag)
                        {
                            iterationPositiveImplications.Add(firedImplication);
                        }
                    }
                }                 // while agenda has more

                // if at least one is accepted ask for another processing iteration
                iterate = (iterationPositiveImplications.Count > 0);
            }             // while iterate

            // perform integrity checks
            foreach (Query integrityQuery in IntegrityQueries)
            {
                QueryResultSet qrs = RunQuery(integrityQuery);
                if (qrs.Count == 0)
                {
                    throw new IntegrityException("Rulebase integrity violated: " + integrityQuery.Label);
                }
            }

            if (HasLogListener)
            {
                ForceDispatchLog("NxBRE Inference Engine Execution Time: " +
                                 (long)(DateTime.Now.Ticks - iniTime) / 10000 +
                                 " milliseconds", LogEventImpl.INFO);
            }

            if (HasLogListener)
            {
                ForceDispatchLog("(Finishing) " +
                                 ((WM.Type == WorkingMemoryTypes.Global)?"Global":"Isolated") +
                                 "Working Memory contains: " +
                                 WM.FB.Count + " facts, " +
                                 IB.Count + " implications, " +
                                 QB.Count + " queries.",
                                 LogEventImpl.DEBUG);
            }
        }