Skip to content

Commit

Permalink
Fixes #2569 use individual building block and module from core
Browse files Browse the repository at this point in the history
  • Loading branch information
msevestre committed Mar 22, 2023
1 parent 6386bef commit 0a6318a
Show file tree
Hide file tree
Showing 27 changed files with 235 additions and 259 deletions.
4 changes: 2 additions & 2 deletions src/PKSim.Assets.Images/PKSim.Assets.Images.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OSPSuite.Assets" Version="12.0.144" />
<PackageReference Include="OSPSuite.Assets.Images" Version="12.0.144" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.145" />
<PackageReference Include="OSPSuite.Assets.Images" Version="12.0.145" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/PKSim.Assets/PKSim.Assets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OSPSuite.Assets" Version="12.0.144" />
<PackageReference Include="OSPSuite.Assets.Images" Version="12.0.144" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.145" />
<PackageReference Include="OSPSuite.Assets.Images" Version="12.0.145" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/PKSim.BatchTool/PKSim.BatchTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OSPSuite.Core" Version="12.0.144" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.144" />
<PackageReference Include="OSPSuite.Core" Version="12.0.145" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.145" />
<PackageReference Include="OSPSuite.DevExpress" Version="21.2.3" />
<PackageReference Include="OSPSuite.FuncParser" Version="4.0.0.54" GeneratePathProperty="true" />
<PackageReference Include="OSPSuite.SimModel" Version="4.0.0.53" GeneratePathProperty="true" />
Expand Down
4 changes: 2 additions & 2 deletions src/PKSim.CLI.Core/PKSim.CLI.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OSPSuite.Assets" Version="12.0.144" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.145" />
<PackageReference Include="OSPSuite.Utility" Version="4.0.0.4" />
<PackageReference Include="OSPSuite.Core" Version="12.0.144" />
<PackageReference Include="OSPSuite.Core" Version="12.0.145" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/PKSim.CLI/PKSim.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.0" />
<PackageReference Include="OSPSuite.Core" Version="12.0.144" />
<PackageReference Include="OSPSuite.Presentation" Version="12.0.144" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.144" />
<PackageReference Include="OSPSuite.Core" Version="12.0.145" />
<PackageReference Include="OSPSuite.Presentation" Version="12.0.145" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.145" />
<PackageReference Include="OSPSuite.Utility" Version="4.0.0.4" />
<PackageReference Include="OSPSuite.FuncParser" Version="4.0.0.54" GeneratePathProperty="true" />
<PackageReference Include="OSPSuite.SimModel" Version="4.0.0.53" GeneratePathProperty="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
using OSPSuite.Core;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Builder;
using OSPSuite.Core.Domain.Formulas;
using OSPSuite.Core.Domain.Services;
using PKSim.Assets;
using PKSim.Core.Model;
using PKSim.Core.Services;
using static PKSim.Core.CoreConstants.CalculationMethod;
using IFormulaFactory = PKSim.Core.Model.IFormulaFactory;
using ILazyLoadTask = OSPSuite.Core.Domain.Services.ILazyLoadTask;

namespace PKSim.Core.Mappers
Expand All @@ -16,11 +18,17 @@ public interface IExpressionProfileToExpressionProfileBuildingBlockMapper : IPat

