private IEnumerable <BoolExpr <T_To> > VisitChildren(TreeExpr <T_From> expression)
 {
     foreach (var child in expression.Children)
     {
         yield return(child.Accept(this));
     }
 }
Exemplo n.º 2
0
 private bool VisitTree(TreeExpr <T_Identifier> expression)
 {
     foreach (var child in expression.Children)
     {
         child.Accept(this);
     }
     return(true);
 }
 private bool VisitTree(TreeExpr <T_Identifier> expression)
 {
     foreach (BoolExpr <T_Identifier> child in expression.Children)
     {
         child.Accept <bool>((Visitor <T_Identifier, bool>) this);
     }
     return(true);
 }
Exemplo n.º 4
0
        private int VisitTree(TreeExpr <T_Identifier> expression)
        {
            var sum = 0;

            foreach (var child in expression.Children)
            {
                sum += child.Accept(this);
            }
            return(sum);
        }
Exemplo n.º 5
0
        private BoolExpr <T_Identifier> SimplifyTree(TreeExpr <T_Identifier> tree)
        {
            var isAnd = ExprType.And == tree.ExprType;

            Debug.Assert(isAnd || ExprType.Or == tree.ExprType);

            // Get list of simplified children, flattening nested And/Or expressions
            var simplifiedChildren = new List <BoolExpr <T_Identifier> >(tree.Children.Count);

            foreach (var child in tree.Children)
            {
                var simplifiedChild = child.Accept(this);
                // And(And(A, B), C) iff. And(A, B, C)
                // Or(Or(A, B), C) iff. Or(A, B, C)
                if (simplifiedChild.ExprType
                    == tree.ExprType)
                {
                    simplifiedChildren.AddRange(((TreeExpr <T_Identifier>)simplifiedChild).Children);
                }
                else
                {
                    simplifiedChildren.Add(simplifiedChild);
                }
            }

            // Track negated children separately to identify tautologies and contradictions
            var negatedChildren = new Dictionary <BoolExpr <T_Identifier>, bool>(tree.Children.Count);
            var otherChildren   = new List <BoolExpr <T_Identifier> >(tree.Children.Count);

            foreach (var simplifiedChild in simplifiedChildren)
            {
                switch (simplifiedChild.ExprType)
                {
                case ExprType.Not:
                    negatedChildren[((NotExpr <T_Identifier>)simplifiedChild).Child] = true;
                    break;

                case ExprType.False:
                    // False And A --> False
                    if (isAnd)
                    {
                        return(FalseExpr <T_Identifier> .Value);
                    }
                    // False || A --> A (omit False from child collections)
                    break;

                case ExprType.True:
                    // True Or A --> True
                    if (!isAnd)
                    {
                        return(TrueExpr <T_Identifier> .Value);
                    }
                    // True And A --> A (omit True from child collections)
                    break;

                default:
                    otherChildren.Add(simplifiedChild);
                    break;
                }
            }
            var children = new List <BoolExpr <T_Identifier> >();

            foreach (var child in otherChildren)
            {
                if (negatedChildren.ContainsKey(child))
                {
                    // A && !A --> False, A || !A --> True
                    if (isAnd)
                    {
                        return(FalseExpr <T_Identifier> .Value);
                    }
                    else
                    {
                        return(TrueExpr <T_Identifier> .Value);
                    }
                }
                children.Add(child);
            }
            foreach (var child in negatedChildren.Keys)
            {
                children.Add(child.MakeNegated());
            }
            if (0 == children.Count)
            {
                // And() iff. True
                if (isAnd)
                {
                    return(TrueExpr <T_Identifier> .Value);
                }
                // Or() iff. False
                else
                {
                    return(FalseExpr <T_Identifier> .Value);
                }
            }
            else if (1 == children.Count)
            {
                // Or(A) iff. A, And(A) iff. A
                return(children[0]);
            }
            else
            {
                // Construct simplified And/Or expression
                TreeExpr <T_Identifier> result;
                if (isAnd)
                {
                    result = new AndExpr <T_Identifier>(children);
                }
                else
                {
                    result = new OrExpr <T_Identifier>(children);
                }
                return(result);
            }
        }
Exemplo n.º 6
0
        private BoolExpr <T_Identifier> SimplifyTree(TreeExpr <T_Identifier> tree)
        {
            bool flag = ExprType.And == tree.ExprType;
            List <BoolExpr <T_Identifier> > boolExprList1 = new List <BoolExpr <T_Identifier> >(tree.Children.Count);

            foreach (BoolExpr <T_Identifier> child in tree.Children)
            {
                BoolExpr <T_Identifier> boolExpr = child.Accept <BoolExpr <T_Identifier> >((Visitor <T_Identifier, BoolExpr <T_Identifier> >) this);
                if (boolExpr.ExprType == tree.ExprType)
                {
                    boolExprList1.AddRange((IEnumerable <BoolExpr <T_Identifier> >)((TreeExpr <T_Identifier>)boolExpr).Children);
                }
                else
                {
                    boolExprList1.Add(boolExpr);
                }
            }
            Dictionary <BoolExpr <T_Identifier>, bool> dictionary    = new Dictionary <BoolExpr <T_Identifier>, bool>(tree.Children.Count);
            List <BoolExpr <T_Identifier> >            boolExprList2 = new List <BoolExpr <T_Identifier> >(tree.Children.Count);

            foreach (BoolExpr <T_Identifier> boolExpr in boolExprList1)
            {
                switch (boolExpr.ExprType)
                {
                case ExprType.Not:
                    dictionary[((NotExpr <T_Identifier>)boolExpr).Child] = true;
                    continue;

                case ExprType.True:
                    if (!flag)
                    {
                        return((BoolExpr <T_Identifier>)TrueExpr <T_Identifier> .Value);
                    }
                    continue;

                case ExprType.False:
                    if (flag)
                    {
                        return((BoolExpr <T_Identifier>)FalseExpr <T_Identifier> .Value);
                    }
                    continue;

                default:
                    boolExprList2.Add(boolExpr);
                    continue;
                }
            }
            List <BoolExpr <T_Identifier> > boolExprList3 = new List <BoolExpr <T_Identifier> >();

            foreach (BoolExpr <T_Identifier> key in boolExprList2)
            {
                if (dictionary.ContainsKey(key))
                {
                    if (flag)
                    {
                        return((BoolExpr <T_Identifier>)FalseExpr <T_Identifier> .Value);
                    }
                    return((BoolExpr <T_Identifier>)TrueExpr <T_Identifier> .Value);
                }
                boolExprList3.Add(key);
            }
            foreach (BoolExpr <T_Identifier> key in dictionary.Keys)
            {
                boolExprList3.Add(key.MakeNegated());
            }
            if (boolExprList3.Count == 0)
            {
                if (flag)
                {
                    return((BoolExpr <T_Identifier>)TrueExpr <T_Identifier> .Value);
                }
                return((BoolExpr <T_Identifier>)FalseExpr <T_Identifier> .Value);
            }
            if (1 == boolExprList3.Count)
            {
                return(boolExprList3[0]);
            }
            return(!flag ? (BoolExpr <T_Identifier>) new OrExpr <T_Identifier>((IEnumerable <BoolExpr <T_Identifier> >)boolExprList3) : (BoolExpr <T_Identifier>) new AndExpr <T_Identifier>((IEnumerable <BoolExpr <T_Identifier> >)boolExprList3));
        }