Visitor.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DataEntity / System / Data / Common / Utils / Boolean / Visitor.cs / 2 / Visitor.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner [....]
// @backupOwner [....] 
//--------------------------------------------------------------------- 

using System; 
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Collections.ObjectModel; 
using System.Diagnostics;
using System.Linq; 
 
namespace System.Data.Common.Utils.Boolean
{ 
    /// 
    /// Abstract visitor class. All Boolean expression nodes know how to
    /// 'accept' a visitor, and delegate to the appropriate visitor method.
    /// For instance, AndExpr invokes Visitor.VisitAnd. 
    /// 
    /// Type of leaf term identifiers in expression. 
    /// Return type for visit methods. 
    internal abstract class Visitor
    { 
        internal abstract T_Return VisitTrue(TrueExpr expression);
        internal abstract T_Return VisitFalse(FalseExpr expression);
        internal abstract T_Return VisitTerm(TermExpr expression);
        internal abstract T_Return VisitNot(NotExpr expression); 
        internal abstract T_Return VisitAnd(AndExpr expression);
        internal abstract T_Return VisitOr(OrExpr expression); 
    } 

    ///  
    /// Basic visitor which reproduces the given expression tree.
    /// 
    /// Type of leaf term identifiers in expression.
    internal abstract class BasicVisitor : Visitor> 
    {
        internal override BoolExpr VisitFalse(FalseExpr expression) { return expression; } 
        internal override BoolExpr VisitTrue(TrueExpr expression) { return expression; } 
        internal override BoolExpr VisitTerm(TermExpr expression) { return expression; }
        internal override BoolExpr VisitNot(NotExpr expression) 
        {
            return new NotExpr(expression.Child.Accept(this));
        }
        internal override BoolExpr VisitAnd(AndExpr expression) 
        {
            return new AndExpr(AcceptChildren(expression.Children)); 
        } 
        internal override BoolExpr VisitOr(OrExpr expression)
        { 
            return new OrExpr(AcceptChildren(expression.Children));
        }
        private IEnumerable> AcceptChildren(IEnumerable> children)
        { 
            foreach (BoolExpr child in children) { yield return child.Accept(this); }
        } 
    } 

    internal class TermCounter : Visitor 
    {
        static readonly TermCounter s_instance = new TermCounter();

        internal static int CountTerms(BoolExpr expression) 
        {
            Debug.Assert(null != expression); 
            return expression.Accept(s_instance); 
        }
 
        internal override int VisitTrue(TrueExpr expression)
        {
            return 0;
        } 

        internal override int VisitFalse(FalseExpr expression) 
        { 
            return 0;
        } 

        internal override int VisitTerm(TermExpr expression)
        {
            return 1; 
        }
 
        internal override int VisitNot(NotExpr expression) 
        {
            return expression.Child.Accept(this); 
        }

        internal override int VisitAnd(AndExpr expression)
        { 
            return VisitTree(expression);
        } 
 
        internal override int VisitOr(OrExpr expression)
        { 
            return VisitTree(expression);
        }

        private int VisitTree(TreeExpr expression) 
        {
            int sum = 0; 
            foreach (var child in expression.Children) 
            {
                sum += child.Accept(this); 
            }
            return sum;
        }
    } 

    ///  
    /// A Visitor class that returns all the leaves in a boolean expression 
    /// 
    /// Type of leaf term identifiers in expression. 
    internal class LeafVisitor : Visitor
    {
        readonly List> _terms;
 
        private LeafVisitor()
        { 
            _terms = new List>(); 
        }
 
        internal static List> GetTerms(BoolExpr expression)
        {
            Debug.Assert(null != expression, "expression must be given");
            LeafVisitor visitor = new LeafVisitor(); 
            expression.Accept(visitor);
            return visitor._terms; 
        } 

        internal static IEnumerable GetLeaves(BoolExpr expression) 
        {
            return GetTerms(expression).Select(term => term.Identifier);
        }
 
        internal override bool VisitTrue(TrueExpr expression)
        { 
            return true; 
        }
 
        internal override bool VisitFalse(FalseExpr expression)
        {
            return true;
        } 

        internal override bool VisitTerm(TermExpr expression) 
        { 
            _terms.Add(expression);
            return true; 
        }

        internal override bool VisitNot(NotExpr expression)
        { 
            return expression.Child.Accept(this);
        } 
 
        internal override bool VisitAnd(AndExpr expression)
        { 
            return VisitTree(expression);
        }

        internal override bool VisitOr(OrExpr expression) 
        {
            return VisitTree(expression); 
        } 

        private bool VisitTree(TreeExpr expression) 
        {
            foreach (BoolExpr child in expression.Children)
            {
                child.Accept(this); 
            }
            return true; 
        } 
    }
 
    /// 
    /// Rewrites the terms in a Boolean expression tree.
    /// 
    /// Term type for leaf nodes of input 
    /// Term type for leaf nodes of output
    internal class BooleanExpressionTermRewriter : Visitor> 
    { 
        private readonly Func, BoolExpr> _translator;
 
