-
Notifications
You must be signed in to change notification settings - Fork 50
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
Changes from 6 commits
1763a1e
113c553
6148a9b
a97afd2
c981493
877a18d
4bb6920
6541612
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We did something like this before
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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