/// <summary>Connect all links and events in simulation</summary> public void ConnectLinksAndEvents() { scope = new ScopingRules(); events = new Events(this); events.ConnectEvents(); links = new Core.Links(); links.Resolve(this); }
/// <summary> /// Runs the simulation on the current thread and waits for the simulation /// to complete before returning to caller. Simulation is NOT cloned before /// running. Use instance of Runner to get more options for running a /// simulation or groups of simulations. /// </summary> /// <param name="cancelToken">Is cancellation pending?</param> public void Run(CancellationTokenSource cancelToken = null) { // If the cancelToken is null then give it a default one. This can happen // when called from the unit tests. if (cancelToken == null) { cancelToken = new CancellationTokenSource(); } // Remove disabled models. RemoveDisabledModels(this); // If this simulation was not created from deserialisation then we need // to parent all child models correctly and call OnCreated for each model. bool hasBeenDeserialised = Children.Count > 0 && Children[0].Parent == this; if (!hasBeenDeserialised) { // Parent all models. Apsim.ParentAllChildren(this); // Call OnCreated in all models. Apsim.ChildrenRecursively(this).ForEach(m => m.OnCreated()); } if (Services == null || Services.Count < 1) { Services = new List <object>(); IDataStore storage = Apsim.Find(this, typeof(IDataStore)) as IDataStore; if (storage != null) { Services.Add(Apsim.Find(this, typeof(IDataStore))); } } var links = new Links(Services); var events = new Events(this); try { // Connect all events. events.ConnectEvents(); // Resolve all links links.Resolve(this, true); // Invoke our commencing event to let all models know we're about to start. Commencing?.Invoke(this, new EventArgs()); // Begin running the simulation. DoCommence?.Invoke(this, new CommenceArgs() { CancelToken = cancelToken }); } catch (Exception err) { // Exception occurred. Write error to summary. string errorMessage = "ERROR in file: " + FileName + Environment.NewLine + "Simulation name: " + Name + Environment.NewLine; errorMessage += err.ToString(); summary?.WriteError(this, errorMessage); // Rethrow exception throw new Exception(errorMessage, err); } finally { // Signal that the simulation is complete. Completed?.Invoke(this, new EventArgs()); // Disconnect our events. events.DisconnectEvents(); // Unresolve all links. links.Unresolve(this, true); } }
/// <summary>Connect all links and events in simulation</summary> public void ConnectLinksAndEvents() { events = new Events(); events.ConnectEvents(this); Apsim.ResolveLinks(this); foreach (Model child in Apsim.ChildrenRecursively(this)) Apsim.ResolveLinks(child); }
/// <summary> /// Prepare the simulation for running. /// </summary> public void Prepare() { // Remove disabled models. RemoveDisabledModels(this); // Standardise the soil. var soils = FindAllDescendants <Soils.Soil>(); foreach (Soils.Soil soil in soils) { SoilStandardiser.Standardise(soil); } // If this simulation was not created from deserialisation then we need // to parent all child models correctly and call OnCreated for each model. bool hasBeenDeserialised = Children.Count > 0 && Children[0].Parent == this; if (!hasBeenDeserialised) { // Parent all models. this.ParentAllDescendants(); // Call OnCreated in all models. foreach (IModel model in FindAllDescendants().ToList()) { model.OnCreated(); } } // Call OnPreLink in all models. // Note the ToList(). This is important because some models can // add/remove models from the simulations tree in their OnPreLink() // method, and FindAllDescendants() is lazy. FindAllDescendants().ToList().ForEach(model => model.OnPreLink()); if (Services == null || Services.Count < 1) { var simulations = FindAncestor <Simulations>(); if (simulations != null) { Services = simulations.GetServices(); } else { Services = new List <object>(); IDataStore storage = this.FindInScope <IDataStore>(); if (storage != null) { Services.Add(this.FindInScope <IDataStore>()); } Services.Add(new ScriptCompiler()); } } var links = new Links(Services); var events = new Events(this); try { // Connect all events. events.ConnectEvents(); // Resolve all links links.Resolve(this, true); events.Publish("SubscribeToEvents", new object[] { this, EventArgs.Empty }); } catch (Exception err) { throw new SimulationException("", err, Name, FileName); } }
/// <summary> /// Runs the simulation on the current thread and waits for the simulation /// to complete before returning to caller. Simulation is NOT cloned before /// running. Use instance of Runner to get more options for running a /// simulation or groups of simulations. /// </summary> /// <param name="cancelToken">Is cancellation pending?</param> public void Run(CancellationTokenSource cancelToken = null) { // If the cancelToken is null then give it a default one. This can happen // when called from the unit tests. if (cancelToken == null) { cancelToken = new CancellationTokenSource(); } // Remove disabled models. RemoveDisabledModels(this); // If this simulation was not created from deserialisation then we need // to parent all child models correctly and call OnCreated for each model. bool hasBeenDeserialised = Children.Count > 0 && Children[0].Parent == this; if (!hasBeenDeserialised) { // Parent all models. this.ParentAllDescendants(); // Call OnCreated in all models. foreach (IModel model in FindAllDescendants().ToList()) { model.OnCreated(); } } // Call OnPreLink in all models. // Note the ToList(). This is important because some models can // add/remove models from the simulations tree in their OnPreLink() // method, and FindAllDescendants() is lazy. FindAllDescendants().ToList().ForEach(model => model.OnPreLink()); if (Services == null || Services.Count < 1) { var simulations = FindAncestor <Simulations>(); if (simulations != null) { Services = simulations.GetServices(); } else { Services = new List <object>(); IDataStore storage = this.FindInScope <IDataStore>(); if (storage != null) { Services.Add(this.FindInScope <IDataStore>()); } Services.Add(new ScriptCompiler()); } } var links = new Links(Services); var events = new Events(this); try { // Connect all events. events.ConnectEvents(); // Resolve all links links.Resolve(this, true); IsRunning = true; // Invoke our commencing event to let all models know we're about to start. Commencing?.Invoke(this, new EventArgs()); // Begin running the simulation. DoCommence?.Invoke(this, new CommenceArgs() { CancelToken = cancelToken }); } catch (Exception err) { // Exception occurred. Write error to summary. string errorMessage = "ERROR in file: " + FileName + Environment.NewLine + "Simulation name: " + Name + Environment.NewLine; errorMessage += err.ToString(); summary?.WriteError(this, errorMessage); // Rethrow exception throw new Exception(errorMessage, err); } finally { // Signal that the simulation is complete. Completed?.Invoke(this, new EventArgs()); // Disconnect our events. events.DisconnectEvents(); // Unresolve all links. links.Unresolve(this, true); IsRunning = false; } }