-
Notifications
You must be signed in to change notification settings - Fork 50
/
SimulationPersistableUpdater.cs
139 lines (111 loc) · 5.42 KB
/
SimulationPersistableUpdater.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using OSPSuite.Core.Domain;
using OSPSuite.Utility.Extensions;
using PKSim.Core.Model;
namespace PKSim.Core.Services
{
public interface ISimulationPersistableUpdater : OSPSuite.Core.Domain.Services.ISimulationPersistableUpdater
{
void UpdatePersistableFromSettings(IndividualSimulation individualSimulation);
void UpdatePersistableFromSettings(PopulationSimulation populationSimulation);
void ResetPersistable(Simulation simulation);
}
public class SimulationPersistableUpdater : OSPSuite.Core.Domain.Services.SimulationPersistableUpdater, ISimulationPersistableUpdater
{
public SimulationPersistableUpdater(IEntitiesInContainerRetriever quantitiesRetriever) : base(quantitiesRetriever)
{
}
public void UpdatePersistableFromSettings(IndividualSimulation individualSimulation)
{
UpdateSimulationPersistable(individualSimulation);
var organism = individualSimulation.Model.Root.Container(Constants.ORGANISM);
individualSimulation.Compounds.Each(compound => addRequiredOutputForSimulation(organism, compound));
}
public void UpdatePersistableFromSettings(PopulationSimulation populationSimulation)
{
UpdateSimulationPersistable(populationSimulation);
}
private void addRequiredOutputForSimulation(IContainer organism, Compound compound)
{
//make sure venous blood plasma is always selected so that PK can be calculated as well
updatePersistable(organism, CoreConstants.Organ.VENOUS_BLOOD, CoreConstants.Compartment.PLASMA,
compound.Name, CoreConstants.Observer.CONCENTRATION_IN_CONTAINER);
//make sure peripheral venous blood plasma is always selected so that PK can be calculated as well
updatePersistable(organism, CoreConstants.Organ.PERIPHERAL_VENOUS_BLOOD, compound.Name,
CoreConstants.Observer.PLASMA_PERIPHERAL_VENOUS_BLOOD);
//make sure lumen FabsOral is always selected for fabs calculation
updatePersistable(organism, CoreConstants.Organ.LUMEN, compound.Name,
CoreConstants.Observer.FABS_ORAL);
}
private void updatePersistable(IContainer container, params string[] path)
{
var observer = container.EntityAt<Observer>(path);
if (observer == null) return;
observer.Persistable = true;
}
public void ResetPersistable(Simulation simulation)
{
SetPersistable(simulation.All<IMoleculeAmount>(), false);
SetPersistable(simulation.All<IObserver>(), true);
setApplicationObserversNonPersistable(simulation);
setUrineFecesAndBileAmountToPersitable(simulation);
}
private void setUrineFecesAndBileAmountToPersitable(Simulation simulation)
{
setUrineFecesAndBilePersitable(simulation, setMoleculeAmountToPersistableIn);
}
private void setUrineFecesAndBileConcentrationToNonPersitable(Simulation simulation)
{
setUrineFecesAndBilePersitable(simulation, setConcentrationObserversToNonPersistableIn);
}
private void setUrineFecesAndBilePersitable(Simulation simulation, Action<IContainer> updatePersitableInContainerAction)
{
var organism = simulation.Model.Root.Container(Constants.ORGANISM);
if (organism == null)
return;
var urine = organism.EntityAt<IContainer>(CoreConstants.Organ.KIDNEY, CoreConstants.Compartment.URINE);
updatePersitableInContainerAction(urine);
var feces = organism.EntityAt<IContainer>(CoreConstants.Organ.LUMEN, CoreConstants.Compartment.FECES);
updatePersitableInContainerAction(feces);
var gallBladder = organism.EntityAt<IContainer>(CoreConstants.Organ.GALLBLADDER);
updatePersitableInContainerAction(gallBladder);
}
private void setMoleculeAmountToPersistableIn(IContainer container)
{
if (container == null) return;
SetPersistable(container.GetAllChildren<IMoleculeAmount>(), true);
}
private void setConcentrationObserversToNonPersistableIn(IContainer container)
{
if (container == null) return;
SetPersistable(container.GetAllChildren<IObserver>(x => x.NameIsOneOf(CoreConstants.Observer.CONCENTRATION_IN_CONTAINER)), false);
}
private void setApplicationObserversNonPersistable(Simulation simulation)
{
//Set all observers defined in application to persistable false
var applicationSet = simulation.Model.Root.GetSingleChildByName<IContainer>(Constants.APPLICATIONS);
if (applicationSet == null) return;
foreach (var appObserver in applicationSet.GetAllChildren<IObserver>())
{
if (applicationObserverShouldBeShown(appObserver))
continue;
appObserver.Persistable = false;
}
}
/// <summary>
/// Return true if an application observer should not be hidden from user
/// </summary>
/// <param name="observer"></param>
private bool applicationObserverShouldBeShown(IObserver observer)
{
string name = observer.Name;
if (name.StartsWith(CoreConstants.Observer.FRACTION_SOLID_PREFIX))
return true;
if (name.StartsWith(CoreConstants.Observer.FRACTION_DISSOLVED_PREFIX))
return true;
if (name.StartsWith(CoreConstants.Observer.FRACTION_INSOLUBLE_PREFIX))
return true;
return false;
}
}
}