Skip to content

Commit ea261bd

Browse files
authored
Fixes #2427 Rename of cloned expression profile updates parameter and initial condition paths (#2433)
Extract expression-profile renaming into a shared ExpressionProfileRenamingTask that sets the name and syncs the molecule in expression parameter and initial condition paths. Use it from the RenameObjectBaseCommand switch and from the clone flow (RenameClone) so cloning a profile with a new molecule name updates its paths.
1 parent ab606c9 commit ea261bd

10 files changed

Lines changed: 208 additions & 186 deletions

src/MoBi.Core/Commands/RenameExpressionProfileBuildingBlockCommand.cs

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/MoBi.Core/Commands/RenameObjectBaseCommand.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ protected virtual void RenameObjectBase(IMoBiContext context)
5353

5454
switch (_objectBase)
5555
{
56+
case ExpressionProfileBuildingBlock expressionProfile:
57+
context.Resolve<IExpressionProfileRenamingTask>().Rename(expressionProfile, _newName);
58+
renameInSimulationTask.RenameInSimulationUsingTemplateBuildingBlock(OldName, expressionProfile);
59+
break;
5660
case IBuildingBlock buildingBlock:
5761
_objectBase.Name = _newName;
5862
renameInSimulationTask.RenameInSimulationUsingTemplateBuildingBlock(OldName, buildingBlock);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using OSPSuite.Core.Domain.Builder;
2+
using OSPSuite.Utility.Extensions;
3+
4+
namespace MoBi.Core.Domain.Services
5+
{
6+
public interface IExpressionProfileRenamingTask
7+
{
8+
/// <summary>
9+
/// Renames the <paramref name="expressionProfile" /> to <paramref name="newName" /> and updates the molecule name in the
10+
/// paths of all expression parameters and initial conditions accordingly.
11+
/// </summary>
12+
void Rename(ExpressionProfileBuildingBlock expressionProfile, string newName);
13+
}
14+
15+
public class ExpressionProfileRenamingTask : IExpressionProfileRenamingTask
16+
{
17+
public void Rename(ExpressionProfileBuildingBlock expressionProfile, string newName)
18+
{
19+
var oldMoleculeName = expressionProfile.MoleculeName;
20+
expressionProfile.Name = newName;
21+
var newMoleculeName = expressionProfile.MoleculeName;
22+
23+
expressionProfile.ExpressionParameters.Each(x => renameMoleculeInPath(x, oldMoleculeName, newMoleculeName));
24+
expressionProfile.InitialConditions.Each(x => renameMoleculeInPath(x, oldMoleculeName, newMoleculeName));
25+
}
26+
27+
private static void renameMoleculeInPath(PathAndValueEntity entity, string oldMoleculeName, string newMoleculeName)
28+
{
29+
var objectPath = entity.Path;
30+
objectPath.Replace(oldMoleculeName, newMoleculeName);
31+
entity.Path = objectPath;
32+
}
33+
}
34+
}

src/MoBi.Presentation/Tasks/Edit/EditTasksForExpressionProfileBuildingBlock.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using MoBi.Core.Commands;
43
using MoBi.Presentation.Presenter;
54
using MoBi.Presentation.Tasks.Interaction;
65
using OSPSuite.Core.Domain;
@@ -36,10 +35,5 @@ protected override IEnumerable<string> GetUnallowedNames(ExpressionProfileBuildi
3635
{
3736
return base.GetUnallowedNames(objectBase, existingObjectsInParent).Concat(_interactionTaskContext.BuildingBlockRepository.ExpressionProfileCollection.AllNames());
3837
}
39-
40-
protected override IMoBiCommand GetRenameCommandFor(ExpressionProfileBuildingBlock expressionProfileBuildingBlock, IBuildingBlock buildingBlock, string newName, string objectType)
41-
{
42-
return new RenameExpressionProfileBuildingBlockCommand(expressionProfileBuildingBlock, newName, buildingBlock) { ObjectType = objectType };
43-
}
4438
}
4539
}

src/MoBi.Presentation/Tasks/Interaction/InteractionTasksForExpressionProfileBuildingBlock.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class InteractionTasksForExpressionProfileBuildingBlock : InteractionTask
3030
private readonly IEditTasksForExpressionProfileBuildingBlock _editTaskForExpressionProfileBuildingBlock;
3131
private readonly IPKSimStarter _pkSimStarter;
3232
private readonly IContainerTask _containerTask;
33+
private readonly IExpressionProfileRenamingTask _expressionProfileRenamingTask;
3334

