Skip to content

Commit

Permalink
Fixes #2297 formulas are resovled before applying pv
Browse files Browse the repository at this point in the history
  • Loading branch information
msevestre committed Aug 8, 2024
1 parent e160e02 commit 938f283
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
28 changes: 25 additions & 3 deletions src/OSPSuite.Core/Domain/Services/ModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal abstract class ModelValidator : IModelValidator, IVisitor
private readonly IObjectPathFactory _objectPathFactory;
private readonly IEnumerable<string> _keywords;
private SimulationBuilder _simulationBuilder;
private ValidationResult _result;
protected ValidationResult _result;

protected ModelValidator(IObjectTypeResolver objectTypeResolver, IObjectPathFactory objectPathFactory)
{
Expand Down Expand Up @@ -65,7 +65,7 @@ protected void CheckParameter(IParameter parameterToCheck, ResolveErrorBehavior
protected void CheckFormulaIn(IUsingFormula entity, IFormula formulaToCheck, ResolveErrorBehavior resolveErrorBehavior)
{
var entityAbsolutePath = _objectPathFactory.CreateAbsoluteObjectPath(entity).ToPathString();
var builder = _simulationBuilder.BuilderFor(entity);
var builder = _simulationBuilder?.BuilderFor(entity);
var objectWithError = builder ?? entity;
void checkPathInEntity(ObjectPath objectPath) => CheckPath(entity, objectPath, resolveErrorBehavior);

Expand All @@ -91,7 +91,7 @@ protected void CheckFormulaIn(IUsingFormula entity, IFormula formulaToCheck, Res

protected void CheckPath(IUsingFormula entity, ObjectPath objectPathToCheck, ResolveErrorBehavior resolveErrorBehavior)
{
var builder = _simulationBuilder.BuilderFor(entity);
var builder = _simulationBuilder?.BuilderFor(entity);
var objectWithError = builder ?? entity;
var entityAbsolutePath = _objectPathFactory.CreateAbsoluteObjectPath(entity).ToString();
var entityType = _objectTypeResolver.TypeFor(entity);
Expand Down Expand Up @@ -150,6 +150,28 @@ public ValidationResult Validate(ModelConfiguration modelConfiguration)
}
}

internal class ValidatorForForFormula : ModelValidator
{
public ValidatorForForFormula(IObjectTypeResolver objectTypeResolver, IObjectPathFactory objectPathFactory)
: base(objectTypeResolver, objectPathFactory)
{
}

public bool IsFormulaValid(IUsingFormula usingFormulaToCheck)
{
try
{
_result = new ValidationResult();
CheckReferences(usingFormulaToCheck);
return _result.ValidationState == ValidationState.Valid;
}
finally
{
_result = null;
}
}
}

internal class ValidatorForQuantities : ModelValidator, IVisitor<IQuantity>, IVisitor<IParameter>
{
public ValidatorForQuantities(IObjectTypeResolver objectTypeResolver, IObjectPathFactory objectPathFactory)
Expand Down
26 changes: 19 additions & 7 deletions src/OSPSuite.Core/Domain/Services/QuantityValuesUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ internal class QuantityValuesUpdater : IQuantityValuesUpdater
private readonly IFormulaFactory _formulaFactory;
private readonly IConcentrationBasedFormulaUpdater _concentrationBasedFormulaUpdater;
private readonly IParameterValueToParameterMapper _parameterValueToParameterMapper;
private readonly ValidatorForForFormula _formulaValidator;

public QuantityValuesUpdater(
IKeywordReplacerTask keywordReplacerTask,
ICloneManagerForModel cloneManagerForModel,
IFormulaFactory formulaFactory,
IConcentrationBasedFormulaUpdater concentrationBasedFormulaUpdater,
IParameterValueToParameterMapper parameterValueToParameterMapper)
IParameterValueToParameterMapper parameterValueToParameterMapper,
ValidatorForForFormula formulaValidator)
{
_keywordReplacerTask = keywordReplacerTask;
_cloneManagerForModel = cloneManagerForModel;
_formulaFactory = formulaFactory;
_concentrationBasedFormulaUpdater = concentrationBasedFormulaUpdater;
_parameterValueToParameterMapper = parameterValueToParameterMapper;
_formulaValidator = formulaValidator;
}

public ValidationResult UpdateQuantitiesValues(ModelConfiguration modelConfiguration)
Expand Down Expand Up @@ -138,14 +141,23 @@ private Action<ParameterValue> addOrUpdateParameterFromParameterValue(ValueUpdat
}

//If the value is defined, this will be used instead of the formula (even if set previously)
if (parameterValue.Value.IsValid())
if (!parameterValue.Value.IsValid())
return;

var actualParameterValue = parameterValue.Value.Value;
if (parameter.Formula is ConstantFormula constantFormula)
{
var actualParameterValue = parameterValue.Value.Value;
if (parameter.Formula is ConstantFormula constantFormula)
constantFormula.Value = actualParameterValue;
else
parameter.Value = actualParameterValue;
constantFormula.Value = actualParameterValue;
return;
}

//now we have a non constant formula. Let's try to see if the reference can be resolved. If yes, we will simply set the value of the parameter
//Otherwise, we will create a new constant formula with the value

if (_formulaValidator.IsFormulaValid(parameter))
parameter.Value = actualParameterValue;
else
parameter.Formula = _formulaFactory.ConstantFormula(actualParameterValue, parameter.Dimension);
};

private void updateMoleculeAmountFromInitialConditions(ModelConfiguration modelConfiguration)
Expand Down

0 comments on commit 938f283

Please sign in to comment.