Smart rounding was ajusted

This commit is contained in:
Evgeny Redikultsev
2024-06-02 16:56:44 +05:00
parent 99d5aa3608
commit 31d668b996
58 changed files with 716 additions and 274 deletions

View File

@@ -0,0 +1,7 @@
namespace StructureHelperCommon.Models.Parameters
{
public interface IProcessValuePairLogic<T>
{
ValuePair<T> GetValuePairByString(string s);
}
}

View File

@@ -0,0 +1,12 @@
namespace StructureHelperCommon.Models.Parameters
{
/// <summary>
/// Represent pair of value with text
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IValuePair<T>
{
string Text { get; set; }
T Value { get; set; }
}
}

View File

@@ -7,14 +7,12 @@ using System.Windows.Media;
namespace StructureHelperCommon.Models.Parameters
{
public interface IValueParameter<T>
public interface IValueParameter<T> : IValuePair<T>
{
bool IsValid { get; set; }
string Name { get; set; }
string ShortName { get; set; }
Color Color { get; set; }
string MeasurementUnit { get; set; }
T Value { get; set; }
string Description { get; set; }
}
}

View File

@@ -0,0 +1,65 @@
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Services.Units;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;
namespace StructureHelperCommon.Models.Parameters
{
public enum DigitPlace
{
Start,
Any
}
public class ProcessDoublePairLogic : IProcessValuePairLogic<double>
{
const string digitalPattern = @"^[-]?[+]?\d*\.?\,?\d*";
const string allowedPattern = @"[0-9]|\.|\,";
const string characterPattern = "[a-z]+$";
const string target = "";
public DigitPlace DigitPlace { get; set; } = DigitPlace.Start;
public ValuePair<double> GetValuePairByString(string s)
{
s = s.Replace(" ", string.Empty);
Regex regexText = new (allowedPattern);
string textString = regexText.Replace(s, target);
var textMatch = Regex.Match(textString, characterPattern, RegexOptions.IgnoreCase);
if (textMatch.Success == true)
{
textString = textMatch.Value.ToLower();
}
var digitalOnlyString = DigitPlace == DigitPlace.Start ? s : s.ToLower().Replace(textString, string.Empty);
var match = Regex.Match(digitalOnlyString, digitalPattern);
if (match.Success == true)
{
return GetDoubleValue(textString, match);
}
throw new StructureHelperException(ErrorStrings.DataIsInCorrect);
}
private static ValuePair<double> GetDoubleValue(string textString, Match match)
{
string digitalString = match.Value;
if (digitalString != string.Empty || digitalString != "")
{
double digit = ProcessString.ConvertCommaToCultureSettings(digitalString);
return new ValuePair<double>()
{
Value = digit,
Text = textString
};
}
else
{
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + ": value does not contain digital simbols");
}
}
}
}

View File

@@ -4,11 +4,12 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services.Units
namespace StructureHelperCommon.Models.Parameters
{
internal class StringDoublePair : IStringDoublePair
/// <inheritdoc/>
public class ValuePair<T> : IValuePair<T>
{
public double Digit { get; set; }
public string Text { get; set; }
public T Value { get; set; }
}
}

View File

