Beispiel #1
0
        private bool IS_TRUE(LogicalExpression expression, Dictionary <string, bool> model)
        {
            bool result = true;

            if (expression.Symbol != null)
            {
                result = IS_TRUE(expression.Symbol, model);
            }
            else
            {
                if (expression.Connective == "&")
                {
                    foreach (LogicalExpression child in expression.Children)
                    {
                        result = result && IS_TRUE(child, model);
                    }
                }
                if (expression.Connective == "\\/")
                {
                    result = false;
                    foreach (LogicalExpression child in expression.Children)
                    {
                        result = result || IS_TRUE(child, model);
                    }
                }
                if (expression.Connective == "=>")
                {
                    if (IS_TRUE(expression.Children[0], model) && !IS_TRUE(expression.Children[1], model))
                    {
                        result = false;
                    }
                    else
                    {
                        result = true;
                    }
                }
                if (expression.Connective == "<=>")
                {
                    if (IS_TRUE(expression.Children[0], model) == IS_TRUE(expression.Children[1], model))
                    {
                        result = true;
                    }
                    else
                    {
                        result = false;
                    }
                }
            }
            return(result);
        }
Beispiel #2
0
        public LogicalExpression ConvertToCNF(LogicalExpression expression)
        {
            if (expression.Symbol != null)
            {
                return(expression);
            }
            else if (expression.Connective == "<=>")
            {
                LogicalExpression newExpression = new LogicalExpression();
                newExpression.Connective = "&";
                LogicalExpression child1 = new LogicalExpression(expression.Children[0].OriginalString);
                LogicalExpression child2 = new LogicalExpression(expression.Children[1].OriginalString);

                List <LogicalExpression> children = new List <LogicalExpression>();
                children.Add(child1);
                children.Add(child2);
                newExpression.Children = children;
                Console.WriteLine("Remove Bi-Implication: ");
                newExpression.printInfo();
                return(newExpression);
            }
            else if (expression.Connective == "=>")
            {
                LogicalExpression newExpression = new LogicalExpression();
                newExpression.Connective = "\\/";
                LogicalExpression child1       = ConvertToCNF(expression.Children[0]);
                LogicalExpression child2       = ConvertToCNF(expression.Children[1]);
                LogicalExpression negateChild1 = new LogicalExpression();
                negateChild1.Connective = "~";
                negateChild1.Children.Add(child1);
                newExpression.Children.Add(negateChild1);
                newExpression.Children.Add(child2);
                Console.WriteLine("Remove Implication: ");
                newExpression.printInfo();
                return(newExpression);
            }
            else if (expression.Connective == "~")
            {
                if (expression.Children[0].Symbol != null)
                {
                    return(expression);
                }
                else
                {
                    if (expression.Children[0].Connective == "\\/")
                    {
                        LogicalExpression newExpression = new LogicalExpression();
                        newExpression.Connective = "&";
                        LogicalExpression child1 = new LogicalExpression();
                        child1.Connective = "~";
                        child1.Children.Add(expression.Children[0].Children[0]);
                        LogicalExpression child2 = new LogicalExpression();
                        child2.Connective = "~";
                        child2.Children.Add(expression.Children[0].Children[1]);
                        newExpression.Children.Add(child1);
                        newExpression.Children.Add(child2);
                        return(newExpression);
                    }
                    else if (expression.Children[0].Connective == "&")
                    {
                        LogicalExpression newExpression = new LogicalExpression();
                        newExpression.Connective = "\\/";
                        LogicalExpression child1 = new LogicalExpression();
                        child1.Connective = "~";
                        child1.Children.Add(expression.Children[0].Children[0]);
                        LogicalExpression child2 = new LogicalExpression();
                        child2.Connective = "~";
                        child2.Children.Add(expression.Children[0].Children[1]);
                        newExpression.Children.Add(child1);
                        newExpression.Children.Add(child2);
                        return(newExpression);
                    }
                }
            }
            return(expression);
        }
        public void SentenceParser(string sentence)
        {
            int  bracketCounter = 0;
            int  operatorIndex  = -1;
            bool trigger        = true;
            bool trigger2       = true;

            sentence.Trim();
            Console.WriteLine("Initial Sentence: " + sentence);
            //Console.Write("OperatorIndex: "+ operatorIndex + "\n");
            for (int i = 0; i < sentence.Length; i++)
            {
                char c = sentence.ElementAt(i);
                if (c == '(')
                {
                    //Console.WriteLine(bracketCounter);
                    bracketCounter = bracketCounter + 1;
                }
                else if (c == ')')
                {
                    bracketCounter--;
                    //Console.WriteLine(bracketCounter);
                }
                else if ((c == '<') && bracketCounter == 0)
                {
                    //Console.WriteLine("bracketCounter should be 0: " + bracketCounter);
                    operatorIndex = i;
                    trigger       = false;
                    trigger2      = false;
                }
                else if ((c == '=' && c + 1 == '>') && bracketCounter == 0 && trigger2)
                {
                    operatorIndex = i;
                    trigger       = false;
                    trigger2      = false;
                }
                else if ((c == '&') && bracketCounter == 0 && trigger && trigger2)
                {
                    operatorIndex = i;
                    trigger       = false;
                    trigger2      = false;
                }
                else if (c == '\\' && bracketCounter == 0 && trigger && trigger2)
                {
                    operatorIndex = i;
                    trigger       = false;
                    trigger2      = false;
                }
                else if (c == '~' && bracketCounter == 0 && operatorIndex < 0 && trigger && trigger2)
                {
                    operatorIndex = i;
                }
            }
            //Console.WriteLine("op: " +operatorIndex);
            if (operatorIndex < 0)
            {
                sentence = sentence.Trim();
                if (sentence.ElementAt(0) == '(' && sentence.ElementAt(sentence.Length - 1) == ')')
                {
                    SentenceParser(sentence.Substring(1, sentence.Length - 2));
                }
            }


            else
            {
                //Console.WriteLine("Entering Else");
                //Console.WriteLine(sentence);
                if (sentence.ElementAt(operatorIndex) == '<')
                {
                    Console.WriteLine("sentence: " + sentence);
                    string first  = sentence.Substring(0, operatorIndex);
                    string second = sentence.Substring(operatorIndex + 3);
                    first  = first.Trim();
                    second = second.Trim();
                    LogicalExpression child1 = new LogicalExpression(first);
                    LogicalExpression child2 = new LogicalExpression(second);
                    _children.Add(child1);
                    _children.Add(child2);
                    _connective = "<=>";
                }
                else if (sentence.ElementAt(operatorIndex) == '=')
                {
                    string first  = sentence.Substring(0, operatorIndex);
                    string second = sentence.Substring(operatorIndex + 2);
                    first  = first.Trim();
                    second = second.Trim();
                    LogicalExpression child1 = new LogicalExpression(first);
                    LogicalExpression child2 = new LogicalExpression(second);
                    _children.Add(child1);
                    _children.Add(child2);
                    _connective = "=>";
                }
                else if (sentence.ElementAt(operatorIndex) == '&')
                {
                    string first  = sentence.Substring(0, operatorIndex);
                    string second = sentence.Substring(operatorIndex + 1);
                    first  = first.Trim();
                    second = second.Trim();
                    LogicalExpression child1 = new LogicalExpression(first);
                    LogicalExpression child2 = new LogicalExpression(second);
                    _children.Add(child1);
                    _children.Add(child2);
                    _connective = "&";
                }
                else if (sentence.ElementAt(operatorIndex) == '\\')
                {
                    string first  = sentence.Substring(0, operatorIndex);
                    string second = sentence.Substring(operatorIndex + 2);
                    first  = first.Trim();
                    second = second.Trim();
                    LogicalExpression child1 = new LogicalExpression(first);
                    LogicalExpression child2 = new LogicalExpression(second);
                    _children.Add(child1);
                    _children.Add(child2);
                    _connective = "\\/";
                }
                else if (sentence.ElementAt(operatorIndex) == '~')
                {
                    string first = sentence.Substring(operatorIndex + 1);
                    first = first.Trim();
                    LogicalExpression child = new LogicalExpression(first);
                    _children.Add(child);
                    _connective = "~";
                }
                //else
                //{
                //    _symbol = sentence;
                //    _connective = null;
                //    _children = null;
                //}
            }
        }
Beispiel #4
0
        /*
         * Reads Problem File
         * @Param filename - string that gives name of file to read from
         * @Param solver - string that tells reader what type of engine is being used. Changes
         *      output if more advanced algorithms used.
         */
        public static bool ReadProblem(string filename, string solver)
        {
            List <string> text = new List <string>();

            //tries to read problem file, if it can't returns false
            try
            {
                StreamReader reader = File.OpenText(filename);
                for (int i = 0; i < 4; i++)
                {
                    text.Add(reader.ReadLine());
                }
                reader.Close();
            }
            catch
            {
                Console.WriteLine("Error: Could not read file");
                return(false);
            }
            string[] knowledge = text[1].Split(';');
            knowledge = knowledge.Take(knowledge.Count() - 1).ToArray();
            List <Clause> clauses = new List <Clause>();

            // If basic checking method
            if (solver != "GTT" || solver != "DPLL")
            {
                foreach (string s in knowledge)
                {
                    if (s.Contains("=>"))
                    {
                        List <string> premiseSymbols = new List <string>();
                        int           index          = s.IndexOf("=>");
                        string        premise        = s.Substring(0, index);
                        string        conclusion     = s.Substring(index + 2);
                        conclusion = conclusion.Trim();
                        string[] splitPremise = { "" };
                        if (premise.Contains("&"))
                        {
                            splitPremise = premise.Split('&');
                        }
                        else
                        {
                            splitPremise[0] = premise;
                        }
                        foreach (string symbol in splitPremise)
                        {
                            string trim = symbol.Trim();
                            premiseSymbols.Add(trim);
                        }
                        clauses.Add(new Clause(premiseSymbols, conclusion));
                    }
                    else
                    {
                        string conclusion = s.Trim();
                        clauses.Add(new Clause(conclusion));
                    }
                }
                _query = text[3];
                _kb    = new KnowledgeBase(clauses);


                return(true);
            }
            // if solving method more advanced clauses
            else
            {
                List <LogicalExpression> kb = new List <LogicalExpression>();
                foreach (string s in knowledge)
                {
                    LogicalExpression exp = new LogicalExpression(s);
                    //exp.printInfo();
                    kb.Add(exp);
                }

                _akb   = new AdvancedKnowledgeBase(kb);
                _query = text[3];
                return(true);
            }
        }