123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using JLHHJSvr.Com.Model;
- using JLHHJSvr.LJException;
- using JLHHJSvr.LJFramework.Tools;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace JLHHJSvr.Tools
- {
- public class CalculateFormula
- {
- private HashSet<FormulaItem> _formula;
- private HashSet<FormulaItem> _const;
- public CalculateFormula()
- {
- _formula = new HashSet<FormulaItem>();
- _const = new HashSet<FormulaItem>();
- }
- public void AddFormulaItem(FormulaItem item)
- {
- if(!_formula.Add(item))
- {
- _formula.Remove(item);
- _formula.Add(item);
- }
- }
- public void AddConstFormulaItem(FormulaItem item)
- {
- if (!_const.Add(item))
- {
- _const.Remove(item);
- _const.Add(item);
- }
- }
- public void AddFormulaItem(string name,object value)
- {
- AddConstFormulaItem(new FormulaItem() { formula_name = name, value = value });
- }
- public void AddFormulaItem(string name, string formula)
- {
- var formulaItem = new FormulaItem() { formula_name = name, formula = formula }
- AddFormulaItem(formulaItem);
- BuildFormulaGraph(formulaItem);
- }
- // 构建拓扑图
- private void BuildFormulaGraph(FormulaItem formula)
- {
- formula.graph = new List<string>();
- // 定义正则表达式模式,匹配包含【】的内容
- string pattern = @"【(.*?)】";
- // 创建正则表达式对象
- Regex regex = new Regex(pattern);
- // 使用正则表达式匹配输入字符串
- MatchCollection matches = regex.Matches(formula.formula);
- foreach (Match match in matches)
- {
- formula.graph.Add(match.Value);
- }
- }
- private string ConvertToEnglishSymbols(string input)
- {
- input = input.Replace("(", "(")
- .Replace(")", ")")
- .Replace(",", ",")
- .Replace("。", ".")
- .Replace(":", ":")
- .Replace(";", ";")
- .Replace("“", "\"")
- .Replace("”", "\"")
- .Replace("‘", "'")
- .Replace("’", "'");
- return input;
- }
- private void FormatExpression(FormulaItem formula)
- {
- formula.formula = ConvertToEnglishSymbols(formula.formula);
- }
- /// <summary>
- /// 公式计算
- /// </summary>
- /// <param name="expression"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- private void Calculate(FormulaItem formula)
- {
- FormatExpression(formula);
- try
- {
- formula.value = LJExprParser.Parse(formula.formula).Result;
- }
- catch (Exception ex)
- {
- throw new LJCommonException($"计算{formula.formula_name}公式错误: {ex.Message}");
- }
- }
- /// <summary>
- /// Sql公式计算
- /// </summary>
- /// <param name="expression"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- private void SqlCalculate(FormulaItem formula)
- {
- FormatExpression(formula);
- try
- {
- //cmd.CommandText = $@"{formula.formula}";
- //cmd.Parameters.Clear();
- //return cmd.ExecuteScalar();
- }
- catch (Exception ex)
- {
- throw new LJCommonException($"计算{formula.formula_name}公式错误: {ex.Message}");
- }
- }
- }
- }
|