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 #2480 individual serialization #2494

Merged
merged 8 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
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.77" />
<PackageReference Include="OSPSuite.Assets.Images" Version="12.0.77" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.84" />
<PackageReference Include="OSPSuite.Assets.Images" Version="12.0.84" />
</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.77" />
<PackageReference Include="OSPSuite.Assets.Images" Version="12.0.77" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.84" />
<PackageReference Include="OSPSuite.Assets.Images" Version="12.0.84" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/PKSim.Assets/PKSimConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,7 @@ public static class UI
public static readonly string Yes = "Yes";
public static readonly string ExportObservedDataToPkml = "Export observed data to pkml";
public static readonly string ExportExpressionProfileToPkml = "Export expression profile to pkml";
public static readonly string ExportIndividual = "Export individual to pkml";
public static readonly string ExportSimulationResultsToExcel = $"Export simulation results to {Excel}";
public static readonly string ExportPopulationAnalysisToExcelTitle = $"Export analysis to {Excel}";
public static readonly string ExportSimulationResultsToCSV = $"Export simulation results to CSV";
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.77" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.77" />
<PackageReference Include="OSPSuite.Core" Version="12.0.84" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.84" />
<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.77" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.84" />
<PackageReference Include="OSPSuite.Utility" Version="4.0.0.4" />
<PackageReference Include="OSPSuite.Core" Version="12.0.77" />
<PackageReference Include="OSPSuite.Core" Version="12.0.84" />
</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.77" />
<PackageReference Include="OSPSuite.Presentation" Version="12.0.77" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.77" />
<PackageReference Include="OSPSuite.Core" Version="12.0.84" />
<PackageReference Include="OSPSuite.Presentation" Version="12.0.84" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.84" />
<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
@@ -1,52 +1,34 @@
using OSPSuite.Core;
using System.Collections.Generic;
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;
using PKSim.Assets;
using PKSim.Core.Model;
using IFormulaFactory = PKSim.Core.Model.IFormulaFactory;

namespace PKSim.Core.Mappers
{
public interface IExpressionProfileToExpressionProfileBuildingBlockMapper : IMapper<ExpressionProfile, ExpressionProfileBuildingBlock>
public interface IExpressionProfileToExpressionProfileBuildingBlockMapper : IPathAndValueBuildingBlockMapper<ExpressionProfile, ExpressionProfileBuildingBlock>
{
}

public class ExpressionProfileToExpressionProfileBuildingBlockMapper : IExpressionProfileToExpressionProfileBuildingBlockMapper
public class ExpressionProfileToExpressionProfileBuildingBlockMapper : PathAndValueBuildingBlockMapper<ExpressionProfile, ExpressionProfileBuildingBlock, ExpressionParameter>, IExpressionProfileToExpressionProfileBuildingBlockMapper
{
private readonly IObjectBaseFactory _objectBaseFactory;
private readonly IEntityPathResolver _entityPathResolver;
private readonly IFormulaFactory _formulaFactory;
private readonly IApplicationConfiguration _applicationConfiguration;

public ExpressionProfileToExpressionProfileBuildingBlockMapper(IObjectBaseFactory objectBaseFactory, IEntityPathResolver entityPathResolver, IFormulaFactory formulaFactory, IApplicationConfiguration applicationConfiguration)
public ExpressionProfileToExpressionProfileBuildingBlockMapper(IObjectBaseFactory objectBaseFactory, IEntityPathResolver entityPathResolver, IApplicationConfiguration applicationConfiguration, ILazyLoadTask lazyLoadTask) :
base(objectBaseFactory, entityPathResolver, applicationConfiguration, lazyLoadTask)
{
_objectBaseFactory = objectBaseFactory;
_entityPathResolver = entityPathResolver;
_formulaFactory = formulaFactory;
_applicationConfiguration = applicationConfiguration;
}

public ExpressionProfileBuildingBlock MapFrom(ExpressionProfile expressionProfile)
protected override IReadOnlyList<IParameter> AllParametersFor(ExpressionProfile sourcePKSimBuildingBlock)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename the parameter here. No need to keep using the generic name of the base class IMO

{
var expressionProfileBuildingBlock = _objectBaseFactory.Create<ExpressionProfileBuildingBlock>();
return sourcePKSimBuildingBlock.GetAllChildren<IParameter>();
}

expressionProfileBuildingBlock.Name = expressionProfile.Name;
expressionProfileBuildingBlock.PKSimVersion = _applicationConfiguration.Version;
expressionProfileBuildingBlock.Description = expressionProfile.Description;
public override ExpressionProfileBuildingBlock MapFrom(ExpressionProfile expressionProfile)
{
var expressionProfileBuildingBlock = base.MapFrom(expressionProfile);

expressionProfileBuildingBlock.Type = mapExpressionType(expressionProfile.Molecule.MoleculeType);

var allParameters = expressionProfile.GetAllChildren<IParameter>();

foreach (var parameter in allParameters)
{
var expressionParameter = mapExpressionParameterFromExpressionProfile(parameter, expressionProfileBuildingBlock);
expressionProfileBuildingBlock.Add(expressionParameter);
}

return expressionProfileBuildingBlock;
}

Expand All @@ -64,29 +46,5 @@ private ExpressionType mapExpressionType(QuantityType moleculeType)

throw new PKSimException(PKSimConstants.Error.CouldNotFindMoleculeType(moleculeType.ToString()));
}

private ExpressionParameter mapExpressionParameterFromExpressionProfile(IParameter parameter,
ExpressionProfileBuildingBlock expressionProfileBuildingBlock)
{
var expressionParameter = _objectBaseFactory.Create<ExpressionParameter>();

if (parameter.Formula != null && parameter.Formula.IsCachable())
{
var formula = _formulaFactory.RateFor(CoreConstants.CalculationMethod.EXPRESSION_PARAMETERS, parameter.Formula.Name,
expressionProfileBuildingBlock.FormulaCache);
expressionParameter.Formula = formula;
}
else
{
(expressionParameter.Value, _) = parameter.TryGetValue();
}

expressionParameter.Name = parameter.Name;

expressionParameter.Path = _entityPathResolver.ObjectPathFor(parameter);
expressionParameter.Dimension = parameter.Dimension;
expressionParameter.DisplayUnit = parameter.DisplayUnit;
return expressionParameter;
}
}
}
94 changes: 94 additions & 0 deletions src/PKSim.Core/Mappers/PathAndValueBuildingBlockMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System.Collections.Generic;
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;
using OSPSuite.Utility.Collections;
using PKSim.Core.Model;

