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

1763 delete observed data entry #1765

Merged
merged 20 commits into from
Nov 16, 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
1 change: 1 addition & 0 deletions src/OSPSuite.Assets/UIConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public static class Captions
public static readonly string ChartExportOptions = "Chart Export Options";
public static readonly string No = "No";
public static readonly string Yes = "Yes";
public static readonly string ReallyRemoveObservedDataFromSimulation = $"Really remove {ObjectTypes.ObservedData} from the simulation?\nHint: {ObjectTypes.ObservedData} will not be deleted from the project";

public static string ShouldWatermarkBeUsedForChartExportToClipboard(string applicationName, string optionLocation)
{
Expand Down
7 changes: 7 additions & 0 deletions src/OSPSuite.Core/Domain/ISimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@ public interface ISimulation : ILazyLoadable, IAnalysable, IUsesObservedData, IM

//ResultsDataRepository will be null for Population Simulations and should never be called for them
DataRepository ResultsDataRepository { get; set; }
void RemoveUsedObservedData(DataRepository dataRepository);
msevestre marked this conversation as resolved.
Show resolved Hide resolved


/// <summary>
/// Remove the output mappings mapped to the dataRepository
/// </summary>
void RemoveOutputMappings(DataRepository dataRepository);
}
}
82 changes: 78 additions & 4 deletions src/OSPSuite.Core/Domain/Services/ObservedDataTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using OSPSuite.Core.Commands;
using OSPSuite.Core.Commands.Core;
using OSPSuite.Core.Domain.Data;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Core.Events;
using OSPSuite.Core.Import;
using OSPSuite.Core.Services;
using OSPSuite.Utility.Collections;
Expand Down Expand Up @@ -47,6 +49,13 @@ public interface IObservedDataTask

void AddObservedDataToProject(DataRepository observedData);
void AddImporterConfigurationToProject(ImporterConfiguration configuration);

/// <summary>
/// Removes from the simulations all the observed data as defined in the pairs of the
/// <paramref name="usedObservedDataList" />, while first checking that they are not used
/// in Parameter Identifications and also requiring confirmation by the user.
/// </summary>
void RemoveUsedObservedDataFromSimulation(IReadOnlyList<UsedObservedData> usedObservedDataList);
}

public abstract class ObservedDataTask : IObservedDataTask
Expand All @@ -57,7 +66,8 @@ public abstract class ObservedDataTask : IObservedDataTask
private readonly IContainerTask _containerTask;
private readonly IObjectTypeResolver _objectTypeResolver;

