Exemplo n.º 1
0
        /// <summary>
        /// Runs an R script and blocks the current thread until the script finishes its execution.
        /// </summary>
        /// <param name="fileName">Path to an R script. May be a file on disk, or an embedded resource.</param>
        /// <param name="arguments">Command line arguments to pass to the script.</param>
        /// <param name="throwOnError">Throw on error from R?</param>
        /// <returns>Standard output generated by the R script.</returns>
        public string Run(string fileName, bool throwOnError = true, params string[] arguments)
        {
            RunAsync(fileName, arguments);
            proc.WaitForExit();
            string message;

            if (proc.ExitCode != 0)
            {
                StringBuilder error = new StringBuilder("Error from R:");
                error.AppendLine($"Script path: '{fileName}'");
                error.AppendLine("Script contents:");
                error.AppendLine(File.ReadAllText(fileName));
                error.AppendLine("StdErr:");
                error.AppendLine(proc.StdErr);
                error.AppendLine("StdOut:");
                error.AppendLine(proc.StdOut);

                message = error.ToString();
                if (throwOnError)
                {
                    throw new Exception(message);
                }
            }
            else
            {
                message = proc.StdOut;
            }
            return(message);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Runs an R script and blocks the current thread until the script finishes its execution.
        /// </summary>
        /// <param name="fileName">Path to an R script. May be a file on disk, or an embedded resource.</param>
        /// <param name="arguments">Command line arguments to pass to the script.</param>
        /// <param name="throwOnError">Throw on error from R?</param>
        /// <returns>Standard output generated by the R script.</returns>
        public string Run(string fileName, bool throwOnError = true, params string[] arguments)
        {
            RunAsync(fileName, arguments);
            proc.WaitForExit();
            string message;

            if (proc.ExitCode != 0)
            {
                StringBuilder error = new StringBuilder();
                error.AppendLine("Error from R:");
                error.AppendLine($"Script path: '{fileName}'");
                error.AppendLine("StdErr:");
                error.AppendLine(proc.StdErr);
                error.AppendLine("StdOut:");
                error.AppendLine(proc.StdOut);
                error.AppendLine("Script contents:");
                error.AppendLine(File.ReadAllText(fileName));

                message = error.ToString();
                // RSCript will exit with non-0 exit code if cancelled.
                // No need to throw an exception in this case.
                if (throwOnError && !cancelToken.IsCancellationRequested)
                {
                    throw new Exception(message);
                }
            }
            else
            {
                message = proc.StdOut;
            }
            return(message);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Gets the path to an executable (uses the Unix which utility).
 /// Throws if the package does not exist. Obviously this will not
 /// work on Windows.
 /// </summary>
 private string GetPathToPackage(string package)
 {
     ProcessUtilities.ProcessWithRedirectedOutput findR = new ProcessUtilities.ProcessWithRedirectedOutput();
     findR.Start("/usr/bin/which", package, Path.GetTempPath(), true);
     findR.WaitForExit();
     if (string.IsNullOrEmpty(findR.StdOut) && !string.IsNullOrEmpty(findR.StdErr))
     {
         // If the shell command generated anything in StdErr, we display that message.
         throw new Exception("Encountered an error while searching for " + package + " installation: " + findR.StdErr);
     }
     return(findR.StdOut);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Runs an R script and blocks the current thread until the script finishes its execution.
 /// </summary>
 /// <param name="fileName">Path to an R script. May be a file on disk, or an embedded resource.</param>
 /// <param name="arguments">Command line arguments to pass to the script.</param>
 /// <param name="throwOnError">Throw on error from R?</param>
 /// <returns>Standard output generated by the R script.</returns>
 public string Run(string fileName, string arguments = "", bool throwOnError = true)
 {
     RunAsync(fileName, arguments);
     proc.WaitForExit();
     if (throwOnError && proc.ExitCode != 0)
     {
         string message = "Error from R: " + Environment.NewLine +
                          proc.StdErr + Environment.NewLine +
                          "Script:" + Environment.NewLine +
                          File.ReadAllText(fileName);
         throw new Exception(message);
     }
     return(Output);
 }
Exemplo n.º 5
0
        public static string RunModels(string arguments)
        {
            string pathToModels = typeof(IModel).Assembly.Location;

            ProcessUtilities.ProcessWithRedirectedOutput proc = new ProcessUtilities.ProcessWithRedirectedOutput();
            proc.Start(pathToModels, arguments, Path.GetTempPath(), true);
            proc.WaitForExit();

            if (proc.ExitCode != 0)
            {
                throw new Exception(proc.StdOut);
            }

            return(proc.StdOut);
        }
Exemplo n.º 6
0
        public void OnCreatedShouldFailRun()
        {
            string json     = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.OnCreatedError.apsimx");
            string fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".apsimx");

            File.WriteAllText(fileName, json);

            Assembly models    = typeof(IModel).Assembly;
            string   modelsExe = AppDomain.CurrentDomain.GetAssemblies().First(a => string.Equals(a.FullName, models.FullName, StringComparison.Ordinal)).Location;

            var proc = new ProcessUtilities.ProcessWithRedirectedOutput();

            proc.Start(modelsExe, fileName, Path.GetTempPath(), true);
            proc.WaitForExit();

            Assert.AreNotEqual(0, proc.ExitCode, "A file ran without error when an exception should have been thrown while opening the file.");
        }
Exemplo n.º 7
0
        /// <summary>
        /// Runs the R installer.
        /// </summary>
        /// <param name="installerPath">Path to the installer.</param>
        private void InstallR(string installerPath)
        {
            if (installerPath == null)
            {
                return;
            }

            // Setup a working directory.
            string workingDirectory = Path.Combine(Path.GetTempPath(), "RSetup");

            if (!Directory.Exists(workingDirectory))
            {
                Directory.CreateDirectory(workingDirectory);
            }

            // Copy installer to working directory.
            try
            {
                string newInstallerPath = Path.Combine(workingDirectory, Path.GetFileNameWithoutExtension(installerPath));
                File.Copy(installerPath, newInstallerPath);
                installerPath = newInstallerPath;
            }
            catch
            {
            }
            // Check to see if installer is already running for whatever reason.
            // Kill them if found.
            foreach (var process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(installerPath)))
            {
                process.Kill();
            }

            // Run the installer.
            var installer = new ProcessUtilities.ProcessWithRedirectedOutput();

            installer.Start(installerPath, "", workingDirectory, false);
            installer.WaitForExit();
        }
Exemplo n.º 8
0
Arquivo: R.cs Projeto: ed2014/ApsimX
 /// <summary>
 /// Runs an R script and blocks the current thread until the script finishes its execution.
 /// </summary>
 /// <param name="fileName">Path to an R script. May be a file on disk, or an embedded resource.</param>
 /// <param name="arguments">Command line arguments to pass to the script.</param>
 /// <returns>Standard output generated by the R script.</returns>
 public string Run(string fileName, string arguments = "")
 {
     RunAsync(fileName, arguments);
     proc.WaitForExit();
     return(Output);
 }
Exemplo n.º 9
0
        public void TestEditing()
        {
            string configFile = Path.GetTempFileName();

            File.WriteAllLines(configFile, new[]
            {
                // Modify an array
                "[Report].VariableNames = x,y,z",

                // Modify a date - try a few different formats.
                "[Clock].StartDate = 2000-01-01",
                "[Clock].EndDate = 2000-01-10T00:00:00",

                // Modify a string
                "[Weather].FileName = fdsa.met",
                @"[Weather2].FullFileName = .\jkl.met",

                // Replace a model with a model from another file.
                $"[Weather3] = {extFile}",
                $"[Weather4] = {extFile};[w2]",

                // Change a property of a resource model.
                "[Wheat].Leaf.Photosynthesis.RUE.FixedValue = 0.4",

                // Change a property of a manager script.
                "[Manager].Script.Amount = 1234",

                // Set an entire array.
                "[Physical].BD = 1, 2, 3, 4, 5",

                // Modify a single element of an array.
                "[Physical].AirDry[2] = 6",

                // Modify multiple elements of an array.
                "[Physical].LL15[3:4] = 7",
            });

            string models = typeof(IModel).Assembly.Location;
            string args   = $"{fileName} /Edit {configFile}";

            var proc = new ProcessUtilities.ProcessWithRedirectedOutput();

            proc.Start(models, args, Path.GetTempPath(), true, writeToConsole: true);
            proc.WaitForExit();

            // Children of simulation are, in order:
            // Clock, summary, zone, Weather, Weather2, w1, w2
            Assert.AreEqual(null, proc.StdOut);
            Assert.AreEqual(null, proc.StdErr);

            Simulations file = FileFormat.ReadFromFile <Simulations>(fileName, out List <Exception> errors);

            if (errors != null && errors.Count > 0)
            {
                throw errors[0];
            }

            var report = Apsim.Find(file, typeof(Models.Report)) as Models.Report;

            string[] variableNames = new[] { "x", "y", "z" };
            Assert.AreEqual(variableNames, report.VariableNames);

            IModel sim = Apsim.Child(file, typeof(Simulation));

            // Use an index-based lookup to locate child models.
            // When we replace an entire model, we want to ensure
            // that the replacement is inserted at the correct index.

            Clock clock = sim.Children[0] as Clock;

            Assert.AreEqual(new DateTime(2000, 1, 1), clock.StartDate);
            Assert.AreEqual(new DateTime(2000, 1, 10), clock.EndDate);

            var weather = sim.Children[3] as Models.Weather;

            Assert.NotNull(weather);
            Assert.AreEqual("Weather", weather.Name);
            Assert.AreEqual("fdsa.met", weather.FileName);

            var weather2 = sim.Children[4] as Models.Weather;

            Assert.NotNull(weather2);
            Assert.AreEqual("Weather2", weather2.Name);
            Assert.AreEqual(@".\jkl.met", weather2.FileName);

            // Weather3 and Weather4 should have been
            // renamed to w1 and w2, respectively.
            var weather3 = sim.Children[5] as Models.Weather;

            Assert.NotNull(weather3);
            Assert.AreEqual("w1", weather3.Name);
            Assert.AreEqual("w1.met", weather3.FileName);

            var weather4 = sim.Children[6] as Models.Weather;

            Assert.NotNull(weather4);
            Assert.AreEqual("w2", weather4.Name);
            Assert.AreEqual("w2.met", weather4.FileName);

            // The edit file operation should have changed RUE value to 0.4.
            var wheat = sim.Children[2].Children[2] as Plant;
            var rue   = wheat.Children[6].Children[5].Children[0] as Constant;

            Assert.AreEqual(0.4, rue.FixedValue);

            double amount = (double)Apsim.Get(sim, "[Manager].Script.Amount");

            Assert.AreEqual(1234, amount);

            Physical physical = sim.Children[2].Children[4] as Physical;

            Assert.AreEqual(new double[5] {
                1, 2, 3, 4, 5
            }, physical.BD);
            Assert.AreEqual(new double[5] {
                0, 6, 0, 0, 0
            }, physical.AirDry);
            Assert.AreEqual(new double[5] {
                0, 0, 7, 7, 0
            }, physical.LL15);
        }
Exemplo n.º 10
0
        public void TestSimNameRegex()
        {
            string models = typeof(IModel).Assembly.Location;
            IModel sim1   = Utilities.GetRunnableSim().Children[1];

            sim1.Name = "sim1";

            IModel sim2 = Utilities.GetRunnableSim().Children[1];

            sim2.Name = "sim2";

            IModel sim3 = Utilities.GetRunnableSim().Children[1];

            sim3.Name = "simulation3";

            IModel sim4 = Utilities.GetRunnableSim().Children[1];

            sim4.Name = "Base";

            Simulations sims = Simulations.Create(new[] { sim1, sim2, sim3, sim4, new DataStore() });

            sims.ParentAllDescendants();

            string apsimxFileName = Path.ChangeExtension(Path.GetTempFileName(), ".apsimx");

            sims.Write(apsimxFileName);

            // Need to quote the regex on unix systems.
            string args;

            if (ProcessUtilities.CurrentOS.IsWindows)
            {
                args = $@"{apsimxFileName} /Verbose /SimulationNameRegexPattern:sim\d";
            }
            else
            {
                args = $@"{apsimxFileName} /Verbose '/SimulationNameRegexPattern:sim\d'";
            }

            ProcessUtilities.ProcessWithRedirectedOutput proc = new ProcessUtilities.ProcessWithRedirectedOutput();
            proc.Start(models, args, Directory.GetCurrentDirectory(), true);
            proc.WaitForExit();

            Assert.Null(proc.StdErr);
            Assert.True(proc.StdOut.Contains("sim1"));
            Assert.True(proc.StdOut.Contains("sim2"));
            Assert.False(proc.StdOut.Contains("simulation3"));
            Assert.False(proc.StdOut.Contains("Base"));

            args = $@"{apsimxFileName} /Verbose /SimulationNameRegexPattern:sim1";
            proc = new ProcessUtilities.ProcessWithRedirectedOutput();
            proc.Start(models, args, Directory.GetCurrentDirectory(), true);
            proc.WaitForExit();

            Assert.Null(proc.StdErr);
            Assert.True(proc.StdOut.Contains("sim1"));
            Assert.False(proc.StdOut.Contains("sim2"));
            Assert.False(proc.StdOut.Contains("simulation3"));
            Assert.False(proc.StdOut.Contains("Base"));

            args = $@"{apsimxFileName} /Verbose /SimulationNameRegexPattern:(simulation3)|(Base)";
            proc = new ProcessUtilities.ProcessWithRedirectedOutput();
            proc.Start(models, args, Directory.GetCurrentDirectory(), true);
            proc.WaitForExit();

            Assert.Null(proc.StdErr);
            Assert.False(proc.StdOut.Contains("sim1"));
            Assert.False(proc.StdOut.Contains("sim2"));
            Assert.True(proc.StdOut.Contains("simulation3"));
            Assert.True(proc.StdOut.Contains("Base"));
        }
Exemplo n.º 11
0
 /// <summary>
 /// Runs an R script and blocks the current thread until the script finishes its execution.
 /// </summary>
 /// <param name="fileName">Path to an R script.</param>
 /// <returns>Standard output generated by the R script.</returns>
 public string Run(string fileName)
 {
     RunAsync(fileName);
     proc.WaitForExit();
     return(Output);
 }
Exemplo n.º 12
0
        public void TestEditing()
        {
            string configFile = Path.GetTempFileName();

            File.WriteAllLines(configFile, new[]
            {
                // Modify an array
                "[Report].VariableNames = x,y,z",

                // Modify a date - try a few different formats.
                "[Clock].StartDate = 2000-01-01",
                "[Clock].EndDate = 2000-01-10T00:00:00",

                // Modify a string
                "[Weather].FileName = fdsa.met",
                @"[Weather2].FullFileName = .\jkl.met",

                // Replace a model with a model from another file.
                $"[Weather3] = {extFile}",
                $"[Weather4] = {extFile};[w2]",
            });

            string models = typeof(IModel).Assembly.Location;
            string args   = $"{fileName} /Edit {configFile}";

            var proc = new ProcessUtilities.ProcessWithRedirectedOutput();

            proc.Start(models, args, Path.GetTempPath(), true, true);
            proc.WaitForExit();

            // Children of simulation are, in order:
            // Clock, summary, zone, Weather, Weather2, w1, w2

            Assert.AreEqual(null, proc.StdOut);
            Assert.AreEqual(null, proc.StdErr);

            Simulations file = FileFormat.ReadFromFile <Simulations>(fileName, out List <Exception> errors);

            if (errors != null && errors.Count > 0)
            {
                throw errors[0];
            }

            var report = Apsim.Find(file, typeof(Models.Report)) as Models.Report;

            string[] variableNames = new[] { "x", "y", "z" };
            Assert.AreEqual(variableNames, report.VariableNames);

            IModel sim = Apsim.Child(file, typeof(Simulation));

            // Use an index-based lookup to locate child models.
            // When we replace an entire model, we want to ensure
            // that the replacement is inserted at the correct index.

            Clock clock = sim.Children[0] as Clock;

            Assert.AreEqual(new DateTime(2000, 1, 1), clock.StartDate);
            Assert.AreEqual(new DateTime(2000, 1, 10), clock.EndDate);

            var weather = sim.Children[3] as Models.Weather;

            Assert.NotNull(weather);
            Assert.AreEqual("Weather", weather.Name);
            Assert.AreEqual("fdsa.met", weather.FileName);

            var weather2 = sim.Children[4] as Models.Weather;

            Assert.NotNull(weather2);
            Assert.AreEqual("Weather2", weather2.Name);
            Assert.AreEqual(@".\jkl.met", weather2.FileName);

            // Weather3 and Weather4 should have been
            // renamed to w1 and w2, respectively.
            var weather3 = sim.Children[5] as Models.Weather;

            Assert.NotNull(weather3);
            Assert.AreEqual("w1", weather3.Name);
            Assert.AreEqual("w1.met", weather3.FileName);

            var weather4 = sim.Children[6] as Models.Weather;

            Assert.NotNull(weather4);
            Assert.AreEqual("w2", weather4.Name);
            Assert.AreEqual("w2.met", weather4.FileName);
        }