Skip to content
Closed
Changes from all 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
33 changes: 33 additions & 0 deletions src/OSPSuite.R/Domain/Simulation.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using OSPSuite.CLI.Core.MinimalImplementations;
using OSPSuite.Core.Chart;
using OSPSuite.Core.Commands;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Builder;
using OSPSuite.Core.Domain.Data;
using OSPSuite.Core.Domain.Services;
using OSPSuite.Utility.Extensions;
using OSPSuite.Utility.Visitor;

namespace OSPSuite.R.Domain
{
public class Simulation : ISimulation
{
private readonly IReadOnlyList<MoleculeBuilder> _allBuildersInSimulation;
public IModelCoreSimulation CoreSimulation { get; }

public bool IsLoaded { get; set; } = true;
Expand Down Expand Up @@ -50,6 +53,9 @@ public void AddAnalysis(ISimulationAnalysis simulationAnalysis)
public Simulation(IModelCoreSimulation modelCoreSimulation)
{
CoreSimulation = modelCoreSimulation;

// Initialize the list of molecule builders for which there are initial conditions in the simulation
_allBuildersInSimulation = allBuildersFor(allMoleculeNamesInSimulation.ToList()).DistinctBy(x => x.Name).ToList();
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 think we can build this list once at construction rather than build the list for each access.

}

public string Name
Expand Down Expand Up @@ -127,6 +133,33 @@ public SimulationConfiguration Configuration

public IReadOnlyList<string> CompoundNames => CoreSimulation.CompoundNames;

public IReadOnlyList<string> AllPresentFloatingMoleculeNames => _allBuildersInSimulation.Where(m => m is { IsFloating: true }).AllNames().ToArray();

public IReadOnlyList<string> AllPresentXenobioticFloatingMoleculeNames => _allBuildersInSimulation.Where(m => m is { IsFloatingXenobiotic: true }).AllNames().ToArray();

public IReadOnlyList<string> AllPresentStationaryMoleculeNames => _allBuildersInSimulation.Where(m => m is { IsFloating: false, IsXenobiotic: true }).AllNames().ToArray();

public IReadOnlyList<string> AllPresentEndogenousStationaryMoleculeNames => _allBuildersInSimulation.Where(m => m is { IsFloating: false, IsXenobiotic: false }).AllNames().ToArray();

private IEnumerable<string> allMoleculeNamesInSimulation =>
CoreSimulation.Configuration.ModuleConfigurations.
Where(x => x.SelectedInitialConditions != null). // Initial conditions are selected
SelectMany(x => x.SelectedInitialConditions).
Where(x => x.IsPresent). // this initial condition is present
Select(ic => ic.MoleculeName).Distinct();

private IEnumerable<MoleculeBuilder> allMoleculeBuilders =>
Copy link
Member

Choose a reason for hiding this comment

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

none of this code is required . The service returning a simulation builder should be used IMO in the simulation.R alongside the simulation and saved as reference to be used with each subsequent access

CoreSimulation.Configuration.ModuleConfigurations.
Where(x => x.Module.Molecules != null). // filter out modules without molecules building blocks
SelectMany(x => x.Module.Molecules);

private IReadOnlyList<MoleculeBuilder> allBuildersFor(IReadOnlyList<string> moleculeNames)
{
var cache = new CacheByName<MoleculeBuilder>();
allMoleculeBuilders.Where(x => moleculeNames.Contains(x.Name)).Each(x => cache.Add(x));
return cache.ToList();
Copy link
Member Author

Choose a reason for hiding this comment

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

Cache by name so that we only pay attention to the last merged version of a builder.

}

public IEnumerable<T> All<T>() where T : class, IEntity => CoreSimulation.All<T>();

public IParameter BodyWeight => CoreSimulation.BodyWeight;
Expand Down