Ejemplo n.º 1
0
        /// <summary>Documents the specified model.</summary>
        /// <param name="modelNameToDocument">The model name to document.</param>
        /// <param name="tags">The auto doc tags.</param>
        /// <param name="headingLevel">The starting heading level.</param>
        public void DocumentModel(string modelNameToDocument, List <AutoDocumentation.ITag> tags, int headingLevel)
        {
            Simulation simulation = Apsim.Find(this, typeof(Simulation)) as Simulation;

            if (simulation != null)
            {
                // Find the model of the right name.
                IModel modelToDocument = Apsim.Find(simulation, modelNameToDocument);

                // If not found then find a model of the specified type.
                if (modelToDocument == null)
                {
                    modelToDocument = Apsim.Get(simulation, "[" + modelNameToDocument + "]") as IModel;
                }

                // If the simulation has the same name as the model we want to document, dig a bit deeper
                if (modelToDocument == simulation)
                {
                    modelToDocument = Apsim.ChildrenRecursivelyVisible(simulation).FirstOrDefault(m => m.Name.Equals(modelNameToDocument, StringComparison.OrdinalIgnoreCase));
                }

                // If still not found throw an error.
                if (modelToDocument == null)
                {
                    throw new ApsimXException(this, "Could not find a model of the name " + modelNameToDocument + ". Simulation file name must match the name of the node to document.");
                }

                // Get the path of the model (relative to parentSimulation) to document so that
                // when replacements happen below we will point to the replacement model not the
                // one passed into this method.
                string pathOfSimulation      = Apsim.FullPath(simulation) + ".";
                string pathOfModelToDocument = Apsim.FullPath(modelToDocument).Replace(pathOfSimulation, "");

                // Clone the simulation
                Simulation clonedSimulation = Apsim.Clone(simulation) as Simulation;

                // Make any substitutions.
                Simulations.MakeSubstitutions(this, new List <Simulation> {
                    clonedSimulation
                });

                // Now use the path to get the model we want to document.
                modelToDocument = Apsim.Get(clonedSimulation, pathOfModelToDocument) as IModel;

                if (modelToDocument == null)
                {
                    throw new Exception("Cannot find model to document: " + modelNameToDocument);
                }

                // resolve all links in cloned simulation.
                Links links = new Core.Links();
                links.Resolve(clonedSimulation);

                // Document the model.
                modelToDocument.Document(tags, headingLevel, 0);

                // Unresolve links.
                links.Unresolve(clonedSimulation);
            }
        }
Ejemplo n.º 2
0
        /// <summary>Called to start the job.</summary>
        /// <param name="cancelToken">The token to check if job has been cancelled</param>
        public void Run(CancellationTokenSource cancelToken)
        {
            if (simulationEngine != null)
            {
                fileName = simulationEngine.FileName;
                Console.Write("File: " + Path.GetFileNameWithoutExtension(fileName) + ", ");
            }
            Console.WriteLine("Simulation " + simulationToRun.Name + " has commenced.");

            // Start timer to record how long it takes to run
            timer = new Stopwatch();
            timer.Start();

            Events events = null;
            Links  links  = null;

            try
            {
                // Clone simulation
                if (cloneSimulationBeforeRun)
                {
                    simulationToRun = Apsim.Clone(simulationToRun) as Simulation;
                    simulationEngine.MakeSubsAndLoad(simulationToRun);
                }
                else
                {
                    events = new Events(simulationToRun);
                }

                // Remove disabled models from simulation
                foreach (IModel model in Apsim.ChildrenRecursively(simulationToRun))
                {
                    if (!model.Enabled)
                    {
                        model.Parent.Children.Remove(model as Model);
                    }
                }

                // Get an event and links service
                if (simulationEngine != null)
                {
                    links = simulationEngine.Links;
                }
                else
                {
                    links = new Core.Links(Services);
                }

                // Resolve links and events.
                links.Resolve(simulationToRun);
                events.ConnectEvents();

                simulationToRun.ClearCaches();

                // Send a commence event so the simulation runs
                object[] args         = new object[] { null, new EventArgs() };
                object[] commenceArgs = new object[] { null, new CommenceArgs()
                                                       {
                                                           CancelToken = cancelToken
                                                       } };
                events.Publish("Commencing", args);
                events.Publish("DoCommence", commenceArgs);
            }
            catch (Exception err)
            {
                string errorMessage = "ERROR in file: " + fileName + "\r\n" +
                                      "Simulation name: " + simulationToRun.Name + "\r\n";
                if (err.InnerException == null)
                {
                    errorMessage += err.Message;
                }
                else
                {
                    errorMessage += err.InnerException.Message;
                }

                ISummary summary = Apsim.Find(simulationToRun, typeof(Summary)) as ISummary;
                if (summary != null)
                {
                    summary.WriteMessage(simulationToRun, errorMessage);
                }

                throw new Exception(errorMessage, err);
            }
            finally
            {
                events.Publish("Completed", new object[] { null, new EventArgs() });

                // Cleanup the simulation
                if (events != null)
                {
                    events.DisconnectEvents();
                }
                links.Unresolve(simulationToRun);

                timer.Stop();
                Console.WriteLine("File: " + Path.GetFileNameWithoutExtension(fileName) +
                                  ", Simulation " + simulationToRun.Name + " complete. Time: " + timer.Elapsed.TotalSeconds.ToString("0.00 sec"));
                simulationEngine = null;
                simulationToRun  = null;
            }
        }
Ejemplo n.º 3
0
        /// <summary>Documents the specified model.</summary>
        /// <param name="modelNameToDocument">The model name to document.</param>
        /// <param name="tags">The auto doc tags.</param>
        /// <param name="headingLevel">The starting heading level.</param>
        public void DocumentModel(string modelNameToDocument, List<AutoDocumentation.ITag> tags, int headingLevel)
        {
            Simulation simulation = Apsim.Find(this, typeof(Simulation)) as Simulation;
            if (simulation != null)
            {
                // Find the model of the right name.
                IModel modelToDocument = Apsim.Find(simulation, modelNameToDocument);

                // If not found then find a model of the specified type.
                if (modelToDocument == null)
                    modelToDocument = Apsim.Get(simulation, "[" + modelNameToDocument + "]") as IModel;

                // If still not found throw an error.
                if (modelToDocument == null)
                    throw new ApsimXException(this, "Could not find a model of the name " + modelNameToDocument + ". Simulation file name must match the name of the node to document.");

                // Get the path of the model (relative to parentSimulation) to document so that
                // when replacements happen below we will point to the replacement model not the
                // one passed into this method.
                string pathOfSimulation = Apsim.FullPath(simulation) + ".";
                string pathOfModelToDocument = Apsim.FullPath(modelToDocument).Replace(pathOfSimulation, "");

                // Clone the simulation
                Simulation clonedSimulation = Apsim.Clone(simulation) as Simulation;

                // Make any substitutions.
                Simulations.MakeSubstitutions(this, new List<Simulation> { clonedSimulation });

                // Now use the path to get the model we want to document.
                modelToDocument = Apsim.Get(clonedSimulation, pathOfModelToDocument) as IModel;

                // resolve all links in cloned simulation.
                Links links = new Core.Links();
                links.Resolve(clonedSimulation);

                // Document the model.
                modelToDocument.Document(tags, headingLevel, 0);

                // Unresolve links.
                links.Unresolve(clonedSimulation);
            }
        }