Esempio n. 1
0
 public Negation(Token parent = null, string name = "")
 {
     this.parent = parent;
     this.name = "NOT";
     this.priority = Global.priorities["NOT"];
     this.childrenCount = 1;
 }
Esempio n. 2
0
 //public Operator() : base() { }
 public Operator(Token parent = null, String name = "")
 {
     this.parent = parent;
     this.name = name;
     this.priority = Global.priorities[this.name];
     
     this.childrenCount = 2;
 }
Esempio n. 3
0
 //public Quantifier() : base() { }
 public Quantifier(Token parent = null, string name = "")
 {
     this.parent = parent;
     this.name = name;
     this.priority = Global.priorities[this.name];
     this.childrenCount = 2;
     
 }
Esempio n. 4
0
        public Function(Token parent = null, String name = "", int argumentCount = 0)
        {
            this.parent = parent;
            this.name = name;
            this.childrenCount = argumentCount;
            this.priority = Global.priorities["FUNCTION"];
            this.arguments = new Token[argumentCount];

        }
Esempio n. 5
0
        public override void setChild(int index, Token token)
        {
            if (index != 0)
                throw new InvalidChildIndexException("Index: " + index);

            this.child = token;
            token.indexInParent = index;
            token.parent = this;
        }
Esempio n. 6
0
        public override void setChild(int index, Token token) {
            switch (index)
            {
                case 0:
                    variable = token as Variable;
                    break;
                case 1:
                    expression = token;
                    break;
                default:
                    throw new Exception("Invalid child index: " + index);

            }
            token.indexInParent = index;
            token.parent = this;
        }
Esempio n. 7
0
 public override void setChild(int index, Token token)
 {
     switch (index)
     {
         case 0:
             left = token;
             break;
         case 1:
             right = token;
             break;
         default:
             throw new InvalidChildIndexException("Index: " + index);
     }
     token.indexInParent = index;
     token.parent = this;
 }
Esempio n. 8
0
        public TokenTree simplify()
        {
            bool somethingDone;
            do
            {
                somethingDone = false;
                List<Token> tokens = ReverseBFS();
                foreach (Token t in tokens)
                {
                    if (t is Operator)
                    {
                        Token left = t.getChild(0);
                        Token right = t.getChild(1);

                        if(left is Variable && right is Variable)
                        {
                            if(left.name == right.name)
                            {
                                if(t.name == "AND" || t.name == "OR" || t.name == "IMPLIES")
                                {
                                    somethingDone = true;
                                    Token n = new Variable(name: left.name);

                                    if (t.parent == null)
                                    {
                                        root = n;
                                    }
                                    else
                                    {
                                        t.parent.setChild(t.indexInParent, n);
                                    }
                                }
                                
                            }
                        }
                        
                    }
                }
            } while (somethingDone);
            return this;
        }
Esempio n. 9
0
 public Variable(Token parent = null, string name = "")
 {
     this.parent = parent;
     this.name = name;
     this.childrenCount = 0;
 }
Esempio n. 10
0
 public Token setParent(Token parent)
 {
     this.parent = parent;
     return this;
 }
Esempio n. 11
0
 public abstract void setChild(int index, Token token);
Esempio n. 12
0
 public TokenTree(Token root = null)
 {
     this.root = root;
 }
Esempio n. 13
0
 public override void setChild(int index, Token token)
 {
     try
     {
         arguments[index] = token;
     }
     catch (Exception)
     {
         throw new InvalidChildIndexException("Index: " + index);
     }
     
     token.indexInParent = index;
     token.parent = this;
 }
Esempio n. 14
0
        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;
        }
Esempio n. 15
0
        private List<Token> getChildrenRecursively(Token t)
        {
            List<Token> result = new List<Token>();
            if (t.getChild(0) is Operator && t.getChild(0).name == "AND")
            {
                result.AddRange(getChildrenRecursively(t.getChild(0)));
            }
            else
                result.Add(t.getChild(0));

            if (t.getChild(1) is Operator && t.getChild(1).name == "AND")
            {
                result.AddRange(getChildrenRecursively(t.getChild(1)));
            }
            else
                result.Add(t.getChild(1));

            return result;
        }
Esempio n. 16
0
        public TokenTree removeForallQuantifier()
        {
            bool somethingDone;
            do
            {
                somethingDone = false;
                List<Token> tokens = ReverseBFS();
                foreach (Token t in tokens)
                {
                    if (t is Quantifier && t.name == "FORALL")
                    {
                        somethingDone = true;
                        Token oldParent = t.parent;
                        Token expression = t.getChild(1);
                        if(oldParent == null)
                        {
                            root = expression;
                            expression.parent = null;
                        }
                        else
                        {
                            oldParent.setChild(t.indexInParent, expression);
                        }
                        

                    }
                }
            } while (somethingDone);
            return this;
        }
Esempio n. 17
0
        public TokenTree removeExistsQuantifier()
        {
            bool somethingDone;
            do
            {
                somethingDone = false;
                List<Token> tokens = ReverseBFS();
                foreach (Token t in tokens)
                {
                    if (t is Quantifier && t.name == "EXISTS" && t.subtreeReplaced == false)
                    {
                        somethingDone = true;
                        Token current = t.parent;
                        List<Quantifier> parentQuantifiers = new List<Quantifier>();
                        while(current != null)
                        {
                            if(current is Quantifier && current.name == "FORALL")
                                parentQuantifiers.Add(current as Quantifier);

                            current = current.parent;
                        }

                        if(parentQuantifiers.Count > 0)
                        {
                            List<Token> subtree = t.getChild(1).BFS();
                            
                            foreach (Token subtoken in subtree)
                            {
                                if (subtoken is Variable)
                                {
                                    if(subtoken.name == t.getChild(0).name)
                                    {
                                        Token n = new Function(name: "F_" + t.getChild(0).name.ToUpper(), argumentCount: parentQuantifiers.Count);
                                        for(int i=parentQuantifiers.Count - 1;i>=0;--i)
                                        {
                                            n.setChild(i, new Variable(name: parentQuantifiers[i].getChild(0).name));
                                        }
                                        subtoken.parent.setChild(subtoken.indexInParent, n);
                                    }
                                }
                                
                            }

                        }
                        else
                        {
                            List<Token> subtree = t.getChild(1).BFS();
                            string constantName = t.getChild(0).name.ToUpper();
                            foreach(Token subtoken in subtree)
                            {
                                if (subtoken is Variable && subtoken.name == t.getChild(0).name)
                                {
                                    Token n = new Constant(name: constantName);
                                    subtoken.parent.setChild(subtoken.indexInParent, n);
                                    
                                }
                            }
                            
                        }

                        if (t.parent == null)
                            root = t.getChild(1);
                        else
                            t.parent.setChild(t.indexInParent, t.getChild(1));

                    }
                }
            } while (somethingDone);
            return this;
        }
Esempio n. 18
0
        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;
        }
Esempio n. 19
0
        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;
        }
Esempio n. 20
0
 public bool precedes(Token other)
 {
     return this.priority > other.priority;
 }
Esempio n. 21
0
 public override void setChild(int index, Token token)
 {
     throw new InvalidChildIndexException("Index: " + index);
 }
Esempio n. 22
0
 public Constant(Token parent = null, string name = "")
 {
     this.parent = parent;
     this.name = name;
     this.childrenCount = 0;
 }