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);
        }
        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());
        }
Beispiel #3
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);
        }