Пример #1
0
        public void TestManagerWithError()
        {
            List <Exception> errors  = null;
            Simulations      sims    = sims = FileFormat.ReadFromString <Simulations>(ReflectionUtilities.GetResourceAsString("UnitTests.ManagerTestsFaultyManager.apsimx"), out errors);
            DataStore        storage = Apsim.Find(sims, typeof(DataStore)) as DataStore;

            sims.FileName = Path.ChangeExtension(Path.GetTempFileName(), ".apsimx");

            IJobManager jobManager = Runner.ForSimulations(sims, sims, false);
            IJobRunner  jobRunner  = new JobRunnerSync();

            jobRunner.JobCompleted += EnsureJobRanRed;
            jobRunner.Run(jobManager, true);
        }
Пример #2
0
        public void ExcelWeatherFileTest()
        {
            Simulation baseSim = new Simulation();

            baseSim.Name = "Base";

            string weatherFilePath = Path.ChangeExtension(Path.GetTempFileName(), ".xlsx");

            using (FileStream file = new FileStream(weatherFilePath, FileMode.Create, FileAccess.Write))
            {
                Assembly.GetExecutingAssembly().GetManifestResourceStream("UnitTests.Weather.WeatherTestsExcelFile.xlsx").CopyTo(file);
            }

            var excelWeather = new Models.Weather()
            {
                Name               = "Weather",
                Parent             = baseSim,
                FullFileName       = weatherFilePath,
                ExcelWorkSheetName = "Sheet1"
            };

            Clock clock = new Clock()
            {
                Name      = "Clock",
                Parent    = baseSim,
                StartDate = new DateTime(1998, 11, 9),
                EndDate   = new DateTime(1998, 11, 12)
            };

            MockSummary summary = new MockSummary()
            {
                Name   = "Summary",
                Parent = baseSim
            };

            baseSim.Children = new List <Model>()
            {
                excelWeather, clock, summary
            };
            MockStorage storage   = new MockStorage();
            Simulations simsToRun = Simulations.Create(new List <IModel> {
                baseSim, storage
            });

            IJobManager jobManager = Runner.ForSimulations(simsToRun, simsToRun, false);
            IJobRunner  jobRunner  = new JobRunnerSync();

            jobRunner.JobCompleted += Utilities.EnsureJobRanGreen;
            jobRunner.Run(jobManager, true);
        }
Пример #3
0
        /// <summary>
        /// Run all simulations.
        /// </summary>
        /// <param name="runType">How should the simulations be run?</param>
        /// <param name="wait">Wait until all simulations are complete?</param>
        /// <param name="verbose">Produce verbose output?</param>
        /// <param name="numberOfProcessors">Number of CPU processes to use. -1 indicates all processes.</param>
        /// <param name="runTests">Run all test models?</param>
        /// <returns>A list of exception or null if no exceptions thrown.</returns>
        public List <Exception> Run(RunTypeEnum runType, bool wait = true, bool verbose = false, int numberOfProcessors = -1, bool runTests = false)
        {
            errors.Clear();

            Apsim.ParentAllChildren(rootModel);
            Apsim.ChildrenRecursively(rootModel).ForEach(m => m.OnCreated());
            IJobManager jobManager = Models.Core.Runners.Runner.ForSimulations(rootModel, relativeTo, false);
            IJobRunner  jobRunner  = new JobRunnerSync();

            jobRunner.JobCompleted += OnJobCompleded;
            jobRunner.Run(jobManager, wait: true);
            jobRunner.JobCompleted -= OnJobCompleded;

            var storage = Apsim.Find(rootModel, typeof(IDataStore)) as IDataStore;

            storage.Writer.Stop();

            return(errors);
        }
