public TokenTree replaceEquals() { List<Token> tokens = ReverseBFS(); foreach(Token t in tokens) { if(t is Operator && t.name == "EQUALS") { Token parent = t.parent; Token left = t.getChild(0); Token right = t.getChild(1); Token n = new Operator(name: "AND"); Token newLeft = new Operator(name: "IMPLIES"); Token newRight = new Operator(name: "IMPLIES"); newLeft.setChild(0, left.Clone()); newLeft.setChild(1, right.Clone()); newRight.setChild(0, right.Clone()); newRight.setChild(1, left.Clone()); n.setChild(0, newLeft); newLeft.parent = n; n.setChild(1, newRight); newRight.parent = n; if (parent == null) { root = n; } else { parent.setChild(t.indexInParent, n); } } } return this; }
public Token Clone() { Token n; if (this is Operator) { n = new Operator(parent, name); } else if (this is Quantifier) { n = new Quantifier(parent, name); } else if(this is Function) { n = new Function(parent, name, childrenCount); } else { n = (Token)Activator.CreateInstance(GetType()); n.name = name; n.priority = priority; n.parent = parent; n.childrenCount = childrenCount; } n.indexInParent = indexInParent; for(int i=0;i<childrenCount;++i) { n.setChild(i, getChild(i)); } return n; }
public TokenTree convertAlternativeToConjunctions() { bool somethingDone; do { somethingDone = false; List<Token> tokens = ReverseBFS(); foreach (Token t in tokens) { if (t is Operator && t.name == "OR") { List<Token> left = new List<Token>(); List<Token> right = new List<Token>(); if(t.getChild(0) is Operator && t.getChild(0).name == "AND") { left.AddRange(getChildrenRecursively(t.getChild(0))); } else { left.Add(t.getChild(0)); } if (t.getChild(1) is Operator && t.getChild(1).name == "AND") { right.AddRange(getChildrenRecursively(t.getChild(1))); } else { right.Add(t.getChild(1)); } if (left.Count == 1 && right.Count == 1) continue; somethingDone = true; List<Operator> alternatives = new List<Operator>(); foreach(Token l in left) { foreach(Token r in right) { Operator n = new Operator(name: "OR"); n.setChild(0, l); n.setChild(1, r); alternatives.Add(n); } } Token mainAnd = new Operator(name: "AND"); Token current = mainAnd; for(int i=0;i<alternatives.Count - 2;++i) { current.setChild(0, alternatives[i]); current.setChild(1, new Operator(name:"AND")); current = current.getChild(1); } current.setChild(0, alternatives[alternatives.Count - 2]); current.setChild(1, alternatives[alternatives.Count - 1]); if (t.parent == null) root = mainAnd; else t.parent.setChild(t.indexInParent, mainAnd); } } } while (somethingDone); return this; }
public TokenTree restrainNegation() { bool somethingDone; do { somethingDone = false; List<Token> tokens = ReverseBFS(); foreach (Token t in tokens) { if (t is Negation && t.getChild(0).isFinal() == false) { Token t_parent = t.parent; Token c = t.getChild(0); if(c is Negation) { somethingDone = true; if (t_parent == null) root = c.getChild(0); else t_parent.setChild(t.indexInParent, c.getChild(0)); // Console.WriteLine(serialize()); } else if(c is Operator) { somethingDone = true; if (c.name == "AND") { Token n = new Operator(name: "OR"); Token left = c.getChild(0); Token right = c.getChild(1); Token newLeft = new Negation(); Token newRight = new Negation(); newLeft.setChild(0, left.Clone()); newRight.setChild(0, right.Clone()); n.setChild(0, newLeft); n.setChild(1, newRight); if (t_parent == null) { root = n; } else { t_parent.setChild(t.indexInParent, n); } //Console.WriteLine(serialize()); } else if (c.name == "OR") { Token n = new Operator(name: "AND"); Token left = c.getChild(0); Token right = c.getChild(1); Token newLeft = new Negation(); Token newRight = new Negation(); newLeft.setChild(0, left.Clone()); newRight.setChild(0, right.Clone()); n.setChild(0, newLeft); n.setChild(1, newRight); if (t_parent == null) { root = n; } else { t_parent.setChild(t.indexInParent, n); } //Console.WriteLine(serialize()); } } else if(c is Quantifier) { somethingDone = true; if (c.name == "FORALL") { Token n = new Quantifier(name: "EXISTS"); Token left = c.getChild(0); Token right = c.getChild(1); Token newRight = new Negation(); newRight.setChild(0, right.Clone()); n.setChild(0, left); n.setChild(1, newRight); if (t_parent == null) { root = n; } else { t_parent.setChild(t.indexInParent, n); } } else if (c.name == "EXISTS") { Token n = new Quantifier(name: "FORALL"); Token left = c.getChild(0); Token right = c.getChild(1); Token newRight = new Negation(); newRight.setChild(0, right.Clone()); n.setChild(0, left); n.setChild(1, newRight); if (t_parent == null) { root = n; } else { t_parent.setChild(t.indexInParent, n); } } } } } } while (somethingDone); return this; }