Skip to content

Commit

Permalink
Fixes #2627 no expression profiles created
Browse files Browse the repository at this point in the history
  • Loading branch information
msevestre committed Nov 10, 2024
1 parent 4241446 commit 658c20c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
5 changes: 1 addition & 4 deletions src/PKSim.Core/Model/Simulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,7 @@ public override void AcceptVisitor(IVisitor visitor)
/// <typeparam name="TBuildingBlock">type of the building blocks we are looking for in the simulation</typeparam>
public virtual IEnumerable<TBuildingBlock> AllBuildingBlocks<TBuildingBlock>() where TBuildingBlock : class, IPKSimBuildingBlock
{
return from usedBb in UsedBuildingBlocks
let bb = usedBb.BuildingBlock as TBuildingBlock
where bb != null
select bb;
return UsedBuildingBlocks.Select(x => x.BuildingBlock as TBuildingBlock).Where(x => x != null);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/PKSim.Core/Services/SimulationBuildingBlockUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public interface ISimulationBuildingBlockUpdater
/// <summary>
/// Sets a clone of the template building block as "used building block" in the simulation
/// </summary>
/// <param name="simulation">Simulation that will be uing the template building block</param>
/// <param name="simulation">Simulation that will be using the template building block</param>
/// <param name="templateBuildingBlock">template building block to be used</param>
/// <param name="buildingBlockType">Type of building block</param>
/// <remarks>
/// This Method should only be used for building blocks whose occurence in a simulation is 0 or 1. (e.g Individual,
/// This Method should only be used for building blocks whose occurrence in a simulation is 0 or 1. (e.g Individual,
/// Protocol, Compound)
/// For other type (e.g. Formulation, Events), this method is not suited
/// </remarks>
Expand All @@ -31,7 +31,7 @@ public interface ISimulationBuildingBlockUpdater
/// <param name="templateBuildingBlocks">All template building blocks</param>
/// <param name="buildingBlockType">Type of building block</param>
/// <remarks>
/// This method should only be used for building blocks whose occurence in a simulation is 0 ..* (e.g. Formulation,
/// This method should only be used for building blocks whose occurrence in a simulation is 0 ..* (e.g. Formulation,
/// Events) for which all building blocks need to be updated at once
/// </remarks>
void UpdateMultipleUsedBuildingBlockInSimulationFromTemplate(Simulation simulation, IEnumerable<IPKSimBuildingBlock> templateBuildingBlocks, PKSimBuildingBlockType buildingBlockType);
Expand Down
35 changes: 32 additions & 3 deletions src/PKSim.Infrastructure/ProjectConverter/v12/Converter11To12.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using OSPSuite.Utility.Visitor;
using PKSim.Core;
using PKSim.Core.Model;
using PKSim.Core.Repositories;
using PKSim.Core.Services;

namespace PKSim.Infrastructure.ProjectConverter.v12
Expand All @@ -17,13 +18,21 @@ public class Converter11To12 : IObjectConverter,
IVisitor<Population>
{
private readonly IDefaultIndividualRetriever _defaultIndividualRetriever;
private readonly IBuildingBlockRepository _buildingBlockRepository;
private readonly ISimulationBuildingBlockUpdater _simulationBuildingBlockUpdater;
private readonly ICloner _cloner;
private bool _converted;
public bool IsSatisfiedBy(int version) => version == ProjectVersions.V11;

public Converter11To12(IDefaultIndividualRetriever defaultIndividualRetriever, ICloner cloner)
public Converter11To12(
IDefaultIndividualRetriever defaultIndividualRetriever,
IBuildingBlockRepository buildingBlockRepository,
ISimulationBuildingBlockUpdater simulationBuildingBlockUpdater,
ICloner cloner)
{
_defaultIndividualRetriever = defaultIndividualRetriever;
_buildingBlockRepository = buildingBlockRepository;
_simulationBuildingBlockUpdater = simulationBuildingBlockUpdater;
_cloner = cloner;
}

Expand Down Expand Up @@ -55,7 +64,7 @@ public Converter11To12(IDefaultIndividualRetriever defaultIndividualRetriever, I
private void convertBuildMode(XElement parameterNode)
{
var buildMode = parameterNode.GetAttribute("mode");

if (!string.Equals(buildMode, "Property"))
return;

Expand Down Expand Up @@ -152,6 +161,26 @@ private void convertIndividual(Individual individual)
private void convertSimulation(Simulation simulation)
{
convertIndividual(simulation.BuildingBlock<Individual>());

addMissingExpressionBuildingBlock(simulation);
}

private void addMissingExpressionBuildingBlock(Simulation simulation)
{
//in v11, the usage of the expression building block was not saved in the used building block. We need to add it if it's not there already

var usedSimulationSubject = simulation.UsedBuildingBlockInSimulation<ISimulationSubject>();

//this can happen for an imported simulation
if (usedSimulationSubject == null)
return;

var templateSimulationSubject = _buildingBlockRepository.ById<ISimulationSubject>(usedSimulationSubject.TemplateId);
//this should never happen
if (templateSimulationSubject == null)
return;

_simulationBuildingBlockUpdater.UpdateMultipleUsedBuildingBlockInSimulationFromTemplate(simulation, templateSimulationSubject.AllExpressionProfiles(), PKSimBuildingBlockType.ExpressionProfile);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public override void PerformMapping()
base.PerformMapping();
Map(x => x.OriginData);
Map(x => x.Seed);

}
}
}
Binary file not shown.
18 changes: 18 additions & 0 deletions tests/PKSim.Tests/ProjectConverter/v12/Converter11To12Specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,22 @@ private void validateIndividual(Individual individual)
individual.OriginData.CalculationMethodCache.Contains(Individual_AgeDependent).ShouldBeTrue();
}
}

public class When_converting_the_v11_expression_profile_to_12 : ContextWithLoadedProject<Converter11To12>
{
private List<PopulationSimulation> _allSimulations;

public override void GlobalContext()
{
base.GlobalContext();
LoadProject("v11_expression_profile");
_allSimulations = All<PopulationSimulation>();
}

[Observation]
public void should_have_added_the_expression_used_by_the_individual_to_the_simulation()
{
_allSimulations.Each(s => s.AllBuildingBlocks<ExpressionProfile>().Count().ShouldBeGreaterThan(0));
}
}
}

0 comments on commit 658c20c

Please sign in to comment.