Пример #4
0
        /// <summary>
        /// Runs an <see cref="IJobManager"/> with all implementations of <see cref="IJobRunner"/>.
        /// </summary>
        /// <param name="jobManager">The job manager to run.</param>
        /// <param name="onSimulationCompleted">Event handler which will be invoked by each job runner after it finishes running.</param>
        private void TestWithAllJobRunners(IJobManager jobManager, EventHandler <AllCompletedArgs> onSimulationCompleted)
        {
            IJobRunner jobRunner = new JobRunnerSync();

            jobRunner.AllJobsCompleted += onSimulationCompleted;
            Assert.DoesNotThrow(() => jobRunner.Run(jobManager, true));

            jobRunner.AllJobsCompleted -= onSimulationCompleted;

            jobRunner = new JobRunnerAsync();
            jobRunner.AllJobsCompleted += onSimulationCompleted;
            Assert.DoesNotThrow(() => jobRunner.Run(jobManager, true));

            jobRunner.AllJobsCompleted -= onSimulationCompleted;

            jobRunner = new JobRunnerMultiProcess(false);
            jobRunner.AllJobsCompleted += onSimulationCompleted;
            Assert.DoesNotThrow(() => jobRunner.Run(jobManager, true));
            jobRunner.AllJobsCompleted -= onSimulationCompleted;
        }
Пример #5
0
        /// <summary>
        /// Main program entry point.
        /// </summary>
        /// <param name="args"> Command line arguments</param>
        /// <returns> Program exit code (0 for success)</returns>
        public static int Main(string[] args)
        {
            string tempFolder = Path.Combine(Path.GetTempPath(), "ApsimX");

            Directory.CreateDirectory(tempFolder);
            Environment.SetEnvironmentVariable("TMP", tempFolder, EnvironmentVariableTarget.Process);
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(Manager.ResolveManagerAssembliesEventHandler);

            int exitCode = 0;

            try
            {
                string fileName = null;

                // Extract file name from command line arguments.
                if (args.Length >= 1)
                {
                    fileName = args[0];
                }

                if (args.Length < 1 || args.Length > 4)
                {
                    throw new Exception("Usage: ApsimX ApsimXFileSpec [/Recurse] [/SingleThreaded] [/RunTests]");
                }

                Stopwatch timer = new Stopwatch();
                timer.Start();

                // If the filename argument has a wildcard then create a IJobManager to go look for matching files to run.
                // Otherwise, create a JobManager to open the filename and run it in a separate, external process
                IJobManager job;
                if (fileName.Contains('*') || fileName.Contains('?'))
                {
                    job = Runner.ForFolder(fileName, args.Contains("/Recurse"), args.Contains("/RunTests"));
                }
                else
                {
                    job = Runner.ForFile(fileName, args.Contains("/RunTests"));
                }

                // Run the job created above using either a single thread or multi threaded (default)
                IJobRunner jobRunner;
                if (args.Contains("/SingleThreaded"))
                {
                    jobRunner = new JobRunnerSync();
                }
                else
                {
                    jobRunner = new JobRunnerAsync();
                }
                jobRunner.JobCompleted += OnJobCompleted;
                jobRunner.Run(job, wait: true);

                // If errors occurred, write them to the console.
                if (errors != null)
                {
                    Console.WriteLine("ERRORS FOUND!!");
                    Console.WriteLine(errors);
                    exitCode = 1;
                }
                else
                {
                    exitCode = 0;
                }

                timer.Stop();
                Console.WriteLine("Finished running simulations. Duration " + timer.Elapsed.TotalSeconds.ToString("#.00") + " sec.");
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
                exitCode = 1;
            }

            return(exitCode);
        }