        /// 
        /// Initialize a new translator
        /// 
        /// Translator delegate; must not be null 
        internal BooleanExpressionTermRewriter(Func, BoolExpr> translator)
        { 
            Debug.Assert(null != translator); 
            _translator = translator;
        } 

        internal override BoolExpr VisitFalse(FalseExpr expression)
        {
            return FalseExpr.Value; 
        }
 
        internal override BoolExpr VisitTrue(TrueExpr expression) 
        {
            return TrueExpr.Value; 
        }

        internal override BoolExpr VisitNot(NotExpr expression)
        { 
            return new NotExpr(expression.Child.Accept(this));
        } 
 
        internal override BoolExpr VisitTerm(TermExpr expression)
        { 
            return _translator(expression);
        }

        internal override BoolExpr VisitAnd(AndExpr expression) 
        {
            return new AndExpr(VisitChildren(expression)); 
        } 

        internal override BoolExpr VisitOr(OrExpr expression) 
        {
            return new OrExpr(VisitChildren(expression));
        }
 
        private IEnumerable> VisitChildren(TreeExpr expression)
        { 
            foreach (BoolExpr child in expression.Children) 
            {
                yield return child.Accept(this); 
            }
        }
    }
 
    /// 
    /// Converts a BoolExpr to a Vertex within a solver. 
    ///  
    internal class ToDecisionDiagramConverter : Visitor
    { 
        private readonly ConversionContext _context;

        private ToDecisionDiagramConverter(ConversionContext context)
        { 
            Debug.Assert(null != context, "must provide a context");
            _context = context; 
        } 

        internal static Vertex TranslateToRobdd(BoolExpr expr, ConversionContext context) 
        {
            Debug.Assert(null != expr, "must provide an expression");
            ToDecisionDiagramConverter converter =
                new ToDecisionDiagramConverter(context); 
            return expr.Accept(converter);
        } 
 
        internal override Vertex VisitTrue(TrueExpr expression)
        { 
            return Vertex.One;
        }

        internal override Vertex VisitFalse(FalseExpr expression) 
        {
            return Vertex.Zero; 
        } 

        internal override Vertex VisitTerm(TermExpr expression) 
        {
            return _context.TranslateTermToVertex(expression);
        }
 
        internal override Vertex VisitNot(NotExpr expression)
        { 
            return _context.Solver.Not(expression.Child.Accept(this)); 
        }
 
        internal override Vertex VisitAnd(AndExpr expression)
        {
            return _context.Solver.And(expression.Children.Select(child => child.Accept(this)));
        } 

