Skip to content

Commit

Permalink
Fixes #700 and Fixes #699 (#701)
Browse files Browse the repository at this point in the history
* Fixes #700 and Fixes #699

* Fixes #700 and Fixes #699

* Also remove starter program from nuget
  • Loading branch information
msevestre authored Jan 21, 2020
1 parent 62d344c commit 8005f80
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 36 deletions.
5 changes: 5 additions & 0 deletions src/OSPSuite.Assets/UIConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,11 @@ public static string PathContainsReservedKeywords(string entity, string entityTy
return $"Reference '{reference}' used in {entityType.ToLower()} '{entity}' with path '{entityPath}' contains reserved keywords. Remove them before resolving the path";
}

public static string ModelNameCannotBeNamedLikeATopContainer(IReadOnlyCollection<string> topContainerNames)
{
return $"The simulation name cannot be one of the following reserved terms:\n\t{topContainerNames.ToString(", ", "'")}.\nPlease rename your simulation.";
}

public static string ErrorUnableToFindReference(string entity, string entityType, string entityPath, string reference)
{
return string.Format("{1} '{2}' with path '{3}' references an entity with path '{0}' that cannot be found", reference, entityType, entity, entityPath);
Expand Down
33 changes: 17 additions & 16 deletions src/OSPSuite.Core/Domain/ObjectPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,23 @@ public virtual T Resolve<T>(IEntity refEntity) where T : class
if (_pathEntries.Count == 0)
return null;

var firstEntry = _pathEntries[0];
var root = refEntity.RootContainer;
var usePath = new List<string>(_pathEntries);
IEntity dependentObject;

var dependentObject = refEntity;
//Do we have a valid relative path from the current object
var resolvedEntity = resolvePath<T>(dependentObject, usePath);
if (resolvedEntity != null)
return resolvedEntity;

var root = refEntity.RootContainer;
var firstEntry = _pathEntries[0];

//We have an absolute Path from the ref entity
if (string.Equals(firstEntry, refEntity.Name))
usePath.RemoveAt(0);

//We have an absolute Path from the root container
if (root != null && string.Equals(firstEntry, root.Name))
else if (root != null && string.Equals(firstEntry, root.Name))
{
if (_pathEntries.Count == 1)
return root as T;
Expand All @@ -178,28 +188,19 @@ public virtual T Resolve<T>(IEntity refEntity) where T : class
dependentObject = root;
}

//We have an absolute Path from the ref entity
else if (string.Equals(firstEntry, refEntity.Name))
{
usePath.RemoveAt(0);
dependentObject = refEntity;
}
//We have a relative path
else
dependentObject = refEntity;

return resolvePath<T>(dependentObject, usePath);
}


public virtual T Clone<T>() where T : IObjectPath
{
return new ObjectPath(_pathEntries).DowncastTo<T>();
}

public string this[int index]
{
get { return _pathEntries[index]; }
set { _pathEntries[index] = value; }
get => _pathEntries[index];
set => _pathEntries[index] = value;
}