public class ExpressionProfileToExpressionProfileBuildingBlockMapper : PathAndValueBuildingBlockMapper<ExpressionProfile, ExpressionProfileBuildingBlock, ExpressionParameter>, IExpressionProfileToExpressionProfileBuildingBlockMapper
{
public ExpressionProfileToExpressionProfileBuildingBlockMapper(IObjectBaseFactory objectBaseFactory, IEntityPathResolver entityPathResolver, IApplicationConfiguration applicationConfiguration, ILazyLoadTask lazyLoadTask, ICloner cloner) :
base(objectBaseFactory, entityPathResolver, applicationConfiguration, lazyLoadTask, cloner)
public ExpressionProfileToExpressionProfileBuildingBlockMapper(IObjectBaseFactory objectBaseFactory, IEntityPathResolver entityPathResolver, IApplicationConfiguration applicationConfiguration, ILazyLoadTask lazyLoadTask, IFormulaFactory formulaFactory) :
base(objectBaseFactory, entityPathResolver, applicationConfiguration, lazyLoadTask, formulaFactory)
{
}

protected override IFormula TemplateFormulaFor(IParameter parameter, IFormulaCache formulaCache, ExpressionProfile expressionProfile)
{
//for expression profile, all formula are in the calculation method EXPRESSION_PARAMETERS
return _formulaFactory.RateFor(new RateKey(EXPRESSION_PARAMETERS, parameter.Formula.Name), formulaCache);
}

protected override IReadOnlyList<IParameter> AllParametersFor(ExpressionProfile expressionProfile)
{
return expressionProfile.GetAllChildren<IParameter>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
using OSPSuite.Core;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Builder;
using OSPSuite.Core.Domain.Formulas;
using OSPSuite.Core.Domain.Services;
using OSPSuite.Utility.Extensions;
using PKSim.Assets;
using PKSim.Core.Model;
using PKSim.Core.Repositories;
using PKSim.Core.Services;
using IFormulaFactory = PKSim.Core.Model.IFormulaFactory;
using ILazyLoadTask = OSPSuite.Core.Domain.Services.ILazyLoadTask;

namespace PKSim.Core.Mappers
Expand All @@ -21,12 +23,39 @@ public class IndividualToIndividualBuildingBlockMapper : PathAndValueBuildingBlo
{
private readonly IRepresentationInfoRepository _representationInfoRepository;
private readonly ICalculationMethodCategoryRepository _calculationMethodCategoryRepository;
private readonly IParameterQuery _parameterQuery;

public IndividualToIndividualBuildingBlockMapper(IObjectBaseFactory objectBaseFactory, IEntityPathResolver entityPathResolver, IApplicationConfiguration applicationConfiguration,
ILazyLoadTask lazyLoadTask, IRepresentationInfoRepository representationInfoRepository, ICalculationMethodCategoryRepository calculationMethodCategoryRepository, ICloner cloner) : base(objectBaseFactory, entityPathResolver, applicationConfiguration, lazyLoadTask, cloner)
public IndividualToIndividualBuildingBlockMapper(IObjectBaseFactory objectBaseFactory,
IEntityPathResolver entityPathResolver,
IApplicationConfiguration applicationConfiguration,
ILazyLoadTask lazyLoadTask,
IRepresentationInfoRepository representationInfoRepository,
ICalculationMethodCategoryRepository calculationMethodCategoryRepository,
IFormulaFactory formulaFactory,
IParameterQuery parameterQuery) : base(objectBaseFactory, entityPathResolver, applicationConfiguration, lazyLoadTask, formulaFactory)
{
_representationInfoRepository = representationInfoRepository;
_calculationMethodCategoryRepository = calculationMethodCategoryRepository;
_parameterQuery = parameterQuery;
}

protected override IFormula TemplateFormulaFor(IParameter parameter, IFormulaCache formulaCache, Individual individual)
{
bool isMetaDataForParameter(ParameterMetaData p) => p.BuildingBlockType == PKSimBuildingBlockType.Individual && string.Equals(p.ParameterName, parameter.Name);


//for individual, the CM to use depends on the CM available in the origin data as well as the container where the parameter resides.
var calculationMethods = individual.OriginData.AllCalculationMethods().AllNames();

var parameterRate = _parameterQuery.ParameterRatesFor(parameter.ParentContainer, calculationMethods, isMetaDataForParameter).ToList();

//this is not possible?
if (parameterRate.Count != 1)
return null;

var cloneFormula = _formulaFactory.RateFor(parameterRate[0], formulaCache);
cloneFormula.ObjectPaths.Each(x => x.Remove(Constants.ROOT));
return cloneFormula;
}

protected override IReadOnlyList<IParameter> AllParametersFor(Individual individual)
Expand Down
51 changes: 25 additions & 26 deletions src/PKSim.Core/Mappers/PathAndValueBuildingBlockMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
using OSPSuite.Core.Domain.Formulas;
using OSPSuite.Core.Domain.Services;
using OSPSuite.Utility;
using OSPSuite.Utility.Collections;
using OSPSuite.Utility.Extensions;
using PKSim.Core.Model;
using PKSim.Core.Services;
using ILazyLoadTask = OSPSuite.Core.Domain.Services.ILazyLoadTask;
using IFormulaFactory = PKSim.Core.Model.IFormulaFactory;

namespace PKSim.Core.Mappers
{
Expand All @@ -21,8 +19,8 @@ public interface IPathAndValueBuildingBlockMapper<in TPKSimBuildingBlock, out TB
public interface IPathAndValueBuildingBlockMapper<in TPKSimBuildingBlock, out TBuildingBlock, out TBuilder> : IPathAndValueBuildingBlockMapper<TPKSimBuildingBlock, TBuildingBlock>
{
/// <summary>
/// Map the parameter to the underlying builder parameter.
/// Note that formula or value will not be set. Only common parameter properties
/// Map the parameter to the underlying builder parameter.
/// Note that formula or value will not be set. Only common parameter properties
/// </summary>
TBuilder MapParameter(IParameter parameter);
}
Expand All @@ -35,24 +33,24 @@ public abstract class PathAndValueBuildingBlockMapper<TPKSimBuildingBlock, TBuil
protected IEntityPathResolver _entityPathResolver;
protected IApplicationConfiguration _applicationConfiguration;
private readonly ILazyLoadTask _lazyLoadTask;
private readonly ICloner _cloner;

//Cache used to store all formula that can be cached. This is required to avoid having the same formula defined multiple times in the building block
//note that a clone of the original formula is added to the cache so that it can be modified if required
private readonly Cache<string, IFormula> _formulaCache = new Cache<string, IFormula>(x => x.Name);
private readonly IFormulaCache _formulaCache = new BuildingBlockFormulaCache();
protected readonly IFormulaFactory _formulaFactory;

protected PathAndValueBuildingBlockMapper(
IObjectBaseFactory objectBaseFactory,
IEntityPathResolver entityPathResolver,
IApplicationConfiguration applicationConfiguration,
ILazyLoadTask lazyLoadTask,
ICloner cloner)
IFormulaFactory formulaFactory)
{
_objectBaseFactory = objectBaseFactory;
_entityPathResolver = entityPathResolver;
_applicationConfiguration = applicationConfiguration;
_lazyLoadTask = lazyLoadTask;
_cloner = cloner;
_formulaFactory = formulaFactory;
}

protected TBuildingBlock CreateBaseObject(TPKSimBuildingBlock pkSimBuildingBlock)
Expand All @@ -74,15 +72,16 @@ public virtual TBuilder MapParameter(IParameter parameter)
return builderParameter;
}

private TBuilder mapBuilderParameter(IParameter parameter)
private TBuilder mapBuilderParameter(IParameter parameter, TPKSimBuildingBlock pkSimBuildingBlock)
{
var builderParameter = MapParameter(parameter);

// Add the formula to the building block formula cache if the formula can be cached
var parameterValue = getParameterValue(parameter);
var valueChanged = parameter.ValueDiffersFromDefault();
var formula = parameter.Formula;

switch (parameter.Formula)
switch (formula)
{
case ConstantFormula _:
builderParameter.Value = parameterValue;
Expand All @@ -98,13 +97,10 @@ private TBuilder mapBuilderParameter(IParameter parameter)
return builderParameter;

default:
if (isFormulaCachable(parameter))
if (formula.IsCachable())
{
if (!_formulaCache.Contains(parameter.Formula.Name))
_formulaCache.Add(cloneFormulaForExport(parameter));

//Set the formula no matter what
builderParameter.Formula = _formulaCache[parameter.Formula.Name];
var templateFormula = retrieveTemplateFormulaFromCache(parameter, pkSimBuildingBlock);
builderParameter.Formula = templateFormula;
}

// Only set the value of the parameter using a formula if it was indeed set
Expand All @@ -115,17 +111,20 @@ private TBuilder mapBuilderParameter(IParameter parameter)
}
}

private IFormula cloneFormulaForExport(IParameter parameter)
private IFormula retrieveTemplateFormulaFromCache(IParameter parameter, TPKSimBuildingBlock pkSimBuildingBlock)
{
var cloneFormula = _cloner.Clone(parameter.Formula);
cloneFormula.ObjectPaths.Each(x => x.Remove(Constants.ROOT));
return cloneFormula;
var formulaName = parameter.Formula.Name;
if (_formulaCache.Contains(formulaName))
return _formulaCache[formulaName];

//This will add the formula top the cache
var templateFormula = TemplateFormulaFor(parameter, _formulaCache, pkSimBuildingBlock);
//We need to remove the ROOT keyword when exporting to PKML structure
templateFormula?.ObjectPaths.Each(x => x.Remove(Constants.ROOT));
return templateFormula;
}

private static bool isFormulaCachable(IParameter parameter)
{
return parameter.Formula != null && parameter.Formula.IsCachable();
}
protected abstract IFormula TemplateFormulaFor(IParameter parameter, IFormulaCache formulaCache, TPKSimBuildingBlock pkSimBuildingBlock);

private static double getParameterValue(IParameter parameter)
{
Expand All @@ -134,7 +133,7 @@ private static double getParameterValue(IParameter parameter)

protected void MapAllParameters(TPKSimBuildingBlock sourcePKSimBuildingBlock, TBuildingBlock buildingBlock)
{
var allBuilderParameters = AllParametersFor(sourcePKSimBuildingBlock).Select(mapBuilderParameter);
var allBuilderParameters = AllParametersFor(sourcePKSimBuildingBlock).Select(x=>mapBuilderParameter(x, sourcePKSimBuildingBlock));
allBuilderParameters.Where(x => x != null).Each(buildingBlock.Add);

//Formula cache already contains a clone of all formula. We can add as is
Expand Down
15 changes: 13 additions & 2 deletions src/PKSim.Core/Model/Individual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,24 @@ public Individual() : base(PKSimBuildingBlockType.Individual)
/// <summary>
/// All defined molecules defined in the individual
/// </summary>
public virtual IEnumerable<IndividualMolecule> AllDefinedMolecules() => AllMolecules().Where(x => !x.IsUndefinedMolecule());
public virtual IEnumerable<IndividualMolecule> AllDefinedMolecules() => AllMolecules<IndividualMolecule>(x => !x.IsUndefinedMolecule());

/// <summary>
/// All defined molecules defined in the individual
/// </summary>
public virtual IEnumerable<IndividualMolecule> AllUndefinedMolecules() => AllMolecules<IndividualMolecule>(x => x.IsUndefinedMolecule());

/// <summary>
/// All protein of type <typeparamref name="TMolecule" /> in the individual
/// </summary>
/// <typeparam name="TMolecule"> Type of molecule to be retrieved </typeparam>
public virtual IEnumerable<TMolecule> AllMolecules<TMolecule>() where TMolecule : IndividualMolecule => AllMolecules<TMolecule>(x => true);

/// <summary>
/// All protein of type <typeparamref name="TMolecule" /> in the individual
/// </summary>
/// <typeparam name="TMolecule"> Type of molecule to be retrieved </typeparam>
public virtual IEnumerable<TMolecule> AllMolecules<TMolecule>() where TMolecule : IndividualMolecule => GetChildren<TMolecule>();
public virtual IEnumerable<TMolecule> AllMolecules<TMolecule>(Func<TMolecule, bool> predicate) where TMolecule : IndividualMolecule => GetChildren(predicate);

public ExpressionProfile ExpressionProfileFor(IndividualMolecule molecule) => AllExpressionProfiles().Find(x => string.Equals(x.MoleculeName, molecule.Name));

Expand Down
8 changes: 4 additions & 4 deletions src/PKSim.Core/PKSim.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="OSPSuite.Utility" Version="4.0.0.4" />
<PackageReference Include="OSPSuite.Core" Version="12.0.144" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.144" />
<PackageReference Include="OSPSuite.Assets.Images" Version="12.0.144" />
<PackageReference Include="OSPSuite.Infrastructure.Import" Version="12.0.144" />
<PackageReference Include="OSPSuite.Core" Version="12.0.145" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.145" />
<PackageReference Include="OSPSuite.Assets.Images" Version="12.0.145" />
<PackageReference Include="OSPSuite.Infrastructure.Import" Version="12.0.145" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
</ItemGroup>

Expand Down
36 changes: 25 additions & 11 deletions src/PKSim.Core/Services/PKSimParameterStartValuesCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,28 @@ public PKSimParameterStartValuesCreator(

public ParameterStartValuesBuildingBlock CreateFor(SimulationConfiguration simulationConfiguration, Simulation simulation)
{
//default default parameter start values matrix
_defaultStartValues = _objectBaseFactory.Create<ParameterStartValuesBuildingBlock>();
updateSimulationParameters(simulation);
return _defaultStartValues.WithName(simulation.Name);
try
{
//default default parameter start values matrix
_defaultStartValues = _objectBaseFactory.Create<ParameterStartValuesBuildingBlock>();
var individual = simulation.Individual;

//set the relative expression values for each molecule undefined molecule of the individual (other will be done in expression profile)
individual.AllUndefinedMolecules().Each(molecule => updateMoleculeParametersValues(molecule, individual));

updateSimulationParameters(simulation);
return _defaultStartValues.WithName(simulation.Name);
}
finally
{
_defaultStartValues = null;
}
}

private void updateMoleculeParametersValues(IndividualMolecule molecule, Individual individual)
{
var allMoleculeParameters = individual.AllMoleculeParametersFor(molecule);
allMoleculeParameters.Each(p => trySetValue(p));
}

private void updateSimulationParameters(Simulation simulation)
Expand All @@ -43,14 +61,10 @@ private void updateSimulationParameters(Simulation simulation)
if (simulation.Model == null)
return;

//TODO: Ensure that the formula will not become a constant after clone
//THis was done with psv.OverrideFormulaWithValue = false;
var allSimulationParameters = simulation.Model.Root.GetAllChildren<IParameter>(isChangedSimulationParameter);
allSimulationParameters.Each(p =>
{
var psv = trySetValue(p);
//Ensure that the formula will not become a constant after clone
//TODO
// psv.OverrideFormulaWithValue = false;
});
allSimulationParameters.Each(p => trySetValue(p));
}

private bool isChangedSimulationParameter(IParameter parameter)
Expand Down
Loading

0 comments on commit 0a6318a

Please sign in to comment.