コード例 #1
0
            /// <summary>
            /// Run this job.
            /// </summary>
            public void Run(object sender, System.ComponentModel.DoWorkEventArgs e)
            {
                // Extract the path from the filespec. If non specified then assume
                // current working directory.
                string path = Path.GetDirectoryName(FileSpec);

                if (path == null)
                {
                    path = Directory.GetCurrentDirectory();
                }

                string fileSpecNoPath = Path.GetFileName(FileSpec);

                List <string> Files;

                if (Recurse)
                {
                    Files = Directory.GetFiles(path, fileSpecNoPath, SearchOption.AllDirectories).ToList();
                }
                else
                {
                    Files = Directory.GetFiles(path, fileSpecNoPath, SearchOption.TopDirectoryOnly).ToList();
                }

                Files.RemoveAll(s => s.Contains("UnitTests"));

                // Get a reference to the JobManager so that we can add jobs to it.
                Utility.JobManager jobManager = e.Argument as Utility.JobManager;

                // For each .apsimx file - read it in and create a job for each simulation it contains.
                bool errorsFound = false;

                foreach (string apsimxFileName in Files)
                {
                    Simulations simulations = Simulations.Read(apsimxFileName);
                    if (simulations.LoadErrors.Count == 0)
                    {
                        jobManager.AddJob(simulations);
                    }
                    else
                    {
                        foreach (Exception err in simulations.LoadErrors)
                        {
                            Console.WriteLine(err.Message);
                            Console.WriteLine("Filename: " + apsimxFileName);
                            Console.WriteLine(err.StackTrace);
                            errorsFound = true;
                        }
                    }
                }

                if (errorsFound)
                {
                    // We've already outputted the load errors above. Just need to flag
                    // that an error has occurred.
                    throw new Exception("");
                }
            }
コード例 #2
0
ファイル: Main.cs プロジェクト: hol353/ApsimX
        /// <summary>
        /// Main program entry point.
        /// </summary>
        static int Main(string[] args)
        {
            try
            {
                string fileName = null;
                string commandLineSwitch = null;

                // Check the command line arguments.
                if (args.Length >= 1)
                    fileName = args[0];
                if (args.Length == 2)
                    commandLineSwitch = args[1];
                if (args.Length < 1 || args.Length > 4)
                    throw new Exception("Usage: ApsimX ApsimXFileSpec [/Recurse] [/Network] [/IP:<server IP>]");

                // Create a instance of a job that will go find .apsimx files. Then
                // pass the job to a job runner.
                RunDirectoryOfApsimFiles runApsim = new RunDirectoryOfApsimFiles(fileName, commandLineSwitch);

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

                int numSimulations = 0;
                if (commandLineSwitch == "/SingleThreaded")
                    numSimulations = RunSingleThreaded(fileName);
                else if (args.Contains("/Network"))
                {
                    try
                    {
                        int IPindex = -1;
                        for (int i = 0; i < args.Length;i++ )
                            if (args[i].Contains("IP"))
                            {
                                IPindex = i;
                                break;
                            }
                        if (IPindex == -1)
                            throw new Exception("/Network specified, but no IP given (/IP:<server IP>]");

                            DoNetworkRun(fileName, args[IPindex].Split(':')[1], args.Contains("/Recurse"));// send files over network
                    }
                    catch (SocketException)
                    {
                        Console.WriteLine("Connection to server terminated.");
                    }
                }
                else
                {
                    Utility.JobManager jobManager = new Utility.JobManager();
                    jobManager.OnComplete += OnError;
                    jobManager.AddJob(runApsim);
                    jobManager.Start(waitUntilFinished: true);
                    if (jobManager.SomeHadErrors)
                    {
                        Console.WriteLine("Errors found");
                        return 1;
                    }

                    // Write out the number of simulations run to the console.
                    numSimulations = jobManager.NumberOfJobs - 1;
                }
                timer.Stop();
                Console.WriteLine("Finished running " + numSimulations.ToString() + " simulations. Duration " + timer.Elapsed.TotalSeconds.ToString("#.00") + " sec.");
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
                return 1;
            }
            return 0;
        }
コード例 #3
0
        /// <summary>
        /// Main program entry point.
        /// </summary>
        static int Main(string[] args)
        {
            try
            {
                string fileName          = null;
                string commandLineSwitch = null;

                // Check the command line arguments.
                if (args.Length >= 1)
                {
                    fileName = args[0];
                }
                if (args.Length == 2)
                {
                    commandLineSwitch = args[1];
                }
                if (args.Length < 1 || args.Length > 4)
                {
                    throw new Exception("Usage: ApsimX ApsimXFileSpec [/Recurse] [/Network] [/IP:<server IP>]");
                }

                // Create a instance of a job that will go find .apsimx files. Then
                // pass the job to a job runner.
                RunDirectoryOfApsimFiles runApsim = new RunDirectoryOfApsimFiles(fileName, commandLineSwitch);

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

                int numSimulations = 0;
                if (commandLineSwitch == "/SingleThreaded")
                {
                    numSimulations = RunSingleThreaded(fileName);
                }
                else if (args.Contains("/Network"))
                {
                    try
                    {
                        int IPindex = -1;
                        for (int i = 0; i < args.Length; i++)
                        {
                            if (args[i].Contains("IP"))
                            {
                                IPindex = i;
                                break;
                            }
                        }
                        if (IPindex == -1)
                        {
                            throw new Exception("/Network specified, but no IP given (/IP:<server IP>]");
                        }

                        DoNetworkRun(fileName, args[IPindex].Split(':')[1], args.Contains("/Recurse"));    // send files over network
                    }
                    catch (SocketException)
                    {
                        Console.WriteLine("Connection to server terminated.");
                    }
                }
                else
                {
                    Utility.JobManager jobManager = new Utility.JobManager();
                    jobManager.OnComplete += OnError;
                    jobManager.AddJob(runApsim);
                    jobManager.Start(waitUntilFinished: true);
                    if (jobManager.SomeHadErrors)
                    {
                        Console.WriteLine("Errors found");
                        return(1);
                    }

                    // Write out the number of simulations run to the console.
                    numSimulations = jobManager.NumberOfJobs - 1;
                }
                timer.Stop();
                Console.WriteLine("Finished running " + numSimulations.ToString() + " simulations. Duration " + timer.Elapsed.TotalSeconds.ToString("#.00") + " sec.");
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
                return(1);
            }
            return(0);
        }