        internal override Vertex VisitOr(OrExpr expression) 
        { 
            return _context.Solver.Or(expression.Children.Select(child => child.Accept(this)));
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner [....]
// @backupOwner [....] 
//--------------------------------------------------------------------- 

using System; 
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Collections.ObjectModel; 
using System.Diagnostics;
using System.Linq; 
 
namespace System.Data.Common.Utils.Boolean
{ 
    /// 
    /// Abstract visitor class. All Boolean expression nodes know how to
    /// 'accept' a visitor, and delegate to the appropriate visitor method.
    /// For instance, AndExpr invokes Visitor.VisitAnd. 
    /// 
    /// Type of leaf term identifiers in expression. 
    /// Return type for visit methods. 
    internal abstract class Visitor
    { 
        internal abstract T_Return VisitTrue(TrueExpr expression);
        internal abstract T_Return VisitFalse(FalseExpr expression);
        internal abstract T_Return VisitTerm(TermExpr expression);
        internal abstract T_Return VisitNot(NotExpr expression); 
        internal abstract T_Return VisitAnd(AndExpr expression);
        internal abstract T_Return VisitOr(OrExpr expression); 
    } 

    ///  
    /// Basic visitor which reproduces the given expression tree.
    /// 
    /// Type of leaf term identifiers in expression.
    internal abstract class BasicVisitor : Visitor> 
    {
        internal override BoolExpr VisitFalse(FalseExpr expression) { return expression; } 
        internal override BoolExpr VisitTrue(TrueExpr expression) { return expression; } 
        internal override BoolExpr VisitTerm(TermExpr expression) { return expression; }
        internal override BoolExpr VisitNot(NotExpr expression) 
        {
            return new NotExpr(expression.Child.Accept(this));
        }
        internal override BoolExpr VisitAnd(AndExpr expression) 
        {
            return new AndExpr(AcceptChildren(expression.Children)); 
        } 
        internal override BoolExpr VisitOr(OrExpr expression)
        { 
            return new OrExpr(AcceptChildren(expression.Children));
        }
        private IEnumerable> AcceptChildren(IEnumerable> children)
        { 
            foreach (BoolExpr child in children) { yield return child.Accept(this); }
        } 
    } 

    internal class TermCounter : Visitor 
    {
        static readonly TermCounter s_instance = new TermCounter();

        internal static int CountTerms(BoolExpr expression) 
        {
            Debug.Assert(null != expression); 
            return expression.Accept(s_instance); 
        }
 
        internal override int VisitTrue(TrueExpr expression)
        {
            return 0;
        } 

        internal override int VisitFalse(FalseExpr expression) 
        { 
            return 0;
        } 

        internal override int VisitTerm(TermExpr expression)
        {
            return 1; 
        }
 
        internal override int VisitNot(NotExpr expression) 
        {
            return expression.Child.Accept(this); 
        }

        internal override int VisitAnd(AndExpr expression)
        { 
            return VisitTree(expression);
        } 
 
        internal override int VisitOr(OrExpr expression)
        { 
            return VisitTree(expression);
        }

        private int VisitTree(TreeExpr expression) 
        {
            int sum = 0; 
            foreach (var child in expression.Children) 
            {
                sum += child.Accept(this); 
            }
            return sum;
        }
    } 

    ///  
    /// A Visitor class that returns all the leaves in a boolean expression 
    /// 
    /// Type of leaf term identifiers in expression. 
    internal class LeafVisitor : Visitor
    {
        readonly List> _terms;
 
        private LeafVisitor()
        { 
            _terms = new List>(); 
        }
 
        internal static List> GetTerms(BoolExpr expression)
        {
            Debug.Assert(null != expression, "expression must be given");
            LeafVisitor visitor = new LeafVisitor(); 
            expression.Accept(visitor);
            return visitor._terms; 
        } 

        internal static IEnumerable GetLeaves(BoolExpr expression) 
        {
            return GetTerms(expression).Select(term => term.Identifier);
        }
 
        internal override bool VisitTrue(TrueExpr expression)
        { 
            return true; 
        }
 
        internal override bool VisitFalse(FalseExpr expression)
        {
            return true;
        } 

        internal override bool VisitTerm(TermExpr expression) 
        { 
            _terms.Add(expression);
            return true; 
        }

        internal override bool VisitNot(NotExpr expression)
        { 
            return expression.Child.Accept(this);
        } 
 
        internal override bool VisitAnd(AndExpr expression)
        { 
            return VisitTree(expression);
        }

        internal override bool VisitOr(OrExpr expression) 
        {
            return VisitTree(expression); 
        } 

        private bool VisitTree(TreeExpr expression) 
        {
            foreach (BoolExpr child in expression.Children)
            {
                child.Accept(this); 
            }
            return true; 
        } 
    }
 
    /// 
    /// Rewrites the terms in a Boolean expression tree.
    /// 
    /// Term type for leaf nodes of input 
    /// Term type for leaf nodes of output
    internal class BooleanExpressionTermRewriter : Visitor> 
    { 
        private readonly Func, BoolExpr> _translator;
 
        /// 
        /// Initialize a new translator
        /// 
        /// Translator delegate; must not be null 
        internal BooleanExpressionTermRewriter(Func, BoolExpr> translator)
        { 
            Debug.Assert(null != translator); 
            _translator = translator;
        } 

        internal override BoolExpr VisitFalse(FalseExpr expression)
        {
            return FalseExpr.Value; 
        }
 
        internal override BoolExpr VisitTrue(TrueExpr expression) 
        {
            return TrueExpr.Value; 
        }

        internal override BoolExpr VisitNot(NotExpr expression)
        { 
            return new NotExpr(expression.Child.Accept(this));
        } 
 
        internal override BoolExpr VisitTerm(TermExpr expression)
        { 
            return _translator(expression);
        }

        internal override BoolExpr VisitAnd(AndExpr expression) 
        {
            return new AndExpr(VisitChildren(expression)); 
        } 

        internal override BoolExpr VisitOr(OrExpr expression) 
        {
            return new OrExpr(VisitChildren(expression));
        }
 
        private IEnumerable> VisitChildren(TreeExpr expression)
        { 
            foreach (BoolExpr child in expression.Children) 
            {
                yield return child.Accept(this); 
            }
        }
    }
 
    /// 
    /// Converts a BoolExpr to a Vertex within a solver. 
    ///  
    internal class ToDecisionDiagramConverter : Visitor
    { 
        private readonly ConversionContext _context;

        private ToDecisionDiagramConverter(ConversionContext context)
        { 
            Debug.Assert(null != context, "must provide a context");
            _context = context; 
        } 

        internal static Vertex TranslateToRobdd(BoolExpr expr, ConversionContext context) 
        {
            Debug.Assert(null != expr, "must provide an expression");
            ToDecisionDiagramConverter converter =
                new ToDecisionDiagramConverter(context); 
            return expr.Accept(converter);
        } 
 
        internal override Vertex VisitTrue(TrueExpr expression)
        { 
            return Vertex.One;
        }

        internal override Vertex VisitFalse(FalseExpr expression) 
        {
            return Vertex.Zero; 
        } 

        internal override Vertex VisitTerm(TermExpr expression) 
        {
            return _context.TranslateTermToVertex(expression);
        }
 
        internal override Vertex VisitNot(NotExpr expression)
        { 
            return _context.Solver.Not(expression.Child.Accept(this)); 
        }
 
        internal override Vertex VisitAnd(AndExpr expression)
        {
            return _context.Solver.And(expression.Children.Select(child => child.Accept(this)));
        } 

        internal override Vertex VisitOr(OrExpr expression) 
        { 
            return _context.Solver.Or(expression.Children.Select(child => child.Accept(this)));
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK