Skip to content

Commit

Permalink
Fixes #2536 loading project from snapshot observed data
Browse files Browse the repository at this point in the history
  • Loading branch information
msevestre committed Mar 14, 2023
1 parent 79e376a commit 1a3d853
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 38 deletions.
36 changes: 36 additions & 0 deletions src/PKSim.Core/Services/IChartTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using OSPSuite.Core.Chart;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Data;
using PKSim.Core.Model;

namespace PKSim.Core.Services
{
public interface IChartTask
{
/// <summary>
/// updates all observed data defined in the simulation to the underlying charts of this simulation
/// </summary>
/// <param name="simulation">Simulation used to retrieve the charts to update</param>
/// <param name="project">Optional project. If undefined, it will be retrieved from the project retriever</param>
void UpdateObservedDataInChartsFor(Simulation simulation, IProject project = null);

/// <summary>
/// adds all observed data defined in the simulation to<paramref name="chartWithObservedData"/>
/// </summary>
/// <param name="simulation">Simulation used to retrieve the charts to update</param>>>
/// <param name="chartWithObservedData">Chart where observed data should be updated</param>
/// <param name="project">Optional project. If undefined, it will be retrieved from the project retriever</param>
void UpdateObservedDataInChartFor(Simulation simulation, ChartWithObservedData chartWithObservedData, IProject project = null);

/// <summary>
/// Returns if the column should be displayed or not
/// </summary>
bool IsColumnVisibleInDataBrowser(DataColumn dataColumn);

/// <summary>
/// Sets origin data for the <paramref name="chart" /> to indicate project name , <paramref name="simulationName" /> and
/// current date/time
/// </summary>
void SetOriginTextFor(string simulationName, IChart chart);
}
}
6 changes: 5 additions & 1 deletion src/PKSim.Core/Snapshots/Mappers/SimulationMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class SimulationMapper : ObjectBaseSnapshotMapperBase<ModelSimulation, Sn
private readonly IOSPSuiteLogger _logger;
private readonly IContainerTask _containerTask;
private readonly IEntityPathResolver _entityPathResolver;
private readonly IChartTask _chartTask;

public SimulationMapper(
SolverSettingsMapper solverSettingsMapper,
Expand All @@ -77,7 +78,8 @@ public SimulationMapper(
ISimulationParameterOriginIdUpdater simulationParameterOriginIdUpdater,
IOSPSuiteLogger logger,
IContainerTask containerTask,
IEntityPathResolver entityPathResolver)
IEntityPathResolver entityPathResolver,
IChartTask chartTask)
{
_solverSettingsMapper = solverSettingsMapper;
_outputSchemaMapper = outputSchemaMapper;
Expand All @@ -101,6 +103,7 @@ public SimulationMapper(
_logger = logger;
_containerTask = containerTask;
_entityPathResolver = entityPathResolver;
_chartTask = chartTask;
}

public override async Task<SnapshotSimulation> MapToSnapshot(ModelSimulation simulation, PKSimProject project)
Expand Down Expand Up @@ -305,6 +308,7 @@ public override async Task<ModelSimulation> MapToModel(SnapshotSimulation snapsh
simulation.AddAnalyses(await populationAnalysesFrom(simulation, snapshot.PopulationAnalyses, snapshotContext));

_simulationParameterOriginIdUpdater.UpdateSimulationId(simulation);
_chartTask.UpdateObservedDataInChartsFor(simulation, snapshotContext.Project);
return simulation;
}

Expand Down
17 changes: 8 additions & 9 deletions src/PKSim.Infrastructure/Services/ChartTask.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using OSPSuite.Core.Chart;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Data;
using OSPSuite.Core.Domain.Services;
using OSPSuite.Utility.Container;
using OSPSuite.Utility.Extensions;
using PKSim.Core.Model;
using PKSim.Core.Services;
using PKSim.Presentation.Services;

namespace PKSim.Infrastructure.Services
Expand All @@ -22,22 +23,20 @@ public void SetOriginTextFor(string simulationName, IChart chart)
chart.SetOriginTextFor(_projectRetriever.CurrentProject.Name, simulationName);
}

