/// <summary> /// Generates an .apsimx file containing replacements model (if it /// exists), a datastore, and all children of this model. Saves the /// file to disk and returns the absolute path to the file. /// </summary> private string GenerateApsimXFile() { Simulations rootNode = FindAncestor <Simulations>(); string apsimxFileName = GetTempFileName($"apsimx_file_{id}", ".apsimx"); Simulations sims = new Simulations(); sims.Children.AddRange(Children.Select(c => Apsim.Clone(c))); sims.Children.RemoveAll(c => c is IDataStore); IModel replacements = this.FindInScope <Replacements>(); if (replacements != null && !sims.Children.Any(c => c is Replacements)) { sims.Children.Add(Apsim.Clone(replacements)); } // Search for IDataStore, not DataStore - to allow for StorageViaSockets. IDataStore storage = this.FindInScope <IDataStore>(); IModel newDataStore = new DataStore(); if (storage != null && storage is IModel m) { newDataStore.Children.AddRange(m.Children.Select(c => Apsim.Clone(c))); } sims.Children.Add(newDataStore); sims.ParentAllDescendants(); sims.Write(apsimxFileName); string originalFile = rootNode?.FileName; if (string.IsNullOrEmpty(originalFile)) { originalFile = storage?.FileName; } // Copy files across. foreach (IReferenceExternalFiles fileReference in sims.FindAllDescendants <IReferenceExternalFiles>().Cast <IReferenceExternalFiles>()) { foreach (string file in fileReference.GetReferencedFileNames()) { string absoluteFileName = PathUtilities.GetAbsolutePath(file, originalFile); string fileName = Path.GetFileName(absoluteFileName); string newPath = Path.GetDirectoryName(sims.FileName); File.Copy(absoluteFileName, Path.Combine(newPath, fileName), true); } } return(apsimxFileName); }
/// <summary> /// Searches the specified file and returns all instances of PredictedObserved data. /// </summary> /// <param name="fileName">Path to the .apsimx file to be searched.</param> private static List <PredictedObservedDetails> GetPredictedObservedDetails(string fullFileName) { List <Exception> errors; // note - if we get a badformat exception thrown here, it's because .net is trying to // load a 64-bit version of sqlite3.dll for some reason. To fix this, we need to // copy the 32-bit version from ApsimX/DeploymentSupport/Windows/Bin/sqlite3.dll to // APSIM.PerformanceTests.Collector/Bin/Debug/ (or release if building in release mode). Simulations sims = FileFormat.ReadFromFile <Simulations>(fullFileName, out errors); if (errors != null && errors.Count > 0) { // Write all errors except for the last to a log file, and throw the last error // to ensure that we don't proceed further. for (int i = 0; i < errors.Count; i++) { if (i == errors.Count - 1) { throw errors[i]; } WriteToLogFile(string.Format(" ERROR opening file {0}: {1}", fullFileName, errors[i].ToString())); } } List <PredictedObservedDetails> predictedObservedDetailList = new List <PredictedObservedDetails>(); foreach (PredictedObserved poModel in sims.FindAllDescendants <PredictedObserved>()) { PredictedObservedDetails instance = new PredictedObservedDetails() { DatabaseTableName = poModel.Name, PredictedTableName = poModel.PredictedTableName, ObservedTableName = poModel.ObservedTableName, FieldNameUsedForMatch = poModel.FieldNameUsedForMatch, FieldName2UsedForMatch = poModel.FieldName2UsedForMatch ?? string.Empty, FieldName3UsedForMatch = poModel.FieldName3UsedForMatch ?? string.Empty, }; instance.Data = GetPredictedObservedDataTable(poModel.Name, Path.ChangeExtension(fullFileName, ".db")); // Only add this instance if there is data. if ((instance.Data != null) && (instance.Data.Rows.Count > 0)) { predictedObservedDetailList.Add(instance); } else { WriteToLogFile(string.Format(" No PredictedObserved data was found for table {0} of file {1}", poModel.Name, fullFileName)); } } return(predictedObservedDetailList); }
private static Simulation CreateSimulation(string path) { path = PathUtilities.GetAbsolutePath(path, null); Simulations sims = FileFormat.ReadFromFile <Simulations>(path, e => throw e, false); foreach (Soil soil in sims.FindAllDescendants <Soil>()) { SoilStandardiser.Standardise(soil); } DataStore storage = sims.FindDescendant <DataStore>(); storage.UseInMemoryDB = true; Clock clock = sims.FindDescendant <Clock>(); clock.EndDate = clock.StartDate.AddYears(1); return(sims.FindDescendant <Simulation>()); }