Skip to content

Commit 85586ef

Browse files
committed
统一 方法名
1 parent 1452cb9 commit 85586ef

File tree

2 files changed

+130
-32
lines changed

2 files changed

+130
-32
lines changed

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

Lines changed: 121 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class AlgorithmEngineEx {
4646
/**
4747
* 跳过条件错误
4848
*/
49-
public Boolean JumpConditionError = false;
49+
public Boolean JumpConditionError = true;
5050
/**
5151
* 跳过公式错误
5252
*/
@@ -88,7 +88,7 @@ protected Operand GetParameter(final String parameter) {
8888
if (conditionCache.GetFormulaProg() == null)
8989
continue;
9090
if (conditionCache.GetConditionProg() != null) {
91-
Operand b = Evaluate(conditionCache.GetConditionProg());
91+
Operand b = EvaluateCategory(conditionCache.GetConditionProg());
9292
if (b.IsError()) {
9393
LastError = "Parameter [" + parameter + "]," + conditionCache.Remark
9494
+ " condition `" + conditionCache.ConditionString + "` is error.\r\n" + b.ErrorMsg();
@@ -99,7 +99,7 @@ protected Operand GetParameter(final String parameter) {
9999
if (b.BooleanValue() == false)
100100
continue;
101101
}
102-
Operand operand = Evaluate(conditionCache.GetFormulaProg());
102+
Operand operand = EvaluateCategory(conditionCache.GetFormulaProg());
103103
if (operand.IsError()) {
104104
LastError = "Parameter [" + parameter + "]," + conditionCache.Remark
105105
+ " formula `" + conditionCache.FormulaString + "` is error.\r\n" + operand.ErrorMsg();
@@ -128,7 +128,7 @@ public void ClearParameters() {
128128
* @param categoryName
129129
* @return
130130
*/
131-
public Operand Evaluate(String categoryName) {
131+
public Operand EvaluateCategory(String categoryName) {
132132
LastError = null;
133133
Operand operand;
134134
ArrayList<ConditionCacheInfo> conditionCaches = MultiConditionCache.GetConditionCaches(categoryName);
@@ -141,7 +141,7 @@ public Operand Evaluate(String categoryName) {
141141
+ " parse condition `" + conditionCache.ConditionString + "` is error.\r\n"
142142
+ conditionCache.LastError);
143143
}
144-
Operand b = Evaluate(conditionCache.GetConditionProg());
144+
Operand b = EvaluateCategory(conditionCache.GetConditionProg());
145145
if (b.IsError()) {
146146
LastError = "Parameter [" + categoryName + "]," + conditionCache.Remark
147147
+ " condition `" + conditionCache.ConditionString + "` is error.\r\n" + b.ErrorMsg();
@@ -157,7 +157,7 @@ public Operand Evaluate(String categoryName) {
157157
+ " parse formula `" + conditionCache.FormulaString + "` is error.\r\n"
158158
+ conditionCache.LastError);
159159
}
160-
operand = Evaluate(conditionCache.GetFormulaProg());
160+
operand = EvaluateCategory(conditionCache.GetFormulaProg());
161161
if (operand.IsError()) {
162162
LastError = "Parameter [" + categoryName + "]," + conditionCache.Remark
163163
+ " formula `" + conditionCache.FormulaString + "` is error.\r\n" + operand.ErrorMsg();
@@ -189,7 +189,7 @@ public String SearchRemark(String categoryName) throws Exception {
189189
+ " parse condition `" + conditionCache.ConditionString + "` is error.\r\n"
190190
+ conditionCache.LastError);
191191
}
192-
Operand b = Evaluate(conditionCache.GetConditionProg());
192+
Operand b = EvaluateCategory(conditionCache.GetConditionProg());
193193
if (b.IsError()) {
194194
LastError = "Parameter [" + categoryName + "]," + conditionCache.Remark
195195
+ " condition `" + conditionCache.ConditionString + "` is error.\r\n" + b.ErrorMsg();
@@ -324,9 +324,9 @@ else if (v.IsNull())
324324
throw new Exception("Parameter is not json String.");
325325
}
326326

327-
public int TryEvaluate(final String categoryName, final int defvalue) {
327+
public int TryEvaluateCategory(final String categoryName, final int defvalue) {
328328
try {
329-
Operand obj = Evaluate(categoryName);
329+
Operand obj = EvaluateCategory(categoryName);
330330
obj = obj.ToNumber("It can't be converted to number!");
331331
if (obj.IsError()) {
332332
LastError = obj.ErrorMsg();
@@ -339,9 +339,9 @@ public int TryEvaluate(final String categoryName, final int defvalue) {
339339
return defvalue;
340340
}
341341

342-
public double TryEvaluate(final String categoryName, final double defvalue) {
342+
public double TryEvaluateCategory(final String categoryName, final double defvalue) {
343343
try {
344-
Operand obj = Evaluate(categoryName);
344+
Operand obj = EvaluateCategory(categoryName);
345345
obj = obj.ToNumber("It can't be converted to number!");
346346
if (obj.IsError()) {
347347
LastError = obj.ErrorMsg();
@@ -354,9 +354,9 @@ public double TryEvaluate(final String categoryName, final double defvalue) {
354354
return defvalue;
355355
}
356356

357-
public String TryEvaluate(final String categoryName, final String defvalue) {
357+
public String TryEvaluateCategory(final String categoryName, final String defvalue) {
358358
try {
359-
Operand obj = Evaluate(categoryName);
359+
Operand obj = EvaluateCategory(categoryName);
360360
if (obj.IsNull()) {
361361
return null;
362362
}
@@ -372,9 +372,9 @@ public String TryEvaluate(final String categoryName, final String defvalue) {
372372
return defvalue;
373373
}
374374

375-
public boolean TryEvaluate(final String categoryName, final boolean defvalue) {
375+
public boolean TryEvaluateCategory(final String categoryName, final boolean defvalue) {
376376
try {
377-
Operand obj = Evaluate(categoryName);
377+
Operand obj = EvaluateCategory(categoryName);
378378
obj = obj.ToBoolean("It can't be converted to bool!");
379379
if (obj.IsError()) {
380380
LastError = obj.ErrorMsg();
@@ -387,9 +387,9 @@ public boolean TryEvaluate(final String categoryName, final boolean defvalue) {
387387
return defvalue;
388388
}
389389

390-
public DateTime TryEvaluate(final String categoryName, final DateTime defvalue) {
390+
public DateTime TryEvaluateCategory(final String categoryName, final DateTime defvalue) {
391391
try {
392-
Operand obj = Evaluate(categoryName);
392+
Operand obj = EvaluateCategory(categoryName);
393393
obj = obj.ToDate("It can't be converted to bool!");
394394
if (obj.IsError()) {
395395
LastError = obj.ErrorMsg();
@@ -402,9 +402,9 @@ public DateTime TryEvaluate(final String categoryName, final DateTime defvalue)
402402
return defvalue;
403403
}
404404

405-
public MyDate TryEvaluate(final String categoryName, final MyDate defvalue) {
405+
public MyDate TryEvaluateCategory(final String categoryName, final MyDate defvalue) {
406406
try {
407-
Operand obj = Evaluate(categoryName);
407+
Operand obj = EvaluateCategory(categoryName);
408408
obj = obj.ToDate("It can't be converted to bool!");
409409
if (obj.IsError()) {
410410
LastError = obj.ErrorMsg();
@@ -416,8 +416,103 @@ public MyDate TryEvaluate(final String categoryName, final MyDate defvalue) {
416416
}
417417
return defvalue;
418418
}
419+
420+
public int TryEvaluate(final String exp, final int defvalue) {
421+
try {
422+
Operand obj = Evaluate(exp);
423+
obj = obj.ToNumber("It can't be converted to number!");
424+
if (obj.IsError()) {
425+
LastError = obj.ErrorMsg();
426+
return defvalue;
427+
}
428+
return obj.IntValue();
429+
} catch (final Exception ex) {
430+
LastError = ex.getMessage();
431+
}
432+
return defvalue;
433+
}
434+
435+
public double TryEvaluate(final String exp, final double defvalue) {
436+
try {
437+
Operand obj = Evaluate(exp);
438+
obj = obj.ToNumber("It can't be converted to number!");
439+
if (obj.IsError()) {
440+
LastError = obj.ErrorMsg();
441+
return defvalue;
442+
}
443+
return obj.NumberValue();
444+
} catch (final Exception ex) {
445+
LastError = ex.getMessage();
446+
}
447+
return defvalue;
448+
}
419449

420-
private Operand Evaluate(ProgContext context) {
450+
public String TryEvaluate(final String exp, final String defvalue) {
451+
try {
452+
Operand obj = Evaluate(exp);
453+
if (obj.IsNull()) {
454+
return null;
455+
}
456+
obj = obj.ToText("It can't be converted to String!");
457+
if (obj.IsError()) {
458+
LastError = obj.ErrorMsg();
459+
return defvalue;
460+
}
461+
return obj.TextValue();
462+
} catch (final Exception ex) {
463+
LastError = ex.getMessage();
464+
}
465+
return defvalue;
466+
}
467+
468+
public boolean TryEvaluate(final String exp, final boolean defvalue) {
469+
try {
470+
Operand obj = Evaluate(exp);
471+
obj = obj.ToBoolean("It can't be converted to bool!");
472+
if (obj.IsError()) {
473+
LastError = obj.ErrorMsg();
474+
return defvalue;
475+
}
476+
return obj.BooleanValue();
477+
} catch (final Exception ex) {
478+
LastError = ex.getMessage();
479+
}
480+
return defvalue;
481+
}
482+
483+
public DateTime TryEvaluate(final String exp, final DateTime defvalue) {
484+
try {
485+
Operand obj = Evaluate(exp);
486+
obj = obj.ToDate("It can't be converted to bool!");
487+
if (obj.IsError()) {
488+
LastError = obj.ErrorMsg();
489+
return defvalue;
490+
}
491+
return obj.DateValue().ToDateTime();
492+
} catch (final Exception ex) {
493+
LastError = ex.getMessage();
494+
}
495+
return defvalue;
496+
}
497+
498+
public MyDate TryEvaluate(final String exp, final MyDate defvalue) {
499+
try {
500+
Operand obj = Evaluate(exp);
501+
obj = obj.ToDate("It can't be converted to bool!");
502+
if (obj.IsError()) {
503+
LastError = obj.ErrorMsg();
504+
return defvalue;
505+
}
506+
return obj.DateValue();
507+
} catch (final Exception ex) {
508+
LastError = ex.getMessage();
509+
}
510+
return defvalue;
511+
}
512+
513+
514+
515+
private Operand EvaluateCategory(ProgContext context) {
421516
try {
422517
final MathVisitor visitor = new MathVisitor();
423518
visitor.GetParameter = f -> {
@@ -473,7 +568,7 @@ public String EvaluateFormula(String formula, List<Character> splitChars) {
473568
} else {
474569
String d = "";
475570
try {
476-
Operand operand = EvaluateOnce(s);
571+
Operand operand = Evaluate(s);
477572
d = operand.ToText("").TextValue();
478573
} catch (Exception ex) {
479574
}
@@ -488,9 +583,12 @@ public String EvaluateFormula(String formula, List<Character> splitChars) {
488583
/// </summary>
489584
/// <param name="exp"></param>
490585
/// <returns></returns>
491-
protected Operand EvaluateOnce(String exp) {
586+
public Operand Evaluate(String exp) {
492587
ProgContext context = Parse(exp);
493-
return Evaluate(context);
588+
if (context == null) {
589+
return Operand.Create(LastError);
590+
}
591+
return EvaluateCategory(context);
494592
}
495593

496594
private ProgContext Parse(String exp) {

java/toolgood.algorithm/src/test/java/toolgood/algorithm/Tests2/AlgorithmEngineExTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void Test() {
3030
desk.Radius = 3;
3131

3232
PriceAlgorithm priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk);
33-
Double p1 = priceAlgorithm.TryEvaluate("价格", 0.0);
33+
Double p1 = priceAlgorithm.TryEvaluateCategory("价格", 0.0);
3434
assertEquals(3 * 3 * Math.PI * 1.5, p1, 0.0001);
3535

3636
Desk desk2 = new Desk();
@@ -39,7 +39,7 @@ public void Test() {
3939
desk2.Width = 1.3;
4040
desk2.Heigth = 1;
4141
priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk2);
42-
Double p2 = priceAlgorithm.TryEvaluate("价格", 0.0);
42+
Double p2 = priceAlgorithm.TryEvaluateCategory("价格", 0.0);
4343
assertEquals(3 * 1.3 * 2 + 1 * 1.1, p2, 0.0001);
4444

4545
Desk desk3 = new Desk();
@@ -49,11 +49,11 @@ public void Test() {
4949
desk3.Heigth = 1;
5050

5151
priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk3);
52-
Double p3 = priceAlgorithm.TryEvaluate("价格", 0.0);
52+
Double p3 = priceAlgorithm.TryEvaluateCategory("价格", 0.0);
5353
assertEquals(0, p3, 0.001);
5454
assertEquals("CategoryName [价格] is missing.", priceAlgorithm.LastError);
5555

56-
Double p4 = priceAlgorithm.TryEvaluate("出错了", 0.0);
56+
Double p4 = priceAlgorithm.TryEvaluateCategory("出错了", 0.0);
5757
}
5858

5959
@Test
@@ -80,7 +80,7 @@ public void Test2() {
8080
desk.Radius = 3;
8181

8282
PriceAlgorithm priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk);
83-
Double p1 = priceAlgorithm.TryEvaluate("价格", 0.0);
83+
Double p1 = priceAlgorithm.TryEvaluateCategory("价格", 0.0);
8484
assertEquals(3 * 3 * Math.PI * 1.5, p1, 0.0001);
8585

8686
Desk desk2 = new Desk();
@@ -89,7 +89,7 @@ public void Test2() {
8989
desk2.Width = 1.3;
9090
desk2.Heigth = 1;
9191
priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk2);
92-
Double p2 = priceAlgorithm.TryEvaluate("价格", 0.0);
92+
Double p2 = priceAlgorithm.TryEvaluateCategory("价格", 0.0);
9393
assertEquals(3 * 1.3 * 2 + 1 * 1.1, p2, 0.0001);
9494

9595
Desk desk3 = new Desk();
@@ -99,11 +99,11 @@ public void Test2() {
9999
desk3.Heigth = 1;
100100

101101
priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk3);
102-
Double p3 = priceAlgorithm.TryEvaluate("价格", 0.0);
102+
Double p3 = priceAlgorithm.TryEvaluateCategory("价格", 0.0);
103103
assertEquals(0, p3, 0.001);
104104
assertEquals("CategoryName [价格] is missing.", priceAlgorithm.LastError);
105105

106-
Double p4 = priceAlgorithm.TryEvaluate("出错了", 0.0);
106+
Double p4 = priceAlgorithm.TryEvaluateCategory("出错了", 0.0);
107107

108108
String tt = priceAlgorithm.EvaluateFormula("长-宽-高", '-');
109109
assertEquals("9-1.3-1", tt);
@@ -154,7 +154,7 @@ public void Test4()
154154
algoEngine.AddParameter("宽", 1.3);
155155
algoEngine.AddParameter("高", 1);
156156

157-
Double p2 = algoEngine.TryEvaluate("价格", 0.0);
157+
Double p2 = algoEngine.TryEvaluateCategory("价格", 0.0);
158158
assertEquals(3 * 1.3 * 2 + 1 * 1.1, p2, 0.0001);
159159

160160
}

0 commit comments

Comments
 (0)