예제 #1
0
        /// <summary>Create a simulations object by reading the specified filename</summary>
        /// <param name="FileName">Name of the file.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">Simulations.Read() failed. Invalid simulation file.\n</exception>
        public static Simulations Read(string FileName)
        {
            // Run the converter.
            ApsimFile.Converter.ConvertToLatestVersion(FileName);

            // Deserialise
            Simulations simulations = XmlUtilities.Deserialise(FileName, Assembly.GetExecutingAssembly()) as Simulations;

            if (simulations != null)
            {
                // Set the filename
                simulations.FileName = FileName;
                simulations.SetFileNameInAllSimulations();

                // Call the OnDeserialised method in each model.
                Events   events = new Core.Events(simulations);
                object[] args   = new object[] { true };
                events.Publish("Deserialised", args);

                // Parent all models.
                simulations.Parent = null;
                Apsim.ParentAllChildren(simulations);

                // Call OnLoaded in all models.
                LoadedEventArgs loadedArgs = new LoadedEventArgs();
                events.Publish("Loaded", new object[] { simulations, loadedArgs });
                if (loadedArgs.errors.Count > 0)
                {
                    simulations.LoadErrors = new List <Exception>();
                    simulations.LoadErrors.AddRange(loadedArgs.errors);
                }
            }

            return(simulations);
        }
예제 #2
0
        /// <summary>Create a simulations object by reading the specified filename</summary>
        /// <param name="node">The node.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">Simulations.Read() failed. Invalid simulation file.\n</exception>
        public static Simulations Read(XmlNode node)
        {
            // Run the converter.
            Converter.ConvertToLatestVersion(node);

            // Deserialise
            Simulations simulations = XmlUtilities.Deserialise(node, Assembly.GetExecutingAssembly()) as Simulations;

            if (simulations != null)
            {
                // Set the filename
                simulations.SetFileNameInAllSimulations();

                // Call the OnSerialised method in each model.
                object[] args = new object[] { true };
                foreach (Model model in Apsim.ChildrenRecursively(simulations))
                {
                    Apsim.CallEventHandler(model, "Deserialised", args);
                }

                // Parent all models.
                simulations.Parent = null;
                Apsim.ParentAllChildren(simulations);

                CallOnLoaded(simulations);
            }
            else
            {
                throw new Exception("Simulations.Read() failed. Invalid simulation file.\n");
            }
            return(simulations);
        }
예제 #3
0
        /// <summary>Create a simulations object by reading the specified filename</summary>
        /// <param name="node">The node.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">Simulations.Read() failed. Invalid simulation file.\n</exception>
        public static Simulations Read(XmlNode node)
        {
            // Run the converter.
            APSIMFileConverter.ConvertToLatestVersion(node, null);

            // Deserialise
            Simulations simulations = XmlUtilities.Deserialise(node, Assembly.GetExecutingAssembly()) as Simulations;

            if (simulations != null)
            {
                // Set the filename
                simulations.SetFileNameInAllSimulations();

                // Call the OnSerialised method in each model.
                object[] args   = new object[] { true };
                Events   events = new Events(simulations);
                events.Publish("Deserialised", args);

                // Parent all models.
                simulations.Parent = null;
                Apsim.ParentAllChildren(simulations);

                events.Publish("Loaded", null);
            }
            else
            {
                throw new Exception("Simulations.Read() failed. Invalid simulation file.\n");
            }
            return(simulations);
        }
예제 #4
0
        /// <summary>Create a simulations object by reading the specified filename</summary>
        /// <param name="FileName">Name of the file.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">Simulations.Read() failed. Invalid simulation file.\n</exception>
        public static Simulations Read(string FileName)
        {
            // Run the converter.
            Converter.ConvertToLatestVersion(FileName);

            // Deserialise
            Simulations simulations = XmlUtilities.Deserialise(FileName, Assembly.GetExecutingAssembly()) as Simulations;

            if (simulations != null)
            {
                // Set the filename
                simulations.FileName = FileName;
                simulations.SetFileNameInAllSimulations();

                // Call the OnDeserialised method in each model.
                object[] args = new object[] { true };
                foreach (Model model in Apsim.ChildrenRecursively(simulations))
                {
                    Apsim.CallEventHandler(model, "Deserialised", args);
                }

                // Parent all models.
                simulations.Parent = null;
                Apsim.ParentAllChildren(simulations);

                // Call OnLoaded in all models.
                simulations.LoadErrors = new List <Exception>();
                foreach (Model child in Apsim.ChildrenRecursively(simulations))
                {
                    try
                    {
                        Apsim.CallEventHandler(child, "Loaded", null);
                    }
                    catch (ApsimXException err)
                    {
                        simulations.LoadErrors.Add(err);
                    }
                    catch (Exception err)
                    {
                        err.Source = child.Name;
                        simulations.LoadErrors.Add(err);
                    }
                }
            }
            else
            {
                throw new Exception("Simulations.Read() failed. Invalid simulation file.\n");
            }
            return(simulations);
        }
예제 #5
0
        /// <summary>Create a simulations object by reading the specified filename</summary>
        /// <param name="FileName">Name of the file.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">Simulations.Read() failed. Invalid simulation file.\n</exception>
        public static Simulations Read(string FileName)
        {
            if (!File.Exists(FileName))
            {
                throw new Exception("Cannot read file: " + FileName + ". File does not exist.");
            }

            // Run the converter.
            Stream inStream = ApsimFile.Converter.ConvertToLatestVersion(FileName);

            // Deserialise
            Simulations simulations = XmlUtilities.Deserialise(inStream, Assembly.GetExecutingAssembly()) as Simulations;

            if (simulations.Version > ApsimFile.Converter.LatestVersion)
            {
                throw new Exception("This file has previously been opened with a more recent version of Apsim. Please upgrade to a newer version to open this file.");
            }
            inStream.Close();

            if (simulations != null)
            {
                // Set the filename
                simulations.FileName = FileName;
                simulations.SetFileNameInAllSimulations();

                // Call the OnDeserialised method in each model.
                Events   events = new Core.Events(simulations);
                object[] args   = new object[] { true };
                events.Publish("Deserialised", args);

                // Parent all models.
                simulations.Parent = null;
                Apsim.ParentAllChildren(simulations);

                // Call OnLoaded in all models.
                LoadedEventArgs loadedArgs = new LoadedEventArgs();
                events.Publish("Loaded", new object[] { simulations, loadedArgs });
                if (loadedArgs.errors.Count > 0)
                {
                    simulations.LoadErrors = new List <Exception>();
                    simulations.LoadErrors.AddRange(loadedArgs.errors);
                }
            }

            return(simulations);
        }