Skip to content

Commit

Permalink
Merge pull request #8881 from par456/8838FactorsRepeating
Browse files Browse the repository at this point in the history
8838 _Factors table fixes
  • Loading branch information
hol353 committed May 1, 2024
2 parents c6d2f70 + 3c61e7e commit 6d5d509
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 61 deletions.
51 changes: 51 additions & 0 deletions Models/Core/Simulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Data;

namespace Models.Core
{
Expand Down Expand Up @@ -258,6 +259,8 @@ public void Prepare()
// Resolve all links
links.Resolve(this, true, throwOnFail: true);

StoreFactorsInDataStore();

events.Publish("SubscribeToEvents", new object[] { this, EventArgs.Empty });
}
catch (Exception err)
Expand Down Expand Up @@ -374,5 +377,53 @@ private static void CheckNotMultipleSoilWaterModels(IModel parentZone)
foreach (IModel zone in parentZone.FindAllChildren<Models.Interfaces.IZone>())
CheckNotMultipleSoilWaterModels(zone);
}

/// <summary>Store descriptors in DataStore.</summary>
private void StoreFactorsInDataStore()
{
IEnumerable<IDataStore> ss = Services.OfType<IDataStore>();
IDataStore storage = null;
if (ss != null && ss.Count() > 0)
storage = ss.First();

if (storage != null && Descriptors != null)
{
var table = new DataTable("_Factors");
table.Columns.Add("ExperimentName", typeof(string));
table.Columns.Add("SimulationName", typeof(string));
table.Columns.Add("FolderName", typeof(string));
table.Columns.Add("FactorName", typeof(string));
table.Columns.Add("FactorValue", typeof(string));

var experimentDescriptor = Descriptors.Find(d => d.Name == "Experiment");
var simulationDescriptor = Descriptors.Find(d => d.Name == "SimulationName");
var folderDescriptor = Descriptors.Find(d => d.Name == "FolderName");

foreach (var descriptor in Descriptors)
{
if (descriptor.Name != "Experiment" &&
descriptor.Name != "SimulationName" &&
descriptor.Name != "FolderName" &&
descriptor.Name != "Zone")
{
var row = table.NewRow();
if (experimentDescriptor != null)
row[0] = experimentDescriptor.Value;
if (simulationDescriptor != null)
row[1] = simulationDescriptor.Value;
if (folderDescriptor != null)
row[2] = folderDescriptor.Value;
row[3] = descriptor.Name;
row[4] = descriptor.Value;
table.Rows.Add(row);
}
}

// Report tables are automatically cleaned before the simulation is run,
// as an optimisation specifically designed for this call to WriteTable().
// Therefore, we do not need to delete existing data here.
storage.Writer.WriteTable(table, false);
}
}
}
}
44 changes: 0 additions & 44 deletions Models/Report/Report.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,50 +365,6 @@ private void AddExperimentFactorLevels()
foreach (var descriptor in simulation.Descriptors)
if (descriptor.Name != "Zone" && descriptor.Name != "SimulationName")
this.Columns.Add(new ReportColumnConstantValue(descriptor.Name, descriptor.Value));
StoreFactorsInDataStore();
}
}

/// <summary>Store descriptors in DataStore.</summary>
private void StoreFactorsInDataStore()
{
if (storage != null && simulation != null && simulation.Descriptors != null)
{
var table = new DataTable("_Factors");
table.Columns.Add("ExperimentName", typeof(string));
table.Columns.Add("SimulationName", typeof(string));
table.Columns.Add("FolderName", typeof(string));
table.Columns.Add("FactorName", typeof(string));
table.Columns.Add("FactorValue", typeof(string));

var experimentDescriptor = simulation.Descriptors.Find(d => d.Name == "Experiment");
var simulationDescriptor = simulation.Descriptors.Find(d => d.Name == "SimulationName");
var folderDescriptor = simulation.Descriptors.Find(d => d.Name == "FolderName");

foreach (var descriptor in simulation.Descriptors)
{
if (descriptor.Name != "Experiment" &&
descriptor.Name != "SimulationName" &&
descriptor.Name != "FolderName" &&
descriptor.Name != "Zone")
{
var row = table.NewRow();
if (experimentDescriptor != null)
row[0] = experimentDescriptor.Value;
if (simulationDescriptor != null)
row[1] = simulationDescriptor.Value;
if (folderDescriptor != null)
row[2] = folderDescriptor.Value;
row[3] = descriptor.Name;
row[4] = descriptor.Value;
table.Rows.Add(row);
}
}

// Report tables are automatically cleaned before the simulation is run,
// as an optimisation specifically designed for this call to WriteTable().
// Therefore, we do not need to delete existing data here.
storage.Writer.WriteTable(table, false);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Models/Storage/CleanCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ namespace Models.Storage
{
internal class CleanCommand : IRunnable
{
private static readonly string[] otherTablesToClean = new string[2]
private static readonly string[] otherTablesToClean = new string[3]
{
"_Messages",
"_Factors",
"_InitialConditions"
};

Expand Down
40 changes: 24 additions & 16 deletions Tests/UnitTests/Report/ReportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,22 +392,30 @@ public void FactorsTableIsWritten()
VariableNames = new string[0],
EventNames = new string[0]
};
var storage = new MockStorage();
Utilities.InjectLink(report, "simulation", sim);
Utilities.InjectLink(report, "storage", storage);
Utilities.InjectLink(report, "clock", new MockClock());

var events = new Events(report);
events.Publish("SubscribeToEvents", new object[] { report, new EventArgs() });
Assert.AreEqual(1, storage.tables.Count);
Assert.AreEqual(storage.tables[0].TableName, "_Factors");


Assert.IsTrue(
Utilities.CreateTable(new string[] { "ExperimentName", "SimulationName", "FolderName", "FactorName", "FactorValue" },
new List<object[]> { new object[] { "exp1", "sim1", "F", "Cultivar", "cult1" },
new object[] { "exp1", "sim1", "F", "N", 0 } })
.IsSame(storage.tables[0]));

SQLite database = new SQLite();
database.OpenDatabase(":memory:", readOnly: false);
DataStore storage = new DataStore(database);

Simulations sims = new Simulations();
sims.Children.Add(sim);
sims.Children.Add(new Summary());
sims.Children.Add(storage);
sims.ParentAllDescendants();

sim.Prepare();

storage.Writer.WaitForIdle();
storage.Reader.Refresh();

Assert.IsNotNull(storage.Reader.GetData("_Factors"));

DataTable dtExpected = Utilities.CreateTable(new string[] { "CheckpointName", "CheckpointID", "SimulationName", "SimulationID", "ExperimentName", "FolderName", "FactorName", "FactorValue" },
new List<object[]> { new object[] { "Current", 1, "", 1, "exp1", "F", "Cultivar", "cult1" },
new object[] { "Current", 1, "", 1, "exp1", "F", "N", 0 } });
DataTable dtActual = storage.Reader.GetData("_Factors");

Assert.IsTrue(dtExpected.IsSame(dtActual));
}

/// <summary>
Expand Down

0 comments on commit 6d5d509

Please sign in to comment.