3435
public InteractionTasksForExpressionProfileBuildingBlock(IInteractionTaskContext interactionTaskContext,
3536
IEditTasksForExpressionProfileBuildingBlock editTask,
@@ -39,7 +40,8 @@ public InteractionTasksForExpressionProfileBuildingBlock(IInteractionTaskContext
3940
IPathAndValueEntityToDistributedParameterMapper pathAndValueEntityToDistributedParameterMapper,
4041
IExportDataTableToExcelTask exportDataTableToExcelTask,
4142
ICloneManagerForBuildingBlock cloneManager,
42-
IExpressionProfileBuildingBlockToDataTableMapper mapper) :
43+
IExpressionProfileBuildingBlockToDataTableMapper mapper,
44+
IExpressionProfileRenamingTask expressionProfileRenamingTask) :
4345
base(interactionTaskContext,
4446
editTask,
4547
formulaTask,
@@ -51,8 +53,11 @@ public InteractionTasksForExpressionProfileBuildingBlock(IInteractionTaskContext
5153
_editTaskForExpressionProfileBuildingBlock = editTask;
5254
_pkSimStarter = pkSimStarter;
5355
_containerTask = containerTask;
56+
_expressionProfileRenamingTask = expressionProfileRenamingTask;
5457
}
5558

59+
protected override void RenameClone(ExpressionProfileBuildingBlock clone, string newName) => _expressionProfileRenamingTask.Rename(clone, newName);
60+
5661
public IMoBiCommand UpdateExpressionProfileFromDatabase(ExpressionProfileBuildingBlock buildingBlock)
5762
{
5863
var expressionProfileUpdate = _pkSimStarter.UpdateExpressionProfileFromDatabase(buildingBlock);

src/MoBi.Presentation/Tasks/Interaction/InteractionTasksForProjectPathAndValueEntityBuildingBlocks.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ public IMoBiCommand Clone(TBuildingBlock buildingBlockToClone)
4949
if (string.IsNullOrEmpty(name))
5050
return new MoBiEmptyCommand();
5151

52-
var clone = InteractionTask.Clone(buildingBlockToClone).WithName(name);
52+
var clone = InteractionTask.Clone(buildingBlockToClone);
53+
RenameClone(clone, name);
5354

5455
return AddToProject(clone);
5556
}
5657

58+
protected virtual void RenameClone(TBuildingBlock clone, string newName) => clone.WithName(newName);
59+
5760
public void ExportBuildingBlockSnapshot(TBuildingBlock buildingBlock)
5861
{
5962
var filePath = InteractionTask.AskForFileToSave(AppConstants.Captions.SaveModuleSnapshot, Constants.Filter.JSON_FILE_FILTER, Constants.DirectoryKey.MODEL_PART, buildingBlock.Name);

tests/MoBi.Tests/Core/Commands/RenameExpressionProfileBuildingBlockCommandSpecs.cs

Lines changed: 0 additions & 120 deletions
This file was deleted.

tests/MoBi.Tests/Core/Commands/RenameObjectBaseCommandSpecs.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,36 @@ public void the_renaming_task_is_used_to_rename_the_module()
130130
A.CallTo(() => _renameInSimulationTask.RenameInSimulationUsingTemplateModule("oldName", _module)).MustHaveHappened();
131131
}
132132
}
133+
134+
public class When_renaming_an_expression_profile : concern_for_RenameObjectBaseCommand
135+
{
136+
private ExpressionProfileBuildingBlock _expressionProfile;
137+
private IExpressionProfileRenamingTask _expressionProfileRenamingTask;
138+
139+
protected override void Context()
140+
{
141+
_expressionProfile = new ExpressionProfileBuildingBlock().WithName("OldMolecule|Species|Category").WithId("id");
142+
base.Context();
143+
_expressionProfileRenamingTask = A.Fake<IExpressionProfileRenamingTask>();
144+
A.CallTo(() => _context.Resolve<IExpressionProfileRenamingTask>()).Returns(_expressionProfileRenamingTask);
145+
}
146+
147+
protected override void Because() => sut.Execute(_context);
148+
149+
protected override IBuildingBlock GetBuildingBlock() => null;
150+
151+
protected override IObjectBase GetObject() => _expressionProfile;
152+
153+
[Observation]
154+
public void the_expression_profile_renaming_task_renames_the_building_block_and_updates_its_paths()
155+
{
156+
A.CallTo(() => _expressionProfileRenamingTask.Rename(_expressionProfile, "newName")).MustHaveHappened();
157+
}
158+
159+
[Observation]
160+
public void the_rename_is_propagated_to_simulations()
161+
{
162+
A.CallTo(() => _renameInSimulationTask.RenameInSimulationUsingTemplateBuildingBlock("OldMolecule|Species|Category", _expressionProfile)).MustHaveHappened();
163+
}
164+
}
133165
}

0 commit comments

Comments
 (0)