Skip to content

Commit

Permalink
Fixes #2183 wrong error range (#2184)
Browse files Browse the repository at this point in the history
* Fixes #2183 wrong error range

* Also update rules setter for other rules
  • Loading branch information
msevestre authored Apr 8, 2022
1 parent 834b589 commit a82a845
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 231 deletions.
16 changes: 8 additions & 8 deletions src/PKSim.Assets/PKSimConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,24 +1449,24 @@ public static class Parameter

public static string ValueShouldBeGreaterThanOrEqualToZero(string parameterName) => $"{parameterName} value should be greater than or equal to 0.";

public static string MinGreaterThanDbMinValue(string parameterName, double? dbMinValue, string unit)
public static string MinGreaterThanDbMinValue(string parameterName, string displayMinValue, string unit)
{
return $"Minimum value for {parameterName} should be greater than or equal to {dbMinValue} {unit}.";
return $"Minimum value for {parameterName} should be greater than or equal to {displayMinValue} {unit}.";
}

public static string MaxGreaterThanDbMinValue(string parameterName, double? dbMinValue, string unit)
public static string MaxGreaterThanDbMinValue(string parameterName, string displayMinValue, string unit)
{
return $"Maximum value for {parameterName} should be greater than or equal to {dbMinValue} {unit}.";
return $"Maximum value for {parameterName} should be greater than or equal to {displayMinValue} {unit}.";
}

public static string MaxLessThanDbMaxValue(string parameterName, double? dbMaxValue, string unit)
public static string MaxLessThanDbMaxValue(string parameterName, string displayMaxValue, string unit)
{
return $"Maximum value for {parameterName} should be less than or equal to {dbMaxValue} {unit}.";
return $"Maximum value for {parameterName} should be less than or equal to {displayMaxValue} {unit}.";
}

public static string MinLessThanDbMaxValue(string parameterName, double? dbMaxValue, string unit)
public static string MinLessThanDbMaxValue(string parameterName, string displayMaxValue, string unit)
{
return $"Minimum value for {parameterName} should be less than or equal to {dbMaxValue} {unit}.";
return $"Minimum value for {parameterName} should be less than or equal to {displayMaxValue} {unit}.";
}

public static string ValueSmallerThanMax(string parameterName, string value, string unit)
Expand Down
29 changes: 10 additions & 19 deletions src/PKSim.BatchTool/DTO/FolderDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,19 @@ public FolderDTO()

private static class AllRules
{
public static IBusinessRule FolderDefined
{
get { return GenericRules.NonEmptyRule<FolderDTO>(x => x.Folder); }
}
public static IBusinessRule FolderDefined { get; } = GenericRules.NonEmptyRule<FolderDTO>(x => x.Folder);

public static IBusinessRule FolderExists
{
get
public static IBusinessRule FolderExists { get; } = CreateRule.For<FolderDTO>()
.Property(item => item.Folder)
.WithRule((dto, folder) =>
{
return CreateRule.For<FolderDTO>()
.Property(item => item.Folder)
.WithRule((dto, folder) =>
{
if (string.IsNullOrEmpty(folder))
return false;
if (string.IsNullOrEmpty(folder))
return false;
new DirectoryInfo(folder);
return true;
})
.WithError(PKSimConstants.Error.ValueIsRequired);
}
}
new DirectoryInfo(folder);
return true;
})
.WithError(PKSimConstants.Error.ValueIsRequired);
}
}
}
102 changes: 37 additions & 65 deletions src/PKSim.Core/Model/ParameterRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Services;
using OSPSuite.Core.Domain.UnitSystem;
using OSPSuite.Utility.Format;
using OSPSuite.Utility.Reflection;
using OSPSuite.Utility.Validation;
using PKSim.Assets;
Expand All @@ -18,6 +19,7 @@ public class ParameterRange : Notifier, IValidatable, IUpdatable
public double? MaxValue { get; set; }
public IDimension Dimension { get; set; }
public Unit Unit { get; set; }
private static readonly NumericFormatter<double> _numericFormatter = new NumericFormatter<double>(NumericFormatterOptions.Instance);

