|
7 | 7 | import java.util.Map; |
8 | 8 | import java.util.function.Function; |
9 | 9 |
|
| 10 | +import org.antlr.v4.runtime.CharStreams; |
| 11 | +import org.antlr.v4.runtime.CommonTokenStream; |
10 | 12 | import org.joda.time.DateTime; |
11 | 13 |
|
| 14 | +import toolgood.algorithm.internals.AntlrCharStream; |
| 15 | +import toolgood.algorithm.internals.AntlrErrorListener; |
| 16 | +import toolgood.algorithm.internals.CharUtil; |
12 | 17 | import toolgood.algorithm.internals.ConditionCacheInfo; |
13 | 18 | import toolgood.algorithm.internals.MathVisitor; |
14 | 19 | import toolgood.algorithm.internals.MyFunction; |
15 | 20 | import toolgood.algorithm.litJson.JsonData; |
16 | 21 | import toolgood.algorithm.litJson.JsonMapper; |
| 22 | +import toolgood.algorithm.math.mathLexer; |
| 23 | +import toolgood.algorithm.math.mathParser; |
17 | 24 | import toolgood.algorithm.math.mathParser2.ProgContext; |
18 | 25 |
|
19 | 26 | /** |
@@ -374,4 +381,89 @@ private Operand Evaluate(ProgContext context) { |
374 | 381 | } |
375 | 382 | } |
376 | 383 |
|
| 384 | + /** |
| 385 | + * 计算公式 |
| 386 | + * |
| 387 | + * @param formula 公式 |
| 388 | + * @param splitChar 分隔符 |
| 389 | + * @return |
| 390 | + */ |
| 391 | + public String EvaluateFormula(String formula, Character splitChar) { |
| 392 | + if (formula == null || formula.equals("")) |
| 393 | + return ""; |
| 394 | + List<Character> splitChars = new ArrayList<>(); |
| 395 | + splitChars.add(splitChar); |
| 396 | + return EvaluateFormula(formula, splitChars); |
| 397 | + } |
| 398 | + |
| 399 | + /** |
| 400 | + * 计算公式 |
| 401 | + * |
| 402 | + * @param formula 公式 |
| 403 | + * @param splitChars 分隔符 |
| 404 | + * @return |
| 405 | + */ |
| 406 | + public String EvaluateFormula(String formula, List<Character> splitChars) { |
| 407 | + if (formula == null || formula.equals("")) |
| 408 | + return ""; |
| 409 | + List<String> sp = CharUtil.SplitFormula(formula, splitChars); |
| 410 | + |
| 411 | + StringBuilder stringBuilder = new StringBuilder(); |
| 412 | + for (int i = 0; i < sp.size(); i++) { |
| 413 | + String s = sp.get(i); |
| 414 | + if (s.length() == 1 && splitChars.contains(s.charAt(0))) { |
| 415 | + stringBuilder.append(s); |
| 416 | + } else { |
| 417 | + String d = ""; |
| 418 | + try { |
| 419 | + Operand operand = EvaluateOnce(s); |
| 420 | + d = operand.ToText("").TextValue(); |
| 421 | + } catch (Exception ex) { } |
| 422 | + stringBuilder.append(d); |
| 423 | + } |
| 424 | + } |
| 425 | + return stringBuilder.toString(); |
| 426 | + } |
| 427 | + |
| 428 | + /// <summary> |
| 429 | + /// 执行一次 |
| 430 | + /// </summary> |
| 431 | + /// <param name="exp"></param> |
| 432 | + /// <returns></returns> |
| 433 | + protected Operand EvaluateOnce(String exp) { |
| 434 | + ProgContext context = Parse(exp); |
| 435 | + return Evaluate(context); |
| 436 | + } |
| 437 | + |
| 438 | + private ProgContext Parse(String exp) { |
| 439 | + if (exp == null || exp.equals("")) { |
| 440 | + LastError = "Parameter exp invalid !"; |
| 441 | + return null; |
| 442 | + } |
| 443 | + try { |
| 444 | + final AntlrCharStream stream = new AntlrCharStream(CharStreams.fromString(exp)); |
| 445 | + final mathLexer lexer = new mathLexer(stream); |
| 446 | + final CommonTokenStream tokens = new CommonTokenStream(lexer); |
| 447 | + final mathParser parser = new mathParser(tokens); |
| 448 | + final AntlrErrorListener antlrErrorListener = new AntlrErrorListener(); |
| 449 | + parser.removeErrorListeners(); |
| 450 | + parser.addErrorListener(antlrErrorListener); |
| 451 | + final ProgContext context = parser.prog(); |
| 452 | + |
| 453 | + final int end = context.stop.getStopIndex(); |
| 454 | + if (end + 1 < exp.length()) { |
| 455 | + LastError = "Parameter exp invalid !"; |
| 456 | + return null; |
| 457 | + } |
| 458 | + if (antlrErrorListener.IsError) { |
| 459 | + LastError = antlrErrorListener.ErrorMsg; |
| 460 | + return null; |
| 461 | + } |
| 462 | + return context; |
| 463 | + } catch (Exception ex) { |
| 464 | + LastError = ex.getMessage(); |
| 465 | + } |
| 466 | + return null; |
| 467 | + } |
| 468 | + |
377 | 469 | } |
0 commit comments