|
| 1 | +package toolgood.algorithm; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.function.Function; |
| 9 | + |
| 10 | +import org.joda.time.DateTime; |
| 11 | + |
| 12 | +import toolgood.algorithm.internals.ConditionCacheInfo; |
| 13 | +import toolgood.algorithm.internals.MathVisitor; |
| 14 | +import toolgood.algorithm.internals.MyFunction; |
| 15 | +import toolgood.algorithm.litJson.JsonData; |
| 16 | +import toolgood.algorithm.litJson.JsonMapper; |
| 17 | +import toolgood.algorithm.math.mathParser2.ProgContext; |
| 18 | + |
| 19 | +/** |
| 20 | + * 算法引擎 扩展版 |
| 21 | + * 支持 一层算法 内套 一层算法 |
| 22 | + * 支持多条件过滤 |
| 23 | + */ |
| 24 | +public class AlgorithmEngineEx { |
| 25 | + public boolean UseExcelIndex = true; |
| 26 | + public String LastError; |
| 27 | + private final Map<String, Operand> _dict = new HashMap<String, Operand>(); |
| 28 | + public Function<MyFunction, Operand> DiyFunction; |
| 29 | + public ConditionCache MultiConditionCache; |
| 30 | + |
| 31 | + public AlgorithmEngineEx(ConditionCache multiConditionCache) { |
| 32 | + MultiConditionCache = multiConditionCache; |
| 33 | + } |
| 34 | + |
| 35 | + protected Operand GetParameter(final String parameter) throws Exception { |
| 36 | + if (_dict.containsKey(parameter)) { |
| 37 | + return _dict.get(parameter); |
| 38 | + } |
| 39 | + ArrayList<ConditionCacheInfo> conditionCaches = MultiConditionCache.GetConditionCaches(parameter); |
| 40 | + for (ConditionCacheInfo conditionCache : conditionCaches) { |
| 41 | + if (conditionCache.GetFormulaProg() == null) |
| 42 | + continue; |
| 43 | + if (conditionCache.GetConditionProg() != null) { |
| 44 | + Operand b = Evaluate(conditionCache.GetConditionProg()); |
| 45 | + if (b.IsError()) { |
| 46 | + return Operand.Error("Parameter [" + parameter + "]," + conditionCache.Remark |
| 47 | + + " condition `" + conditionCache.ConditionString + "` is error.\r\n" + b.ErrorMsg()); |
| 48 | + } |
| 49 | + if (b.BooleanValue() == false) |
| 50 | + continue; |
| 51 | + } |
| 52 | + Operand operand = Evaluate(conditionCache.GetFormulaProg()); |
| 53 | + if (operand.IsError()) { |
| 54 | + return Operand.Error("Parameter [" + parameter + "]," + conditionCache.Remark |
| 55 | + + " formula `" + conditionCache.FormulaString + "` is error.\r\n" + operand.ErrorMsg()); |
| 56 | + } |
| 57 | + _dict.put(parameter, operand); |
| 58 | + return operand; |
| 59 | + } |
| 60 | + |
| 61 | + return Operand.Error("Parameter [" + parameter + "] is missing."); |
| 62 | + } |
| 63 | + |
| 64 | + protected Operand ExecuteDiyFunction(final String funcName, final List<Operand> operands) { |
| 65 | + if (DiyFunction != null) { |
| 66 | + MyFunction f = new MyFunction(); |
| 67 | + f.Name = funcName; |
| 68 | + f.OperandList = operands; |
| 69 | + return DiyFunction.apply(f); |
| 70 | + } |
| 71 | + return Operand.Error("DiyFunction [" + funcName + "] is missing."); |
| 72 | + } |
| 73 | + |
| 74 | + public void ClearDiyFunctions() { |
| 75 | + DiyFunction = null; |
| 76 | + } |
| 77 | + |
| 78 | + public void ClearParameters() { |
| 79 | + _dict.clear(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * 执行 |
| 84 | + * |
| 85 | + * @param categoryName |
| 86 | + * @return |
| 87 | + */ |
| 88 | + public Operand Evaluate(String categoryName) { |
| 89 | + Operand operand; |
| 90 | + ArrayList<ConditionCacheInfo> conditionCaches = MultiConditionCache.GetConditionCaches(categoryName); |
| 91 | + for (ConditionCacheInfo conditionCache : conditionCaches) { |
| 92 | + if (conditionCache.FormulaString == null || conditionCache.FormulaString.length() == 0) |
| 93 | + continue; |
| 94 | + if (conditionCache.ConditionString != null && conditionCache.ConditionString.length() > 0) { |
| 95 | + if (conditionCache.GetConditionProg() == null) { |
| 96 | + return Operand.Error("CategoryName [" + categoryName + "]," + conditionCache.Remark |
| 97 | + + " parse condition `" + conditionCache.ConditionString + "` is error.\r\n" |
| 98 | + + conditionCache.LastError); |
| 99 | + } |
| 100 | + Operand b = Evaluate(conditionCache.GetConditionProg()); |
| 101 | + if (b.IsError()) { |
| 102 | + return Operand.Error("Parameter [" + categoryName + "]," + conditionCache.Remark |
| 103 | + + " condition `" + conditionCache.ConditionString + "` is error.\r\n" + b.ErrorMsg()); |
| 104 | + } |
| 105 | + if (b.BooleanValue() == false) |
| 106 | + continue; |
| 107 | + } |
| 108 | + if (conditionCache.GetFormulaProg() == null) { |
| 109 | + return Operand.Error("CategoryName [" + categoryName + "]," + conditionCache.Remark |
| 110 | + + " parse formula `" + conditionCache.FormulaString + "` is error.\r\n" |
| 111 | + + conditionCache.LastError); |
| 112 | + } |
| 113 | + operand = Evaluate(conditionCache.GetFormulaProg()); |
| 114 | + if (operand.IsError()) { |
| 115 | + return Operand.Error("Parameter [" + categoryName + "]," + conditionCache.Remark |
| 116 | + + " formula `" + conditionCache.FormulaString + "` is error.\r\n" + operand.ErrorMsg()); |
| 117 | + } |
| 118 | + return operand; |
| 119 | + } |
| 120 | + return Operand.Error("CategoryName [{categoryName}] is missing."); |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * 查找备注 |
| 126 | + * |
| 127 | + * @param categoryName |
| 128 | + * @return |
| 129 | + * @throws Exception |
| 130 | + */ |
| 131 | + public String SearchRemark(String categoryName) throws Exception { |
| 132 | + ArrayList<ConditionCacheInfo> conditionCaches = MultiConditionCache.GetConditionCaches(categoryName); |
| 133 | + for (ConditionCacheInfo conditionCache : conditionCaches) { |
| 134 | + if (conditionCache.ConditionString != null && conditionCache.ConditionString.length() > 0) { |
| 135 | + if (conditionCache.GetConditionProg() == null) { |
| 136 | + throw new Exception("CategoryName [" + categoryName + "]," + conditionCache.Remark |
| 137 | + + " parse condition `" + conditionCache.ConditionString + "` is error.\r\n" |
| 138 | + + conditionCache.LastError); |
| 139 | + } |
| 140 | + Operand b = Evaluate(conditionCache.GetConditionProg()); |
| 141 | + if (b.IsError()) { |
| 142 | + throw new Exception("Parameter [" + categoryName + "]," + conditionCache.Remark |
| 143 | + + " condition `" + conditionCache.ConditionString + "` is error.\r\n" + b.ErrorMsg()); |
| 144 | + } |
| 145 | + if (b.BooleanValue() == false) |
| 146 | + continue; |
| 147 | + } |
| 148 | + return conditionCache.Remark; |
| 149 | + } |
| 150 | + throw new Exception("CategoryName [{categoryName}] is missing."); |
| 151 | + } |
| 152 | + |
| 153 | + public void AddParameter(final String key, final Operand obj) { |
| 154 | + _dict.put(key, obj); |
| 155 | + } |
| 156 | + |
| 157 | + public void AddParameter(final String key, final boolean obj) { |
| 158 | + _dict.put(key, Operand.Create(obj)); |
| 159 | + } |
| 160 | + |
| 161 | + public void AddParameter(final String key, final short obj) { |
| 162 | + _dict.put(key, Operand.Create(obj)); |
| 163 | + } |
| 164 | + |
| 165 | + public void AddParameter(final String key, final int obj) { |
| 166 | + _dict.put(key, Operand.Create(obj)); |
| 167 | + } |
| 168 | + |
| 169 | + public void AddParameter(final String key, final long obj) { |
| 170 | + _dict.put(key, Operand.Create(obj)); |
| 171 | + } |
| 172 | + |
| 173 | + public void AddParameter(final String key, final float obj) { |
| 174 | + _dict.put(key, Operand.Create(obj)); |
| 175 | + } |
| 176 | + |
| 177 | + public void AddParameter(final String key, final double obj) { |
| 178 | + _dict.put(key, Operand.Create(obj)); |
| 179 | + } |
| 180 | + |
| 181 | + public void AddParameter(final String key, final BigDecimal obj) { |
| 182 | + _dict.put(key, Operand.Create(obj)); |
| 183 | + } |
| 184 | + |
| 185 | + public void AddParameter(final String key, final String obj) { |
| 186 | + _dict.put(key, Operand.Create(obj)); |
| 187 | + } |
| 188 | + |
| 189 | + public void AddParameter(final String key, final MyDate obj) { |
| 190 | + _dict.put(key, Operand.Create(obj)); |
| 191 | + } |
| 192 | + |
| 193 | + public void AddParameter(final String key, final List<Operand> obj) { |
| 194 | + _dict.put(key, Operand.Create(obj)); |
| 195 | + } |
| 196 | + |
| 197 | + public void AddParameterFromJson(final String json) throws Exception { |
| 198 | + if (json.startsWith("{") && json.endsWith("}")) { |
| 199 | + final JsonData jo = (JsonData) JsonMapper.ToObject(json); |
| 200 | + if (jo.IsObject()) { |
| 201 | + for (String item : jo.inst_object.keySet()) { |
| 202 | + final JsonData v = jo.inst_object.get(item); |
| 203 | + if (v.IsString()) |
| 204 | + _dict.put(item, Operand.Create(v.StringValue())); |
| 205 | + else if (v.IsBoolean()) |
| 206 | + _dict.put(item, Operand.Create(v.BooleanValue())); |
| 207 | + else if (v.IsDouble()) |
| 208 | + _dict.put(item, Operand.Create(v.NumberValue())); |
| 209 | + else if (v.IsObject()) |
| 210 | + _dict.put(item, Operand.Create(v)); |
| 211 | + else if (v.IsArray()) |
| 212 | + _dict.put(item, Operand.Create(v)); |
| 213 | + else if (v.IsNull()) |
| 214 | + _dict.put(item, Operand.CreateNull()); |
| 215 | + } |
| 216 | + return; |
| 217 | + } |
| 218 | + } |
| 219 | + throw new Exception("Parameter is not json String."); |
| 220 | + } |
| 221 | + |
| 222 | + public int TryEvaluate(final String categoryName, final int defvalue) { |
| 223 | + try { |
| 224 | + Operand obj = Evaluate(categoryName); |
| 225 | + obj = obj.ToNumber("It can't be converted to number!"); |
| 226 | + if (obj.IsError()) { |
| 227 | + LastError = obj.ErrorMsg(); |
| 228 | + return defvalue; |
| 229 | + } |
| 230 | + return obj.IntValue(); |
| 231 | + } catch (final Exception ex) { |
| 232 | + LastError = ex.getMessage(); |
| 233 | + } |
| 234 | + return defvalue; |
| 235 | + } |
| 236 | + |
| 237 | + public double TryEvaluate(final String categoryName, final double defvalue) { |
| 238 | + try { |
| 239 | + Operand obj = Evaluate(categoryName); |
| 240 | + obj = obj.ToNumber("It can't be converted to number!"); |
| 241 | + if (obj.IsError()) { |
| 242 | + LastError = obj.ErrorMsg(); |
| 243 | + return defvalue; |
| 244 | + } |
| 245 | + return obj.NumberValue(); |
| 246 | + } catch (final Exception ex) { |
| 247 | + LastError = ex.getMessage(); |
| 248 | + } |
| 249 | + return defvalue; |
| 250 | + } |
| 251 | + |
| 252 | + public String TryEvaluate(final String categoryName, final String defvalue) { |
| 253 | + try { |
| 254 | + Operand obj = Evaluate(categoryName); |
| 255 | + if (obj.IsNull()) { |
| 256 | + return null; |
| 257 | + } |
| 258 | + obj = obj.ToText("It can't be converted to String!"); |
| 259 | + if (obj.IsError()) { |
| 260 | + LastError = obj.ErrorMsg(); |
| 261 | + return defvalue; |
| 262 | + } |
| 263 | + return obj.TextValue(); |
| 264 | + } catch (final Exception ex) { |
| 265 | + LastError = ex.getMessage(); |
| 266 | + } |
| 267 | + return defvalue; |
| 268 | + } |
| 269 | + |
| 270 | + public boolean TryEvaluate(final String categoryName, final boolean defvalue) { |
| 271 | + try { |
| 272 | + Operand obj = Evaluate(categoryName); |
| 273 | + obj = obj.ToBoolean("It can't be converted to bool!"); |
| 274 | + if (obj.IsError()) { |
| 275 | + LastError = obj.ErrorMsg(); |
| 276 | + return defvalue; |
| 277 | + } |
| 278 | + return obj.BooleanValue(); |
| 279 | + } catch (final Exception ex) { |
| 280 | + LastError = ex.getMessage(); |
| 281 | + } |
| 282 | + return defvalue; |
| 283 | + } |
| 284 | + |
| 285 | + public DateTime TryEvaluate(final String categoryName, final DateTime defvalue) { |
| 286 | + try { |
| 287 | + Operand obj = Evaluate(categoryName); |
| 288 | + obj = obj.ToDate("It can't be converted to bool!"); |
| 289 | + if (obj.IsError()) { |
| 290 | + LastError = obj.ErrorMsg(); |
| 291 | + return defvalue; |
| 292 | + } |
| 293 | + return obj.DateValue().ToDateTime(); |
| 294 | + } catch (final Exception ex) { |
| 295 | + LastError = ex.getMessage(); |
| 296 | + } |
| 297 | + return defvalue; |
| 298 | + } |
| 299 | + |
| 300 | + public MyDate TryEvaluate(final String categoryName, final MyDate defvalue) { |
| 301 | + try { |
| 302 | + Operand obj = Evaluate(categoryName); |
| 303 | + obj = obj.ToDate("It can't be converted to bool!"); |
| 304 | + if (obj.IsError()) { |
| 305 | + LastError = obj.ErrorMsg(); |
| 306 | + return defvalue; |
| 307 | + } |
| 308 | + return obj.DateValue(); |
| 309 | + } catch (final Exception ex) { |
| 310 | + LastError = ex.getMessage(); |
| 311 | + } |
| 312 | + return defvalue; |
| 313 | + } |
| 314 | + |
| 315 | + private Operand Evaluate(ProgContext context) { |
| 316 | + try { |
| 317 | + final MathVisitor visitor = new MathVisitor(); |
| 318 | + visitor.GetParameter = f -> { |
| 319 | + try { |
| 320 | + return GetParameter(f); |
| 321 | + } catch (Exception e) { |
| 322 | + } |
| 323 | + return null; |
| 324 | + }; |
| 325 | + visitor.excelIndex = UseExcelIndex ? 1 : 0; |
| 326 | + visitor.DiyFunction = f -> { |
| 327 | + return ExecuteDiyFunction(f.Name, f.OperandList); |
| 328 | + }; |
| 329 | + return visitor.visit(context); |
| 330 | + } catch (Exception ex) { |
| 331 | + LastError = ex.getMessage(); |
| 332 | + return Operand.Error(ex.getMessage()); |
| 333 | + } |
| 334 | + } |
| 335 | + |
| 336 | +} |
0 commit comments