WIP: Resolve all code issues

This commit is contained in:
Michał Zieliski
2024-06-18 18:39:02 +02:00
parent f93bb9cd42
commit c2a98e0386
10 changed files with 363 additions and 441 deletions

View File

@@ -2,62 +2,57 @@ using System.Globalization;
using DiunaBIWebAPI.dataProcessors;
using WebAPI.Models;
using AngouriMath;
using AngouriMath.Extensions;
namespace WebAPI.Calculator
namespace WebAPI.Calculator;
public class BaseCalc
{
public class BaseCalc
public string Expression { get; }
private string ResultCode { get; set; }
private string Formula { get; }
public BaseCalc(string expression)
{
public string Expresion { get; set; }
private string ResultCode { get; set; }
private string Formula { get; set; }
Expression = expression;
Formula = Expression.Split("=")[1];
ResultCode = Expression.Split("=")[0];
}
public BaseCalc(string expresion)
public bool IsFormulaCorrect()
{
// check left side of expression
if (!ResultCode.StartsWith("[") || !ResultCode.EndsWith("]"))
{
Expresion = expresion;
Formula = Expresion.Split("=")[1];
ResultCode = Expresion.Split("=")[0];
return false;
}
if (!ResultCode.Substring(1, ResultCode.Length - 2).All(char.IsDigit))
{
return false;
}
public bool IsFormulaCorrect()
{
// check left side of expression
if (!ResultCode.StartsWith("[") || !ResultCode.EndsWith("]"))
{
return false;
}
if (!ResultCode.Substring(1, ResultCode.Length - 2).All(char.IsDigit))
{
return false;
}
ResultCode = ResultCode.Substring(1, ResultCode.Length - 2);
ResultCode = ResultCode.Substring(1, ResultCode.Length - 2);
// check right side of expression
if (!(!string.IsNullOrEmpty(Formula) &&
Formula.All(c => char.IsDigit(c) || c == '[' || c == ']' || c == '+')))
{
return false;
}
return true;
}
// check right side of expression
return !string.IsNullOrEmpty(Formula) &&
Formula.All(c => char.IsDigit(c) || c == '[' || c == ']' || c == '+');
}
public Record CalculateT3(List<Record> records)
public Record CalculateT3(List<Record> records)
{
var resultCode = ResultCode;
{
Record result = new Record
var result = new Record
{
Id = Guid.NewGuid(),
Code = this.ResultCode,
Code = resultCode,
CreatedAt = DateTime.UtcNow,
ModifiedAt = DateTime.UtcNow,
ModifiedAt = DateTime.UtcNow
};
List<string> codes = GetCodes();
List<Record> ingredients = new List<Record>();
foreach (string code in codes)
var codes = GetCodes();
var ingredients = new List<Record>();
foreach (var code in codes)
{
Record? ingredient = records.FirstOrDefault(r => r.Code == code);
var ingredient = records.FirstOrDefault(r => r.Code == code);
if (ingredient == null)
{
throw new Exception($"Record for code {code} not found.");
@@ -65,44 +60,39 @@ namespace WebAPI.Calculator
ingredients.Add(ingredient);
}
for (int i = 1; i <= 32; i++)
for (var i = 1; i <= 32; i++)
{
string formula = Formula;
foreach (Record ingredient in ingredients)
{
formula = formula.Replace($"[{ingredient.Code}]", ProcessHelper.getValue(ingredient, i)?.ToString(CultureInfo.InvariantCulture));
}
if (formula.Contains("["))
var formula = ingredients.Aggregate(Formula, (current, ingredient) => current.Replace($"[{ingredient.Code}]", ProcessHelper.getValue(ingredient, i)?.ToString(CultureInfo.InvariantCulture)));
if (formula.Contains('['))
{
throw new Exception($"Not all placeholders were replaced. Value{i} [{formula}]");
}
Entity expr = formula;
double val = (double)expr.EvalNumerical();
ProcessHelper.setValue(result, i, (double)expr.EvalNumerical());
}
return result;
}
}
private List<string> GetCodes()
private List<string> GetCodes()
{
var codes = new List<string>();
var endIndex = -1;
while (true)
{
List<string> codes = new List<string>();
int endIndex = -1;
var startIndex = Formula.IndexOf("[", endIndex + 1, StringComparison.CurrentCulture);
endIndex = Formula.IndexOf("]", startIndex + 1, StringComparison.CurrentCulture);
while (true)
if (startIndex == -1 || endIndex == -1)
{
int startIndex = Formula.IndexOf("[", endIndex + 1, StringComparison.CurrentCulture);
endIndex = Formula.IndexOf("]", startIndex + 1, StringComparison.CurrentCulture);
if (startIndex == -1 || endIndex == -1)
{
break;
}
string valueCode = Formula.Substring(startIndex + 1, endIndex - startIndex - 1);
codes.Add(valueCode);
break;
}
return codes;
var valueCode = Formula.Substring(startIndex + 1, endIndex - startIndex - 1);
codes.Add(valueCode);
}
return codes;
}
}