22
33import java .math .BigDecimal ;
44import java .util .ArrayList ;
5- import java .util .HashMap ;
65import java .util .List ;
76import java .util .Map ;
8- import java .util .function . Function ;
7+ import java .util .TreeMap ;
98
109import toolgood .algorithm .internals .AntlrErrorListener ;
1110import toolgood .algorithm .internals .CharUtil ;
1211import toolgood .algorithm .internals .AntlrCharStream ;
1312import toolgood .algorithm .internals .MathVisitor ;
14- import toolgood .algorithm .internals .MyFunction ;
1513import toolgood .algorithm .litJson .JsonData ;
1614import toolgood .algorithm .litJson .JsonMapper ;
1715import toolgood .algorithm .math .mathLexer ;
2321import org .antlr .v4 .runtime .RecognitionException ;
2422import org .joda .time .DateTime ;
2523
26-
2724public class AlgorithmEngine {
25+ /**
26+ * 使用EXCEL索引
27+ */
2828 public boolean UseExcelIndex = true ;
29+ /**
30+ * 最后一个错误
31+ */
2932 public String LastError ;
33+ /**
34+ * 保存到临时文档
35+ */
36+ public boolean UseTempDict = false ;
37+ /**
38+ * 是否忽略大小写
39+ */
40+ public final boolean IgnoreCase ;
3041 private ProgContext _context ;
31- protected final Map <String , Operand > _dict = new HashMap <String , Operand >();
32- public Function <MyFunction , Operand > DiyFunction ;
42+ private final Map <String , Operand > _tempdict ;
43+
44+ /// <summary>
45+ /// 默认不带缓存
46+ /// </summary>
47+ public AlgorithmEngine () {
48+ IgnoreCase = false ;
49+ _tempdict = new TreeMap <String , Operand >();
50+ }
3351
34- protected Operand GetParameter (final String parameter ) throws Exception {
35- if (_dict .containsKey (parameter )) {
36- return _dict .get (parameter );
52+ /// <summary>
53+ /// 带缓存关键字大小写参数
54+ /// </summary>
55+ /// <param name="ignoreCase"></param>
56+ public AlgorithmEngine (boolean ignoreCase ) {
57+ IgnoreCase = ignoreCase ;
58+ if (ignoreCase ) {
59+ _tempdict = new TreeMap <String , Operand >(String .CASE_INSENSITIVE_ORDER );
60+ } else {
61+ _tempdict = new TreeMap <String , Operand >();
3762 }
38- return Operand .Error ("Parameter [" + parameter + "] is missing." );
3963 }
4064
41- protected Operand ExecuteDiyFunction (final String funcName , final List <Operand > operands ) {
42- if (DiyFunction != null ) {
43- MyFunction f =new MyFunction ();
44- f .Name =funcName ;
45- f .OperandList =operands ;
46- return DiyFunction .apply (f );
65+ private Operand GetDiyParameterInside (String parameter ) {
66+ if (_tempdict .containsKey (parameter )) {
67+ return _tempdict .get (parameter );
4768 }
48- return Operand .Error ("DiyFunction [" + funcName + "] is missing." );
69+ Operand result = GetParameter (parameter );
70+ if (UseTempDict ) {
71+ _tempdict .put (parameter , result );
72+ }
73+ return result ;
4974 }
5075
51- public void ClearDiyFunctions () {
52- DiyFunction = null ;
76+ protected Operand GetParameter (final String parameter ) {
77+ return Operand .Error ("Parameter [" + parameter + "] is missing." );
78+ }
79+
80+ protected Operand ExecuteDiyFunction (final String funcName , final List <Operand > operands ) {
81+ return Operand .Error ("DiyFunction [" + funcName + "] is missing." );
5382 }
5483
5584 public void ClearParameters () {
56- _dict .clear ();
85+ _tempdict .clear ();
5786 }
5887
88+ /**
89+ * 添加自定义参数
90+ */
5991 public void AddParameter (final String key , final Operand obj ) {
60- _dict .put (key , obj );
92+ _tempdict .put (key , obj );
6193 }
6294
95+ /**
96+ * 添加自定义参数
97+ */
6398 public void AddParameter (final String key , final boolean obj ) {
64- _dict .put (key , Operand .Create (obj ));
99+ _tempdict .put (key , Operand .Create (obj ));
65100 }
66101
102+ /**
103+ * 添加自定义参数
104+ */
67105 public void AddParameter (final String key , final short obj ) {
68- _dict .put (key , Operand .Create (obj ));
106+ _tempdict .put (key , Operand .Create (obj ));
69107 }
70108
109+ /**
110+ * 添加自定义参数
111+ */
71112 public void AddParameter (final String key , final int obj ) {
72- _dict .put (key , Operand .Create (obj ));
113+ _tempdict .put (key , Operand .Create (obj ));
73114 }
74115
116+ /**
117+ * 添加自定义参数
118+ */
75119 public void AddParameter (final String key , final long obj ) {
76- _dict .put (key , Operand .Create (obj ));
120+ _tempdict .put (key , Operand .Create (obj ));
77121 }
78122
123+ /**
124+ * 添加自定义参数
125+ */
79126 public void AddParameter (final String key , final float obj ) {
80- _dict .put (key , Operand .Create (obj ));
127+ _tempdict .put (key , Operand .Create (obj ));
81128 }
82129
130+ /**
131+ * 添加自定义参数
132+ */
83133 public void AddParameter (final String key , final double obj ) {
84- _dict .put (key , Operand .Create (obj ));
134+ _tempdict .put (key , Operand .Create (obj ));
85135 }
86136
137+ /**
138+ * 添加自定义参数
139+ */
87140 public void AddParameter (final String key , final BigDecimal obj ) {
88- _dict .put (key , Operand .Create (obj ));
141+ _tempdict .put (key , Operand .Create (obj ));
89142 }
90143
144+ /**
145+ * 添加自定义参数
146+ */
91147 public void AddParameter (final String key , final String obj ) {
92- _dict .put (key , Operand .Create (obj ));
148+ _tempdict .put (key , Operand .Create (obj ));
93149 }
94150
151+ /**
152+ * 添加自定义参数
153+ */
95154 public void AddParameter (final String key , final MyDate obj ) {
96- _dict .put (key , Operand .Create (obj ));
155+ _tempdict .put (key , Operand .Create (obj ));
97156 }
98157
158+ /**
159+ * 添加自定义参数
160+ */
99161 public void AddParameter (final String key , final List <Operand > obj ) {
100- _dict .put (key , Operand .Create (obj ));
162+ _tempdict .put (key , Operand .Create (obj ));
101163 }
102164
165+ /**
166+ * 添加自定义参数
167+ */
103168 public void AddParameterFromJson (final String json ) throws Exception {
104169 if (json .startsWith ("{" ) && json .endsWith ("}" )) {
105170 final JsonData jo = (JsonData ) JsonMapper .ToObject (json );
106171 if (jo .IsObject ()) {
107172 for (String item : jo .inst_object .keySet ()) {
108173 final JsonData v = jo .inst_object .get (item );
109174 if (v .IsString ())
110- _dict .put (item , Operand .Create (v .StringValue ()));
175+ _tempdict .put (item , Operand .Create (v .StringValue ()));
111176 else if (v .IsBoolean ())
112- _dict .put (item , Operand .Create (v .BooleanValue ()));
177+ _tempdict .put (item , Operand .Create (v .BooleanValue ()));
113178 else if (v .IsDouble ())
114- _dict .put (item , Operand .Create (v .NumberValue ()));
179+ _tempdict .put (item , Operand .Create (v .NumberValue ()));
115180 else if (v .IsObject ())
116- _dict .put (item , Operand .Create (v ));
181+ _tempdict .put (item , Operand .Create (v ));
117182 else if (v .IsArray ())
118- _dict .put (item , Operand .Create (v ));
183+ _tempdict .put (item , Operand .Create (v ));
119184 else if (v .IsNull ())
120- _dict .put (item , Operand .CreateNull ());
185+ _tempdict .put (item , Operand .CreateNull ());
121186 }
122187 return ;
123188 }
124189 }
125190 throw new Exception ("Parameter is not json String." );
126191 }
127192
128-
129193 public boolean Parse (final String exp ) throws RecognitionException {
130194 if (exp == null || exp .equals ("" )) {
131195 LastError = "Parameter exp invalid !" ;
132196 return false ;
133197 }
134- // try {
135198 final AntlrCharStream stream = new AntlrCharStream (CharStreams .fromString (exp ));
136199 final mathLexer lexer = new mathLexer (stream );
137200 final CommonTokenStream tokens = new CommonTokenStream (lexer );
@@ -148,10 +211,6 @@ public boolean Parse(final String exp) throws RecognitionException {
148211 }
149212 _context = context ;
150213 return true ;
151- // } catch (Exception ex) {
152- // LastError = ex.Message;
153- // return false;
154- // }
155214 }
156215
157216 public Operand Evaluate () throws Exception {
@@ -162,15 +221,18 @@ public Operand Evaluate() throws Exception {
162221 final MathVisitor visitor = new MathVisitor ();
163222 visitor .GetParameter = f -> {
164223 try {
165- return GetParameter (f );
224+ return GetDiyParameterInside (f );
166225 } catch (Exception e ) {
167226 }
168227 return null ;
169228 };
170229 visitor .excelIndex = UseExcelIndex ? 1 : 0 ;
171- visitor .DiyFunction = f ->{ return ExecuteDiyFunction (f .Name ,f .OperandList ); };
230+ visitor .DiyFunction = f -> {
231+ return ExecuteDiyFunction (f .Name , f .OperandList );
232+ };
172233 return visitor .visit (_context );
173234 }
235+
174236 public int TryEvaluate (final String exp , final int defvalue ) {
175237 try {
176238 if (Parse (exp )) {
@@ -187,6 +249,7 @@ public int TryEvaluate(final String exp, final int defvalue) {
187249 }
188250 return defvalue ;
189251 }
252+
190253 public double TryEvaluate (final String exp , final double defvalue ) {
191254 try {
192255 if (Parse (exp )) {
@@ -203,6 +266,7 @@ public double TryEvaluate(final String exp, final double defvalue) {
203266 }
204267 return defvalue ;
205268 }
269+
206270 public String TryEvaluate (final String exp , final String defvalue ) {
207271 try {
208272 if (Parse (exp )) {
@@ -222,6 +286,7 @@ public String TryEvaluate(final String exp, final String defvalue) {
222286 }
223287 return defvalue ;
224288 }
289+
225290 public boolean TryEvaluate (final String exp , final boolean defvalue ) {
226291 try {
227292 if (Parse (exp )) {
@@ -238,6 +303,7 @@ public boolean TryEvaluate(final String exp, final boolean defvalue) {
238303 }
239304 return defvalue ;
240305 }
306+
241307 public DateTime TryEvaluate (final String exp , final DateTime defvalue ) {
242308 try {
243309 if (Parse (exp )) {
@@ -254,6 +320,7 @@ public DateTime TryEvaluate(final String exp, final DateTime defvalue) {
254320 }
255321 return defvalue ;
256322 }
323+
257324 public MyDate TryEvaluate (final String exp , final MyDate defvalue ) {
258325 try {
259326 if (Parse (exp )) {
@@ -270,27 +337,33 @@ public MyDate TryEvaluate(final String exp, final MyDate defvalue) {
270337 }
271338 return defvalue ;
272339 }
340+
273341 /**
274342 * 计算公式
275- * @param formula 公式
343+ *
344+ * @param formula 公式
276345 * @param splitChar 分隔符
277346 * @return
278347 */
279348 public String EvaluateFormula (String formula , Character splitChar ) {
280- if (formula == null || formula .equals ("" )) return "" ;
349+ if (formula == null || formula .equals ("" ))
350+ return "" ;
281351 List <Character > splitChars = new ArrayList <>();
282352 splitChars .add (splitChar );
283353 return EvaluateFormula (formula , splitChars );
284354 }
355+
285356 /**
286357 * 计算公式
287- * @param formula 公式
358+ *
359+ * @param formula 公式
288360 * @param splitChars 分隔符
289361 * @return
290362 */
291363 public String EvaluateFormula (String formula , List <Character > splitChars ) {
292- if (formula == null || formula .equals ("" )) return "" ;
293- List <String > sp = CharUtil .SplitFormula (formula ,splitChars );
364+ if (formula == null || formula .equals ("" ))
365+ return "" ;
366+ List <String > sp = CharUtil .SplitFormula (formula , splitChars );
294367
295368 StringBuilder stringBuilder = new StringBuilder ();
296369 for (int i = 0 ; i < sp .size (); i ++) {
0 commit comments