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); }
/// <summary>Documents the specified model.</summary> /// <param name="modelNameToDocument">The model name to document.</param> /// <param name="tags">The auto doc tags.</param> /// <param name="headingLevel">The starting heading level.</param> public void DocumentModel(string modelNameToDocument, List <AutoDocumentation.ITag> tags, int headingLevel) { Simulation simulation = Apsim.Find(this, typeof(Simulation)) as Simulation; if (simulation != null) { // Find the model of the right name. IModel modelToDocument = Apsim.Find(simulation, modelNameToDocument); // If not found then find a model of the specified type. if (modelToDocument == null) { modelToDocument = Apsim.Get(simulation, "[" + modelNameToDocument + "]") as IModel; } // If the simulation has the same name as the model we want to document, dig a bit deeper if (modelToDocument == simulation) { modelToDocument = Apsim.ChildrenRecursivelyVisible(simulation).FirstOrDefault(m => m.Name.Equals(modelNameToDocument, StringComparison.OrdinalIgnoreCase)); } // If still not found throw an error. if (modelToDocument != null) { // Get the path of the model (relative to parentSimulation) to document so that // when replacements happen below we will point to the replacement model not the // one passed into this method. string pathOfSimulation = Apsim.FullPath(simulation) + "."; string pathOfModelToDocument = Apsim.FullPath(modelToDocument).Replace(pathOfSimulation, ""); // Clone the simulation SimulationDescription simDescription = new SimulationDescription(simulation); Simulation clonedSimulation = simDescription.ToSimulation(); // Now use the path to get the model we want to document. modelToDocument = Apsim.Get(clonedSimulation, pathOfModelToDocument) as IModel; if (modelToDocument == null) { throw new Exception("Cannot find model to document: " + modelNameToDocument); } // resolve all links in cloned simulation. Links.Resolve(clonedSimulation, true); modelToDocument.IncludeInDocumentation = true; foreach (IModel child in Apsim.ChildrenRecursively(modelToDocument)) { child.IncludeInDocumentation = true; } // Document the model. AutoDocumentation.DocumentModel(modelToDocument, tags, headingLevel, 0, documentAllChildren: true); // Unresolve links. Links.Unresolve(clonedSimulation, true); } } }
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); }
/// <summary>Documents the specified model.</summary> /// <param name="modelNameToDocument">The model name to document.</param> /// <param name="tags">The auto doc tags.</param> /// <param name="headingLevel">The starting heading level.</param> public void DocumentModel(string modelNameToDocument, List <AutoDocumentation.ITag> tags, int headingLevel) { Simulation simulation = this.FindInScope <Simulation>(); if (simulation != null) { // Find the model of the right name. IModel modelToDocument = simulation.FindInScope(modelNameToDocument); // If not found then find a model of the specified type. if (modelToDocument == null) { modelToDocument = simulation.FindByPath("[" + modelNameToDocument + "]")?.Value as IModel; } // If the simulation has the same name as the model we want to document, dig a bit deeper if (modelToDocument == simulation) { modelToDocument = simulation.FindAllDescendants().Where(m => !m.IsHidden).ToList().FirstOrDefault(m => m.Name.Equals(modelNameToDocument, StringComparison.OrdinalIgnoreCase)); } // If still not found throw an error. if (modelToDocument != null) { // Get the path of the model (relative to parentSimulation) to document so that // when replacements happen below we will point to the replacement model not the // one passed into this method. string pathOfSimulation = simulation.FullPath + "."; string pathOfModelToDocument = modelToDocument.FullPath.Replace(pathOfSimulation, ""); // Clone the simulation SimulationDescription simDescription = new SimulationDescription(simulation); Simulation clonedSimulation = simDescription.ToSimulation(); // Prepare the simulation for running - this perform misc cleanup tasks such // as removing disabled models, standardising the soil, resolving links, etc. clonedSimulation.Prepare(); FindInScope <IDataStore>().Writer.Stop(); // Now use the path to get the model we want to document. modelToDocument = clonedSimulation.FindByPath(pathOfModelToDocument)?.Value as IModel; if (modelToDocument == null) { throw new Exception("Cannot find model to document: " + modelNameToDocument); } // resolve all links in cloned simulation. Links.Resolve(clonedSimulation, true); // Document the model. AutoDocumentation.DocumentModel(modelToDocument, tags, headingLevel, 0, documentAllChildren: true); // Unresolve links. Links.Unresolve(clonedSimulation, true); } } }
/// <summary> /// Perform the command /// </summary> public void Do(CommandHistory commandHistory) { Simulation clonedSimulation = null; IEvent events = null; try { List <Simulation> sims = new List <Models.Core.Simulation>(); var sim = new SimulationDescription(simulation); sims.Add(sim.ToSimulation(explorerPresenter.ApsimXFile)); events = explorerPresenter.ApsimXFile.GetEventService(clonedSimulation); events.ConnectEvents(); explorerPresenter.ApsimXFile.Links.Resolve(clonedSimulation); 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 tempFileName = Path.GetTempFileName(); File.Delete(tempFileName); string htmlFileName = Path.ChangeExtension(tempFileName, ".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) { events.DisconnectEvents(); explorerPresenter.ApsimXFile.Links.Unresolve(clonedSimulation, allLinks: true); } } }
public void EnsureReplacementWithInvalidNameDoesntMatch() { var simulations = new Simulations() { Children = new List <Model>() { new Folder() { Name = "Replacements", Children = new List <Model>() { new MockWeather() { Name = "Dummy name", 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; // Name ('Dummy name') didn't match so property should still be 1. Assert.AreEqual(weather.MaxT, 1); }
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"); }
public void EnsureSoilIsStandardised() { var sim = new Simulation() { Name = "Simulation", Children = new List <Model>() { new Soil { Children = new List <Model>() { new Physical() { Thickness = new double[] { 100, 300, 300 }, BD = new double[] { 1.36, 1.216, 1.24 }, AirDry = new double[] { 0.135, 0.214, 0.261 }, LL15 = new double[] { 0.27, 0.267, 0.261 }, DUL = new double[] { 0.365, 0.461, 0.43 }, SAT = new double[] { 0.400, 0.481, 0.45 }, Children = new List <Model>() { new SoilCrop { Name = "Wheat", KL = new double[] { 0.06, 0.060, 0.060 }, LL = new double[] { 0.27, 0.267, 0.261 } } } }, new SoilWater(), new Organic { Thickness = new double[] { 100, 300 }, Carbon = new double[] { 2, 1 } }, new Chemical { Thickness = new double[] { 100, 200 }, CL = new double[] { 38, double.NaN } }, new Sample { Thickness = new double[] { 500 }, SW = new double[] { 0.103 }, OC = new double[] { 1.35 }, SWUnits = Sample.SWUnitsEnum.Gravimetric }, new Sample { Thickness = new double[] { 1000 }, NO3N = new double[] { 27 }, OC = new double[] { 1.35 }, SWUnits = Sample.SWUnitsEnum.Volumetric }, new CERESSoilTemperature(), } } } }; Apsim.ParentAllChildren(sim); var originalSoil = sim.Children[0] as Soil; var originalWater = originalSoil.Children[0] as Physical; originalSoil.OnCreated(); var simulationDescription = new SimulationDescription(sim); var newSim = simulationDescription.ToSimulation(); var water = newSim.Children[0].Children[0] as Physical; var soilOrganicMatter = newSim.Children[0].Children[2] as Organic; var sample = newSim.Children[0].Children[4] as Sample; // Make sure layer structures have been standardised. Assert.AreEqual(water.Thickness, originalWater.Thickness); Assert.AreEqual(soilOrganicMatter.Thickness, originalWater.Thickness); Assert.AreEqual(sample.Thickness, originalWater.Thickness); // Make sure sample units are volumetric. Assert.AreEqual(sample.SWUnits, Sample.SWUnitsEnum.Volumetric); }