Example #1
0
 //public bool Alpha(bool implication, string alpha)
 //{
 //    if(alpha == true)
 //}
 //check each sentence with model
 public bool EachKBTrue(Sentences KB, TTModel model)
 {
     if (KB.symbol != null)
     {
         if (!EachKBTrueInModel(KB.symbol, model))
         {
             return(false);
         }
     }
     else if (KB.connective == "=>" && KB.children[0].symbol != null)
     {
         // Console.WriteLine(KB.children[0].symbol);
         //Console.WriteLine(KB.children[1].symbol);
         if (!EachKBTrueInModel(KB.children[1].symbol, model))
         {
             return(!EachKBTrueInModel(KB.children[1].symbol, model));
         }
     }
     else if (KB.connective == "=>" && KB.children[0].symbol == null)
     {
         //Console.WriteLine("{0}{1}{2}{3}{4}", KB.children[0].children[0].symbol, KB.children[0].connective,
         //                  KB.children[0].children[1].symbol, KB.connective, KB.children[1].symbol);
         if (!EachKBTrueInModel(KB.children[1].symbol, model))
         {
             return((!EachKBTrueInModel(KB.children[0].children[0].symbol, model)) && (!EachKBTrueInModel(KB.children[0].children[1].symbol, model)));
         }
     }
     return(true);
 }
        public void GetKnowledgeBase(string clausesString)
        {
            string[] clauses = clausesString.Split(';');


            foreach (string s in clauses)
            {
                if (AtomicSentence(s))
                {
                    KB.Add(new Sentences(s));
                }
                else if (!s.Contains("&"))
                {
                    string tempKB = s;
                    tempKB = tempKB.Replace("=>", ";");
                    string[] tempKBChildren = tempKB.Split(';');
                    KB.Add(new Sentences("=>", tempKBChildren));
                }
                else
                {
                    string tempKB = s;
                    tempKB = tempKB.Replace("=>", ";");
                    string[]    tempKBChildrenA   = tempKB.Split(';');
                    string[]    tempKBChildrenB   = tempKBChildrenA[0].Split('&');
                    Sentences   tempSentence      = new Sentences("&", tempKBChildrenB);
                    Sentences[] tempSentenceArray = { tempSentence, new Sentences(tempKBChildrenA[1]) };
                    KB.Add(new Sentences("=>", tempSentenceArray));
                }
            }
        }
        public bool TTCheckAll(List <Sentences> KB, Sentences alpha, Queue <string> symbolsQ, Dictionary <string, bool> model)
        {
            if (symbolsQ.Count == 0)
            {
                if (KBTrue(KB, model))
                {
                    if (EachSentence(alpha, model))
                    {
                        a++;
                    }
                    else
                    {
                        b++;
                    }
                }
            }
            else
            {
                string topSymbol = symbolsQ.Dequeue();

                return((TTCheckAll(KB, alpha, new Queue <string>(symbolsQ), AddToDictionary(model, topSymbol, true)) &&
                        (TTCheckAll(KB, alpha, new Queue <string>(symbolsQ), AddToDictionary(model, topSymbol, false)))));
            }

            return(true);
        }
Example #4
0
        public void PlFcEntails(List <Sentences> kb, Sentences alpha)
        {
            Dictionary <Sentences, int> Clauses = new Dictionary <Sentences, int>(clauses);
            Queue <string> printResult          = new Queue <string>();
            bool           end = false;

            while (agenda.Count != 0 && !end)
            {
                string currentAgenda = agenda.Dequeue();
                printResult.Enqueue(currentAgenda);
                if (currentAgenda == alpha.symbol)
                {
                    end = true;  break;
                }
                if (inferred.ContainsKey(currentAgenda))
                {
                    inferred[currentAgenda] = true;
                }


                // if currentAgends (symbol) is present in PREMISE of sentence, -1 for its int
                foreach (KeyValuePair <Sentences, int> s in Clauses)
                {
                    PremiseContainAgenda(s.Key, currentAgenda);
                }

                // when int is 0 change false to true in inferred
                foreach (KeyValuePair <Sentences, int> s in clauses)
                {
                    if (s.Value <= 0 && s.Key.symbol == null)
                    {
                        if (!agenda.Contains(s.Key.children[1].symbol) &&
                            !printResult.Contains(s.Key.children[1].symbol))
                        {
                            agenda.Enqueue(s.Key.children[1].symbol);
                        }
                    }
                }
            }

            //write answer - if found write path - else not found
            if (printResult.Contains(alpha.symbol))
            {
                Console.Write("YES:");
                foreach (string s in printResult)
                {
                    Console.Write(" {0}", s);
                }
            }
            else
            {
                Console.WriteLine("NO");
            }
        }
