Files
DiunaBI/WebAPI/Calculators/BaseCalc.cs

144 lines
4.3 KiB
C#
Raw Normal View History

2024-05-24 20:38:32 +02:00
using System.Globalization;
using DiunaBIWebAPI.dataProcessors;
using WebAPI.Models;
using AngouriMath;
2024-06-18 18:39:02 +02:00
namespace WebAPI.Calculator;
public class BaseCalc
2024-05-24 20:38:32 +02:00
{
2024-06-18 18:39:02 +02:00
public string Expression { get; }
private string ResultCode { get; set; }
private string Formula { get; }
public BaseCalc(string expression)
2024-05-24 20:38:32 +02:00
{
2024-06-18 18:39:02 +02:00
Expression = expression;
Formula = Expression.Split("=")[1];
ResultCode = Expression.Split("=")[0];
}
2024-05-24 20:38:32 +02:00
2024-06-18 18:39:02 +02:00
public bool IsFormulaCorrect()
{
// check left side of expression
2024-06-18 22:24:04 +02:00
if (!ResultCode.StartsWith('[') || !ResultCode.EndsWith(']'))
2024-05-24 20:38:32 +02:00
{
2024-06-18 18:39:02 +02:00
return false;
2024-05-24 20:38:32 +02:00
}
2024-08-13 23:50:59 +02:00
2024-06-18 18:39:02 +02:00
if (!ResultCode.Substring(1, ResultCode.Length - 2).All(char.IsDigit))
2024-05-24 20:38:32 +02:00
{
2024-06-18 18:39:02 +02:00
return false;
2024-05-24 20:38:32 +02:00
}
2024-06-18 18:39:02 +02:00
ResultCode = ResultCode.Substring(1, ResultCode.Length - 2);
// check right side of expression
return !string.IsNullOrEmpty(Formula) &&
Formula.All(c => char.IsDigit(c) || c == '[' || c == ']' || c == '+');
}
public Record CalculateT3(List<Record> records)
{
var resultCode = ResultCode;
2024-05-24 20:38:32 +02:00
{
2024-06-18 18:39:02 +02:00
var result = new Record
2024-05-24 20:38:32 +02:00
{
Id = Guid.NewGuid(),
2024-06-18 18:39:02 +02:00
Code = resultCode,
2024-05-24 20:38:32 +02:00
CreatedAt = DateTime.UtcNow,
2024-06-18 18:39:02 +02:00
ModifiedAt = DateTime.UtcNow
2024-05-24 20:38:32 +02:00
};
2024-06-18 18:39:02 +02:00
var codes = GetCodes();
var ingredients = new List<Record>();
foreach (var code in codes)
2024-05-24 20:38:32 +02:00
{
2024-06-18 18:39:02 +02:00
var ingredient = records.FirstOrDefault(r => r.Code == code);
2024-05-24 20:38:32 +02:00
if (ingredient == null)
{
throw new Exception($"Record for code {code} not found.");
}
2024-08-13 23:50:59 +02:00
2024-05-24 20:38:32 +02:00
ingredients.Add(ingredient);
}
2024-08-13 23:50:59 +02:00
2024-06-18 18:39:02 +02:00
for (var i = 1; i <= 32; i++)
2024-05-24 20:38:32 +02:00
{
2024-08-13 23:50:59 +02:00
var formula = ingredients.Aggregate(Formula,
(current, ingredient) => current.Replace($"[{ingredient.Code}]",
ProcessHelper.GetValue(ingredient, i)?.ToString(CultureInfo.InvariantCulture)));
2024-06-18 18:39:02 +02:00
if (formula.Contains('['))
2024-05-24 20:38:32 +02:00
{
throw new Exception($"Not all placeholders were replaced. Value{i} [{formula}]");
}
Entity expr = formula;
2024-06-18 19:40:16 +02:00
ProcessHelper.SetValue(result, i, (double)expr.EvalNumerical());
2024-05-24 20:38:32 +02:00
}
2024-08-13 23:50:59 +02:00
return result;
}
}
public Record CalculateT1(List<Record> records)
{
var resultCode = ResultCode;
{
var result = new Record
{
Id = Guid.NewGuid(),
Code = resultCode,
CreatedAt = DateTime.UtcNow,
ModifiedAt = DateTime.UtcNow
};
var codes = GetCodes();
var ingredients = new List<Record>();
foreach (var code in codes)
{
var ingredient = records.FirstOrDefault(r => r.Code == code);
if (ingredient == null)
{
throw new Exception($"Record for code {code} not found.");
}
ingredients.Add(ingredient);
}
var formula = ingredients.Aggregate(Formula,
(current, ingredient) => current.Replace($"[{ingredient.Code}]",
ProcessHelper.GetValue(ingredient, 32)?.ToString(CultureInfo.InvariantCulture)));
if (formula.Contains('['))
{
throw new Exception($"Not all placeholders were replaced. Value{1} [{formula}]");
}
Entity expr = formula;
ProcessHelper.SetValue(result, 32, (double)expr.EvalNumerical());
2024-05-24 20:38:32 +02:00
return result;
}
2024-06-18 18:39:02 +02:00
}
2024-05-24 20:38:32 +02:00
2024-06-18 18:39:02 +02:00
private List<string> GetCodes()
{
var codes = new List<string>();
var endIndex = -1;
while (true)
2024-05-24 20:38:32 +02:00
{
2024-06-18 18:39:02 +02:00
var startIndex = Formula.IndexOf("[", endIndex + 1, StringComparison.CurrentCulture);
endIndex = Formula.IndexOf("]", startIndex + 1, StringComparison.CurrentCulture);
2024-05-24 20:38:32 +02:00
2024-06-18 18:39:02 +02:00
if (startIndex == -1 || endIndex == -1)
2024-05-24 20:38:32 +02:00
{
2024-06-18 18:39:02 +02:00
break;
2024-05-24 20:38:32 +02:00
}
2024-06-18 18:39:02 +02:00
var valueCode = Formula.Substring(startIndex + 1, endIndex - startIndex - 1);
codes.Add(valueCode);
2024-05-24 20:38:32 +02:00
}
2024-08-13 23:50:59 +02:00
2024-06-18 18:39:02 +02:00
return codes;
2024-05-24 20:38:32 +02:00
}
}