namespace PKSim.Core.Mappers
{
public interface IPathAndValueBuildingBlockMapper<in T, out TBuildingBlock> : IMapper<T, TBuildingBlock>
{
}

public abstract class PathAndValueBuildingBlockMapper<T, TBuildingBlock, TBuilder> : IPathAndValueBuildingBlockMapper<T, TBuildingBlock> where T : PKSimBuildingBlock where TBuildingBlock : PathAndValueEntityBuildingBlockFromPKSim<TBuilder> where TBuilder : PathAndValueEntity
{
protected IObjectBaseFactory _objectBaseFactory;
protected IEntityPathResolver _entityPathResolver;
protected IApplicationConfiguration _applicationConfiguration;
private readonly ILazyLoadTask _lazyLoadTask;
private readonly Cache<string, IFormula> _formulaCache = new Cache<string, IFormula>(x => x.Name);

protected PathAndValueBuildingBlockMapper(IObjectBaseFactory objectBaseFactory, IEntityPathResolver entityPathResolver, IApplicationConfiguration applicationConfiguration, ILazyLoadTask lazyLoadTask)
{
_objectBaseFactory = objectBaseFactory;
_entityPathResolver = entityPathResolver;
_applicationConfiguration = applicationConfiguration;
_lazyLoadTask = lazyLoadTask;
}

protected TBuildingBlock CreateBaseObject(T pkSimBuildingBlock)
{
var buildingBlock = _objectBaseFactory.Create<TBuildingBlock>();

buildingBlock.Name = pkSimBuildingBlock.Name;
buildingBlock.PKSimVersion = _applicationConfiguration.Version;
buildingBlock.Description = pkSimBuildingBlock.Description;
return buildingBlock;
}

private TBuilder mapBuilderParameter(IParameter parameter)
{
var builderParameter = _objectBaseFactory.Create<TBuilder>();

if (parameter.Formula != null && parameter.Formula.IsCachable())
{
if (!_formulaCache.Contains(parameter.Formula.Name))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not understand how to use the formula factory in this more generalized case. We had previously hard-coded the category to EXPRESSION_PARAMETERS but I couldn't generalize it.

In the end, I just did my own uniqueness cache scheme

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We did something like this before

var formula = _formulaFactory.RateFor(CoreConstants.CalculationMethod.EXPRESSION_PARAMETERS, parameter.Formula.Name,
               expressionProfileBuildingBlock.FormulaCache)

But this 'EXPRESSION_PARAMETERS' value was just hard coded. I didn't know what to use to make it more general, so I just created my own Cache scheme to make sure the formulae were unique by name instead of using _formulaFactory

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah but the code is not equivalent. One you were creating anew instance, here you are adding the same reference

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plus you are not adding it to the building block FormulaCache. they won';t be serialized

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are copied over to the bb somewhere else

_formulaCache.Add(parameter.Formula);

builderParameter.Formula = _formulaCache[parameter.Formula.Name];
}
else
{
(builderParameter.Value, _) = parameter.TryGetValue();
}

builderParameter.Name = parameter.Name;

builderParameter.Path = _entityPathResolver.ObjectPathFor(parameter);
builderParameter.Dimension = parameter.Dimension;
builderParameter.DisplayUnit = parameter.DisplayUnit;
return builderParameter;
}

protected void MapAllParameters(T sourcePKSimBuildingBlock, TBuildingBlock buildingBlock)
{
var allParameters = AllParametersFor(sourcePKSimBuildingBlock);

foreach (var parameter in allParameters)
{
var builderParameter = mapBuilderParameter(parameter);
buildingBlock.Add(builderParameter);
}

foreach (var formula in _formulaCache)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we copy all the formulae into the BB for serialization

{
buildingBlock.FormulaCache.Add(formula);
}
}

protected abstract IReadOnlyList<IParameter> AllParametersFor(T sourcePKSimBuildingBlock);

public virtual TBuildingBlock MapFrom(T input)
{
_lazyLoadTask.Load(input);

var buildingBlock = CreateBaseObject(input);
MapAllParameters(input, buildingBlock);
return buildingBlock;
}
}
}
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.77" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.77" />
<PackageReference Include="OSPSuite.Assets.Images" Version="12.0.77" />
<PackageReference Include="OSPSuite.Infrastructure.Import" Version="12.0.77" />
<PackageReference Include="OSPSuite.Core" Version="12.0.84" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.84" />
<PackageReference Include="OSPSuite.Assets.Images" Version="12.0.84" />
<PackageReference Include="OSPSuite.Infrastructure.Import" Version="12.0.84" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
</ItemGroup>

