Example #1
0
        private static ParseTreeFOL_Q ParseQuantifiers(string quantifierString, ParseTreeFOL_O expression)
        {
            quantifierString = FillInQuantifiers(quantifierString);
            string[] quantifierStrings = quantifierString.Split(':');
            quantifierStrings = quantifierStrings.Reverse().ToArray();
            ParseTreeFOL_Q output = new ParseTreeFOL_Q(null, null, expression);

            if (quantifierStrings[0] != "")
            {
                foreach (string str in quantifierStrings)
                {
                    if (str.First().Equals('E'))
                    {
                        output = new ParseTreeFOL_Q(FOLQuantifier.E, str.Substring(1), output);
                    }
                    else if (str.First().Equals('A'))
                    {
                        output = new ParseTreeFOL_Q(FOLQuantifier.A, str.Substring(1), output);
                    }
                    else
                    {
                        throw (new Exception("Failed to parse quantifiers."));
                    }
                }
            }
            return(output);
        }
Example #2
0
 private static List <List <Assignment> > EvaluateLogicalExpression(ParseTreeFOL_O expression, string[] subwords, string body)
 {
     if (expression.getTerm() != null)
     {
         Term     term     = expression.getTerm();
         string[] freeVars = FOLSearcher.GetFreeVars(term);
         return(TopDownFOLEvaluator.evaluateParsedExpression(term, subwords, freeVars, body));
     }
     else
     {
         List <List <Assignment> > leftTable  = EvaluateLogicalExpression(expression.getLeftChild(), subwords, body);
         List <List <Assignment> > rightTable = EvaluateLogicalExpression(expression.getRightChild(), subwords, body);
         rightTable = FillInSolutions(rightTable, leftTable, subwords);
         leftTable  = FillInSolutions(leftTable, rightTable, subwords);
         if (expression.getFolOperator() == FOLOperator.Disjunction)
         {
             leftTable.AddRange(rightTable.Where(r => !leftTable.Any(l => CompareSolutions(l, r))));
         }
         else
         {
             leftTable.RemoveAll(l => !rightTable.Any(r => CompareSolutions(l, r)));
         }
         return(leftTable);
     }
 }
 public static string[] GetFreeVars(ParseTreeFOL_O expression)
 {
     if (expression.getTerm() != null)
     {
         return(GetFreeVars(expression.getTerm()));
     }
     return(GetFreeVars(expression.getLeftChild()).ToList().Concat(GetFreeVars(expression.getRightChild()).ToList()).Distinct().ToArray());
 }
        private static bool evaluateLogicalExpression(ParseTreeFOL_O expressionTree, List <Assignment> varVals)
        {
            FOLOperator?op = expressionTree.getFolOperator();

            if (expressionTree.getTerm() != null)
            {
                return(TermEvaluator.evaluateTerm(expressionTree.getTerm(), varVals));
            }
            else
            {
                if (op == FOLOperator.Conjunction)
                {
                    return(evaluateLogicalExpression(expressionTree.getLeftChild(), varVals) &&
                           evaluateLogicalExpression(expressionTree.getRightChild(), varVals));
                }
                else if (op == FOLOperator.Disjunction)
                {
                    return(evaluateLogicalExpression(expressionTree.getLeftChild(), varVals) ||
                           evaluateLogicalExpression(expressionTree.getRightChild(), varVals));
                }
            }
            throw (new Exception("Bad expression parsing"));
        }
 public ParseTreeFOL_O(FOLOperator folOperator, ParseTreeFOL_O leftChild, ParseTreeFOL_O rightChild)
 {
     this.folOperator = folOperator;
     this.leftChild   = leftChild;
     this.rightChild  = rightChild;
 }
Example #6
0
 public FOLExpression(ParseTreeFOL_Q quantifiers, ParseTreeFOL_O expression)
 {
     this.FOQuantifiers = quantifiers;
     this.PLExpression  = expression;
 }
Example #7
0
 public ParseTreeFOL_Q(FOLQuantifier?folquantifier, string quantifiedVar, ParseTreeFOL_O expression)
 {
     this.folQuantifier = folquantifier;
     this.quantifiedVar = quantifiedVar;
     this.expression    = expression;
 }
Example #8
0
        private static ParseTreeFOL_O ParseExpression(List <String> lexemes, string material)
        {
            if (lexemes.Count == 1)
            {
                return(new ParseTreeFOL_O(ParseTerm(lexemes[0], material)));
            }
            int i            = 0;
            int bracketDepth = 0;

            do
            {
                switch (lexemes[i])
                {
                case "(":
                    bracketDepth++;
                    break;

                case ")":
                    bracketDepth--;
                    break;

                case "+":
                    if (bracketDepth == 0)
                    {
                        ParseTreeFOL_O left  = ParseExpression(lexemes.Take(i).ToList(), material);
                        ParseTreeFOL_O right = ParseExpression(lexemes.Skip(i + 1).ToList(), material);
                        return(new ParseTreeFOL_O(FOLOperator.Disjunction, left, right));
                    }
                    break;
                }
                i++;
            } while (i < lexemes.Count);
            i            = 0;
            bracketDepth = 0;
            do
            {
                switch (lexemes[i])
                {
                case "(":
                    bracketDepth++;
                    break;

                case ")":
                    bracketDepth--;
                    break;

                case "*":
                    if (bracketDepth == 0)
                    {
                        ParseTreeFOL_O left  = ParseExpression(lexemes.Take(i).ToList(), material);
                        ParseTreeFOL_O right = ParseExpression(lexemes.Skip(i + 1).ToList(), material);
                        return(new ParseTreeFOL_O(FOLOperator.Conjunction, left, right));
                    }
                    break;
                }
                i++;
            } while (i < lexemes.Count);

            if (lexemes.First() == "(" && lexemes.Last() == ")")
            {
                return(ParseExpression(lexemes.Skip(1).Take(lexemes.Count - 2).ToList(), material));
            }
            return(null);
        }
 public static List <List <Assignment> > evaluateParsedExpression(ParseTreeFOL_O expression, string[] subwords, string[] freeVars, string body)
 {
     return(evaluateParsedExpression(new ParseTreeFOL_Q(null, null, expression), subwords, freeVars, body));
 }