Пример #6
0
        /// <summary>
        /// Main program entry point.
        /// </summary>
        /// <param name="args"> Command line arguments</param>
        /// <returns> Program exit code (0 for success)</returns>
        public static int Main(string[] args)
        {
            string tempFolder = Path.Combine(Path.GetTempPath(), "ApsimX");

            Directory.CreateDirectory(tempFolder);
            Environment.SetEnvironmentVariable("TMP", tempFolder, EnvironmentVariableTarget.Process);
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(Manager.ResolveManagerAssembliesEventHandler);

            int exitCode = 0;

            try
            {
                string fileName = null;

                // Extract file name from command line arguments.
                if (args.Length >= 1)
                {
                    fileName = args[0];
                }

                if (args.Contains("/?"))
                {
                    string detailedHelpInfo = "Usage: Models ApsimXFileSpec [/Recurse] [/SingleThreaded] [/RunTests] [/Csv] [/?]";
                    detailedHelpInfo += Environment.NewLine + Environment.NewLine;
                    detailedHelpInfo += "ApsimXFileSpec:          The path to an .apsimx file. May include wildcard.";
                    detailedHelpInfo += Environment.NewLine + Environment.NewLine + "Options:" + Environment.NewLine;
                    detailedHelpInfo += "    /Recurse             Recursively search subdirectories for files matching ApsimXFileSpec" + Environment.NewLine;
                    detailedHelpInfo += "    /SingleThreaded      Run all simulations in a single thread." + Environment.NewLine;
                    detailedHelpInfo += "    /RunTests            Run all tests." + Environment.NewLine;
                    detailedHelpInfo += "    /Csv                 Export all reports to .csv files." + Environment.NewLine;
                    detailedHelpInfo += "    /?                   Show detailed help information.";
                    Console.WriteLine(detailedHelpInfo);
                    return(1);
                }

                if (args.Length < 1 || args.Length > 6)
                {
                    Console.WriteLine("Usage: Models ApsimXFileSpec [/Recurse] [/SingleThreaded] [/RunTests] [/Csv] [/?]");
                    return(1);
                }

                Stopwatch timer = new Stopwatch();
                timer.Start();

                // If the filename argument has a wildcard then create a IJobManager to go look for matching files to run.
                // Otherwise, create a JobManager to open the filename and run it in a separate, external process
                IJobManager job;
                if (fileName.Contains('*') || fileName.Contains('?'))
                {
                    job = Runner.ForFolder(fileName, args.Contains("/Recurse"), args.Contains("/RunTests"));
                }
                else
                {
                    job = Runner.ForFile(fileName, args.Contains("/RunTests"));
                }

                // Run the job created above using either a single thread or multi threaded (default)
                IJobRunner jobRunner;
                if (args.Contains("/SingleThreaded"))
                {
                    jobRunner = new JobRunnerSync();
                }
                else
                {
                    jobRunner = new JobRunnerAsync();
                }
                if (args.Select(arg => arg.ToLower()).Contains("/csv"))
                {
                    string dir = Path.GetDirectoryName(fileName);
                    if (dir == "")
                    {
                        dir = Directory.GetCurrentDirectory();
                    }
                    files = Directory.GetFiles(
                        dir,
                        Path.GetFileName(fileName),
                        args.Contains("/Recurse") ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).ToList();
                    jobRunner.AllJobsCompleted += GenerateCsvFiles;
                }
                jobRunner.JobCompleted     += OnJobCompleted;
                jobRunner.AllJobsCompleted += OnAllJobsCompleted;
                jobRunner.Run(job, wait: true);

                // If errors occurred, write them to the console.
                if (errors != string.Empty)
                {
                    Console.WriteLine("ERRORS FOUND!!");
                    Console.WriteLine(errors);
                    exitCode = 1;
                }
                else
                {
                    exitCode = 0;
                }

                timer.Stop();
                Console.WriteLine("Finished running simulations. Duration " + timer.Elapsed.TotalSeconds.ToString("#.00") + " sec.");
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
                exitCode = 1;
            }

            return(exitCode);
        }
