Esempio n. 1
0
        /// <summary>
        /// Create a simulations model
        /// </summary>
        /// <param name="children">The child models</param>
        public static Simulations Create(IEnumerable <IModel> children)
        {
            Simulations newSimulations = new Core.Simulations();

            newSimulations.Children.AddRange(children.Cast <Model>());

            // Call the OnDeserialised method in each model.
            Events events = new Core.Events(newSimulations);

            object[] args = new object[] { true };
            events.Publish("Deserialised", args);

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

            // Call OnLoaded in all models.
            LoadedEventArgs loadedArgs = new LoadedEventArgs();

            events.Publish("Loaded", new object[] { newSimulations, loadedArgs });
            if (loadedArgs.errors.Count > 0)
            {
                newSimulations.LoadErrors = new List <Exception>();
                newSimulations.LoadErrors.AddRange(loadedArgs.errors);
            }
            return(newSimulations);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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.
            APSIMFileConverter.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();
                events.AddModelEvents(simulations);
                object[] args = new object[] { true };
                events.CallEventHandler(simulations, "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
                    {
                        events.CallEventHandler(child, "Loaded", null);
                    }
                    catch (ApsimXException err)
                    {
                        simulations.LoadErrors.Add(err);
                    }
                    catch (Exception err)
                    {
                        err.Source = child.Name;
                        simulations.LoadErrors.Add(err);
                    }
                }
            }

            return(simulations);
        }
Esempio n. 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)
        {
            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);
        }
Esempio n. 5
0
        /// <summary>
        /// Create a simulations model
        /// </summary>
        /// <param name="children">The child models</param>
        public static Simulations Create(IEnumerable <Model> children)
        {
            Simulations newSimulations = new Core.Simulations();

            newSimulations.Children.AddRange(children);

            // Call the OnDeserialised method in each model.
            Events events = new Core.Events(newSimulations);

            object[] args = new object[] { true };
            events.Publish("Deserialised", args);

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

            // Call OnLoaded in all models.
            newSimulations.LoadErrors = events.Publish("Loaded", null);
            return(newSimulations);
        }
Esempio n. 6
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.
            APSIMFileConverter.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();
                events.AddModelEvents(simulations);
                object[] args = new object[] { true };
                events.CallEventHandler(simulations, "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
                    {
                        events.CallEventHandler(child, "Loaded", null);
                    }
                    catch (ApsimXException err)
                    {
                        simulations.LoadErrors.Add(err);
                    }
                    catch (Exception err)
                    {
                        err.Source = child.Name;
                        simulations.LoadErrors.Add(err);
                    }
                }
            }

            return simulations;
        }