Ejemplo n.º 1
0
        /// <summary>
        /// Perform the command
        /// </summary>
        public void Do(CommandHistory CommandHistory)
        {
            Simulation clonedSimulation = null;

            try
            {
                List <Simulation> sims = new List <Models.Core.Simulation>();
                clonedSimulation = Apsim.Clone(simulation) as Simulation;
                sims.Add(clonedSimulation);
                Simulations.MakeSubstitutions(explorerPresenter.ApsimXFile, sims);

                clonedSimulation.ConnectLinksAndEvents();

                List <ModelDoc> models = new List <ModelDoc>();
                foreach (IModel model in Apsim.ChildrenRecursively(clonedSimulation))
                {
                    ModelDoc newModelDoc = DocumentModel(model);
                    newModelDoc.Name = Apsim.FullPath(model);
                    models.Add(newModelDoc);
                }

                StringWriter  rawXML     = new StringWriter();
                XmlSerializer serialiser = new XmlSerializer(typeof(List <ModelDoc>));
                serialiser.Serialize(rawXML, models);
                rawXML.Close();

                // Load the XSL transform from the resource
                Stream s         = Assembly.GetExecutingAssembly().GetManifestResourceStream("ApsimNG.Resources.DebugDoc.xsl");
                var    transform = new XslCompiledTransform();
                using (XmlReader reader = XmlReader.Create(s))
                {
                    transform.Load(reader);
                }

                // Apply the transform to the reader and write it to a temporary file.
                string htmlFileName = Path.GetTempFileName() + ".html";
                using (XmlReader reader = XmlReader.Create(new StringReader(rawXML.ToString())))
                    using (XmlWriter htmlWriter = XmlWriter.Create(htmlFileName))
                    {
                        transform.Transform(reader, htmlWriter);
                    }
                Process.Start(htmlFileName);
            }
            finally
            {
                if (clonedSimulation != null)
                {
                    clonedSimulation.DisconnectLinksAndEvents();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>Gets the next job to run</summary>
        public Simulation NextSimulationToRun()
        {
            if (allCombinations == null || allCombinations.Count == 0)
            {
                return(null);
            }

            if (serialisedBase == null)
            {
                allCombinations   = AllCombinations();
                parentSimulations = Apsim.Parent(this, typeof(Simulations)) as Simulations;
                Simulation baseSimulation = Apsim.Child(this, typeof(Simulation)) as Simulation;
                serialisedBase = Apsim.SerialiseToStream(baseSimulation) as Stream;
            }

            var combination = allCombinations[0];

            allCombinations.RemoveAt(0);
            string newSimulationName = Name;

            foreach (FactorValue value in combination)
            {
                newSimulationName += value.Name;
            }

            Simulation newSimulation = Apsim.DeserialiseFromStream(serialisedBase) as Simulation;

            newSimulation.Name     = newSimulationName;
            newSimulation.Parent   = null;
            newSimulation.FileName = parentSimulations.FileName;
            Apsim.ParentAllChildren(newSimulation);

            // Make substitutions.
            parentSimulations.MakeSubstitutions(newSimulation);

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

            events.Publish("Loaded", new object[] { newSimulation, loadedArgs });

            foreach (FactorValue value in combination)
            {
                value.ApplyToSimulation(newSimulation);
            }

            PushFactorsToReportModels(newSimulation, combination);
            StoreFactorsInDataStore(newSimulation, combination);
            return(newSimulation);
        }
Ejemplo n.º 3
0
        /// <summary>Called to start the job.</summary>
        /// <param name="jobManager">The job manager running this job.</param>
        /// <param name="workerThread">The thread this job is running on.</param>
        public void Run(JobManager jobManager, BackgroundWorker workerThread)
        {
            Simulation clonedSim = Apsim.Clone(simulation) as Simulation;

            Simulations simulations = Apsim.Parent(simulation, typeof(Simulations)) as Simulations;

            simulation.FileName = simulations.FileName;

            Simulations.MakeSubstitutions(simulations, new List <Simulation> {
                clonedSim
            });

            Simulations.CallOnLoaded(clonedSim);
            jobManager.AddChildJob(this, clonedSim);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create a specific simulation.
        /// </summary>
        public Simulation CreateSpecificSimulation(string name)
        {
            List <List <FactorValue> > allCombinations = AllCombinations();
            Simulation  baseSimulation    = Apsim.Child(this, typeof(Simulation)) as Simulation;
            Simulations parentSimulations = Apsim.Parent(this, typeof(Simulations)) as Simulations;

            foreach (List <FactorValue> combination in allCombinations)
            {
                string newSimulationName = Name;
                foreach (FactorValue value in combination)
                {
                    newSimulationName += value.Name;
                }

                if (newSimulationName == name)
                {
                    Simulation newSimulation = Apsim.Clone(baseSimulation) as Simulation;
                    newSimulation.Name     = newSimulationName;
                    newSimulation.Parent   = null;
                    newSimulation.FileName = parentSimulations.FileName;
                    Apsim.ParentAllChildren(newSimulation);

                    // Make substitutions.
                    Simulations.MakeSubstitutions(parentSimulations, new List <Simulation> {
                        newSimulation
                    });

                    // Connect events and links in our new  simulation.
                    Events events = new Events();
                    events.AddModelEvents(newSimulation);
                    events.CallEventHandler(newSimulation, "Loaded", null);

                    foreach (FactorValue value in combination)
                    {
                        value.ApplyToSimulation(newSimulation);
                    }

                    PushFactorsToReportModels(newSimulation, combination);

                    return(newSimulation);
                }
            }

            return(null);
        }
Ejemplo n.º 5
0
        /// <summary>Called to start the job.</summary>
        /// <param name="jobManager">The job manager running this job.</param>
        /// <param name="workerThread">The thread this job is running on.</param>
        public void Run(JobManager jobManager, BackgroundWorker workerThread)
        {
            List <List <FactorValue> > allCombinations = AllCombinations();
            Simulation  baseSimulation    = Apsim.Child(this, typeof(Simulation)) as Simulation;
            Simulations parentSimulations = Apsim.Parent(this, typeof(Simulations)) as Simulations;

            Stream serialisedBase = Apsim.SerialiseToStream(baseSimulation) as Stream;

            List <Simulation> simulations = new List <Simulation>();

            foreach (List <FactorValue> combination in allCombinations)
            {
                string newSimulationName = Name;
                foreach (FactorValue value in combination)
                {
                    newSimulationName += value.Name;
                }

                Simulation newSimulation = Apsim.DeserialiseFromStream(serialisedBase) as Simulation;
                newSimulation.Name     = newSimulationName;
                newSimulation.Parent   = null;
                newSimulation.FileName = parentSimulations.FileName;
                Apsim.ParentAllChildren(newSimulation);

                // Make substitutions.
                Simulations.MakeSubstitutions(parentSimulations, new List <Simulation> {
                    newSimulation
                });

                // Call OnLoaded in all models.
                Events events = new Events();
                events.AddModelEvents(newSimulation);
                events.CallEventHandler(newSimulation, "Loaded", null);

                foreach (FactorValue value in combination)
                {
                    value.ApplyToSimulation(newSimulation);
                }

                PushFactorsToReportModels(newSimulation, combination);
                StoreFactorsInDataStore(newSimulation, combination);
                jobManager.AddChildJob(this, newSimulation);
            }
        }
Ejemplo n.º 6
0
        /// <summary>Gets the next job to run</summary>
        public IRunnable NextJobToRun()
        {
            if (allCombinations == null || allCombinations.Count == 0)
            {
                return(null);
            }

            var combination = allCombinations[0];

            allCombinations.RemoveAt(0);
            string newSimulationName = Name;

            foreach (FactorValue value in combination)
            {
                newSimulationName += value.Name;
            }

            Simulation newSimulation = Apsim.DeserialiseFromStream(serialisedBase) as Simulation;

            newSimulation.Name     = newSimulationName;
            newSimulation.Parent   = null;
            newSimulation.FileName = parentSimulations.FileName;
            Apsim.ParentAllChildren(newSimulation);

            // Make substitutions.
            parentSimulations.MakeSubstitutions(newSimulation);

            // Call OnLoaded in all models.
            Events events = new Events(newSimulation);

            events.Publish("Loaded", null);

            foreach (FactorValue value in combination)
            {
                value.ApplyToSimulation(newSimulation);
            }

            PushFactorsToReportModels(newSimulation, combination);
            StoreFactorsInDataStore(newSimulation, combination);
            return(new RunSimulation(newSimulation, doClone: false));
        }