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

Fixes #1533 add throw if not found #1534

Merged
merged 1 commit into from
Mar 8, 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
21 changes: 12 additions & 9 deletions src/OSPSuite.R/Services/ContainerTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,24 @@ public interface IContainerTask
/// </summary>
/// <param name="simulation">Simulation to use to find the quantity by path</param>
/// <param name="path">Absolute path of the quantity</param>
string BaseUnitNameByPath(IModelCoreSimulation simulation, string path);
/// <param name="throwIfNotFound">Should an error be thrown if the quantity by path is not found?</param>
string BaseUnitNameByPath(IModelCoreSimulation simulation, string path, bool throwIfNotFound);

/// <summary>
/// Returns names of dimension of entities with given path (may contain wildcards)
/// </summary>
/// <param name="simulation">Simulation to use to find the quantity by path</param>
/// <param name="path">Absolute path of the quantity</param>
string DimensionNameByPath(IModelCoreSimulation simulation, string path);
/// <param name="throwIfNotFound">Should an error be thrown if the quantity by path is not found?</param>
string DimensionNameByPath(IModelCoreSimulation simulation, string path, bool throwIfNotFound);

/// <summary>
/// Returns if the start values of entities with given path (may contain wildcards) are defined by an explicit formula
/// </summary>
/// <param name="simulation">Simulation to use to find the quantity by path</param>
/// <param name="path">Absolute path of the quantity</param>
bool IsExplicitFormulaByPath(IModelCoreSimulation simulation, string path);
/// <param name="throwIfNotFound">Should an error be thrown if the quantity by path is not found?</param>
bool IsExplicitFormulaByPath(IModelCoreSimulation simulation, string path, bool throwIfNotFound);

/// <summary>
/// Adds quantities with given path (may contain wildcards) to output selections of the simulation.
Expand Down Expand Up @@ -155,11 +158,11 @@ public IMoleculeAmount[] AllMoleculesMatching(IContainer container, string path)

public string[] AllStateVariableParameterPathsIn(IModelCoreSimulation simulation) => AllStateVariableParameterPathsIn(simulation?.Model?.Root);

public string BaseUnitNameByPath(IModelCoreSimulation simulation, string path) => singleQuantityByPath(simulation, path).BaseUnitName();
public string BaseUnitNameByPath(IModelCoreSimulation simulation, string path, bool throwIfNotFound) => singleQuantityByPath(simulation, path, throwIfNotFound).BaseUnitName();

public string DimensionNameByPath(IModelCoreSimulation simulation, string path) => singleQuantityByPath(simulation, path).DimensionName();
public string DimensionNameByPath(IModelCoreSimulation simulation, string path, bool throwIfNotFound) => singleQuantityByPath(simulation, path, throwIfNotFound).DimensionName();

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

public void AddQuantitiesToSimulationOutputByPath(IModelCoreSimulation simulation, string path) =>
AllQuantitiesMatching(simulation, path).Each(simulation.OutputSelections.AddQuantity);
Expand All @@ -168,7 +171,7 @@ public void SetValueByPath(IModelCoreSimulation simulation, string path, double
{
if (path.Contains(WILD_CARD))
throw new OSPSuiteException(Error.CannotSetValueByPathUsingWildCard(path));

var pathArray = path.ToPathArray();
var quantity = simulation.Model.Root.EntityAt<IQuantity>(pathArray);
if (quantity != null)
Expand All @@ -183,14 +186,14 @@ public void SetValueByPath(IModelCoreSimulation simulation, string path, double
_logger.AddWarning(Error.CouldNotFindQuantityWithPath(path));
}

private IQuantity singleQuantityByPath(IModelCoreSimulation simulation, string path)
private IQuantity singleQuantityByPath(IModelCoreSimulation simulation, string path, bool throwIfNotFound)
{
if (path.Contains(WILD_CARD))
throw new OSPSuiteException(Error.CannotSetValueByPathUsingWildCard(path));

var pathArray = path.ToPathArray();
var quantity = simulation.Model.Root.EntityAt<IQuantity>(pathArray);
if (quantity == null)
if (quantity == null && throwIfNotFound)
throw new OSPSuiteException(Error.CouldNotFindQuantityWithPath(path));

return quantity;
Expand Down
27 changes: 17 additions & 10 deletions tests/OSPSuite.R.Tests/Services/ContainerTaskSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,15 @@ public class When_retrieving_the_is_formula_flag_for_a_given_path : concern_for_
[Observation]
public void should_return_the_expected_entries()
{
The.Action(() => sut.IsExplicitFormulaByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, $"Vol{Constants.WILD_CARD}"))).ShouldThrowAn<OSPSuiteException>();
The.Action(() => sut.IsExplicitFormulaByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, $"Vol{Constants.WILD_CARD}"), throwIfNotFound: true)).ShouldThrowAn<OSPSuiteException>();

