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