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

601 fixing nightly build #606

Merged
merged 17 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from 15 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
4 changes: 2 additions & 2 deletions src/MoBi.Engine/EngineRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public override void RegisterInContainer(IContainer container)
container.AddScanner(x =>
{
x.AssemblyContainingType<EngineRegister>();
x.WithConvention(new OSPSuiteRegistrationConvention(registerConcreteType: true));
x.ExcludeType<UnitDefinitionImporter>();
x.ExcludeType<FunctionDefinitionImporter>();
x.WithConvention(new OSPSuiteRegistrationConvention(registerConcreteType: true));
});

//No idea why this is required explicitly. Maybe because the class only has yield operators?
container.Register<SBMLImporterRepository, SBMLImporterRepository>();
container.Register<ASTHandler, ASTHandler>();
container.Register<IUnitDefinitionImporter, UnitDefinitionImporter>(LifeStyle.Singleton);
container.Register<IFunctionDefinitionImporter, FunctionDefinitionImporter>(LifeStyle.Singleton);
}
Expand Down
7 changes: 7 additions & 0 deletions src/MoBi.Engine/ISBMLInitializableImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MoBi.Engine
{
public interface ISBMLInitializableImporter
{
void Initialize();
}
}
23 changes: 20 additions & 3 deletions src/MoBi.Engine/Sbml/EventImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ protected override void Import(Model model)
{
_astHandler.NeedAbsolutePath = true;
CreateEGBandEGBB();

for (long i = 0; i < model.getNumEvents(); i++)
var numEvents = model.getNumEvents();
if (numEvents == 0)
{
CreateEmptyEvent();
}
else
{
CreateEvent(model.getEvent(i));
for (var i = 0; i < numEvents; i++)
{
CreateEvent(model.getEvent(i));
}
}

_astHandler.NeedAbsolutePath = false;
AddToProject();
}
Expand Down Expand Up @@ -90,6 +98,15 @@ internal void CreateEvent(Event sbmlEvent)
EventGroupBuilder.Add(EventBuilder);
}

private void CreateEmptyEvent()
{
EventGroupBuilder.Add(
new EventBuilder()
.WithId(SBMLConstants.SBML_EVENTBUILDER)
.WithName(SBMLConstants.SBML_DEFAULTEVENTNAME)
);
}

/// <summary>
/// Creates warnings to inform the user, that "Delay" and "Priority" of events are not supported/considered.
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion src/MoBi.Engine/Sbml/FunctionDefinitionImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface IFunctionDefinitionImporter : ISBMLImporter
List<FunctionDefinition> FunctionDefinitions { get; }
}

public class FunctionDefinitionImporter : SBMLImporter, IFunctionDefinitionImporter
public class FunctionDefinitionImporter : SBMLImporter, ISBMLInitializableImporter, IFunctionDefinitionImporter
{
private readonly List<FunctionDefinition> _functionDefinitions;
public List<FunctionDefinition> FunctionDefinitions { get => _functionDefinitions; }
Expand All @@ -32,5 +32,10 @@ protected override void Import(Model model)
public override void AddToProject()
{
}

public void Initialize()
{
_functionDefinitions.Clear();
Copy link
Member

Choose a reason for hiding this comment

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

is this class FunctionDefinitionImporter singleton? Why isn't it done in constructor?
We have a IStartable interface also... maybe that should be enough?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is a singleton yes. Then if you load a file and load a second file, it will remember all the units and functions defined so you need to clearly trigger "this is a new file" and reset to a clean state. This is why for example tests were failing.

}
}
}
1 change: 1 addition & 0 deletions src/MoBi.Engine/Sbml/SBMLConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static class SBMLConstants
public static string FORMULA = "Formula";
public const string SBML_EVENT_ASSIGNMENT = "SBML Event Assignment ";
public const string SBML_EVENTBUILDER = "EVENTBUILDER";
public const string SBML_DEFAULTEVENTNAME = "Default Event";

//Building Block Names
public const string SBML_REACTION_BB = "SBML Reactions";
Expand Down
72 changes: 35 additions & 37 deletions src/MoBi.Engine/Sbml/SBMLImporterRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,47 @@

