Skip to content

Commit 97473e5

Browse files
committed
修改出错提示
1 parent e934cb7 commit 97473e5

File tree

4 files changed

+57
-37
lines changed

4 files changed

+57
-37
lines changed

csharp/ToolGood.Algorithm2.Test/AlgorithmEngineEx/AlgorithmEngineExTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ public void Test()
2121
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<2.5", "[桌面积]*1.3");
2222
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<5", "[桌面积]*1.5");
2323
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<7", "[桌面积]*2");
24-
multiConditionCache.AddFormula("价格", "", "[桌面积]*2.5");
24+
multiConditionCache.AddFormula("价格", "[圆桌]", "[桌面积]*2.5");
2525

2626
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<1.3", "[桌面积]*1.3+[高]*1.1");
2727
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<2", "[桌面积]*1.5+[高]*1.1");
2828
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<5", "[桌面积]*2+[高]*1.1");
2929
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<7", "[桌面积]*2.5");
3030

31+
3132
multiConditionCache.AddFormula("出错了", "[方桌]&& [长]<11", "[出错]*2.5");
3233

3334

csharp/ToolGood.Algorithm2/AlgorithmEngineEx.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ protected virtual Operand GetParameter(string parameter)
5050
}
5151
var conditionCaches = MultiConditionCache.GetConditionCaches(parameter);
5252
foreach (var conditionCache in conditionCaches) {
53-
if (conditionCache.Formula == null) continue;
54-
if (conditionCache.Condition != null) {
55-
var b = Evaluate(conditionCache.Condition);
53+
if (conditionCache.FormulaProg == null) continue;
54+
if (conditionCache.ConditionProg != null) {
55+
var b = Evaluate(conditionCache.ConditionProg);
5656
if (b.IsError) {
5757
return Operand.Error($"Parameter [{parameter}] condition `{conditionCache.ConditionString}` is error.\r\n{b.ErrorMsg}");
5858
}
5959
if (b.BooleanValue == false) continue;
6060
}
61-
operand = Evaluate(conditionCache.Formula);
61+
operand = Evaluate(conditionCache.FormulaProg);
6262
if (operand.IsError) {
6363
operand = Operand.Error($"Parameter [{parameter}] formula `{conditionCache.FormulaString}` is error.\r\n{operand.ErrorMsg}");
6464
}
@@ -106,19 +106,19 @@ public Operand Evaluate(string categoryName)
106106
foreach (var conditionCache in conditionCaches) {
107107
if (string.IsNullOrEmpty(conditionCache.FormulaString)) continue;
108108
if (string.IsNullOrEmpty(conditionCache.ConditionString) == false) {
109-
if (conditionCache.Condition == null) {
109+
if (conditionCache.ConditionProg == null) {
110110
return Operand.Error($"CategoryName [{categoryName}] parse condition `{conditionCache.ConditionString}` is error.\r\n{conditionCache.LastError}");
111111
}
112-
var b = Evaluate(conditionCache.Condition);
112+
var b = Evaluate(conditionCache.ConditionProg);
113113
if (b.IsError) {
114114
return Operand.Error($"CategoryName [{categoryName}] condition `{conditionCache.ConditionString}` is error.\r\n{b.ErrorMsg}");
115115
}
116116
if (b.BooleanValue == false) continue;
117117
}
118-
if (conditionCache.Formula == null) {
118+
if (conditionCache.FormulaProg == null) {
119119
return Operand.Error($"CategoryName [{categoryName}] parse formula `{ conditionCache.FormulaString}` is error.\r\n{conditionCache.LastError}");
120120
}
121-
operand = Evaluate(conditionCache.Formula);
121+
operand = Evaluate(conditionCache.FormulaProg);
122122
if (operand.IsError) {
123123
operand = Operand.Error($"CategoryName [{categoryName}] formula `{conditionCache.FormulaString}` is error.\r\n{operand.ErrorMsg}");
124124
}
@@ -139,10 +139,10 @@ public string SearchRemark(string categoryName)
139139
var conditionCaches = MultiConditionCache.GetConditionCaches(categoryName);
140140
foreach (var conditionCache in conditionCaches) {
141141
if (string.IsNullOrEmpty(conditionCache.ConditionString) == false) {
142-
if (conditionCache.Condition == null) {
142+
if (conditionCache.ConditionProg == null) {
143143
throw new Exception($"CategoryName [{categoryName}] parse formula `{ conditionCache.FormulaString}` is error.\r\n{conditionCache.LastError}");
144144
}
145-
var b = Evaluate(conditionCache.Condition);
145+
var b = Evaluate(conditionCache.ConditionProg);
146146
if (b.IsError) {
147147
throw new Exception($"CategoryName [{categoryName}] formula `{ conditionCache.FormulaString}` is error.\r\n{b.ErrorMsg}");
148148
}

csharp/ToolGood.Algorithm2/ConditionCache.cs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,47 @@ public class ConditionCache
2020
/// 是否开启延迟加载
2121
/// 开启后,不会立即调取Parse
2222
/// </summary>
23-
public bool LazyLoad=false;
23+
public bool LazyLoad = false;
2424

2525

2626
/// <summary>
2727
/// 添加公式缓存
2828
/// </summary>
2929
/// <param name="categoryName">分类名称</param>
30-
/// <param name="formula">公式</param>
31-
/// <param name="condition">条件</param>
30+
/// <param name="formula">公式 必埴</param>
31+
/// <param name="condition">条件 可空</param>
3232
/// <param name="remark">备注</param>
3333
/// <returns></returns>
3434
public bool AddFormula(string categoryName, string condition, string formula, string remark = null)
3535
{
36+
if (string.IsNullOrEmpty(categoryName)) {
37+
LastError = "Parameter categoryName is null or empty.";
38+
return false;
39+
}
40+
if (string.IsNullOrEmpty(formula)) {
41+
LastError = "Parameter formula is null or empty.";
42+
return false;
43+
}
3644
Internals.ConditionCache conditionCache = new Internals.ConditionCache() {
3745
CategoryName = categoryName,
3846
Remark = remark,
3947
ConditionString = condition,
4048
FormulaString = formula
4149
};
42-
if (LazyLoad==false) {
43-
if (condition != null) {
50+
51+
if (LazyLoad == false) {
52+
if (string.IsNullOrEmpty(condition) == false) {
4453
var conditionProg = Parse(condition);
4554
if (conditionProg == null) {
4655
return false;
4756
}
48-
conditionCache.Condition = conditionProg;
57+
conditionCache.ConditionProg = conditionProg;
4958
}
5059
var formulaProg = Parse(formula);
5160
if (formulaProg == null) {
5261
return false;
5362
}
54-
conditionCache.Formula = formulaProg;
63+
conditionCache.FormulaProg = formulaProg;
5564
}
5665

5766
List<Internals.ConditionCache> list;
@@ -68,23 +77,33 @@ public bool AddFormula(string categoryName, string condition, string formula, st
6877
/// 添加 条件缓存,
6978
/// 有先后顺序的
7079
/// </summary>
71-
/// <param name="categoryName"></param>
80+
/// <param name="categoryName">分类名称</param>
7281
/// <param name="condition"></param>
73-
/// <param name="remark"></param>
82+
/// <param name="remark">备注 必埴</param>
7483
/// <returns></returns>
7584
public bool AddCondition(string categoryName, string condition, string remark)
7685
{
86+
if (string.IsNullOrEmpty(categoryName)) {
87+
LastError = "Parameter categoryName is null or empty.";
88+
return false;
89+
}
90+
if (string.IsNullOrEmpty(remark)) {
91+
LastError = "Parameter remark is null or empty.";
92+
return false;
93+
}
7794
Internals.ConditionCache conditionCache = new Internals.ConditionCache() {
7895
CategoryName = categoryName,
7996
Remark = remark,
8097
ConditionString = condition,
8198
};
82-
if (condition != null) {
83-
var conditionProg = Parse(condition);
84-
if (conditionProg == null) {
85-
return false;
99+
if (LazyLoad == false) {
100+
if (string.IsNullOrEmpty(condition) == false) {
101+
var conditionProg = Parse(condition);
102+
if (conditionProg == null) {
103+
return false;
104+
}
105+
conditionCache.ConditionProg = conditionProg;
86106
}
87-
conditionCache.Condition = conditionProg;
88107
}
89108
List<Internals.ConditionCache> list;
90109
if (conditionCaches.TryGetValue(categoryName, out list) == false) {

csharp/ToolGood.Algorithm2/Internals/ConditionCache.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ internal class ConditionCache
1313

1414
public string ConditionString;
1515

16-
private ProgContext _Condition;
17-
public ProgContext Condition {
16+
private ProgContext _ConditionProg;
17+
public ProgContext ConditionProg {
1818
get {
19-
if (_Condition == null && string.IsNullOrEmpty(ConditionString) == false && string.IsNullOrEmpty(LastError)) {
20-
_Condition = Parse(ConditionString);
19+
if (_ConditionProg == null && string.IsNullOrEmpty(ConditionString) == false && string.IsNullOrEmpty(LastError)) {
20+
_ConditionProg = Parse(ConditionString);
2121
}
22-
return _Condition;
22+
return _ConditionProg;
2323
}
24-
set { _Condition = value; }
24+
set { _ConditionProg = value; }
2525
}
2626

2727
public string FormulaString;
28-
private ProgContext _Formula;
29-
public ProgContext Formula {
28+
private ProgContext _FormulaProg;
29+
public ProgContext FormulaProg {
3030
get {
31-
if (_Formula == null && string.IsNullOrEmpty(FormulaString) == false && string.IsNullOrEmpty(LastError)) {
32-
_Formula = Parse(FormulaString);
31+
if (_FormulaProg == null && string.IsNullOrEmpty(FormulaString) == false && string.IsNullOrEmpty(LastError)) {
32+
_FormulaProg = Parse(FormulaString);
3333
}
34-
return _Formula;
34+
return _FormulaProg;
3535
}
36-
set { _Formula = value; }
36+
set { _FormulaProg = value; }
3737
}
3838

3939

0 commit comments

Comments
 (0)