From 4171cd27b2f2ee11736754cd24b26455f1377e7c Mon Sep 17 00:00:00 2001 From: Michael Sevestre Date: Fri, 10 Mar 2023 15:09:23 -0500 Subject: [PATCH] Fixes #1920 create neighborhood builder with path instead of container: (#1922) * Fixes #1920 create neighborhood builder with path instead of container: * Fixes #1920 create neighborhood builder with path instead of container * add icons * add icons --- .../ApplicationIcons.cs | 1 + src/OSPSuite.Assets/UIConstants.cs | 4 +- .../Comparison/NeighborhoodBaseDiffBuilder.cs | 60 ++++++++++---- .../Converters/v12/Converter110To120.cs | 68 ++++++++++++++++ .../Domain/Builder/NeighborhoodBuilder.cs | 58 +++++++++----- .../Builder/NeighborhoodBuilderFactory.cs | 29 +++---- .../Domain/Builder/SpatialStructure.cs | 80 +++++-------------- src/OSPSuite.Core/Domain/Container.cs | 8 +- .../Domain/ContainerExtensions.cs | 4 +- src/OSPSuite.Core/Domain/Exceptions.cs | 2 +- src/OSPSuite.Core/Domain/INeighborhoodBase.cs | 15 ---- ...NeighborhoodBuilderToNeighborhoodMapper.cs | 70 ++++++++-------- ...NeighborhoodCollectionToContainerMapper.cs | 18 ++--- src/OSPSuite.Core/Domain/Neighborhood.cs | 60 ++++++-------- .../Domain/NeighborhoodBaseExtensions.cs | 18 ----- .../Domain/NeighborhoodExtensions.cs | 18 +++++ .../Domain/QuantityAndContainer.cs | 4 +- .../Domain/Services/CalculationMethodTask.cs | 11 ++- .../Domain/Services/ContainerTask.cs | 31 +++---- .../Domain/Services/FormulaTask.cs | 2 +- .../Domain/Services/KeywordReplacerTask.cs | 22 ++--- .../Domain/Services/ModelConstructor.cs | 2 +- .../Domain/Services/ModelFinalizer.cs | 12 +-- .../Domain/Services/ModelReportCreator.cs | 6 +- .../MoleculePropertiesContainerTask.cs | 8 +- .../Domain/Services/TransportCreator.cs | 24 +++--- src/OSPSuite.Core/OSPSuite.Core.csproj | 1 + .../Serialization/PKMLVersion.cs | 3 +- .../Xml/NeighborhoodBuilderXmlSerializer.cs | 5 ++ .../Xml/NeighborhoodXmlSerializer.cs | 2 +- .../Diagram/BaseDiagramManager.cs | 1 + .../NeighborhoodBuilderDiffBuilderSpecs.cs | 42 ++++++---- .../Helpers/AssertForSpecs.cs | 16 ++-- .../ModelConstructorIntegrationTests.cs | 8 +- .../Serializers/ModelXmlSerializerSpecs.cs | 2 +- .../NeighborhoodBuilderXmlSerializerSpecs.cs | 6 +- .../NeighborhoodXmlSerializerSpecs.cs | 6 +- .../SpatialStructureSpecs.cs | 4 +- .../Domain/ContainerTaskSpecs.cs | 55 +++++++------ .../Domain/NeighborhoodBuilderFactorySpecs.cs | 32 ++++---- .../Domain/NeighborhoodExtensionsSpecs.cs | 10 +-- .../Domain/NeighborhoodSpecs.cs | 78 +++++++++--------- ...borhoodBuilderToNeighborhoodMapperSpecs.cs | 43 +++++----- .../TestObjectBaseFactory.cs | 2 +- 44 files changed, 503 insertions(+), 448 deletions(-) create mode 100644 src/OSPSuite.Core/Converters/v12/Converter110To120.cs delete mode 100644 src/OSPSuite.Core/Domain/INeighborhoodBase.cs delete mode 100644 src/OSPSuite.Core/Domain/NeighborhoodBaseExtensions.cs create mode 100644 src/OSPSuite.Core/Domain/NeighborhoodExtensions.cs diff --git a/src/OSPSuite.Assets.Images/ApplicationIcons.cs b/src/OSPSuite.Assets.Images/ApplicationIcons.cs index 9517e947b..f48d8e919 100644 --- a/src/OSPSuite.Assets.Images/ApplicationIcons.cs +++ b/src/OSPSuite.Assets.Images/ApplicationIcons.cs @@ -497,6 +497,7 @@ public static class ApplicationIcons public static readonly ApplicationIcon PKSimModulesFolder = AddNamedIcon("PKSimModulesFolder"); // TODO add icon svg file. Presently just a copy of Folder svg public static readonly ApplicationIcon ExtensionModulesFolder = AddNamedIcon("ExtensionModulesFolder"); // TODO add icon svg file. Presently just a copy of Folder svg public static readonly ApplicationIcon Module = AddNamedIcon("Module"); // TODO add icon svg file. Presently just a copy of BBExplorer svg + public static readonly ApplicationIcon Neighborhood = AddNamedIcon("OSPSuite", "Neighborhood"); // TODO add icon svg file. Presently just a copy of BBExplorer svg // All icons should go at the end of the preceding list, before this delimiting icon - EmptyIcon private static ApplicationIcon createEmptyIcon() => new ApplicationIcon((SvgImage) null); diff --git a/src/OSPSuite.Assets/UIConstants.cs b/src/OSPSuite.Assets/UIConstants.cs index 1b62ec0e7..b7fdc852b 100644 --- a/src/OSPSuite.Assets/UIConstants.cs +++ b/src/OSPSuite.Assets/UIConstants.cs @@ -2267,7 +2267,7 @@ public static class ObjectTypes public static readonly string Formula = "Formula"; public static readonly string Simulation = "Simulation"; public static readonly string ExplicitFormula = "Formula"; - public static readonly string NeighborhoodBuilder = "Neighborhood"; + public static readonly string Neighborhood = "Neighborhood"; public static readonly string PassiveTransportBuilder = "Passive Transport"; public static readonly string ActiveTransportBuilder = "Active Transport"; public static readonly string TransportBuilder = "Transport"; @@ -2286,7 +2286,7 @@ public static class ObjectTypes public static readonly string SumFormula = "Sum Formula"; public static readonly string Quantities = "Quantities"; public static readonly string Model = "Model"; - public static readonly string MoleculeList = "Moleculelist"; + public static readonly string MoleculeList = "Molecule List"; public static readonly string SolverProperty = "Solver Property"; public static readonly string SimulationSettings = "Simulation Settings"; public static readonly string OutputSelections = "Output Selections"; diff --git a/src/OSPSuite.Core/Comparison/NeighborhoodBaseDiffBuilder.cs b/src/OSPSuite.Core/Comparison/NeighborhoodBaseDiffBuilder.cs index 36bfcd157..d564cb2e6 100644 --- a/src/OSPSuite.Core/Comparison/NeighborhoodBaseDiffBuilder.cs +++ b/src/OSPSuite.Core/Comparison/NeighborhoodBaseDiffBuilder.cs @@ -1,45 +1,75 @@ using OSPSuite.Assets; using OSPSuite.Core.Domain; +using OSPSuite.Core.Domain.Builder; using OSPSuite.Core.Domain.Services; namespace OSPSuite.Core.Comparison { - public class NeighborhoodBaseDiffBuilder : DiffBuilder + public abstract class NeighborhoodBaseDiffBuilder : DiffBuilder where TNeighborhood : class, IContainer { private readonly ContainerDiffBuilder _containerDiffBuilder; - private readonly IEntityPathResolver _entityPathResolver; - public NeighborhoodBaseDiffBuilder(ContainerDiffBuilder containerDiffBuilder, IEntityPathResolver entityPathResolver) + protected NeighborhoodBaseDiffBuilder(ContainerDiffBuilder containerDiffBuilder) { _containerDiffBuilder = containerDiffBuilder; - _entityPathResolver = entityPathResolver; } - public override void Compare(IComparison comparison) + public override void Compare(IComparison comparison) { _containerDiffBuilder.Compare(comparison); compareNeighbors(comparison); } - private void compareNeighbors(IComparison comparison) + private void compareNeighbors(IComparison comparison) { - var firstNeigbor1Path = _entityPathResolver.PathFor(comparison.Object1.FirstNeighbor); - var secondNeigbor1Path = _entityPathResolver.PathFor(comparison.Object1.SecondNeighbor); - var firstNeigbor2Path = _entityPathResolver.PathFor(comparison.Object2.FirstNeighbor); - var secondNeigbor2Path = _entityPathResolver.PathFor(comparison.Object2.SecondNeighbor); + var (firstNeighbor1Path, secondNeighbor1Path, firstNeighbor2Path, secondNeighbor2Path) = GetNeighborsComparisonPath(comparison); - if (firstNeigbor1Path.Equals(firstNeigbor2Path) && secondNeigbor1Path.Equals(secondNeigbor2Path) || - firstNeigbor1Path.Equals(secondNeigbor2Path) && secondNeigbor1Path.Equals(firstNeigbor2Path)) + if (firstNeighbor1Path.Equals(firstNeighbor2Path) && secondNeighbor1Path.Equals(secondNeighbor2Path) || + firstNeighbor1Path.Equals(secondNeighbor2Path) && secondNeighbor1Path.Equals(firstNeighbor2Path)) return; comparison.Add(new PropertyValueDiffItem { CommonAncestor = comparison.Object1, - FormattedValue1 = Captions.Diff.ConnectionBetween(firstNeigbor1Path, secondNeigbor1Path), - FormattedValue2 = Captions.Diff.ConnectionBetween(firstNeigbor2Path, secondNeigbor2Path), + FormattedValue1 = Captions.Diff.ConnectionBetween(firstNeighbor1Path, secondNeighbor1Path), + FormattedValue2 = Captions.Diff.ConnectionBetween(firstNeighbor2Path, secondNeighbor2Path), PropertyName = Captions.Diff.Connection, - Description = Captions.Diff.PropertyDiffers(Captions.Diff.Connection, Captions.Diff.ConnectionBetween(firstNeigbor1Path, secondNeigbor1Path), Captions.Diff.ConnectionBetween(firstNeigbor2Path, secondNeigbor2Path)) + Description = Captions.Diff.PropertyDiffers(Captions.Diff.Connection, Captions.Diff.ConnectionBetween(firstNeighbor1Path, secondNeighbor1Path), Captions.Diff.ConnectionBetween(firstNeighbor2Path, secondNeighbor2Path)) }); } + + protected abstract (ObjectPath firstNeighbor1Path, ObjectPath secondNeighbor1Path, ObjectPath firstNeighbor2Path, ObjectPath secondNeighbor2Path) GetNeighborsComparisonPath(IComparison comparison); + } + + public class NeighborhoodBuilderDiffBuilder : NeighborhoodBaseDiffBuilder + { + public NeighborhoodBuilderDiffBuilder(ContainerDiffBuilder containerDiffBuilder) : base(containerDiffBuilder) + { + } + + protected override (ObjectPath firstNeighbor1Path, ObjectPath secondNeighbor1Path, ObjectPath firstNeighbor2Path, ObjectPath secondNeighbor2Path) GetNeighborsComparisonPath(IComparison comparison) + { + return (comparison.Object1.FirstNeighborPath, comparison.Object1.SecondNeighborPath, comparison.Object2.FirstNeighborPath, comparison.Object2.SecondNeighborPath); + } + } + + public class NeighborhoodDiffBuilder : NeighborhoodBaseDiffBuilder + { + private readonly IEntityPathResolver _entityPathResolver; + + public NeighborhoodDiffBuilder(ContainerDiffBuilder containerDiffBuilder, IEntityPathResolver entityPathResolver) : base(containerDiffBuilder) + { + _entityPathResolver = entityPathResolver; + } + + protected override (ObjectPath firstNeighbor1Path, ObjectPath secondNeighbor1Path, ObjectPath firstNeighbor2Path, ObjectPath secondNeighbor2Path) GetNeighborsComparisonPath(IComparison comparison) + { + var firstNeighbor1Path = _entityPathResolver.ObjectPathFor(comparison.Object1.FirstNeighbor); + var secondNeighbor1Path = _entityPathResolver.ObjectPathFor(comparison.Object1.SecondNeighbor); + var firstNeighbor2Path = _entityPathResolver.ObjectPathFor(comparison.Object2.FirstNeighbor); + var secondNeighbor2Path = _entityPathResolver.ObjectPathFor(comparison.Object2.SecondNeighbor); + + return (firstNeighbor1Path, secondNeighbor1Path, firstNeighbor2Path, secondNeighbor2Path); + } } } \ No newline at end of file diff --git a/src/OSPSuite.Core/Converters/v12/Converter110To120.cs b/src/OSPSuite.Core/Converters/v12/Converter110To120.cs new file mode 100644 index 000000000..aaec81660 --- /dev/null +++ b/src/OSPSuite.Core/Converters/v12/Converter110To120.cs @@ -0,0 +1,68 @@ +using System.Xml.Linq; +using OSPSuite.Core.Domain; +using OSPSuite.Core.Domain.Builder; +using OSPSuite.Core.Serialization; +using OSPSuite.Core.Serialization.Exchange; +using OSPSuite.Utility.Extensions; +using OSPSuite.Utility.Visitor; + +namespace OSPSuite.Core.Converters.v12 +{ + public class Converter110To120 : IObjectConverter, + IVisitor, + IVisitor, + IVisitor, + IVisitor + { + private readonly IObjectPathFactory _objectPathFactory; + private bool _converted; + + public Converter110To120(IObjectPathFactory objectPathFactory) + { + _objectPathFactory = objectPathFactory; + } + + public bool IsSatisfiedBy(int version) => version == PKMLVersion.V11_0; + + public (int convertedToVersion, bool conversionHappened) Convert(object objectToUpdate) + { + _converted = false; + performConversion(objectToUpdate); + return (PKMLVersion.V12_0, _converted); + } + + public (int convertedToVersion, bool conversionHappened) ConvertXml(XElement element) + { + return (PKMLVersion.V12_0, false); + } + + private void performConversion(object objectToUpdate) => this.Visit(objectToUpdate); + + public void Visit(ISpatialStructure spatialStructure) + { + spatialStructure.Neighborhoods.Each(updateNeighborsPathIn); + } + + private void updateNeighborsPathIn(NeighborhoodBuilder neighborhoodBuilder) + { + neighborhoodBuilder.FirstNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(neighborhoodBuilder.FirstNeighbor); + neighborhoodBuilder.SecondNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(neighborhoodBuilder.SecondNeighbor); + _converted = true; + } + + public void Visit(SimulationTransfer simulationTransfer) + { + Visit(simulationTransfer.Simulation); + } + + public void Visit(IModelCoreSimulation modelCoreSimulation) + { + Visit(modelCoreSimulation.BuildConfiguration); + } + + public void Visit(IBuildConfiguration buildConfiguration) + { + Visit(buildConfiguration.SpatialStructure); + } + } +} \ No newline at end of file diff --git a/src/OSPSuite.Core/Domain/Builder/NeighborhoodBuilder.cs b/src/OSPSuite.Core/Domain/Builder/NeighborhoodBuilder.cs index 927d8f9c3..1d3bd69b6 100644 --- a/src/OSPSuite.Core/Domain/Builder/NeighborhoodBuilder.cs +++ b/src/OSPSuite.Core/Domain/Builder/NeighborhoodBuilder.cs @@ -1,49 +1,65 @@ using System.Collections.Generic; +using OSPSuite.Core.Domain.Services; namespace OSPSuite.Core.Domain.Builder { - public interface INeighborhoodBuilder : INeighborhoodBase + public interface INeighborhoodBase : IContainer { - IContainer MoleculeProperties { get; } - IEnumerable Parameters { get; } - void AddParameter(IParameter newParameter); - void RemoveParameter(IParameter parameterToRemove); - bool IsConnectedTo(IContainer container); + /// + /// First neighbor in the neighborhood. + /// + IContainer FirstNeighbor { get; } + + /// + /// Second neighbor in the neighborhood. + /// + IContainer SecondNeighbor { get; } } - public class NeighborhoodBuilder : Container, INeighborhoodBuilder + public class NeighborhoodBuilder : Container, INeighborhoodBase { + //We define a property set for the first neighbor only to be compatible with serialization prior to v12 public IContainer FirstNeighbor { get; set; } + + //We define a property set for the second neighbor only to be compatible with serialization prior to v12 public IContainer SecondNeighbor { get; set; } + public ObjectPath FirstNeighborPath { get; set; } + public ObjectPath SecondNeighborPath { get; set; } + public NeighborhoodBuilder() { ContainerType = ContainerType.Neighborhood; } - public IContainer MoleculeProperties - { - get { return this.GetSingleChildByName(Constants.MOLECULE_PROPERTIES); } - } + public IContainer MoleculeProperties => this.Container(Constants.MOLECULE_PROPERTIES); - public IEnumerable Parameters - { - get { return GetChildren(); } - } + public IEnumerable Parameters => GetChildren(); + + public void AddParameter(IParameter newParameter) => Add(newParameter); - public void AddParameter(IParameter newParameter) + public void RemoveParameter(IParameter parameterToRemove) => RemoveChild(parameterToRemove); + + public bool IsConnectedTo(ObjectPath containerPath) { - Add(newParameter); + return Equals(FirstNeighborPath, containerPath) || Equals(SecondNeighborPath, containerPath); } - public void RemoveParameter(IParameter parameterToRemove) + public void ResolveReference(IContainer container) { - RemoveChild(parameterToRemove); + FirstNeighbor = FirstNeighborPath.Resolve(container); + SecondNeighbor = SecondNeighborPath.Resolve(container); } - public bool IsConnectedTo(IContainer container) + public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager cloneManager) { - return Equals(FirstNeighbor, container) || Equals(SecondNeighbor, container); + base.UpdatePropertiesFrom(source, cloneManager); + var sourceNeighborhood = source as NeighborhoodBuilder; + if (sourceNeighborhood == null) + return; + + FirstNeighborPath = sourceNeighborhood.FirstNeighborPath; + SecondNeighborPath = sourceNeighborhood.SecondNeighborPath; } } } \ No newline at end of file diff --git a/src/OSPSuite.Core/Domain/Builder/NeighborhoodBuilderFactory.cs b/src/OSPSuite.Core/Domain/Builder/NeighborhoodBuilderFactory.cs index 815e551e7..f23b036f6 100644 --- a/src/OSPSuite.Core/Domain/Builder/NeighborhoodBuilderFactory.cs +++ b/src/OSPSuite.Core/Domain/Builder/NeighborhoodBuilderFactory.cs @@ -9,14 +9,14 @@ public interface INeighborhoodBuilderFactory /// Creates a new instance of a . /// /// the new - INeighborhoodBuilder Create(); + NeighborhoodBuilder Create(); /// /// Creates a new between and . /// /// The first neighbor. /// The second neighbor. /// the new - INeighborhoodBuilder CreateBetween(IContainer firstNeighbor, IContainer secondNeighbor); + NeighborhoodBuilder CreateBetween(IContainer firstNeighbor, IContainer secondNeighbor); } /// @@ -25,35 +25,36 @@ public interface INeighborhoodBuilderFactory public class NeighborhoodBuilderFactory : INeighborhoodBuilderFactory { private readonly IObjectBaseFactory _objectBaseFactory; + private readonly IObjectPathFactory _objectPathFactory; /// /// Initializes a new instance of the class. /// - /// The object base factory used to create new objects. - public NeighborhoodBuilderFactory(IObjectBaseFactory objectBaseFactory) + public NeighborhoodBuilderFactory(IObjectBaseFactory objectBaseFactory, IObjectPathFactory objectPathFactory) { _objectBaseFactory = objectBaseFactory; + _objectPathFactory = objectPathFactory; } /// /// Creates a new instance of a . /// /// the new - public INeighborhoodBuilder Create() + public NeighborhoodBuilder Create() { var neighborhoodBuilder = CreateNeighborhoodBuilder().WithMode(ContainerMode.Logical); - var moelculePropertiers = CreateMoleculeProperties() + var moleculeProperties = CreateMoleculeProperties() .WithName(Constants.MOLECULE_PROPERTIES) .WithMode(ContainerMode.Logical); - neighborhoodBuilder.Add(moelculePropertiers); + neighborhoodBuilder.Add(moleculeProperties); return neighborhoodBuilder; } - protected INeighborhoodBuilder CreateNeighborhoodBuilder() + protected NeighborhoodBuilder CreateNeighborhoodBuilder() { - return _objectBaseFactory.Create(); + return _objectBaseFactory.Create(); } protected IContainer CreateMoleculeProperties() @@ -67,12 +68,12 @@ protected IContainer CreateMoleculeProperties() /// The first neighbor. /// The second neighbor. /// the new - public INeighborhoodBuilder CreateBetween(IContainer firstNeighbor, IContainer secondNeighbor) + public NeighborhoodBuilder CreateBetween(IContainer firstNeighbor, IContainer secondNeighbor) { - var neighborhoohBuilder = Create(); - neighborhoohBuilder.FirstNeighbor = firstNeighbor; - neighborhoohBuilder.SecondNeighbor = secondNeighbor; - return neighborhoohBuilder; + var neighborhoodBuilder = Create(); + neighborhoodBuilder.FirstNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(firstNeighbor); + neighborhoodBuilder.SecondNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(secondNeighbor); + return neighborhoodBuilder; } } } \ No newline at end of file diff --git a/src/OSPSuite.Core/Domain/Builder/SpatialStructure.cs b/src/OSPSuite.Core/Domain/Builder/SpatialStructure.cs index effe74a87..abdb3ab75 100644 --- a/src/OSPSuite.Core/Domain/Builder/SpatialStructure.cs +++ b/src/OSPSuite.Core/Domain/Builder/SpatialStructure.cs @@ -1,35 +1,36 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using OSPSuite.Assets; +using OSPSuite.Core.Domain.Services; using OSPSuite.Utility.Extensions; using OSPSuite.Utility.Visitor; -using OSPSuite.Core.Domain.Services; namespace OSPSuite.Core.Domain.Builder { public interface ISpatialStructure : IBuildingBlock { /// - /// Container-structure with subcontainers and parameters (e.g. one container but for special modelling situation like + /// Container-structure with subcontainers and parameters (e.g. one container but for special modeling situation like /// mother/featus model, 2 root containers would be used) /// - IEnumerable TopContainers { get; } + IReadOnlyList TopContainers { get; } /// - /// Add container in the top hiearchy level of the spatial structure + /// Add container in the top hierarchy level of the spatial structure /// void AddTopContainer(IContainer container); /// - /// Remove container from the top hiearchy level of the spatial structure + /// Remove container from the top hierarchy level of the spatial structure /// void RemoveTopContainer(IContainer container); /// /// All neighborhoods defined for the spatial structure /// - IEnumerable Neighborhoods { get; } + IReadOnlyList Neighborhoods { get; } /// /// Container which contains all NeighborhoodBuilder @@ -39,9 +40,9 @@ public interface ISpatialStructure : IBuildingBlock /// /// Add neighborhood to spatial structure /// - void AddNeighborhood(INeighborhoodBuilder neighborhoodBuilder); + void AddNeighborhood(NeighborhoodBuilder neighborhoodBuilder); - void RemoveNeighborhood(INeighborhoodBuilder neighborhoodBuilder); + void RemoveNeighborhood(NeighborhoodBuilder neighborhoodBuilder); /// /// Molecule-dependent properties, which must be defined only @@ -56,11 +57,13 @@ public interface ISpatialStructure : IBuildingBlock /// Returns all physical containers starting at "Root" /// IEnumerable PhysicalContainers { get; } + + IReadOnlyList AllNeighborhoodBuildersConnectedWith(ObjectPath containerPath); } public class SpatialStructure : BuildingBlock, ISpatialStructure { - private readonly IList _allTopContainers; + private readonly List _allTopContainers; public IContainer GlobalMoleculeDependentProperties { get; set; } public IContainer NeighborhoodsContainer { get; set; } @@ -70,10 +73,7 @@ public SpatialStructure() _allTopContainers = new List(); } - public IEnumerable TopContainers - { - get { return _allTopContainers; } - } + public IReadOnlyList TopContainers => _allTopContainers; public void AddTopContainer(IContainer container) { @@ -87,23 +87,23 @@ public void RemoveTopContainer(IContainer container) _allTopContainers.Remove(container); } - public IEnumerable Neighborhoods + public IReadOnlyList Neighborhoods { get { if (NeighborhoodsContainer == null) - return Enumerable.Empty(); + return Array.Empty(); - return NeighborhoodsContainer.GetChildren(); + return NeighborhoodsContainer.GetChildren().ToList(); } } - public void AddNeighborhood(INeighborhoodBuilder neighborhoodBuilder) + public void AddNeighborhood(NeighborhoodBuilder neighborhoodBuilder) { NeighborhoodsContainer.Add(neighborhoodBuilder); } - public void RemoveNeighborhood(INeighborhoodBuilder neighborhoodBuilder) + public void RemoveNeighborhood(NeighborhoodBuilder neighborhoodBuilder) { NeighborhoodsContainer.RemoveChild(neighborhoodBuilder); } @@ -124,6 +124,8 @@ from physicalContainer in root.GetAllChildren(c => c.Mode == Contain } } + public IReadOnlyList AllNeighborhoodBuildersConnectedWith(ObjectPath containerPath) => Neighborhoods.Where(x => x.IsConnectedTo(containerPath)).ToList(); + public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager cloneManager) { base.UpdatePropertiesFrom(source, cloneManager); @@ -133,12 +135,6 @@ public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager clone sourceSpatialStructure.TopContainers.Each(c => AddTopContainer(cloneManager.Clone(c))); NeighborhoodsContainer = cloneManager.Clone(sourceSpatialStructure.NeighborhoodsContainer); - foreach (var neighborhoodBuilder in Neighborhoods) - { - var sourceNeighborhood = neighborhoodBuilderSourceFor(neighborhoodBuilder, sourceSpatialStructure.Neighborhoods); - updateNeighborsReferences(sourceNeighborhood, neighborhoodBuilder); - } - GlobalMoleculeDependentProperties = cloneManager.Clone(sourceSpatialStructure.GlobalMoleculeDependentProperties); } @@ -154,40 +150,6 @@ public override void AcceptVisitor(IVisitor visitor) NeighborhoodsContainer.AcceptVisitor(visitor); } - /// - /// Copy the references for first and second Neighbor from to - /// . - /// - /// The source neighborhood builder to copy neighbors from. - /// The neighborhood builder to copy neighbors to. - private void updateNeighborsReferences(INeighborhoodBuilder sourceNeighborhoodBuilder, INeighborhoodBuilder targetNeighborhoodBuilder) - { - var objectPathFactory = new ObjectPathFactory(new AliasCreator()); - var firstPath = objectPathFactory.CreateAbsoluteObjectPath(sourceNeighborhoodBuilder.FirstNeighbor); - var secondPath = objectPathFactory.CreateAbsoluteObjectPath(sourceNeighborhoodBuilder.SecondNeighbor); - - foreach (var topContainer in TopContainers) - { - var firstNeighbor = firstPath.Resolve(topContainer); - var secondNeighbor = secondPath.Resolve(topContainer); - - if (firstNeighbor != null) - targetNeighborhoodBuilder.FirstNeighbor = firstNeighbor; - - if (secondNeighbor != null) - targetNeighborhoodBuilder.SecondNeighbor = secondNeighbor; - - //both neighbors set. early exit - if (targetNeighborhoodBuilder.FirstNeighbor != null && targetNeighborhoodBuilder.SecondNeighbor != null) - return; - } - } - - private INeighborhoodBuilder neighborhoodBuilderSourceFor(INeighborhoodBuilder neighborhoodBuilder, IEnumerable sourceNeighborhoods) - { - return sourceNeighborhoods.FindByName(neighborhoodBuilder.Name); - } - public IEnumerator GetEnumerator() { foreach (var topContainer in TopContainers) diff --git a/src/OSPSuite.Core/Domain/Container.cs b/src/OSPSuite.Core/Domain/Container.cs index 438d780ef..a3973adc8 100644 --- a/src/OSPSuite.Core/Domain/Container.cs +++ b/src/OSPSuite.Core/Domain/Container.cs @@ -71,13 +71,13 @@ public interface IContainer : IEntity, IEnumerable /// /// returns the neighbors from the container connected by the given neighborhoods. /// - IReadOnlyList GetNeighborsFrom(IReadOnlyList neighborhoods); + IReadOnlyList GetNeighborsFrom(IReadOnlyList neighborhoods); /// /// returns the neighborhoods connecting the container with its neighbors. /// /// The possible neighborhoods. - IReadOnlyList GetNeighborhoods(IReadOnlyList neighborhoods); + IReadOnlyList GetNeighborhoods(IReadOnlyList neighborhoods); /// /// Returns all children containers defined and the container itself, if the container is form type @@ -211,7 +211,7 @@ where predicate(castChild) select castChild; } - public virtual IReadOnlyList GetNeighborsFrom(IReadOnlyList neighborhoods) + public virtual IReadOnlyList GetNeighborsFrom(IReadOnlyList neighborhoods) { var first = from neighborhood in GetNeighborhoods(neighborhoods) where neighborhood.FirstNeighbor != this @@ -224,7 +224,7 @@ public virtual IReadOnlyList GetNeighborsFrom(IReadOnlyList GetNeighborhoods(IReadOnlyList neighborhoods) + public virtual IReadOnlyList GetNeighborhoods(IReadOnlyList neighborhoods) { var first = from neighborhood in neighborhoods where neighborhood.FirstNeighbor == this diff --git a/src/OSPSuite.Core/Domain/ContainerExtensions.cs b/src/OSPSuite.Core/Domain/ContainerExtensions.cs index b6b3a33eb..da80fb7db 100644 --- a/src/OSPSuite.Core/Domain/ContainerExtensions.cs +++ b/src/OSPSuite.Core/Domain/ContainerExtensions.cs @@ -58,9 +58,9 @@ public static TContainer WithContainerType(this TContainer container /// parent neighborhood container /// criteria that should be met by the first container of the neighborhood /// criteria that should be met by the second container of the neighborhood - internal static IEnumerable AllNeighborhoodsFor(this IContainer neighborhood, DescriptorCriteria criteriaForFirstContainer, DescriptorCriteria criteriaForSecondContainer) + internal static IEnumerable AllNeighborhoodsFor(this IContainer neighborhood, DescriptorCriteria criteriaForFirstContainer, DescriptorCriteria criteriaForSecondContainer) { - return neighborhood.GetChildren(n => n.StrictlySatisfies(criteriaForFirstContainer, criteriaForSecondContainer)); + return neighborhood.GetChildren(n => n.StrictlySatisfies(criteriaForFirstContainer, criteriaForSecondContainer)); } /// diff --git a/src/OSPSuite.Core/Domain/Exceptions.cs b/src/OSPSuite.Core/Domain/Exceptions.cs index 636d6a99e..1a67a3ded 100644 --- a/src/OSPSuite.Core/Domain/Exceptions.cs +++ b/src/OSPSuite.Core/Domain/Exceptions.cs @@ -8,7 +8,7 @@ namespace OSPSuite.Core.Domain { public class BothNeighborsSatisfyingCriteriaException : OSPSuiteException { - public BothNeighborsSatisfyingCriteriaException(INeighborhood neighborhood) : base(Error.BothNeighborsSatisfying(neighborhood.Name)) + public BothNeighborsSatisfyingCriteriaException(Neighborhood neighborhood) : base(Error.BothNeighborsSatisfying(neighborhood.Name)) { } } diff --git a/src/OSPSuite.Core/Domain/INeighborhoodBase.cs b/src/OSPSuite.Core/Domain/INeighborhoodBase.cs deleted file mode 100644 index 19f4c9747..000000000 --- a/src/OSPSuite.Core/Domain/INeighborhoodBase.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace OSPSuite.Core.Domain -{ - public interface INeighborhoodBase : IContainer - { - /// - /// First neighboor in the neighborhood. - /// - IContainer FirstNeighbor { get; set; } - - /// - /// Second neighboor in the neighborhood. - /// - IContainer SecondNeighbor { get; set; } - } -} \ No newline at end of file diff --git a/src/OSPSuite.Core/Domain/Mappers/NeighborhoodBuilderToNeighborhoodMapper.cs b/src/OSPSuite.Core/Domain/Mappers/NeighborhoodBuilderToNeighborhoodMapper.cs index be6baacbf..bed3e0cce 100644 --- a/src/OSPSuite.Core/Domain/Mappers/NeighborhoodBuilderToNeighborhoodMapper.cs +++ b/src/OSPSuite.Core/Domain/Mappers/NeighborhoodBuilderToNeighborhoodMapper.cs @@ -1,95 +1,95 @@ using System.Collections.Generic; using System.Linq; -using OSPSuite.Utility.Extensions; using OSPSuite.Core.Domain.Builder; using OSPSuite.Core.Domain.Services; +using OSPSuite.Utility.Extensions; namespace OSPSuite.Core.Domain.Mappers { /// - /// Maps neighborhood builder object into a neighborhood + /// Maps neighborhood builder object into a neighborhood /// public interface INeighborhoodBuilderToNeighborhoodMapper { /// - /// Maps neighborhood builder object into a neighborhood + /// Maps neighborhood builder object into a neighborhood /// /// Neighborhood builder to be mapped /// Model, required to link neighbor containers in model /// Build configuration - /// All molecules present in both neighbours, required to create molecule properties subcontainers + /// + /// All molecules present in both neighbors, required to create molecule properties + /// subcontainers + /// /// Molecules having CopyMoleculeDependentProperties=true /// - INeighborhood MapFrom(INeighborhoodBuilder neighborhoodBuilder, IModel model, - IBuildConfiguration buildConfiguration, IEnumerable moleculeNames, - IEnumerable moleculeNamesWithCopyPropertiesRequired); + Neighborhood MapFrom(NeighborhoodBuilder neighborhoodBuilder, IModel model, + IBuildConfiguration buildConfiguration, IEnumerable moleculeNames, + IEnumerable moleculeNamesWithCopyPropertiesRequired); } public class NeighborhoodBuilderToNeighborhoodMapper : INeighborhoodBuilderToNeighborhoodMapper { private readonly IObjectBaseFactory _objectBaseFactory; private readonly IContainerBuilderToContainerMapper _containerMapper; - private readonly IObjectPathFactory _objectPathFactory; private readonly IKeywordReplacerTask _keywordReplacerTask; private readonly ICloneManagerForModel _cloneManagerForModel; private readonly IParameterBuilderToParameterMapper _parameterMapper; public NeighborhoodBuilderToNeighborhoodMapper(IObjectBaseFactory objectBaseFactory, - IContainerBuilderToContainerMapper containerMapper, - IObjectPathFactory objectPathFactory, - IKeywordReplacerTask keywordReplacerTask, - ICloneManagerForModel cloneManagerForModel,IParameterBuilderToParameterMapper parameterMapper) + IContainerBuilderToContainerMapper containerMapper, + IKeywordReplacerTask keywordReplacerTask, + ICloneManagerForModel cloneManagerForModel, IParameterBuilderToParameterMapper parameterMapper) { _objectBaseFactory = objectBaseFactory; _containerMapper = containerMapper; - _objectPathFactory = objectPathFactory; _keywordReplacerTask = keywordReplacerTask; _cloneManagerForModel = cloneManagerForModel; _parameterMapper = parameterMapper; } - public INeighborhood MapFrom(INeighborhoodBuilder neighborhoodBuilder, IModel model, - IBuildConfiguration buildConfiguration, - IEnumerable moleculeNames, - IEnumerable moleculeNamesWithCopyPropertiesRequired) + public Neighborhood MapFrom(NeighborhoodBuilder neighborhoodBuilder, IModel model, + IBuildConfiguration buildConfiguration, + IEnumerable moleculeNames, + IEnumerable moleculeNamesWithCopyPropertiesRequired) { - var neighborhood = _objectBaseFactory.Create(); - neighborhood.UpdatePropertiesFrom(neighborhoodBuilder,_cloneManagerForModel); + var neighborhood = _objectBaseFactory.Create(); + neighborhood.UpdatePropertiesFrom(neighborhoodBuilder, _cloneManagerForModel); buildConfiguration.AddBuilderReference(neighborhood, neighborhoodBuilder); - neighborhood.FirstNeighbor = resolveReference(model, neighborhoodBuilder.FirstNeighbor); - neighborhood.SecondNeighbor = resolveReference(model, neighborhoodBuilder.SecondNeighbor); - if(neighborhoodBuilder.MoleculeProperties!=null) + neighborhood.FirstNeighbor = resolveReference(model, neighborhoodBuilder.FirstNeighborPath); + neighborhood.SecondNeighbor = resolveReference(model, neighborhoodBuilder.SecondNeighborPath); + + if (neighborhoodBuilder.MoleculeProperties != null) { moleculeNames.Each(moleculeName => neighborhood.Add( createMoleculePropertiesFor(neighborhoodBuilder, moleculeName, model.Root, buildConfiguration, moleculeNamesWithCopyPropertiesRequired))); } - //Add neighorhood parameter to the neighborhood (clone the existing parmeter) - neighborhoodBuilder.Parameters.Each(param => neighborhood.Add(_parameterMapper.MapFrom(param,buildConfiguration))); + //Add neighborhood parameter to the neighborhood (clone the existing parameter) + neighborhoodBuilder.Parameters.Each(param => neighborhood.Add(_parameterMapper.MapFrom(param, buildConfiguration))); return neighborhood; } - private IContainer resolveReference(IModel model, IContainer neighborBuilderContainer) + private IContainer resolveReference(IModel model, ObjectPath objectPath) { - var objectPathInBuilder = _objectPathFactory.CreateAbsoluteObjectPath(neighborBuilderContainer); - var objectPathInModel = _keywordReplacerTask.CreateModelPathFor(objectPathInBuilder, model.Root); + var objectPathInModel = _keywordReplacerTask.CreateModelPathFor(objectPath, model.Root); return objectPathInModel.Resolve(model.Root); } - private IContainer createMoleculePropertiesFor(INeighborhoodBuilder neighborhoodBuilder, - string moleculeName, IContainer rootContainer, - IBuildConfiguration buildConfiguration, - IEnumerable moleculeNamesWithCopyPropertiesRequired) + private IContainer createMoleculePropertiesFor(NeighborhoodBuilder neighborhoodBuilder, + string moleculeName, IContainer rootContainer, + IBuildConfiguration buildConfiguration, + IEnumerable moleculeNamesWithCopyPropertiesRequired) { //Create a new model container from the neighborhood container var moleculePropertiesContainer = _containerMapper.MapFrom(neighborhoodBuilder.MoleculeProperties, buildConfiguration); moleculePropertiesContainer.ContainerType = ContainerType.Molecule; - //Assigment molecule properties subcontainer name from + //Assignment molecule properties subcontainer name from //to concrete molecule name moleculePropertiesContainer.Name = moleculeName; - - _keywordReplacerTask.ReplaceIn(moleculePropertiesContainer,rootContainer,moleculeName); + + _keywordReplacerTask.ReplaceIn(moleculePropertiesContainer, rootContainer, moleculeName); //remove children if molecule properties should not be copied if (!moleculeNamesWithCopyPropertiesRequired.Contains(moleculeName)) @@ -97,7 +97,5 @@ private IContainer createMoleculePropertiesFor(INeighborhoodBuilder neighborhood return moleculePropertiesContainer; } - - } } \ No newline at end of file diff --git a/src/OSPSuite.Core/Domain/Mappers/NeighborhoodCollectionToContainerMapper.cs b/src/OSPSuite.Core/Domain/Mappers/NeighborhoodCollectionToContainerMapper.cs index 2dea5da18..c4aaee6dd 100644 --- a/src/OSPSuite.Core/Domain/Mappers/NeighborhoodCollectionToContainerMapper.cs +++ b/src/OSPSuite.Core/Domain/Mappers/NeighborhoodCollectionToContainerMapper.cs @@ -21,16 +21,13 @@ public class NeighborhoodCollectionToContainerMapper : INeighborhoodCollectionTo { private readonly IObjectBaseFactory _objectBaseFactory; private readonly INeighborhoodBuilderToNeighborhoodMapper _neighborhoodMapper; - private readonly IObjectPathFactory _objectPathFactory; public NeighborhoodCollectionToContainerMapper( IObjectBaseFactory objectBaseFactory, - INeighborhoodBuilderToNeighborhoodMapper neighborhoodMapper, - IObjectPathFactory objectPathFactory) + INeighborhoodBuilderToNeighborhoodMapper neighborhoodMapper) { _objectBaseFactory = objectBaseFactory; _neighborhoodMapper = neighborhoodMapper; - _objectPathFactory = objectPathFactory; } public IContainer MapFrom(IModel model, IBuildConfiguration buildConfiguration) @@ -55,17 +52,17 @@ public IContainer MapFrom(IModel model, IBuildConfiguration buildConfiguration) return neighborhoodsParentContainer; } - private ICache> presentMoleculesCachedByContainerPath(IEnumerable namesOfFloatingMolecules, IBuildConfiguration buildConfiguration) + private ICache> presentMoleculesCachedByContainerPath(IEnumerable namesOfFloatingMolecules, IBuildConfiguration buildConfiguration) { var startValues = buildConfiguration.MoleculeStartValues.Where(msv => (msv.IsPresent && namesOfFloatingMolecules.Contains(msv.MoleculeName))).ToList(); - var moleculeStartValuesPerContainer = new Cache>(); + var moleculeStartValuesPerContainer = new Cache>(); foreach (var msv in startValues) { - IList moleculeNames; + List moleculeNames; var path = msv.ContainerPath.ToString(); if (moleculeStartValuesPerContainer.Contains(path)) @@ -87,11 +84,10 @@ private ICache> presentMoleculesCachedByContainerPath(IEnu /// /// Returns molecules which will be created in both neighbors of the neighborhood /// - private IEnumerable moleculeNamesFor(INeighborhoodBuilder neighborhoodBuilder, - ICache> moleculesStartValuesForFloatingMolecules) + private IEnumerable moleculeNamesFor(NeighborhoodBuilder neighborhoodBuilder, ICache> moleculesStartValuesForFloatingMolecules) { - var pathToFirstNeighbor = _objectPathFactory.CreateAbsoluteObjectPath(neighborhoodBuilder.FirstNeighbor).ToString(); - var pathToSecondNeighbor = _objectPathFactory.CreateAbsoluteObjectPath(neighborhoodBuilder.SecondNeighbor).ToString(); + var pathToFirstNeighbor = neighborhoodBuilder.FirstNeighborPath.PathAsString; + var pathToSecondNeighbor = neighborhoodBuilder.SecondNeighborPath.PathAsString; // check if both neighbors has at least 1 molecule (if not - return empty list) if (!moleculesStartValuesForFloatingMolecules.Contains(pathToFirstNeighbor) || diff --git a/src/OSPSuite.Core/Domain/Neighborhood.cs b/src/OSPSuite.Core/Domain/Neighborhood.cs index 9757a10c7..377fc4300 100644 --- a/src/OSPSuite.Core/Domain/Neighborhood.cs +++ b/src/OSPSuite.Core/Domain/Neighborhood.cs @@ -1,41 +1,13 @@ -using OSPSuite.Core.Domain.Descriptors; +using OSPSuite.Core.Domain.Builder; +using OSPSuite.Core.Domain.Descriptors; using OSPSuite.Core.Domain.Services; namespace OSPSuite.Core.Domain { - public interface INeighborhood : INeighborhoodBase - { - /// - /// Return one neighbor in the neighborhood satisfying the criteria. - /// If both first and second neighbors fulfill the criteria, exception is thrown. - /// If none of the neighbors fulfills the criteria, null is returned - /// - /// Thrown when both Neighbors satisfy the criteria - IContainer GetNeighborSatisfying(DescriptorCriteria criteria); - - /// - /// return true either if the first neighbor satisfies the and the second neighbor satisfies the - /// or if the first neighbor satisfies the and the second neighbor satisfies the . - /// Otherwise false - /// - /// One criteria to be satisfied by at least one neighbor - /// Another criteria to be satisfied by at least one neighbor - /// - bool Satisfies(DescriptorCriteria criteriaForOneNeighbor, DescriptorCriteria criteriaForTheOtherNeighbor); - - /// - /// return true either if the first neighbor satisfies the and the second neighbor satisfies the . - /// Otherwise false - /// - /// The criteria to be satisfied by the first neighbor - /// The criteria to be satisfied by the second neighbor - /// - bool StrictlySatisfies(DescriptorCriteria criteriaFoFirstNeighbor, DescriptorCriteria criteriaForTheSecondNeighbor); - } - - public class Neighborhood : Container, INeighborhood + public class Neighborhood : Container, INeighborhoodBase { public IContainer FirstNeighbor { get; set; } + public IContainer SecondNeighbor { get; set; } public Neighborhood() @@ -43,10 +15,18 @@ public Neighborhood() ContainerType = ContainerType.Neighborhood; } + /// + /// Return one neighbor in the neighborhood satisfying the criteria. + /// + /// If both first and second neighbors fulfill the criteria, exception is thrown. + /// + /// If none of the neighbors fulfills the criteria, null is returned + /// + /// Thrown public IContainer GetNeighborSatisfying(DescriptorCriteria criteria) { - bool isSatisfiedByFirstNeighbor = criteria.IsSatisfiedBy(FirstNeighbor); - bool isSatisfiedBySecondNeighbor = criteria.IsSatisfiedBy(SecondNeighbor); + var isSatisfiedByFirstNeighbor = criteria.IsSatisfiedBy(FirstNeighbor); + var isSatisfiedBySecondNeighbor = criteria.IsSatisfiedBy(SecondNeighbor); if (isSatisfiedByFirstNeighbor && isSatisfiedBySecondNeighbor) throw new BothNeighborsSatisfyingCriteriaException(this); @@ -60,6 +40,16 @@ public IContainer GetNeighborSatisfying(DescriptorCriteria criteria) return null; } + /// + /// return true either if the first neighbor satisfies the and the second + /// neighbor satisfies the + /// or if the first neighbor satisfies the and the second neighbor + /// satisfies the . + /// Otherwise false + /// + /// One criteria to be satisfied by at least one neighbor + /// Another criteria to be satisfied by at least one neighbor + /// public bool Satisfies(DescriptorCriteria criteriaForOneNeighbor, DescriptorCriteria criteriaForTheOtherNeighbor) { return (criteriaForOneNeighbor.IsSatisfiedBy(FirstNeighbor) && @@ -78,7 +68,7 @@ public override void UpdatePropertiesFrom(IUpdatable source, ICloneManager clone { base.UpdatePropertiesFrom(source, cloneManager); - var srcNeighborhood = source as INeighborhood; + var srcNeighborhood = source as Neighborhood; if (srcNeighborhood == null) return; //First/Second neighbor should NOT be cloned diff --git a/src/OSPSuite.Core/Domain/NeighborhoodBaseExtensions.cs b/src/OSPSuite.Core/Domain/NeighborhoodBaseExtensions.cs deleted file mode 100644 index d8ba5ffca..000000000 --- a/src/OSPSuite.Core/Domain/NeighborhoodBaseExtensions.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace OSPSuite.Core.Domain -{ - public static class NeighborhoodBaseExtensions - { - public static TNeighborhood WithFirstNeighbor(this TNeighborhood neighborhood, IContainer firstNeighbor) where TNeighborhood : INeighborhoodBase - { - neighborhood.FirstNeighbor = firstNeighbor; - return neighborhood; - } - - public static TNeighborhood WithSecondNeighbor(this TNeighborhood neighborhood, IContainer secondNeighbor) where TNeighborhood : INeighborhoodBase - { - neighborhood.SecondNeighbor= secondNeighbor; - return neighborhood; - } - - } -} \ No newline at end of file diff --git a/src/OSPSuite.Core/Domain/NeighborhoodExtensions.cs b/src/OSPSuite.Core/Domain/NeighborhoodExtensions.cs new file mode 100644 index 000000000..3bfcdf5c6 --- /dev/null +++ b/src/OSPSuite.Core/Domain/NeighborhoodExtensions.cs @@ -0,0 +1,18 @@ +namespace OSPSuite.Core.Domain +{ + public static class NeighborhoodExtensions + { + public static Neighborhood WithFirstNeighbor(this Neighborhood neighborhood, IContainer firstNeighbor) + { + neighborhood.FirstNeighbor = firstNeighbor; + return neighborhood; + } + + public static Neighborhood WithSecondNeighbor(this Neighborhood neighborhood, IContainer secondNeighbor) + { + neighborhood.SecondNeighbor= secondNeighbor; + return neighborhood; + } + + } +} \ No newline at end of file diff --git a/src/OSPSuite.Core/Domain/QuantityAndContainer.cs b/src/OSPSuite.Core/Domain/QuantityAndContainer.cs index 05feb9ac2..54e312c21 100644 --- a/src/OSPSuite.Core/Domain/QuantityAndContainer.cs +++ b/src/OSPSuite.Core/Domain/QuantityAndContainer.cs @@ -64,9 +64,9 @@ public void Add(IEntity newChild) public IEnumerable GetChildren() where T : class, IEntity => _container.GetChildren(); - public IReadOnlyList GetNeighborsFrom(IReadOnlyList neighborhoods) => _container.GetNeighborsFrom(neighborhoods); + public IReadOnlyList GetNeighborsFrom(IReadOnlyList neighborhoods) => _container.GetNeighborsFrom(neighborhoods); - public IReadOnlyList GetNeighborhoods(IReadOnlyList neighborhoods) => _container.GetNeighborhoods(neighborhoods); + public IReadOnlyList GetNeighborhoods(IReadOnlyList neighborhoods) => _container.GetNeighborhoods(neighborhoods); public IReadOnlyList GetAllContainersAndSelf() where TContainer : class, IContainer => GetAllContainersAndSelf(x => true); diff --git a/src/OSPSuite.Core/Domain/Services/CalculationMethodTask.cs b/src/OSPSuite.Core/Domain/Services/CalculationMethodTask.cs index b2c212cc3..e4d43af44 100644 --- a/src/OSPSuite.Core/Domain/Services/CalculationMethodTask.cs +++ b/src/OSPSuite.Core/Domain/Services/CalculationMethodTask.cs @@ -130,11 +130,14 @@ private void replaceKeyWordsIn(IParameter parameter, string moleculeName) _keywordReplacerTask.ReplaceIn(neighborhood, _model.Root); } - private static INeighborhood neighborhoodAncestorFor(IEntity entity) + private static Neighborhood neighborhoodAncestorFor(IEntity entity) { - if (entity == null) return null; - if (entity.IsAnImplementationOf()) - return entity.DowncastTo(); + if (entity == null) + return null; + + if (entity.IsAnImplementationOf()) + return entity.DowncastTo(); + return neighborhoodAncestorFor(entity.ParentContainer); } diff --git a/src/OSPSuite.Core/Domain/Services/ContainerTask.cs b/src/OSPSuite.Core/Domain/Services/ContainerTask.cs index 04cb831c8..b4a22cd5c 100644 --- a/src/OSPSuite.Core/Domain/Services/ContainerTask.cs +++ b/src/OSPSuite.Core/Domain/Services/ContainerTask.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using OSPSuite.Core.Domain.Builder; +using OSPSuite.Utility.Extensions; namespace OSPSuite.Core.Domain.Services { @@ -60,8 +61,6 @@ public interface IContainerTask /// string CreateUniqueName(IEnumerable usedNames, string baseName, bool canUseBaseName = false); - IEnumerable AllNeighborhoodBuildersConnectedWith(ISpatialStructure spatialStructure, IContainer container); - /// /// Returns a cache of all children satisfying by path defined in the /// @@ -85,15 +84,17 @@ public interface IContainerTask public class ContainerTask : IContainerTask { private readonly IEntityPathResolver _entityPathResolver; + private readonly IObjectPathFactory _objectPathFactory; private readonly IObjectBaseFactory _objectBaseFactory; //format used to generate the unique name - private readonly string _uniqueNameFormat = "{0} "; + private const string _uniqueNameFormat = "{0} "; - public ContainerTask(IObjectBaseFactory objectBaseFactory, IEntityPathResolver entityPathResolver) + public ContainerTask(IObjectBaseFactory objectBaseFactory, IEntityPathResolver entityPathResolver, IObjectPathFactory objectPathFactory) { _objectBaseFactory = objectBaseFactory; _entityPathResolver = entityPathResolver; + _objectPathFactory = objectPathFactory; } public IContainer CreateOrRetrieveSubContainerByName(IContainer parentContainer, string subContainerName) @@ -113,31 +114,19 @@ public IContainer CreateOrRetrieveSubContainerByName(IContainer parentContainer, public void RemoveContainerFrom(ISpatialStructure spatialStructure, IContainer containerToRemove) { - var neighborhoodBuilderToRemove = AllNeighborhoodBuildersConnectedWith(spatialStructure, containerToRemove); - foreach (var neighborhoodBuilder in neighborhoodBuilderToRemove.ToList()) - { - spatialStructure.RemoveNeighborhood(neighborhoodBuilder); - } - + var containerPath = _objectPathFactory.CreateAbsoluteObjectPath(containerToRemove); + spatialStructure.AllNeighborhoodBuildersConnectedWith(containerPath).Each(spatialStructure.RemoveNeighborhood); containerToRemove.ParentContainer.RemoveChild(containerToRemove); } - public IEnumerable AllNeighborhoodBuildersConnectedWith(ISpatialStructure spatialStructure, IContainer container) - { - return spatialStructure.Neighborhoods.Where(neighborhood => neighborhood.IsConnectedTo(container)); - } - - public string CreateUniqueName(IEnumerable usedNames, string baseName, bool canUseBaseName = false) - { - return CreateUniqueName(usedNames.Select(x => x.Name).ToList(), baseName, canUseBaseName); - } + public string CreateUniqueName(IEnumerable usedNames, string baseName, bool canUseBaseName = false) => CreateUniqueName(usedNames.Select(x => x.Name).ToList(), baseName, canUseBaseName); public string CreateUniqueName(IReadOnlyList usedNames, string baseName, bool canUseBaseName = false) { if (!usedNames.Contains(baseName) && canUseBaseName) return baseName; - string baseFormat = string.Format(_uniqueNameFormat, baseName); + var baseFormat = string.Format(_uniqueNameFormat, baseName); //get all endings var allUsedNamesMatchingBaseFormat = usedNames.Where(n => n.StartsWith(baseFormat)) @@ -151,10 +140,8 @@ private int getNextAvailableIndexBasedOn(IEnumerable allUsedNamesMatchin { var allValues = new List(); foreach (var suffix in allUsedNamesMatchingBaseFormat) - { if (int.TryParse(suffix, out var value)) allValues.Add(value); - } if (allValues.Count == 0) return 1; diff --git a/src/OSPSuite.Core/Domain/Services/FormulaTask.cs b/src/OSPSuite.Core/Domain/Services/FormulaTask.cs index 8214343b9..fe0edc9db 100644 --- a/src/OSPSuite.Core/Domain/Services/FormulaTask.cs +++ b/src/OSPSuite.Core/Domain/Services/FormulaTask.cs @@ -178,7 +178,7 @@ private void updateNeighborhoodReferencingPath(IModel model, FormulaUsablePath f var container1 = getContainerOrThrow(pathToFirstContainer, usingFormula); var container2 = getContainerOrThrow(pathToSecondContainer, usingFormula); - var allNeighborhoods = model.Neighborhoods.GetAllChildren(); + var allNeighborhoods = model.Neighborhoods.GetAllChildren(); var allNeighborhoodsConnectedToContainer1 = container1.GetNeighborhoods(allNeighborhoods); var neighborhoodsBetweenContainer1AndContainer2 = container2.GetNeighborhoods(allNeighborhoodsConnectedToContainer1); if (neighborhoodsBetweenContainer1AndContainer2.Count == 0) diff --git a/src/OSPSuite.Core/Domain/Services/KeywordReplacerTask.cs b/src/OSPSuite.Core/Domain/Services/KeywordReplacerTask.cs index 7b17893f5..5641e0b9a 100644 --- a/src/OSPSuite.Core/Domain/Services/KeywordReplacerTask.cs +++ b/src/OSPSuite.Core/Domain/Services/KeywordReplacerTask.cs @@ -26,13 +26,13 @@ public interface IKeywordReplacerTask /// The Molecule keyword will also be replaced with the moleculeName /// The Neighborhood keywords will also be replaced by the names from the neighborhood /// - void ReplaceIn(IObserver observer, IContainer rootContainer, string moleculeName, INeighborhood neighborhood); + void ReplaceIn(IObserver observer, IContainer rootContainer, string moleculeName, Neighborhood neighborhood); /// /// Replace the keywords used in the neighborhoods entities with the appropriate names from the root container. /// The Neighborhood keywords will also be replaced by the names from the neighborhood /// - void ReplaceIn(INeighborhood neighborhood, IContainer rootContainer); + void ReplaceIn(Neighborhood neighborhood, IContainer rootContainer); /// /// Replace the keywords used in the event transport kinetic with the appropriate names from the root container. @@ -45,7 +45,7 @@ public interface IKeywordReplacerTask /// The Molecule keyword will also be replaced with the moleculeName /// The Neighborhood keywords will also be replaced by the names from the neighborhood /// - void ReplaceIn(ITransport passiveTransport, IContainer rootContainer, string moleculeName, INeighborhood neighborhood); + void ReplaceIn(ITransport passiveTransport, IContainer rootContainer, string moleculeName, Neighborhood neighborhood); /// /// Replace the keywords used in the active transport kinetic with the appropriate names from the root container. @@ -53,7 +53,7 @@ public interface IKeywordReplacerTask /// The Neighborhood keywords will also be replaced by the names from the neighborhood /// The keywords TRANSPORT, SOURCE and TARGET will also be replaced by the names from the transporter and transport /// - void ReplaceIn(ITransport realization, IContainer rootContainer, string moleculeName, INeighborhood neighborhood, string transportName, + void ReplaceIn(ITransport realization, IContainer rootContainer, string moleculeName, Neighborhood neighborhood, string transportName, string transporterName); /// @@ -85,7 +85,7 @@ void ReplaceIn(ITransport realization, IContainer rootContainer, string molecule } internal class KeywordReplacerTask : IKeywordReplacerTask, - IVisitor, + IVisitor, IVisitor, IVisitor { @@ -113,7 +113,7 @@ public void ReplaceIn(IObserver observer, IContainer rootContainer, string molec ReplaceIn(observer, rootContainer, moleculeName, null); } - public void ReplaceIn(INeighborhood neighborhood, IContainer rootContainer) + public void ReplaceIn(Neighborhood neighborhood, IContainer rootContainer) { var keywordReplacer = new KeywordReplacerCollection(); addCommonModelReplacersTo(keywordReplacer, rootContainer); @@ -127,12 +127,12 @@ public void ReplaceIn(ITransport eventTransport, IContainer rootContainer, strin ReplaceIn(eventTransport, rootContainer, moleculeName, null); } - public void ReplaceIn(ITransport passiveTransport, IContainer rootContainer, string moleculeName, INeighborhood neighborhood) + public void ReplaceIn(ITransport passiveTransport, IContainer rootContainer, string moleculeName, Neighborhood neighborhood) { ReplaceIn(passiveTransport, rootContainer, moleculeName, neighborhood, null, null); } - public void ReplaceIn(ITransport realization, IContainer rootContainer, string moleculeName, INeighborhood neighborhood, string transportName, + public void ReplaceIn(ITransport realization, IContainer rootContainer, string moleculeName, Neighborhood neighborhood, string transportName, string transporterName) { var keywordReplacer = new KeywordReplacerCollection(); @@ -214,7 +214,7 @@ private void replaceWithMoleculeKeywords(IEnumerable generalKe container.GetChildren().Each(keywordReplacer.ReplaceIn); } - public void ReplaceIn(IObserver observer, IContainer rootContainer, string moleculeName, INeighborhood neighborhood) + public void ReplaceIn(IObserver observer, IContainer rootContainer, string moleculeName, Neighborhood neighborhood) { var keywordReplacer = new KeywordReplacerCollection(); addCommonModelReplacersTo(keywordReplacer, rootContainer); @@ -230,7 +230,7 @@ private void addCommonModelReplacersTo(IKeywordReplacerCollection keywordReplace keywordReplacer.AddReplacement(new TopContainerPathReplacer(rootContainer.Name, new[] {MOLECULE, Constants.NEIGHBORHOODS})); } - private void addCommonNeighborhoodReplacersTo(IKeywordReplacerCollection keywordReplacer, INeighborhood neighborhood) + private void addCommonNeighborhoodReplacersTo(IKeywordReplacerCollection keywordReplacer, Neighborhood neighborhood) { if (neighborhood == null) return; keywordReplacer.AddReplacement(new KeywordWithPathReplacer(FIRST_NEIGHBOR, _objectPathFactory.CreateAbsoluteObjectPath(neighborhood.FirstNeighbor))); @@ -291,7 +291,7 @@ public void ReplaceIn(IMoleculeAmount moleculeAmount) moleculeAmount.GetAllChildren().Each(keywordReplacer.ReplaceIn); } - public void Visit(INeighborhood neighborhood) + public void Visit(Neighborhood neighborhood) { ReplaceIn(neighborhood, _rootContainer); } diff --git a/src/OSPSuite.Core/Domain/Services/ModelConstructor.cs b/src/OSPSuite.Core/Domain/Services/ModelConstructor.cs index 5ea008c4e..6e1016475 100644 --- a/src/OSPSuite.Core/Domain/Services/ModelConstructor.cs +++ b/src/OSPSuite.Core/Domain/Services/ModelConstructor.cs @@ -164,7 +164,7 @@ private CreationResult buildProcess(IModel model, IBuildConfiguration buildConfi foreach (var step in steps) { - //call each build process with the model and the buildconfiguration + //call each build process with the model and the buildConfiguration result.Add(step(model, buildConfiguration)); progress?.IncrementProgress(); diff --git a/src/OSPSuite.Core/Domain/Services/ModelFinalizer.cs b/src/OSPSuite.Core/Domain/Services/ModelFinalizer.cs index f950b8def..1693407c3 100644 --- a/src/OSPSuite.Core/Domain/Services/ModelFinalizer.cs +++ b/src/OSPSuite.Core/Domain/Services/ModelFinalizer.cs @@ -108,7 +108,7 @@ private Cache createEventGroupCache(IModel model) return cache; } - private void finalizeNeighborhoods(IModel cloneModel, IEnumerable sourceNeighborhoods, ICache cloneNeighborhoods) + private void finalizeNeighborhoods(IModel cloneModel, IEnumerable sourceNeighborhoods, ICache cloneNeighborhoods) { foreach (var sourceNeighborhood in sourceNeighborhoods) { @@ -118,7 +118,7 @@ private void finalizeNeighborhoods(IModel cloneModel, IEnumerable } } - private void finalizeTransportsInNeighborhood(IModel cloneModel, INeighborhood sourceNeighborhood, INeighborhood cloneNeighborhood) + private void finalizeTransportsInNeighborhood(IModel cloneModel, Neighborhood sourceNeighborhood, Neighborhood cloneNeighborhood) { finalizeTransportsInMoleculeParentContainer(cloneModel, sourceNeighborhood, cloneNeighborhood); foreach (var sourceMoleculeContainer in sourceNeighborhood.GetChildren()) @@ -170,7 +170,7 @@ private void resolveAmounts(IModel cloneModel, ITransport sourceTransport, ITran cloneTransport.TargetAmount = targetAmountPath.Resolve(cloneModel.Root); } - private void resolveNeighbors(IModel cloneModel, INeighborhood sourceNeighborhood, INeighborhood cloneNeighborhood) + private void resolveNeighbors(IModel cloneModel, Neighborhood sourceNeighborhood, Neighborhood cloneNeighborhood) { var firstNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(sourceNeighborhood.FirstNeighbor); var secondNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(sourceNeighborhood.SecondNeighbor); @@ -179,10 +179,10 @@ private void resolveNeighbors(IModel cloneModel, INeighborhood sourceNeighborhoo cloneNeighborhood.SecondNeighbor = secondNeighborPath.Resolve(cloneModel.Root); } - private ICache createNeighborhoodCache(IModel model) + private ICache createNeighborhoodCache(IModel model) { - var cloneNeighborhoods = new Cache(x => x.Name); - cloneNeighborhoods.AddRange(model.Neighborhoods.GetChildren()); + var cloneNeighborhoods = new Cache(x => x.Name); + cloneNeighborhoods.AddRange(model.Neighborhoods.GetChildren()); return cloneNeighborhoods; } } diff --git a/src/OSPSuite.Core/Domain/Services/ModelReportCreator.cs b/src/OSPSuite.Core/Domain/Services/ModelReportCreator.cs index d139bff74..3932644e2 100644 --- a/src/OSPSuite.Core/Domain/Services/ModelReportCreator.cs +++ b/src/OSPSuite.Core/Domain/Services/ModelReportCreator.cs @@ -88,7 +88,7 @@ private string containerReport(IContainer container, IModel model = null) private void reportFor(IContainer container) { - if (container.IsAnImplementationOf()) + if (container.IsAnImplementationOf()) return; if (container.IsAnImplementationOf()) return; @@ -127,7 +127,7 @@ private void reportFor(IContainer container) _report.AppendLine(); } - private void reportFor(INeighborhood neighborhood) + private void reportFor(Neighborhood neighborhood) { _report.AppendFormat("Neighborhood: {0}", neighborhood.Name); _report.AppendLine(); @@ -380,7 +380,7 @@ private void neighborhoodsReportFor(IModel model) if (model == null) return; addSeparatorFor("NEIGHBORHOODS"); - foreach (var neighborhood in model.Neighborhoods.GetAllChildren()) + foreach (var neighborhood in model.Neighborhoods.GetAllChildren()) { reportFor(neighborhood); } diff --git a/src/OSPSuite.Core/Domain/Services/MoleculePropertiesContainerTask.cs b/src/OSPSuite.Core/Domain/Services/MoleculePropertiesContainerTask.cs index cd92dc999..f6237a018 100644 --- a/src/OSPSuite.Core/Domain/Services/MoleculePropertiesContainerTask.cs +++ b/src/OSPSuite.Core/Domain/Services/MoleculePropertiesContainerTask.cs @@ -12,7 +12,7 @@ public interface IMoleculePropertiesContainerTask /// /// is thorwn if the container does not exist in the root for the molecule /// - IContainer NeighborhoodMoleculeContainerFor(INeighborhood neighborhood, string moleculeName); + IContainer NeighborhoodMoleculeContainerFor(Neighborhood neighborhood, string moleculeName); /// /// Creates the global molecule container for the molecule and add parameters with the build modes other than "Local" @@ -24,7 +24,7 @@ public interface IMoleculePropertiesContainerTask /// Returns (and creates if not already there) the sub container for the transport process named /// in the for the transporter /// - IContainer NeighborhoodMoleculeTransportContainerFor(INeighborhood neighborhood, string transportedMoleculeName, TransporterMoleculeContainer transporterMolecule, string transportName, IBuildConfiguration buildConfiguration); + IContainer NeighborhoodMoleculeTransportContainerFor(Neighborhood neighborhood, string transportedMoleculeName, TransporterMoleculeContainer transporterMolecule, string transportName, IBuildConfiguration buildConfiguration); } internal class MoleculePropertiesContainerTask : IMoleculePropertiesContainerTask @@ -41,7 +41,7 @@ public MoleculePropertiesContainerTask(IContainerTask containerTask, IParameterB _keywordReplacer = keywordReplacer; } - public IContainer NeighborhoodMoleculeContainerFor(INeighborhood neighborhood, string moleculeName) + public IContainer NeighborhoodMoleculeContainerFor(Neighborhood neighborhood, string moleculeName) { var moleculeContainer = neighborhood.GetSingleChildByName(moleculeName); if (moleculeContainer == null) @@ -111,7 +111,7 @@ private IEnumerable allLocalParametersFrom(IContainer container, IBu return _parameterCollectionMapper.MapLocalFrom(container, buildConfiguration); } - public IContainer NeighborhoodMoleculeTransportContainerFor(INeighborhood neighborhood, string transportedMoleculeName,TransporterMoleculeContainer transporterMolecule, string transportName, IBuildConfiguration buildConfiguration) + public IContainer NeighborhoodMoleculeTransportContainerFor(Neighborhood neighborhood, string transportedMoleculeName,TransporterMoleculeContainer transporterMolecule, string transportName, IBuildConfiguration buildConfiguration) { var moleculeContainer = NeighborhoodMoleculeContainerFor(neighborhood, transportedMoleculeName); var transportContainer = moleculeContainer.EntityAt(transporterMolecule.TransportName); diff --git a/src/OSPSuite.Core/Domain/Services/TransportCreator.cs b/src/OSPSuite.Core/Domain/Services/TransportCreator.cs index 9aa774809..c79f68497 100644 --- a/src/OSPSuite.Core/Domain/Services/TransportCreator.cs +++ b/src/OSPSuite.Core/Domain/Services/TransportCreator.cs @@ -30,7 +30,7 @@ public TransportCreator(ITransportBuilderToTransportMapper transportMapper, IKey public void CreatePassiveTransport(IModel model, ITransportBuilder passiveTransportBuilder, IBuildConfiguration buildConfiguration) { - var allNeighborhoods = model.Neighborhoods.GetChildren().ToList(); + var allNeighborhoods = model.Neighborhoods.GetChildren().ToList(); foreach (var molecule in buildConfiguration.Molecules.AllFloating()) { @@ -40,7 +40,7 @@ public void CreatePassiveTransport(IModel model, ITransportBuilder passiveTransp public void CreateActiveTransport(IModel model, IBuildConfiguration buildConfiguration) { - var allNeighborhoods = model.Neighborhoods.GetChildren().ToList(); + var allNeighborhoods = model.Neighborhoods.GetChildren().ToList(); var molecules = buildConfiguration.Molecules; foreach (var molecule in molecules.AllFloating()) { @@ -59,7 +59,7 @@ public void CreateActiveTransport(IModel model, IBuildConfiguration buildConfigu } } - private void addPassiveTransportToModel(IModel model, ITransportBuilder passiveTransportBuilder, IEnumerable allNeighborhoods, + private void addPassiveTransportToModel(IModel model, ITransportBuilder passiveTransportBuilder, IEnumerable allNeighborhoods, IMoleculeBuilder molecule, IBuildConfiguration buildConfiguration) { // first check if the molecule should be transported @@ -76,7 +76,7 @@ private void addPassiveTransportToModel(IModel model, ITransportBuilder passiveT } } - private void addActiveTransportToModel(IModel model, ITransportBuilder activeTransportBuilder, IEnumerable allNeighborhoods, + private void addActiveTransportToModel(IModel model, ITransportBuilder activeTransportBuilder, IEnumerable allNeighborhoods, IMoleculeBuilder molecule, TransporterMoleculeContainer transporterMolecule, IBuildConfiguration buildConfiguration) { var neighborhoods = getNeighborhoodsForActiveTransport(activeTransportBuilder, allNeighborhoods, molecule.Name, transporterMolecule.Name); @@ -92,7 +92,7 @@ private void addActiveTransportToModel(IModel model, ITransportBuilder activeTra } } - private IEnumerable getNeighborhoodsForActiveTransport(ITransportBuilder transport, IEnumerable allNeighborhoods, + private IEnumerable getNeighborhoodsForActiveTransport(ITransportBuilder transport, IEnumerable allNeighborhoods, string moleculeName, string transporterName) { try @@ -110,21 +110,21 @@ private IEnumerable getNeighborhoodsForActiveTransport(ITransport } } - private IEnumerable getNeighborhoodsForPassiveTransport(ITransportBuilder passiveTransportBuilder, - IEnumerable allNeighborhoods, string moleculeName) + private IEnumerable getNeighborhoodsForPassiveTransport(ITransportBuilder passiveTransportBuilder, + IEnumerable allNeighborhoods, string moleculeName) { return getNeighborhoodsByNeighborCriteria(allNeighborhoods, passiveTransportBuilder.SourceCriteria, passiveTransportBuilder.TargetCriteria, moleculeName); } - private IEnumerable getNeighborhoodsByNeighborCriteria(IEnumerable neighborhoods, + private IEnumerable getNeighborhoodsByNeighborCriteria(IEnumerable neighborhoods, DescriptorCriteria conditionsForOneNeighbor, DescriptorCriteria conditionsForTheOtherNeighbor, string name) { return getNeighborhoodsByNeighborCriteria(neighborhoods, conditionsForOneNeighbor, conditionsForTheOtherNeighbor, new[] {name}); } - private IEnumerable getNeighborhoodsByNeighborCriteria(IEnumerable neighborhoods, + private IEnumerable getNeighborhoodsByNeighborCriteria(IEnumerable neighborhoods, DescriptorCriteria conditionsForOneNeighbor, DescriptorCriteria conditionsForTheOtherNeighbor, IEnumerable moleculeNames) { @@ -135,13 +135,13 @@ where neighborhood.SecondNeighbor.ContainsNames(moleculeNames) select neighborhood; } - private IContainer addPassiveTransportToNeighborhood(INeighborhood neighborhood, string moleculeName, ITransport transport) + private IContainer addPassiveTransportToNeighborhood(Neighborhood neighborhood, string moleculeName, ITransport transport) { return _moleculePropertiesContainerTask.NeighborhoodMoleculeContainerFor(neighborhood, moleculeName) .WithChild(transport); } - private IContainer addActiveTransportToNeighborhood(INeighborhood neighborhood, ITransport transport, + private IContainer addActiveTransportToNeighborhood(Neighborhood neighborhood, ITransport transport, TransporterMoleculeContainer transporterMolecule, string transportedMoleculeName, IBuildConfiguration buildConfiguration) { return _moleculePropertiesContainerTask.NeighborhoodMoleculeTransportContainerFor(neighborhood, transportedMoleculeName, transporterMolecule, @@ -149,7 +149,7 @@ private IContainer addActiveTransportToNeighborhood(INeighborhood neighborhood, .WithChild(transport); } - private ITransport mapFrom(ITransportBuilder transportBuilder, INeighborhood neighborhood, string moleculeName, + private ITransport mapFrom(ITransportBuilder transportBuilder, Neighborhood neighborhood, string moleculeName, IBuildConfiguration buildConfiguration) { var transport = _transportMapper.MapFrom(transportBuilder, buildConfiguration); diff --git a/src/OSPSuite.Core/OSPSuite.Core.csproj b/src/OSPSuite.Core/OSPSuite.Core.csproj index 5f671ecd3..d99a494d1 100644 --- a/src/OSPSuite.Core/OSPSuite.Core.csproj +++ b/src/OSPSuite.Core/OSPSuite.Core.csproj @@ -58,6 +58,7 @@ + diff --git a/src/OSPSuite.Core/Serialization/PKMLVersion.cs b/src/OSPSuite.Core/Serialization/PKMLVersion.cs index 0cfcac9e2..321882629 100644 --- a/src/OSPSuite.Core/Serialization/PKMLVersion.cs +++ b/src/OSPSuite.Core/Serialization/PKMLVersion.cs @@ -16,6 +16,7 @@ public static class PKMLVersion public const int V9_0 = 14; public const int V10_0 = 15; public const int V11_0 = 16; - public const int CURRENT = V11_0; + public const int V12_0 = 17; + public const int CURRENT = V12_0; } } \ No newline at end of file diff --git a/src/OSPSuite.Core/Serialization/Xml/NeighborhoodBuilderXmlSerializer.cs b/src/OSPSuite.Core/Serialization/Xml/NeighborhoodBuilderXmlSerializer.cs index 11501284d..bde20801a 100644 --- a/src/OSPSuite.Core/Serialization/Xml/NeighborhoodBuilderXmlSerializer.cs +++ b/src/OSPSuite.Core/Serialization/Xml/NeighborhoodBuilderXmlSerializer.cs @@ -7,6 +7,11 @@ public class NeighborhoodBuilderXmlSerializer : ContainerXmlSerializer x.FirstNeighborPath); + Map(x => x.SecondNeighborPath); + + //Compatibility with older projects MapReference(x => x.FirstNeighbor); MapReference(x => x.SecondNeighbor); } diff --git a/src/OSPSuite.Core/Serialization/Xml/NeighborhoodXmlSerializer.cs b/src/OSPSuite.Core/Serialization/Xml/NeighborhoodXmlSerializer.cs index a8a0a6caa..6dc53a45a 100644 --- a/src/OSPSuite.Core/Serialization/Xml/NeighborhoodXmlSerializer.cs +++ b/src/OSPSuite.Core/Serialization/Xml/NeighborhoodXmlSerializer.cs @@ -3,7 +3,7 @@ namespace OSPSuite.Core.Serialization.Xml { //necessary for PkSimNeighborhood - public class NeighborhoodXmlSerializerBase : ContainerXmlSerializer where TNeighborhood : class, INeighborhood + public class NeighborhoodXmlSerializerBase : ContainerXmlSerializer where TNeighborhood : Neighborhood { public override void PerformMapping() { diff --git a/src/OSPSuite.Presentation/Diagram/BaseDiagramManager.cs b/src/OSPSuite.Presentation/Diagram/BaseDiagramManager.cs index c7d999691..d74547a84 100644 --- a/src/OSPSuite.Presentation/Diagram/BaseDiagramManager.cs +++ b/src/OSPSuite.Presentation/Diagram/BaseDiagramManager.cs @@ -4,6 +4,7 @@ using OSPSuite.Utility.Extensions; using OSPSuite.Core.Diagram; using OSPSuite.Core.Domain; +using OSPSuite.Core.Domain.Builder; using OSPSuite.Presentation.Diagram.Elements; using OSPSuite.Presentation.Extensions; diff --git a/tests/OSPSuite.Core.IntegrationTests/DiffBuilders/NeighborhoodBuilderDiffBuilderSpecs.cs b/tests/OSPSuite.Core.IntegrationTests/DiffBuilders/NeighborhoodBuilderDiffBuilderSpecs.cs index a75167b72..9e29fcc38 100644 --- a/tests/OSPSuite.Core.IntegrationTests/DiffBuilders/NeighborhoodBuilderDiffBuilderSpecs.cs +++ b/tests/OSPSuite.Core.IntegrationTests/DiffBuilders/NeighborhoodBuilderDiffBuilderSpecs.cs @@ -2,10 +2,21 @@ using OSPSuite.BDDHelper.Extensions; using OSPSuite.Core.Domain; using OSPSuite.Core.Domain.Builder; +using OSPSuite.Utility.Container; namespace OSPSuite.Core.DiffBuilders { - public class When_comparring_two_similar_neighborhood_builder : concern_for_ObjectComparer + public abstract class concern_for_NeighborhoodBuilderDiffBuilder : concern_for_ObjectComparer + { + protected IObjectPathFactory _objectPathFactory; + + protected override void Context() + { + base.Context(); + _objectPathFactory = IoC.Resolve(); + } + } + public class When_comparing_two_similar_neighborhood_builder : concern_for_NeighborhoodBuilderDiffBuilder { protected override void Context() { @@ -16,14 +27,14 @@ protected override void Context() var tc1 = new Container().WithName("Root"); var cell1 = new Container().WithName("Cell").WithParentContainer(tc1); var int1 = new Container().WithName("int").WithParentContainer(tc1); - n1.FirstNeighbor = cell1; - n1.SecondNeighbor = int1; + n1.FirstNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(cell1); + n1.SecondNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(int1); var tc2 = new Container().WithName("Root"); var cell2 = new Container().WithName("Cell").WithParentContainer(tc2); var int2 = new Container().WithName("int").WithParentContainer(tc2); - n2.FirstNeighbor = cell2; - n2.SecondNeighbor = int2; + n2.FirstNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(cell2); + n2.SecondNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(int2); _object1 = n1; _object2 = n2; @@ -36,7 +47,7 @@ public void should_not_report_any_differences() } } - public class When_comparring_two_neighborhood_builder_with_swapped_neighbors : concern_for_ObjectComparer + public class When_comparing_two_neighborhood_builder_with_swapped_neighbors : concern_for_NeighborhoodBuilderDiffBuilder { protected override void Context() { @@ -47,14 +58,15 @@ protected override void Context() var tc1 = new Container().WithName("Root"); var cell1 = new Container().WithName("Cell").WithParentContainer(tc1); var int1 = new Container().WithName("int").WithParentContainer(tc1); - n1.FirstNeighbor = cell1; - n1.SecondNeighbor = int1; + n1.FirstNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(cell1); + n1.SecondNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(int1); var tc2 = new Container().WithName("Root"); var cell2 = new Container().WithName("Cell").WithParentContainer(tc2); var int2 = new Container().WithName("int").WithParentContainer(tc2); - n2.FirstNeighbor = int2; - n2.SecondNeighbor = cell2; + + n2.FirstNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(int2); + n2.SecondNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(cell2); _object1 = n1; _object2 = n2; @@ -66,7 +78,7 @@ public void should_not_report_any_differences() _report.ShouldBeEmpty(); } } - public class When_comparring_two_neighborhood_builder_with_different_neighbors : concern_for_ObjectComparer + public class When_comparing_two_neighborhood_builder_with_different_neighbors : concern_for_NeighborhoodBuilderDiffBuilder { protected override void Context() { @@ -77,14 +89,14 @@ protected override void Context() var tc1 = new Container().WithName("Root"); var cell1 = new Container().WithName("Cell").WithParentContainer(tc1); var int1 = new Container().WithName("int").WithParentContainer(tc1); - n1.FirstNeighbor = cell1; - n1.SecondNeighbor = int1; + n1.FirstNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(cell1); + n1.SecondNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(int1); var tc2 = new Container().WithName("Root"); var cell2 = new Container().WithName("Cell").WithParentContainer(tc2); var int2 = new Container().WithName("pls").WithParentContainer(tc2); - n2.FirstNeighbor = int2; - n2.SecondNeighbor = cell2; + n2.FirstNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(int2); + n2.SecondNeighborPath = _objectPathFactory.CreateAbsoluteObjectPath(cell2); _object1 = n1; _object2 = n2; diff --git a/tests/OSPSuite.Core.IntegrationTests/Helpers/AssertForSpecs.cs b/tests/OSPSuite.Core.IntegrationTests/Helpers/AssertForSpecs.cs index f36781199..f636a0880 100644 --- a/tests/OSPSuite.Core.IntegrationTests/Helpers/AssertForSpecs.cs +++ b/tests/OSPSuite.Core.IntegrationTests/Helpers/AssertForSpecs.cs @@ -80,9 +80,9 @@ public static void AreEqual(T x1, T x2) where T : class } // Types derived from other Builder - if (x1.IsAnImplementationOf()) + if (x1.IsAnImplementationOf()) { - AreEqualNeighborhoodBuilder((INeighborhoodBuilder) x1, (INeighborhoodBuilder) x2); + AreEqualNeighborhoodBuilder(x1.DowncastTo(), x2.DowncastTo()); return; } @@ -273,9 +273,9 @@ public static void AreEqual(T x1, T x2) where T : class } // Types derived from ObjectBase and Container - if (x1.IsAnImplementationOf()) + if (x1.IsAnImplementationOf()) { - AreEqualNeighborhood((INeighborhood) x1, (INeighborhood) x2); + AreEqualNeighborhood(x1.DowncastTo(), x2.DowncastTo()); return; } @@ -537,7 +537,7 @@ public static void AreEqualContainer(IContainer x1, IContainer x2) AreEqualEnumerableOfNamedObjects(x1.Children, x2.Children, x => x.Name); } - public static void AreEqualNeighborhood(INeighborhood x1, INeighborhood x2) + public static void AreEqualNeighborhood(Neighborhood x1, Neighborhood x2) { if (!AssertBothNotNull(x1, x2)) return; AreEqualContainer(x1, x2); @@ -1032,12 +1032,12 @@ public static void AreEqualReactionBuilder(IReactionBuilder x1, IReactionBuilder x2.ModifierNames.ShouldOnlyContain(x1.ModifierNames.ToArray()); } - public static void AreEqualNeighborhoodBuilder(INeighborhoodBuilder x1, INeighborhoodBuilder x2) + public static void AreEqualNeighborhoodBuilder(NeighborhoodBuilder x1, NeighborhoodBuilder x2) { if (!AssertBothNotNull(x1, x2)) return; AreEqualContainer(x1, x2); - AreEqualContainer(x1.FirstNeighbor, x2.FirstNeighbor); - AreEqualContainer(x1.SecondNeighbor, x2.SecondNeighbor); + AreEqualObjectPath(x1.FirstNeighborPath, x2.FirstNeighborPath); + AreEqualObjectPath(x1.SecondNeighborPath, x2.SecondNeighborPath); AreEqualContainer(x1.MoleculeProperties, x2.MoleculeProperties); } diff --git a/tests/OSPSuite.Core.IntegrationTests/ModelConstructorIntegrationTests.cs b/tests/OSPSuite.Core.IntegrationTests/ModelConstructorIntegrationTests.cs index e568f43e6..2fe29a0a8 100644 --- a/tests/OSPSuite.Core.IntegrationTests/ModelConstructorIntegrationTests.cs +++ b/tests/OSPSuite.Core.IntegrationTests/ModelConstructorIntegrationTests.cs @@ -143,7 +143,7 @@ public void Influx_transport_from_transporter_D_should_only_take_place_between_l var A = _model.MoleculeContainerInNeighborhood("lng_pls_to_lng_cell", "A"); var B = _model.MoleculeContainerInNeighborhood("lng_pls_to_lng_cell", "B"); - var allContainingTransporter1 = _model.Neighborhoods.GetAllChildren() + var allContainingTransporter1 = _model.Neighborhoods.GetAllChildren() .SelectMany(x => x.GetAllChildren()).Where(c => c.ContainsName("My Transport1")); allContainingTransporter1.ShouldOnlyContain(A, B); @@ -154,7 +154,7 @@ public void Efflux_transport_from_transporter_E_should_only_take_place_between_b { var A = _model.MoleculeContainerInNeighborhood("bon_pls_to_bon_cell", "A"); - var allContainingTransporter1 = _model.Neighborhoods.GetAllChildren() + var allContainingTransporter1 = _model.Neighborhoods.GetAllChildren() .SelectMany(x => x.GetAllChildren()).Where(c => c.ContainsName("My Transport2")); allContainingTransporter1.ShouldOnlyContain(A); @@ -462,12 +462,12 @@ internal static class ModelExtensionsForSpecs { internal static IContainer MoleculeContainerInNeighborhood(this IModel model, string neighborhoodName, string moleculeName) { - return model.Neighborhoods.GetSingleChildByName(neighborhoodName).GetSingleChildByName(moleculeName); + return model.Neighborhoods.GetSingleChildByName(neighborhoodName).Container(moleculeName); } internal static IContainer ModelOrganCompartment(this IModel model, string organName, string compartmentName) { - return model.Root.GetSingleChildByName(ConstantsForSpecs.Organism).GetSingleChildByName(organName).GetSingleChildByName(compartmentName); + return model.Root.GetSingleChildByName(ConstantsForSpecs.Organism).Container(organName).Container(compartmentName); } internal static IMoleculeAmount ModelOrganCompartmentMolecule(this IModel model, string organName, string compartmentName, string moleculeName) diff --git a/tests/OSPSuite.Core.IntegrationTests/Serializers/ModelXmlSerializerSpecs.cs b/tests/OSPSuite.Core.IntegrationTests/Serializers/ModelXmlSerializerSpecs.cs index 1fd040ceb..77eab7e29 100644 --- a/tests/OSPSuite.Core.IntegrationTests/Serializers/ModelXmlSerializerSpecs.cs +++ b/tests/OSPSuite.Core.IntegrationTests/Serializers/ModelXmlSerializerSpecs.cs @@ -18,7 +18,7 @@ public void TestSimpleModelContainer() x1.Root.Add(c2); x1.Neighborhoods = CreateObject().WithName(Constants.NEIGHBORHOODS); - INeighborhood n12 = CreateObject().WithName("Nina").WithFirstNeighbor(c1).WithSecondNeighbor(c2); + var n12 = CreateObject().WithName("Nina").WithFirstNeighbor(c1).WithSecondNeighbor(c2); x1.Neighborhoods.Add(n12); Model x2 = SerializeAndDeserialize(x1); diff --git a/tests/OSPSuite.Core.IntegrationTests/Serializers/NeighborhoodBuilderXmlSerializerSpecs.cs b/tests/OSPSuite.Core.IntegrationTests/Serializers/NeighborhoodBuilderXmlSerializerSpecs.cs index 1bc450959..5c8c3cb4d 100644 --- a/tests/OSPSuite.Core.IntegrationTests/Serializers/NeighborhoodBuilderXmlSerializerSpecs.cs +++ b/tests/OSPSuite.Core.IntegrationTests/Serializers/NeighborhoodBuilderXmlSerializerSpecs.cs @@ -14,10 +14,10 @@ public class NeighborhoodBuilderXmlSerializerSpecs : MiniEnvironmentSerializerBa public void TestSerialization() { var x1 = CreateObject().WithName("Nena.Builder") - .WithMode(ContainerMode.Physical) - .WithFirstNeighbor(C1) - .WithSecondNeighbor(C2); + .WithMode(ContainerMode.Physical); + x1.FirstNeighborPath = new ObjectPath("A|B|C"); + x1.SecondNeighborPath = new ObjectPath("D|E|F"); x1.Add(CreateObject().WithMode(ContainerMode.Logical).WithName(Constants.MOLECULE_PROPERTIES)); var K = CreateObject().WithName("K").WithFormula(CreateObject().WithValue(23.4)); diff --git a/tests/OSPSuite.Core.IntegrationTests/Serializers/NeighborhoodXmlSerializerSpecs.cs b/tests/OSPSuite.Core.IntegrationTests/Serializers/NeighborhoodXmlSerializerSpecs.cs index d4ac558d5..05720b307 100644 --- a/tests/OSPSuite.Core.IntegrationTests/Serializers/NeighborhoodXmlSerializerSpecs.cs +++ b/tests/OSPSuite.Core.IntegrationTests/Serializers/NeighborhoodXmlSerializerSpecs.cs @@ -12,16 +12,16 @@ public class NeighborhoodXmlSerializerSpecs : ModellingXmlSerializerBaseSpecs public void TestSerializationEmptyNeighborhood() { //Neighborhood x1 = new Neighborhood().WithName("otto"); Does not help, because ObjectBaseFactory is used in Deserialization - Neighborhood x1 = CreateObject().WithName("Nele"); + var x1 = CreateObject().WithName("Nele"); Assert.IsNull(x1.FirstNeighbor); Assert.IsNull(x1.SecondNeighbor); - INeighborhood x2 = SerializeAndDeserialize(x1); + var x2 = SerializeAndDeserialize(x1); AssertForSpecs.AreEqualNeighborhood(x2, x1); } [Test] - public void TestSerializationNeigborhoodWithContainers() + public void TestSerializationNeighborhoodWithContainers() { Neighborhood x1 = CreateObject().WithName("Nele"); diff --git a/tests/OSPSuite.Core.IntegrationTests/SpatialStructureSpecs.cs b/tests/OSPSuite.Core.IntegrationTests/SpatialStructureSpecs.cs index 73a18e988..9781e15b2 100644 --- a/tests/OSPSuite.Core.IntegrationTests/SpatialStructureSpecs.cs +++ b/tests/OSPSuite.Core.IntegrationTests/SpatialStructureSpecs.cs @@ -47,8 +47,8 @@ protected override void Because() [Observation] public void all_neighbors_should_be_cloned() { - sut.Neighborhoods.Each(neighbor => neighbor.FirstNeighbor.ShouldNotBeNull()); - sut.Neighborhoods.Each(neighbor => neighbor.SecondNeighbor.ShouldNotBeNull()); + sut.Neighborhoods.Each(neighbor => neighbor.FirstNeighborPath.ShouldNotBeNull()); + sut.Neighborhoods.Each(neighbor => neighbor.SecondNeighborPath.ShouldNotBeNull()); } [Observation] diff --git a/tests/OSPSuite.Core.Tests/Domain/ContainerTaskSpecs.cs b/tests/OSPSuite.Core.Tests/Domain/ContainerTaskSpecs.cs index a2bb9d675..c2ab5c5d1 100644 --- a/tests/OSPSuite.Core.Tests/Domain/ContainerTaskSpecs.cs +++ b/tests/OSPSuite.Core.Tests/Domain/ContainerTaskSpecs.cs @@ -11,12 +11,14 @@ internal abstract class concern_for_ContainerTask : ContextSpecification(); _entityPathResolver = A.Fake(); - sut = new ContainerTask(_objectBaseFactory, _entityPathResolver); + _objectPathFactory = A.Fake(); + sut = new ContainerTask(_objectBaseFactory, _entityPathResolver, _objectPathFactory); } } @@ -82,26 +84,39 @@ public void should_create_a_container_into_the_parent_container_and_return_the_n internal class When_removing_a_container_in_SpatialStructure : concern_for_ContainerTask { private ISpatialStructure _spatialStructure; - private INeighborhoodBuilder _firstNeighborRemove; - private INeighborhoodBuilder _secondNeighborRemove; + private NeighborhoodBuilder _firstNeighborRemove; + private NeighborhoodBuilder _secondNeighborRemove; private IContainer _containerToRemove; private IContainer _parent; + private ObjectPath _containerToRemovePath; + private NeighborhoodBuilder _thirdNeighborhood; protected override void Context() { base.Context(); - _spatialStructure = A.Fake(); + _spatialStructure = new SpatialStructure(); + _spatialStructure.NeighborhoodsContainer = new Container(); _parent = A.Fake(); _containerToRemove = A.Fake(); _containerToRemove.ParentContainer = _parent; - _firstNeighborRemove = new NeighborhoodBuilder().WithFirstNeighbor(_containerToRemove).WithSecondNeighbor(A.Fake()); - _secondNeighborRemove = new NeighborhoodBuilder().WithFirstNeighbor(A.Fake()).WithSecondNeighbor(_containerToRemove); - A.CallTo(() => _spatialStructure.Neighborhoods).Returns(new[] - { - new NeighborhoodBuilder().WithFirstNeighbor(A.Fake()).WithSecondNeighbor(A.Fake()), - _firstNeighborRemove, - _secondNeighborRemove - }); + _containerToRemovePath = new ObjectPath("containerToRemovePath"); + A.CallTo(() => _objectPathFactory.CreateAbsoluteObjectPath(_containerToRemove)).Returns(_containerToRemovePath); + _firstNeighborRemove = new NeighborhoodBuilder + { + FirstNeighborPath = _containerToRemovePath, + SecondNeighborPath = new ObjectPath("anotherContainer"), + Name = "firstNeighborRemove" + }; + _secondNeighborRemove = new NeighborhoodBuilder + { + FirstNeighborPath = new ObjectPath("anotherContainer"), + SecondNeighborPath = _containerToRemovePath, + Name = "secondNeighborRemove" + }; + _thirdNeighborhood = new NeighborhoodBuilder {Name = "thirdNeighborhood"}; + _spatialStructure.AddNeighborhood(_firstNeighborRemove); + _spatialStructure.AddNeighborhood(_secondNeighborRemove); + _spatialStructure.AddNeighborhood(_thirdNeighborhood); } protected override void Because() @@ -110,21 +125,15 @@ protected override void Because() } [Observation] - public void should_call_remove_for_firstNeighborRemove() + public void should_remove_the_neighborhood_connected_to_the_container_to_remove() { - A.CallTo(() => _spatialStructure.RemoveNeighborhood(_firstNeighborRemove)).MustHaveHappened(); + _spatialStructure.Neighborhoods.ShouldOnlyContain(_thirdNeighborhood); } [Observation] - public void should_call_remove_for_secondNeighborRemove() + public void should_have_called_remove_child_at_parent() { - A.CallTo(() => _spatialStructure.RemoveNeighborhood(_secondNeighborRemove)).MustHaveHappened(); - } - - [Observation] - public void should_have_calles_remove_child_at_parent() - { - A.CallTo(() => _parent.RemoveChild(_containerToRemove)); + A.CallTo(() => _parent.RemoveChild(_containerToRemove)).MustHaveHappened(); } } @@ -193,7 +202,7 @@ protected override void Because() } [Observation] - public void should_create_a_cache_containg_all_the_matching_children() + public void should_create_a_cache_containing_all_the_matching_children() { _cache.ShouldOnlyContain(_para1, _para2); } diff --git a/tests/OSPSuite.Core.Tests/Domain/NeighborhoodBuilderFactorySpecs.cs b/tests/OSPSuite.Core.Tests/Domain/NeighborhoodBuilderFactorySpecs.cs index 7cc08c87e..980dcf257 100644 --- a/tests/OSPSuite.Core.Tests/Domain/NeighborhoodBuilderFactorySpecs.cs +++ b/tests/OSPSuite.Core.Tests/Domain/NeighborhoodBuilderFactorySpecs.cs @@ -9,30 +9,31 @@ namespace OSPSuite.Core.Domain public abstract class concern_for_NeighborhoodBuilderFactory : ContextSpecification { private IObjectBaseFactory _objectBaseFactory; + protected IObjectPathFactory _objectPathFactory; protected override void Context() { - _objectBaseFactory =new TestObjectBaseFactory(); - sut = new NeighborhoodBuilderFactory(_objectBaseFactory); + _objectBaseFactory = new TestObjectBaseFactory(); + _objectPathFactory = new ObjectPathFactoryForSpecs(); + sut = new NeighborhoodBuilderFactory(_objectBaseFactory, _objectPathFactory); } } - - - - public class When_creating_a_new_neigborhood_builder : concern_for_NeighborhoodBuilderFactory + public class When_creating_a_new_neighborhood_builder : concern_for_NeighborhoodBuilderFactory { - private INeighborhoodBuilder _result; + private NeighborhoodBuilder _result; protected override void Because() { - _result= sut.Create(); + _result = sut.Create(); } + [Observation] - public void should_create_a_new_neiighborhood_builder() + public void should_create_a_new_neighborhood_builder() { _result.ShouldNotBeNull(); } + [Observation] public void should_have_set_the_right_molecule_properties_container() { @@ -43,18 +44,17 @@ public void should_have_set_the_right_molecule_properties_container() } } - class When_creating_a_neighborhood_between_two_containers : concern_for_NeighborhoodBuilderFactory { private IContainer _secondNeighbor; private IContainer _firstNeighbor; - private INeighborhoodBuilder _result; + private NeighborhoodBuilder _result; protected override void Context() { base.Context(); - _firstNeighbor = A.Fake(); - _secondNeighbor = A.Fake(); + _firstNeighbor = new Container().WithName("A"); + _secondNeighbor = new Container().WithName("B"); } protected override void Because() @@ -65,8 +65,8 @@ protected override void Because() [Observation] public void should_have_set_the_neighbors_right() { - _result.FirstNeighbor.ShouldBeEqualTo(_firstNeighbor); - _result.SecondNeighbor.ShouldBeEqualTo(_secondNeighbor); + _result.FirstNeighborPath.PathAsString.ShouldBeEqualTo(_objectPathFactory.CreateAbsoluteObjectPath(_firstNeighbor)); + _result.SecondNeighborPath.PathAsString.ShouldBeEqualTo(_objectPathFactory.CreateAbsoluteObjectPath(_secondNeighbor)); } } -} \ No newline at end of file +} \ No newline at end of file diff --git a/tests/OSPSuite.Core.Tests/Domain/NeighborhoodExtensionsSpecs.cs b/tests/OSPSuite.Core.Tests/Domain/NeighborhoodExtensionsSpecs.cs index 6509c14ec..71b94d15e 100644 --- a/tests/OSPSuite.Core.Tests/Domain/NeighborhoodExtensionsSpecs.cs +++ b/tests/OSPSuite.Core.Tests/Domain/NeighborhoodExtensionsSpecs.cs @@ -4,18 +4,18 @@ namespace OSPSuite.Core.Domain { - public abstract class concern_for_neighborhood_extensions : StaticContextSpecification + public abstract class concern_for_NeighborhoodExtensions : StaticContextSpecification { - protected INeighborhood _neighborhood; + protected Neighborhood _neighborhood; protected override void Context() { - _neighborhood = A.Fake(); + _neighborhood = A.Fake(); } } - public class When_setting_the_first_neighbor_of_a_neighborhood_with_the_extension : concern_for_neighborhood_extensions + public class When_setting_the_first_neighbor_of_a_neighborhood_with_the_extension : concern_for_NeighborhoodExtensions { private IContainer _firstContainer; @@ -37,7 +37,7 @@ public void should_set_the_first_neighbor_in_the_neighborhood_to_the_given_conta } } - public class When_setting_the_second_neighbor_of_a_neighborhood_with_the_extension : concern_for_neighborhood_extensions + public class When_setting_the_second_neighbor_of_a_neighborhood_with_the_extension : concern_for_NeighborhoodExtensions { private IContainer _firstContainer; diff --git a/tests/OSPSuite.Core.Tests/Domain/NeighborhoodSpecs.cs b/tests/OSPSuite.Core.Tests/Domain/NeighborhoodSpecs.cs index ec9d00793..3faf0fdb0 100644 --- a/tests/OSPSuite.Core.Tests/Domain/NeighborhoodSpecs.cs +++ b/tests/OSPSuite.Core.Tests/Domain/NeighborhoodSpecs.cs @@ -6,7 +6,7 @@ namespace OSPSuite.Core.Domain { - public abstract class concern_for_neighborhood : ContextSpecification + public abstract class concern_for_Neighborhood : ContextSpecification { protected IContainer _firstNeighbor; protected IContainer _secondNeighbor; @@ -14,15 +14,14 @@ public abstract class concern_for_neighborhood : ContextSpecification(); - _secondNeighbor=A.Fake(); + _firstNeighbor = A.Fake(); + _secondNeighbor = A.Fake(); sut.FirstNeighbor = _firstNeighbor; sut.SecondNeighbor = _secondNeighbor; - } } - public class When_retrieving_the_neighboor_satisfying_a_criteria : concern_for_neighborhood + public class When_retrieving_the_neighbor_satisfying_a_criteria : concern_for_Neighborhood { private DescriptorCriteria _aNonExistingCriteria; private DescriptorCriteria _criteriaForSecondNeighbor; @@ -34,13 +33,14 @@ protected override void Context() _aNonExistingCriteria = A.Fake(); _criteriaForSecondNeighbor = A.Fake(); _criteriaForBothNeighbors = A.Fake(); - A.CallTo(()=>_aNonExistingCriteria.IsSatisfiedBy(_firstNeighbor)).Returns(false); - A.CallTo(()=>_aNonExistingCriteria.IsSatisfiedBy(_secondNeighbor)).Returns(false); - A.CallTo(()=>_criteriaForSecondNeighbor.IsSatisfiedBy(_firstNeighbor)).Returns(false); - A.CallTo(()=>_criteriaForSecondNeighbor.IsSatisfiedBy(_secondNeighbor)).Returns(true); - A.CallTo(()=>_criteriaForBothNeighbors.IsSatisfiedBy(_firstNeighbor)).Returns(true); + A.CallTo(() => _aNonExistingCriteria.IsSatisfiedBy(_firstNeighbor)).Returns(false); + A.CallTo(() => _aNonExistingCriteria.IsSatisfiedBy(_secondNeighbor)).Returns(false); + A.CallTo(() => _criteriaForSecondNeighbor.IsSatisfiedBy(_firstNeighbor)).Returns(false); + A.CallTo(() => _criteriaForSecondNeighbor.IsSatisfiedBy(_secondNeighbor)).Returns(true); + A.CallTo(() => _criteriaForBothNeighbors.IsSatisfiedBy(_firstNeighbor)).Returns(true); A.CallTo(() => _criteriaForBothNeighbors.IsSatisfiedBy(_secondNeighbor)).Returns(true); } + [Observation] public void should_return_one_neighbor_satisfying_that_criteria_if_available() { @@ -51,22 +51,22 @@ public void should_return_one_neighbor_satisfying_that_criteria_if_available() public void should_return_null_if_none_of_the_neighbor_in_the_neighborhood_matches_the_criteria() { sut.GetNeighborSatisfying(_aNonExistingCriteria).ShouldBeNull(); - } + [Observation] public void should_throw_an_exception_if_both_neighbors_are_matching_the_criteria() { The.Action(() => sut.GetNeighborSatisfying(_criteriaForBothNeighbors)).ShouldThrowAn(); } - } - public class When_checking_if_a_neighborhood_satifies_a_couple_of_criteria : concern_for_neighborhood + public class When_checking_if_a_neighborhood_satisfies_a_couple_of_criteria : concern_for_Neighborhood { private DescriptorCriteria _aNonExistingCriteria; private DescriptorCriteria _criteriaForFirstNeighbor; private DescriptorCriteria _criteriaForSecondNeighbor; private DescriptorCriteria _criteriaForBothNeighbors; + protected override void Context() { base.Context(); @@ -74,30 +74,29 @@ protected override void Context() _criteriaForSecondNeighbor = A.Fake(); _criteriaForFirstNeighbor = A.Fake(); _criteriaForBothNeighbors = A.Fake(); - A.CallTo(()=>_aNonExistingCriteria.IsSatisfiedBy(_firstNeighbor)).Returns(false); - A.CallTo(()=>_aNonExistingCriteria.IsSatisfiedBy(_secondNeighbor)).Returns(false); - A.CallTo(()=>_criteriaForSecondNeighbor.IsSatisfiedBy(_firstNeighbor)).Returns(false); - A.CallTo(()=>_criteriaForSecondNeighbor.IsSatisfiedBy(_secondNeighbor)).Returns(true); - A.CallTo(()=>_criteriaForFirstNeighbor.IsSatisfiedBy(_firstNeighbor)).Returns(true); - A.CallTo(()=>_criteriaForFirstNeighbor.IsSatisfiedBy(_secondNeighbor)).Returns(false); - A.CallTo(()=>_criteriaForBothNeighbors.IsSatisfiedBy(_firstNeighbor)).Returns(true); + A.CallTo(() => _aNonExistingCriteria.IsSatisfiedBy(_firstNeighbor)).Returns(false); + A.CallTo(() => _aNonExistingCriteria.IsSatisfiedBy(_secondNeighbor)).Returns(false); + A.CallTo(() => _criteriaForSecondNeighbor.IsSatisfiedBy(_firstNeighbor)).Returns(false); + A.CallTo(() => _criteriaForSecondNeighbor.IsSatisfiedBy(_secondNeighbor)).Returns(true); + A.CallTo(() => _criteriaForFirstNeighbor.IsSatisfiedBy(_firstNeighbor)).Returns(true); + A.CallTo(() => _criteriaForFirstNeighbor.IsSatisfiedBy(_secondNeighbor)).Returns(false); + A.CallTo(() => _criteriaForBothNeighbors.IsSatisfiedBy(_firstNeighbor)).Returns(true); A.CallTo(() => _criteriaForBothNeighbors.IsSatisfiedBy(_secondNeighbor)).Returns(true); - } + [Observation] - public void should_return_true_if_each_neigbhors_satisfies_at_least_one_criteria_but_not_the_same_as_the_other_neighbor() + public void should_return_true_if_each_neighbors_satisfies_at_least_one_criteria_but_not_the_same_as_the_other_neighbor() { - sut.Satisfies(_criteriaForFirstNeighbor,_criteriaForSecondNeighbor).ShouldBeTrue(); + sut.Satisfies(_criteriaForFirstNeighbor, _criteriaForSecondNeighbor).ShouldBeTrue(); sut.Satisfies(_criteriaForSecondNeighbor, _criteriaForFirstNeighbor).ShouldBeTrue(); sut.Satisfies(_criteriaForSecondNeighbor, _criteriaForBothNeighbors).ShouldBeTrue(); sut.Satisfies(_criteriaForBothNeighbors, _criteriaForSecondNeighbor).ShouldBeTrue(); } [Observation] - public void should_return_false_if_both_neighbors_match_the_same_criteria_and_one_criteria_reamains_unmatch() + public void should_return_false_if_both_neighbors_match_the_same_criteria_and_one_criteria_remains_unmatched() { sut.Satisfies(_criteriaForBothNeighbors, _aNonExistingCriteria).ShouldBeFalse(); - } [Observation] @@ -105,19 +104,16 @@ public void should_return_false_if_one_neighbor_does_not_match_any_criteria() { sut.Satisfies(_aNonExistingCriteria, _criteriaForFirstNeighbor).ShouldBeFalse(); sut.Satisfies(_aNonExistingCriteria, _criteriaForSecondNeighbor).ShouldBeFalse(); - } - } - - - public class When_checking_if_a_neighborhood_strictly_satifies_a_couple_of_criteria : concern_for_neighborhood + public class When_checking_if_a_neighborhood_strictly_satisfies_a_couple_of_criteria : concern_for_Neighborhood { private DescriptorCriteria _aNonExistingCriteria; private DescriptorCriteria _criteriaForFirstNeighbor; private DescriptorCriteria _criteriaForSecondNeighbor; private DescriptorCriteria _criteriaForBothNeighbors; + protected override void Context() { base.Context(); @@ -125,18 +121,18 @@ protected override void Context() _criteriaForSecondNeighbor = A.Fake(); _criteriaForFirstNeighbor = A.Fake(); _criteriaForBothNeighbors = A.Fake(); - A.CallTo(()=>_aNonExistingCriteria.IsSatisfiedBy(_firstNeighbor)).Returns(false); - A.CallTo(()=>_aNonExistingCriteria.IsSatisfiedBy(_secondNeighbor)).Returns(false); - A.CallTo(()=>_criteriaForSecondNeighbor.IsSatisfiedBy(_firstNeighbor)).Returns(false); - A.CallTo(()=>_criteriaForSecondNeighbor.IsSatisfiedBy(_secondNeighbor)).Returns(true); - A.CallTo(()=>_criteriaForFirstNeighbor.IsSatisfiedBy(_firstNeighbor)).Returns(true); - A.CallTo(()=>_criteriaForFirstNeighbor.IsSatisfiedBy(_secondNeighbor)).Returns(false); - A.CallTo(()=>_criteriaForBothNeighbors.IsSatisfiedBy(_firstNeighbor)).Returns(true); + A.CallTo(() => _aNonExistingCriteria.IsSatisfiedBy(_firstNeighbor)).Returns(false); + A.CallTo(() => _aNonExistingCriteria.IsSatisfiedBy(_secondNeighbor)).Returns(false); + A.CallTo(() => _criteriaForSecondNeighbor.IsSatisfiedBy(_firstNeighbor)).Returns(false); + A.CallTo(() => _criteriaForSecondNeighbor.IsSatisfiedBy(_secondNeighbor)).Returns(true); + A.CallTo(() => _criteriaForFirstNeighbor.IsSatisfiedBy(_firstNeighbor)).Returns(true); + A.CallTo(() => _criteriaForFirstNeighbor.IsSatisfiedBy(_secondNeighbor)).Returns(false); + A.CallTo(() => _criteriaForBothNeighbors.IsSatisfiedBy(_firstNeighbor)).Returns(true); A.CallTo(() => _criteriaForBothNeighbors.IsSatisfiedBy(_secondNeighbor)).Returns(true); - } + [Observation] - public void should_return_true_if_the_first_neighbor_satisfies_the_first_criteria_and_the_second_neighboor_the_second_criteria() + public void should_return_true_if_the_first_neighbor_satisfies_the_first_criteria_and_the_second_neighbor_the_second_criteria() { sut.StrictlySatisfies(_criteriaForFirstNeighbor, _criteriaForSecondNeighbor).ShouldBeTrue(); } @@ -145,7 +141,6 @@ public void should_return_true_if_the_first_neighbor_satisfies_the_first_criteri public void should_return_false_if_the_first_neighbor_satisfies_the_second_criteria_and_the_second_neighbor_satisfies_the_first_criteria() { sut.StrictlySatisfies(_criteriaForSecondNeighbor, _criteriaForFirstNeighbor).ShouldBeFalse(); - } [Observation] @@ -154,6 +149,5 @@ public void should_return_false_if_one_neighbor_does_not_match_any_criteria() sut.StrictlySatisfies(_criteriaForFirstNeighbor, _aNonExistingCriteria).ShouldBeFalse(); sut.StrictlySatisfies(_aNonExistingCriteria, _criteriaForSecondNeighbor).ShouldBeFalse(); } - } -} \ No newline at end of file +} \ No newline at end of file diff --git a/tests/OSPSuite.Core.Tests/Mappers/NeighborhoodBuilderToNeighborhoodMapperSpecs.cs b/tests/OSPSuite.Core.Tests/Mappers/NeighborhoodBuilderToNeighborhoodMapperSpecs.cs index f45a026f0..b1243d46b 100644 --- a/tests/OSPSuite.Core.Tests/Mappers/NeighborhoodBuilderToNeighborhoodMapperSpecs.cs +++ b/tests/OSPSuite.Core.Tests/Mappers/NeighborhoodBuilderToNeighborhoodMapperSpecs.cs @@ -13,7 +13,6 @@ public abstract class concern_for_NeighborhoodBuilderToNeighborhoodMapper : Cont { protected IContainerBuilderToContainerMapper _containerMapper; protected IObjectBaseFactory _objectBaseFactory; - protected IObjectPathFactory _objectPathFactory; protected ICloneManagerForModel _cloneManagerForModel; protected IKeywordReplacerTask _keywordReplacerTask; protected IParameterBuilderToParameterMapper _parameterMapper; @@ -22,18 +21,17 @@ protected override void Context() { _objectBaseFactory = A.Fake(); _containerMapper = A.Fake(); - _objectPathFactory = A.Fake(); _cloneManagerForModel = A.Fake(); _keywordReplacerTask = A.Fake(); _parameterMapper = A.Fake(); - sut = new NeighborhoodBuilderToNeighborhoodMapper(_objectBaseFactory, _containerMapper, _objectPathFactory, _keywordReplacerTask, _cloneManagerForModel, _parameterMapper); + sut = new NeighborhoodBuilderToNeighborhoodMapper(_objectBaseFactory, _containerMapper, _keywordReplacerTask, _cloneManagerForModel, _parameterMapper); } } public class When_mapping_a_neighborhood_builder_to_a_neighborhood : concern_for_NeighborhoodBuilderToNeighborhoodMapper { - private INeighborhoodBuilder _neighborhoodBuilder; - private INeighborhood _neighborhood; + private NeighborhoodBuilder _neighborhoodBuilder; + private Neighborhood _neighborhood; private IEnumerable _moleculeNames; private IContainer _rootContainer; private string _molecule1; @@ -53,25 +51,23 @@ protected override void Context() _buildConfiguration = A.Fake(); _model = A.Fake(); _model.Root = _rootContainer; - _neighborhoodBuilder = A.Fake().WithName("tralala"); - _neighborhoodBuilder.FirstNeighbor = A.Fake(); - _neighborhoodBuilder.SecondNeighbor = A.Fake(); - A.CallTo(() => _neighborhoodBuilder.MoleculeProperties).Returns(A.Fake()); - var para1 = A.Fake(); - var para2 = A.Fake(); - A.CallTo(() => _neighborhoodBuilder.Parameters).Returns(new[] {para1, para2}); - _clonePara1 = A.Fake(); - _clonePara2 = A.Fake(); - var firstNeighborBuilderPath = A.Fake(); - var secondNeighborBuilderPath = A.Fake(); - A.CallTo(() => _objectPathFactory.CreateAbsoluteObjectPath(_neighborhoodBuilder.FirstNeighbor)).Returns(firstNeighborBuilderPath); - A.CallTo(() => _objectPathFactory.CreateAbsoluteObjectPath(_neighborhoodBuilder.SecondNeighbor)).Returns(secondNeighborBuilderPath); + _neighborhoodBuilder =new NeighborhoodBuilder().WithName("tralala"); + _neighborhoodBuilder.Add(new Container().WithName(Constants.MOLECULE_PROPERTIES)); + + var para1 = new Parameter().WithName("Para1"); + var para2 = new Parameter().WithName("Para2"); + _neighborhoodBuilder.AddParameter(para1); + _neighborhoodBuilder.AddParameter(para2); + _clonePara1 = new Parameter().WithName("Para1"); + _clonePara2 = new Parameter().WithName("Para2"); + _neighborhoodBuilder.FirstNeighborPath = A.Fake(); + _neighborhoodBuilder.SecondNeighborPath = A.Fake(); var firstNeighborModelPath = A.Fake(); var secondNeighborModelPath = A.Fake(); _firstNeighborInModel = A.Fake(); _secondNeighborInModel = A.Fake(); - A.CallTo(() => _keywordReplacerTask.CreateModelPathFor(firstNeighborBuilderPath, _model.Root)).Returns(firstNeighborModelPath); - A.CallTo(() => _keywordReplacerTask.CreateModelPathFor(secondNeighborBuilderPath, _model.Root)).Returns(secondNeighborModelPath); + A.CallTo(() => _keywordReplacerTask.CreateModelPathFor(_neighborhoodBuilder.FirstNeighborPath, _model.Root)).Returns(firstNeighborModelPath); + A.CallTo(() => _keywordReplacerTask.CreateModelPathFor(_neighborhoodBuilder.SecondNeighborPath, _model.Root)).Returns(secondNeighborModelPath); A.CallTo(() => firstNeighborModelPath.Resolve(_rootContainer)).Returns(_firstNeighborInModel); A.CallTo(() => secondNeighborModelPath.Resolve(_rootContainer)).Returns(_secondNeighborInModel); _moleculeContainer = A.Fake(); @@ -79,7 +75,7 @@ protected override void Context() _molecule1 = "molecule1"; _molecule2 = "molecule2"; _moleculeNames = new List {_molecule1, _molecule2}; - A.CallTo(() => _objectBaseFactory.Create()).Returns(A.Fake()); + A.CallTo(() => _objectBaseFactory.Create()).Returns(new Neighborhood()); A.CallTo(() => _parameterMapper.MapFrom(para1, _buildConfiguration)).Returns(_clonePara1); A.CallTo(() => _parameterMapper.MapFrom(para2, _buildConfiguration)).Returns(_clonePara2); } @@ -92,14 +88,13 @@ protected override void Because() [Observation] public void should_return_a_neighborhood_whose_name_was_set_to_the_name_of_the_neighborhood_builder() { - A.CallTo(() => _neighborhood.UpdatePropertiesFrom(_neighborhoodBuilder, _cloneManagerForModel)).MustHaveHappened(); + _neighborhood.Name.ShouldBeEqualTo(_neighborhoodBuilder.Name); } [Observation] public void should_have_added_a_clone_of_the_neighborhood_parameters_to_the_created_neighborhood() { - A.CallTo(() => _neighborhood.Add(_clonePara1)).MustHaveHappened(); - A.CallTo(() => _neighborhood.Add(_clonePara2)).MustHaveHappened(); + _neighborhood.AllParameters().ShouldContain(_clonePara1, _clonePara2); } [Observation] diff --git a/tests/OSPSuite.HelpersForTests/TestObjectBaseFactory.cs b/tests/OSPSuite.HelpersForTests/TestObjectBaseFactory.cs index c061fde93..0b60c8dd8 100644 --- a/tests/OSPSuite.HelpersForTests/TestObjectBaseFactory.cs +++ b/tests/OSPSuite.HelpersForTests/TestObjectBaseFactory.cs @@ -15,7 +15,7 @@ public T Create() where T : class, IObjectBase if (typeof(T).IsAnImplementationOf()) return new MoleculeAmount().DowncastTo(); - if (typeof(T).IsAnImplementationOf()) + if (typeof(T).IsAnImplementationOf()) return new NeighborhoodBuilder().DowncastTo(); return A.Fake();