Example #5
0
 // returns each sentence KB in string
 public string GetKB(Sentences s)
 {
     if (s.symbol != null)
     {
         return(s.symbol);
     }
     else
     {
         return(GetKB(s.children[0]) + s.connective + GetKB(s.children[1]));
     }
 }
 public bool Antecedent(Sentences s, Dictionary <string, bool> model)
 {
     // checks single antecedents like p1, a, b
     if (s.symbol != null)
     {
         return(!model[s.symbol]);
     }
     else
     {
         // // checks && sentences like a&b
         return(!(model[s.children[0].symbol] && model[s.children[1].symbol]));
     }
 }
Example #7
0
        // returns 0 for Atanomus sentence like p
        // returns 1 for sentence like p => e
        // returns 2 for sentence like p&g => e
        public int GetSentenceCount(Sentences sent)
        {
            int temp = 0;

            if (sent.symbol != null)
            {
                //temp++;
                return(temp);
            }
            else
            {
                temp++;
                foreach (Sentences s in sent.children)
                {
                    temp += GetSentenceCount(s);
                }
                return(temp);
            }
        }
        public bool BC(List <Sentences> kb, Sentences alpha, Stack <string> printResult)
        {
            if (inferredTrue.Contains(alpha.symbol))
            {
                return(true);
            }

            foreach (Sentences s in kb)
            {
                if (s.symbol != null)
                {
                    if (s.symbol == alpha.symbol)
                    {
                        return(true);
                    }
                }
                else if (s.children[1].symbol == alpha.symbol)
                {
                    if (s.children[0].symbol != null)
                    {
                        if (!printResult.Contains(s.children[0].symbol))
                        {
                            printResult.Push(s.children[0].symbol);
                        }
                        return(BC(kb, s.children[0], printResult));
                    }
                    else
                    {
                        if (!printResult.Contains(s.children[0].children[0].symbol))
                        {
                            printResult.Push(s.children[0].children[0].symbol);
                        }
                        if (!printResult.Contains(s.children[0].children[1].symbol))
                        {
                            printResult.Push(s.children[0].children[1].symbol);
                        }
                        return(BC(kb, s.children[0].children[0], printResult) && BC(kb, s.children[0].children[1], printResult));
                    }
                }
            }
            return(false);
        }
Example #9
0
 // if currentAgends (symbol) is present in PREMISE of sentence, -1 for its int
 public void PremiseContainAgenda(Sentences s, string currentAgenda)
 {
     if (s.symbol == null)
     {
         if (s.children[0].symbol != null)
         {
             if (s.children[0].symbol == currentAgenda)
             {
                 clauses[s]--;
             }
         }
         else
         {
             if (s.children[0].children[0].symbol == currentAgenda || s.children[0].children[1].symbol == currentAgenda)
             {
                 clauses[s]--;
             }
         }
     }
 }
Example #10
0
 public bool EachSentence(Sentences s, Dictionary <string, bool> model)
 {
     if (s.symbol != null)
     {
         // checks single sentences like p1, a, b
         if (model.ContainsKey(s.symbol))
         {
             return(model[s.symbol]);
         }
     }
     else
     {
         // if consequent is false it needs to checks antecedent
         if (!model[s.children[1].symbol])
         {
             return(Antecedent(s.children[0], model));
         }
         return(true);
     }
     return(false);
 }
Example #11
0
        public bool KBSetUp(Sentences KB, TTModel model, string alpha)
        {
            bool childZero;
            bool childOne     = true;
            bool alphaBool    = true;
            bool andChildOne  = false;
            bool andChildZero = false;

            if (KB.connective == "=>")
            {
                if (KB.children[0].connective == null)
                {
                    foreach (KeyValuePair <string, bool> s in model.ModelDictionary)
                    {
                        if (KB.children[0].symbol == s.Key)
                        {
                            childZero = s.Value;
                        }
                        if (alpha == s.Key)
                        {
                            alphaBool = s.Value;
                            //Console.WriteLine(s.Key);
                        }
                    }
                }
                else
                {
                    foreach (KeyValuePair <string, bool> s in model.ModelDictionary)
                    {
                        if (KB.children[0].children[0].symbol == s.Key)
                        {
                            andChildZero = s.Value;
                        }

                        if (KB.children[0].children[1].symbol == s.Key)
                        {
                            andChildOne = s.Value;
                        }
                    }
                }
                if (KB.children[1].connective == null)
                {
                    foreach (KeyValuePair <string, bool> s in model.ModelDictionary)
                    {
                        if (KB.children[1].symbol == s.Key)
                        {
                            childOne = s.Value;
                            //Console.WriteLine(s.Key);
                        }
                    }
                }
                if (KB.connective == null)
                {
                    if (KB.children == null)
                    {
                        foreach (KeyValuePair <string, bool> s in model.ModelDictionary)
                        {
                            if (KB.symbol == s.Key)
                            {
                                childOne = s.Value;
                            }
                        }/// ????????????????????????????????
                    }
                }
            }
            childZero = andLogic(andChildOne, andChildZero);

            if (Implication(childOne, childZero) && alphaBool)
            {
                Console.WriteLine("TRUE");
            }

            return(true);
        }