-
Notifications
You must be signed in to change notification settings - Fork 8
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 #1970 add simulation builder #1973
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,32 @@ | ||
namespace OSPSuite.Core.Domain.Builder | ||
{ | ||
public class ModelConfiguration | ||
internal class ModelConfiguration | ||
{ | ||
public SimulationBuilder SimulationBuilder { get; } | ||
public IModel Model { get; } | ||
public SimulationConfiguration SimulationConfiguration { get; } | ||
|
||
public ModelConfiguration(IModel model, SimulationConfiguration simulationConfiguration) | ||
public ModelConfiguration(IModel model, SimulationConfiguration simulationConfiguration, SimulationBuilder simulationBuilder) | ||
{ | ||
Model = model; | ||
SimulationConfiguration = simulationConfiguration; | ||
SimulationBuilder = simulationBuilder; | ||
} | ||
|
||
public bool ShouldValidate => SimulationConfiguration.ShouldValidate; | ||
|
||
public void Deconstruct(out IModel model, out SimulationConfiguration simulationConfiguration) | ||
public void Deconstruct(out IModel model, out SimulationConfiguration simulationConfiguration, out SimulationBuilder simulationBuilder) | ||
{ | ||
model = Model; | ||
simulationConfiguration = SimulationConfiguration; | ||
simulationBuilder = SimulationBuilder; | ||
} | ||
|
||
|
||
public void Deconstruct(out IModel model, out SimulationBuilder simulationBuilder) | ||
{ | ||
model = Model; | ||
simulationBuilder = SimulationBuilder; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using OSPSuite.Utility.Collections; | ||
|
||
namespace OSPSuite.Core.Domain.Builder | ||
{ | ||
public class SimulationBuilder | ||
{ | ||
private readonly SimulationConfiguration _simulationConfiguration; | ||
|
||
private readonly ObjectBaseCache<ITransportBuilder> _passiveTransports = new ObjectBaseCache<ITransportBuilder>(); | ||
private readonly ObjectBaseCache<IReactionBuilder> _reactions = new ObjectBaseCache<IReactionBuilder>(); | ||
private readonly ObjectBaseCache<IEventGroupBuilder> _eventGroups = new ObjectBaseCache<IEventGroupBuilder>(); | ||
private readonly ObjectBaseCache<IObserverBuilder> _observers = new ObjectBaseCache<IObserverBuilder>(); | ||
private readonly ObjectBaseCache<IMoleculeBuilder> _molecules = new ObjectBaseCache<IMoleculeBuilder>(); | ||
private readonly StartValueCache<ParameterStartValue> _parameterStartValues = new StartValueCache<ParameterStartValue>(); | ||
private readonly StartValueCache<MoleculeStartValue> _moleculeStartValues = new StartValueCache<MoleculeStartValue>(); | ||
|
||
private readonly Cache<IObjectBase, IObjectBase> _builderCache = new Cache<IObjectBase, IObjectBase>(onMissingKey: x => null); | ||
|
||
public SimulationBuilder(SimulationConfiguration simulationConfiguration) | ||
{ | ||
_simulationConfiguration = simulationConfiguration; | ||
performMerge(); | ||
} | ||
|
||
internal IObjectBase BuilderFor(IObjectBase modelObject) => _builderCache[modelObject]; | ||
|
||
internal void AddBuilderReference(IObjectBase modelObject, IObjectBase builder) | ||
{ | ||
_builderCache[modelObject] = builder; | ||
} | ||
|
||
private IReadOnlyList<T> all<T>(Func<Module, T> propAccess) where T : IBuildingBlock => | ||
_simulationConfiguration.ModuleConfigurations.Select(x => propAccess(x.Module)).Where(x => x != null).ToList(); | ||
|
||
private IEnumerable<T> allBuilder<T>(Func<Module, IBuildingBlock<T>> propAccess) where T : IBuilder => | ||
all(propAccess).SelectMany(x => x); | ||
|
||
private IEnumerable<T> allStartValueBuilder<T>(Func<ModuleConfiguration, IBuildingBlock<T>> propAccess) where T : IStartValue => | ||
_simulationConfiguration.ModuleConfigurations.Select(propAccess).Where(x => x != null).SelectMany(x => x); | ||
|
||
internal IEnumerable<IMoleculeBuilder> AllPresentMolecules() | ||
{ | ||
var moleculeNames = _moleculeStartValues | ||
.Where(moleculeStartValue => moleculeStartValue.IsPresent) | ||
.Select(moleculeStartValue => moleculeStartValue.MoleculeName) | ||
.Distinct(); | ||
|
||
|
||
return moleculeNames.Select(x => _molecules[x]).Where(m => m != null); | ||
} | ||
|
||
internal IEnumerable<MoleculeStartValue> AllPresentMoleculeValues() => | ||
AllPresentMoleculeValuesFor(_molecules.Select(x => x.Name)); | ||
|
||
internal IEnumerable<MoleculeStartValue> AllPresentMoleculeValuesFor(IEnumerable<string> moleculeNames) | ||
{ | ||
return _moleculeStartValues | ||
.Where(msv => moleculeNames.Contains(msv.MoleculeName)) | ||
.Where(msv => msv.IsPresent); | ||
} | ||
|
||
internal IEnumerable<IMoleculeBuilder> AllFloatingMolecules() => Molecules.Where(x => x.IsFloating); | ||
|
||
internal IReadOnlyList<string> AllPresentMoleculeNames() => AllPresentMoleculeNames(x => true); | ||
|
||
//Uses toArray so that the marshaling to R works out of the box (array vs list) | ||
internal IReadOnlyList<string> AllPresentMoleculeNames(Func<IMoleculeBuilder, bool> query) => | ||
AllPresentMolecules().Where(query).Select(x => x.Name).ToArray(); | ||
|
||
internal IReadOnlyList<string> AllPresentFloatingMoleculeNames() => | ||
AllPresentMoleculeNames(m => m.IsFloating); | ||
|
||
internal IReadOnlyList<string> AllPresentStationaryMoleculeNames() => | ||
AllPresentMoleculeNames(m => !m.IsFloating); | ||
|
||
internal IReadOnlyList<string> AllPresentXenobioticFloatingMoleculeNames() => | ||
AllPresentMoleculeNames(m => m.IsFloating && m.IsXenobiotic); | ||
|
||
internal IReadOnlyList<string> AllPresentEndogenousStationaryMoleculeNames() => | ||
AllPresentMoleculeNames(m => !m.IsFloating && !m.IsXenobiotic); | ||
|
||
internal IReadOnlyList<string> AllPresentEndogenousMoleculeNames() => AllPresentMoleculeNames(m => !m.IsXenobiotic); | ||
|
||
private void performMerge() | ||
{ | ||
_passiveTransports.AddRange(allBuilder(x => x.PassiveTransports)); | ||
_reactions.AddRange(allBuilder(x => x.Reactions)); | ||
_eventGroups.AddRange(allBuilder(x => x.EventGroups)); | ||
_observers.AddRange(allBuilder(x => x.Observers)); | ||
_molecules.AddRange(allBuilder(x => x.Molecules)); | ||
_parameterStartValues.AddRange(allStartValueBuilder(x => x.SelectedParameterStartValues)); | ||
_moleculeStartValues.AddRange(allStartValueBuilder(x => x.SelectedMoleculeStartValues)); | ||
} | ||
|
||
internal IReadOnlyList<ISpatialStructure> SpatialStructures => all(x => x.SpatialStructure); | ||
internal IReadOnlyCollection<ITransportBuilder> PassiveTransports => _passiveTransports; | ||
internal IReadOnlyCollection<IReactionBuilder> Reactions => _reactions; | ||
internal IReadOnlyCollection<IEventGroupBuilder> EventGroups => _eventGroups; | ||
internal IReadOnlyCollection<IObserverBuilder> Observers => _observers; | ||
internal IReadOnlyCollection<IMoleculeBuilder> Molecules => _molecules; | ||
internal IReadOnlyCollection<ParameterStartValue> ParameterStartValues => _parameterStartValues; | ||
internal IReadOnlyCollection<MoleculeStartValue> MoleculeStartValues => _moleculeStartValues; | ||
|
||
internal IMoleculeBuilder MoleculeByName(string name) => _molecules[name]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
ah, cool. I didn't know about this before.