Example #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);
     }
 }
        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);
        }