Skip to content

Commit

Permalink
Fixes #2263 (#2264)
Browse files Browse the repository at this point in the history
  • Loading branch information
msevestre committed Jun 18, 2024
1 parent 6f347e7 commit fc66a8a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 52 deletions.
5 changes: 0 additions & 5 deletions src/OSPSuite.Assets/UIConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1590,11 +1590,6 @@ public static string UndefinedFormulaInHelpParameter(string parameterName, strin
return $"Cannot add help parameter '{parameterName}' with an undefined formula (null) in calculation method '{calculationMethod}' for category '{category}'";
}

public static string TwoDifferentFormulaForSameParameter(string parameter, string parameterPath)
{
return $"Formula in parameter '{parameter}' with path '{parameterPath}' was described inconsistently by more than one calculation.";
}

public static string HelpParameterAlreadyDefinedWithAnotherFormula(string calculationMethod, string parameterPath)
{
return $"There is another parameter defined at '{calculationMethod}' with another formula. Help parameter for calculation method {parameterPath} cannot be created";
Expand Down
58 changes: 11 additions & 47 deletions src/OSPSuite.Core/Domain/Services/CalculationMethodTask.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using OSPSuite.Assets;
using OSPSuite.Core.Domain.Builder;
using OSPSuite.Core.Domain.Descriptors;
using OSPSuite.Core.Domain.Formulas;
using OSPSuite.Core.Domain.Mappers;
using OSPSuite.Core.Extensions;
using OSPSuite.Utility.Exceptions;
using OSPSuite.Utility.Extensions;

namespace OSPSuite.Core.Domain.Services
Expand Down Expand Up @@ -48,7 +46,7 @@ public void MergeCalculationMethodInModel(ModelConfiguration modelConfiguration)
{
try
{
(_model, _simulationBuilder, _replacementContext) = modelConfiguration;
(_model, _simulationBuilder, _replacementContext) = modelConfiguration;
_simulationConfiguration = modelConfiguration.SimulationConfiguration;
_allContainers = _model.Root.GetAllContainersAndSelf<IContainer>().ToEntityDescriptorMapList();
_allBlackBoxParameters = _model.Root.GetAllChildren<IParameter>().Where(p => p.Formula.IsBlackBox()).ToList();
Expand Down Expand Up @@ -80,16 +78,14 @@ private void addHelpParametersFor(CoreCalculationMethod calculationMethod, IList
{
foreach (var container in allMoleculeContainersFor(containerDescriptor, molecule))
{
//make sure we remove the parameter if it exists already
var existingParameter = container.Parameter(helpParameter.Name);
//does not exist yet
if (existingParameter == null)
{
var parameter = _parameterMapper.MapFrom(helpParameter, _simulationBuilder);
container.Add(parameter);
replaceKeyWordsIn(parameter, molecule.Name);
}
else if (!formulasAreTheSameForParameter(existingParameter, helpParameter.Formula, molecule.Name))
throw new OSPSuiteException(Error.HelpParameterAlreadyDefinedWithAnotherFormula(calculationMethod.Name, _objectPathFactory.CreateAbsoluteObjectPath(helpParameter).ToString()));
if (existingParameter != null)
container.RemoveChild(existingParameter);

var parameter = _parameterMapper.MapFrom(helpParameter, _simulationBuilder);
container.Add(parameter);
replaceKeyWordsIn(parameter, molecule.Name);
}
}
}
Expand All @@ -108,17 +104,8 @@ private void createFormulaForBlackBoxParameters(CoreCalculationMethod calculatio
if (parameterIsNotBlackBoxParameter(parameter))
continue;

//parameter formula was not set yet
if (parameter.Formula.IsBlackBox())
{
parameter.Formula = _formulaMapper.MapFrom(formula, _simulationBuilder);
replaceKeyWordsIn(parameter, molecule.Name);
}
else
{
if (!formulasAreTheSameForParameter(parameter, formula, molecule.Name))
throw new OSPSuiteException(Error.TwoDifferentFormulaForSameParameter(parameter.Name, _objectPathFactory.CreateAbsoluteObjectPath(parameter).ToPathString()));
}
parameter.Formula = _formulaMapper.MapFrom(formula, _simulationBuilder);
replaceKeyWordsIn(parameter, molecule.Name);
}
}
}
Expand All @@ -144,30 +131,7 @@ private static Neighborhood neighborhoodAncestorFor(IEntity entity)
return neighborhoodAncestorFor(entity.ParentContainer);
}

private bool formulasAreTheSameForParameter(IParameter originalParameter, IFormula calculationMethodFormula, string moleculeName)
{
var previousFormula = originalParameter.Formula;
//needs to use the parameter in order to keep the hierarchy. Hence we set the formula in the parameter
originalParameter.Formula = _formulaMapper.MapFrom(calculationMethodFormula, _simulationBuilder);

//check if the formula set are the same. it is necessary to replace keywords before doing that
replaceKeyWordsIn(originalParameter, moleculeName);

try
{
return _formulaTask.FormulasAreTheSame(originalParameter.Formula, previousFormula);
}
finally
{
//reset the origianl formula in any case
originalParameter.Formula = previousFormula;
}
}

private bool parameterIsNotBlackBoxParameter(IParameter parameter)
{
return !_allBlackBoxParameters.Contains(parameter);
}
private bool parameterIsNotBlackBoxParameter(IParameter parameter) => !_allBlackBoxParameters.Contains(parameter);

private IEnumerable<MoleculeBuilder> allMoleculesUsing(CoreCalculationMethod calculationMethod, IReadOnlyCollection<MoleculeBuilder> molecules)
{
Expand Down

0 comments on commit fc66a8a

Please sign in to comment.