@@ -13,7 +13,7 @@ namespace StructureHelperCommon.Models.Parameters
public string Name { get; set; }
public string ShortName { get; set; }
public Color Color { get; set; }
public string MeasurementUnit { get; set; }
public string Text { get; set; }
public T Value { get; set; }
public string Description { get; set; }
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services
{
public class DirectRoundLogic : IMathRoundLogic
{
public double RoundValue(double value)
{
return value;
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services
{
public class FixedRoundLogic : IDigitRoundLogic
{
public int DigitQuant { get; set; }
/// <summary>
/// Умное окруление до нужного числа значащих цифр, например (12345, 3) дает результат 12300, например (0.12345, 3) дает результат 0,123
/// </summary>
/// <param name="value"></param>
/// <param name="quant"></param>
/// <returns></returns>
public double RoundValue(double value)
{
double roundedValue = Math.Round(value, DigitQuant);
return roundedValue;
}
}
}

View File

@@ -0,0 +1,7 @@
namespace StructureHelperCommon.Services
{
public interface IDigitRoundLogic : IMathRoundLogic
{
int DigitQuant { get; set; }
}
}

View File

@@ -0,0 +1,7 @@
namespace StructureHelperCommon.Services
{
public interface IMathRoundLogic
{
double RoundValue(double value);
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services
{
public class SmartRoundLogic : IDigitRoundLogic
{
public int DigitQuant { get; set; } = 3;
/// <summary>
/// Умное окруление до нужного числа значащих цифр, например (12345, 3) дает результат 12300, например (0.12345, 3) дает результат 0,123
/// </summary>
/// <param name="value"></param>
/// <param name="quant"></param>
/// <returns></returns>
public double RoundValue(double value)
{
if (value == 0d) return 0d;
double valueOrder = Math.Log10(Math.Abs(value));
int order = Convert.ToInt32(Math.Ceiling(valueOrder));
double requiredOrder = Math.Pow(10, DigitQuant - order);
double roundedAbsValue = Math.Round(Math.Abs(value) * requiredOrder) / requiredOrder;
double roundedValue = Math.Sign(value) * roundedAbsValue;
return roundedValue;
}
}
}

View File

@@ -1,116 +0,0 @@
using StructureHelperCommon.Infrastructures.Enums;
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Services.Units;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Documents;
namespace StructureHelperCommon.Services.Units
{
public static class CommonOperation
{
private static IEnumerable<IUnit> units = UnitsFactory.GetUnitCollection();
public static double ConvertToDoubleChangeComma(string s)
{
double result;
if (!double.TryParse(s, NumberStyles.Any, CultureInfo.CurrentCulture, out result) &&
!double.TryParse(s, NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out result) &&
!double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
{
throw new StructureHelperException($"{ErrorStrings.IncorrectValue}: {s}");
}
return result;
}
public static IStringDoublePair DivideIntoStringDoublePair(string s)
{
s = s.Replace(" ", "");
//string digitPattern = @"^[-]?[+]?\d+(\.?)|(\,?)\d*";
string digitPattern = @"^[-]?[+]?\d*\.?\,?\d*";
string textPattern = @"[0-9]|\.|\,";
string caracterPattern = "[a-z]+$";
string target = "";
Regex regexText = new Regex(textPattern);
string textString = regexText.Replace(s, target);
var textMatch = Regex.Match(textString, caracterPattern, RegexOptions.IgnoreCase);
if (textMatch.Success) {textString = textMatch.Value.ToLower();}
var match = Regex.Match(s, digitPattern);
if (match.Success)
{
string digitString = match.Value;
double digit = ConvertToDoubleChangeComma(digitString);
return new StringDoublePair() { Digit = digit, Text = textString };
}
throw new StructureHelperException(ErrorStrings.DataIsInCorrect);
}
public static IUnit GetUnit(UnitTypes unitType, string unitName = null)
{
if (unitName is null)
{
var boolResult = DefaultUnitNames.TryGetValue(unitType, out unitName);
if (boolResult == false)
{
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + $": unit type{unitType} is unknown");
}
}
return units.Where(u => u.UnitType == unitType & u.Name == unitName).Single();
}
public static Dictionary<UnitTypes, string> DefaultUnitNames => new()
{
{ UnitTypes.Length, "m"},
{ UnitTypes.Area, "m2"},
{ UnitTypes.Force, "kN" },
{ UnitTypes.Moment, "kNm"},
{ UnitTypes.Stress, "MPa"},
{ UnitTypes.Curvature, "1/m"},
};
public static string Convert(IUnit unit, string unitName, object value)
{
double val;
if (value != null) { val = (double)value; }
else { throw new Exception($"{unitName} value is null"); }
val *= unit.Multiplyer;
string strValue = $"{val} {unit.Name}";
return strValue;
}
public static double ConvertBack(UnitTypes unitType, IUnit unit, object value)
{
double val;
double multy;
double coefficient = unit.Multiplyer;
var strVal = value as string;
IStringDoublePair pair = DivideIntoStringDoublePair(strVal);
try
{
multy = GetMultiplyer(unitType, pair.Text);
}
catch (Exception ex)
{
multy = coefficient;
}
val = pair.Digit / multy;
return val;
}
public static double GetMultiplyer(UnitTypes unitType, string unitName)
{
try
{
return units.Where(u => u.UnitType == unitType & u.Name == unitName).Single().Multiplyer;
}
catch (Exception ex)
{
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + ex);
}
}
}
}

View File

@@ -0,0 +1,85 @@
using StructureHelperCommon.Infrastructures.Enums;
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Models.Parameters;
using StructureHelperCommon.Services.Units;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Documents;
namespace StructureHelperCommon.Services.Units
{
public class ConvertUnitLogic : IConvertUnitLogic
{
private static IEnumerable<IUnit> units = UnitsFactory.GetUnitCollection();
private static IProcessValuePairLogic<double> pairLogic = new ProcessDoublePairLogic();
public IMathRoundLogic MathRoundLogic { get; set; } = new DirectRoundLogic();
public ValuePair<double> Convert(IUnit unit, string unitName, object value)
{
double val;
if (value != null)
{
try
{
val = (double)value;
}
catch (Exception ex)
{
throw new StructureHelperException($"{ErrorStrings.IncorrectValue}");
}
}
else
{
throw new StructureHelperException($"{ErrorStrings.ParameterIsNull}: {unitName}");
}
val *= unit.Multiplyer;
var pair = new ValuePair<double>
{
Text = unit.Name,
Value = val
};
return pair;
}
public double ConvertBack(UnitTypes unitType, IUnit unit, object value)
{
double val;
double multy;
double factor = unit.Multiplyer;
var strVal = value as string;
var pair = pairLogic.GetValuePairByString(strVal);
try
{
multy = GetMultiplyer(unitType, pair.Text);
}
catch (Exception ex)
{
multy = factor;
}
val = pair.Value / multy;
return val;
}
private double GetMultiplyer(UnitTypes unitType, string unitName)
{
try
{
return units
.Where(u =>
u.UnitType == unitType
&
u.Name == unitName)
.Single()
.Multiplyer;
}
catch (Exception ex)
{
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + ex);
}
}
}
}

View File

@@ -0,0 +1,50 @@
using StructureHelperCommon.Infrastructures.Enums;
using StructureHelperCommon.Infrastructures.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services.Units
{
public class GetUnitLogic : IGetUnitLogic
{
private static IEnumerable<IUnit> units = UnitsFactory.GetUnitCollection();
private Dictionary<UnitTypes, string> defaultUnitNames;
public GetUnitLogic()
{
defaultUnitNames = new()
{
{ UnitTypes.Length, "m"},
{ UnitTypes.Area, "m2"},
{ UnitTypes.Force, "kN" },
{ UnitTypes.Moment, "kNm"},
{ UnitTypes.Stress, "MPa"},
{ UnitTypes.Curvature, "1/m"},
};
}
public IUnit GetUnit(UnitTypes unitType, string unitName = null)
{
if (unitName is null)
{
var boolResult = defaultUnitNames.TryGetValue(unitType, out unitName);
if (boolResult == false)
{
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + $": unit type{unitType} is unknown");
}
}
return units
.Where(u =>
u.UnitType == unitType
&
u.Name == unitName)
.Single();
}
}
}

View File

@@ -0,0 +1,13 @@
using StructureHelperCommon.Infrastructures.Enums;
using StructureHelperCommon.Models.Parameters;
using System.Collections.Generic;
namespace StructureHelperCommon.Services.Units
{
public interface IConvertUnitLogic
{
IMathRoundLogic MathRoundLogic { get; set; }
ValuePair<double> Convert(IUnit unit, string unitName, object value);
double ConvertBack(UnitTypes unitType, IUnit unit, object value);
}
}

View File

@@ -0,0 +1,9 @@
using StructureHelperCommon.Infrastructures.Enums;
namespace StructureHelperCommon.Services.Units
{
public interface IGetUnitLogic
{
IUnit GetUnit(UnitTypes unitType, string unitName = null);
}
}

View File

@@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services.Units
{
public interface IStringDoublePair
{
double Digit { get; }
string Text { get; }
}
}

View File

@@ -0,0 +1,25 @@
using StructureHelperCommon.Infrastructures.Exceptions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services.Units
{
public static class ProcessString
{
public static double ConvertCommaToCultureSettings(string s)
{
double result;
if (!double.TryParse(s, NumberStyles.Any, CultureInfo.CurrentCulture, out result) &&
!double.TryParse(s, NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out result) &&
!double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
{
throw new StructureHelperException($"{ErrorStrings.IncorrectValue}: {s}");
}
return result;
}
}
}

View File

@@ -5,6 +5,10 @@ namespace StructureHelperCommon.Services.Units
{
public static class UnitsFactory
{
/// <summary>
/// Returns collection of unit
/// </summary>
/// <returns></returns>
public static List<IUnit> GetUnitCollection()
{
List<IUnit> units = new List<IUnit>();
@@ -28,8 +32,8 @@ namespace StructureHelperCommon.Services.Units
type = UnitTypes.Moment;
units.Add(new Unit() { UnitType = type, Name = "Nm", Multiplyer = 1d });
units.Add(new Unit() { UnitType = type, Name = "kNm", Multiplyer = 1e-3d });
units.Add(new Unit() { UnitType = type, Name = "kgfm", Multiplyer = 9.8d });
units.Add(new Unit() { UnitType = type, Name = "tfm", Multiplyer = 9.8e-3d });
units.Add(new Unit() { UnitType = type, Name = "kgfm", Multiplyer = 9.81d });
units.Add(new Unit() { UnitType = type, Name = "tfm", Multiplyer = 9.81e-3d });
type = UnitTypes.Curvature;
units.Add(new Unit() { UnitType = type, Name = "1/m", Multiplyer = 1d });
units.Add(new Unit() { UnitType = type, Name = "1/mm", Multiplyer = 1e-3d });