Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Common / CommandTrees / Internal / PatternMatchRules.cs / 1305376 / PatternMatchRules.cs
//---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- using System.Data.Common.CommandTrees; using System.Collections.Generic; using System.Data.Metadata.Edm; using System.Diagnostics; using System.Data.Common.Utils; using System.Linq; using System.Globalization; using System.Data.Common.CommandTrees.ExpressionBuilder; namespace System.Data.Common.CommandTrees.Internal { ////// PatternMatchRule is a specialization of internal class PatternMatchRule : DbExpressionRule { private readonly Functhat uses a Func<DbExpression, bool> 'pattern' /// to implement and a Func<DbExpression, DbExpression> 'processor' to implement /// . The 'processor' should return null to indicate that the expression was not /// successfully processed, otherwise it should return the new result expression. ///isMatch; private readonly Func process; private readonly ProcessedAction processed; private PatternMatchRule(Func matchFunc, Func processor, ProcessedAction onProcessed) { this.isMatch = matchFunc; this.process = processor; this.processed = onProcessed; } internal override bool ShouldProcess(DbExpression expression) { return this.isMatch(expression); } internal override bool TryProcess(DbExpression expression, out DbExpression result) { result = this.process(expression); return (result != null); } internal override ProcessedAction OnExpressionProcessed { get { return this.processed; } } /// /// Constructs a new PatternMatch rule with the specified pattern, processor and default internal static PatternMatchRule Create(Funcof /// matchFunc, Func processor) { return PatternMatchRule.Create(matchFunc, processor, ProcessedAction.Reset); } /// /// Constructs a new PatternMatchRule with the specified pattern, processor and internal static PatternMatchRule Create(Func/// matchFunc, Func processor, ProcessedAction onProcessed) { EntityUtil.CheckArgumentNull(matchFunc, "matchFunc"); EntityUtil.CheckArgumentNull(processor, "processor"); return new PatternMatchRule(matchFunc, processor, onProcessed); } } /// /// PatternMatchRuleProcessor is a specialization of internal class PatternMatchRuleProcessor : DbExpressionRuleProcessingVisitor { private readonly System.Collections.ObjectModel.ReadOnlyCollectionthat uses a collection of s /// as its ruleset. The static Create methods can be used to construct a new PatternMatchRuleProcessor that applies the specified PatternMatchRules, which is /// returned as a Func<DbExpression, DbExpression> that can be invoked directly on an expression to apply the ruleset to it. /// ruleSet; private PatternMatchRuleProcessor(System.Collections.ObjectModel.ReadOnlyCollection rules) { Debug.Assert(rules.Count() != 0, "At least one PatternMatchRule is required"); Debug.Assert(rules.Where(r => r == null).Count() == 0, "Individual PatternMatchRules must not be null"); this.ruleSet = rules; } private DbExpression Process(DbExpression expression) { EntityUtil.CheckArgumentNull(expression, "expression"); expression = this.VisitExpression(expression); return expression; } protected override IEnumerable GetRules() { return this.ruleSet; } internal static Func Create(params PatternMatchRule[] rules) { EntityUtil.CheckArgumentNull(rules, "rules"); return new PatternMatchRuleProcessor(new System.Collections.ObjectModel.ReadOnlyCollection (rules)).Process; } } /// /// Provides a means of constructing Func<DbExpression, bool> 'patterns' for use with internal static class Patterns { #region Pattern Combinators ///s. /// /// Constructs a new pattern that is matched iff both internal static Funcand are matched. Does NOT return a pattern that matches . Use with an argument of to match an AND expression /// And(Func pattern1, Func pattern2) { return (e => pattern1(e) && pattern2(e)); } /// /// Constructs a new pattern that is matched iff all of internal static Func, and are matched. Does NOT return a pattern that matches . Use with an argument of to match an AND expression /// And(Func pattern1, Func pattern2, Func pattern3) { return (e => pattern1(e) && pattern2(e) && pattern3(e)); } /// /// Constructs a new pattern that is matched if either internal static Funcor are matched. Does NOT return a pattern that matches . Use with an argument of to match an OR expression /// Or(Func pattern1, Func pattern2) { return (e => pattern1(e) || pattern2(e)); } /// /// Constructs a new pattern that is matched if either internal static Func, or are matched. Does NOT return a pattern that matches . Use with an argument of to match an OR expression /// Or(Func pattern1, Func pattern2, Func pattern3) { return (e => pattern1(e) || pattern2(e) || pattern3(e)); } #if _ENABLE_UNUSED_PATTERNS_ /// /// Constructs a new pattern that is matched iff the argument pattern is not matched. Does NOT return a pattern that matches internal static Func. Use with an argument of to match a NOT expression /// Not(Func pattern) { return (e => !pattern(e)); } #endif #endregion #region Constant Patterns /// /// Returns a pattern that will match any expression, returning internal static Functrue for any argument, including null. ///AnyExpression { get { return (e => true); } } /// /// Returns a pattern that will match any collection of expressions, returning internal static Functrue for any argument, including a null or empty enumerable. ///, bool> AnyExpressions { get { return (elems => true); } } #endregion #region Result Type Patterns #if _ENABLE_UNUSED_PATTERNS_ /// /// Returns a pattern that is matched if the the argument has a Boolean result type /// internal static FuncMatchBooleanType { get { return (e => TypeSemantics.IsBooleanType(e.ResultType)); } } #endif /// /// Returns a pattern that is matched if the argument has a complex result type /// internal static FuncMatchComplexType { get { return (e => TypeSemantics.IsComplexType(e.ResultType)); } } /// /// Returns a pattern that is matched if the argument has an entity result type /// internal static FuncMatchEntityType { get { return (e => TypeSemantics.IsEntityType(e.ResultType)); } } /// /// Returns a pattern that is matched if the argument has a row result type /// internal static FuncMatchRowType { get { return (e => TypeSemantics.IsRowType(e.ResultType)); } } #endregion #region General Patterns /// /// Constructs a new pattern that will match an expression with the specified internal static Func. /// MatchKind(DbExpressionKind kindToMatch) { return (e => e.ExpressionKind == kindToMatch); } /// /// Constructs a new pattern that will match iff the specified pattern argument is matched for all expressions in the collection argument. /// internal static Func, bool> MatchForAll(Func elementPattern) { return (elems => elems.FirstOrDefault(e => !elementPattern(e)) == null); } #if _ENABLE_UNUSED_PATTERNS_ /// /// Constructs a new pattern that will match if the specified pattern argument is matched for any expression in the collection argument. /// internal static Func, bool> MatchForAny(Func elementPattern) { return (elems => elems.FirstOrDefault(e => elementPattern(e)) != null); } #endif #endregion #region Type-specific Patterns #if _ENABLE_UNUSED_PATTERNS_ /// /// Returns a pattern that is matched if the argument expression is a internal static Func/// MatchUnary() { return (e => e is DbUnaryExpression); } /// /// Constructs a new pattern that is matched iff the argument expression is a internal static Funcand matches /// MatchUnary(Func argumentPattern) { return (e => (e is DbUnaryExpression) && argumentPattern(((DbUnaryExpression)e).Argument)); } #endif /// /// Returns a pattern that is matched if the argument expression is a internal static Func/// MatchBinary() { return (e => e is DbBinaryExpression); } #if _ENABLE_UNUSED_PATTERNS_ /// /// Constructs a new pattern that is matched iff the argument expression is a internal static Funcwith left and right subexpressions that match the corresponding and patterns /// MatchBinary(Func leftPattern, Func rightPattern) { return (e => { DbBinaryExpression binEx = (e as DbBinaryExpression); return (binEx != null && leftPattern(binEx.Left) && rightPattern(binEx.Right)); }); } #endif /// /// Constructs a new pattern that is matched iff the argument expression is a internal static Funcwith input and predicate subexpressions that match the corresponding and patterns /// MatchFilter(Func inputPattern, Func predicatePattern) { return (e => { if (e.ExpressionKind != DbExpressionKind.Filter) { return false; } else { DbFilterExpression filterEx = (DbFilterExpression)e; return inputPattern(filterEx.Input.Expression) && predicatePattern(filterEx.Predicate); } }); } /// /// Constructs a new pattern that is matched iff the argument expression is a internal static Funcwith input and projection subexpressions that match the corresponding and patterns /// MatchProject(Func inputPattern, Func projectionPattern) { return (e => { if (e.ExpressionKind != DbExpressionKind.Project) { return false; } else { DbProjectExpression projectEx = (DbProjectExpression)e; return inputPattern(projectEx.Input.Expression) && projectionPattern(projectEx.Projection); } }); } /// /// Constructs a new pattern that is matched iff the argument expression is a internal static Funcwith 'when' and 'then' subexpression lists that match the specified and collection patterns and an 'else' subexpression that matches the specified expression pattern /// MatchCase(Func , bool> whenPattern, Func , bool> thenPattern, Func elsePattern) { return (e => { if (e.ExpressionKind != DbExpressionKind.Case) { return false; } else { DbCaseExpression caseEx = (DbCaseExpression)e; return whenPattern(caseEx.When) && thenPattern(caseEx.Then) && elsePattern(caseEx.Else); } }); } /// /// Gets a pattern that is matched if the argument expression is a internal static Func. This property can be used instead of repeated calls to with an argument of /// MatchNewInstance() { return (e => e.ExpressionKind == DbExpressionKind.NewInstance); } /// /// Constructs a new pattern that is matched iff the argument expression is a internal static Funcwith arguments that match the specified collection pattern /// MatchNewInstance(Func , bool> argumentsPattern) { return (e => { if (e.ExpressionKind != DbExpressionKind.NewInstance) { return false; } else { DbNewInstanceExpression newInst = (DbNewInstanceExpression)e; return argumentsPattern(newInst.Arguments); } }); } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- using System.Data.Common.CommandTrees; using System.Collections.Generic; using System.Data.Metadata.Edm; using System.Diagnostics; using System.Data.Common.Utils; using System.Linq; using System.Globalization; using System.Data.Common.CommandTrees.ExpressionBuilder; namespace System.Data.Common.CommandTrees.Internal { ////// PatternMatchRule is a specialization of internal class PatternMatchRule : DbExpressionRule { private readonly Functhat uses a Func<DbExpression, bool> 'pattern' /// to implement and a Func<DbExpression, DbExpression> 'processor' to implement /// . The 'processor' should return null to indicate that the expression was not /// successfully processed, otherwise it should return the new result expression. ///isMatch; private readonly Func process; private readonly ProcessedAction processed; private PatternMatchRule(Func matchFunc, Func processor, ProcessedAction onProcessed) { this.isMatch = matchFunc; this.process = processor; this.processed = onProcessed; } internal override bool ShouldProcess(DbExpression expression) { return this.isMatch(expression); } internal override bool TryProcess(DbExpression expression, out DbExpression result) { result = this.process(expression); return (result != null); } internal override ProcessedAction OnExpressionProcessed { get { return this.processed; } } /// /// Constructs a new PatternMatch rule with the specified pattern, processor and default internal static PatternMatchRule Create(Funcof /// matchFunc, Func processor) { return PatternMatchRule.Create(matchFunc, processor, ProcessedAction.Reset); } /// /// Constructs a new PatternMatchRule with the specified pattern, processor and internal static PatternMatchRule Create(Func/// matchFunc, Func processor, ProcessedAction onProcessed) { EntityUtil.CheckArgumentNull(matchFunc, "matchFunc"); EntityUtil.CheckArgumentNull(processor, "processor"); return new PatternMatchRule(matchFunc, processor, onProcessed); } } /// /// PatternMatchRuleProcessor is a specialization of internal class PatternMatchRuleProcessor : DbExpressionRuleProcessingVisitor { private readonly System.Collections.ObjectModel.ReadOnlyCollectionthat uses a collection of s /// as its ruleset. The static Create methods can be used to construct a new PatternMatchRuleProcessor that applies the specified PatternMatchRules, which is /// returned as a Func<DbExpression, DbExpression> that can be invoked directly on an expression to apply the ruleset to it. /// ruleSet; private PatternMatchRuleProcessor(System.Collections.ObjectModel.ReadOnlyCollection rules) { Debug.Assert(rules.Count() != 0, "At least one PatternMatchRule is required"); Debug.Assert(rules.Where(r => r == null).Count() == 0, "Individual PatternMatchRules must not be null"); this.ruleSet = rules; } private DbExpression Process(DbExpression expression) { EntityUtil.CheckArgumentNull(expression, "expression"); expression = this.VisitExpression(expression); return expression; } protected override IEnumerable GetRules() { return this.ruleSet; } internal static Func Create(params PatternMatchRule[] rules) { EntityUtil.CheckArgumentNull(rules, "rules"); return new PatternMatchRuleProcessor(new System.Collections.ObjectModel.ReadOnlyCollection (rules)).Process; } } /// /// Provides a means of constructing Func<DbExpression, bool> 'patterns' for use with internal static class Patterns { #region Pattern Combinators ///s. /// /// Constructs a new pattern that is matched iff both internal static Funcand are matched. Does NOT return a pattern that matches . Use with an argument of to match an AND expression /// And(Func pattern1, Func pattern2) { return (e => pattern1(e) && pattern2(e)); } /// /// Constructs a new pattern that is matched iff all of internal static Func, and are matched. Does NOT return a pattern that matches . Use with an argument of to match an AND expression /// And(Func pattern1, Func pattern2, Func pattern3) { return (e => pattern1(e) && pattern2(e) && pattern3(e)); } /// /// Constructs a new pattern that is matched if either internal static Funcor are matched. Does NOT return a pattern that matches . Use with an argument of to match an OR expression /// Or(Func pattern1, Func pattern2) { return (e => pattern1(e) || pattern2(e)); } /// /// Constructs a new pattern that is matched if either internal static Func, or are matched. Does NOT return a pattern that matches . Use with an argument of to match an OR expression /// Or(Func pattern1, Func pattern2, Func pattern3) { return (e => pattern1(e) || pattern2(e) || pattern3(e)); } #if _ENABLE_UNUSED_PATTERNS_ /// /// Constructs a new pattern that is matched iff the argument pattern is not matched. Does NOT return a pattern that matches internal static Func. Use with an argument of to match a NOT expression /// Not(Func pattern) { return (e => !pattern(e)); } #endif #endregion #region Constant Patterns /// /// Returns a pattern that will match any expression, returning internal static Functrue for any argument, including null. ///AnyExpression { get { return (e => true); } } /// /// Returns a pattern that will match any collection of expressions, returning internal static Functrue for any argument, including a null or empty enumerable. ///, bool> AnyExpressions { get { return (elems => true); } } #endregion #region Result Type Patterns #if _ENABLE_UNUSED_PATTERNS_ /// /// Returns a pattern that is matched if the the argument has a Boolean result type /// internal static FuncMatchBooleanType { get { return (e => TypeSemantics.IsBooleanType(e.ResultType)); } } #endif /// /// Returns a pattern that is matched if the argument has a complex result type /// internal static FuncMatchComplexType { get { return (e => TypeSemantics.IsComplexType(e.ResultType)); } } /// /// Returns a pattern that is matched if the argument has an entity result type /// internal static FuncMatchEntityType { get { return (e => TypeSemantics.IsEntityType(e.ResultType)); } } /// /// Returns a pattern that is matched if the argument has a row result type /// internal static FuncMatchRowType { get { return (e => TypeSemantics.IsRowType(e.ResultType)); } } #endregion #region General Patterns /// /// Constructs a new pattern that will match an expression with the specified internal static Func. /// MatchKind(DbExpressionKind kindToMatch) { return (e => e.ExpressionKind == kindToMatch); } /// /// Constructs a new pattern that will match iff the specified pattern argument is matched for all expressions in the collection argument. /// internal static Func, bool> MatchForAll(Func elementPattern) { return (elems => elems.FirstOrDefault(e => !elementPattern(e)) == null); } #if _ENABLE_UNUSED_PATTERNS_ /// /// Constructs a new pattern that will match if the specified pattern argument is matched for any expression in the collection argument. /// internal static Func, bool> MatchForAny(Func elementPattern) { return (elems => elems.FirstOrDefault(e => elementPattern(e)) != null); } #endif #endregion #region Type-specific Patterns #if _ENABLE_UNUSED_PATTERNS_ /// /// Returns a pattern that is matched if the argument expression is a internal static Func/// MatchUnary() { return (e => e is DbUnaryExpression); } /// /// Constructs a new pattern that is matched iff the argument expression is a internal static Funcand matches /// MatchUnary(Func argumentPattern) { return (e => (e is DbUnaryExpression) && argumentPattern(((DbUnaryExpression)e).Argument)); } #endif /// /// Returns a pattern that is matched if the argument expression is a internal static Func/// MatchBinary() { return (e => e is DbBinaryExpression); } #if _ENABLE_UNUSED_PATTERNS_ /// /// Constructs a new pattern that is matched iff the argument expression is a internal static Funcwith left and right subexpressions that match the corresponding and patterns /// MatchBinary(Func leftPattern, Func rightPattern) { return (e => { DbBinaryExpression binEx = (e as DbBinaryExpression); return (binEx != null && leftPattern(binEx.Left) && rightPattern(binEx.Right)); }); } #endif /// /// Constructs a new pattern that is matched iff the argument expression is a internal static Funcwith input and predicate subexpressions that match the corresponding and patterns /// MatchFilter(Func inputPattern, Func predicatePattern) { return (e => { if (e.ExpressionKind != DbExpressionKind.Filter) { return false; } else { DbFilterExpression filterEx = (DbFilterExpression)e; return inputPattern(filterEx.Input.Expression) && predicatePattern(filterEx.Predicate); } }); } /// /// Constructs a new pattern that is matched iff the argument expression is a internal static Funcwith input and projection subexpressions that match the corresponding and patterns /// MatchProject(Func inputPattern, Func projectionPattern) { return (e => { if (e.ExpressionKind != DbExpressionKind.Project) { return false; } else { DbProjectExpression projectEx = (DbProjectExpression)e; return inputPattern(projectEx.Input.Expression) && projectionPattern(projectEx.Projection); } }); } /// /// Constructs a new pattern that is matched iff the argument expression is a internal static Funcwith 'when' and 'then' subexpression lists that match the specified and collection patterns and an 'else' subexpression that matches the specified expression pattern /// MatchCase(Func , bool> whenPattern, Func , bool> thenPattern, Func elsePattern) { return (e => { if (e.ExpressionKind != DbExpressionKind.Case) { return false; } else { DbCaseExpression caseEx = (DbCaseExpression)e; return whenPattern(caseEx.When) && thenPattern(caseEx.Then) && elsePattern(caseEx.Else); } }); } /// /// Gets a pattern that is matched if the argument expression is a internal static Func. This property can be used instead of repeated calls to with an argument of /// MatchNewInstance() { return (e => e.ExpressionKind == DbExpressionKind.NewInstance); } /// /// Constructs a new pattern that is matched iff the argument expression is a internal static Funcwith arguments that match the specified collection pattern /// MatchNewInstance(Func , bool> argumentsPattern) { return (e => { if (e.ExpressionKind != DbExpressionKind.NewInstance) { return false; } else { DbNewInstanceExpression newInst = (DbNewInstanceExpression)e; return argumentsPattern(newInst.Arguments); } }); } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SafeNativeMethods.cs
- OrderingInfo.cs
- ListenerConnectionDemuxer.cs
- XPathNodeIterator.cs
- codemethodreferenceexpression.cs
- CapiHashAlgorithm.cs
- TextEditor.cs
- UriSection.cs
- ActiveDocumentEvent.cs
- OwnerDrawPropertyBag.cs
- CharEnumerator.cs
- CodeRegionDirective.cs
- GradientBrush.cs
- OutputCacheProviderCollection.cs
- GradientSpreadMethodValidation.cs
- AsyncPostBackErrorEventArgs.cs
- ExpressionPrefixAttribute.cs
- QuaternionAnimationBase.cs
- DoubleLinkListEnumerator.cs
- SafeHandle.cs
- TextRunCache.cs
- IRCollection.cs
- IdentifierService.cs
- XmlSchemaComplexContentExtension.cs
- SmiRequestExecutor.cs
- CodeLabeledStatement.cs
- DataGridViewBindingCompleteEventArgs.cs
- DataControlFieldCollection.cs
- OptimalTextSource.cs
- SqlDataSourceCache.cs
- UpdateCompiler.cs
- HttpContext.cs
- CrossContextChannel.cs
- RowToParametersTransformer.cs
- CultureSpecificCharacterBufferRange.cs
- String.cs
- OdbcCommand.cs
- RequestCacheManager.cs
- StylusEditingBehavior.cs
- LayoutInformation.cs
- WindowsGraphicsCacheManager.cs
- DataGridViewCellStyle.cs
- DataGridViewColumnCollectionEditor.cs
- PageVisual.cs
- XmlSignificantWhitespace.cs
- SrgsDocument.cs
- SessionSwitchEventArgs.cs
- ToolStripStatusLabel.cs
- SafeLocalMemHandle.cs
- IPGlobalProperties.cs
- TextBoxRenderer.cs
- Formatter.cs
- StyleHelper.cs
- DataGridViewCellStateChangedEventArgs.cs
- LongValidator.cs
- ObjectSet.cs
- SelectionHighlightInfo.cs
- WorkflowStateRollbackService.cs
- OracleParameterBinding.cs
- Hex.cs
- InteropAutomationProvider.cs
- OleStrCAMarshaler.cs
- HttpVersion.cs
- oledbmetadatacollectionnames.cs
- Metadata.cs
- SoapObjectInfo.cs
- DataMember.cs
- RadioButton.cs
- ColumnCollectionEditor.cs
- ModuleElement.cs
- LayoutUtils.cs
- TextStore.cs
- HttpApplication.cs
- LabelInfo.cs
- _Win32.cs
- PassportPrincipal.cs
- ModifierKeysValueSerializer.cs
- CommonProperties.cs
- TextElement.cs
- EntityCommand.cs
- SqlServices.cs
- ScrollBar.cs
- TextAutomationPeer.cs
- TimeSpanValidatorAttribute.cs
- HttpTransportElement.cs
- PerformanceCounterPermission.cs
- CredentialCache.cs
- EntityType.cs
- CallbackValidatorAttribute.cs
- WeakKeyDictionary.cs
- ColorPalette.cs
- InfoCardServiceInstallComponent.cs
- ErrorFormatter.cs
- SchemaContext.cs
- DelegateCompletionCallbackWrapper.cs
- CallId.cs
- CellIdBoolean.cs
- PriorityItem.cs
- ProtocolImporter.cs
- BitmapEffectRenderDataResource.cs