CSharp: Interpreter Pattern in donet core 3

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/// <summary>
   ///  解释器模式 Interpreter Pattern
   ///  geovindu, Geovin Du edit
   /// </summary>
   interface Employee
   {
       /// <summary>
       /// 是否解释
       /// </summary>
       /// <param name="context">输入背景</param>
       /// <returns></returns>
       bool Interpret(Context context);
   }
   /// <summary>
   /// IndividualEmployee class
   /// 单独雇员
   /// </summary>
   class IndividualEmployee : Employee
   {
       /// <summary>
       /// 年经验
       /// </summary>
       private int yearOfExperience;
       /// <summary>
       /// 当前等级
       /// </summary>
       private string currentGrade;
       /// <summary>
       ///
       /// </summary>
       /// <param name="experience">经验年数</param>
       /// <param name="grade">等级</param>
       public IndividualEmployee(int experience, string grade)
       {
           this.yearOfExperience = experience;
           this.currentGrade = grade;
       }
       /// <summary>
       /// 是否解释
       /// </summary>
       /// <param name="context">输入背景</param>
       /// <returns></returns>
       public bool Interpret(Context context)
       {
           if (this.yearOfExperience >= context.GetYearofExperience()
                && context.GetPermissibleGrades().Contains(this.currentGrade))
           {
               return true;
           }
           return false;
       }
   }
   /// <summary>
   /// OrExpression class
   /// 或经验
   /// </summary>
   class OrExpression : Employee
   {
       private Employee emp1;
       private Employee emp2;
       /// <summary>
       ///
       /// </summary>
       /// <param name="emp1">输入雇员</param>
       /// <param name="emp2">输入雇员</param>
       public OrExpression(Employee emp1, Employee emp2)
       {
           this.emp1 = emp1;
           this.emp2 = emp2;
       }
       /// <summary>
       /// 是否解释
       /// </summary>
       /// <param name="context">输入背景</param>
       /// <returns></returns>
       public bool Interpret(Context context)
       {
           return emp1.Interpret(context) || emp2.Interpret(context);
       }
   }
   /// <summary>
   /// AndExpression class
   /// 添加经验
   /// </summary>
   class AndExpression : Employee
   {
       private Employee emp1;
       private Employee emp2;
 
       /// <summary>
       /// 添加经验
       /// </summary>
       /// <param name="emp1">输入雇员</param>
       /// <param name="emp2">输入雇员</param>
       public AndExpression(Employee emp1, Employee emp2)
       {
           this.emp1 = emp1;
           this.emp2 = emp2;
       }
       /// <summary>
       /// 是否解释
       /// </summary>
       /// <param name="context">输入背景</param>
       /// <returns></returns>
       public bool Interpret(Context context)
       {
           return emp1.Interpret(context) && emp2.Interpret(context);
       }
   }
   /// <summary>
   /// NotExpression class
   /// 没有经验
   /// </summary>
   class NotExpression : Employee
   {
       private Employee emp;
 
       /// <summary>
       /// 构造
       /// </summary>
       /// <param name="expr">输入雇员</param>
       public NotExpression(Employee expr)
       {
           this.emp = expr;
       }
       /// <summary>
       /// 是否解释
       /// </summary>
       /// <param name="context">输入背景</param>
       /// <returns></returns>
       public bool Interpret(Context context)
       {
           return !emp.Interpret(context);
       }
   }
   /// <summary>
   /// Context class
   ///背景
   /// </summary>
   class Context
   {
       /// <summary>
       /// 晋升所需的经验
       /// </summary>
       private int experienceReqdForPromotion;
       /// <summary>
       /// 允许成绩列表
       /// </summary>
       private List<string> allowedGrades;
       /// <summary>
       ///
       /// </summary>
       /// <param name="experience">经验年数</param>
       /// <param name="allowedGrades">允许成绩列表</param>
       public Context(int experience, List<string> allowedGrades)
       {
           this.experienceReqdForPromotion = experience;
           this.allowedGrades = new List<string>();
           foreach (string grade in allowedGrades)
           {
               this.allowedGrades.Add(grade);
           }
       }
       /// <summary>
       /// 赋值经验年数
       /// </summary>
       /// <returns></returns>
       public int GetYearofExperience()
       {
           return experienceReqdForPromotion;
       }
       /// <summary>
       /// 赋值允许的成绩
       /// </summary>
       /// <returns></returns>
       public List<string> GetPermissibleGrades()
       {
           return allowedGrades;
       }
   }
   /// <summary>
   /// EmployeeBuilder class
   /// </summary>
   class EmployeeBuilder
   {
       // Building the tree
       //Complex Rule-1: emp1 and (emp2 or (emp3 or emp4))
       /// <summary>
       /// 创建规则
       /// </summary>
       /// <param name="emp1"></param>
       /// <param name="emp2"></param>
       /// <param name="emp3"></param>
       /// <param name="emp4"></param>
       /// <returns></returns>
       public Employee BuildTreeBasedOnRule1(Employee emp1, Employee emp2, Employee emp3, Employee emp4)
       {
           // emp3 or emp4
           Employee firstPhase = new OrExpression(emp3, emp4);
           // emp2 or (emp3 or emp4)
           Employee secondPhase = new OrExpression(emp2, firstPhase);
           // emp1 and (emp2 or (emp3 or emp4))
           Employee finalPhase = new AndExpression(emp1, secondPhase);
           return finalPhase;
       }
       //Complex Rule-2: emp1 or (emp2 and (not emp3 ))
 
       /// <summary>
       /// 创建规则
       /// </summary>
       /// <param name="emp1"></param>
       /// <param name="emp2"></param>
       /// <param name="emp3"></param>
       /// <returns></returns>
       public Employee BuildTreeBasedOnRule2(Employee emp1, Employee emp2, Employee emp3)
       {
           // Not emp3
           Employee firstPhase = new NotExpression(emp3);
           // emp2 or (not emp3)
           Employee secondPhase = new AndExpression(emp2, firstPhase);
           // emp1 and (emp2 or (not emp3 ))
           Employee finalPhase = new OrExpression(emp1, secondPhase);
           return finalPhase;
       }
   }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/// <summary>
 ///  解释器模式 Interpreter Pattern
 ///  geovindu, Geovin Du edit
 /// </summary>
 public interface IElement
 {
     int Value { get; }
 }
 /// <summary>
 ///
 /// </summary>
 public class Integer : IElement
 {
     public Integer(int value)
     {
         Value = value;
     }
 
     public int Value { get; }
 }
 /// <summary>
 ///
 /// </summary>
 public class BinaryOperation : IElement
 {
     public enum Type
     {
         Addition,
         Subtraction
     }
 
     public Type MyType;
     public IElement Left, Right;
 
     public int Value
     {
         get
         {
             switch (MyType)
             {
                 case Type.Addition:
                     return Left.Value + Right.Value;
                 case Type.Subtraction:
                     return Left.Value - Right.Value;
                 default:
                     throw new ArgumentOutOfRangeException();
             }
         }
     }
 }
 /// <summary>
 ///
 /// </summary>
 public class Token
 {
     public enum Type
     {
         Integer, Plus, Minus, Lparen, Rparen
     }
 
     public Type MyType;
     public string Text;
 
     public Token(Type type, string text)
     {
         MyType = type;
         Text = text;
     }
 
     public override string ToString()
     {
         return $"`{Text}`";
     }
 }
 /// <summary>
 ///
 /// </summary>
 public class GevovinDu
 {
     public static List<Token> Lex(string input)
     {
         var result = new List<Token>();
 
         for (int i = 0; i < input.Length; i++)
         {
             switch (input[i])
             {
                 case '+':
                     result.Add(new Token(Token.Type.Plus, "+"));
                     break;
                 case '-':
                     result.Add(new Token(Token.Type.Minus, "-"));
                     break;
                 case '(':
                     result.Add(new Token(Token.Type.Lparen, "("));
                     break;
                 case ')':
                     result.Add(new Token(Token.Type.Rparen, ")"));
                     break;
                 default:
                     var sb = new StringBuilder(input[i].ToString());
                     for (int j = i + 1; j < input.Length; ++j)
                     {
                         if (char.IsDigit(input[j]))
                         {
                             sb.Append(input[j]);
                             ++i;
                         }
                         else
                         {
                             result.Add(new Token(Token.Type.Integer, sb.ToString()));
                             break;
                         }
                     }
                     break;
             }
         }
 
         return result;
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="tokens"></param>
     /// <returns></returns>
     /// <exception cref="ArgumentOutOfRangeException"></exception>
     public  static IElement Parse(IReadOnlyList<Token> tokens)
     {
         var result = new BinaryOperation();
         bool haveLHS = false;
         for (int i = 0; i < tokens.Count; i++)
         {
             var token = tokens[i];
 
             // look at the type of token
             switch (token.MyType)
             {
                 case Token.Type.Integer:
                     var integer = new Integer(int.Parse(token.Text));
                     if (!haveLHS)
                     {
                         result.Left = integer;
                         haveLHS = true;
                     }
                     else
                     {
                         result.Right = integer;
                     }
                     break;
                 case Token.Type.Plus:
                     result.MyType = BinaryOperation.Type.Addition;
                     break;
                 case Token.Type.Minus:
                     result.MyType = BinaryOperation.Type.Subtraction;
                     break;
                 case Token.Type.Lparen:
                     int j = i;
                     for (; j < tokens.Count; ++j)
                         if (tokens[j].MyType == Token.Type.Rparen)
                             break; // found it!
                                    // process subexpression w/o opening
                     var subexpression = tokens.Skip(i + 1).Take(j - i - 1).ToList();
                     var element = Parse(subexpression);
                     if (!haveLHS)
                     {
                         result.Left = element;
                         haveLHS = true;
                     }
                     else result.Right = element;
                     i = j; // advance
                     break;
                 default:
                     throw new ArgumentOutOfRangeException();
             }
         }
         return result;
     }
 }

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//解释器模式
Console.WriteLine("*** 解释器模式 Interpreter Pattern Demonstration-2***\n");
 
// Minimum Criteria for promoton is:
// The year of experience is minimum 10 yrs. and
// Employee grade should be either G2 or G3
List<string> allowedGrades = new List<string> { "G2", "G3" };
Context context = new Context(10, allowedGrades);
Employee emp1 = new IndividualEmployee(5, "G1");
Employee emp2 = new IndividualEmployee(10, "G2");
Employee emp3 = new IndividualEmployee(15, "G3");
Employee emp4 = new IndividualEmployee(20, "G4");
 
EmployeeBuilder builder = new EmployeeBuilder();
 
//Validating the 1st complex rule
Console.WriteLine("----- 验证第一个复杂规则.-----");
Console.WriteLine("emp1和emp2、emp3、emp4中的任何一个是否有资格晋升?"
    + builder.BuildTreeBasedOnRule1(emp1, emp2, emp3, emp4).Interpret(context));
Console.WriteLine("emp2和emp1、emp3、emp4中的任何一个是否有资格晋升?"
    + builder.BuildTreeBasedOnRule1(emp2, emp1, emp3, emp4).Interpret(context));
Console.WriteLine("emp3和emp1、emp2、emp3中的任何一个是否有资格晋升?"
    + builder.BuildTreeBasedOnRule1(emp3, emp1, emp2, emp4).Interpret(context));
Console.WriteLine("emp4和emp1、emp2、emp3中的任何一个是否有资格晋升?"
    + builder.BuildTreeBasedOnRule1(emp4, emp1, emp2, emp3).Interpret(context));
 
Console.WriteLine("-----现在验证第二个复杂的规则.-----");
//Validating the 2nd complex rule
Console.WriteLine("emp1或(emp2而不是emp3)是否有资格晋升?"
    + builder.BuildTreeBasedOnRule2(emp1, emp2, emp3).Interpret(context));
Console.WriteLine("emp2或(emp3但不是emp4)是否有资格晋升?"
    + builder.BuildTreeBasedOnRule2(emp2, emp3, emp4).Interpret(context));
 
Console.WriteLine();
 
var input = "(13+4)-(12+1)";
var tokens = GevovinDu.Lex(input);
Console.WriteLine(string.Join("\t", tokens));
 
var parsed = GevovinDu.Parse(tokens);
Console.WriteLine($"{input} = {parsed.Value}");

  

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
***?解释器模式 Interpreter Pattern Demonstration-2***
 
----- 验证第一个复杂规则.-----
emp1和emp2、emp3、emp4中的任何一个是否有资格晋升?False
emp2和emp1、emp3、emp4中的任何一个是否有资格晋升?True
emp3和emp1、emp2、emp3中的任何一个是否有资格晋升?True
emp4和emp1、emp2、emp3中的任何一个是否有资格晋升?False
-----现在验证第二个复杂的规则.-----
emp1或(emp2而不是emp3)是否有资格晋升?False
emp2或(emp3但不是emp4)是否有资格晋升?True
 
`(`     `13`    `+`     `4`     `)`     `-`     `(`     `12`    `+`     `1`     `)`
(13+4)-(12+1) = 4

  

 

posted @   ®Geovin Du Dream Park™  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2019-10-12 css: hide or dispaly div
2019-10-12 Python: simple code
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示