Exemple #1
0
        public void Initialise()
        {
            FileStream oldfile = new FileStream("Continuous_Wheat.apsim", FileMode.Create);

            oldfile.Write(UnitTests.Properties.Resources.Continuous_Wheat, 0, UnitTests.Properties.Resources.Continuous_Wheat.Length);
            oldfile.Close();

            FileStream f = new FileStream("Test.apsimx", FileMode.Create);

            f.Write(UnitTests.Properties.Resources.TestFile, 0, UnitTests.Properties.Resources.TestFile.Length);
            f.Close();
            FileStream w = new FileStream("Goondiwindi.met", FileMode.Create);

            w.Write(UnitTests.Properties.Resources.Goondiwindi, 0, UnitTests.Properties.Resources.Goondiwindi.Length);
            w.Close();
            this.simulations = Simulations.Read("Test.apsimx");

            string sqliteSourceFileName = this.FindSqlite3DLL();

            string sqliteFileName = Path.Combine(Directory.GetCurrentDirectory(), "sqlite3.dll");

            if (!File.Exists(sqliteFileName))
            {
                File.Copy(sqliteSourceFileName, sqliteFileName);
            }

            this.simulation = this.simulations.Children[0] as Simulation;
            this.simulation.StartRun();
        }
        /// <summary>Open an .apsimx file into the current tab.</summary>
        /// <param name="fileName">The file to open</param>
        /// <param name="onLeftTabControl">If true a tab will be added to the left hand tab control.</param>
        public ExplorerPresenter OpenApsimXFileInTab(string fileName, bool onLeftTabControl)
        {
            ExplorerPresenter presenter = null;

            if (fileName != null)
            {
                presenter = PresenterForFile(fileName, onLeftTabControl);
                if (presenter != null)
                {
                    view.SelectTabContaining(presenter.GetView().MainWidget);
                    return(presenter);
                }
                view.ShowWaitCursor(true);
                try
                {
                    Simulations simulations = Simulations.Read(fileName);
                    presenter = CreateNewTab(fileName, simulations, onLeftTabControl);
                    // Add to MRU list and update display
                    Utility.Configuration.Settings.AddMruFile(fileName);
                    UpdateMRUDisplay();
                }

                catch (Exception err)
                {
                    this.view.ShowMessage(err.Message, Simulation.ErrorLevel.Error);
                }

                view.ShowWaitCursor(false);
            }

            return(presenter);
        }
Exemple #3
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("");
                }
            }
Exemple #4
0
        /// <summary>Open an .apsimx file into the current tab.</summary>
        /// <param name="name">Name of the simulation</param>
        /// <param name="contents">The xml content</param>
        /// <param name="onLeftTabControl">If true a tab will be added to the left hand tab control.</param>
        private void OpenApsimXFromMemoryInTab(string name, string contents, bool onLeftTabControl)
        {
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(contents);
            Simulations simulations = Simulations.Read(doc.DocumentElement);

            this.CreateNewTab(name, simulations, onLeftTabControl, "UserInterface.Views.ExplorerView", "UserInterface.Presenters.ExplorerPresenter");
        }
        /// <summary>Open an .apsimx file into the current tab.</summary>
        /// <param name="name">Name of the simulation</param>
        /// <param name="contents">The xml content</param>
        /// <param name="onLeftTabControl">If true a tab will be added to the left hand tab control.</param>
        private void OpenApsimXFromMemoryInTab(string name, string contents, bool onLeftTabControl)
        {
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(contents);
            Simulations simulations = Simulations.Read(doc.DocumentElement);

            CreateNewTab(name, simulations, onLeftTabControl);
        }
