Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1825 Refactor StartValues vs PathWithValueEntity #1824

Merged
merged 4 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/OSPSuite.Core/Comparison/BuildingBlockDiffBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ private string startValueDisplayFor(TStartValue startValue)
return string.Equals(name, display) ? name : $"{name} ({display})";
}

var value = startValue.ConvertToDisplayUnit(startValue.StartValue);
var value = startValue.ConvertToDisplayUnit(startValue.Value);
return _unitFormatter.Format(value, startValue.DisplayUnit);
}
}

public class MoleculeStartValueBuildingBlockDiffBuilder : StartValueBuildingBlockDiffBuilder<IMoleculeStartValuesBuildingBlock, IMoleculeStartValue>
public class MoleculeStartValueBuildingBlockDiffBuilder : StartValueBuildingBlockDiffBuilder<IMoleculeStartValuesBuildingBlock, MoleculeStartValue>
{
public MoleculeStartValueBuildingBlockDiffBuilder(ObjectBaseDiffBuilder objectBaseDiffBuilder, EnumerableComparer enumerableComparer) : base(objectBaseDiffBuilder, enumerableComparer)
{
}
}

public class ParameterStartValueBuildingBlockDiffBuilder : StartValueBuildingBlockDiffBuilder<IParameterStartValuesBuildingBlock, IParameterStartValue>
public class ParameterStartValueBuildingBlockDiffBuilder : StartValueBuildingBlockDiffBuilder<IParameterStartValuesBuildingBlock, ParameterStartValue>
{
public ParameterStartValueBuildingBlockDiffBuilder(ObjectBaseDiffBuilder objectBaseDiffBuilder, EnumerableComparer enumerableComparer) : base(objectBaseDiffBuilder, enumerableComparer)
{
Expand Down
4 changes: 2 additions & 2 deletions src/OSPSuite.Core/Comparison/ExpressionProfileDiffBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public override void Compare(IComparison<ExpressionProfileBuildingBlock> compari
}
}

internal class ExpressionParameterDiffBuilder : StartValueDiffBuilder<ExpressionParameter>
internal class ExpressionParameterDiffBuilder : PathAndValueEntityDiffBuilder<ExpressionParameter>
{
public ExpressionParameterDiffBuilder(IObjectComparer objectComparer, EntityDiffBuilder entityDiffBuilder, WithValueOriginComparison<ExpressionParameter> valueOriginComparison) : base(objectComparer, entityDiffBuilder, valueOriginComparison)
public ExpressionParameterDiffBuilder(IObjectComparer objectComparer, EntityDiffBuilder entityDiffBuilder) : base(objectComparer, entityDiffBuilder)
{
}
}
Expand Down
47 changes: 31 additions & 16 deletions src/OSPSuite.Core/Comparison/StartValueDiffBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,71 @@
using OSPSuite.Core.Domain.Builder;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Builder;

namespace OSPSuite.Core.Comparison
{
internal abstract class StartValueDiffBuilder<T> : DiffBuilder<T> where T : class, IStartValue
internal abstract class PathAndValueEntityDiffBuilder<T> : DiffBuilder<T> where T : PathAndValueEntity
{
private readonly IObjectComparer _objectComparer;
private readonly EntityDiffBuilder _entityDiffBuilder;
private readonly WithValueOriginComparison<T> _valueOriginComparison;
protected IObjectComparer _objectComparer;
protected EntityDiffBuilder _entityDiffBuilder;

protected StartValueDiffBuilder(IObjectComparer objectComparer, EntityDiffBuilder entityDiffBuilder, WithValueOriginComparison<T> valueOriginComparison)
protected PathAndValueEntityDiffBuilder(IObjectComparer objectComparer, EntityDiffBuilder entityDiffBuilder)
{
_objectComparer = objectComparer;
_entityDiffBuilder = entityDiffBuilder;
_valueOriginComparison = valueOriginComparison;
}

public override void Compare(IComparison<T> comparison)
{
_valueOriginComparison.AddValueOriginToComparison(comparison, this, CompareStartValue);
ComparePathAndEntityValues(comparison);
}

protected virtual void CompareStartValue(IComparison<T> comparison)
protected virtual void ComparePathAndEntityValues(IComparison<T> comparison)
{
_entityDiffBuilder.Compare(comparison);
CompareValues(x => x.Dimension, x => x.Dimension, comparison);
CompareValues(x => x.ContainerPath, x => x.ContainerPath, comparison);

// Always Compare Value and Formula, independent from settings as these are two different properties of a start value
CompareNullableDoubleValues(x => x.StartValue, x => x.StartValue, comparison, x => x.DisplayUnit);
CompareNullableDoubleValues(x => x.Value, x => x.Value, comparison, x => x.DisplayUnit);
_objectComparer.Compare(comparison.FormulaComparison());
}
}

internal class MoleculeStartValueDiffBuilder : StartValueDiffBuilder<IMoleculeStartValue>
internal abstract class StartValueDiffBuilder<T> : PathAndValueEntityDiffBuilder<T> where T : PathAndValueEntity, IWithValueOrigin
{
private readonly WithValueOriginComparison<T> _valueOriginComparison;

protected StartValueDiffBuilder(IObjectComparer objectComparer, EntityDiffBuilder entityDiffBuilder, WithValueOriginComparison<T> valueOriginComparison) :base(objectComparer, entityDiffBuilder)
{

_valueOriginComparison = valueOriginComparison;
}

public override void Compare(IComparison<T> comparison)
{
_valueOriginComparison.AddValueOriginToComparison(comparison, this, ComparePathAndEntityValues);
}
}

internal class MoleculeStartValueDiffBuilder : StartValueDiffBuilder<MoleculeStartValue>
{
public MoleculeStartValueDiffBuilder(IObjectComparer objectComparer, EntityDiffBuilder entityDiffBuilder, WithValueOriginComparison<IMoleculeStartValue> valueOriginComparison)
public MoleculeStartValueDiffBuilder(IObjectComparer objectComparer, EntityDiffBuilder entityDiffBuilder, WithValueOriginComparison<MoleculeStartValue> valueOriginComparison)
: base(objectComparer, entityDiffBuilder, valueOriginComparison)
{
}

protected override void CompareStartValue(IComparison<IMoleculeStartValue> comparison)
protected override void ComparePathAndEntityValues(IComparison<MoleculeStartValue> comparison)
{
base.CompareStartValue(comparison);
base.ComparePathAndEntityValues(comparison);
CompareValues(x => x.IsPresent, x => x.IsPresent, comparison);
CompareValues(x => x.ScaleDivisor, x => x.ScaleDivisor, comparison);
CompareValues(x => x.NegativeValuesAllowed, x => x.NegativeValuesAllowed, comparison);
}
}

internal class ParameterStartValueDiffBuilder : StartValueDiffBuilder<IParameterStartValue>
internal class ParameterStartValueDiffBuilder : StartValueDiffBuilder<ParameterStartValue>
{
public ParameterStartValueDiffBuilder(IObjectComparer objectComparer, EntityDiffBuilder entityDiffBuilder, WithValueOriginComparison<IParameterStartValue> valueOriginComparison)
public ParameterStartValueDiffBuilder(IObjectComparer objectComparer, EntityDiffBuilder entityDiffBuilder, WithValueOriginComparison<ParameterStartValue> valueOriginComparison)
: base(objectComparer, entityDiffBuilder, valueOriginComparison)
{
}
Expand Down
4 changes: 2 additions & 2 deletions src/OSPSuite.Core/Domain/Builder/BuildConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface IBuildConfiguration : IVisitable<IVisitor>
/// Return the molecules values defined in the configuration for which a molecule also exists in the Molecule building
/// block
/// </summary>
IEnumerable<IMoleculeStartValue> AllPresentMoleculeValues();
IEnumerable<MoleculeStartValue> AllPresentMoleculeValues();

/// <summary>
/// Return the names of all molecules defined in the configuration that will be present in the model
Expand Down Expand Up @@ -171,7 +171,7 @@ public virtual IEnumerable<IMoleculeBuilder> AllPresentMolecules()
return Molecules.AllPresentFor(MoleculeStartValues);
}

public virtual IEnumerable<IMoleculeStartValue> AllPresentMoleculeValues()
public virtual IEnumerable<MoleculeStartValue> AllPresentMoleculeValues()
{
var moleculeNames = Molecules.Select(x => x.Name);
return MoleculeStartValues.Where(msv => moleculeNames.Contains(msv.MoleculeName))
Expand Down
20 changes: 2 additions & 18 deletions src/OSPSuite.Core/Domain/Builder/ExpressionParameter.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
using OSPSuite.Core.Domain.Services;

namespace OSPSuite.Core.Domain.Builder
namespace OSPSuite.Core.Domain.Builder
{
public class ExpressionParameter : PathAndValueEntity, IStartValue
public class ExpressionParameter : PathAndValueEntity
{
/// <summary>
/// Do not use! When refactoring on promotion to core, this should be removed
/// </summary>
public double? StartValue { get; set; }

public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager cloneManager)
{
base.UpdatePropertiesFrom(source, cloneManager);

if (!(source is ExpressionParameter sourceExpressionParameter))
return;

StartValue = sourceExpressionParameter.StartValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace OSPSuite.Core.Domain.Builder
{
public class ExpressionProfileBuildingBlock : StartValueBuildingBlock<ExpressionParameter>
public class ExpressionProfileBuildingBlock : PathAndValueEntityBuildingBlock<ExpressionParameter>
{
public override string Icon => Type.IconName;

Expand Down
8 changes: 8 additions & 0 deletions src/OSPSuite.Core/Domain/Builder/IWithPath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OSPSuite.Core.Domain.Builder
{
public interface IWithPath
{
IObjectPath Path { get; set; }
IObjectPath ContainerPath { get; set; }
}
}
26 changes: 5 additions & 21 deletions src/OSPSuite.Core/Domain/Builder/MoleculeStartValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,7 @@

namespace OSPSuite.Core.Domain.Builder
{
public interface IMoleculeStartValue : IStartValue, IWithScaleDivisor
{
bool IsPresent { set; get; }

string MoleculeName { get; }

/// <summary>
/// Tests whether or not the value is public-member-equivalent to the target
/// </summary>
/// <param name="moleculeStartValue">The comparable object</param>
/// <returns>True if all the public members are equal, otherwise false</returns>
bool IsEquivalentTo(IMoleculeStartValue moleculeStartValue);

bool NegativeValuesAllowed { get; set; }
}

public class MoleculeStartValue : StartValueBase, IMoleculeStartValue
public class MoleculeStartValue : StartValueBase, IWithScaleDivisor
{
private bool _isPresent;
private double _scaleDivisor;
Expand All @@ -34,7 +18,7 @@ public MoleculeStartValue()
public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager cloneManager)
{
base.UpdatePropertiesFrom(source, cloneManager);
var sourceMoleculeStartValue = source as IMoleculeStartValue;
var sourceMoleculeStartValue = source as MoleculeStartValue;
if (sourceMoleculeStartValue == null) return;
IsPresent = sourceMoleculeStartValue.IsPresent;
ScaleDivisor = sourceMoleculeStartValue.ScaleDivisor;
Expand All @@ -49,19 +33,19 @@ public bool IsPresent

public string MoleculeName => Name;

public bool IsEquivalentTo(IMoleculeStartValue moleculeStartValue)
public bool IsEquivalentTo(MoleculeStartValue moleculeStartValue)
{
var isBaseEquivalent = base.IsEquivalentTo(moleculeStartValue);

var isEquivalent =
(IsPresent == moleculeStartValue.IsPresent) &&
IsPresent == moleculeStartValue.IsPresent &&
NullableEqualsCheck(MoleculeName, moleculeStartValue.MoleculeName) &&
ValueComparer.AreValuesEqual(ScaleDivisor, moleculeStartValue.ScaleDivisor);

return isBaseEquivalent && isEquivalent;
}

public virtual double ScaleDivisor
public double ScaleDivisor
{
get => _scaleDivisor;
set => SetProperty(ref _scaleDivisor, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace OSPSuite.Core.Domain.Builder
{
public interface IMoleculeStartValuesBuildingBlock : IStartValuesBuildingBlock<IMoleculeStartValue>
public interface IMoleculeStartValuesBuildingBlock : IStartValuesBuildingBlock<MoleculeStartValue>
{
}

public class MoleculeStartValuesBuildingBlock : StartValueBuildingBlock<IMoleculeStartValue>, IMoleculeStartValuesBuildingBlock
public class MoleculeStartValuesBuildingBlock : StartValueBuildingBlock<MoleculeStartValue>, IMoleculeStartValuesBuildingBlock
{
public MoleculeStartValuesBuildingBlock()
{
Expand Down
27 changes: 3 additions & 24 deletions src/OSPSuite.Core/Domain/Builder/ParameterStartValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,13 @@

namespace OSPSuite.Core.Domain.Builder
{
public interface IParameterStartValue : IStartValue, IWithDefaultState
{
string ParameterName { get; }

/// <summary>
/// Tests whether or not the value is public-member-equivalent to the target
/// </summary>
/// <param name="parameterStartValue">The comparable object</param>
/// <returns>True if all the public members are equal, otherwise false</returns>
bool IsEquivalentTo(IParameterStartValue parameterStartValue);

/// <summary>
/// This option is only used at the moment in PKSim when cloning a simulation. The goal is to not replace the formula of
/// a parameter
/// changed in the simulation with constant. This parameter is temporary is should not be serialized or updated when
/// cloning etc.
/// Default value is <c>true</c>
/// </summary>
bool OverrideFormulaWithValue { get; set; }
}

public class ParameterStartValue : StartValueBase, IParameterStartValue
public class ParameterStartValue : StartValueBase, IWithDefaultState
{
public string ParameterName => Name;
public bool OverrideFormulaWithValue { get; set; } = true;
public bool IsDefault { get; set; }

public bool IsEquivalentTo(IParameterStartValue parameterStartValue)
public bool IsEquivalentTo(ParameterStartValue parameterStartValue)
{
var isBaseEquivalent = base.IsEquivalentTo(parameterStartValue);
var isEquivalent = NullableEqualsCheck(ParameterName, parameterStartValue.ParameterName);
Expand All @@ -40,7 +19,7 @@ public bool IsEquivalentTo(IParameterStartValue parameterStartValue)
public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager cloneManager)
{
base.UpdatePropertiesFrom(source, cloneManager);
var sourceParameterStartValue = source as IParameterStartValue;
var sourceParameterStartValue = source as ParameterStartValue;
if (sourceParameterStartValue == null) return;
IsDefault = sourceParameterStartValue.IsDefault;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace OSPSuite.Core.Domain.Builder
{
public interface IParameterStartValuesBuildingBlock : IStartValuesBuildingBlock<IParameterStartValue>
public interface IParameterStartValuesBuildingBlock : IStartValuesBuildingBlock<ParameterStartValue>
{
}

public class ParameterStartValuesBuildingBlock : StartValueBuildingBlock<IParameterStartValue>, IParameterStartValuesBuildingBlock
public class ParameterStartValuesBuildingBlock : StartValueBuildingBlock<ParameterStartValue>, IParameterStartValuesBuildingBlock
{
public ParameterStartValuesBuildingBlock()
{
Expand Down
23 changes: 12 additions & 11 deletions src/OSPSuite.Core/Domain/Builder/PathAndValueEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OSPSuite.Core.Domain.Builder
{
public abstract class PathAndValueEntity : Entity, IUsingFormula, IWithDisplayUnit, IWithValueOrigin
public abstract class PathAndValueEntity : Entity, IUsingFormula, IWithDisplayUnit, IWithPath, IWithNullableValue
{
private IObjectPath _containerPath;
protected IFormula _formula;
Expand Down Expand Up @@ -62,23 +62,13 @@ public Unit DisplayUnit
set => SetProperty(ref _displayUnit, value);
}

public ValueOrigin ValueOrigin { get; }

public IObjectPath Path
{
get => ContainerPath.Clone<IObjectPath>().AndAdd(Name);
set => entityFullPathToComponents(value);
}

public void UpdateValueOriginFrom(ValueOrigin sourceValueOrigin)
rwmcintosh marked this conversation as resolved.
Show resolved Hide resolved
{
if (Equals(ValueOrigin, sourceValueOrigin))
return;

ValueOrigin.UpdateFrom(sourceValueOrigin);
OnPropertyChanged(() => ValueOrigin);
}

/// <summary>
/// Tests whether or not the value is public-member-equivalent to the target
/// </summary>
Expand Down Expand Up @@ -132,6 +122,8 @@ public double? Value
set => SetProperty(ref _value, value);
}

public ValueOrigin ValueOrigin { get; }

public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager cloneManager)
{
base.UpdatePropertiesFrom(source, cloneManager);
Expand All @@ -146,6 +138,15 @@ public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager clone
ValueOrigin.UpdateAllFrom(sourcePathAndValueEntity.ValueOrigin);
}

public void UpdateValueOriginFrom(ValueOrigin sourceValueOrigin)
{
if (Equals(ValueOrigin, sourceValueOrigin))
return;

ValueOrigin.UpdateFrom(sourceValueOrigin);
OnPropertyChanged(() => ValueOrigin);
}

public override string ToString() => $"Path={ContainerPath}, Name={Name}";
}
}
Loading