/// <summary>Call Loaded event in specified model and all children</summary> /// <param name="model">The model.</param> public static void CallOnLoaded(IModel model) { Events events = new Events(); events.AddModelEvents(model); events.CallEventHandler(model, "Loaded", null); }
/// <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. APSIMFileConverter.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 }; Events events = new Events(); events.AddModelEvents(simulations); events.CallEventHandler(simulations, "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>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 }; Events events = new Events(); events.AddModelEvents(this); events.CallEventHandler(this, "Serialising", args); try { stream.Write(XmlUtilities.Serialise(this, true)); } finally { events.CallEventHandler(this, "Serialised", args); } }
/// <summary> /// Serialize the model to a string and return the string. /// </summary> /// <param name="model">The model to serialize</param> /// <returns>The string version of the model</returns> public static string Serialise(IModel model) { Events events = new Events(null); events.AddModelEvents(model); // Let all models know that we're about to serialise. object[] args = new object[] { true }; events.CallEventHandler(model, "Serialising", args); // Do the serialisation StringWriter writer = new StringWriter(); writer.Write(XmlUtilities.Serialise(model, true)); // Let all models know that we have completed serialisation. events.CallEventHandler(model, "Serialised", args); // Set the clipboard text. return(writer.ToString()); }
/// <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; // Call deserialised Events events = new Events(null); events.AddModelEvents(modelToAdd); object[] args = new object[] { true }; events.CallEventHandler(modelToAdd, "Deserialised", args); // Correctly parent all models. Add(parent, modelToAdd); // Ensure the model name is valid. Apsim.EnsureNameIsUnique(modelToAdd); // Call OnLoaded events.CallEventHandler(modelToAdd, "Loaded", null); Locator(parent).Clear(); return modelToAdd; }
/// <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; // Call deserialised Events events = new Events(null); events.AddModelEvents(modelToAdd); object[] args = new object[] { true }; events.CallEventHandler(modelToAdd, "Deserialised", args); // Correctly parent all models. Add(parent, modelToAdd); // Ensure the model name is valid. Apsim.EnsureNameIsUnique(modelToAdd); // Call OnLoaded events.CallEventHandler(modelToAdd, "Loaded", null); Locator(parent).Clear(); return(modelToAdd); }
/// <summary> /// Serialize the model to a string and return the string. /// </summary> /// <param name="model">The model to serialize</param> /// <returns>The string version of the model</returns> public static string Serialise(IModel model) { Events events = new Events(null); events.AddModelEvents(model); // Let all models know that we're about to serialise. object[] args = new object[] { true }; events.CallEventHandler(model, "Serialising", args); // Do the serialisation StringWriter writer = new StringWriter(); writer.Write(XmlUtilities.Serialise(model, true)); // Let all models know that we have completed serialisation. events.CallEventHandler(model, "Serialised", args); // Set the clipboard text. return writer.ToString(); }
/// <summary>Call Loaded event in specified model and all children</summary> /// <param name="model">The model.</param> public static void CallOnLoaded(IModel model) { Events events = new Events(); events.AddModelEvents(model); events.CallEventHandler(model, "Loaded", null); }
/// <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 }; Events events = new Events(); events.AddModelEvents(this); events.CallEventHandler(this, "Serialising", args); try { stream.Write(XmlUtilities.Serialise(this, true)); } finally { events.CallEventHandler(this, "Serialised", args); } }
/// <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. APSIMFileConverter.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 }; Events events = new Events(); events.AddModelEvents(simulations); events.CallEventHandler(simulations, "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> /// Create a specific simulation. /// </summary> public Simulation CreateSpecificSimulation(string name) { List<List<FactorValue>> allCombinations = AllCombinations(); Simulation baseSimulation = Apsim.Child(this, typeof(Simulation)) as Simulation; Simulations parentSimulations = Apsim.Parent(this, typeof(Simulations)) as Simulations; foreach (List<FactorValue> combination in allCombinations) { string newSimulationName = Name; foreach (FactorValue value in combination) newSimulationName += value.Name; if (newSimulationName == name) { Simulation newSimulation = Apsim.Clone(baseSimulation) as Simulation; newSimulation.Name = newSimulationName; newSimulation.Parent = null; newSimulation.FileName = parentSimulations.FileName; Apsim.ParentAllChildren(newSimulation); // Make substitutions. Simulations.MakeSubstitutions(parentSimulations, new List<Simulation> { newSimulation }); // Connect events and links in our new simulation. Events events = new Events(); events.AddModelEvents(newSimulation); events.CallEventHandler(newSimulation, "Loaded", null); foreach (FactorValue value in combination) value.ApplyToSimulation(newSimulation); PushFactorsToReportModels(newSimulation, combination); return newSimulation; } } return null; }
/// <summary>Called to start the job.</summary> /// <param name="jobManager">The job manager running this job.</param> /// <param name="workerThread">The thread this job is running on.</param> public void Run(JobManager jobManager, BackgroundWorker workerThread) { List<List<FactorValue>> allCombinations = AllCombinations(); Simulation baseSimulation = Apsim.Child(this, typeof(Simulation)) as Simulation; Simulations parentSimulations = Apsim.Parent(this, typeof(Simulations)) as Simulations; Stream serialisedBase = Apsim.SerialiseToStream(baseSimulation) as Stream; List<Simulation> simulations = new List<Simulation>(); foreach (List<FactorValue> combination in allCombinations) { string newSimulationName = Name; foreach (FactorValue value in combination) newSimulationName += value.Name; Simulation newSimulation = Apsim.DeserialiseFromStream(serialisedBase) as Simulation; newSimulation.Name = newSimulationName; newSimulation.Parent = null; newSimulation.FileName = parentSimulations.FileName; Apsim.ParentAllChildren(newSimulation); // Make substitutions. Simulations.MakeSubstitutions(parentSimulations, new List<Simulation> { newSimulation }); // Call OnLoaded in all models. Events events = new Events(); events.AddModelEvents(newSimulation); events.CallEventHandler(newSimulation, "Loaded", null); foreach (FactorValue value in combination) value.ApplyToSimulation(newSimulation); PushFactorsToReportModels(newSimulation, combination); jobManager.AddChildJob(this, newSimulation); } }
/// <summary> /// Replace the object specified by 'path' in 'newSimulation' with the specified 'value' /// </summary> private static void ApplyModelReplacement(Simulation newSimulation, string path, IModel value) { IModel newModel = Apsim.Clone(value); newSimulation.Locater.Clear(); IModel modelToReplace = newSimulation.Get(path) as IModel; if (modelToReplace == null) throw new Exception("Cannot find model to replace. Model path: " + path); int index = modelToReplace.Parent.Children.IndexOf(modelToReplace as Model); if (index == -1) throw new Exception("Cannot find model to replace. Model path: " + path); modelToReplace.Parent.Children.RemoveAt(index); modelToReplace.Parent.Children.Insert(index, newModel as Model); newModel.Name = modelToReplace.Name; newModel.Parent = modelToReplace.Parent; Events events = new Events(); events.AddModelEvents(newModel); events.CallEventHandler(newModel, "Loaded", null); }