Skip to content
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 #2017 Move Initial Conditions relating to an expression profile… #2018

Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/OSPSuite.Core/Comparison/BuildingBlockDiffBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace OSPSuite.Core.Comparison
public abstract class BuildingBlockDiffBuilder<TBuildingBlock, TBuilder> : DiffBuilder<TBuildingBlock> where TBuildingBlock : class, IBuildingBlock<TBuilder> where TBuilder : class, IBuilder
{
private readonly ObjectBaseDiffBuilder _objectBaseDiffBuilder;
private readonly EnumerableComparer _enumerableComparer;
protected readonly EnumerableComparer _enumerableComparer;
private readonly Func<TBuilder, object> _equalityProperty;
protected Func<TBuilder, string> _presentObjectDetailsFunc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public override void Compare(IComparison<ExpressionProfileBuildingBlock> compari
{
base.Compare(comparison);
CompareValues(x => x.Type, x => x.Type, comparison);
_enumerableComparer.CompareEnumerables(comparison, x => x.InitialConditions, x => x.Path);
}
}

Expand Down
33 changes: 31 additions & 2 deletions src/OSPSuite.Core/Domain/Builder/ExpressionProfileBuildingBlock.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using OSPSuite.Core.Domain.Services;
using System.Collections.Generic;
using OSPSuite.Core.Domain.Services;
using OSPSuite.Utility.Extensions;
using OSPSuite.Utility.Visitor;
using static OSPSuite.Core.Domain.Constants.ContainerName;

namespace OSPSuite.Core.Domain.Builder
{
public class ExpressionProfileBuildingBlock : PathAndValueEntityBuildingBlockFromPKSim<ExpressionParameter>
{
private readonly StartValueCache<InitialCondition> _initialConditions = new StartValueCache<InitialCondition>();
public override string Icon => Type.IconName;

public virtual string MoleculeName { get; private set; }
Expand All @@ -13,7 +17,6 @@ public class ExpressionProfileBuildingBlock : PathAndValueEntityBuildingBlockFro

public ExpressionType Type { set; get; }


public virtual string Category { get; private set; }

public override string Name
Expand All @@ -37,6 +40,29 @@ public override string Name
}
}

public void RemoveInitialCondition(InitialCondition initialCondition)
{
if (initialCondition == null)
return;

_initialConditions.Remove(initialCondition.Path);
initialCondition.BuildingBlock = null;
}

public void AddInitialCondition(InitialCondition initialCondition)
{
_initialConditions.Add(initialCondition);
initialCondition.BuildingBlock = this;
}

public override void AcceptVisitor(IVisitor visitor)
{
base.AcceptVisitor(visitor);
_initialConditions.Each(ic => ic.AcceptVisitor(visitor));
}

public IReadOnlyCollection<InitialCondition> InitialConditions => _initialConditions;

public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager cloneManager)
{
base.UpdatePropertiesFrom(source, cloneManager);
Expand All @@ -48,6 +74,9 @@ public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager clone
// Name is required because our base objects will the private property
// But in this case the name is decomposed and stored in 3 other properties
Name = sourceExpressionProfile.Name;

_initialConditions.Clear();
sourceExpressionProfile.InitialConditions.Each(initialCondition => AddInitialCondition(cloneManager.Clone(initialCondition)));
rwmcintosh marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
32 changes: 0 additions & 32 deletions src/OSPSuite.Core/Domain/Builder/InitialConditionsBuildingBlock.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using OSPSuite.Assets;
using OSPSuite.Core.Domain.Services;

namespace OSPSuite.Core.Domain.Builder
{
Expand All @@ -9,36 +8,5 @@ public InitialConditionsBuildingBlock()
{
Icon = IconNames.INITIAL_CONDITIONS;
}

/// <summary>
/// Id of the spatial structure used to create the parameter values.
/// </summary>
public string SpatialStructureId { get; set; }

/// <summary>
/// Id of the molecule building block used to create the parameter values
/// </summary>
public string MoleculeBuildingBlockId { get; set; }

public bool Uses(IBuildingBlock buildingBlock)
{
return refersTo(buildingBlock, SpatialStructureId)
|| refersTo(buildingBlock, MoleculeBuildingBlockId);
}

private bool refersTo(IBuildingBlock buildingBlock, string idToCheck)
{
return string.Equals(idToCheck, buildingBlock.Id);
}

public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager cloneManager)
{
base.UpdatePropertiesFrom(source, cloneManager);
var sourceInitialConditionsBuildingBlock = source as InitialConditionsBuildingBlock;
if (sourceInitialConditionsBuildingBlock == null) return;

MoleculeBuildingBlockId = sourceInitialConditionsBuildingBlock.MoleculeBuildingBlockId;
SpatialStructureId = sourceInitialConditionsBuildingBlock.SpatialStructureId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ IEnumerator IEnumerable.GetEnumerator()
public override void AcceptVisitor(IVisitor visitor)
{
base.AcceptVisitor(visitor);
_allValues.Each(initialCondition => initialCondition.AcceptVisitor(visitor));
_allValues.Each(x => x.AcceptVisitor(visitor));
}

public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager cloneManager)
Expand Down
3 changes: 2 additions & 1 deletion src/OSPSuite.Core/Domain/Builder/SimulationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ private void performMerge()
_observers.AddRange(allBuilder(x => x.Observers));
_molecules.AddRange(allBuilder(x => x.Molecules));
_parameterValues.AddRange(allStartValueBuilder(x => x.SelectedParameterValues));
_initialConditions.AddRange(allStartValueBuilder(x => x.SelectedInitialConditions));
_initialConditions.AddRange(allStartValueBuilder(x => x.SelectedInitialConditions)
Copy link
Member Author

Choose a reason for hiding this comment

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

Now we need to look in all the expression profiles to build the list of all InitialConditions

.Concat(_simulationConfiguration.ExpressionProfiles.SelectMany(x => x.InitialConditions)));
}

