diff --git a/APSIM.POStats.Shared/Collector.cs b/APSIM.POStats.Shared/Collector.cs index e26712a..122dd47 100644 --- a/APSIM.POStats.Shared/Collector.cs +++ b/APSIM.POStats.Shared/Collector.cs @@ -142,8 +142,25 @@ private static List GetTablesFromFile(string apsimxFileName) Data = new List() }); } - var matchFields = GetMatchColumnNamesForTable(apsimxFileName, tableName) - .Where(f => f != "SimulationName"); + + List matchFields = new List(); + IEnumerable fields = ["field_match_one", "field_match_two", "field_match_three", "field_match_four"]; + using (SqliteCommand cmd = new SqliteCommand($"SELECT * FROM _PredictedObserved WHERE name = '{tableName}'", db)) + { + using (SqliteDataReader reader = cmd.ExecuteReader()) + { + if (reader.HasRows) + { + reader.Read(); + foreach(string field in fields) + { + string match = reader[field].ToString(); + if (!string.IsNullOrEmpty(match) && match != "SimulationName") + matchFields.Add(match); + } + } + } + } string selectSQL = $"SELECT * FROM {tableName}"; using (SqliteCommand cmd = new SqliteCommand(selectSQL, db)) @@ -227,34 +244,6 @@ private static Dictionary GetSimulationNameIdPairs(SqliteConnectio return returnPairs; } - /// - /// Get the column names that were used to match the predicted/observed data in a table. - /// - /// The .apsimx file that contains the match column names. - /// The name of the table. - /// - private static IEnumerable GetMatchColumnNamesForTable(string apsimxFileName, string tableName) - { - var options = new JsonDocumentOptions { AllowTrailingCommas = true }; - using (JsonDocument document = JsonDocument.Parse(File.ReadAllText(apsimxFileName), options)) - { - var rootNode = new JsonProxyForModel(document.RootElement); - var predictedObservedModel = rootNode.ChildrenRecursively - .FirstOrDefault(child => child.Type == "PredictedObserved" && - child.Name == tableName); - if (predictedObservedModel == null) - throw new Exception($"Cannot find predicted observed table {tableName} in file {apsimxFileName}"); - - var matchElements = new List() - { - predictedObservedModel.GetPropertyValue("FieldNameUsedForMatch"), - predictedObservedModel.GetPropertyValue("FieldName2UsedForMatch"), - predictedObservedModel.GetPropertyValue("FieldName3UsedForMatch") - }; - return matchElements.Where(element => element != null); - } - } - /// /// Create a label for the current row of a SqliteDataReader /// diff --git a/APSIM.POStats.Tests/CollectorTests.cs b/APSIM.POStats.Tests/CollectorTests.cs index be9081c..4e3c06b 100644 --- a/APSIM.POStats.Tests/CollectorTests.cs +++ b/APSIM.POStats.Tests/CollectorTests.cs @@ -52,6 +52,10 @@ public void TearDown() [Test] public void TestEnsureNormalCollectorOperationWorks() { + SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_PredictedObserved", + "Name,field_match_one,field_match_two,field_match_three,field_match_four" + Environment.NewLine + + "PO1,Date,,," + Environment.NewLine + )); SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_Simulations", "ID,Name" + Environment.NewLine + " 1,Sim1" + Environment.NewLine @@ -154,6 +158,11 @@ public void EnsureObservedStringColumnsAreIgnored() [Test] public void EnsureObservedStringValuesInRowsAreIgnored() { + SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_PredictedObserved", + "Name,field_match_one,field_match_two,field_match_three,field_match_four" + Environment.NewLine + + "PO1,Date,,," + Environment.NewLine + )); + SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_Simulations", "ID,Name" + Environment.NewLine + " 1,Sim1" + Environment.NewLine @@ -182,6 +191,11 @@ public void EnsureObservedStringValuesInRowsAreIgnored() [Test] public void EnsurePOTableNotUnderDataStoreIsFound() { + SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_PredictedObserved", + "Name,field_match_one,field_match_two,field_match_three,field_match_four" + Environment.NewLine + + "PO1,Date,,," + Environment.NewLine + )); + SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_Simulations", "ID,Name" + Environment.NewLine + " 1,Sim1" + Environment.NewLine @@ -208,6 +222,11 @@ public void EnsurePOTableNotUnderDataStoreIsFound() [Test] public void EnsureCollectorFindsIntegers() { + SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_PredictedObserved", + "Name,field_match_one,field_match_two,field_match_three,field_match_four" + Environment.NewLine + + "PO1,Date,,," + Environment.NewLine + )); + SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_Simulations", "ID,Name" + Environment.NewLine + " 1,Sim1" + Environment.NewLine