Skip to content

Commit 29d6113

Browse files
authored
Merge pull request #2776 from Open-Systems-Pharmacology/copilot/add-throw-if-not-found-argument
Add throwIfNotFound parameter to AddQuantitiesToSimulationOutputByPath
2 parents db6ece2 + f15d8b8 commit 29d6113

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

src/OSPSuite.R/Services/ContainerTask.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ public interface IContainerTask
7272
/// <summary>
7373
/// Adds quantities with given path (may contain wildcards) to output selections of the simulation.
7474
/// </summary>
75-
void AddQuantitiesToSimulationOutputByPath(IModelCoreSimulation simulation, string path);
75+
/// <param name="simulation">Simulation to use to find the quantity by path</param>
76+
/// <param name="path">Absolute path of the quantity (may contain wildcards)</param>
77+
/// <param name="throwIfNotFound">Should an error be thrown if the quantity by path is not found?</param>
78+
void AddQuantitiesToSimulationOutputByPath(IModelCoreSimulation simulation, string path, bool throwIfNotFound);
7679

7780
/// <summary>
7881
/// Sets the value of the quantity by path
@@ -173,8 +176,21 @@ public MoleculeAmount[] AllMoleculesMatching(IContainer container, string path)
173176

174177
public bool IsExplicitFormulaByPath(IModelCoreSimulation simulation, string path, bool throwIfNotFound) => singleQuantityByPath(simulation, path, throwIfNotFound)?.Formula.IsExplicit() ?? false;
175178

176-
public void AddQuantitiesToSimulationOutputByPath(IModelCoreSimulation simulation, string path) =>
177-
AllQuantitiesMatching(simulation, path).Each(simulation.OutputSelections.AddQuantity);
179+
public void AddQuantitiesToSimulationOutputByPath(IModelCoreSimulation simulation, string path, bool throwIfNotFound)
180+
{
181+
var quantities = AllQuantitiesMatching(simulation, path);
182+
183+
if (quantities.Length != 0)
184+
{
185+
quantities.Each(simulation.OutputSelections.AddQuantity);
186+
return;
187+
}
188+
189+
if (throwIfNotFound)
190+
throw new OSPSuiteException(Error.CouldNotFindQuantityWithPath(path));
191+
192+
_logger.AddWarning(Error.CouldNotFindQuantityWithPath(path));
193+
}
178194

