Exemple #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);
        }
Exemple #2
0
        public bool BCentails()
        {
            while (Agenda.Count > 0)
            {
                // take the first item and process it
                string q = Agenda[Agenda.Count - 1];
                Agenda.RemoveAt(Agenda.Count - 1);

                if (q != Ask)
                {
                    if (!Entailed.Contains(q))
                    {
                        Entailed.Insert(0, q);
                    }
                }

                if (!(Facts.Contains(q)))
                {
                    List <string> prem = new List <string>();
                    // for each of the clauses...
                    for (int i = 0; i < Clauses.Count; i++)
                    {
                        // .... that contain p in its premise
                        if (ClauseContains(Clauses[i], q, 1))
                        {
                            List <string> temp = GetPremises(Clauses[i]);
                            for (int j = 0; j < temp.Count; j++)
                            {
                                prem.Add(temp[j]);
                            }
                        }
                    }
                    if (prem.Count == 0)
                    {
                        return(false);
                    }
                    else
                    {
                        for (int i = 0; i < prem.Count; i++)
                        {
                            if (!Entailed.Contains(prem[i]))
                            {
                                Agenda.Add(prem[i]);
                            }
                        }
                    }
                }
            }
            // while end
            return(true);
        }
        // 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);
        }