public ParameterRange()
{
Expand Down Expand Up @@ -57,6 +59,10 @@ public double? MaxValueInDisplayUnit
set => MaxValue = baseValueFrom(value);
}

public double? DbMinValueInDisplayUnit => displayValueFrom(DbMinValue);

public double? DbMaxValueInDisplayUnit => displayValueFrom(DbMaxValue);

private double? baseValueFrom(double? displayValue)
{
if (!displayValue.HasValue)
Expand Down Expand Up @@ -86,71 +92,35 @@ public override string ToString()

private static class AllRules
{
public static IBusinessRule MinLessThanMax
{
get
{
return CreateRule.For<ParameterRange>()
.Property(item => item.MinValueInDisplayUnit)
.WithRule(minLessThanMax)
.WithError((param, value) => PKSimConstants.Rules.Parameter.MinLessThanMax(param.ParameterName));
}
}

public static IBusinessRule MaxGreaterThanMin
{
get
{
return CreateRule.For<ParameterRange>()
.Property(item => item.MaxValueInDisplayUnit)
.WithRule(maxGreaterThanMin)
.WithError((param, value) => PKSimConstants.Rules.Parameter.MaxGreaterThanMin(param.ParameterName));
}
}

public static IBusinessRule MinGreaterThanDbMin
{
get
{
return CreateRule.For<ParameterRange>()
.Property(item => item.MinValueInDisplayUnit)
.WithRule(minGreaterThanDbMin)
.WithError((param, value) => PKSimConstants.Rules.Parameter.MinGreaterThanDbMinValue(param.ParameterName, param.DbMinValue, param.Unit.ToString()));
}
}

public static IBusinessRule MaxLessThanDbMax
{
get
{
return CreateRule.For<ParameterRange>()
.Property(item => item.MaxValueInDisplayUnit)
.WithRule(maxLessThanDbMax)
.WithError((param, value) => PKSimConstants.Rules.Parameter.MaxLessThanDbMaxValue(param.ParameterName, param.DbMaxValue, param.Unit.ToString()));
}
}

public static IBusinessRule MinLessThanDbMax
{
get
{
return CreateRule.For<ParameterRange>()
.Property(item => item.MinValueInDisplayUnit)
.WithRule(minLessThanDbMax)
.WithError((param, value) => PKSimConstants.Rules.Parameter.MinLessThanDbMaxValue(param.ParameterName, param.DbMaxValue, param.Unit.ToString()));
}
}

public static IBusinessRule MaxGreaterThanDbMin
{
get
{
return CreateRule.For<ParameterRange>()
.Property(item => item.MaxValueInDisplayUnit)
.WithRule(maxGreaterThanDbMin)
.WithError((param, value) => PKSimConstants.Rules.Parameter.MaxGreaterThanDbMinValue(param.ParameterName, param.DbMinValue, param.Unit.ToString()));
}
}
public static IBusinessRule MinLessThanMax { get; } = CreateRule.For<ParameterRange>()
.Property(item => item.MinValueInDisplayUnit)
.WithRule(minLessThanMax)
.WithError((param, value) => PKSimConstants.Rules.Parameter.MinLessThanMax(param.ParameterName));

public static IBusinessRule MaxGreaterThanMin { get; } = CreateRule.For<ParameterRange>()
.Property(item => item.MaxValueInDisplayUnit)
.WithRule(maxGreaterThanMin)
.WithError((param, value) => PKSimConstants.Rules.Parameter.MaxGreaterThanMin(param.ParameterName));

public static IBusinessRule MinGreaterThanDbMin { get; } = CreateRule.For<ParameterRange>()
.Property(item => item.MinValueInDisplayUnit)
.WithRule(minGreaterThanDbMin)
.WithError((param, value) => PKSimConstants.Rules.Parameter.MinGreaterThanDbMinValue(param.ParameterName, displayFor(param.DbMinValueInDisplayUnit), param.Unit.ToString()));

public static IBusinessRule MaxLessThanDbMax { get; } = CreateRule.For<ParameterRange>()
.Property(item => item.MaxValueInDisplayUnit)
.WithRule(maxLessThanDbMax)
.WithError((param, value) => PKSimConstants.Rules.Parameter.MaxLessThanDbMaxValue(param.ParameterName, displayFor(param.DbMaxValueInDisplayUnit), param.Unit.ToString()));

public static IBusinessRule MinLessThanDbMax { get; } = CreateRule.For<ParameterRange>()
.Property(item => item.MinValueInDisplayUnit)
.WithRule(minLessThanDbMax)
.WithError((param, value) => PKSimConstants.Rules.Parameter.MinLessThanDbMaxValue(param.ParameterName, displayFor(param.DbMaxValueInDisplayUnit), param.Unit.ToString()));

public static IBusinessRule MaxGreaterThanDbMin { get; } = CreateRule.For<ParameterRange>()
.Property(item => item.MaxValueInDisplayUnit)
.WithRule(maxGreaterThanDbMin)
.WithError((param, value) => PKSimConstants.Rules.Parameter.MaxGreaterThanDbMinValue(param.ParameterName, displayFor(param.DbMinValueInDisplayUnit), param.Unit.ToString()));

private static bool minLessThanMax(ParameterRange param, double? minValueInDisplayUnit)
{
Expand Down Expand Up @@ -212,6 +182,8 @@ private static bool doesNotNeedCheck(double? referenceValue, double? valueToChec

return false;
}

private static string displayFor(double? value) => _numericFormatter.Format(value ?? double.NaN);
}

public virtual void UpdatePropertiesFrom(IUpdatable source, ICloneManager cloneManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,21 @@ public static IEnumerable<IBusinessRule> All
}
}

private static IBusinessRule nameNotEmpty
{
get { return GenericRules.NonEmptyRule<PopulationAnalysisFieldBase>(x => x.Name); }
}
private static IBusinessRule nameNotEmpty { get; } = GenericRules.NonEmptyRule<PopulationAnalysisFieldBase>(x => x.Name);

private static IBusinessRule nameUnique
{
get
private static IBusinessRule nameUnique { get; } = CreateRule.For<PopulationAnalysisFieldBase>()
.Property(x => x.Name)
.WithRule((field, name) =>
{
return CreateRule.For<PopulationAnalysisFieldBase>()
.Property(x => x.Name)
.WithRule((field, name) =>
{
var populationAnalysis = field.PopulationAnalysis;
var populationAnalysis = field.PopulationAnalysis;
var otherField = populationAnalysis?.FieldByName(name);
if (otherField == null)
return true;
var otherField = populationAnalysis?.FieldByName(name);
if (otherField == null)
return true;
return otherField == field;
})
.WithError((field, name) => PKSimConstants.Error.NameAlreadyExistsInContainerType(name, PKSimConstants.ObjectTypes.PopulationAnalysis));
}
}
return otherField == field;
})
.WithError((field, name) => PKSimConstants.Error.NameAlreadyExistsInContainerType(name, PKSimConstants.ObjectTypes.PopulationAnalysis));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@ public ParameterScaleWithFactorDTO()

