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")); }