Example #1
0
        public void NandNode_11_Positive()
        {
            INode f1 = new FakeNode(true);
            INode f2 = new FakeNode(true);

            NandNode node = new NandNode("testName");

            node.Inputs.Add(f1);
            node.Inputs.Add(f2);

            Assert.AreEqual(false, node.Process()[0]);
        }
Example #2
0
        public void NandNodeTest(bool input1, bool input2, bool expectedOutput)
        {
            var node   = new NandNode("");
            var states = new List <State>
            {
                new State(input1),
                new State(input2)
            };

            node.Calculate(states.ToArray());

            Assert.Equal(expectedOutput, node.CurrentState.LogicState);
        }
Example #3
0
        public void NandNodeOutputTest()
        {
            var a1 = new NandNode();
            var a2 = new NandNode();
            var a3 = new NandNode();
            var a4 = new NandNode();

            a1.Step(NodeCurrent.Low);
            a1.Step(NodeCurrent.Low);

            a2.Step(NodeCurrent.High);
            a2.Step(NodeCurrent.Low);

            a3.Step(NodeCurrent.Low);
            a3.Step(NodeCurrent.High);

            a4.Step(NodeCurrent.High);
            a4.Step(NodeCurrent.High);

            Assert.IsTrue(a1.Value == NodeCurrent.High);
            Assert.IsTrue(a2.Value == NodeCurrent.High);
            Assert.IsTrue(a3.Value == NodeCurrent.High);
            Assert.IsTrue(a4.Value == NodeCurrent.Low);
        }
Example #4
0
        public Node Nandify(Node tree)
        {
            /*
             * possible cases:
             * 1) implication
             * 2) bi-implication
             * 3) disjunction
             * 4) conjunction
             * 5) not
             *
             */

            if (tree == null)
            {
                return(null);
            }


            var leftOfTree  = tree.left;
            var rightOfTree = tree.right;

            var nandifiedLeftSubtree  = Nandify(leftOfTree);
            var nandifiedRightSubtree = Nandify(rightOfTree);

            Node newTree;

            if (tree is ImplicationNode)
            {
                newTree = new NandNode(nandifiedLeftSubtree, new NandNode(nandifiedRightSubtree, DeepCopyTree(nandifiedRightSubtree)));
            }
            else if (tree is BiImplicationNode)
            {
                newTree = new NandNode(
                    new NandNode(
                        new NandNode(nandifiedLeftSubtree, DeepCopyTree(nandifiedLeftSubtree)),
                        new NandNode(nandifiedRightSubtree, DeepCopyTree(nandifiedRightSubtree))
                        ),
                    new NandNode(DeepCopyTree(nandifiedLeftSubtree), DeepCopyTree(nandifiedRightSubtree))
                    );
            }
            else if (tree is DisjunctionNode)
            {
                newTree = new NandNode(
                    new NandNode(nandifiedLeftSubtree, DeepCopyTree(nandifiedLeftSubtree)),
                    new NandNode(nandifiedRightSubtree, DeepCopyTree(nandifiedRightSubtree))
                    );
            }
            else if (tree is ConjunctionNode)
            {
                newTree = new NandNode(
                    new NandNode(nandifiedLeftSubtree, DeepCopyTree(nandifiedRightSubtree)),
                    new NandNode(DeepCopyTree(nandifiedLeftSubtree), nandifiedRightSubtree)
                    );
            }
            else if (tree is NotNode)
            {
                newTree = new NandNode(nandifiedLeftSubtree, DeepCopyTree(nandifiedLeftSubtree));
            }
            else
            {
                newTree = tree;
            }

            return(newTree);
        }