public void UpdateObservedDataInChartsFor(Simulation simulation)
public void UpdateObservedDataInChartsFor(Simulation simulation, IProject project = null)
{
simulation.ChartWithObservedData.Each(c => UpdateObservedDataInChartFor(simulation, c));
simulation.ChartWithObservedData.Each(c => UpdateObservedDataInChartFor(simulation, c, project));
}

public void UpdateObservedDataInChartFor(Simulation simulation, ChartWithObservedData chartWithObservedData)
public void UpdateObservedDataInChartFor(Simulation simulation, ChartWithObservedData chartWithObservedData, IProject project = null)
{
var projectToUse = project ?? _projectRetriever.CurrentProject;
foreach (var usedObservedData in simulation.UsedObservedData)
{
chartWithObservedData.AddObservedData(_projectRetriever.CurrentProject.ObservedDataBy(usedObservedData.Id));
chartWithObservedData.AddObservedData(projectToUse.ObservedDataBy(usedObservedData.Id));
}
}

public bool IsColumnVisibleInDataBrowser(DataColumn dataColumn)
{
return !dataColumn.IsBaseGrid();
}
public bool IsColumnVisibleInDataBrowser(DataColumn dataColumn) => !dataColumn.IsBaseGrid();
}
}
1 change: 1 addition & 0 deletions src/PKSim.Infrastructure/Services/ChartTemplatingTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using PKSim.Core.Chart;
using PKSim.Core.Commands;
using PKSim.Core.Model;
using PKSim.Core.Services;
using PKSim.Presentation.Services;

namespace PKSim.Infrastructure.Services
Expand Down
22 changes: 0 additions & 22 deletions src/PKSim.Presentation/Services/IChartTask.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/PKSim.Presentation/Views/Charts/PKSimChartFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using OSPSuite.Utility.Extensions;
using PKSim.Core.Chart;
using PKSim.Core.Model;
using PKSim.Core.Services;
using PKSim.Presentation.Services;

namespace PKSim.Presentation.Views.Charts
Expand Down
16 changes: 12 additions & 4 deletions tests/PKSim.Tests/Core/SimulationMapperSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using OSPSuite.BDDHelper.Extensions;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Builder;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Core.Domain.Services;
using OSPSuite.Core.Extensions;
using OSPSuite.Core.Services;
Expand All @@ -17,21 +16,22 @@
using PKSim.Core.Services;
using PKSim.Core.Snapshots;
using PKSim.Core.Snapshots.Mappers;
using PKSim.Infrastructure.Services;
using AdvancedParameter = PKSim.Core.Snapshots.AdvancedParameter;
using Compound = PKSim.Core.Model.Compound;
using CompoundProperties = PKSim.Core.Model.CompoundProperties;
using DataRepository = OSPSuite.Core.Domain.Data.DataRepository;
using Individual = PKSim.Core.Model.Individual;
using ModelOutputMapping = OSPSuite.Core.Domain.OutputMapping;
using ObserverSet = PKSim.Core.Model.ObserverSet;
using OutputSchema = OSPSuite.Core.Domain.OutputSchema;
using OutputSelections = PKSim.Core.Snapshots.OutputSelections;
using PopulationAnalysisChart = PKSim.Core.Model.PopulationAnalyses.PopulationAnalysisChart;
using Protocol = PKSim.Core.Model.Protocol;
using Simulation = PKSim.Core.Snapshots.Simulation;
using SimulationRunOptions = PKSim.Core.Services.SimulationRunOptions;
using SolverSettings = OSPSuite.Core.Domain.SolverSettings;
using ModelOutputMapping = OSPSuite.Core.Domain.OutputMapping;
using SnapshotOutputMapping = PKSim.Core.Snapshots.OutputMapping;
using SolverSettings = OSPSuite.Core.Domain.SolverSettings;

