//======================================================================= /// <summary> /// Initialise the list of properties with values from the init section /// SDML. /// </summary> /// <param name="initXML">The init section which is <code><initsection>...</initsection></code></param> //======================================================================= private Boolean InitFromInitSection(String initXML) { if (initXML.Length > 0) { typedvals.Clear(); TInitParser initPsr = new TInitParser(initXML); for (int i = 1; i <= initPsr.initCount(); i++) { String initText = initPsr.initText((uint)i); TSDMLValue sdmlinit = new TSDMLValue(initText, ""); typedvals.Add(sdmlinit); if (sdmlinit.Name == "rules") { StringBuilder str = new StringBuilder(); for (uint line = 1; line <= sdmlinit.count(); line++) { str.Append(sdmlinit.item(line).asStr() + "\n"); } Highlight(str.ToString()); } if (sdmlinit.Name == "logfile") { textBox1.Text = sdmlinit.asStr(); checkBox1.Checked = (textBox1.Text.Length > 0); } if (sdmlinit.Name == "log_set") { } } } return(initXML.Length > 0); }
//===================================================================== /// <summary> /// Write the TTypedValues to an xml string. /// </summary> /// <returns>The init section string</returns> //===================================================================== protected override String WriteInitsectionXml() { StringBuilder newXML = new StringBuilder(); newXML.Append("<initsection>"); TSDMLValue sdmlWriter = new TSDMLValue("<init/>", ""); if (propertyList.Count > 0) //if using the full component description { for (int i = 0; i < propertyList.Count; i++) { if (propertyList[i].bInit == true) { newXML.Append(sdmlWriter.getText(propertyList[i].InitValue, 0, 2)); } } } else { for (int i = 0; i < typedvals.Count; i++) { newXML.Append(sdmlWriter.getText(typedvals[i], 0, 2)); } } newXML.Append("</initsection>"); return(newXML.ToString()); }
//===================================================================== /// <summary> /// Write the TTypedValues to an xml string. /// </summary> /// <returns>The init section string</returns> //===================================================================== protected override String WriteInitsectionXml() { StringBuilder newXML = new StringBuilder(); newXML.Append("<initsection>"); TSDMLValue sdmlWriter = new TSDMLValue("<init/>", ""); for (int i = 0; i < typedvals.Count; i++) { newXML.Append(sdmlWriter.getText(typedvals[i], 0, 2)); } newXML.Append("</initsection>"); return(newXML.ToString()); }
//======================================================================= /// <summary> /// Initialise the lists of properties with values from the init section /// SDML. And reload the tree with the values. /// </summary> /// <param name="initXML">The init section which is <code><initsection>...</initsection></code></param> //======================================================================= private Boolean InitFromInitSection(String initXML) { if (initXML.Length > 0) { typedvals.Clear(); TInitParser initPsr = new TInitParser(initXML); for (int i = 1; i <= initPsr.initCount(); i++) { String initText = initPsr.initText((uint)i); TSDMLValue sdmlinit = new TSDMLValue(initText, ""); typedvals.Add(sdmlinit); } } //if the component description is valid then use it. if (propertyList.Count > 0) { foreach (TTypedValue value in typedvals) //for every init section value { //find the property in the component description list int i = 0; TCompProperty prop = propertyList[i]; while ((prop != null) && (i < propertyList.Count)) { if (value.Name.ToLower() == prop.Name.ToLower()) { prop.InitValue.setValue(value); //set the property's value } i++; if (i < propertyList.Count) { prop = propertyList[i]; } } } } if (propertyList.Count > 0) { populateTreeModel(propertyList); //can populate with the full list } else { populateTreeModel(); //use init section only } return((initXML.Length > 0) || (propertyList.Count > 0)); }
//===================================================================== /// <summary> /// Scan through the cultivar list in mvCotton.xml and find any that have /// parameters and form them into an XML document based on TSDMLValue. /// </summary> /// <returns>The cultivar list as XML.</returns> //===================================================================== private String WriteCultivars() { //create new TSDMLValue for the cultivar details TDDMLValue CultivarValues = new TDDMLValue(CULTIVARTYPE, ""); CultivarValues.setElementCount(0); uint cultivarCount = 0; XmlDocument ModelDoc = new XmlDocument(); String ComponentType = Controller.ApsimData.Find(NodePath).Type; ModelDoc.LoadXml("<Model>" + Types.Instance.ModelContents(ComponentType) + "</Model>"); foreach (XmlNode Child in ModelDoc.DocumentElement.ChildNodes) { if (XmlHelper.Attribute(Child, "cultivar").ToLower() == "yes") { if (Child.ChildNodes.Count > 0) { XmlDocument CultivarDoc = new XmlDocument(); CultivarDoc.LoadXml(Child.OuterXml); CultivarValues.setElementCount(++cultivarCount); TTypedValue arrayitem = CultivarValues.item(cultivarCount); arrayitem.member("cultivar").setValue(CultivarDoc.DocumentElement.Name); //read root tag name foreach (XmlNode param in CultivarDoc.DocumentElement.ChildNodes) //for each child of cultivardoc { if (param.NodeType == XmlNodeType.Element) { TTypedValue field = arrayitem.member(param.Name); if (field != null) { if (param.Name == "FRUDD" || param.Name == "BLTME" | param.Name == "WT") { //add array node values String arrayField = param.InnerText; if (arrayField.Contains(" ")) { string[] VariableNameBits = arrayField.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); double[] values = MathUtility.StringsToDoubles(VariableNameBits); uint count = 0; foreach (double val in values) { field.setElementCount(++count); field.item(count).setValue(val); } } } else { double val = Convert.ToDouble(param.InnerText); //these are all doubles field.setValue(val); //copy child to new doc } } else { throw new Exception("Cannot set init value for " + param.Name + " in WriteCultivars()"); } } } } } } TSDMLValue writer = new TSDMLValue("<type/>", ""); return(writer.getText(CultivarValues, 0, 2)); }