Skip to content

Commit

Permalink
Fixes #2203 Instantiate a single observed data simple chart and updat…
Browse files Browse the repository at this point in the history
…e that single instance each time the user selects an observed data (#2209)
  • Loading branch information
rwmcintosh committed Feb 14, 2024
1 parent 69e1dfd commit 98304a6
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 819 deletions.
1 change: 1 addition & 0 deletions src/OSPSuite.Assets/UIConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public static class Captions
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 readonly string SimulationWasCanceled = "Simulation was canceled";
public static readonly string SelectMappingToShowObservedData = "Select mapping to show observed data";

public static string ShouldWatermarkBeUsedForChartExportToClipboard(string applicationName, string optionLocation)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public interface ISimpleChartPresenter : IPresenter<ISimpleChartView>
/// Refresh the display after external changes were made to the chart
/// </summary>
void Refresh();

void Clear();
}

public class SimpleChartPresenter : AbstractCommandCollectorPresenter<ISimpleChartView, ISimpleChartPresenter>, ISimpleChartPresenter
Expand All @@ -93,6 +95,8 @@ public SimpleChartPresenter(ISimpleChartView view, IChartDisplayPresenter chartD
AddSubPresenters(_chartDisplayPresenter);
}

public void Clear() => _chartDisplayPresenter.Clear();