internal IReadOnlyList<SpatialStructure> SpatialStructures => all(x => x.SpatialStructure);
Expand Down
15 changes: 7 additions & 8 deletions src/OSPSuite.Core/Domain/Services/InitialConditionsCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace OSPSuite.Core.Domain.Services
{
public interface IInitialConditionsCreator : IEmptyStartValueCreator<InitialCondition>
{
InitialConditionsBuildingBlock CreateFrom(SpatialStructure spatialStructure, MoleculeBuildingBlock moleculeBuildingBlock);
InitialConditionsBuildingBlock CreateFrom(SpatialStructure spatialStructure, IReadOnlyList<MoleculeBuilder> molecules);

/// <summary>
/// Creates an initial condition
Expand Down Expand Up @@ -42,27 +42,26 @@ public InitialConditionsCreator(
_idGenerator = idGenerator;
}

public InitialConditionsBuildingBlock CreateFrom(SpatialStructure spatialStructure, MoleculeBuildingBlock moleculeBuildingBlock)
public InitialConditionsBuildingBlock CreateFrom(SpatialStructure spatialStructure, IReadOnlyList<MoleculeBuilder> molecules)
{
var initialConditions = _objectBaseFactory.Create<InitialConditionsBuildingBlock>().WithName(DefaultNames.InitialConditions);
initialConditions.SpatialStructureId = spatialStructure.Id;
initialConditions.MoleculeBuildingBlockId = moleculeBuildingBlock.Id;

foreach (var container in spatialStructure.PhysicalContainers)
{
addMoleculesFrom(initialConditions, container, moleculeBuildingBlock);
addMoleculesFrom(initialConditions, container, molecules);
}

return initialConditions;
}

private void addMoleculesFrom(InitialConditionsBuildingBlock moleculesStartValuesBuildingBlock, IEntity container, IEnumerable<MoleculeBuilder> molecules)
private void addMoleculesFrom(InitialConditionsBuildingBlock initialConditionsBuildingBlock, IEntity container, IEnumerable<MoleculeBuilder> molecules)
{
foreach (var molecule in molecules)
{
var initialCondition = CreateInitialCondition(_objectPathFactory.CreateAbsoluteObjectPath(container), molecule.Name, molecule.Dimension, molecule.DisplayUnit);
setInitialCondition(molecule, initialCondition);
setInitialConditionFormula(molecule.DefaultStartFormula, initialCondition, moleculesStartValuesBuildingBlock);
moleculesStartValuesBuildingBlock.Add(initialCondition);
setInitialConditionFormula(molecule.DefaultStartFormula, initialCondition, initialConditionsBuildingBlock);
initialConditionsBuildingBlock.Add(initialCondition);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public abstract class PathAndValueEntityBuildingBlockXmlSerializer<TBuildingBloc
{
}

public class PathAndValueEntityBuildingBlockFromPKSimXmlSerializer<TBuildingBlock, TBuilder> : PathAndValueEntityBuildingBlockXmlSerializer<TBuildingBlock, TBuilder>
where TBuilder : PathAndValueEntity
public class PathAndValueEntityBuildingBlockFromPKSimXmlSerializer<TBuildingBlock, TBuilder> : PathAndValueEntityBuildingBlockXmlSerializer<TBuildingBlock, TBuilder>
where TBuilder : PathAndValueEntity
where TBuildingBlock : PathAndValueEntityBuildingBlockFromPKSim<TBuilder>
{
public override void PerformMapping()
Expand All @@ -118,17 +118,12 @@ public override void PerformMapping()
{
base.PerformMapping();
Map(x => x.Type);
MapEnumerable(x => x.InitialConditions, x => x.AddInitialCondition);
}
}

public class InitialConditionsBuildingBlockXmlSerializer : PathAndValueEntityBuildingBlockXmlSerializer<InitialConditionsBuildingBlock, InitialCondition>
{
public override void PerformMapping()
{
base.PerformMapping();
Map(x => x.SpatialStructureId);
Map(x => x.MoleculeBuildingBlockId);
}
}

public class ParameterValuesBuildingBlockXmlSerializer : PathAndValueEntityBuildingBlockXmlSerializer<ParameterValuesBuildingBlock, ParameterValue>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using FakeItEasy;
using OSPSuite.BDDHelper;
using OSPSuite.BDDHelper.Extensions;
using OSPSuite.Core.Domain.Builder;
Expand Down Expand Up @@ -38,6 +39,12 @@ protected override void Context()
_expressionProfileBuildingBlock.Type = ExpressionTypes.MetabolizingEnzyme;
_expressionProfileBuildingBlock.PKSimVersion = "11.1";
_expressionProfileBuildingBlock.Add(new ExpressionParameter().WithName("name1"));
var initialCondition = new InitialCondition().WithName("ic1");
initialCondition.Path = new ObjectPath("path1");
_expressionProfileBuildingBlock.AddInitialCondition(initialCondition);

var clonedInitialCondition = new InitialCondition().WithName(initialCondition.Name);
clonedInitialCondition.Path = initialCondition.Path;
}

protected override void Because()
Expand All @@ -52,6 +59,7 @@ public void the_updated_expression_profile_should_have_properties_set()
sut.Type.ShouldBeEqualTo(ExpressionTypes.MetabolizingEnzyme);
sut.PKSimVersion.ShouldBeEqualTo("11.1");
sut.Count().ShouldBeEqualTo(1);
sut.InitialConditions.Count().ShouldBeEqualTo(1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,83 +16,6 @@ protected override void Context()
}
}

internal class when_checking_if_a_building_block_is_referenced_by_the_initial_conditions_building_block_without_a_reference: concern_for_InitialConditionsBuildingBlock
{
private IBuildingBlock _buildingBlock;
private bool _result;

protected override void Context()
{
base.Context();
_buildingBlock = A.Fake<IBuildingBlock>();

A.CallTo(() => _buildingBlock.Id).Returns("id");
}

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

[Observation]
public void result_should_show_that_building_block_is_used()
{
_result.ShouldBeFalse();
}
}

internal class when_checking_if_a_molecule_building_block_is_referenced_by_the_initial_conditions_building_block : concern_for_InitialConditionsBuildingBlock
{
private IBuildingBlock _buildingBlock;
private bool _result;

protected override void Context()
{
base.Context();
sut.MoleculeBuildingBlockId = "id";
_buildingBlock = A.Fake<IBuildingBlock>();

A.CallTo(() => _buildingBlock.Id).Returns("id");
}

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

[Observation]
public void result_should_show_that_building_block_is_used()
{
_result.ShouldBeTrue();
}
}

internal class when_checking_if_a_spatial_structure_is_referenced_by_the_initial_conditions_building_block : concern_for_InitialConditionsBuildingBlock
{
private IBuildingBlock _buildingBlock;
private bool _result;

protected override void Context()
{
base.Context();
sut.SpatialStructureId = "id";
_buildingBlock = A.Fake<IBuildingBlock>();

A.CallTo(() => _buildingBlock.Id).Returns("id");
}

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

[Observation]
public void result_should_show_that_building_block_is_used()
{
_result.ShouldBeTrue();
}
}

internal class when_adding_initial_conditions_twice : concern_for_InitialConditionsBuildingBlock
{
private IFormula _formula;
Expand All @@ -119,6 +42,7 @@ public void the_cache_should_only_contain_one_formula()
internal class when_adding_initial_conditions_with_matching_id_to_building_block : concern_for_InitialConditionsBuildingBlock
{
private IFormula _formula, _addedFormula;

protected override void Context()
{
base.Context();
Expand Down Expand Up @@ -152,14 +76,13 @@ protected override void Context()

protected override void Because()
{
sut.Add(new InitialCondition {Formula = _formula});
sut.Add(new InitialCondition { Formula = _formula });
}

[Observation]
public void should_not_add_formula_cache_to_building_block()
{
sut.FormulaCache.Any().ShouldBeFalse();
}

}
}
}
Loading