Example #5
0
        /// <summary>
        /// A method that is called to actually build a tree from a given input. Is called from ProcessStringInput()
        /// </summary>
        /// <param name="input">input string</param>
        /// <param name="root">Node used in recursion for creating a binary tree</param>
        /// <exception cref="Exception"></exception>
        private void BuildTree(string input, Node root)
        {
            if (input == string.Empty)
            {
                return;
            }

            char first_character = input[0];

            if (first_character == '~')
            {
                NotNode node = new NotNode(input, root);
                root.Insert(node);
                BuildTree(node.Value, node);
            }
            else if (first_character == '>')
            {
                ImplicationNode node = new ImplicationNode(input, root);
                root.Insert(node);
                BuildTree(node.Value, node);
            }
            else if (first_character == '=')
            {
                BiImplicationNode node = new BiImplicationNode(input, root);
                root.Insert(node);
                BuildTree(node.Value, node);
            }
            else if (first_character == '&')
            {
                ConjunctionNode node = new ConjunctionNode(input, root);
                root.Insert(node);
                BuildTree(node.Value, node);
            }
            else if (first_character == '|')
            {
                DisjunctionNode node = new DisjunctionNode(input, root);
                root.Insert(node);
                BuildTree(node.Value, node);
            }
            else if (first_character == '%')
            {
                NandNode node = new NandNode(input, root);
                root.Insert(node);
                BuildTree(node.Value, node);
            }
            else if (Char.IsUpper(first_character) && input[1] == '(')
            {
                /* predicate case */
                /* P(x), Q(x) */

                int closingBracketIndex = GetIndexOfClosingBracket(input, 1);

                var vars = QuantifierInputHandler.StringStartEndIndex(input, 2, closingBracketIndex - 1).Split(',');

                PredicateNode predicate = new PredicateNode(first_character);

                List <PropositionNode> propositions = new List <PropositionNode>();

                foreach (var variable in vars)
                {
                    propositions.Add(new PropositionNode(variable));
                }

                predicate.Formulas = propositions;

                root.Insert(predicate);
                predicate.parent = root;

                input = input.Substring(closingBracketIndex + 1);

                BuildTree(input, predicate);
            }
            else if (first_character == '@' || first_character == '!')
            {
                string quantifierInput = QuantifierInputHandler.ParseOutInputForQuantifiers(input);
                QuantifierInputHandler quantifierInputHandler = new QuantifierInputHandler(quantifierInput);
                var node = quantifierInputHandler.Create();

                node.parent = root;

                root.Insert(node);

                var newInput = input.Substring(IndexTillWhichStringAreSame(quantifierInput, input) + 1);

                BuildTree(newInput, node);
            }
            else if (first_character == ',')
            {
                if (root.parent == null)
                {
                    throw new Exception("Error in your input");
                }

                root  = root.parent;
                input = input.Substring(1);
                BuildTree(input, root);
            }
            else if (Char.IsLetter(first_character))
            {
                PropositionNode node = new PropositionNode(first_character.ToString(), input, root);
                root.Insert(node);
                BuildTree(node.Value, node);
            }
            else if (first_character == ')')
            {
                int numberOflevels = CalculateNumberOfLevelsToGoUp(input);

                for (int i = 0; i < numberOflevels; i++)
                {
                    if (root.parent != null)
                    {
                        root = root.parent;
                    }
                    else
                    {
                        throw new Exception("Error in string input. Source: class Processor, method BuildTree, else if (')')");
                    }
                }

                input = input.Substring(numberOflevels);
                BuildTree(input, root);
            }
            else if (first_character == '(')
            {
                input = input.Substring(1);
                BuildTree(input, root);
            }
        }
Example #6
0
 public override Node CloneForFunctionCall(int clonedId, Node[] functionInputs)
 {
     if (_clonedId != clonedId)
     {
         _clonedId = clonedId;
         _cloned = new NandNode(_thisFunction, Left.CloneForFunctionCall(clonedId, functionInputs), Right.CloneForFunctionCall(clonedId, functionInputs));
     }
     return _cloned;
 }
 public int visit(NandNode NandNodeItem)
 {
     return((NandNodeItem.PropogationDelay) + 6);
 }