/// <summary> /// Initializes a new instance of the <see cref="APSIMFileWriter"/> class. /// This object is used to write the fields to the SDML xml file /// passed from an AusFarmSpec object via the AusFarmFiles host. /// </summary> public AusFarmFileWriter(SimulationType templateType) { // Load the template file. string scriptTemplate = ""; switch (templateType) { case SimulationType.stCropOnly: scriptTemplate = "APSIM.Cloud.Shared.Resources.ausfarm_crop_only.sdml"; break; case SimulationType.stSingleFlock: scriptTemplate = "APSIM.Cloud.Shared.Resources.ausfarm_warooka.sdml"; break; case SimulationType.stDualFlock: scriptTemplate = "APSIM.Cloud.Shared.Resources.ausfarm_dual_flock.sdml"; break; } if (scriptTemplate.Length > 0) { Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(scriptTemplate); StreamReader reader = new StreamReader(s); xmlScriptDoc = new TCompParser(reader.ReadToEnd()); simulationXMLNode = xmlScriptDoc.rootNode(); } else simulationXMLNode = null; }
/// <summary> /// Get the init sdml property from an AusFarm component's init section. /// </summary> /// <param name="compNode">The xml node for the component</param> /// <param name="varName">Name of the init value</param> /// <returns>An TSDMLValue</returns> private TSDMLValue GetTypedInit(XmlNode compNode, string varName) { TSDMLValue init = null; if (compNode != null) { TCompParser comp = new TCompParser(compNode.OuterXml); if (!comp.IsAPSRU()) { //AusFarm inits string sdml = comp.initTextByName(varName); if (sdml.Length > 0) { init = new TSDMLValue(sdml, ""); } } } return init; }
/// <summary> /// Set the init value in the document using the TSDMLValue passed in. /// </summary> /// <param name="compNode">Component node in the document</param> /// <param name="varName">Name of the variable in the AusFarm init section</param> /// <param name="init">The TTypedValue with the required settings</param> private void SetTypedInit(XmlNode compNode, string varName, TSDMLValue init) { if (compNode != null) { TCompParser comp = new TCompParser(compNode.OuterXml); if (!comp.IsAPSRU()) { StringBuilder newInitSection = new StringBuilder(); newInitSection.Append("<initsection>"); //AusFarm inits //find the init node in the section uint i = 1; while (i <= comp.initCount()) { if (comp.initName(i) == varName) { string initStr = init.getText(init, 0, 2); initStr = initStr.Replace("'", "'"); //replace any escaped single quotes newInitSection.Append(initStr); } else newInitSection.Append(comp.initText(i)); i++; } newInitSection.Append("</initsection>"); XmlNode initdata = compNode.SelectSingleNode("initdata"); initdata.InnerXml = "<![CDATA[" + newInitSection.ToString() + "]]>"; } } }
/// <summary> /// Set a scalar value in the init section of a component. /// Handles the init sections of APSIM and AusFarm components. /// </summary> /// <param name="compNode">XmlNode for the component found</param> /// <param name="varName">Name of the variable. APSIM - xml tag, AusFarm - init name</param> /// <param name="value">The string value</param> private void SetValue(XmlNode compNode, string varName, string value) { if (compNode != null) { TCompParser comp = new TCompParser(compNode.OuterXml); XmlNode initdata = compNode.SelectSingleNode("initdata"); string newInitDataSection; if (comp.IsAPSRU()) { XmlUtilities.SetValue(initdata, varName, value); } else { //AusFarm inits string cdata = comp.initData(); XmlDocument doc = new XmlDocument(); doc.LoadXml(cdata); XmlNode initSection = doc.DocumentElement; XmlNode val = initSection.SelectSingleNode("/initsection/init[attribute::name=\"" + varName + "\"]/val"); if (val != null) val.InnerText = value; newInitDataSection = initSection.OuterXml; initdata.InnerXml = "<![CDATA[\n" + newInitDataSection + "\n]]>"; } } }