Пример #7
0
        /// <summary>
        /// Main program entry point.
        /// </summary>
        /// <param name="args"> Command line arguments</param>
        /// <returns> Program exit code (0 for success)</returns>
        public static int Main(string[] args)
        {
            int exitCode = 0;

            try
            {
                string fileName = null;

                // Extract file name from command line arguments.
                if (args.Length >= 1)
                {
                    fileName = args[0];
                }

                string usageMessage = "Usage: Models ApsimXFileSpec [/Recurse] [/SingleThreaded] [/RunTests] [/Csv] [/Version] [/Verbose] [/Upgrade] [/m] [/?]";
                if (args.Contains("/?"))
                {
                    string detailedHelpInfo = usageMessage;
                    detailedHelpInfo += Environment.NewLine + Environment.NewLine;
                    detailedHelpInfo += "ApsimXFileSpec:          The path to an .apsimx file. May include wildcard.";
                    detailedHelpInfo += Environment.NewLine + Environment.NewLine + "Options:" + Environment.NewLine;
                    detailedHelpInfo += "    /Recurse             Recursively search subdirectories for files matching ApsimXFileSpec" + Environment.NewLine;
                    detailedHelpInfo += "    /SingleThreaded      Run all simulations in a single thread." + Environment.NewLine;
                    detailedHelpInfo += "    /RunTests            Run all tests." + Environment.NewLine;
                    detailedHelpInfo += "    /Csv                 Export all reports to .csv files." + Environment.NewLine;
                    detailedHelpInfo += "    /Version             Display the version number." + Environment.NewLine;
                    detailedHelpInfo += "    /Verbose             Write messages to StdOut when a simulation starts/finishes. Only has an effect when running a directory of .apsimx files (*.apsimx)." + Environment.NewLine;
                    detailedHelpInfo += "    /Upgrade             Upgrades a file to the latest version of the .apsimx file format. Does not run the file." + Environment.NewLine;
                    detailedHelpInfo += "    /m                   Use the experimental multi-process job runner." + Environment.NewLine;
                    detailedHelpInfo += "    /?                   Show detailed help information.";
                    Console.WriteLine(detailedHelpInfo);
                    return(1);
                }

                if (args.Length < 1 || args.Length > 10)
                {
                    Console.WriteLine(usageMessage);
                    return(1);
                }

                if (args.Contains("/Version"))
                {
                    Model m = new Model();
                    Console.WriteLine(m.ApsimVersion);
                    return(0);
                }

                if (args.Contains("/Upgrade"))
                {
                    string dir = Path.GetDirectoryName(fileName);
                    if (string.IsNullOrWhiteSpace(dir))
                    {
                        dir = Directory.GetCurrentDirectory();
                    }
                    string[] files = Directory.EnumerateFiles(dir, Path.GetFileName(fileName), args.Contains("/Recurse") ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).ToArray();
                    foreach (string file in files)
                    {
                        List <Exception> errors;
                        IModel           sims = FileFormat.ReadFromFile <Model>(file, out errors);
                        if (errors != null && errors.Count > 0)
                        {
                            foreach (Exception error in errors)
                            {
                                Console.Error.WriteLine(error.ToString());
                            }
                        }
                        File.WriteAllText(file, FileFormat.WriteToString(sims));
                        Console.WriteLine("Successfully upgraded " + file);
                    }
                    return(0);
                }

                Stopwatch timer = new Stopwatch();
                timer.Start();

                // If the filename argument has a wildcard then create a IJobManager to go look for matching files to run.
                // Otherwise, create a JobManager to open the filename and run it in a separate, external process
                IJobManager job;
                bool        hasWildcard = fileName.Contains('*') || fileName.Contains('?');
                if (hasWildcard)
                {
                    job = Runner.ForFolder(fileName, args.Contains("/Recurse"), args.Contains("/RunTests"), args.Contains("/Verbose"), args.Contains("/m"));
                }
                else
                {
                    job = Runner.ForFile(fileName, args.Contains("/RunTests"));
                }

                // Run the job created above using either a single thread or multi threaded (default)
                IJobRunner jobRunner;
                if (args.Contains("/SingleThreaded"))
                {
                    jobRunner = new JobRunnerSync();
                }
                else if (args.Contains("/m"))
                {
                    // If the multi-process switch has been provided as well as a wildcard in the filename,
                    // we want to use the single threaded job runner, but run each job in multi-process mode.
                    // TODO : might be useful to allow users to run a directory of apsimx files via the multi-
                    // process job runner. This could be faster if they have many small files.
                    if (hasWildcard)
                    {
                        jobRunner = new JobRunnerSync();
                    }
                    else
                    {
                        jobRunner = new JobRunnerMultiProcess(args.Contains("/Verbose"));
                    }
                }
                else
                {
                    jobRunner = new JobRunnerAsync();
                }
                if (args.Select(arg => arg.ToLower()).Contains("/csv"))
                {
                    string dir = Path.GetDirectoryName(fileName);
                    if (dir == "")
                    {
                        dir = Directory.GetCurrentDirectory();
                    }
                    files = Directory.GetFiles(
                        dir,
                        Path.GetFileName(fileName),
                        args.Contains("/Recurse") ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).ToList();
                    jobRunner.AllJobsCompleted += GenerateCsvFiles;
                }
                jobRunner.JobCompleted     += OnJobCompleted;
                jobRunner.AllJobsCompleted += OnAllJobsCompleted;
                jobRunner.Run(job, wait: true);

                // If errors occurred, write them to the console.
                if (errors != string.Empty)
                {
                    Console.WriteLine("ERRORS FOUND!!");
                    Console.WriteLine(errors);
                    exitCode = 1;
                }
                else
                {
                    exitCode = 0;
                }

                timer.Stop();
                Console.WriteLine("Finished running simulations. Duration " + timer.Elapsed.TotalSeconds.ToString("#.00") + " sec.");
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
                exitCode = 1;
            }

            return(exitCode);
        }