コード例 #1
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);
     }
 }
コード例 #2
0
        private void Search(bool isTopDown)
        {
            DateTime start = DateTime.Now;

            resultsGridView.Rows.Clear();
            List <List <Assignment> > res;

            try
            {
                string query = FOLParser.RemoveUnquotedInstancesOf(queryBox.Text, " ");
                res = FOLSearcher.Search(materialBox.Text, query, isTopDown);

                TimeSpan duration = DateTime.Now.Subtract(start);
                MessageBox.Show($"Time taken to evaluate: {duration.Hours} hours, {duration.Minutes} minutes, {duration.Seconds} seconds, {duration.Milliseconds} milliseconds.");
                start = DateTime.Now;

                DisplayResults(res);

                duration = DateTime.Now.Subtract(start);
                MessageBox.Show($"Time taken to display: {duration.Hours} hours, {duration.Minutes} minutes, {duration.Seconds} seconds, {duration.Milliseconds} milliseconds.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
コード例 #3
0
        static List <List <Assignment> > FillInSolutions(List <List <Assignment> > left, List <List <Assignment> > right, string[] subwords)
        {
            string[] leftVars  = FOLSearcher.GetFreeVars(left);
            string[] rightVars = FOLSearcher.GetFreeVars(right);

            string[] newLeftVars = rightVars.Where(r => !leftVars.Any(l => r == l)).ToArray();

            return(FillInSolutions(left, newLeftVars, subwords));
        }
コード例 #4
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);
        }
コード例 #5
0
        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);
        }