Skip to content

Commit 934f5d5

Browse files
author
linzhijun
committed
修改
1 parent c319e98 commit 934f5d5

File tree

2 files changed

+66
-25
lines changed

2 files changed

+66
-25
lines changed

java/toolgood.algorithm/src/main/java/toolgood/algorithm/AlgorithmEngine.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
package toolgood.algorithm;
22

3-
import java.math.BigDecimal;
4-
import java.util.ArrayList;
5-
import java.util.List;
6-
import java.util.Map;
7-
import java.util.TreeMap;
8-
9-
import toolgood.algorithm.internals.AntlrErrorListener;
10-
import toolgood.algorithm.internals.CharUtil;
11-
import toolgood.algorithm.internals.MathSimplifiedFormulaVisitor;
12-
import toolgood.algorithm.internals.AntlrCharStream;
13-
import toolgood.algorithm.internals.MathVisitor;
3+
import org.antlr.v4.runtime.CharStreams;
4+
import org.antlr.v4.runtime.CommonTokenStream;
5+
import org.antlr.v4.runtime.RecognitionException;
6+
import org.joda.time.DateTime;
7+
import toolgood.algorithm.internals.*;
148
import toolgood.algorithm.litJson.JsonData;
159
import toolgood.algorithm.litJson.JsonMapper;
1610
import toolgood.algorithm.math.mathLexer;
1711
import toolgood.algorithm.math.mathParser;
18-
import toolgood.algorithm.math.mathParser.*;
12+
import toolgood.algorithm.math.mathParser.ProgContext;
1913

20-
import org.antlr.v4.runtime.CharStreams;
21-
import org.antlr.v4.runtime.CommonTokenStream;
22-
import org.antlr.v4.runtime.RecognitionException;
23-
import org.joda.time.DateTime;
14+
import java.math.BigDecimal;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.TreeMap;
2419

2520
public class AlgorithmEngine {
2621
/**
@@ -234,6 +229,23 @@ public Operand Evaluate() throws Exception {
234229
return visitor.visit(_context);
235230
}
236231

232+
public BigDecimal TryEvaluate(final String exp, final BigDecimal defvalue) {
233+
try {
234+
if (Parse(exp)) {
235+
Operand obj = Evaluate();
236+
obj = obj.ToNumber("It can't be converted to number!");
237+
if (obj.IsError()) {
238+
LastError = obj.ErrorMsg();
239+
return defvalue;
240+
}
241+
return obj.NumberValue();
242+
}
243+
} catch (final Exception ex) {
244+
LastError = ex.getMessage();
245+
}
246+
return defvalue;
247+
}
248+
237249
public int TryEvaluate(final String exp, final int defvalue) {
238250
try {
239251
if (Parse(exp)) {
@@ -260,13 +272,14 @@ public double TryEvaluate(final String exp, final double defvalue) {
260272
LastError = obj.ErrorMsg();
261273
return defvalue;
262274
}
263-
return obj.NumberValue().doubleValue();
275+
return obj.DoubleValue();
264276
}
265277
} catch (final Exception ex) {
266278
LastError = ex.getMessage();
267279
}
268280
return defvalue;
269281
}
282+
270283
public long TryEvaluate(final String exp, final long defvalue) {
271284
try {
272285
if (Parse(exp)) {
@@ -357,9 +370,8 @@ public MyDate TryEvaluate(final String exp, final MyDate defvalue) {
357370

358371
/**
359372
* 获取简化公式
360-
*
373+
*
361374
* @param formula 公式
362-
*
363375
*/
364376
public String GetSimplifiedFormula(final String formula) {
365377
try {
@@ -392,7 +404,7 @@ public String GetSimplifiedFormula(final String formula) {
392404

393405
/**
394406
* 计算公式
395-
*
407+
*
396408
* @param formula 公式
397409
* @param splitChar 分隔符
398410
* @return
@@ -407,7 +419,7 @@ public String EvaluateFormula(String formula, Character splitChar) {
407419

408420
/**
409421
* 计算公式
410-
*
422+
*
411423
* @param formula 公式
412424
* @param splitChars 分隔符
413425
* @return

java/toolgood.algorithm/src/main/java/toolgood/algorithm/AlgorithmEngineEx.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,21 @@ else if (v.IsNull())
325325
throw new Exception("Parameter is not json String.");
326326
}
327327

328+
public BigDecimal TryEvaluateCategory(final String categoryName, final BigDecimal defvalue) {
329+
try {
330+
Operand obj = EvaluateCategory(categoryName);
331+
obj = obj.ToNumber("It can't be converted to number!");
332+
if (obj.IsError()) {
333+
LastError = obj.ErrorMsg();
334+
return defvalue;
335+
}
336+
return obj.NumberValue();
337+
} catch (final Exception ex) {
338+
LastError = ex.getMessage();
339+
}
340+
return defvalue;
341+
}
342+
328343
public int TryEvaluateCategory(final String categoryName, final int defvalue) {
329344
try {
330345
Operand obj = EvaluateCategory(categoryName);
@@ -348,7 +363,7 @@ public double TryEvaluateCategory(final String categoryName, final double defval
348363
LastError = obj.ErrorMsg();
349364
return defvalue;
350365
}
351-
return obj.NumberValue().doubleValue();
366+
return obj.DoubleValue();
352367
} catch (final Exception ex) {
353368
LastError = ex.getMessage();
354369
}
@@ -362,7 +377,7 @@ public long TryEvaluateCategory(final String categoryName, final long defvalue)
362377
LastError = obj.ErrorMsg();
363378
return defvalue;
364379
}
365-
return obj.NumberValue().longValue();
380+
return obj.LongValue();
366381
} catch (final Exception ex) {
367382
LastError = ex.getMessage();
368383
}
@@ -431,6 +446,20 @@ public MyDate TryEvaluateCategory(final String categoryName, final MyDate defval
431446
}
432447
return defvalue;
433448
}
449+
public BigDecimal TryEvaluate(final String exp, final BigDecimal defvalue) {
450+
try {
451+
Operand obj = Evaluate(exp);
452+
obj = obj.ToNumber("It can't be converted to number!");
453+
if (obj.IsError()) {
454+
LastError = obj.ErrorMsg();
455+
return defvalue;
456+
}
457+
return obj.NumberValue();
458+
} catch (final Exception ex) {
459+
LastError = ex.getMessage();
460+
}
461+
return defvalue;
462+
}
434463

435464
public int TryEvaluate(final String exp, final int defvalue) {
436465
try {
@@ -455,7 +484,7 @@ public double TryEvaluate(final String exp, final double defvalue) {
455484
LastError = obj.ErrorMsg();
456485
return defvalue;
457486
}
458-
return obj.NumberValue().doubleValue();
487+
return obj.DoubleValue();
459488
} catch (final Exception ex) {
460489
LastError = ex.getMessage();
461490
}
@@ -469,7 +498,7 @@ public long TryEvaluate(final String exp, final long defvalue) {
469498
LastError = obj.ErrorMsg();
470499
return defvalue;
471500
}
472-
return obj.NumberValue().longValue();
501+
return obj.LongValue();
473502
} catch (final Exception ex) {
474503
LastError = ex.getMessage();
475504
}

0 commit comments

Comments
 (0)