Esempio n. 1
0
        // help with creating binary tree
        private void InsertTreeHelper(ref Node n, string c)
        {
            switch (c)
            {
            case "&":
                n           = new AndOperator(c);
                n.LeftNode  = this._myStack.Pop();
                n.RightNode = this._myStack.Pop();
                this._myStack.Push(n);
                break;

            case "|":
                n           = new OrOperator(c);
                n.LeftNode  = this._myStack.Pop();
                n.RightNode = this._myStack.Pop();
                this._myStack.Push(n);
                break;

            case ">":
                n           = new Implication(c);
                n.LeftNode  = this._myStack.Pop();
                n.RightNode = this._myStack.Pop();
                this._myStack.Push(n);
                break;

            case "~":
                n           = new Negation(c);
                n.RightNode = this._myStack.Pop();
                this._myStack.Push(n);
                break;

            case "=":
                n           = new BiImplication(c);
                n.LeftNode  = this._myStack.Pop();
                n.RightNode = this._myStack.Pop();
                this._myStack.Push(n);
                break;

            case "%":
                n           = new NANDOperator(c);
                n.LeftNode  = this._myStack.Pop();
                n.RightNode = this._myStack.Pop();
                this._myStack.Push(n);
                break;

            case "!":
                n           = new Existential(c, new PredicateVariable(this._myStack.Pop().ToString()));
                n.RightNode = this._myStack.Pop();
                this._myStack.Push(n);
                break;

            case "@":
                n           = new Universal(c, new PredicateVariable(this._myStack.Pop().ToString()));
                n.RightNode = this._myStack.Pop();
                this._myStack.Push(n);
                break;

            default:
                if (char.IsLower(char.Parse(c)))
                {
                    n = new PredicateVariable(c);
                    this._myStack.Push(n);
                }
                else
                {
                    n = new Variable(c);
                    int total = this._myStack.Count;
                    for (int i = 0; i < total; i++)
                    {
                        if (_myStack.Peek() is PredicateVariable p)
                        {
                            n.AddVariable(p);
                            _myStack.Pop();
                        }
                    }
                    this._myStack.Push(n);
                }
                break;
            }
        }
Esempio n. 2
0
        public static Component CloneNode(Component node, BinaryTree bt, char current = '!', char rename = '!')
        {
            Component newNode;

            if (node is BiImplication)
            {
                newNode = new BiImplication();
            }
            else if (node is Implication)
            {
                newNode = new Implication();
            }
            else if (node is Conjunction)
            {
                newNode = new Conjunction();
            }
            else if (node is Disjunction)
            {
                newNode = new Disjunction();
            }
            else if (node is Negation negation)
            {
                newNode = new Negation();
                ((Negation)newNode).GammaProcessed = negation.GammaProcessed;
            }
            else if (node is Nand)
            {
                newNode = new Nand();
            }
            else if (node is TrueFalse)
            {
                newNode = new TrueFalse(((TrueFalse)node).Data);
            }
            else if (node is Predicate predicate)
            {
                newNode = new Predicate(node.Symbol);
                predicate.ObjectVariables.Variables.ForEach(x => ((IVariableContainer)newNode)
                                                            .ObjectVariables.AddPropositionalVariable(CloneVariableForPredicate(x, current, rename))
                                                            );
            }
            else if (node is Universal universal)
            {
                newNode = new Universal();
                universal.ObjectVariables.Variables.ForEach(x => ((IVariableContainer)newNode)
                                                            .ObjectVariables.AddPropositionalVariable(CloneVariableForPredicate(x, current, rename))
                                                            );
                ((Universal)newNode).GammaProcessed = universal.GammaProcessed;
            }
            else if (node is Existential existential)
            {
                newNode = new Existential();
                existential.ObjectVariables.Variables.ForEach(x => ((IVariableContainer)newNode)
                                                              .ObjectVariables.AddPropositionalVariable(CloneVariableForPredicate(x, current, rename))
                                                              );
            }
            else
            {//
                newNode = new Variable(((Variable)node).Symbol, ((Variable)node).BindVariable);
                bt.PropositionalVariables.AddPropositionalVariable((Variable)newNode);
            }

            newNode.Parent = node.Parent;
            if (node is CompositeComponent)
            {
                if (node is Negation)
                {
                    newNode.LeftNode = CloneNode(node.LeftNode, bt, current, rename);
                }
                else
                {
                    newNode.LeftNode = node.LeftNode != null?CloneNode(node.LeftNode, bt, current, rename) : node.LeftNode;

                    newNode.RightNode = node.RightNode != null?CloneNode(node.RightNode, bt, current, rename) : node.RightNode;
                }
            }
            newNode.NodeNumber++;
            return(newNode);
        }