@@ -22,29 +22,65 @@ public class AlgorithmEngine
2222 /// 最后一个错误
2323 /// </summary>
2424 public string LastError { get ; private set ; }
25+ /// <summary>
26+ /// 保存到临时文档
27+ /// </summary>
28+ public bool UseTempDict { get ; set ; } = false ;
29+ /// <summary>
30+ /// 是否忽略大小写
31+ /// </summary>
32+ public bool IgnoreCase { get ; private set ; }
33+
2534 private ProgContext _context ;
26- protected Dictionary < string , Operand > _dict = new Dictionary < string , Operand > ( ) ;
35+ private readonly Dictionary < string , Operand > _tempdict ;
36+
37+
38+ #region 构造函数
2739 /// <summary>
28- /// 自定义 函数
40+ /// 默认不带缓存
2941 /// </summary>
30- public event Func < string , List < Operand > , Operand > DiyFunction ;
42+ public AlgorithmEngine ( )
43+ {
44+ _tempdict = new Dictionary < string , Operand > ( ) ;
45+ }
46+ /// <summary>
47+ /// 带缓存关键字大小写参数
48+ /// </summary>
49+ /// <param name="ignoreCase"></param>
50+ public AlgorithmEngine ( bool ignoreCase )
51+ {
52+ IgnoreCase = ignoreCase ;
53+ if ( ignoreCase ) {
54+ _tempdict = new Dictionary < string , Operand > ( StringComparer . OrdinalIgnoreCase ) ;
55+ } else {
56+ _tempdict = new Dictionary < string , Operand > ( ) ;
57+ }
58+ }
59+ #endregion
60+
61+
62+ #region GetParameter GetDiyParameterInside ExecuteDiyFunction
63+ private Operand GetDiyParameterInside ( string parameter )
64+ {
65+ if ( _tempdict . TryGetValue ( parameter , out Operand operand ) ) {
66+ return operand ;
67+ }
68+ var result = GetParameter ( parameter ) ;
69+ if ( UseTempDict ) {
70+ _tempdict [ parameter ] = result ;
71+ }
72+ return result ;
73+ }
3174
32- #region GetParameter
3375 /// <summary>
3476 /// 自定义参数
3577 /// </summary>
3678 /// <param name="parameter"></param>
3779 /// <returns></returns>
3880 protected virtual Operand GetParameter ( string parameter )
3981 {
40- if ( _dict . TryGetValue ( parameter , out Operand operand ) ) {
41- return operand ;
42- }
4382 return Operand . Error ( $ "Parameter [{ parameter } ] is missing.") ;
4483 }
45- #endregion
46-
47- #region ExecuteDiyFunction
4884 /// <summary>
4985 /// 自定义 函数
5086 /// </summary>
@@ -53,18 +89,8 @@ protected virtual Operand GetParameter(string parameter)
5389 /// <returns></returns>
5490 protected virtual Operand ExecuteDiyFunction ( string funcName , List < Operand > operands )
5591 {
56- if ( DiyFunction != null ) {
57- return DiyFunction . Invoke ( funcName , operands ) ;
58- }
5992 return Operand . Error ( $ "DiyFunction [{ funcName } ] is missing.") ;
6093 }
61- /// <summary>
62- /// 清理 自定义函数
63- /// </summary>
64- public void ClearDiyFunctions ( )
65- {
66- DiyFunction = null ;
67- }
6894
6995 #endregion
7096
@@ -75,7 +101,7 @@ public void ClearDiyFunctions()
75101 /// </summary>
76102 public void ClearParameters ( )
77103 {
78- _dict . Clear ( ) ;
104+ _tempdict . Clear ( ) ;
79105 }
80106
81107 /// <summary>
@@ -85,7 +111,7 @@ public void ClearParameters()
85111 /// <param name="obj"></param>
86112 public void AddParameter ( string key , Operand obj )
87113 {
88- _dict [ key ] = obj ;
114+ _tempdict [ key ] = obj ;
89115 }
90116 /// <summary>
91117 /// 添加自定义参数
@@ -94,7 +120,7 @@ public void AddParameter(string key, Operand obj)
94120 /// <param name="obj"></param>
95121 public void AddParameter ( string key , bool obj )
96122 {
97- _dict [ key ] = Operand . Create ( obj ) ;
123+ _tempdict [ key ] = Operand . Create ( obj ) ;
98124 }
99125 #region number
100126 /// <summary>
@@ -104,7 +130,7 @@ public void AddParameter(string key, bool obj)
104130 /// <param name="obj"></param>
105131 public void AddParameter ( string key , short obj )
106132 {
107- _dict [ key ] = Operand . Create ( ( int ) obj ) ;
133+ _tempdict [ key ] = Operand . Create ( ( int ) obj ) ;
108134 }
109135 /// <summary>
110136 /// 添加自定义参数
@@ -113,7 +139,7 @@ public void AddParameter(string key, short obj)
113139 /// <param name="obj"></param>
114140 public void AddParameter ( string key , int obj )
115141 {
116- _dict [ key ] = Operand . Create ( obj ) ;
142+ _tempdict [ key ] = Operand . Create ( obj ) ;
117143 }
118144 /// <summary>
119145 /// 添加自定义参数
@@ -122,7 +148,7 @@ public void AddParameter(string key, int obj)
122148 /// <param name="obj"></param>
123149 public void AddParameter ( string key , long obj )
124150 {
125- _dict [ key ] = Operand . Create ( ( double ) obj ) ;
151+ _tempdict [ key ] = Operand . Create ( ( double ) obj ) ;
126152 }
127153 /// <summary>
128154 /// 添加自定义参数
@@ -131,7 +157,7 @@ public void AddParameter(string key, long obj)
131157 /// <param name="obj"></param>
132158 public void AddParameter ( string key , ushort obj )
133159 {
134- _dict [ key ] = Operand . Create ( ( int ) obj ) ;
160+ _tempdict [ key ] = Operand . Create ( ( int ) obj ) ;
135161 }
136162 /// <summary>
137163 /// 添加自定义参数
@@ -140,7 +166,7 @@ public void AddParameter(string key, ushort obj)
140166 /// <param name="obj"></param>
141167 public void AddParameter ( string key , uint obj )
142168 {
143- _dict [ key ] = Operand . Create ( ( double ) obj ) ;
169+ _tempdict [ key ] = Operand . Create ( ( double ) obj ) ;
144170 }
145171 /// <summary>
146172 /// 添加自定义参数
@@ -149,7 +175,7 @@ public void AddParameter(string key, uint obj)
149175 /// <param name="obj"></param>
150176 public void AddParameter ( string key , ulong obj )
151177 {
152- _dict [ key ] = Operand . Create ( ( double ) obj ) ;
178+ _tempdict [ key ] = Operand . Create ( ( double ) obj ) ;
153179 }
154180 /// <summary>
155181 /// 添加自定义参数
@@ -158,7 +184,7 @@ public void AddParameter(string key, ulong obj)
158184 /// <param name="obj"></param>
159185 public void AddParameter ( string key , float obj )
160186 {
161- _dict [ key ] = Operand . Create ( ( double ) obj ) ;
187+ _tempdict [ key ] = Operand . Create ( ( double ) obj ) ;
162188 }
163189 /// <summary>
164190 /// 添加自定义参数
@@ -167,7 +193,7 @@ public void AddParameter(string key, float obj)
167193 /// <param name="obj"></param>
168194 public void AddParameter ( string key , double obj )
169195 {
170- _dict [ key ] = Operand . Create ( obj ) ;
196+ _tempdict [ key ] = Operand . Create ( obj ) ;
171197 }
172198 /// <summary>
173199 /// 添加自定义参数
@@ -176,7 +202,7 @@ public void AddParameter(string key, double obj)
176202 /// <param name="obj"></param>
177203 public void AddParameter ( string key , decimal obj )
178204 {
179- _dict [ key ] = Operand . Create ( ( double ) obj ) ;
205+ _tempdict [ key ] = Operand . Create ( ( double ) obj ) ;
180206 }
181207 #endregion
182208 /// <summary>
@@ -186,7 +212,7 @@ public void AddParameter(string key, decimal obj)
186212 /// <param name="obj"></param>
187213 public void AddParameter ( string key , string obj )
188214 {
189- _dict [ key ] = Operand . Create ( obj ) ;
215+ _tempdict [ key ] = Operand . Create ( obj ) ;
190216 }
191217 #region MyDate
192218 /// <summary>
@@ -196,7 +222,7 @@ public void AddParameter(string key, string obj)
196222 /// <param name="obj"></param>
197223 public void AddParameter ( string key , MyDate obj )
198224 {
199- _dict [ key ] = Operand . Create ( obj ) ;
225+ _tempdict [ key ] = Operand . Create ( obj ) ;
200226 }
201227 /// <summary>
202228 /// 添加自定义参数
@@ -205,7 +231,7 @@ public void AddParameter(string key, MyDate obj)
205231 /// <param name="obj"></param>
206232 public void AddParameter ( string key , DateTime obj )
207233 {
208- _dict [ key ] = Operand . Create ( obj ) ;
234+ _tempdict [ key ] = Operand . Create ( obj ) ;
209235 }
210236 /// <summary>
211237 /// 添加自定义参数
@@ -214,7 +240,7 @@ public void AddParameter(string key, DateTime obj)
214240 /// <param name="obj"></param>
215241 public void AddParameter ( string key , TimeSpan obj )
216242 {
217- _dict [ key ] = Operand . Create ( obj ) ;
243+ _tempdict [ key ] = Operand . Create ( obj ) ;
218244 }
219245 #endregion
220246 #region array
@@ -225,7 +251,7 @@ public void AddParameter(string key, TimeSpan obj)
225251 /// <param name="obj"></param>
226252 public void AddParameter ( string key , List < Operand > obj )
227253 {
228- _dict [ key ] = Operand . Create ( obj ) ;
254+ _tempdict [ key ] = Operand . Create ( obj ) ;
229255 }
230256 /// <summary>
231257 /// 添加自定义参数
@@ -234,7 +260,7 @@ public void AddParameter(string key, List<Operand> obj)
234260 /// <param name="obj"></param>
235261 public void AddParameter ( string key , ICollection < string > obj )
236262 {
237- _dict [ key ] = Operand . Create ( obj ) ;
263+ _tempdict [ key ] = Operand . Create ( obj ) ;
238264 }
239265 /// <summary>
240266 /// 添加自定义参数
@@ -243,7 +269,7 @@ public void AddParameter(string key, ICollection<string> obj)
243269 /// <param name="obj"></param>
244270 public void AddParameter ( string key , ICollection < double > obj )
245271 {
246- _dict [ key ] = Operand . Create ( obj ) ;
272+ _tempdict [ key ] = Operand . Create ( obj ) ;
247273
248274 }
249275 /// <summary>
@@ -253,7 +279,7 @@ public void AddParameter(string key, ICollection<double> obj)
253279 /// <param name="obj"></param>
254280 public void AddParameter ( string key , ICollection < int > obj )
255281 {
256- _dict [ key ] = Operand . Create ( obj ) ;
282+ _tempdict [ key ] = Operand . Create ( obj ) ;
257283 }
258284 /// <summary>
259285 /// 添加自定义参数
@@ -262,7 +288,7 @@ public void AddParameter(string key, ICollection<int> obj)
262288 /// <param name="obj"></param>
263289 public void AddParameter ( string key , ICollection < bool > obj )
264290 {
265- _dict [ key ] = Operand . Create ( obj ) ;
291+ _tempdict [ key ] = Operand . Create ( obj ) ;
266292 }
267293 #endregion
268294 /// <summary>
@@ -277,17 +303,17 @@ public void AddParameterFromJson(string json)
277303 foreach ( var item in jo . inst_object ) {
278304 var v = item . Value ;
279305 if ( v . IsString )
280- _dict [ item . Key ] = Operand . Create ( v . StringValue ) ;
306+ _tempdict [ item . Key ] = Operand . Create ( v . StringValue ) ;
281307 else if ( v . IsBoolean )
282- _dict [ item . Key ] = Operand . Create ( v . BooleanValue ) ;
308+ _tempdict [ item . Key ] = Operand . Create ( v . BooleanValue ) ;
283309 else if ( v . IsDouble )
284- _dict [ item . Key ] = Operand . Create ( v . NumberValue ) ;
310+ _tempdict [ item . Key ] = Operand . Create ( v . NumberValue ) ;
285311 else if ( v . IsObject )
286- _dict [ item . Key ] = Operand . Create ( v ) ;
312+ _tempdict [ item . Key ] = Operand . Create ( v ) ;
287313 else if ( v . IsArray )
288- _dict [ item . Key ] = Operand . Create ( v ) ;
314+ _tempdict [ item . Key ] = Operand . Create ( v ) ;
289315 else if ( v . IsNull )
290- _dict [ item . Key ] = Operand . CreateNull ( ) ;
316+ _tempdict [ item . Key ] = Operand . CreateNull ( ) ;
291317 }
292318 return ;
293319 }
@@ -297,7 +323,7 @@ public void AddParameterFromJson(string json)
297323
298324 #endregion
299325
300- #region Parse
326+ #region Parse Evaluate
301327
302328 /// <summary>
303329 /// 编译公式,默认
@@ -310,8 +336,6 @@ public bool Parse(string exp)
310336 LastError = "Parameter exp invalid !" ;
311337 return false ;
312338 }
313- //try {
314-
315339 var stream = new AntlrCharStream ( new AntlrInputStream ( exp ) ) ;
316340 var lexer = new mathLexer ( stream ) ;
317341 var tokens = new CommonTokenStream ( lexer ) ;
@@ -328,14 +352,8 @@ public bool Parse(string exp)
328352 }
329353 _context = context ;
330354 return true ;
331- //} catch (Exception ex) {
332- // LastError = ex.Message;
333- // return false;
334- //}
335355 }
336- #endregion
337-
338- #region Evaluate
356+
339357 /// <summary>
340358 /// 执行函数
341359 /// </summary>
@@ -347,7 +365,7 @@ public Operand Evaluate()
347365 throw new Exception ( "Please use Parse to compile formula !" ) ;
348366 }
349367 var visitor = new MathVisitor ( ) ;
350- visitor . GetParameter += GetParameter ;
368+ visitor . GetParameter += GetDiyParameterInside ;
351369 visitor . excelIndex = UseExcelIndex ? 1 : 0 ;
352370 visitor . DiyFunction += ExecuteDiyFunction ;
353371 return visitor . Visit ( _context ) ;
0 commit comments