179195
public void SetValueByPath(IModelCoreSimulation simulation, string path, double value, bool throwIfNotFound)
180196
{

tests/OSPSuite.R.Tests/Services/ConcurrentSimulationRunnerSpecs.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ public override void GlobalContext()
522522
_simulation = _simulationPersister.LoadSimulation(HelperForSpecs.DataFile("not_ok_sim.pkml"));
523523

524524
var containerTask = Api.GetContainerTask();
525-
containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|skin_compartment|SC_skin_sublayer|comp10_1|layer1|permeant");
525+
containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|skin_compartment|SC_skin_sublayer|comp10_1|layer1|permeant", throwIfNotFound: false);
526526

527527
_simulationBatch = new ConcurrentRunSimulationBatch
528528
(
@@ -571,9 +571,9 @@ public override void GlobalContext()
571571
_simulationRunner = Api.GetSimulationRunner();
572572

573573
_containerTask = Api.GetContainerTask();
574-
_containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|permeant|Mass_balance_observer");
575-
_containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|permeant|Stratum_corneum_observer");
576-
_containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|permeant|Vehicle_observer");
574+
_containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|permeant|Mass_balance_observer", throwIfNotFound: false);
575+
_containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|permeant|Stratum_corneum_observer", throwIfNotFound: false);
576+
_containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|permeant|Vehicle_observer", throwIfNotFound: false);
577577

578578
_simulationBatch = new ConcurrentRunSimulationBatch
579579
(
@@ -632,9 +632,9 @@ public override void GlobalContext()
632632
_simulationRunner = Api.GetSimulationRunner();
633633

634634
_containerTask = Api.GetContainerTask();
635-
_containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|permeant|Mass_balance_observer");
636-
_containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|permeant|Stratum_corneum_observer");
637-
_containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|permeant|Vehicle_observer");
635+
_containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|permeant|Mass_balance_observer", throwIfNotFound: false);
636+
_containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|permeant|Stratum_corneum_observer", throwIfNotFound: false);
637+
_containerTask.AddQuantitiesToSimulationOutputByPath(_simulation, "DERMAL_APPLICATION_AREA|permeant|Vehicle_observer", throwIfNotFound: false);
638638

639639
_simulationBatch = new ConcurrentRunSimulationBatch
640640
(

tests/OSPSuite.R.Tests/Services/ContainerTaskSpecs.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public void should_return_the_matching_parameters()
144144
{
145145
sut.AllParametersMatching(_organism, pathFrom(Constants.WILD_CARD_RECURSIVE, INTRACELLULAR, VOLUME)).ShouldOnlyContain(_volumeLiverCell, _volumeKidneyCell);
146146
sut.AllParametersMatching(_organism, pathFrom(Constants.WILD_CARD_RECURSIVE, VOLUME)).ShouldOnlyContain(_volumeLiver, _volumeKidney, _volumeKidneyCell, _volumeLiverCell, _volumeOrganism);
147-
sut.AllParametersMatching(_organism, pathFrom(Constants.WILD_CARD_RECURSIVE, _clearance.Name)).ShouldOnlyContain(_clearance);
147+
sut.AllParametersMatching(_organism, pathFrom(Constants.WILD_CARD_RECURSIVE, _clearance.Name)).ShouldOnlyContain(_clearance);
148148
}
149149
}
150150

@@ -283,7 +283,7 @@ protected override void Because()
283283
[Observation]
284284
public void should_return_the_expected_path()
285285
{
286-
var quantities = new IQuantity[] {_volumeLiver, _volumeLiverCell, _clearance, _liverIntracellularMoleculeAmount, _liverRelExp, _volumeLiverCellOtherEntity};
286+
var quantities = new IQuantity[] { _volumeLiver, _volumeLiverCell, _clearance, _liverIntracellularMoleculeAmount, _liverRelExp, _volumeLiverCellOtherEntity };
287287
var expected = quantities.Select(x => x.EntityPath()).ToArray();
288288
_result.ShouldOnlyContain(expected);
289289
}
@@ -301,7 +301,7 @@ protected override void Because()
301301
[Observation]
302302
public void should_return_the_expected_path()
303303
{
304-
var containers = new[] {_kidney, _liver, _liverIntracellular, _kidneyIntracellular, _liverIntracellularSubContainer,};
304+
var containers = new[] { _kidney, _liver, _liverIntracellular, _kidneyIntracellular, _liverIntracellularSubContainer, };
305305
var expected = containers.Select(x => x.EntityPath()).ToArray();
306306
_result.ShouldOnlyContain(expected);
307307
}
@@ -319,7 +319,7 @@ protected override void Because()
319319
[Observation]
320320
public void should_only_return_parameters_with_a_RHS_formula_defined()
321321
{
322-
var parameters = new[] {_paramWithRHS};
322+
var parameters = new[] { _paramWithRHS };
323323
var expected = parameters.Select(x => x.EntityPath()).ToArray();
324324
_result.ShouldOnlyContain(expected);
325325
}
@@ -419,4 +419,26 @@ public void should_get_the_value_of_the_parameter_as_expected_otherwise()
419419
sut.GetValueByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, _volumeLiverCell.Name), throwIfNotFound: true).ShouldBeEqualTo(666);
420420
}
421421
}
422+
423+
public class When_adding_quantities_to_simulation_output_by_path : concern_for_ContainerTask
424+
{
425+
[Observation]
426+
public void should_throw_an_exception_if_the_path_does_not_exist_and_throw_flag_is_true()
427+
{
428+
The.Action(() => sut.AddQuantitiesToSimulationOutputByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "TOTO"), throwIfNotFound: true)).ShouldThrowAn<OSPSuiteException>();
429+
}
430+
431+
[Observation]
432+
public void should_not_throw_an_exception_if_the_path_does_not_exist_and_the_throw_flag_is_set_to_false()
433+
{
434+
sut.AddQuantitiesToSimulationOutputByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "TOTO"), throwIfNotFound: false);
435+
}
436+
437+
[Observation]
438+
public void should_add_quantities_to_output_selections_when_path_is_found()
439+
{
440+
sut.AddQuantitiesToSimulationOutputByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, VOLUME), throwIfNotFound: true);
441+
A.CallTo(() => _simulation.OutputSelections.AddQuantity(A<IQuantity>._)).MustHaveHappened(1, Times.Exactly);
442+
}
443+
}
422444
}

0 commit comments

Comments
 (0)