Skip to content

Commit

Permalink
添加 EvaluateFormula
Browse files Browse the repository at this point in the history
  • Loading branch information
toolgood committed Jan 18, 2022
1 parent e29dff2 commit 1a04db2
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public void Test()

var p4 = priceAlgorithm.TryEvaluate("出错了", 0.0);


var tt = priceAlgorithm.EvaluateFormula("长-宽-高", '-');
Assert.AreEqual("9-1.3-1", tt);
}
[Test]
public void Test2()
Expand Down
6 changes: 3 additions & 3 deletions csharp/ToolGood.Algorithm2/AlgorithmEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ public String EvaluateFormula(String formula, params char[] splitChars)
/// <param name="formula">公式</param>
/// <param name="splitChars">分隔符</param>
/// <returns></returns>
public String EvaluateFormula(String formula, List<char> splitChars)
public virtual String EvaluateFormula(String formula, List<char> splitChars)
{
if (string.IsNullOrEmpty(formula)) return "";

Expand All @@ -732,11 +732,11 @@ public String EvaluateFormula(String formula, List<char> splitChars)
} else {
// TODO 替换此处
String d = TryEvaluate(s, "");
stringBuilder.Append(d.ToString());
stringBuilder.Append(d);
}
}
return stringBuilder.ToString();
}
}
#endregion

}
Expand Down
86 changes: 85 additions & 1 deletion csharp/ToolGood.Algorithm2/AlgorithmEngineEx.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Antlr4.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -736,5 +737,88 @@ private Operand Evaluate(ProgContext context)

}

#region EvaluateFormula
/// <summary>
/// 计算公式
/// </summary>
/// <param name="formula">公式</param>
/// <param name="splitChars">分隔符</param>
/// <returns></returns>
public String EvaluateFormula(String formula, params char[] splitChars)
{
if (string.IsNullOrEmpty(formula)) return "";
return EvaluateFormula(formula, splitChars.ToList());
}
/// <summary>
/// 计算公式
/// </summary>
/// <param name="formula">公式</param>
/// <param name="splitChars">分隔符</param>
/// <returns></returns>
public virtual String EvaluateFormula(String formula, List<char> splitChars)
{
if (string.IsNullOrEmpty(formula)) return "";

List<String> sp = CharUtil.SplitFormula(formula, splitChars);

StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < sp.Count; i++) {
String s = sp[i];
if (s.Length == 1 && splitChars.Contains(s[0])) {
stringBuilder.Append(s);
} else {
String d = "";
try {
Operand operand = EvaluateOnce(s);
d = operand.ToText().TextValue;
} catch (Exception) { }
stringBuilder.Append(d);
}
}
return stringBuilder.ToString();
}
/// <summary>
/// 执行一次
/// </summary>
/// <param name="exp"></param>
/// <returns></returns>
protected Operand EvaluateOnce(string exp)
{
ProgContext context = Parse(exp);
return Evaluate(context);
}

private ProgContext Parse(string exp)
{
if (string.IsNullOrWhiteSpace(exp)) { return null; }
try {
var stream = new AntlrCharStream(new AntlrInputStream(exp));
var lexer = new mathLexer(stream);
var tokens = new CommonTokenStream(lexer);
var parser = new mathParser(tokens);
var antlrErrorListener = new AntlrErrorListener();
parser.RemoveErrorListeners();
parser.AddErrorListener(antlrErrorListener);

var context = parser.prog();
var end = context.Stop.StopIndex;
if (end + 1 < exp.Length) {

LastError = "Parameter exp invalid !";
return null;
}
if (antlrErrorListener.IsError) {
LastError = antlrErrorListener.ErrorMsg;
return null;
}
return context;
} catch (Exception ex) {
LastError = ex.Message;
}
return null;
}
#endregion


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public String EvaluateFormula(String formula, List<Character> splitChars) {
} else {
// TODO 替换此处
String d = TryEvaluate(s, "");
stringBuilder.append(d.toString());
stringBuilder.append(d);
}
}
return stringBuilder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
import java.util.Map;
import java.util.function.Function;

import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.joda.time.DateTime;

import toolgood.algorithm.internals.AntlrCharStream;
import toolgood.algorithm.internals.AntlrErrorListener;
import toolgood.algorithm.internals.CharUtil;
import toolgood.algorithm.internals.ConditionCacheInfo;
import toolgood.algorithm.internals.MathVisitor;
import toolgood.algorithm.internals.MyFunction;
import toolgood.algorithm.litJson.JsonData;
import toolgood.algorithm.litJson.JsonMapper;
import toolgood.algorithm.math.mathLexer;
import toolgood.algorithm.math.mathParser;
import toolgood.algorithm.math.mathParser2.ProgContext;

/**
Expand Down Expand Up @@ -374,4 +381,89 @@ private Operand Evaluate(ProgContext context) {
}
}

/**
* 计算公式
*
* @param formula 公式
* @param splitChar 分隔符
* @return
*/
public String EvaluateFormula(String formula, Character splitChar) {
if (formula == null || formula.equals(""))
return "";
List<Character> splitChars = new ArrayList<>();
splitChars.add(splitChar);
return EvaluateFormula(formula, splitChars);
}

/**
* 计算公式
*
* @param formula 公式
* @param splitChars 分隔符
* @return
*/
public String EvaluateFormula(String formula, List<Character> splitChars) {
if (formula == null || formula.equals(""))
return "";
List<String> sp = CharUtil.SplitFormula(formula, splitChars);

StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < sp.size(); i++) {
String s = sp.get(i);
if (s.length() == 1 && splitChars.contains(s.charAt(0))) {
stringBuilder.append(s);
} else {
String d = "";
try {
Operand operand = EvaluateOnce(s);
d = operand.ToText("").TextValue();
} catch (Exception ex) { }
stringBuilder.append(d);
}
}
return stringBuilder.toString();
}

/// <summary>
/// 执行一次
/// </summary>
/// <param name="exp"></param>
/// <returns></returns>
protected Operand EvaluateOnce(String exp) {
ProgContext context = Parse(exp);
return Evaluate(context);
}

private ProgContext Parse(String exp) {
if (exp == null || exp.equals("")) {
LastError = "Parameter exp invalid !";
return null;
}
try {
final AntlrCharStream stream = new AntlrCharStream(CharStreams.fromString(exp));
final mathLexer lexer = new mathLexer(stream);
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final mathParser parser = new mathParser(tokens);
final AntlrErrorListener antlrErrorListener = new AntlrErrorListener();
parser.removeErrorListeners();
parser.addErrorListener(antlrErrorListener);
final ProgContext context = parser.prog();

final int end = context.stop.getStopIndex();
if (end + 1 < exp.length()) {
LastError = "Parameter exp invalid !";
return null;
}
if (antlrErrorListener.IsError) {
LastError = antlrErrorListener.ErrorMsg;
return null;
}
return context;
} catch (Exception ex) {
LastError = ex.getMessage();
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public void Test2() {

Double p4 = priceAlgorithm.TryEvaluate("出错了", 0.0);

String tt = priceAlgorithm.EvaluateFormula("长-宽-高", '-');
assertEquals("9-1.3-1", tt);

}

@Test
Expand Down

0 comments on commit 1a04db2

Please sign in to comment.