public static bool evaluateQuantifiedExpression(ParseTreeFOL_Q quantifiedExpression, string[] subwords, List <Assignment> varVals)
        {
            if (quantifiedExpression.getExpression() != null)
            {
                return(evaluateLogicalExpression(quantifiedExpression.getExpression(), varVals));
            }
            bool res      = false;
            bool endState = false;

            //handles the difference between "There Exists" and "For All" quantifiers
            if (quantifiedExpression.getFOLQuantifier() == FOLQuantifier.E)
            {
                endState = true;
            }
            else
            {
                res = true;
            }

            //until we've tried all subwords, or the result proves the quantifier true or false
            for (int j = 0; j < subwords.Length && res != endState; j++)
            {
                List <Assignment> newVarVals = new List <Assignment>();
                newVarVals.AddRange(varVals);
                Assignment varVal = new Assignment(quantifiedExpression.getQuantifiedVar(), subwords[j]);
                newVarVals.Add(varVal);

                res = evaluateQuantifiedExpression(quantifiedExpression.getChild(), subwords, newVarVals);
            }
            return(res);
        }
Exemple #2
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);
        }
Exemple #3
0
        public static List <List <Assignment> > EvaluateParsedExpression(ParseTreeFOL_Q quantifiedExpression, string[] subwords, string[] freeVars, string body)
        {
            List <List <Assignment> > res     = EvaluateQuantifiedExpression(quantifiedExpression, subwords, body);
            List <string>             varList = freeVars.ToList();

            varList.RemoveAll(var => res.Any(sol => sol.Any(a => a.GetVariableName() == var)));
            res = FillInSolutions(res, varList.ToArray(), subwords);
            return(res);
        }
        public static string[] GetFreeVars(ParseTreeFOL_Q query)
        {
            if (query.getExpression() != null)
            {
                return(GetFreeVars(query.getExpression()));
            }
            List <string> vars = GetFreeVars(query.getChild()).ToList();

            vars.Remove(query.getQuantifiedVar());
            return(vars.ToArray());
        }
Exemple #5
0
        public static List <List <Assignment> > EvaluateQuantifiedExpression(ParseTreeFOL_Q quantifiedExpression, string[] subwords, string body)
        {
            List <List <Assignment> > solutions;

            if (quantifiedExpression.getExpression() == null)
            {
                solutions = EvaluateQuantifiedExpression(quantifiedExpression.getChild(), subwords, body);
                FOLQuantifier quantifier   = (FOLQuantifier)quantifiedExpression.getFOLQuantifier();
                string        variableName = quantifiedExpression.getQuantifiedVar();
                if (quantifier == FOLQuantifier.E)
                {
                    if (!solutions.Any(sol => sol.Any(assignment => assignment.GetVariableName() == variableName)))
                    {
                        solutions.RemoveAll(a => true);
                    }
                    else
                    {
                        solutions = FOLSearcher.removeVariable(solutions, variableName);
                    }
                }
                else
                {
                    List <List <Assignment> > newSols        = new List <List <Assignment> >();
                    List <List <Assignment> > solsWithoutVar = FOLSearcher.removeVariable(
                        solutions.ConvertAll(sol => new List <Assignment>(sol)),
                        variableName);

                    foreach (List <Assignment> solWithoutVar in solsWithoutVar)
                    {
                        bool include = true;
                        foreach (string word in subwords)
                        {
                            List <Assignment> sol = new List <Assignment>(solWithoutVar);
                            sol.Add(new Assignment(variableName, word));
                            if (!solutions.Any(solution => CompareSolutions(sol, solution)))
                            {
                                include = false;
                            }
                        }
                        if (include)
                        {
                            newSols.Add(solWithoutVar);
                        }
                    }
                    solutions = newSols;
                }
            }
            else
            {
                solutions = EvaluateLogicalExpression(quantifiedExpression.getExpression(), subwords, body);
            }
            return(solutions);
        }
        public static List <List <Assignment> > Search(string body, string query, bool topDownSearch)
        {
            List <string> subwords       = getAllSubwords(body);
            string        folExpression  = query.Substring(query.IndexOf(":=") + 2);
            string        freeVarsString = query.Substring(query.IndexOf('(') + 1).Substring(0, query.IndexOf(')') - 2);

            string[] freeVars = freeVarsString.Split(',');

            ParseTreeFOL_Q parsedExpression = FOLParser.Parse(folExpression, body);

            List <List <Assignment> > res;

            if (topDownSearch)
            {
                res = TopDownFOLEvaluator.evaluateParsedExpression(parsedExpression, subwords.ToArray(), freeVars, body);
            }
            else
            {
                res = BottomUpFOLEvaluator.EvaluateParsedExpression(parsedExpression, subwords.ToArray(), freeVars, body);
            }
            return(res);
        }
        public static List <List <Assignment> > evaluateParsedExpression(ParseTreeFOL_Q parsedExpression, string[] subwords, string[] freeVars, string body)
        {
            int[] freeVarValIndices = new int[freeVars.Length];
            for (int i = 0; i < freeVarValIndices.Length; i++)
            {
                freeVarValIndices[i] = 0;
            }
            List <List <Assignment> > successfulValueCombinations = new List <List <Assignment> >();

            do
            {
                List <Assignment> varVals = new List <Assignment> {
                    new Assignment("S", body)
                };
                for (int i = 0; i < freeVarValIndices.Length; i++)
                {
                    Assignment varVal = new Assignment(freeVars[i], subwords[freeVarValIndices[i]]);
                    varVals.Add(varVal);
                }
                if (evaluateQuantifiedExpression(parsedExpression, subwords.ToArray(), varVals))
                {
                    successfulValueCombinations.Add(varVals);
                }
                freeVarValIndices[freeVarValIndices.Length - 1]++;
                for (int i = freeVarValIndices.Length - 1; i > 0; i--)
                {
                    if (freeVarValIndices[i] >= subwords.Count())
                    {
                        freeVarValIndices[i] = 0;
                        freeVarValIndices[i - 1]++;
                    }
                }
            } while (freeVarValIndices[0] != subwords.Count());
            successfulValueCombinations = FOLSearcher.removeVariable(successfulValueCombinations, "S");
            return(successfulValueCombinations);
        }
Exemple #8
0
 public FOLExpression(ParseTreeFOL_Q quantifiers, ParseTreeFOL_O expression)
 {
     this.FOQuantifiers = quantifiers;
     this.PLExpression  = expression;
 }
Exemple #9
0
 public ParseTreeFOL_Q(FOLQuantifier?folquantifier, string quantifiedVar, ParseTreeFOL_Q child)
 {
     this.folQuantifier = folquantifier;
     this.quantifiedVar = quantifiedVar;
     this.child         = child;
 }