private static class AllRules
{
private static IBusinessRule factorBiggerThanZero
{
get
{
return CreateRule.For<ParameterScaleWithFactorDTO>()
.Property(item => item.Factor)
.WithRule((param, value) => value > 0)
.WithError((param, value) => PKSimConstants.Error.FactorShouldBeBiggerThanZero);
}
}
private static IBusinessRule factorBiggerThanZero { get; } = CreateRule.For<ParameterScaleWithFactorDTO>()
.Property(item => item.Factor)
.WithRule((param, value) => value > 0)
.WithError((param, value) => PKSimConstants.Error.FactorShouldBeBiggerThanZero);

public static IEnumerable<IBusinessRule> All()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ public virtual void UpdateFrom(GroupingItem groupingItem)

private static class AllRules
{
private static IBusinessRule labelDefined
{
get { return GenericRules.NonEmptyRule<GroupingItemDTO>(x => x.Label); }
}
private static IBusinessRule labelDefined { get; } = GenericRules.NonEmptyRule<GroupingItemDTO>(x => x.Label);

public static IEnumerable<IBusinessRule> All
{
Expand Down
20 changes: 7 additions & 13 deletions src/PKSim.Presentation/DTO/Populations/ExtractIndividualsDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,18 @@ public int CountFor(string individualIds)
private static class AllRules
{
private static IBusinessRule namingPatternDefined { get; } = GenericRules.NonEmptyRule<ExtractIndividualsDTO>(x => x.NamingPattern);
private static IBusinessRule indiviudalIdsDefined { get; } = GenericRules.NonEmptyRule<ExtractIndividualsDTO>(x => x.IndividualIdsExpression);
private static IBusinessRule individualIdsDefined { get; } = GenericRules.NonEmptyRule<ExtractIndividualsDTO>(x => x.IndividualIdsExpression);

private static IBusinessRule indiviudalIdsWellFormatted
{
get
{
return CreateRule.For<ExtractIndividualsDTO>()
.Property(dto => dto.IndividualIdsExpression)
.WithRule((dto, individualIds) => dto.CountFor(individualIds) > 0)
.WithError((dto, individualIds) => PKSimConstants.Error.AtLeastOneIndividualIdRequiredToPerformPopulationExtraction);
}
}
private static IBusinessRule individualIdsWellFormatted { get; } = CreateRule.For<ExtractIndividualsDTO>()
.Property(dto => dto.IndividualIdsExpression)
.WithRule((dto, individualIds) => dto.CountFor(individualIds) > 0)
.WithError((dto, individualIds) => PKSimConstants.Error.AtLeastOneIndividualIdRequiredToPerformPopulationExtraction);

internal static IEnumerable<IBusinessRule> All()
{
yield return namingPatternDefined;
yield return indiviudalIdsDefined;
yield return indiviudalIdsWellFormatted;
yield return individualIdsDefined;
yield return individualIdsWellFormatted;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,15 @@ public ImportPopulationSettingsDTO()

private static class AllRules
{
private static IBusinessRule atLeastOneFileDefined
{
get
{
return CreateRule.For<ImportPopulationSettingsDTO>()
.Property(item => item.PopulationFiles)
.WithRule((item, files) => files.Any())
.WithError((item, files) => PKSimConstants.Error.AtLeastOneFileRequiredToStartPopulationImport);
}
}

private static IBusinessRule individualDefined
{
get
{
return CreateRule.For<ImportPopulationSettingsDTO>()
.Property(item => item.Individual)
.WithRule((item, ind) => ind != null)
.WithError((dto, ind) => PKSimConstants.Error.BuildingBlockNotDefined(PKSimConstants.ObjectTypes.Individual));
}
}
private static IBusinessRule atLeastOneFileDefined { get; } = CreateRule.For<ImportPopulationSettingsDTO>()
.Property(item => item.PopulationFiles)
.WithRule((item, files) => files.Any())
.WithError((item, files) => PKSimConstants.Error.AtLeastOneFileRequiredToStartPopulationImport);

private static IBusinessRule individualDefined { get; } = CreateRule.For<ImportPopulationSettingsDTO>()
.Property(item => item.Individual)
.WithRule((item, ind) => ind != null)
.WithError((dto, ind) => PKSimConstants.Error.BuildingBlockNotDefined(PKSimConstants.ObjectTypes.Individual));

internal static IEnumerable<IBusinessRule> All()
{
Expand Down
Loading

0 comments on commit a82a845

Please sign in to comment.