protected ObservedDataTask(IDialogCreator dialogCreator, IOSPSuiteExecutionContext executionContext, IDataRepositoryExportTask dataRepositoryExportTask, IContainerTask containerTask, IObjectTypeResolver objectTypeResolver)
protected ObservedDataTask(IDialogCreator dialogCreator, IOSPSuiteExecutionContext executionContext,
IDataRepositoryExportTask dataRepositoryExportTask, IContainerTask containerTask, IObjectTypeResolver objectTypeResolver)
{
_dialogCreator = dialogCreator;
_executionContext = executionContext;
Expand All @@ -68,7 +78,7 @@ protected ObservedDataTask(IDialogCreator dialogCreator, IOSPSuiteExecutionConte

public bool Delete(DataRepository observedData)
{
return Delete(new[] {observedData});
return Delete(new[] { observedData });
}

public bool Delete(IEnumerable<DataRepository> observedDataToBeRemoved, bool silent = false)
Expand Down Expand Up @@ -99,7 +109,8 @@ public bool Delete(IEnumerable<DataRepository> observedDataToBeRemoved, bool sil
return deleteAll(observedDataThatCanBeRemoved);
}

private string messageForObservedDataUsedByAnalysable(DataRepository observedData, Cache<DataRepository, IEnumerable<IUsesObservedData>> usersOfObservedDataCache)
private string messageForObservedDataUsedByAnalysable(DataRepository observedData,
Cache<DataRepository, IEnumerable<IUsesObservedData>> usersOfObservedDataCache)
{
var typeNamesUsingObservedData = usersOfObservedDataCache[observedData].Select(typeNamed).ToList();
return Error.CannotDeleteObservedData(observedData.Name, typeNamesUsingObservedData);
Expand Down Expand Up @@ -133,7 +144,8 @@ private string typeNamed(IUsesObservedData usesObservedData)

public void Export(DataRepository observedData)
{
var file = _dialogCreator.AskForFileToSave(Captions.ExportObservedDataToExcel, Constants.Filter.EXCEL_SAVE_FILE_FILTER, Constants.DirectoryKey.OBSERVED_DATA, observedData.Name);
var file = _dialogCreator.AskForFileToSave(Captions.ExportObservedDataToExcel, Constants.Filter.EXCEL_SAVE_FILE_FILTER,
Constants.DirectoryKey.OBSERVED_DATA, observedData.Name);
if (string.IsNullOrEmpty(file)) return;

_dataRepositoryExportTask.ExportToExcel(observedData, file, launchExcel: true);
Expand Down Expand Up @@ -166,11 +178,73 @@ public void AddImporterConfigurationToProject(ImporterConfiguration configuratio
_executionContext.AddToHistory(new AddImporterConfigurationToProjectCommand(configuration).Run(_executionContext));
}

public void RemoveUsedObservedDataFromSimulation(IReadOnlyList<UsedObservedData> usedObservedDataList)
{
if (!usedObservedDataList.Any())
return;

foreach (var usedObservedData in usedObservedDataList)
georgeDaskalakis marked this conversation as resolved.
Show resolved Hide resolved
{
var parameterIdentifications = findParameterIdentificationsUsing(usedObservedData).ToList();
if (!parameterIdentifications.Any())
continue;

_dialogCreator.MessageBoxInfo(
Captions.ParameterIdentification.CannotRemoveObservedDataBeingUsedByParameterIdentification(observedDataFrom(usedObservedData).Name,
parameterIdentifications.AllNames().ToList()));
return;
}

var viewResult = _dialogCreator.MessageBoxYesNo(Captions.ReallyRemoveObservedDataFromSimulation);
if (viewResult == ViewResult.No)
return;

usedObservedDataList.GroupBy(x => x.Simulation).Each(x => removeUsedObservedDataFromSimulation(x, x.Key));
}

private IEnumerable<ParameterIdentification> findParameterIdentificationsUsing(UsedObservedData usedObservedData)
{
var observedData = observedDataFrom(usedObservedData);
var simulation = usedObservedData.Simulation;

return from parameterIdentification in allParameterIdentifications()
let outputMappings = parameterIdentification.AllOutputMappingsFor(simulation)
where outputMappings.Any(x => x.UsesObservedData(observedData))
select parameterIdentification;
}

private void removeUsedObservedDataFromSimulation(IEnumerable<UsedObservedData> usedObservedData, ISimulation simulation)
{
_executionContext.Load(simulation);

var observedDataList = observedDataListFrom(usedObservedData);
observedDataList.Each(simulation.RemoveUsedObservedData);
observedDataList.Each(simulation.RemoveOutputMappings);

_executionContext.PublishEvent(new ObservedDataRemovedFromAnalysableEvent(simulation, observedDataList));
_executionContext.PublishEvent(new SimulationStatusChangedEvent(simulation));
}

private DataRepository observedDataFrom(UsedObservedData usedObservedData)
{
return _executionContext.Project.ObservedDataBy(usedObservedData.Id);
}

private IReadOnlyCollection<ParameterIdentification> allParameterIdentifications()
{
return _executionContext.Project.AllParameterIdentifications;
}

private IEnumerable<IUsesObservedData> allUsersOfObservedData(DataRepository observedData)
{
return _executionContext.Project.AllUsersOfObservedData.Where(x => x.UsesObservedData(observedData));
}

private IReadOnlyList<DataRepository> observedDataListFrom(IEnumerable<UsedObservedData> usedObservedData)
{
return usedObservedData.Select(observedDataFrom).ToList();
}

//TODO See if code can be merged between APPS
public abstract void Rename(DataRepository observedData);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using OSPSuite.Assets;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Data;
using OSPSuite.Core.Domain.Repositories;
Expand All @@ -23,7 +24,7 @@ public interface ISimulationOutputMappingPresenter : IPresenter<ISimulationOutpu
{
void EditSimulation(ISimulation simulation);
IReadOnlyList<SimulationQuantitySelectionDTO> AllAvailableOutputs { get; }
void RemoveOutputMapping(SimulationOutputMappingDTO outputMappingDTO);
void RemoveObservedData(SimulationOutputMappingDTO outputMappingDTO);
msevestre marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Completely rebinds the view to the content of the data source
Expand All @@ -39,10 +40,12 @@ public class SimulationOutputMappingPresenter : AbstractSubPresenter<ISimulation
{
private readonly IEntitiesInSimulationRetriever _entitiesInSimulationRetriever;
private readonly IObservedDataRepository _observedDataRepository;
private readonly IObservedDataTask _observedDataTask;
private readonly ISimulationOutputMappingToOutputMappingDTOMapper _outputMappingDTOMapper;
private readonly IQuantityToSimulationQuantitySelectionDTOMapper _simulationQuantitySelectionDTOMapper;
private readonly List<SimulationQuantitySelectionDTO> _allAvailableOutputs = new List<SimulationQuantitySelectionDTO>();
private readonly IOutputMappingMatchingTask _outputMappingMatchingTask;
private readonly SimulationQuantitySelectionDTO _noneEntry;

private readonly NotifyList<SimulationOutputMappingDTO> _listOfOutputMappingDTOs;

Expand All @@ -54,14 +57,17 @@ public SimulationOutputMappingPresenter(
IEntitiesInSimulationRetriever entitiesInSimulationRetriever,
IObservedDataRepository observedDataRepository,
ISimulationOutputMappingToOutputMappingDTOMapper outputMappingDTOMapper,
IQuantityToSimulationQuantitySelectionDTOMapper simulationQuantitySelectionDTOMapper) : base(view)
IQuantityToSimulationQuantitySelectionDTOMapper simulationQuantitySelectionDTOMapper,
IObservedDataTask observedDataTask) : base(view)
{
_entitiesInSimulationRetriever = entitiesInSimulationRetriever;
_observedDataRepository = observedDataRepository;
_outputMappingDTOMapper = outputMappingDTOMapper;
_simulationQuantitySelectionDTOMapper = simulationQuantitySelectionDTOMapper;
_noneEntry = new SimulationQuantitySelectionDTO(null, null, Captions.SimulationUI.NoneEditorNullText);
georgeDaskalakis marked this conversation as resolved.
Show resolved Hide resolved
_outputMappingMatchingTask = new OutputMappingMatchingTask(_entitiesInSimulationRetriever);
_listOfOutputMappingDTOs = new NotifyList<SimulationOutputMappingDTO>();
_observedDataTask = observedDataTask;
}

public void Refresh()
Expand All @@ -79,6 +85,9 @@ private void updateOutputLists()
_allAvailableOutputs.Clear();
var outputs = _entitiesInSimulationRetriever.OutputsFrom(_simulation);
_allAvailableOutputs.AddRange(outputs.Select(x => mapFrom(_simulation, x)).OrderBy(x => x.DisplayString));

//adding a none DTO output, in order to be able to select it and delete the outputMapping
_allAvailableOutputs.Add(_noneEntry);
}

public void EditSimulation(ISimulation simulation)
Expand Down Expand Up @@ -113,7 +122,8 @@ private void updateOutputMappingList()

public IReadOnlyList<SimulationQuantitySelectionDTO> AllAvailableOutputs => _allAvailableOutputs;

private SimulationQuantitySelectionDTO mapFrom(ISimulation simulation, IQuantity quantity) => _simulationQuantitySelectionDTOMapper.MapFrom(simulation, quantity);
private SimulationQuantitySelectionDTO mapFrom(ISimulation simulation, IQuantity quantity) =>
_simulationQuantitySelectionDTOMapper.MapFrom(simulation, quantity);

private SimulationOutputMappingDTO mapFrom(OutputMapping outputMapping) => _outputMappingDTOMapper.MapFrom(outputMapping, AllAvailableOutputs);

Expand All @@ -127,6 +137,13 @@ private IEnumerable<DataRepository> allAvailableObservedDataUsedBySimulation()
public void UpdateSimulationOutputMappings(SimulationOutputMappingDTO simulationOutputMappingDTO)
{
MarkSimulationAsChanged();

if (Equals(simulationOutputMappingDTO.Output, _noneEntry))
{
_simulation.RemoveOutputMappings(simulationOutputMappingDTO.ObservedData);
return;
}

if (!_simulation.OutputMappings.OutputMappingsUsingDataRepository(simulationOutputMappingDTO.ObservedData).Any())
_simulation.OutputMappings.Add(simulationOutputMappingDTO.Mapping);

Expand All @@ -138,11 +155,15 @@ public void MarkSimulationAsChanged()
_simulation.HasChanged = true;
}

public void RemoveOutputMapping(SimulationOutputMappingDTO outputMappingDTO)
public void RemoveObservedData(SimulationOutputMappingDTO outputMappingDTO)
{
outputMappingDTO.Output = null;
_simulation.OutputMappings.Remove(outputMappingDTO.Mapping);
_view.RefreshGrid();
var usedObservedData = new UsedObservedData
{
Id = outputMappingDTO.ObservedData.Id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using an id here an not the ObservedData itself. This USedObservedData does not feel like the right structure to me (this might be used like this somewhere else but in this case..I don't get it. Later, you are going through the project to retrieve the observed data by id. all of that is not required if we just have a reference to the observed data?

Simulation = _simulation
};

_observedDataTask.RemoveUsedObservedDataFromSimulation(new List<UsedObservedData>() {usedObservedData});
}

public void Handle(ObservedDataAddedToAnalysableEvent eventToHandle)
Expand Down
9 changes: 9 additions & 0 deletions src/OSPSuite.R/Domain/Simulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public class Simulation : ISimulation
public IEnumerable<CurveChart> Charts { get; } = new List<CurveChart>();
public OutputMappings OutputMappings { get; set; }
public DataRepository ResultsDataRepository { get; set; }
public void RemoveUsedObservedData(DataRepository dataRepository)
{
// nothing to do in R
}

public void RemoveOutputMappings(DataRepository dataRepository)
{
// nothing to do in R
}

public void RemoveAnalysis(ISimulationAnalysis simulationAnalysis)
{
Expand Down
12 changes: 4 additions & 8 deletions src/OSPSuite.UI/Views/SimulationOutputMappingView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ public override void InitializeBinding()

_gridViewBinder.Changed += NotifyViewChanged;

_removeButtonRepository.ButtonClick += (o, e) => OnEvent(() => _presenter.RemoveOutputMapping(_gridViewBinder.FocusedElement));
_removeButtonRepository.ButtonClick += (o, e) => OnEvent(() => _presenter.RemoveObservedData(_gridViewBinder.FocusedElement));
}

private void onOutputMappingEdited()
{
_presenter.MarkSimulationAsChanged();
}

private void onOutputValueChanged(SimulationOutputMappingDTO simulationOutputMappingDTO)
{
_presenter.UpdateSimulationOutputMappings(simulationOutputMappingDTO);
Expand All @@ -111,13 +112,8 @@ private IFormatter<DataRepository> observedDataDisplay(SimulationOutputMappingDT

private RepositoryItem allOutputsRepository(SimulationOutputMappingDTO dto)
{
return RepositoryItemFor(_presenter.AllAvailableOutputs, _outputRepository);
}

protected RepositoryItem RepositoryItemFor<T>(IEnumerable<T> allItems, UxRepositoryItemComboBox listRepositoryItems)
{
listRepositoryItems.FillComboBoxRepositoryWith(allItems);
return listRepositoryItems;
_outputRepository.FillComboBoxRepositoryWith(_presenter.AllAvailableOutputs);
return _outputRepository;
}

public override string Caption => Captions.SimulationUI.ObservedDataSelection;
Expand Down
Loading