Exemple #6
0
        /// <summary>
        /// Run all simulations in the specified 'fileName' in a serial mode.
        /// i.e. don't use the multi-threaded JobManager. Useful for profiling.
        /// </summary>
        /// <param name="fileName"> the name of file to run </param>
        private static void RunSingleThreaded(string fileName)
        {
            Simulations simulations = Simulations.Read(fileName);

            // Don't use JobManager - just run the simulations.
            JobManager.IRunnable simulationsToRun = Runner.ForSimulations(simulations, simulations, false);
            JobManager           jobManager       = new JobManager();

            jobManager.AddJob(simulationsToRun);
            jobManager.Run();
        }
Exemple #7
0
        /// <summary>
        /// Run all simulations in the specified 'fileName' in a serial mode.
        /// i.e. don't use the multi-threaded JobManager. Useful for profiling.
        /// </summary>
        /// <param name="fileName"> the name of file to run </param>
        private static void RunSingleThreaded(string fileName)
        {
            Simulations simulations = Simulations.Read(fileName);

            // Don't use JobManager - just run the simulations.
            Simulation[] simulationsToRun = Simulations.FindAllSimulationsToRun(simulations);
            foreach (Simulation simulation in simulationsToRun)
            {
                simulation.Run(null, null);
            }
        }
Exemple #8
0
        public void ImportOldAPSIM()
        {
            // test the importing of an example simulation from APSIM 7.6
            APSIMImporter importer = new APSIMImporter();

            importer.ProcessFile("Continuous_Wheat.apsim");

            Simulations testrunSimulations = Simulations.Read("Continuous_Wheat.apsimx");

            Assert.IsNotNull(Apsim.Find(testrunSimulations, "wheat"));
            Assert.IsNotNull(Apsim.Find(testrunSimulations, "clock"));
            Assert.IsNotNull(Apsim.Find(testrunSimulations, "SoilNitrogen"));
            Assert.IsNotNull(Apsim.Find(testrunSimulations, "SoilWater"));
        }
Exemple #9
0
        /// <summary>
        /// Open an .apsimx file into the current tab.
        /// </summary>
        /// <param name="name">Name of the simulation</param>
        /// <param name="contents">The xml content</param>
        public void OpenApsimXFromMemoryInTab(string name, string contents)
        {
            ExplorerView      explorerView = new ExplorerView();
            ExplorerPresenter presenter    = new ExplorerPresenter();

            this.Presenters.Add(presenter);
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(contents);
            Simulations simulations = Simulations.Read(doc.DocumentElement);

            presenter.Attach(simulations, explorerView, null);

            this.view.AddTab(name, Properties.Resources.apsim_logo32, explorerView, true);
        }
Exemple #10
0
            /// <summary>
            /// Run this job.
            /// </summary>
            /// <param name="sender">A system telling us to go </param>
            /// <param name="e">Arguments to same </param>
            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(this.FileSpec);

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

                List <string> files = Directory.GetFiles(
                    path,
                    Path.GetFileName(this.FileSpec),
                    this.DoRecurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).ToList();

                // See above. FIXME!
                files.RemoveAll(s => s.Contains("UnitTests"));

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

                // For each .apsimx file - read it in and create a job for each simulation it contains.
                string errors = string.Empty;

                foreach (string apsimxFileName in files)
                {
                    Simulations simulations = Simulations.Read(apsimxFileName);
                    if (simulations.LoadErrors.Count == 0)
                    {
                        jobs.Add(simulations);
                        jobManager.AddJob(simulations);
                    }
                    else
                    {
                        foreach (Exception err in simulations.LoadErrors)
                        {
                            errors += err.Message + "\r\n";
                            errors += err.StackTrace + "\r\n";
                        }
                    }
                }

                if (errors != string.Empty)
                {
                    ErrorMessage = errors;
                }
            }