public Action<int> HotTracked
{
set => _chartDisplayPresenter.HotTracked = value;
Expand Down Expand Up @@ -136,15 +140,12 @@ private void bindToChart()
public CurveChart Plot(DataRepository dataRepository, Scalings scale)
{
Chart = _chartFactory.CreateChartFor(dataRepository, scale);
setChartScalingForObservedData(new[] {dataRepository});
setChartScalingForObservedData(new[] { dataRepository });
bindToChart();
return Chart;
}

public CurveChart Plot(DataRepository dataRepository)
{
return Plot(dataRepository, _presentationUserSettings.DefaultChartYScaling);
}
public CurveChart Plot(DataRepository dataRepository) => Plot(dataRepository, _presentationUserSettings.DefaultChartYScaling);

public CurveChart PlotObservedData(IEnumerable<DataRepository> observedData)
{
Expand All @@ -165,20 +166,11 @@ private void setChartScalingForObservedData(IReadOnlyList<DataRepository> observ
Chart.DefaultYAxisScaling = Scalings.Linear;
}

private bool shouldUseLinearScaling(IReadOnlyList<DataRepository> observedData)
{
return observedData.Any(dataRepositoryHasFraction);
}
private bool shouldUseLinearScaling(IReadOnlyList<DataRepository> observedData) => observedData.Any(dataRepositoryHasFraction);

private bool dataRepositoryHasFraction(DataRepository dataRepository)
{
return dataRepository.AllButBaseGrid().Any(x => x.IsFraction());
}
private bool dataRepositoryHasFraction(DataRepository dataRepository) => dataRepository.AllButBaseGrid().Any(x => x.IsFraction());

public CurveChart PlotObservedData(DataRepository observedData)
{
return PlotObservedData(new[] {observedData}).WithName(observedData.Name);
}
public CurveChart PlotObservedData(DataRepository observedData) => PlotObservedData(new[] { observedData }).WithName(observedData.Name);

private void setScaleInView(CurveChart chart)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System;
using OSPSuite.Utility.Events;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Core.Events;
using OSPSuite.Presentation.Views.ParameterIdentifications;
using OSPSuite.Utility.Events;

namespace OSPSuite.Presentation.Presenters.ParameterIdentifications
{
public interface IParameterIdentificationDataSelectionPresenter : IParameterIdentificationItemPresenter,
IListener<ObservedDataAddedToAnalysableEvent>,
IListener<ObservedDataRemovedFromAnalysableEvent>,
IListener<RenamedEvent>,
IListener<SimulationRemovedEvent>,
IListener<ObservedDataAddedToAnalysableEvent>,
IListener<ObservedDataRemovedFromAnalysableEvent>,
IListener<RenamedEvent>,
IListener<SimulationRemovedEvent>,
IListener<SimulationReplacedInParameterAnalyzableEvent>
{
event EventHandler<SimulationEventArgs> SimulationAdded;
Expand All @@ -22,23 +22,23 @@ public class ParameterIdentificationDataSelectionPresenter : AbstractSubPresente
{
private readonly IParameterIdentificationSimulationSelectionPresenter _simulationSelectionPresenter;
private readonly IParameterIdentificationOutputMappingPresenter _outputMappingPresenter;
private readonly IParameterIdentificationWeightedObservedDataCollectorPresenter _weightedObservedDataCollectorPresenter;
private readonly IParameterIdentificationWeightedObservedDataPresenter _weightedObservedDataPresenter;
private ParameterIdentification _parameterIdentification;
public event EventHandler<SimulationEventArgs> SimulationAdded = delegate { };
public event EventHandler<SimulationEventArgs> SimulationRemoved = delegate { };

public ParameterIdentificationDataSelectionPresenter(IParameterIdentificationDataSelectionView view,
IParameterIdentificationSimulationSelectionPresenter simulationSelectionPresenter,
IParameterIdentificationOutputMappingPresenter outputMappingPresenter,
IParameterIdentificationWeightedObservedDataCollectorPresenter weightedObservedDataCollectorPresenter) : base(view)
IParameterIdentificationWeightedObservedDataPresenter weightedObservedDataPresenter) : base(view)
{
_simulationSelectionPresenter = simulationSelectionPresenter;
_outputMappingPresenter = outputMappingPresenter;
_weightedObservedDataCollectorPresenter = weightedObservedDataCollectorPresenter;
_weightedObservedDataPresenter = weightedObservedDataPresenter;
view.AddSimulationSelectionView(_simulationSelectionPresenter.BaseView);
view.AddOutputMappingView(_outputMappingPresenter.BaseView);
view.AddWeightedObservedDataCollectorView(_weightedObservedDataCollectorPresenter.BaseView);
AddSubPresenters(_simulationSelectionPresenter, _outputMappingPresenter, _weightedObservedDataCollectorPresenter);
view.AddWeightedObservedDataCollectorView(_weightedObservedDataPresenter.BaseView);
AddSubPresenters(_simulationSelectionPresenter, _outputMappingPresenter, _weightedObservedDataPresenter);

_simulationSelectionPresenter.SimulationAdded += (o, e) => simulationAdded(e);
_simulationSelectionPresenter.SimulationRemoved += (o, e) => simulationRemoved(e);
Expand All @@ -48,20 +48,11 @@ public ParameterIdentificationDataSelectionPresenter(IParameterIdentificationDat
_outputMappingPresenter.ObservedDataSelected += (o, e) => observedDataSelected(e.WeightedObservedData);
}

private void observedDataUnmapped(WeightedObservedData weightedObservedData)
{
_weightedObservedDataCollectorPresenter.RemoveObservedData(weightedObservedData);
}
private void observedDataUnmapped(WeightedObservedData weightedObservedData) => _weightedObservedDataPresenter.Clear(weightedObservedData);

private void observedDataSelected(WeightedObservedData weightedObservedData)
{
_weightedObservedDataCollectorPresenter.SelectObservedData(weightedObservedData);
}
private void observedDataSelected(WeightedObservedData weightedObservedData) => _weightedObservedDataPresenter.Edit(weightedObservedData);

private void observedDataMapped(WeightedObservedData weightedObservedData)
{
_weightedObservedDataCollectorPresenter.AddObservedData(weightedObservedData);
}
private void observedDataMapped(WeightedObservedData weightedObservedData) => _weightedObservedDataPresenter.Edit(weightedObservedData);

private void simulationAdded(SimulationEventArgs e)
{
Expand All @@ -72,7 +63,6 @@ private void simulationAdded(SimulationEventArgs e)
private void updateOutputAndWeightsPresenters()
{
_outputMappingPresenter.Refresh();
_weightedObservedDataCollectorPresenter.Refresh();
ViewChanged();
}

Expand All @@ -87,7 +77,6 @@ public void EditParameterIdentification(ParameterIdentification parameterIdentif
_parameterIdentification = parameterIdentification;
_simulationSelectionPresenter.EditParameterIdentification(_parameterIdentification);
_outputMappingPresenter.EditParameterIdentification(_parameterIdentification);
_weightedObservedDataCollectorPresenter.EditParameterIdentification(_parameterIdentification);
}

public void Handle(ObservedDataAddedToAnalysableEvent eventToHandle)
Expand All @@ -104,10 +93,7 @@ private bool canHandle(AnalysableEvent analysableEvent)
return identificationUsesSimulation(analysableEvent.Analysable as ISimulation);
}

private bool identificationUsesSimulation(ISimulation simulation)
{
return _parameterIdentification.UsesSimulation(simulation);
}
private bool identificationUsesSimulation(ISimulation simulation) => _parameterIdentification.UsesSimulation(simulation);

public void Handle(ObservedDataRemovedFromAnalysableEvent eventToHandle)
{
Expand All @@ -124,21 +110,14 @@ public void Handle(RenamedEvent eventToHandle)
_outputMappingPresenter.Refresh();
}

private bool canHandle(RenamedEvent eventToHandle)
{
return identificationUsesSimulation(eventToHandle.RenamedObject as ISimulation);
}
private bool canHandle(RenamedEvent eventToHandle) => identificationUsesSimulation(eventToHandle.RenamedObject as ISimulation);

public void Handle(SimulationRemovedEvent eventToHandle)
{
refreshSubPresenters();
}
public void Handle(SimulationRemovedEvent eventToHandle) => refreshSubPresenters();

private void refreshSubPresenters()
{
_simulationSelectionPresenter.Refresh();
_outputMappingPresenter.Refresh();
_weightedObservedDataCollectorPresenter.Refresh();
}

public void Handle(SimulationReplacedInParameterAnalyzableEvent eventToHandle)
Expand All @@ -147,9 +126,6 @@ public void Handle(SimulationReplacedInParameterAnalyzableEvent eventToHandle)
refreshSubPresenters();
}

private bool canHandle(SimulationReplacedInParameterAnalyzableEvent eventToHandle)
{
return Equals(eventToHandle.ParameterAnalysable, _parameterIdentification);
}
private bool canHandle(SimulationReplacedInParameterAnalyzableEvent eventToHandle) => Equals(eventToHandle.ParameterAnalysable, _parameterIdentification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,12 @@ public void Refresh()
this.DoWithinLatch(() =>
{
UpdateCache();
updateOutputMapppingList();
_view.BindTo(_allOutputMappingDTOs);
updateOutputMappingList();
});
_view.BindTo(_allOutputMappingDTOs);
}

public void UpdateCache()
{
_allAvailableOutputs.Clear();
}
public void UpdateCache() => _allAvailableOutputs.Clear();

public void AddOutputMapping()
{
Expand All @@ -114,7 +111,7 @@ public void AddOutputMapping()
OnStatusChanged();
}

private void updateOutputMapppingList()
private void updateOutputMappingList()
{
_allOutputMappingDTOs.Clear();
_parameterIdentification.AllOutputMappings.Each(x => _allOutputMappingDTOs.Add(mapFrom(x)));
Expand All @@ -137,20 +134,11 @@ public IEnumerable<SimulationQuantitySelectionDTO> AllAvailableOutputs
}
}

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

private OutputMappingDTO mapFrom(OutputMapping outputMapping)
{
return _outputMappingDTOMapper.MapFrom(outputMapping, AllAvailableOutputs);
}
private OutputMappingDTO mapFrom(OutputMapping outputMapping) => _outputMappingDTOMapper.MapFrom(outputMapping, AllAvailableOutputs);

public IEnumerable<DataRepository> AllObservedDataFor(OutputMappingDTO outputMappingDTO)
{
return allPossibleObservedDataForOutput(outputMappingDTO.Output);
}
public IEnumerable<DataRepository> AllObservedDataFor(OutputMappingDTO outputMappingDTO) => allPossibleObservedDataForOutput(outputMappingDTO.Output);

private IEnumerable<DataRepository> allPossibleObservedDataForOutput(SimulationQuantitySelectionDTO outputSelectionDTO)
{
Expand All @@ -165,7 +153,7 @@ private IEnumerable<DataRepository> allPossibleObservedDataForOutput(SimulationQ

public void ObservedDataSelectionChanged(OutputMappingDTO dto, DataRepository newObservedData, DataRepository oldObservedData)
{
var allOutputsUsingObservedData = _allOutputMappingDTOs.Where(x => Equals(x.ObservedData, newObservedData)).Except(new[] {dto}).ToList();
var allOutputsUsingObservedData = _allOutputMappingDTOs.Where(x => Equals(x.ObservedData, newObservedData)).Except(new[] { dto }).ToList();

if (observedDataAlreadySelectedForSameOutput(dto.Output, newObservedData))
{
Expand Down Expand Up @@ -197,10 +185,7 @@ public void ObservedDataSelectionChanged(OutputMappingDTO dto, DataRepository ne
return allIds.Max() + 1;
}

private bool observedDataAlreadySelectedForSameOutput(SimulationQuantitySelectionDTO outputDTO, DataRepository observedData)
{
return _allOutputMappingDTOs.Count(x => Equals(x.Output, outputDTO) && Equals(x.ObservedData, observedData)) > 1;
}
private bool observedDataAlreadySelectedForSameOutput(SimulationQuantitySelectionDTO outputDTO, DataRepository observedData) => _allOutputMappingDTOs.Count(x => Equals(x.Output, outputDTO) && Equals(x.ObservedData, observedData)) > 1;

public void OutputSelectionChanged(OutputMappingDTO dto, SimulationQuantitySelectionDTO newOutput, SimulationQuantitySelectionDTO oldOutput)
{
Expand All @@ -214,10 +199,7 @@ public void OutputSelectionChanged(OutputMappingDTO dto, SimulationQuantitySelec
dto.Scaling = _parameterIdentificationTask.DefaultScalingFor(newOutput.Quantity);
}

public void Select(OutputMappingDTO outputMappingDTO)
{
this.DoWithinLatch(() => ObservedDataSelected(this, new ObservedDataEventArgs(outputMappingDTO.WeightedObservedData)));
}
public void Select(OutputMappingDTO outputMappingDTO) => this.DoWithinLatch(() => ObservedDataSelected(this, new ObservedDataEventArgs(outputMappingDTO.WeightedObservedData)));

public void RemoveOutputMapping(OutputMappingDTO outputMappingDTO)
{
Expand All @@ -234,9 +216,6 @@ private void raiseObservedDataUnmappedFor(WeightedObservedData weightedObservedD
ObservedDataUnmapped(this, new ObservedDataEventArgs(weightedObservedData));
}

private void raiseObservedDataMappedFor(WeightedObservedData weightedObservedData)
{
ObservedDataMapped(this, new ObservedDataEventArgs(weightedObservedData));
}
private void raiseObservedDataMappedFor(WeightedObservedData weightedObservedData) => ObservedDataMapped(this, new ObservedDataEventArgs(weightedObservedData));
}
}
Loading

0 comments on commit 98304a6

Please sign in to comment.