/* Replace the variables describing the state <from> with the ones that describe * state <to> */ public static FormulaNode Substitute(FormulaNode formula, SymbolicState from, SymbolicState to) { if (formula.GetLogicOperator() == LogicOperator.VAR) { if (from.nameToElementary.Keys.Contains(formula.GetName())) { // this variable represents an elementary formula var elementary = from.nameToElementary[formula.GetName()]; return(new FormulaNode(to.elementaryNames[elementary])); } else { return(new FormulaNode(formula.GetName())); } } if (formula.GetLogicOperator() == LogicOperator.ALL || formula.GetLogicOperator() == LogicOperator.EXISTS) { FormulaNode result = new FormulaNode(formula.GetLogicOperator()); if (from.nameToElementary.Keys.Contains(formula.GetName())) { // this variable represents an elementary formula var elementary = from.nameToElementary[formula.GetName()]; result.SetName(to.elementaryNames[elementary]); } else { result.SetName(formula.GetName()); } result.SetChildren(Substitute(formula[0], from, to), null); return(result); } FormulaNode res = new FormulaNode(formula.GetLogicOperator()); FormulaNode left = Substitute(formula[0], from, to); FormulaNode right = null; if (formula[1] != null) { right = Substitute(formula[1], from, to); } res.SetChildren(left, right); return(res); }
public FormulaNode Quantify(LogicOperator quantifier, FormulaNode formula) { FormulaNode result = formula; foreach (string name in elementaryNames.Values) { result = new FormulaNode(quantifier, result, null); result.SetName(name); } return(result); }
private static FormulaNode Parse(List <Token> tokens) { FormulaNode result = null; List <Token> rightSide = new List <Token>(); BinaryToken lastOp = null; // the input is a single token - must be an atom or an unparsed string if (tokens.Count == 1) { var tok = tokens[0] as LiteralToken; if (tok.type == TokenType.ATOM) { return(new FormulaNode(tok.value)); } return(Parse(ToplevelTokenize(tok.value))); } int minPrecedence = MAX_PRECEDENCE; for (int i = 0; i < tokens.Count; i++) { if (tokens[i] is BinaryToken) { var op = tokens[i] as BinaryToken; minPrecedence = Math.Min(minPrecedence, op.precedence); } } if (minPrecedence == MAX_PRECEDENCE) { // the input didn't contain any toplevel binary operators var opToken = tokens[0] as UnaryToken; result = new FormulaNode(opToken.logicOperator); var operand = Parse(tokens.GetRange(1, tokens.Count - 1)); if (untilOperators.Contains(opToken.logicOperator)) { result.SetChildren(operand[0], operand[1]); } else if (quanitifers.Contains(opToken.logicOperator)) { result.SetName(operand[0].GetName()); result.SetChildren(operand[1], null); } else { result.SetChildren(operand, null); } return(result); } // if we got here - split by lowest-precedence binary operator foreach (Token t in tokens) { if (t is BinaryToken) { var op = t as BinaryToken; if (op.precedence == minPrecedence) { if (result == null) { result = Parse(rightSide); } else { var leftSide = result; result = new FormulaNode(lastOp.logicOperator); result.SetChildren(leftSide, Parse(rightSide)); } rightSide = new List <Token>(); lastOp = op; continue; } } rightSide.Add(t); } if (rightSide.Count != 0) { var leftSide = result; result = new FormulaNode(lastOp.logicOperator); result.SetChildren(leftSide, Parse(rightSide)); } return(result); }