Exemple #11
0
        /// <summary>
        /// Open an .apsimx file into the current tab.
        /// </summary>
        /// <param name="fileName">The file to open</param>
        public void OpenApsimXFileInTab(string fileName)
        {
            if (fileName != null)
            {
                ExplorerView      explorerView = new ExplorerView(this.view as ViewBase);
                ExplorerPresenter presenter    = new ExplorerPresenter();
                this.Presenters.Add(presenter);
                try
                {
                    this.view.WaitCursor = true;
                    try
                    {
                        Simulations simulations = Simulations.Read(fileName);
                        presenter.Attach(simulations, explorerView, null);
                        this.view.AddTab(fileName, "ApsimNG.Resources.apsim logo32.png", explorerView.MainWidget, true);

                        // restore the simulation tree width on the form
                        if (simulations.ExplorerWidth == 0)
                        {
                            presenter.TreeWidth = 250;
                        }
                        else
                        {
                            presenter.TreeWidth = Math.Min(simulations.ExplorerWidth, this.view.TabWidth - 20); // ?
                        }
                        Utility.Configuration.Settings.AddMruFile(fileName);
                        List <string> validMrus = new List <string>();                           // make sure recently used files still exist before displaying them
                        foreach (string s in Utility.Configuration.Settings.MruList)
                        {
                            if (File.Exists(s))
                            {
                                validMrus.Add(s);
                            }
                        }
                        Utility.Configuration.Settings.MruList = validMrus;
                        //this.view.FillMruList(validMrus);
                    }
                    finally
                    {
                        this.view.WaitCursor = false;
                    }
                }
                catch (Exception err)
                {
                    this.view.ShowError(err.Message);
                }
            }
        }
Exemple #12
0
        /// <summary>Open an .apsimx file into the current tab.</summary>
        /// <param name="fileName">The file to open</param>
        /// <param name="onLeftTabControl">If true a tab will be added to the left hand tab control.</param>
        /// <returns>The presenter</returns>
        public ExplorerPresenter OpenApsimXFileInTab(string fileName, bool onLeftTabControl)
        {
            ExplorerPresenter presenter = null;

            if (fileName != null)
            {
                presenter = this.PresenterForFile(fileName, onLeftTabControl);
                if (presenter != null)
                {
                    this.view.SelectTabContaining(presenter.GetView().MainWidget);
                    return(presenter);
                }

                this.view.ShowWaitCursor(true);
                try
                {
                    Simulations simulations = Simulations.Read(fileName);
                    presenter = (ExplorerPresenter)this.CreateNewTab(fileName, simulations, onLeftTabControl, "UserInterface.Views.ExplorerView", "UserInterface.Presenters.ExplorerPresenter");
                    if (simulations.LoadErrors.Count > 0)
                    {
                        foreach (Exception error in simulations.LoadErrors)
                        {
                            this.view.ShowMessage(error.ToString(), Simulation.ErrorLevel.Error);
                        }
                    }

                    // Add to MRU list and update display
                    Utility.Configuration.Settings.AddMruFile(fileName);
                    this.UpdateMRUDisplay();
                }
                catch (Exception err)
                {
                    string message = err.Message;
                    while (err.InnerException != null)
                    {
                        message += "\r\n" + err.InnerException.Message;
                        err      = err.InnerException;
                    }
                    this.view.ShowMessage(message, Simulation.ErrorLevel.Error);
                }

                this.view.ShowWaitCursor(false);
            }

            return(presenter);
        }
Exemple #13
0
        /// <summary>Open an .apsimx file into the current tab.</summary>
        /// <param name="fileName">The file to open</param>
        /// <param name="onLeftTabControl">If true a tab will be added to the left hand tab control.</param>
        public ExplorerPresenter OpenApsimXFileInTab(string fileName, bool onLeftTabControl)
        {
            ExplorerPresenter presenter = null;

            if (fileName != null)
            {
                view.ShowWaitCursor(true);
                try
                {
                    Simulations simulations = Simulations.Read(fileName);
                    presenter = CreateNewTab(fileName, simulations, onLeftTabControl);
                    Utility.Configuration.Settings.AddMruFile(fileName);
                }

                catch (Exception err)
                {
                    this.view.ShowMessage(err.Message, DataStore.ErrorLevel.Error);
                }

                view.ShowWaitCursor(false);
            }

            return(presenter);
        }