Skip to content

Commit

Permalink
Fixes #1920 create neighborhood builder with path instead of containe…
Browse files Browse the repository at this point in the history
…r: (#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
  • Loading branch information
msevestre authored Mar 10, 2023
1 parent 4963347 commit 4171cd2
Show file tree
Hide file tree
Showing 44 changed files with 503 additions and 448 deletions.
1 change: 1 addition & 0 deletions src/OSPSuite.Assets.Images/ApplicationIcons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/OSPSuite.Assets/UIConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down
60 changes: 45 additions & 15 deletions src/OSPSuite.Core/Comparison/NeighborhoodBaseDiffBuilder.cs
Original file line number Diff line number Diff line change
@@ -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<INeighborhoodBase>
public abstract class NeighborhoodBaseDiffBuilder<TNeighborhood> : DiffBuilder<TNeighborhood> 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<INeighborhoodBase> comparison)
public override void Compare(IComparison<TNeighborhood> comparison)
{
_containerDiffBuilder.Compare(comparison);
compareNeighbors(comparison);
}

private void compareNeighbors(IComparison<INeighborhoodBase> comparison)
private void compareNeighbors(IComparison<TNeighborhood> 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<TNeighborhood> comparison);
}

public class NeighborhoodBuilderDiffBuilder : NeighborhoodBaseDiffBuilder<NeighborhoodBuilder>
{
public NeighborhoodBuilderDiffBuilder(ContainerDiffBuilder containerDiffBuilder) : base(containerDiffBuilder)
{
}

protected override (ObjectPath firstNeighbor1Path, ObjectPath secondNeighbor1Path, ObjectPath firstNeighbor2Path, ObjectPath secondNeighbor2Path) GetNeighborsComparisonPath(IComparison<NeighborhoodBuilder> comparison)
{
return (comparison.Object1.FirstNeighborPath, comparison.Object1.SecondNeighborPath, comparison.Object2.FirstNeighborPath, comparison.Object2.SecondNeighborPath);
}
}

public class NeighborhoodDiffBuilder : NeighborhoodBaseDiffBuilder<Neighborhood>
{
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<Neighborhood> 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);
}
}
}
68 changes: 68 additions & 0 deletions src/OSPSuite.Core/Converters/v12/Converter110To120.cs
Original file line number Diff line number Diff line change
@@ -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<ISpatialStructure>,
IVisitor<SimulationTransfer>,
IVisitor<IModelCoreSimulation>,
IVisitor<IBuildConfiguration>
{
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);
}
}
}
58 changes: 37 additions & 21 deletions src/OSPSuite.Core/Domain/Builder/NeighborhoodBuilder.cs
Original file line number Diff line number Diff line change
@@ -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<IParameter> Parameters { get; }
void AddParameter(IParameter newParameter);
void RemoveParameter(IParameter parameterToRemove);
bool IsConnectedTo(IContainer container);
/// <summary>
/// First neighbor in the neighborhood.
/// </summary>
IContainer FirstNeighbor { get; }

/// <summary>
/// Second neighbor in the neighborhood.
/// </summary>
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<IContainer>(Constants.MOLECULE_PROPERTIES); }
}
public IContainer MoleculeProperties => this.Container(Constants.MOLECULE_PROPERTIES);

public IEnumerable<IParameter> Parameters
{
get { return GetChildren<IParameter>(); }
}
public IEnumerable<IParameter> Parameters => GetChildren<IParameter>();

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<IContainer>(container);
SecondNeighbor = SecondNeighborPath.Resolve<IContainer>(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;
}
}
}
29 changes: 15 additions & 14 deletions src/OSPSuite.Core/Domain/Builder/NeighborhoodBuilderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public interface INeighborhoodBuilderFactory
/// Creates a new instance of a <see cref="NeighborhoodBuilder"/>.
/// </summary>
///<returns> the new <see cref="NeighborhoodBuilder"/></returns>
INeighborhoodBuilder Create();
NeighborhoodBuilder Create();
/// <summary>
/// Creates a new <see cref="NeighborhoodBuilder"/> between <paramref name="firstNeighbor"/> and <paramref name="secondNeighbor"/>.
/// </summary>
/// <param name="firstNeighbor">The first neighbor.</param>
/// <param name="secondNeighbor">The second neighbor.</param>
///<returns> the new <see cref="NeighborhoodBuilder"/></returns>
INeighborhoodBuilder CreateBetween(IContainer firstNeighbor, IContainer secondNeighbor);
NeighborhoodBuilder CreateBetween(IContainer firstNeighbor, IContainer secondNeighbor);
}

/// <summary>
Expand All @@ -25,35 +25,36 @@ public interface INeighborhoodBuilderFactory
public class NeighborhoodBuilderFactory : INeighborhoodBuilderFactory
{
private readonly IObjectBaseFactory _objectBaseFactory;
private readonly IObjectPathFactory _objectPathFactory;

/// <summary>
/// Initializes a new instance of the <see cref="NeighborhoodBuilderFactory"/> class.
/// </summary>
/// <param name="objectBaseFactory">The object base factory used to create new objects.</param>
public NeighborhoodBuilderFactory(IObjectBaseFactory objectBaseFactory)
public NeighborhoodBuilderFactory(IObjectBaseFactory objectBaseFactory, IObjectPathFactory objectPathFactory)
{
_objectBaseFactory = objectBaseFactory;
_objectPathFactory = objectPathFactory;
}

/// <summary>
/// Creates a new instance of a <see cref="NeighborhoodBuilder"/>.
/// </summary>
///<returns> the new <see cref="NeighborhoodBuilder"/></returns>
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<INeighborhoodBuilder>();
return _objectBaseFactory.Create<NeighborhoodBuilder>();
}

protected IContainer CreateMoleculeProperties()
Expand All @@ -67,12 +68,12 @@ protected IContainer CreateMoleculeProperties()
/// <param name="firstNeighbor">The first neighbor.</param>
/// <param name="secondNeighbor">The second neighbor.</param>
///<returns> the new <see cref="NeighborhoodBuilder"/></returns>
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;
}
}
}
Loading

0 comments on commit 4171cd2

Please sign in to comment.