/// <summary>Create a simulations object by reading the specified filename</summary> /// <param name="node">The node.</param> /// <returns></returns> /// <exception cref="System.Exception">Simulations.Read() failed. Invalid simulation file.\n</exception> public static Simulations Read(XmlNode node) { // Run the converter. Converter.ConvertToLatestVersion(node); // Deserialise Simulations simulations = XmlUtilities.Deserialise(node, Assembly.GetExecutingAssembly()) as Simulations; if (simulations != null) { // Set the filename simulations.SetFileNameInAllSimulations(); // Call the OnSerialised method in each model. object[] args = new object[] { true }; foreach (Model model in Apsim.ChildrenRecursively(simulations)) { Apsim.CallEventHandler(model, "Deserialised", args); } // Parent all models. simulations.Parent = null; Apsim.ParentAllChildren(simulations); CallOnLoaded(simulations); } else { throw new Exception("Simulations.Read() failed. Invalid simulation file.\n"); } return(simulations); }
/// <summary>Adds a new model (as specified by the xml node) to the specified parent.</summary> /// <param name="parent">The parent to add the model to</param> /// <param name="node">The XML representing the new model</param> /// <returns>The newly created model.</returns> public static IModel Add(IModel parent, XmlNode node) { IModel modelToAdd = XmlUtilities.Deserialise(node, Assembly.GetExecutingAssembly()) as Model; // Get all child models List <IModel> modelsToNotify = Apsim.ChildrenRecursively(modelToAdd); // Call deserialised in all models. object[] args = new object[] { true }; CallEventHandler(modelToAdd, "Deserialised", args); foreach (IModel modelToNotify in modelsToNotify) { CallEventHandler(modelToNotify, "Deserialised", args); } // Corrently parent all models. modelToAdd.Parent = parent; Apsim.ParentAllChildren(modelToAdd); parent.Children.Add(modelToAdd as Model); // Ensure the model name is valid. Apsim.EnsureNameIsUnique(modelToAdd); // Call OnLoaded Apsim.CallEventHandler(modelToAdd, "Loaded", null); foreach (IModel child in modelsToNotify) { Apsim.CallEventHandler(child, "Loaded", null); } Locator(parent).Clear(); return(modelToAdd); }
/// <summary>Call the all completed event in all models.</summary> public void CallAllCompleted() { object[] args = new object[] { this, new EventArgs() }; foreach (Model model in Apsim.ChildrenRecursively(this)) { Apsim.CallEventHandler(model, "AllCompleted", args); } }
/// <summary>Call Loaded event in specified model and all children</summary> /// <param name="model">The model.</param> private static void CallOnLoaded(IModel model) { // Call OnLoaded in all models. foreach (Model child in Apsim.ChildrenRecursively(model)) { Apsim.CallEventHandler(child, "Loaded", null); } }
/// <summary>Create a simulations object by reading the specified filename</summary> /// <param name="FileName">Name of the file.</param> /// <returns></returns> /// <exception cref="System.Exception">Simulations.Read() failed. Invalid simulation file.\n</exception> public static Simulations Read(string FileName) { // Run the converter. Converter.ConvertToLatestVersion(FileName); // Deserialise Simulations simulations = XmlUtilities.Deserialise(FileName, Assembly.GetExecutingAssembly()) as Simulations; if (simulations != null) { // Set the filename simulations.FileName = FileName; simulations.SetFileNameInAllSimulations(); // Call the OnDeserialised method in each model. object[] args = new object[] { true }; foreach (Model model in Apsim.ChildrenRecursively(simulations)) { Apsim.CallEventHandler(model, "Deserialised", args); } // Parent all models. simulations.Parent = null; Apsim.ParentAllChildren(simulations); // Call OnLoaded in all models. simulations.LoadErrors = new List <Exception>(); foreach (Model child in Apsim.ChildrenRecursively(simulations)) { try { Apsim.CallEventHandler(child, "Loaded", null); } catch (ApsimXException err) { simulations.LoadErrors.Add(err); } catch (Exception err) { err.Source = child.Name; simulations.LoadErrors.Add(err); } } } else { throw new Exception("Simulations.Read() failed. Invalid simulation file.\n"); } return(simulations); }
/// <summary>Write the specified simulation set to the specified 'stream'</summary> /// <param name="stream">The stream.</param> public void Write(TextWriter stream) { object[] args = new object[] { true }; foreach (Model model in Apsim.ChildrenRecursively(this)) { Apsim.CallEventHandler(model, "Serialising", args); } try { stream.Write(XmlUtilities.Serialise(this, true)); } finally { foreach (Model model in Apsim.ChildrenRecursively(this)) { Apsim.CallEventHandler(model, "Serialised", args); } } }
/// <summary>Run the jobs.</summary> /// <param name="sender"></param> /// <param name="e"></param> public void Run(object sender, System.ComponentModel.DoWorkEventArgs e) { JobManager jobManager = e.Argument as JobManager; List <JobManager.IRunnable> jobs = new List <JobManager.IRunnable>(); DataStore store = Apsim.Child(simulations, typeof(DataStore)) as DataStore; Simulation[] simulationsToRun; if (model is Simulations) { // As we are going to run all simulations, we can delete all tables in the DataStore. This // will clean up order of columns in the tables and removed unused ones. store.DeleteAllTables(); simulationsToRun = Simulations.FindAllSimulationsToRun(simulations); } else { store.RemoveUnwantedSimulations(simulations); if (model is Simulation) { if (model.Parent == null) { // model is already a cloned simulation, probably from user running a single // simulation from an experiment. simulationsToRun = new Simulation[1] { model as Simulation }; } else { simulationsToRun = new Simulation[1] { Apsim.Clone(model as Simulation) as Simulation }; Simulations.CallOnLoaded(simulationsToRun[0]); } } else { simulationsToRun = Simulations.FindAllSimulationsToRun(model); } } store.Disconnect(); simulations.MakeSubstitutions(simulationsToRun); foreach (Simulation simulation in simulationsToRun) { jobs.Add(simulation); jobManager.AddJob(simulation); } // Wait until all our jobs are all finished. while (AreSomeJobsRunning(jobs)) { Thread.Sleep(200); } // Collect all error messages. foreach (Simulation job in simulationsToRun) { if (job.ErrorMessage != null) { ErrorMessage += job.ErrorMessage + Environment.NewLine; } } // <summary>Call the all completed event in all models.</summary> object[] args = new object[] { this, new EventArgs() }; foreach (Model childModel in Apsim.ChildrenRecursively(simulations)) { try { Apsim.CallEventHandler(childModel, "AllCompleted", args); } catch (Exception err) { ErrorMessage += "Error in file: " + simulations.FileName + Environment.NewLine; ErrorMessage += err.ToString() + Environment.NewLine + Environment.NewLine; } } if (ErrorMessage != null) { throw new Exception(ErrorMessage); } }