Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #603 extension of formula reference #1899

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/OSPSuite.Assets/UIConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,8 @@ public static string TimeArrayValuesDoesNotMatchFirstIndividual(int id, int inde

public static string UnitIsNotDefinedInDimension(string unit, string dimension) => $"Unit '{unit}' is not defined in dimension '{dimension}'.";

public static string CouldNotFindNeighborhoodBetween(string container1, string container2) => $"Could not find neighborhood between '{container1}' and '{container2}'";

public static class SensitivityAnalysis
{
public static readonly string NoSimulationDefined = "No simulation defined";
Expand Down
26 changes: 13 additions & 13 deletions src/OSPSuite.Core/Domain/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public interface IContainer : IEntity, IEnumerable<IEntity>
IEnumerable<T> GetChildren<T>(Func<T, bool> predicate) where T : class, IEntity;

/// <summary>
/// returns the neighbors from the container connected by the given neigborhoods.
/// returns the neighbors from the container connected by the given neighborhoods.
/// </summary>
IEnumerable<IContainer> GetNeighborsFrom(IEnumerable<INeighborhood> neighborhoods);
IReadOnlyList<IContainer> GetNeighborsFrom(IReadOnlyList<INeighborhood> neighborhoods);

/// <summary>
/// returns the neighborhoods connecting the container with its neighbors.
/// </summary>
/// <param name="neighborhoods"> The possible neighborhoods. </param>
IEnumerable<INeighborhood> GetNeighborhoods(IEnumerable<INeighborhood> neighborhoods);
IReadOnlyList<INeighborhood> GetNeighborhoods(IReadOnlyList<INeighborhood> neighborhoods);

/// <summary>
/// Returns all children containers defined and the container itself, if the container is form type
Expand Down Expand Up @@ -204,30 +204,30 @@ where predicate(castChild)
select castChild;
}

public virtual IEnumerable<IContainer> GetNeighborsFrom(IEnumerable<INeighborhood> neighborhoods)
public virtual IReadOnlyList<IContainer> GetNeighborsFrom(IReadOnlyList<INeighborhood> neighborhoods)
{
var allNeighborhoods = neighborhoods.ToList();
var first = from neighborhood in GetNeighborhoods(allNeighborhoods)
var first = from neighborhood in GetNeighborhoods(neighborhoods)
where neighborhood.FirstNeighbor != this
select neighborhood.FirstNeighbor;

var second = from neighborhood in GetNeighborhoods(allNeighborhoods)
var second = from neighborhood in GetNeighborhoods(neighborhoods)
where neighborhood.SecondNeighbor != this
select neighborhood.SecondNeighbor;
return first.Union(second);

return first.Union(second).ToList();
}

public virtual IEnumerable<INeighborhood> GetNeighborhoods(IEnumerable<INeighborhood> neighborhoods)
public virtual IReadOnlyList<INeighborhood> GetNeighborhoods(IReadOnlyList<INeighborhood> neighborhoods)
{
var allNeighborhoods = neighborhoods.ToList();
var first = from neighborhood in allNeighborhoods
var first = from neighborhood in neighborhoods
where neighborhood.FirstNeighbor == this
select neighborhood;

var second = from neighborhood in allNeighborhoods
var second = from neighborhood in neighborhoods
where neighborhood.SecondNeighbor == this
select neighborhood;
return first.Union(second);

return first.Union(second).ToList();
}

public IReadOnlyList<TContainer> GetAllContainersAndSelf<TContainer>() where TContainer : class, IContainer => GetAllContainersAndSelf<TContainer>(x => true);
Expand Down
2 changes: 1 addition & 1 deletion src/OSPSuite.Core/Domain/EntityRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static IBusinessRule NotEmptyName

public static IEnumerable<IBusinessRule> All()
{
return _allEntityRules;
return _allEntityRules;
}
}
}
5 changes: 2 additions & 3 deletions src/OSPSuite.Core/Domain/FormulaUsablePath.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using OSPSuite.Utility.Extensions;
using OSPSuite.Core.Domain.UnitSystem;
using OSPSuite.Utility.Extensions;

