From 670a4fc81f4a214529658d8a46873282efa87f76 Mon Sep 17 00:00:00 2001 From: Michael Sevestre Date: Mon, 2 Jan 2023 11:30:52 -0500 Subject: [PATCH] Fixes #2485 observed added mobi (#2500) --- .../Services/SimulationPersistableUpdater.cs | 30 ++----- .../Core/SimulationPersistableUpdaterSpecs.cs | 84 ++++++++++++++----- 2 files changed, 68 insertions(+), 46 deletions(-) diff --git a/src/PKSim.Core/Services/SimulationPersistableUpdater.cs b/src/PKSim.Core/Services/SimulationPersistableUpdater.cs index 215e3b119..482825009 100644 --- a/src/PKSim.Core/Services/SimulationPersistableUpdater.cs +++ b/src/PKSim.Core/Services/SimulationPersistableUpdater.cs @@ -87,36 +87,16 @@ private void setMoleculeAmountToPersistableIn(IContainer container) private void setApplicationObserversNonPersistable(Simulation simulation) { //Set all observers defined in application to persistable false - var applicationSet = simulation.Model.Root.GetSingleChildByName(Constants.APPLICATIONS); - if (applicationSet == null) return; + var applications = simulation.Model.Root.Container(Constants.APPLICATIONS); - foreach (var appObserver in applicationSet.GetAllChildren()) - { - if (applicationObserverShouldBeShown(appObserver)) - continue; - - appObserver.Persistable = false; - } + applications?.GetAllChildren(applicationObserverShouldBeHidden) + .Each(x => x.Persistable = false); } /// - /// Return true if an application observer should not be hidden from user + /// Return true if an application observer should be hidden from user /// /// - private bool applicationObserverShouldBeShown(IObserver observer) - { - string name = observer.Name; - - if (name.StartsWith(CoreConstants.Observer.FRACTION_SOLID_PREFIX)) - return true; - - if (name.StartsWith(CoreConstants.Observer.FRACTION_DISSOLVED_PREFIX)) - return true; - - if (name.StartsWith(CoreConstants.Observer.FRACTION_INSOLUBLE_PREFIX)) - return true; - - return false; - } + private bool applicationObserverShouldBeHidden(IObserver observer) => observer.Name == CoreConstants.Observer.CONCENTRATION_IN_CONTAINER; } } \ No newline at end of file diff --git a/tests/PKSim.Tests/Core/SimulationPersistableUpdaterSpecs.cs b/tests/PKSim.Tests/Core/SimulationPersistableUpdaterSpecs.cs index 44e9caa57..0d5fc4bb5 100644 --- a/tests/PKSim.Tests/Core/SimulationPersistableUpdaterSpecs.cs +++ b/tests/PKSim.Tests/Core/SimulationPersistableUpdaterSpecs.cs @@ -1,11 +1,11 @@ -using OSPSuite.BDDHelper; -using OSPSuite.BDDHelper.Extensions; -using FakeItEasy; +using FakeItEasy; using NUnit.Framework; -using PKSim.Core.Model; -using PKSim.Core.Services; +using OSPSuite.BDDHelper; +using OSPSuite.BDDHelper.Extensions; using OSPSuite.Core.Domain; using OSPSuite.Core.Domain.Builder; +using PKSim.Core.Model; +using PKSim.Core.Services; namespace PKSim.Core { @@ -30,10 +30,11 @@ public class When_updating_the_simulation_settings_for_a_simulation : concern_fo protected override void Context() { base.Context(); - var organsim = new Container().WithName(Constants.ORGANISM); - var venousBlood = new Container().WithName(CoreConstants.Organ.VENOUS_BLOOD).WithParentContainer(organsim); - var peripheralVenousBlood = new Container().WithName(CoreConstants.Organ.PERIPHERAL_VENOUS_BLOOD).WithParentContainer(organsim); - var lumen = new Container().WithName(CoreConstants.Organ.LUMEN).WithParentContainer(organsim); + var organism = new Container().WithName(Constants.ORGANISM); + var applications = new Container().WithName(Constants.APPLICATIONS); + var venousBlood = new Container().WithName(CoreConstants.Organ.VENOUS_BLOOD).WithParentContainer(organism); + var peripheralVenousBlood = new Container().WithName(CoreConstants.Organ.PERIPHERAL_VENOUS_BLOOD).WithParentContainer(organism); + var lumen = new Container().WithName(CoreConstants.Organ.LUMEN).WithParentContainer(organism); var plasma = new Container().WithName(CoreConstants.Compartment.PLASMA); var moleculeVenousBlood = new Container().WithName("DRUG") .WithParentContainer(plasma.WithParentContainer(venousBlood)); @@ -53,14 +54,14 @@ protected override void Context() _fabsObserver = new Observer().WithName(CoreConstants.Observer.FABS_ORAL); moleculeLumen.Add(_fabsObserver); + var compound = new Compound().WithName("DRUG"); _individualSimulation = new IndividualSimulation(); _individualSimulation.SimulationSettings = new SimulationSettings(); - _individualSimulation.AddUsedBuildingBlock(new UsedBuildingBlock("Id", PKSimBuildingBlockType.Compound) { BuildingBlock = compound }); + _individualSimulation.AddUsedBuildingBlock(new UsedBuildingBlock("Id", PKSimBuildingBlockType.Compound) {BuildingBlock = compound}); _individualSimulation.Model = new OSPSuite.Core.Domain.Model(); - _individualSimulation.Model.Root = new Container(); - _individualSimulation.Model.Root.Add(organsim); + _individualSimulation.Model.Root = new Container {organism, applications}; } protected override void Because() @@ -81,12 +82,53 @@ public void should_always_select_the_lumen_fabs_observer() } [Observation] - public void should_always_select_the_peripheral_venous_blood_plasna() + public void should_always_select_the_peripheral_venous_blood_plasma() { _peripheralVenousBloodObserver.Persistable.ShouldBeTrue(); } } + public class When_resetting_the_persitable_flags_for_a_simulation : concern_for_SimulationPersistableUpdater + { + private IndividualSimulation _individualSimulation; + private Observer _appConcentrationObserver; + private Observer _appUserDefinedObserver; + + protected override void Context() + { + base.Context(); + var organism = new Container().WithName(Constants.ORGANISM); + var applications = new Container().WithName(Constants.APPLICATIONS); + + _appConcentrationObserver = new Observer().WithName(CoreConstants.Observer.CONCENTRATION_IN_CONTAINER); + _appUserDefinedObserver = new Observer().WithName("Super awesome observer"); + + applications.Add(_appConcentrationObserver); + applications.Add(_appUserDefinedObserver); + + + _individualSimulation = new IndividualSimulation + { + Model = new OSPSuite.Core.Domain.Model + { + Root = new Container {organism, applications} + } + }; + } + + protected override void Because() + { + sut.ResetPersistable(_individualSimulation); + } + + [Observation] + public void should_show_all_observer_under_application_except_concentration() + { + _appUserDefinedObserver.Persistable.ShouldBeTrue(); + _appConcentrationObserver.Persistable.ShouldBeFalse(); + } + } + public class When_updating_the_simulation_settings_for_a_population_simulation : concern_for_SimulationPersistableUpdater { private PopulationSimulation _populationSimulation; @@ -97,10 +139,10 @@ public class When_updating_the_simulation_settings_for_a_population_simulation : protected override void Context() { base.Context(); - var organsim = new Container().WithName(Constants.ORGANISM); - var venousBlood = new Container().WithName(CoreConstants.Organ.VENOUS_BLOOD).WithParentContainer(organsim); - var peripheralVenousBlood = new Container().WithName(CoreConstants.Organ.PERIPHERAL_VENOUS_BLOOD).WithParentContainer(organsim); - var lumen = new Container().WithName(CoreConstants.Organ.LUMEN).WithParentContainer(organsim); + var organism = new Container().WithName(Constants.ORGANISM); + var venousBlood = new Container().WithName(CoreConstants.Organ.VENOUS_BLOOD).WithParentContainer(organism); + var peripheralVenousBlood = new Container().WithName(CoreConstants.Organ.PERIPHERAL_VENOUS_BLOOD).WithParentContainer(organism); + var lumen = new Container().WithName(CoreConstants.Organ.LUMEN).WithParentContainer(organism); var plasma = new Container().WithName(CoreConstants.Compartment.PLASMA); var moleculeVenousBlood = new Container().WithName("DRUG") .WithParentContainer(plasma.WithParentContainer(venousBlood)); @@ -124,10 +166,10 @@ protected override void Context() _populationSimulation = new PopulationSimulation(); _populationSimulation.SimulationSettings = new SimulationSettings(); - _populationSimulation.AddUsedBuildingBlock(new UsedBuildingBlock("Id", PKSimBuildingBlockType.Compound) { BuildingBlock = compound }); + _populationSimulation.AddUsedBuildingBlock(new UsedBuildingBlock("Id", PKSimBuildingBlockType.Compound) {BuildingBlock = compound}); _populationSimulation.Model = new OSPSuite.Core.Domain.Model(); _populationSimulation.Model.Root = new Container(); - _populationSimulation.Model.Root.Add(organsim); + _populationSimulation.Model.Root.Add(organism); } protected override void Because() @@ -197,8 +239,8 @@ protected override void Context() var compound = new Compound().WithName("DRUG"); _individualSimulation = new IndividualSimulation(); - _individualSimulation.AddUsedBuildingBlock(new UsedBuildingBlock("Id", PKSimBuildingBlockType.Compound) { BuildingBlock = compound }); - _individualSimulation.Model = new OSPSuite.Core.Domain.Model { Root = new Container { organsim } }; + _individualSimulation.AddUsedBuildingBlock(new UsedBuildingBlock("Id", PKSimBuildingBlockType.Compound) {BuildingBlock = compound}); + _individualSimulation.Model = new OSPSuite.Core.Domain.Model {Root = new Container {organsim}}; } protected override void Because()