Пример #1
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;
        }
Пример #2
0
        public TokenTree replaceImplies()
        {
            List<Token> tokens = ReverseBFS();
            foreach (Token t in tokens)
            {
                if (t is Operator && t.name == "IMPLIES")
                {

                    Token parent = t.parent;

                    Token left = t.getChild(0);
                    Token right = t.getChild(1);

                    Token n = new Operator(name: "OR");
                    Token newLeft = new Negation();

                    newLeft.setChild(0, left.Clone());
                    
                    n.setChild(0, newLeft);
                    n.setChild(1, right.Clone());

                    if (parent == null)
                    {
                        root = n;
                    }
                    else
                    {
                        parent.setChild(t.indexInParent, n);
                    }
                    
                }
            }
            return this;
        }