Beispiel #1
0
        // Start the Front-Chain process
        public override bool Algorithm()
        {
            while (Agenda.Count != 0)
            {
                // Pop the first element at agenda and temporarily store it at p
                string p = Agenda[0];
                Entailed.Add(p);
                Agenda.RemoveAt(0);

                for (int i = 0; i < Clauses.Count; i++)
                {
                    if (Contains(Clauses[i], p))
                    {
                        //Decrement Count at i and checks if zero
                        if ((--Count[i]) == 0)
                        {
                            // Get the Conclusion
                            string head = Regex.Split(Clauses[i], "=>")[1];
                            // Check if reach ask
                            if (head.Equals(Ask))
                            {
                                return(true);
                            }
                            Agenda.Add(head);
                        }
                    }
                }
            }
            // Cannot be entailed
            return(false);
        }
        // FC algorithm
        public bool FCentails()
        {
            // loop through while there are unprocessed facts
            while (Agenda.Count > 0)
            {
                // take the first item and process it
                string p = Agenda[0];
                Agenda.RemoveAt(0);
                // add to entailed :: List of symbols already processed
                Entailed.Add(p);
                if (p == Ask)
                {
                    return(true);
                }

                // for each of the clauses...
                for (int i = 0; i < Clauses.Count; i++)
                {
                    // .... that contain p in its premise
                    if (ClauseContains(Clauses[i], p, 0))
                    {
                        // reduce count : unknown elements in each premise
                        int j = Count[i];
                        Count[i] = --j;
                        // all the elements in the premise are now known
                        if (Count[i] == 0)
                        {
                            string[] separatingChars = { "=>" };
                            string   head            = Clauses[i].Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries)[1];
                            if (head.Equals(Ask))
                            {
                                return(true);
                            }
                            if (!Entailed.Contains(head))
                            {
                                Agenda.Add(head);
                            }
                        }
                    }
                }
            }
            // if we arrive here then ask cannot entailed
            return(false);
        }