The.Action(() => sut.IsExplicitFormulaByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "NOPE"))).ShouldThrowAn<OSPSuiteException>();
The.Action(() => sut.IsExplicitFormulaByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "NOPE"), throwIfNotFound: true)).ShouldThrowAn<OSPSuiteException>();

sut.IsExplicitFormulaByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, Constants.Parameters.VOLUME)).ShouldBeFalse();
sut.IsExplicitFormulaByPath(_simulation, pathFrom(_liver.Name, Constants.Parameters.VOLUME)).ShouldBeTrue();
sut.IsExplicitFormulaByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, Constants.Parameters.VOLUME), throwIfNotFound: true).ShouldBeFalse();
sut.IsExplicitFormulaByPath(_simulation, pathFrom(_liver.Name, Constants.Parameters.VOLUME), throwIfNotFound: true).ShouldBeTrue();

//do not exist
sut.IsExplicitFormulaByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "NOPE"), throwIfNotFound:false).ShouldBeFalse();
}
}

Expand All @@ -339,7 +342,9 @@ public class When_retrieving_the_base_unit_names_for_a_given_path : concern_for_
[Observation]
public void should_return_the_expected_entries()
{
sut.BaseUnitNameByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, Constants.Parameters.VOLUME)).ShouldBeEqualTo(_volumeLiverCell.Dimension.BaseUnit.Name);
sut.BaseUnitNameByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, Constants.Parameters.VOLUME), throwIfNotFound: true).ShouldBeEqualTo(_volumeLiverCell.Dimension.BaseUnit.Name);
sut.BaseUnitNameByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "NOPE"), throwIfNotFound: false).ShouldBeNullOrEmpty();
The.Action(() => sut.BaseUnitNameByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "NOPE"), throwIfNotFound: true)).ShouldThrowAn<OSPSuiteException>();
}
}

Expand All @@ -348,7 +353,9 @@ public class When_retrieving_the_base_dimensions_names_for_a_given_path : concer
[Observation]
public void should_return_the_expected_entries()
{
sut.DimensionNameByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, Constants.Parameters.VOLUME)).ShouldBeEqualTo(_volumeLiverCell.Dimension.Name);
sut.DimensionNameByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, Constants.Parameters.VOLUME), throwIfNotFound: true).ShouldBeEqualTo(_volumeLiverCell.Dimension.Name);
sut.DimensionNameByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "NOPE"), throwIfNotFound: false).ShouldBeNullOrEmpty();
The.Action(() => sut.DimensionNameByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "NOPE"), throwIfNotFound: true)).ShouldThrowAn<OSPSuiteException>();
}
}

Expand All @@ -357,25 +364,25 @@ public class When_setting_a_value_by_path : concern_for_ContainerTask
[Observation]
public void should_throw_an_exception_if_the_path_contains_wild_cards()
{
The.Action(() => sut.SetValueByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, $"Vol{Constants.WILD_CARD}"), 5, true)).ShouldThrowAn<OSPSuiteException>();
The.Action(() => sut.SetValueByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, $"Vol{Constants.WILD_CARD}"), 5, throwIfNotFound: true)).ShouldThrowAn<OSPSuiteException>();
}

[Observation]
public void should_throw_an_exception_if_the_path_does_not_exist_in_the_simulation()
{
The.Action(() => sut.SetValueByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "TOTO"), 5, true)).ShouldThrowAn<OSPSuiteException>();
The.Action(() => sut.SetValueByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "TOTO"), 5, throwIfNotFound: true)).ShouldThrowAn<OSPSuiteException>();
}

[Observation]
public void should_not_throw_an_exception_if_the_path_does_not_exist_in_the_simulation_and_the_throw_flag_is_set_to_false()
{
sut.SetValueByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "TOTO"), 5, false);
sut.SetValueByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, "TOTO"), 5, throwIfNotFound: false);
}

[Observation]
public void should_set_the_value_of_the_parameter_as_expected_otherwise()
{
sut.SetValueByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, _volumeLiverCell.Name), 666, true);
sut.SetValueByPath(_simulation, pathFrom(_liver.Name, INTRACELLULAR, _volumeLiverCell.Name), 666, throwIfNotFound: true);
_volumeLiverCell.Value.ShouldBeEqualTo(666);
}
}
Expand Down