public void RemoveAt(int index)
Expand Down
2 changes: 1 addition & 1 deletion src/OSPSuite.Core/Domain/Services/KeywordReplacerTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public void ReplaceIn(IObserver observer, IContainer rootContainer, string molec
private void addCommonModelReplacersTo(IKeywordReplacerCollection keywordReplacer, IContainer rootContainer)
{
//Replace the predefined keywords
keywordReplacer.AddReplacement(new TopContainerPathReplacer(rootContainer.Name, rootContainer.GetChildren<IContainer>().Select(x => x.Name)));
keywordReplacer.AddReplacement(new TopContainerPathReplacer(rootContainer.Name, rootContainer.GetChildren<IContainer>().AllNames()));
keywordReplacer.AddReplacement(new TopContainerPathReplacer(rootContainer.Name, new[] {ObjectPathKeywords.MOLECULE, Constants.NEIGHBORHOODS}));
}

Expand Down
12 changes: 9 additions & 3 deletions src/OSPSuite.Core/Domain/Services/ModelConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
using System.Collections.Generic;
using System.Linq;
using OSPSuite.Assets;
using OSPSuite.Utility.Events;
using OSPSuite.Utility.Extensions;
using OSPSuite.Core.Domain.Builder;
using OSPSuite.Core.Domain.Descriptors;
using OSPSuite.Core.Domain.Mappers;
using OSPSuite.Core.Services;
using OSPSuite.Utility.Events;
using OSPSuite.Utility.Extensions;

namespace OSPSuite.Core.Domain.Services
{
Expand Down Expand Up @@ -78,6 +78,7 @@ public CreationResult CreateModelFrom(IBuildConfiguration buildConfiguration, st
//One function per process step
checkBuildConfiguration,
createModelStructure,
validateModelName,
createProcesses,
createObserversAndEvents,
setQuantitiesValues);
Expand Down Expand Up @@ -155,6 +156,7 @@ private CreationResult buildProcess(IModel model, IBuildConfiguration buildConfi
{
progress?.Dispose();
}

return result;
}

Expand Down Expand Up @@ -186,10 +188,14 @@ private ValidationResult createModelStructure(IModel model, IBuildConfiguration
// replace all keywords define in the model structure
_keywordReplacerTask.ReplaceIn(model.Root);


return new ValidationResult(moleculeMessages);
}

private ValidationResult validateModelName(IModel model, IBuildConfiguration buildConfiguration)
{
return validate<ModelNameValidator>(model, buildConfiguration);
}

private void createMoleculeCalculationMethodsFormula(IModel model, IBuildConfiguration buildConfiguration)
{
_calculationMethodTask.MergeCalculationMethodInModel(model, buildConfiguration);
Expand Down
18 changes: 18 additions & 0 deletions src/OSPSuite.Core/Domain/Services/ModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,22 @@ public void Visit(IEventAssignment eventAssignment)
CheckPath(eventAssignment, eventAssignment.ObjectPath, ResolveErrorBehavior.Error);
}
}

internal class ModelNameValidator : IModelValidator
{
public ValidationResult Validate(IObjectBase objectToValidate, IBuildConfiguration buildConfiguration)
{
var model = objectToValidate as IModel;
var result = new ValidationResult();
if (model == null)
return result;

var allTopContainerNames = model.Root.GetChildren<IContainer>().AllNames();
if (!allTopContainerNames.Contains(model.Name))
return result;

result.AddMessage(NotificationType.Error, objectToValidate, Validation.ModelNameCannotBeNamedLikeATopContainer(allTopContainerNames));
return result;
}
}
}
7 changes: 4 additions & 3 deletions src/OSPSuite.Core/Domain/Services/TopContainerPathReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
namespace OSPSuite.Core.Domain.Services
{
/// <summary>
/// Add the modelname at the begining of the path, if the path starts with one of the top container names
/// Adds the root name at the beginning of the path, if the path starts with one of the top container names provided as
/// parameters or if the path does not start with the root name already
/// </summary>
public class TopContainerPathReplacer : IKeywordInObjectPathReplacer
{
private readonly string _rootName;
private readonly IEnumerable<string> _topContainerNames;
private readonly IReadOnlyList<string> _topContainerNames;

public TopContainerPathReplacer(string rootName, IEnumerable<string> topContainerNames)
public TopContainerPathReplacer(string rootName, IReadOnlyList<string> topContainerNames)
{
_rootName = rootName;
_topContainerNames = topContainerNames;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using OSPSuite.Assets;
using OSPSuite.BDDHelper;
using OSPSuite.BDDHelper.Extensions;
using OSPSuite.Utility.Container;
Expand All @@ -19,6 +20,7 @@ public abstract class concern_for_ModelConstructor : ContextForIntegration<IMode
protected CreationResult _result;
protected IModel _model;

protected string _modelName = "MyModel";
public override void GlobalContext()
{
base.GlobalContext();
Expand All @@ -28,7 +30,7 @@ public override void GlobalContext()
protected override void Because()
{
sut = IoC.Resolve<IModelConstructor>();
_result = sut.CreateModelFrom(_buildConfiguration, "MyModel");
_result = sut.CreateModelFrom(_buildConfiguration, _modelName);
_model = _result.Model;
}
}
Expand Down Expand Up @@ -484,4 +486,52 @@ public void should_return_a_successful_validation()
_result.ValidationResult.Messages.Count().ShouldBeEqualTo(1, _result.ValidationResult.Messages.Select(x => x.Text).ToString("\n"));
}
}

public class When_naming_the_model_like_a_distributed_parameter_sub_parameter : concern_for_ModelConstructor
{
protected override void Context()
{
base.Context();
_modelName = "Mean";
}

[Observation]
public void should_be_able_to_create_the_model()
{
_result.ValidationResult.ValidationState.ShouldBeEqualTo(ValidationState.Valid);
}
}

public class When_naming_the_model_like_the_neighborhood_container : concern_for_ModelConstructor
{
protected override void Context()
{
base.Context();
_modelName = Constants.NEIGHBORHOODS;
}

[Observation]
public void should_not_be_able_to_create_the_model()
{
_result.ValidationResult.ValidationState.ShouldBeEqualTo(ValidationState.Invalid);
}
}

public class When_naming_the_model_like_the_organism_container : concern_for_ModelConstructor
{
protected override void Context()
{
base.Context();
_modelName = Constants.ORGANISM;
}

[Observation]
public void should_not_be_able_to_create_the_model()
{
_result.ValidationResult.ValidationState.ShouldBeEqualTo(ValidationState.Invalid);
_result.ValidationResult.Messages.Count().ShouldBeEqualTo(1, _result.ValidationResult.Messages.Select(x => x.Text).ToString("\n"));
_result.ValidationResult.Messages.ElementAt(0).Text.ShouldBeEqualTo(Validation.ModelNameCannotBeNamedLikeATopContainer(_model.Root.GetChildren<IContainer>().AllNames()));
}
}

}
22 changes: 10 additions & 12 deletions tests/OSPSuite.Core.Tests/Services/TopContainerPathReplacerSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@