namespace MoBi.Engine.Sbml
{
public class SBMLImporterRepository
{
private readonly IContainer _container;
public class SBMLImporterRepository
{
private readonly IContainer _container;

public SBMLImporterRepository(IContainer container)
{
_container = container;
}
public SBMLImporterRepository(IContainer container)
{
_container = container;
}

public IEnumerable<ISBMLImporter> All()
{
yield return _container.Resolve<IUnitDefinitionImporter>();
yield return _container.Resolve<CompartmentImporter>();
yield return _container.Resolve<SpeciesImporter>();
yield return _container.Resolve<ParameterImporter>();
yield return _container.Resolve<IFunctionDefinitionImporter>();
yield return _container.Resolve<ReactionImporter>();
yield return _container.Resolve<AssignmentImporter>();
yield return _container.Resolve<EventImporter>();
}

public IEnumerable<ISBMLImporter> AllFor(Model sbmlModel)
{
if (sbmlModel.getNumUnitDefinitions() != 0)
public IEnumerable<ISBMLImporter> All()
{
yield return _container.Resolve<IUnitDefinitionImporter>();
yield return _container.Resolve<CompartmentImporter>();
if (sbmlModel.getNumSpecies() != 0)
yield return _container.Resolve<CompartmentImporter>();
yield return _container.Resolve<SpeciesImporter>();
if (sbmlModel.getNumParameters() != 0)
yield return _container.Resolve<ParameterImporter>();
if (sbmlModel.getNumFunctionDefinitions() != 0)
yield return _container.Resolve<IFunctionDefinitionImporter>();
if (sbmlModel.getNumReactions() != 0)
yield return _container.Resolve<ReactionImporter>();
yield return _container.Resolve<AssignmentImporter>();
if (sbmlModel.getNumEvents() != 0)
yield return _container.Resolve<AssignmentImporter>();
yield return _container.Resolve<EventImporter>();
}

public IEnumerable<SBMLImporter> Assignment()
{
yield return _container.Resolve<RuleImporter>();
yield return _container.Resolve<InitialAssignmentImporter>();
}
}
}
public IEnumerable<ISBMLImporter> AllFor(Model sbmlModel)
{
if (sbmlModel.getNumUnitDefinitions() != 0)
yield return _container.Resolve<IUnitDefinitionImporter>();
yield return _container.Resolve<CompartmentImporter>();
if (sbmlModel.getNumSpecies() != 0)
yield return _container.Resolve<SpeciesImporter>();
if (sbmlModel.getNumParameters() != 0)
yield return _container.Resolve<ParameterImporter>();
if (sbmlModel.getNumFunctionDefinitions() != 0)
yield return _container.Resolve<IFunctionDefinitionImporter>();
if (sbmlModel.getNumReactions() != 0)
yield return _container.Resolve<ReactionImporter>();
yield return _container.Resolve<AssignmentImporter>();
if (sbmlModel.getNumEvents() != 0)
yield return _container.Resolve<EventImporter>();
}
public IEnumerable<SBMLImporter> Assignment()
{
yield return _container.Resolve<RuleImporter>();
yield return _container.Resolve<InitialAssignmentImporter>();
}
}
}
10 changes: 2 additions & 8 deletions src/MoBi.Engine/Sbml/SBMLTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public IMoBiCommand ImportModelFromSbml(string filename, IMoBiProject project)
project.Name = getProjectName(model);

reportConstraints(project, model);
_importerRepository.AllFor(model).OfType<ISBMLInitializableImporter>().Each(impoter => impoter.Initialize());

foreach (var importer in _importerRepository.AllFor(model))
{
Expand Down Expand Up @@ -105,14 +106,7 @@ public Model GetModel(string filename)
{
throw new MoBiException(SBMLConstants.ModelNotRead(sbmlDoc.getErrorLog().ToString()));
}
try
{
convertSBML(sbmlDoc);
}
catch (Exception _)
{
throw new MoBiException(SBMLConstants.NoValidConversionToLevel3Version2);
}
convertSBML(sbmlDoc);
var model = sbmlDoc.getModel();
SaveSBMLInformation(model, sbmlDoc);
return model;
Expand Down
9 changes: 8 additions & 1 deletion src/MoBi.Engine/Sbml/UnitDefinitionImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface IUnitDefinitionImporter : ISBMLImporter
double[] ToMobiBaseUnit(string unit, IEnumerable<double> value);
}

