Skip to content

Commit

Permalink
Fixes Open-Systems-Pharmacology#2586 Provide info which parameters ar…
Browse files Browse the repository at this point in the history
…e not common for all species
  • Loading branch information
Yuri05 authored and Yuri05 committed Apr 5, 2023
1 parent 82fff03 commit b1c2f0c
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using OSPSuite.Utility.Collections;

namespace PKSim.Core.Repositories
{
public interface IContainerParametersNotCommonForAllSpeciesRepository : IStartableRepository<(string ContainerPath, string ParameterName, int SpeciesCount)>
{
bool UsedForAllSpecies(string containerPath, string parameterName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace PKSim.Infrastructure.ORM.FlatObjects
{
public class FlatContainerParametersNotCommonForAllSpecies
{
public int ContainerId { get; set; }
public string ContainerType { get; set; }
public string ContainerName { get; set; }
public string ParameterName { get; set; }
public int SpeciesCount { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Collections.Generic;
using System.Linq;
using OSPSuite.Utility.Collections;
using PKSim.Core.Repositories;

namespace PKSim.Infrastructure.ORM.Repositories
{
public class ContainerParametersNotCommonForAllSpeciesRepository: StartableRepository<(string ContainerPath, string ParameterName, int SpeciesCount)>, IContainerParametersNotCommonForAllSpeciesRepository
{
private readonly IFlatContainerRepository _flatContainerRepository;
private readonly IFlatContainerParametersNotCommonForAllSpeciesRepository _flatContainerParametersNotCommonForAllSpeciesRepository;

private readonly List<(string ContainerPath, string ParameterName, int SpeciesCount)> _containerParametersNotCommonForAllSpecies;
private readonly ICache<string, List<string>> _parametersNotCommonForAllSpeciesByContainer;

public ContainerParametersNotCommonForAllSpeciesRepository(
IFlatContainerRepository flatContainerRepository,
IFlatContainerParametersNotCommonForAllSpeciesRepository flatContainerParametersNotCommonForAllSpeciesRepository)
{
_flatContainerRepository = flatContainerRepository;
_flatContainerParametersNotCommonForAllSpeciesRepository = flatContainerParametersNotCommonForAllSpeciesRepository;

_containerParametersNotCommonForAllSpecies = new List<(string ContainerPath, string ParameterName, int SpeciesCount)>();
_parametersNotCommonForAllSpeciesByContainer = new Cache<string, List<string>>();
}

protected override void DoStart()
{
var flatContainerParametersNotCommonForAllSpecies = _flatContainerParametersNotCommonForAllSpeciesRepository.All().ToList();

foreach(var containerParameter in flatContainerParametersNotCommonForAllSpecies)
{
var containerPath = _flatContainerRepository.ContainerPathFrom(containerParameter.ContainerId).ToString();
_containerParametersNotCommonForAllSpecies.Add((containerPath, containerParameter.ParameterName, containerParameter.SpeciesCount));
}

//cache the parameters by container path
foreach (var containerParametersInContainer in _containerParametersNotCommonForAllSpecies.GroupBy(x => x.ContainerPath))
{
_parametersNotCommonForAllSpeciesByContainer.Add(containerParametersInContainer.Key, containerParametersInContainer.Select(cp=>cp.ParameterName).ToList());
}
}

public override IEnumerable<(string ContainerPath, string ParameterName, int SpeciesCount)> All()
{
Start();
return _containerParametersNotCommonForAllSpecies;
}

public bool UsedForAllSpecies(string containerPath, string parameterName)
{
Start();
var parametersUsedNotForAllSpeciesInContainer = _parametersNotCommonForAllSpeciesByContainer[containerPath];

return parametersUsedNotForAllSpeciesInContainer == null || !parametersUsedNotForAllSpeciesInContainer.Contains(parameterName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using PKSim.Core;
using PKSim.Infrastructure.ORM.Core;
using PKSim.Infrastructure.ORM.FlatObjects;
using PKSim.Infrastructure.ORM.Mappers;

namespace PKSim.Infrastructure.ORM.Repositories
{
public interface IFlatContainerParametersNotCommonForAllSpeciesRepository : IMetaDataRepository<FlatContainerParametersNotCommonForAllSpecies>
{
}

public class FlatContainerParametersNotCommonForAllSpeciesRepository : MetaDataRepository<FlatContainerParametersNotCommonForAllSpecies>, IFlatContainerParametersNotCommonForAllSpeciesRepository
{
public FlatContainerParametersNotCommonForAllSpeciesRepository(IDbGateway dbGateway, IDataTableToMetaDataMapper<FlatContainerParametersNotCommonForAllSpecies> mapper)
: base(dbGateway, mapper, CoreConstants.ORM.VIEW_CONTAINER_PARAMETER_NOT_FOR_ALL_SPECIES)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using System.Linq;
using FluentNHibernate.Utils;
using OSPSuite.BDDHelper;
using OSPSuite.BDDHelper.Extensions;
using PKSim.Core.Repositories;

namespace PKSim.IntegrationTests
{
public abstract class concern_for_ContainerParametersNotCommonForAllSpeciesRepository : ContextForIntegration<IContainerParametersNotCommonForAllSpeciesRepository>
{
}

public class When_retrieving_parameters_not_common_for_all_species_from_the_repository : concern_for_ContainerParametersNotCommonForAllSpeciesRepository
{
private IEnumerable<(string ContainerPath, string ParameterName, int SpeciesCount)> _result;

protected override void Because()
{
_result = sut.All();
}

[Observation]
public void should_return_at_least_one_parameter()
{
_result.Count().ShouldBeGreaterThan(0);
}

[Observation]
public void all_parameter_paths_should_start_with_organism_or_neighborhoods()
{
//because we deal here with individual parameters only: only parameters with path "Organism|..." or "Neighborhoods|..." may appear in the list
_result.Each(p=>
{
var containerPath = p.ContainerPath;
(containerPath.StartsWith("Organism")|| containerPath.StartsWith("Neighborhoods")).ShouldBeTrue($"{p.ContainerPath}|{p.ParameterName}");
});
}
}

public class When_testing_if_a_parameter_is_common_for_all_species : concern_for_ContainerParametersNotCommonForAllSpeciesRepository
{
[Observation]
public void age_parameter_should_be_defined_not_for_all_species()
{
sut.UsedForAllSpecies("Organism","Age").ShouldBeFalse();
}

[Observation]
public void weight_parameter_should_be_defined_for_all_species()
{
sut.UsedForAllSpecies("Organism", "Weight").ShouldBeTrue();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Linq;
using OSPSuite.BDDHelper;
using OSPSuite.BDDHelper.Extensions;
using PKSim.Infrastructure.ORM.FlatObjects;
using PKSim.Infrastructure.ORM.Repositories;

namespace PKSim.IntegrationTests
{
public abstract class concern_for_FlatContainerParametersNotCommonForAllSpeciesRepository : ContextForIntegration<IFlatContainerParametersNotCommonForAllSpeciesRepository>
{
}

public class When_resolving_all_parameters_not_common_for_all_species_as_a_flat_table : concern_for_FlatContainerParametersNotCommonForAllSpeciesRepository
{
private IEnumerable<FlatContainerParametersNotCommonForAllSpecies> _result;

protected override void Because()
{
_result = sut.All();
}

[Observation]
public void should_retrieve_some_object_from_the_underlying_database()
{
_result.Count().ShouldBeGreaterThan(0);
}
}
}

0 comments on commit b1c2f0c

Please sign in to comment.