예제 #1
0
        public void ConjunctionNodeTest_OR()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            Expression expectedLeft = Expression.Constant(false);
            Expression expectedRight = Expression.Constant(true);

            ConjunctionNode sut = new ConjunctionNode(ConjunctionNode.Conjunction.OR, new TestNode(expectedLeft), new TestNode(expectedRight), expectedLocation);

            Assert.AreEqual(TNode.CONJUNCTION, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("OR"));
            Assert.IsInstanceOfType(sut.SubExpression, typeof(BinaryExpression));
            Assert.AreEqual(ExpressionType.Or, sut.SubExpression.NodeType);
            Assert.AreEqual(typeof(bool), sut.SubExpression.Type);
            Assert.AreEqual(expectedLeft, ((BinaryExpression)sut.SubExpression).Left);
            Assert.AreEqual(expectedRight, ((BinaryExpression)sut.SubExpression).Right);
        }
예제 #2
0
        private object EvaluateNode(ConjunctionNode expression, Context context)
        {
            var value = EvaluateNode(expression.lft, context);

            if (expression.rgt == null || expression.rgt.Length == 0)
            {
                return(value);
            }
            foreach (var rgt in expression.rgt)
            {
                if ((bool)value == false)
                {
                    return(null);
                }
                value = EvaluateNode(rgt, context);
            }
            if ((bool)value == false)
            {
                return(null);
            }
            return(value);
        }
예제 #3
0
파일: Processor.cs 프로젝트: droft1312/lpp
        /// <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);
            }
        }