Skip to content

Commit

Permalink
Fixes #2217 11.3 Crash in cloned PI after project save/load (#2218)
Browse files Browse the repository at this point in the history
* Fixes #2217 11.3 Crash in cloned PI after project save/load

* Moved this implementation of setting HasChanged into cloneManagerStrategy

* add interface to other core objects with the property
  • Loading branch information
rwmcintosh authored Mar 20, 2024
1 parent 0e48981 commit d742a99
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/OSPSuite.Core/Domain/IProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OSPSuite.Core.Domain
{
public interface IProject : IObjectBase, IWithChartTemplates, IWithCreationMetaData
public interface IProject : IObjectBase, IWithChartTemplates, IWithCreationMetaData, IWithHasChanged
{
/// <summary>
/// Full path of project (empty if the project was not saved yet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace OSPSuite.Core.Domain.ParameterIdentifications
{
public class ParameterIdentification : ObjectBase, IParameterAnalysable, IUsesObservedData
public class ParameterIdentification : ObjectBase, IParameterAnalysable, IUsesObservedData, IWithHasChanged
{
private readonly List<ISimulation> _allSimulations = new List<ISimulation>();
private readonly List<IdentificationParameter> _allIdentificationParameters = new List<IdentificationParameter>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace OSPSuite.Core.Domain.SensitivityAnalyses
{
public class SensitivityAnalysis : ObjectBase, IParameterAnalysable, IVisitor
public class SensitivityAnalysis : ObjectBase, IParameterAnalysable, IVisitor, IWithHasChanged
{
public virtual ISimulation Simulation { get; set; }
private readonly List<ISimulationAnalysis> _allSimulationAnalyses = new List<ISimulationAnalysis>();
Expand Down
11 changes: 6 additions & 5 deletions src/OSPSuite.Core/Domain/Services/CloneManagerStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ public virtual T Clone<T>(T objectToClone) where T : class, IUpdatable
if (objectToClone == null)
return null;

var repository = objectToClone as DataRepository;
if (repository != null)
if (objectToClone is DataRepository repository)
{
return cloneDataRepository(objectToClone.DowncastTo<DataRepository>()) as T;
return cloneDataRepository(repository) as T;
}

var formulaToClone = objectToClone as IFormula;
if (formulaToClone != null)
if (objectToClone is IFormula formulaToClone)
return CreateFormulaCloneFor(formulaToClone).DowncastTo<T>();

var clone = _objectBaseFactory.CreateObjectBaseFrom(objectToClone);
Expand All @@ -41,6 +39,9 @@ public virtual T Clone<T>(T objectToClone) where T : class, IUpdatable
//it is necessary to update the formula before the properties, since UpdatePropertiesFrom might use formula
clone.UpdatePropertiesFrom(objectToClone, this);

if (clone is IWithHasChanged withHasChanged)
withHasChanged.HasChanged = true;

return clone;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/OSPSuite.Core.Tests/Domain/CloneManagerForModelSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using OSPSuite.BDDHelper;
using OSPSuite.BDDHelper.Extensions;
using OSPSuite.Core.Domain.Formulas;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Core.Domain.Services;
using OSPSuite.Core.Domain.UnitSystem;
using OSPSuite.Helpers;
Expand Down Expand Up @@ -59,6 +60,22 @@ private IContainer createEntityToClone()
}
}

public class When_cloning_a_with_changes_object_that_does_not_have_changes : concern_for_CloneManagerForModel
{
private IWithHasChanged _clonedObject;

protected override void Because()
{
_clonedObject = sut.Clone(new ParameterIdentification());
}

[Observation]
public void should_return_a_cloned_object()
{
_clonedObject.HasChanged.ShouldBeTrue();
}
}

public class When_asked_to_clone_an_object : concern_for_CloneManagerForModel
{
[Observation]
Expand Down
4 changes: 4 additions & 0 deletions tests/OSPSuite.HelpersForTests/ModelHelperForSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using OSPSuite.Core.Domain.Builder;
using OSPSuite.Core.Domain.Descriptors;
using OSPSuite.Core.Domain.Formulas;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Core.Domain.Services;
using OSPSuite.Core.Domain.UnitSystem;

Expand Down Expand Up @@ -1174,6 +1175,9 @@ public T CreateObjectBaseFrom<T>(T sourceObject)
if (sourceObject.IsAnImplementationOf<NormalDistributionFormula>())
return new NormalDistributionFormula().WithDimension(_dimensionFactory.NoDimension).WithId(id).DowncastTo<T>();

if(sourceObject.IsAnImplementationOf<ParameterIdentification>())
return new ParameterIdentification().WithId(id).DowncastTo<T>();

return default(T);
}

Expand Down

0 comments on commit d742a99

Please sign in to comment.