namespace OSPSuite.Core.Domain
{
Expand Down Expand Up @@ -32,8 +32,7 @@ public override T Clone<T>()
{
Alias = Alias,
Dimension = Dimension
}
.DowncastTo<T>();
}.DowncastTo<T>();
}

public bool Equals(FormulaUsablePath other)
Expand Down
12 changes: 11 additions & 1 deletion src/OSPSuite.Core/Domain/Formulas/FormulaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public static bool IsExplicit(this IFormula formula)
return formula.IsAnImplementationOf<ExplicitFormula>();
}

/// <summary>
/// Returns true if the formula contains at least one entry using neighborhood reference otherwise false
/// </summary>
/// <param name="formula"></param>
public static bool IsReferencingNeighborhood(this IFormula formula)
{
return formula.ObjectPaths.Any(x => x.Contains(ObjectPathKeywords.NBH));
Yuri05 marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Returns true the formula is a dynamic formula otherwise false
/// </summary>
Expand Down Expand Up @@ -83,7 +92,8 @@ public static bool IsCachable(this IFormula formula)
}

/// <summary>
/// Returns the used object path with the given <paramref name="alias"/> in the <paramref name="formula"/> or null if the <paramref name="alias"/> is not used.
/// Returns the used object path with the given <paramref name="alias" /> in the <paramref name="formula" /> or null if
/// the <paramref name="alias" /> is not used.
/// </summary>
public static IFormulaUsablePath FormulaUsablePathBy(this IFormula formula, string alias)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace OSPSuite.Core.Domain.Mappers
/// <para></para>
/// Creates Top-Container named "NEIGHBORHOODS", all mapped neighborhoods
/// <para></para>
/// are added as childs of this top container
/// are added as children of this top container
/// </summary>
public interface INeighborhoodCollectionToContainerMapper : IBuilderMapper<IModel, IContainer>
{
Expand All @@ -23,7 +23,8 @@ public class NeighborhoodCollectionToContainerMapper : INeighborhoodCollectionTo
private readonly INeighborhoodBuilderToNeighborhoodMapper _neighborhoodMapper;
private readonly IObjectPathFactory _objectPathFactory;

public NeighborhoodCollectionToContainerMapper(IObjectBaseFactory objectBaseFactory,
public NeighborhoodCollectionToContainerMapper(
IObjectBaseFactory objectBaseFactory,
INeighborhoodBuilderToNeighborhoodMapper neighborhoodMapper,
IObjectPathFactory objectPathFactory)
{
Expand Down Expand Up @@ -84,15 +85,15 @@ private ICache<string, IList<string>> presentMoleculesCachedByContainerPath(IEnu
}

/// <summary>
/// Returns molecules which will be created in both neighbours of the neighbourhood
/// Returns molecules which will be created in both neighbors of the neighborhood
/// </summary>
private IEnumerable<string> moleculeNamesFor(INeighborhoodBuilder neighborhoodBuilder,
ICache<string, IList<string>> moleculesStartValuesForFloatingMolecules)
{
var pathToFirstNeighbor = _objectPathFactory.CreateAbsoluteObjectPath(neighborhoodBuilder.FirstNeighbor).ToString();
var pathToSecondNeighbor = _objectPathFactory.CreateAbsoluteObjectPath(neighborhoodBuilder.SecondNeighbor).ToString();

// check if both neighbours has at least 1 molecule (if not - return empty list)
// check if both neighbors has at least 1 molecule (if not - return empty list)
if (!moleculesStartValuesForFloatingMolecules.Contains(pathToFirstNeighbor) ||
!moleculesStartValuesForFloatingMolecules.Contains(pathToSecondNeighbor))
return new List<string>();
Expand Down
16 changes: 7 additions & 9 deletions src/OSPSuite.Core/Domain/ObjectPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ public interface IObjectPath : IReadOnlyCollection<string>
/// </summary>
void RemoveFirst();


/// <summary>
/// Replaces the path with the path entries in <paramref name="pathEntries"/>
/// Replaces the path with the path entries in <paramref name="pathEntries" />
/// </summary>
/// <param name="pathEntries">Path entries used to replace the path</param>
void ReplaceWith(IEnumerable<string> pathEntries);
Expand All @@ -97,11 +96,11 @@ public class ObjectPath : IObjectPath
/// <summary>
/// String separating elements of the <see cref="FormulaUsablePath" /> in String Representation
/// </summary>
public const string PATH_DELIMITER = "|";
public const string PATH_DELIMITER = "|";

protected readonly List<string> _pathEntries;

public static IObjectPath Empty { get; } = new ObjectPath();
public static IObjectPath Empty { get; } = new ObjectPath();

public ObjectPath() : this(new List<string>())
{
Expand Down Expand Up @@ -129,6 +128,7 @@ public virtual string PathAsString
returnString.Append(PATH_DELIMITER);
returnString.Append(str);
}

return returnString.ToString().Substring(PATH_DELIMITER.Length);
}
}
Expand Down Expand Up @@ -198,7 +198,6 @@ public virtual T Resolve<T>(IEntity refEntity) where T : class
return resolvePath<T>(dependentObject, usePath);
}


public virtual T Clone<T>() where T : IObjectPath
{
return new ObjectPath(_pathEntries).DowncastTo<T>();
Expand Down Expand Up @@ -278,6 +277,7 @@ public bool Equals(ObjectPath other)
if (!_pathEntries[i].Equals(entry)) return false;
i++;
}

return true;
}

