Skip to content

Commit

Permalink
Fixes #2183 wrong error range
Browse files Browse the repository at this point in the history
  • Loading branch information
msevestre committed Apr 8, 2022
1 parent 834b589 commit a4bdff5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 99 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
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
27 changes: 9 additions & 18 deletions src/PKSim.Presentation/DTO/Populations/ParameterRangeDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,33 @@ namespace PKSim.Presentation.DTO.Populations
{
public class ParameterRangeDTO : DxValidatableDTO<ParameterRange>
{
public ParameterRange ParameterRange { get; private set; }
public ParameterRange ParameterRange { get; }

public ParameterRangeDTO(ParameterRange parameterRange)
: base(parameterRange)
{
ParameterRange = parameterRange;
}

public string ParameterName
{
get { return ParameterRange.ParameterName; }
}
public string ParameterName => ParameterRange.ParameterName;

public string ParameterDisplayName
{
get { return ParameterRange.ParameterDisplayName; }
}
public string ParameterDisplayName => ParameterRange.ParameterDisplayName;

public double? MinValueInDisplayUnit
{
get { return ParameterRange.MinValueInDisplayUnit; }
set { ParameterRange.MinValueInDisplayUnit = value; }
get => ParameterRange.MinValueInDisplayUnit;
set => ParameterRange.MinValueInDisplayUnit = value;
}

public double? MaxValueInDisplayUnit
{
get { return ParameterRange.MaxValueInDisplayUnit; }
set { ParameterRange.MaxValueInDisplayUnit = value; }
get => ParameterRange.MaxValueInDisplayUnit;
set => ParameterRange.MaxValueInDisplayUnit = value;
}

public Unit Unit
{
get { return ParameterRange.Unit; }
get => ParameterRange.Unit;
set
{
var minDisplayValue = MinValueInDisplayUnit;
Expand All @@ -52,10 +46,7 @@ public Unit Unit
}
}

public bool IsDiscrete
{
get { return ParameterRange.IsAnImplementationOf<DiscreteParameterRange>(); }
}
public bool IsDiscrete => ParameterRange.IsAnImplementationOf<DiscreteParameterRange>();

public IEnumerable<double> ListOfValues
{
Expand Down
41 changes: 33 additions & 8 deletions tests/PKSim.Tests/Core/ParameterRangeSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using FakeItEasy;
using NUnit.Framework;
using OSPSuite.BDDHelper;
using OSPSuite.BDDHelper.Extensions;
using OSPSuite.Utility.Format;
using OSPSuite.Utility.Validation;
using NUnit.Framework;
using PKSim.Assets;
using PKSim.Core.Model;

namespace PKSim.Core
Expand All @@ -11,7 +12,11 @@ public abstract class concern_for_ParameterRange : ContextSpecification<Paramete
{
protected override void Context()
{
sut = new ParameterRange {Dimension = DomainHelperForSpecs.TimeDimensionForSpecs()};
sut = new ParameterRange
{
Dimension = DomainHelperForSpecs.TimeDimensionForSpecs(),
ParameterName = "PARAM"
};
sut.Unit = sut.Dimension.Unit("h");
}

Expand All @@ -31,10 +36,20 @@ public class When_checking_if_a_value_in_display_unit_for_minimum_is_valid : con
[TestCase(null, 60.0, 120.0, 0.5, false)]
[TestCase(80.0, 60.0, 120.0, 1.0, true)]
[TestCase(80.0, null, null, 2.0, false)]
public void should_return_the_validation_according_to_acceptable_range(double?max, double? dbMin, double? dbMax, double? valueInHours, bool valid)
public void should_return_the_validation_according_to_acceptable_range(double? max, double? dbMin, double? dbMax, double? valueInHours, bool valid)
{
InitRange(null, max, dbMin, dbMax);
sut.Validate(x => x.MinValueInDisplayUnit, valueInHours).IsEmpty.ShouldBeEqualTo(valid);
InitRange(null, max, dbMin, dbMax);
sut.Validate(x => x.MinValueInDisplayUnit, valueInHours).IsEmpty.ShouldBeEqualTo(valid);
}

[TestCase(90, 1)]
public void should_return_a_message_when_invalid_using_the_display_database_values(double? dbMin, double? valueInHours)
{
InitRange(null, null, dbMin);
var formatter = new NumericFormatter<double>(NumericFormatterOptions.Instance);
var validation = sut.Validate(x => x.MinValueInDisplayUnit, valueInHours);
validation.IsEmpty.ShouldBeEqualTo(false);
validation.Message.ShouldBeEqualTo(PKSimConstants.Rules.Parameter.MinGreaterThanDbMinValue(sut.ParameterName, formatter.Format(sut.DbMinValueInDisplayUnit.Value), sut.Unit.Name));
}
}

Expand All @@ -45,10 +60,20 @@ public class When_checking_if_a_value_in_display_unit_for_maximum_is_valid : con
[TestCase(null, 60.0, 120.0, 3.0, false)]
[TestCase(80.0, 60.0, 120.0, 1.0, false)]
[TestCase(60.0, null, null, 2.0, true)]
public void should_return_the_validation_according_to_acceptable_range(double? min, double? dbMin, double? dbMax, double? valueInHours, bool valid)
public void should_return_the_validation_according_to_acceptable_range(double? min, double? dbMin, double? dbMax, double? valueInHours, bool valid)
{
InitRange(min, null, dbMin, dbMax);
sut.Validate(x => x.MaxValueInDisplayUnit, valueInHours).IsEmpty.ShouldBeEqualTo(valid);
}

[TestCase(90, 2)]
public void should_return_a_message_when_invalid_using_the_display_database_values(double? dbMax, double? valueInHours)
{
InitRange(null, null, null, dbMax);
var formatter = new NumericFormatter<double>(NumericFormatterOptions.Instance);
var validation = sut.Validate(x => x.MaxValueInDisplayUnit, valueInHours);
validation.IsEmpty.ShouldBeEqualTo(false);
validation.Message.ShouldBeEqualTo(PKSimConstants.Rules.Parameter.MaxLessThanDbMaxValue(sut.ParameterName, formatter.Format(sut.DbMaxValueInDisplayUnit.Value), sut.Unit.Name));
}
}
}
}

0 comments on commit a4bdff5

Please sign in to comment.