コード例 #1
0
        /// <summary>
        /// Apply this CompositeFactor to the specified simulation
        /// </summary>
        /// <param name="simulationDescription">A description of a simulation.</param>
        public void ApplyToSimulation(SimulationDescription simulationDescription)
        {
            ParseAllSpecifications(out List <string> allPaths, out List <object> allValues);

            if (allPaths.Count > 1 && allPaths.Count != allValues.Count)
            {
                throw new Exception("The number of factor paths does not match the number of factor values");
            }

            // Add a simulation override for each path / value combination.
            for (int i = 0; i != allPaths.Count; i++)
            {
                if (allValues[i] is IModel)
                {
                    simulationDescription.AddOverride(new ModelReplacement(allPaths[i], allValues[i] as IModel));
                }
                else
                {
                    simulationDescription.AddOverride(new PropertyReplacement(allPaths[i], allValues[i]));
                }
            }

            if (!(Parent is Factors))
            {
                // Set descriptors in simulation.
                string descriptorName = Name;
                if (Parent != null)
                {
                    descriptorName = Parent.Name;
                }
                if (Specifications != null && Specifications.Count > 0)
                {
                    // compound factor value ie. one that has multiple specifications.
                    simulationDescription.Descriptors.Add(new SimulationDescription.Descriptor(descriptorName, Name));
                }
                else
                {
                    if (allValues[0] is IModel)
                    {
                        simulationDescription.Descriptors.Add(new SimulationDescription.Descriptor(descriptorName, (allValues[0] as IModel).Name));
                    }
                    else
                    {
                        simulationDescription.Descriptors.Add(new SimulationDescription.Descriptor(descriptorName, allValues[0].ToString()));
                    }
                }
            }
        }
コード例 #2
0
        public void EnsurePropertyReplacementsWork()
        {
            var sim = new Simulation()
            {
                Name     = "BaseSimulation",
                Children = new List <Model>()
                {
                    new MockWeather()
                    {
                        Name      = "Weather",
                        MaxT      = 1,
                        StartDate = DateTime.MinValue
                    },
                }
            };

            Apsim.ParentAllChildren(sim);

            var simulationDescription = new SimulationDescription(sim, "CustomName");

            simulationDescription.AddOverride(new PropertyReplacement("Weather.MaxT", 2));

            var newSim = simulationDescription.ToSimulation();

            var weather = newSim.Children[0] as MockWeather;

            Assert.AreEqual(weather.MaxT, 2);
        }
コード例 #3
0
        public void EnsureReplacementsNodeWorks()
        {
            var simulations = new Simulations()
            {
                Children = new List <Model>()
                {
                    new Folder()
                    {
                        Name     = "Replacements",
                        Children = new List <Model>()
                        {
                            new MockWeather()
                            {
                                Name      = "Weather",
                                MaxT      = 2,
                                StartDate = DateTime.MinValue
                            }
                        }
                    },

                    new Simulation()
                    {
                        Name     = "BaseSimulation",
                        Children = new List <Model>()
                        {
                            new MockWeather()
                            {
                                Name      = "Weather",
                                MaxT      = 1,
                                StartDate = DateTime.MinValue
                            },
                        }
                    }
                }
            };

            Apsim.ParentAllChildren(simulations);

            var sim = simulations.Children[1] as Simulation;
            var simulationDescription = new SimulationDescription(sim);

            var newSim  = simulationDescription.ToSimulation();
            var weather = newSim.Children[0] as MockWeather;

            Assert.AreEqual(weather.MaxT, 2);

            // Make sure any property overrides happens after a model replacement.
            simulationDescription.AddOverride(new PropertyReplacement("Weather.MaxT", 3));
            newSim  = simulationDescription.ToSimulation();
            weather = newSim.Children[0] as MockWeather;
            Assert.AreEqual(weather.MaxT, 3);
        }
コード例 #4
0
        public void EnsureModelOverrideWork()
        {
            var sim = new Simulation()
            {
                Name     = "BaseSimulation",
                Children = new List <Model>()
                {
                    new MockWeather()
                    {
                        Name      = "Weather",
                        MaxT      = 1,
                        StartDate = DateTime.MinValue
                    },
                }
            };

            Apsim.ParentAllChildren(sim);

            var replacementWeather = new MockWeather()
            {
                Name      = "Weather2",
                MaxT      = 2,
                StartDate = DateTime.MinValue
            };

            var simulationDescription = new SimulationDescription(sim, "CustomName");

            simulationDescription.AddOverride(new ModelReplacement("Weather", replacementWeather));

            var newSim = simulationDescription.ToSimulation();

            Assert.AreEqual(newSim.Name, "CustomName");

            var weather = newSim.Children[0] as MockWeather;

            Assert.AreEqual(weather.MaxT, 2);

            // The name of the new model should be the same as the original model.
            Assert.AreEqual(weather.Name, "Weather");
        }