Expand All @@ -299,13 +299,11 @@ public override int GetHashCode()
hashCode = hashCode ^ _pathEntries[i].GetHashCode();
}
}

return hashCode;
}

public override string ToString()
{
return PathAsString;
}
public override string ToString() => PathAsString;

public static implicit operator string(ObjectPath objectPath)
{
Expand Down
8 changes: 7 additions & 1 deletion src/OSPSuite.Core/Domain/ObjectPathKeywords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class ObjectPathKeywords
public static readonly string TARGET = addKeyword("TARGET");

/// <summary>
/// String representing a reference to the Neighborhood there the transport is created
/// String representing a reference to the Neighborhood where the transport is created
/// </summary>
public static readonly string NEIGHBORHOOD = addKeyword("NEIGHBORHOOD");

Expand Down Expand Up @@ -61,6 +61,12 @@ public static class ObjectPathKeywords
/// </summary>
public static readonly string ALL_FLOATING_MOLECULES = addKeyword("ALL_FLOATING_MOLECULES");

/// <summary>
/// Represents the keywords allowing us to identify a neighborhood between two containers
/// For example CONTAINER1|/<NBH/>CONTAINER2/<NBH/> means the neighborhood between CONTAINER1 and CONTAINER2
/// </summary>
public static readonly string NBH = addKeyword("<NBH>");
Yuri05 marked this conversation as resolved.
Show resolved Hide resolved

private static string addKeyword(string keyword)
{
_allKeywords.Add(keyword);
Expand Down
4 changes: 2 additions & 2 deletions src/OSPSuite.Core/Domain/QuantityAndContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public void Add(IEntity newChild)

public IEnumerable<T> GetChildren<T>() where T : class, IEntity => _container.GetChildren<T>();

public IEnumerable<IContainer> GetNeighborsFrom(IEnumerable<INeighborhood> neighborhoods) => _container.GetNeighborsFrom(neighborhoods);
public IReadOnlyList<IContainer> GetNeighborsFrom(IReadOnlyList<INeighborhood> neighborhoods) => _container.GetNeighborsFrom(neighborhoods);

public IEnumerable<INeighborhood> GetNeighborhoods(IEnumerable<INeighborhood> neighborhoods) => _container.GetNeighborhoods(neighborhoods);
public IReadOnlyList<INeighborhood> GetNeighborhoods(IReadOnlyList<INeighborhood> neighborhoods) => _container.GetNeighborhoods(neighborhoods);

public IReadOnlyList<TContainer> GetAllContainersAndSelf<TContainer>() where TContainer : class, IContainer => GetAllContainersAndSelf<TContainer>(x => true);

Expand Down
Loading