namespace OSPSuite.Core.Services
{
public abstract class concern_for_top_container_path_replacer : ContextSpecification<TopContainerPathReplacer>
public abstract class concern_for_TopContainerPathReplacer : ContextSpecification<TopContainerPathReplacer>
{
protected string _modelName;
protected IObjectPath _objectPath;
private IEnumerable<string> _topContainerNames;
private IReadOnlyList<string> _topContainerNames;

protected override void Context()
{
_modelName = "toto";
_topContainerNames = new List<string> {"root1", "root2"};
sut = new TopContainerPathReplacer(_modelName,_topContainerNames);
sut = new TopContainerPathReplacer(_modelName, _topContainerNames);
}

protected override void Because()
{
sut.ReplaceIn(_objectPath);
}
}


public class When_replacing_a_path_containing_a_key_word : concern_for_top_container_path_replacer
public class When_replacing_a_path_containing_a_key_word : concern_for_TopContainerPathReplacer
{
private string _pathAsString;

protected override void Context()
{
base.Context();
_objectPath = new ObjectPath(new[]{ObjectPathKeywords.MOLECULE, "A"});
_objectPath = new ObjectPath(ObjectPathKeywords.MOLECULE, "A");
_pathAsString = _objectPath.ToString();
}

Expand All @@ -43,15 +43,14 @@ public void should_not_change_the_path()
}
}


public class When_replacing_a_relative_path : concern_for_top_container_path_replacer
public class When_replacing_a_relative_path : concern_for_TopContainerPathReplacer
{
private string _pathAsString;

protected override void Context()
{
base.Context();
_objectPath = new ObjectPath(new[] { ObjectPath.PARENT_CONTAINER, "A" });
_objectPath = new ObjectPath(ObjectPath.PARENT_CONTAINER, "A");
_pathAsString = _objectPath.ToString();
}

Expand All @@ -62,7 +61,7 @@ public void should_not_change_the_path()
}
}

public class When_replacing_a_path_that_does_not_start_with_a_key_word_or_a_relative_path_marker: concern_for_top_container_path_replacer
public class When_replacing_a_path_that_does_not_start_with_a_key_word_or_a_relative_path_marker : concern_for_TopContainerPathReplacer
{
private IObjectPath _pathWithModelNameFirst;

Expand All @@ -78,7 +77,6 @@ protected override void Context()
public void should_have_added_the_model_name_to_the_path()
{
_objectPath.PathAsString.ShouldBeEqualTo(_pathWithModelNameFirst.PathAsString);

}
}
}
}
1 change: 1 addition & 0 deletions tests/OSPSuite.Starter/OSPSuite.Starter.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsPackable>false</IsPackable>
<TargetFramework>net472</TargetFramework>
<OutputType>WinExe</OutputType>
<Version>1.0.0</Version>
Expand Down

0 comments on commit 8005f80

Please sign in to comment.