namespace PKSim.Core
{
Expand Down Expand Up @@ -93,6 +93,7 @@ public abstract class concern_for_SimulationMapper : ContextSpecificationAsync<S
protected ModelOutputMapping _outputMapping;
protected OutputMappingMapper _outputMappingMapper;
protected SnapshotOutputMapping _snapshotOutputMapping;
protected IChartTask _chartTask;

protected override Task Context()
{
Expand All @@ -118,6 +119,7 @@ protected override Task Context()
_logger = A.Fake<IOSPSuiteLogger>();
_containerTask = A.Fake<IContainerTask>();
_entityPathResolver = A.Fake<IEntityPathResolver>();
_chartTask = A.Fake<IChartTask>();

sut = new SimulationMapper(_solverSettingsMapper, _outputSchemaMapper,
_outputSelectionMapper, _compoundPropertiesMapper, _parameterMapper,
Expand All @@ -126,7 +128,7 @@ protected override Task Context()
_simulationFactory, _executionContext, _simulationModelCreator,
_simulationBuildingBlockUpdater, _modelPropertiesTask,
_simulationRunner, _simulationParameterOriginIdUpdater,
_logger, _containerTask, _entityPathResolver
_logger, _containerTask, _entityPathResolver, _chartTask
);

_project = new PKSimProject();
Expand Down Expand Up @@ -654,6 +656,12 @@ public void should_update_the_altered_flag_for_each_altered_building_block()
{
_simulation.UsedBuildingBlockInSimulation<Individual>().Altered.ShouldBeTrue();
}

[Observation]
public void should_have_updated_the_reference_to_observed_data_in_all_charts()
{
A.CallTo(() => _chartTask.UpdateObservedDataInChartsFor(_simulation, _project)).MustHaveHappened();
}
}

public class when_mapping_a_population_simulation_snapshot_to_simulation : concern_for_SimulationMapper
Expand Down
3 changes: 2 additions & 1 deletion tests/PKSim.Tests/Infrastructure/ChartTemplatingTaskSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using PKSim.Core;
using PKSim.Core.Chart;
using PKSim.Core.Model;
using PKSim.Core.Services;
using PKSim.Infrastructure.Serialization.Xml.Serializers;
using PKSim.Infrastructure.Services;
using PKSim.Presentation.Services;
Expand Down Expand Up @@ -88,7 +89,7 @@ protected override void Because()
[Observation]
public void the_chart_task_should_update_observed_data()
{
A.CallTo(() => _chartTask.UpdateObservedDataInChartFor(A<Simulation>._, _chartWithObservedData)).MustHaveHappened();
A.CallTo(() => _chartTask.UpdateObservedDataInChartFor(A<Simulation>._, _chartWithObservedData, null)).MustHaveHappened();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void should_add_the_charts_to_the_simulation()
[Observation]
public void should_add_all_observed_data_defined_in_the_simulation()
{
A.CallTo(() => _chartTask.UpdateObservedDataInChartsFor(_simulation)).MustHaveHappened();
A.CallTo(() => _chartTask.UpdateObservedDataInChartsFor(_simulation, null)).MustHaveHappened();
}
}
}
1 change: 1 addition & 0 deletions tests/PKSim.Tests/Presentation/ChartTaskSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using OSPSuite.Core.Domain.Services;
using OSPSuite.Utility.Events;
using PKSim.Core;
using PKSim.Core.Services;
using PKSim.Infrastructure.Services;
using PKSim.Presentation.Services;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using PKSim.Core;
using PKSim.Core.Chart;
using PKSim.Core.Model;
using PKSim.Core.Services;
using PKSim.Presentation.Nodes;
using PKSim.Presentation.Presenters.Charts;
using PKSim.Presentation.Presenters.Simulations;
Expand Down
1 change: 1 addition & 0 deletions tests/PKSim.Tests/Presentation/PKSimChartFactorySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using OSPSuite.Presentation;
using OSPSuite.Utility.Container;
using PKSim.Core.Chart;
using PKSim.Core.Services;
using PKSim.Presentation.Services;
using PKSim.Presentation.Views.Charts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using PKSim.Core;
using PKSim.Core.Chart;
using PKSim.Core.Model;
using PKSim.Core.Services;
using PKSim.Presentation.Presenters.Charts;
using PKSim.Presentation.Presenters.Simulations;
using PKSim.Presentation.Services;
Expand Down

0 comments on commit 1a3d853

Please sign in to comment.