1 //---------------------------------------------------------------------
2 // <copyright file="DbExpressionRules.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
8 //---------------------------------------------------------------------
10 using System.Data.Common.CommandTrees;
11 using System.Collections.Generic;
12 using System.Data.Metadata.Edm;
13 using System.Diagnostics;
14 using System.Data.Common.Utils;
16 using System.Globalization;
17 using System.Data.Common.CommandTrees.ExpressionBuilder;
19 namespace System.Data.Common.CommandTrees.Internal
22 /// Enacapsulates the logic that defines an expression 'rule' which is capable of transforming a candidate <see cref="DbExpression"/>
23 /// into a result DbExpression, and indicating what action should be taken on that result expression by the rule application logic.
25 internal abstract class DbExpressionRule
28 /// Indicates what action the rule processor should take if the rule successfully processes an expression.
30 internal enum ProcessedAction
33 /// Continue to apply rules, from the rule immediately following this rule, to the result expression
38 /// Going back to the first rule, apply all rules to the result expression
43 /// Stop all rule processing and return the result expression as the final result expression
49 /// Indicates whether <see cref="TryProcess"/> should be called on the specified argument expression.
51 /// <param name="expression">The <see cref="DbExpression"/> that the rule should inspect and determine if processing is possible</param>
52 /// <returns><c>true</c> if the rule can attempt processing of the expression via the <see cref="TryProcess"/> method; otherwise <c>false</c></returns>
53 internal abstract bool ShouldProcess(DbExpression expression);
56 /// Attempts to process the input <paramref name="expression"/> to produce a <paramref name="result"/> <see cref="DbExpression"/>.
58 /// <param name="expression">The input expression that the rule should process</param>
59 /// <param name="result">The result expression produced by the rule if processing was successful</param>
60 /// <returns><c>true</c> if the rule was able to successfully process the input expression and produce a result expression; otherwise <c>false</c></returns>
61 internal abstract bool TryProcess(DbExpression expression, out DbExpression result);
64 /// Indicates what action - as a <see cref="ProcessedAction"/> value - the rule processor should take if <see cref="TryProcess"/> returns true.
66 internal abstract ProcessedAction OnExpressionProcessed { get; }
70 /// Abstract base class for a DbExpression visitor that can apply a collection of <see cref="DbExpressionRule"/>s during the visitor pass, returning the final result expression.
71 /// This class encapsulates the rule application logic that applies regardless of how the ruleset - modelled as the abstract <see cref="GetRules"/> method - is provided.
73 internal abstract class DbExpressionRuleProcessingVisitor : DefaultExpressionVisitor
75 protected DbExpressionRuleProcessingVisitor() { }
77 protected abstract IEnumerable<DbExpressionRule> GetRules();
79 private static Tuple<DbExpression, DbExpressionRule.ProcessedAction> ProcessRules(DbExpression expression, List<DbExpressionRule> rules)
81 // Considering each rule in the rule set in turn, if the rule indicates that it can process the
82 // input expression, call TryProcess to attempt processing. If successful, take the action specified
83 // by the rule's OnExpressionProcessed action, which may involve returning the action and the result
84 // expression so that processing can be reset or halted.
86 for (int idx = 0; idx < rules.Count; idx++)
88 DbExpressionRule currentRule = rules[idx];
89 if (currentRule.ShouldProcess(expression))
92 if (currentRule.TryProcess(expression, out result))
94 if (currentRule.OnExpressionProcessed != DbExpressionRule.ProcessedAction.Continue)
96 return Tuple.Create(result, currentRule.OnExpressionProcessed);
105 return Tuple.Create(expression, DbExpressionRule.ProcessedAction.Continue);
108 private bool _stopped;
110 private DbExpression ApplyRules(DbExpression expression)
112 // Driver loop to apply rules while the status of processing is 'Reset',
113 // or correctly set the _stopped flag if status is 'Stopped'.
115 List<DbExpressionRule> currentRules = this.GetRules().ToList();
116 var ruleResult = ProcessRules(expression, currentRules);
117 while (ruleResult.Item2 == DbExpressionRule.ProcessedAction.Reset)
119 currentRules = this.GetRules().ToList();
120 ruleResult = ProcessRules(ruleResult.Item1, currentRules);
122 if (ruleResult.Item2 == DbExpressionRule.ProcessedAction.Stop)
126 return ruleResult.Item1;
129 protected override DbExpression VisitExpression(DbExpression expression)
131 // Pre-process this visitor's rules
132 DbExpression result = ApplyRules(expression);
135 // If rule processing was stopped, the result expression must be returned immediately
139 // Visit the expression to recursively apply rules to subexpressions
140 result = base.VisitExpression(result);
143 // If rule processing was stopped, the result expression must be returned immediately
147 // Post-process the rules over the resulting expression and return the result.
148 // This is done so that rules that did not match the original structure of the
149 // expression have an opportunity to examine the structure of the result expression.
150 result = ApplyRules(result);