Skip to content
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
22 changes: 17 additions & 5 deletions src/OSPSuite.Core/Domain/Services/ParameterValuesCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,26 @@ private static ExpressionParameter expressionSourceFor(List<ExpressionParameter>
{
var formulaSource = pathMatchedExpressionParameterFor(nameMatchedExpressionParameters, formulaTarget);

if (formulaSource == null && hasCompartment(formulaTarget))
formulaSource = compartmentMatchedExpressionParameterFor(formulaTarget, nameMatchedExpressionParameters);
if (formulaSource != null || !hasCompartment(formulaTarget))
return formulaSource;

var potentialSources = compartmentMatchedExpressionParametersFor(formulaTarget, nameMatchedExpressionParameters);
return !potentialSources.Any() ? null : mostFrequentFormulaExpression(potentialSources);
}

return formulaSource;
private static ExpressionParameter mostFrequentFormulaExpression(IReadOnlyList<ExpressionParameter> potentialSources)
{
// Group the potential sources by formula name, order the groups by the count, flatten the groups, take the first expression parameter
return potentialSources.GroupBy(formulaGroupingName).OrderByDescending(x => x.Count()).SelectMany(x => x).First();
}

private static ExpressionParameter compartmentMatchedExpressionParameterFor(ParameterValue formulaTarget, List<ExpressionParameter> nameMatchedExpressionParameters) =>
nameMatchedExpressionParameters.Where(hasCompartment).FirstOrDefault(x => Equals(compartmentFor(x), compartmentFor(formulaTarget)));
/// <summary>
/// Returns a name for the <paramref name="expressionParameter"/> formula. Expression parameters with value return the value as string
/// </summary>
private static string formulaGroupingName(ExpressionParameter expressionParameter) => expressionParameter.Formula == null ? expressionParameter.Value.ToString() : expressionParameter.Formula.Name;

private static IReadOnlyList<ExpressionParameter> compartmentMatchedExpressionParametersFor(ParameterValue formulaTarget, List<ExpressionParameter> nameMatchedExpressionParameters) =>
nameMatchedExpressionParameters.Where(x => hasCompartment(x) && Equals(compartmentFor(x), compartmentFor(formulaTarget))).ToList();

private static string compartmentFor(ParameterValue formulaTarget) => formulaTarget.Path[compartmentIndex(formulaTarget)];

Expand Down
38 changes: 35 additions & 3 deletions tests/OSPSuite.Core.Tests/Domain/ParameterValuesCreatorSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,32 @@ protected override void Context()

_expressionProfile = new ExpressionProfileBuildingBlock
{
new ExpressionParameter().WithName(Constants.Parameters.REL_EXP),
new ExpressionParameter().WithName("thalf")
}.WithName("protein|something|something");


var expressionParameter = new ExpressionParameter().WithName(Constants.Parameters.REL_EXP);
expressionParameter.ContainerPath = new ObjectPath("organ", "Top", "compartment", "protein");
expressionParameter.Value = 5.0;

_expressionProfile.Add(expressionParameter);

expressionParameter = new ExpressionParameter().WithName(Constants.Parameters.REL_EXP);
expressionParameter.ContainerPath = new ObjectPath("organ", "org", "compartment", "protein");
expressionParameter.Value = 4.0;

_expressionProfile.Add(expressionParameter);

expressionParameter = new ExpressionParameter().WithName(Constants.Parameters.REL_EXP);
expressionParameter.ContainerPath = new ObjectPath("organ", "org2", "compartment", "protein");
expressionParameter.Value = 4.0;

_expressionProfile.Add(expressionParameter);

A.CallTo(() => _currentProject.All<ExpressionProfileBuildingBlock>()).Returns(new List<ExpressionProfileBuildingBlock> { _expressionProfile });
_protein = new MoleculeBuilder().WithName("protein");
_protein.QuantityType = QuantityType.Transporter;
var parameter = new Parameter().WithName(Constants.Parameters.REL_EXP);
var parameter = new Parameter().WithName(Constants.Parameters.REL_EXP).WithFormula(new ExplicitFormula("99"));
_protein.AddParameter(parameter);
var globalParameter = new Parameter().WithName("thalf");
globalParameter.BuildMode = ParameterBuildMode.Global;
Expand All @@ -178,10 +197,23 @@ protected override void Because()
_parameterValues = sut.CreateExpressionFrom(_organContainer, _molecules);
}

[Observation]
public void the_parameter_values_with_compartment_match_should_use_the_best_match_expression_value()
{
_parameterValues.Single(x => x.Path.ToString().Equals($"Top|organ|compartment|protein|{Constants.Parameters.REL_EXP}")).Value.ShouldBeEqualTo(4.0);
}

[Observation]
public void the_parameter_values_without_compartment_match_should_have_value_from_molecule_parameter()
{
var formula = _parameterValues.Single(x => x.Path.ToString().Equals($"Top|organ|protein|{Constants.Parameters.REL_EXP}")).Formula as ExplicitFormula;
formula.ToString().ShouldBeEqualTo("99");
}

[Observation]
public void the_parameter_values_should_include_local_expression_parameters_for_all_containers()
{
_parameterValues.Select(x => x.Path.ToString()).ShouldOnlyContain($"Top|organ|compartment|protein|{Constants.Parameters.REL_EXP}", $"Top|organ|compartment|protein|{Constants.Parameters.REL_EXP}");
_parameterValues.Select(x => x.Path.ToString()).ShouldOnlyContain($"Top|organ|compartment|protein|{Constants.Parameters.REL_EXP}", $"Top|organ|protein|{Constants.Parameters.REL_EXP}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ protected override void Context()
}

private ImportedDataSet _importedDataSet;
private DataSetToDataRepositoryMappingResult _result;

protected override void Because()
{
Expand Down
Loading