public class UnitDefinitionImporter : SBMLImporter, IUnitDefinitionImporter
public class UnitDefinitionImporter : SBMLImporter, ISBMLInitializableImporter, IUnitDefinitionImporter
{
private readonly IMoBiDimensionFactory _moBiDimensionFactory;
private readonly IDictionary<int, Unit> _baseUnitsDictionary;
Expand Down Expand Up @@ -98,6 +98,7 @@ public IDimension ConvertUnit(UnitDefinition unitDefinition)
if (dimension != Constants.Dimension.NO_DIMENSION)
{
_sbmlInformation.MobiDimension[sbmlUnit] = dimension;
_unitConvertionDictionary.Add(sbmlUnit, new UnitConvertionInfo() { Dimension = dimension, Unit = dimension.DefaultUnit, Rate = 1 });
return dimension;
}

Expand All @@ -114,6 +115,7 @@ public IDimension ConvertUnit(UnitDefinition unitDefinition)
return unitDimension;
}
}
_unitConvertionDictionary.Add(sbmlUnit, new UnitConvertionInfo() { Dimension = dimension, Unit = dimension.DefaultUnit, Rate = 1 });
return dimension;
}

Expand All @@ -136,6 +138,11 @@ public double[] ToMobiBaseUnit(string unit, IEnumerable<double> value)
var convertionData = _unitConvertionDictionary[unit];
return convertionData.Dimension.UnitValuesToBaseUnitValues(convertionData.Unit, value.Select(v => v * convertionData.Rate));
}

public void Initialize()
Copy link
Member

Choose a reason for hiding this comment

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

same question here

Copy link
Member Author

Choose a reason for hiding this comment

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

Didn't know about IStartable... already committed.

{
_unitConvertionDictionary.Clear();
}
}

}
18 changes: 17 additions & 1 deletion tests/MoBi.Tests/Core/SBML/EventImporterSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ protected override void Context()
}
}

public class DefaultEventImporterTests : ConcernForEventImporter
{
protected override void Because()
{
var sbmlModel = new Model(3, 1);

sut.DoImport(sbmlModel, new MoBiProject(), A.Fake<SBMLInformation>(), new MoBiMacroCommand());
}

[Observation]
public void LoadsDefaultIfNoEvent()
{
sut.EventGroupBuilder.Children.AllNames().ShouldNotBeEmpty();
}
}

public class EventImporterTests : ConcernForEventImporter
{
protected override void Because()
Expand Down Expand Up @@ -74,7 +90,7 @@ public void EventCreationTest()
public void TriggerCreationTest()
{
sut.EventGroupBuilder.Children.ExistsByName("e1").ShouldBeTrue();
var event1 = (EventBuilder) sut.EventGroupBuilder.Children.FindByName("e1");
var event1 = (EventBuilder)sut.EventGroupBuilder.Children.FindByName("e1");
event1.Formula.ShouldNotBeNull();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/MoBi.Tests/Core/SBML/Testfiles/UnitTestFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<compartment metaid="default" id="default" size="1" units="volume"/>
</listOfCompartments>
<listOfSpecies>
<species metaid="s1" id="s1" name="s1" compartment="default" initialAmount="5" substanceUnits="mole" hasOnlySubstanceUnits="true" charge="0">
<species metaid="s1" id="s1" name="s1" compartment="default" initialAmount="5" substanceUnits="substance" hasOnlySubstanceUnits="true" charge="0">
<annotation>
<celldesigner:extension>
<celldesigner:positionToCompartment>inside</celldesigner:positionToCompartment>
Expand Down
2 changes: 1 addition & 1 deletion tests/MoBi.Tests/Core/SBML/UnitImporterSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace MoBi.Core.SBML
{
public abstract class UnitImporterSpecs : ContextForSBMLIntegration<UnitDefinitionImporter>
public abstract class UnitImporterSpecs : ContextForSBMLIntegration<IUnitDefinitionImporter>
{
private Model _sbmlModel;

Expand Down
3 changes: 3 additions & 0 deletions tests/MoBi.Tests/MoBi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
<None Update="Core\SBML\Testfiles\Barros2021_RAJI.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Core\SBML\Testfiles\UnitTestFile.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>


Expand Down