Expand Down
7 changes: 3 additions & 4 deletions src/PKSim.Core/Snapshots/Mappers/ExpressionProfileMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
using PKSim.Core.Services;
using PKSim.Core.Snapshots.Services;
using ModelIndividual = PKSim.Core.Model.Individual;
using ModelExpressionProfile = PKSim.Core.Model.ExpressionProfile;
using SnapshotExpressionProfile = PKSim.Core.Snapshots.ExpressionProfile;

namespace PKSim.Core.Snapshots.Mappers
{
public class ExpressionProfileMapper : ObjectBaseSnapshotMapperBase<ModelExpressionProfile, SnapshotExpressionProfile>
public class ExpressionProfileMapper : ObjectBaseSnapshotMapperBase<Model.ExpressionProfile, SnapshotExpressionProfile>
{
private readonly ParameterMapper _parameterMapper;
private readonly ExpressionContainerMapper _expressionContainerMapper;
Expand Down Expand Up @@ -41,7 +40,7 @@ IMoleculeParameterTask moleculeParameterTask
_ontogenyMapper = ontogenyMapper;
}

public override async Task<SnapshotExpressionProfile> MapToSnapshot(ModelExpressionProfile expressionProfile)
public override async Task<SnapshotExpressionProfile> MapToSnapshot(Model.ExpressionProfile expressionProfile)
{
var (molecule, individual) = expressionProfile;
//We do not use the base method here as we want to save the name differently using the composite part of the name
Expand Down Expand Up @@ -86,7 +85,7 @@ private void updateMoleculeSpecificPropertiesToSnapshot(SnapshotExpressionProfil
}
}

public override async Task<ModelExpressionProfile> MapToModel(SnapshotExpressionProfile snapshot, SnapshotContext snapshotContext)
public override async Task<Model.ExpressionProfile> MapToModel(SnapshotExpressionProfile snapshot, SnapshotContext snapshotContext)
{
var expressionProfile = _expressionProfileFactory.Create(snapshot.Type, snapshot.Species, snapshot.Molecule);
expressionProfile.Description = snapshot.Description;
Expand Down
18 changes: 9 additions & 9 deletions src/PKSim.Infrastructure/PKSim.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.0" />
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.13" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.77" />
<PackageReference Include="OSPSuite.Core" Version="12.0.77" />
<PackageReference Include="OSPSuite.Infrastructure" Version="12.0.77" />
<PackageReference Include="OSPSuite.Infrastructure.Castle" Version="12.0.77" />
<PackageReference Include="OSPSuite.Infrastructure.Export" Version="12.0.77" />
<PackageReference Include="OSPSuite.Infrastructure.Import" Version="12.0.77" />
<PackageReference Include="OSPSuite.Infrastructure.Reporting" Version="12.0.77" />
<PackageReference Include="OSPSuite.Infrastructure.Serialization" Version="12.0.77" />
<PackageReference Include="OSPSuite.Presentation.Serialization" Version="12.0.77" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.84" />
<PackageReference Include="OSPSuite.Core" Version="12.0.84" />
<PackageReference Include="OSPSuite.Infrastructure" Version="12.0.84" />
<PackageReference Include="OSPSuite.Infrastructure.Castle" Version="12.0.84" />
<PackageReference Include="OSPSuite.Infrastructure.Export" Version="12.0.84" />
<PackageReference Include="OSPSuite.Infrastructure.Import" Version="12.0.84" />
<PackageReference Include="OSPSuite.Infrastructure.Reporting" Version="12.0.84" />
<PackageReference Include="OSPSuite.Infrastructure.Serialization" Version="12.0.84" />
<PackageReference Include="OSPSuite.Presentation.Serialization" Version="12.0.84" />
<PackageReference Include="OSPSuite.Utility" Version="4.0.0.4" />
</ItemGroup>

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

<ItemGroup>
<PackageReference Include="OSPSuite.Assets" Version="12.0.77" />
<PackageReference Include="OSPSuite.Assets" Version="12.0.84" />
<PackageReference Include="OSPSuite.Utility" Version="4.0.0.4" />
<PackageReference Include="OSPSuite.Core" Version="12.0.77" />
<PackageReference Include="OSPSuite.Core" Version="12.0.84" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading