Ejemplo n.º 1
0
        /// <summary>
        /// Does the OR calculation to generate a list of AND groups of
        /// operands.
        /// </summary>
        /// <param name="startingOpNode"></param>
        /// <param name="isNot"></param>
        /// <returns></returns>
        private List <List <Operand> > OrOperation(BoolExprTreeNode startingOpNode, bool isNot)
        {
            if (null == startingOpNode)
            {
                throw new ArgumentNullException($"{nameof(startingOpNode)}");
            }
            if (startingOpNode.IsLeaf())
            {
                throw new BoolExprTreeException("The specified starting node doesn't have 2 children.");
            }
            if (startingOpNode.Operator == BoolExprTreeNode.BoolOperator.NOT)
            {
                throw new BoolExprTreeException("The specified starting node is a unary operator node.");
            }

            var left  = ConvertToOrGroups(startingOpNode.Left, isNot);
            var right = ConvertToOrGroups(startingOpNode.Right, isNot);

            if (null == left || null == right)
            {
                throw new BoolExprTreeException("The specified Binary Boolean Expression Tree is not valid.");
            }

            left.AddRange(right);
            return(left);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Deeply copies the subtree starting from the specified node.
 /// </summary>
 /// <param name="other"></param>
 public BoolExprTreeNode(BoolExprTreeNode other)
 {
     _op      = other._op;
     _left    = other._left == null ? null : new BoolExprTreeNode(other._left);
     _right   = other._right == null ? null : new BoolExprTreeNode(other._right);
     _literal = new StringBuilder(other._literal).ToString();
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Builds the Binary Boolean Expression Tree.
 /// </summary>
 /// <param name="polishNotationTokensEnumerator"></param>
 /// <returns></returns>
 public BoolExprTreeNode GenerateBoolBinTree(ref List <Token> .Enumerator polishNotationTokensEnumerator)
 {
     if (polishNotationTokensEnumerator.Current.Type == Token.TokenType.LITERAL)
     {
         var literalNode = BoolExprTreeNode.CreateBoolVar(polishNotationTokensEnumerator.Current.Value);
         polishNotationTokensEnumerator.MoveNext();
         return(literalNode);
     }
     else
     {
         if (polishNotationTokensEnumerator.Current.Value == "NOT")
         {
             polishNotationTokensEnumerator.MoveNext();
             var operand = GenerateBoolBinTree(ref polishNotationTokensEnumerator);
             return(BoolExprTreeNode.CreateNotOpNode(operand));
         }
         else if (polishNotationTokensEnumerator.Current.Value == "AND")
         {
             polishNotationTokensEnumerator.MoveNext();
             var leftNode  = GenerateBoolBinTree(ref polishNotationTokensEnumerator);
             var rightNode = GenerateBoolBinTree(ref polishNotationTokensEnumerator);
             return(BoolExprTreeNode.CreateAndOpNode(leftNode, rightNode));
         }
         else if (polishNotationTokensEnumerator.Current.Value == "OR")
         {
             polishNotationTokensEnumerator.MoveNext();
             var leftNode  = GenerateBoolBinTree(ref polishNotationTokensEnumerator);
             var rightNode = GenerateBoolBinTree(ref polishNotationTokensEnumerator);
             return(BoolExprTreeNode.CreateOrOpNode(leftNode, rightNode));
         }
     }
     return(null);
 }
Ejemplo n.º 4
0
 private BoolExprTreeNode(string literal)
 {
     _op      = BoolOperator.LEAF;
     _left    = null;
     _right   = null;
     _literal = literal;
 }
Ejemplo n.º 5
0
 //
 // Private constructors
 //
 private BoolExprTreeNode(BoolOperator op, BoolExprTreeNode left, BoolExprTreeNode right)
 {
     _op      = op;
     _left    = left;
     _right   = right;
     _literal = null;
 }
Ejemplo n.º 6
0
 public void Dispose()
 {
     _op      = BoolOperator.LEAF;
     _left    = null;
     _right   = null;
     _literal = null;
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Traverse the Binary Boolean Expression Tree from the given starting
        /// node, in order to generate a list of OR groups of operands.
        /// </summary>
        /// <param name="boolExprTreeNode"></param>
        /// <param name="isNot"></param>
        /// <returns></returns>
        public List <List <Operand> > ConvertToOrGroups(BoolExprTreeNode boolExprTreeNode, bool isNot = false)
        {
            if (null != boolExprTreeNode)
            {
                if (boolExprTreeNode.IsLeaf())
                {
                    return(new List <List <Operand> >()
                    {
                        new List <Operand>()
                        {
                            new Operand(boolExprTreeNode.Literal, isNot)
                        }
                    });
                }
                else
                {
                    if (boolExprTreeNode.Operator == BoolExprTreeNode.BoolOperator.OR)
                    {
                        if (isNot)
                        {
                            return(AndOperation(boolExprTreeNode, isNot));
                        }
                        else
                        {
                            return(OrOperation(boolExprTreeNode, isNot));
                        }
                    }

                    if (boolExprTreeNode.Operator == BoolExprTreeNode.BoolOperator.AND)
                    {
                        var left  = ConvertToOrGroups(boolExprTreeNode.Left);
                        var right = ConvertToOrGroups(boolExprTreeNode.Right);

                        if (isNot)
                        {
                            return(OrOperation(boolExprTreeNode, isNot));
                        }
                        else
                        {
                            return(AndOperation(boolExprTreeNode, isNot));
                        }
                    }

                    if (boolExprTreeNode.Operator == BoolExprTreeNode.BoolOperator.NOT)
                    {
                        if (null != boolExprTreeNode.Right || null == boolExprTreeNode.Left)
                        {
                            throw new BoolExprTreeException("The specified Binary Boolean Expression Tree is not valid.");
                        }

                        return(ConvertToOrGroups(boolExprTreeNode.Left, true));
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Does the AND calculation to generate a list of AND groups of
        /// operands.
        /// </summary>
        /// <param name="startingOpNode"></param>
        /// <param name="isNot"></param>
        /// <returns></returns>
        private List <List <Operand> > AndOperation(BoolExprTreeNode startingOpNode, bool isNot)
        {
            if (null == startingOpNode)
            {
                throw new ArgumentNullException($"{nameof(startingOpNode)}");
            }
            if (startingOpNode.IsLeaf())
            {
                throw new BoolExprTreeException("The specified starting node doesn't have 2 children.");
            }
            if (startingOpNode.Operator == BoolExprTreeNode.BoolOperator.NOT)
            {
                throw new BoolExprTreeException("The specified starting node is a unary operator node.");
            }

            var left  = ConvertToOrGroups(startingOpNode.Left, isNot);
            var right = ConvertToOrGroups(startingOpNode.Right, isNot);

            if (null == left || null == right)
            {
                throw new BoolExprTreeException("The specified Binary Boolean Expression Tree is not valid.");
            }

            var combinations = (from l in left
                                from r in right
                                select new { l, r }).ToList();

            var results = new List <List <Operand> >();

            combinations.ForEach(combination =>
            {
                var result   = new List <Operand>(combination.l);
                var newRight = new List <Operand>(combination.r);
                result.AddRange(newRight);
                results.Add(result);
            });

            return(results);
        }
Ejemplo n.º 9
0
 public static BoolExprTreeNode CreateOrOpNode(BoolExprTreeNode left, BoolExprTreeNode right)
 {
     return(new BoolExprTreeNode(BoolOperator.OR, left, right));
 }
Ejemplo n.º 10
0
 public static BoolExprTreeNode CreateNotOpNode(BoolExprTreeNode child)
 {
     return(new BoolExprTreeNode(BoolOperator.NOT, child, null));
 }