Skip to content

Commit

Permalink
#1554 rename module
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaperez1983 committed Oct 7, 2024
1 parent cdf3288 commit 298bf93
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
26 changes: 21 additions & 5 deletions src/MoBi.Core/Services/SimulationLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ private void addSimulationToProject(IMoBiSimulation simulation, MoBiMacroCommand

var project = _context.CurrentProject;

renameCollidingEntities(simulation.Modules, project.Modules);
moBiSimulation.ResultsDataRepository = simulation.ResultsDataRepository;
if (!_nameCorrector.CorrectName(project.Simulations, moBiSimulation))
return;

renameCollidingModules(simulation.Modules, moBiSimulation.Name, project.Modules);
renameCollidingEntities(simulation.Configuration.ExpressionProfiles, project.ExpressionProfileCollection);

if (simulation.Configuration.Individual != null)
Expand All @@ -68,14 +72,26 @@ private void addSimulationToProject(IMoBiSimulation simulation, MoBiMacroCommand

addSimulationConfigurationToProject(moBiSimulation, loadCommand);

moBiSimulation.ResultsDataRepository = simulation.ResultsDataRepository;
if (!_nameCorrector.CorrectName(project.Simulations, moBiSimulation))
return;

moBiSimulation.HasChanged = true;
loadCommand.AddCommand(new AddSimulationCommand(moBiSimulation));
}

private void renameCollidingModules(IEnumerable<Module> modulesToRename, string simulationName, IReadOnlyList<IWithName> existingEntities)
{
var takenNames = existingEntities.AllNames();

modulesToRename
.Where(x => takenNames.Contains(x.Name))
.Select((x, index) => new { Module = x, Index = index })
.Each(item => item.Module.Name = getModuleNameByIndex(simulationName,item.Index) );

//If there are still colliding names based on the simulation name, we need to correct them
modulesToRename.Where(x => takenNames.Contains(x.Name)).Each(x => _nameCorrector.AutoCorrectName(takenNames, x));
}

private string getModuleNameByIndex(string name, int index)=>
$"{name}{(index == 0 ? string.Empty : " " + index)}";

private void renameCollidingEntities(IEnumerable<IObjectBase> entitiesToRename, IReadOnlyList<IWithName> existingEntities)
{
var takenNames = existingEntities.AllNames();
Expand Down
21 changes: 17 additions & 4 deletions tests/MoBi.Tests/Core/SimulationLoaderSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using MoBi.Helpers;
using OSPSuite.Core.Commands.Core;
using OSPSuite.Utility.Extensions;
using DevExpress.DataProcessing.InMemoryDataProcessor;
using OSPSuite.SimModel;

namespace MoBi.Core
{
Expand Down Expand Up @@ -62,6 +64,8 @@ protected override void Context()
{
base.Context();
_project.AddModule(new Module().WithName("moduleName"));
_project.AddModule(new Module().WithName("newModuleName"));


_clonedBuildingBlock = new ObserverBuildingBlock().WithId("SP2");
_clonedModule = new Module
Expand All @@ -73,12 +77,13 @@ protected override void Context()
_clonedSimulationConfiguration = new SimulationConfiguration();
_clonedSimulationConfiguration.AddModuleConfiguration(new ModuleConfiguration(_clonedModule));
_clonedSimulationConfiguration.Individual = _clonedIndividual;
_simulation.Configuration.AddModuleConfiguration(new ModuleConfiguration(new Module().WithName("newModuleName")));
_simulation.Configuration.AddModuleConfiguration(new ModuleConfiguration(new Module().WithName("Unique moduleName")));
_simulation.Name = "Sim1";

A.CallTo(() => _cloneManager.CloneSimulationConfiguration(_simulationConfiguration)).Returns(_clonedSimulationConfiguration);

A.CallTo(_nameCorrector).WithReturnType<bool>().Returns(true);

A.CallTo(() => _nameCorrector.AutoCorrectName(A<IEnumerable<string>>._, A<IObjectBase>._)).Invokes(x => x.GetArgument<Module>(1).Name = "the corrected name");
}

protected override void Because()
Expand Down Expand Up @@ -117,10 +122,18 @@ public void should_add_clone_of_module_to_the_project_as_well()
}

[Observation]
public void the_module_in_the_simulation_should_be_renamed()
public void the_module_in_the_simulation_with_conflicting_names_should_be_renamed()
{
_simulation.Modules[0].Name.ShouldBeEqualTo(_simulation.Name);
_simulation.Modules[1].Name.ShouldBeEqualTo($"{_simulation.Name} 1");
}

[Observation]
public void the_module_in_the_simulation_with_no_conflicting_name_should_not_be_renamed()
{
_simulation.Modules[0].Name.ShouldBeEqualTo("the corrected name");
_simulation.Modules[2].Name.ShouldBeEqualTo("Unique moduleName");
}

}

public class When_adding_a_simulation_to_project_that_does_already_exists_by_name_and_the_user_cancels_the_rename : concern_for_SimulationLoader
Expand Down

0 comments on commit 298bf93

Please sign in to comment.