コード例 #1
0
 private void FindSimsAndConvert(ApsimFile.Component Apsim, string SimPath, bool dontWriteSimFiles)
 {
     // Iterate through all nested simulations and convert them to
     // .sim format if necessary.
     foreach (ApsimFile.Component Child in Apsim.ChildNodes)
     {
         if (Child.Type.ToLower() == "simulation" && Child.Enabled)
         {
             if (SimPath == "" || string.Equals(Child.FullPath, SimPath, StringComparison.CurrentCultureIgnoreCase))
             {
                 string SimFileName = ApsimToSim.WriteSimFile(Child, dontWriteSimFiles);
                 Console.Error.WriteLine("Written " + SimFileName);
             }
         }
         else if (Child.Type.ToLower() == "folder")
         {
             FindSimsAndConvert(Child, SimPath, dontWriteSimFiles);
         }
     }
 }
コード例 #2
0
        private static string CreateJobFromSimulation(Component Simulation, SortedDictionary <string, string> factorsList, bool titleIsSingleLine, string destFolder)
        {
            //add title line into simulation
            //find all components that are outputfiles... or use xml?
            List <Component> outputfiles = new List <Component>();

            AddOutputFilesToList(Simulation, outputfiles);
            foreach (Component comp in outputfiles)
            {
                // Ensure the output name is correct for this simulation
                XmlNode compNode = comp.ContentsAsXML;
                XmlNode fileNode = compNode.SelectSingleNode("//filename");
                if (fileNode != null)
                {
                    fileNode.InnerText = ComponentUtility.CalcFileName(comp);
                }
                else
                {
                    throw new Exception("Cant find an outputfile filename node!");
                }

                if (titleIsSingleLine)
                {
                    //Title node is now read only, so need to add this as a constant
                    //XmlNode titleNode = compNode.SelectSingleNode("//title");
                    //if (titleNode != null)
                    //    titleNode.InnerText = factorsList;
                    //else
                    //    throw new Exception("Cant find an outputfile title node!");
                    Component constantsComponent = null;
                    foreach (Component c in comp.ChildNodes)
                    {
                        if (c.Type == "variables")
                        {
                            constantsComponent = c;
                        }
                    }
                    if (constantsComponent == null)
                    {
                        throw new Exception("No variables in outputfile!");
                    }

                    XmlNode componentNode = constantsComponent.ContentsAsXML;
                    XmlNode constantsNode = componentNode.SelectSingleNode("//constants");
                    if (constantsNode == null)
                    {
                        constantsNode = componentNode.OwnerDocument.CreateElement("constants");
                        componentNode.AppendChild(constantsNode);
                    }
                    XmlNode factorNode = constantsNode.SelectSingleNode("//constant[@name='factors']");
                    if (factorNode == null)
                    {
                        factorNode = constantsNode.OwnerDocument.CreateElement("constant");
                        constantsNode.AppendChild(factorNode);
                    }
                    XmlHelper.SetAttribute(factorNode, "name", "factors");
                    factorNode.InnerText        = FactorItem.ToKVString(factorsList);
                    constantsComponent.Contents = componentNode.OuterXml;
                }
                else
                {
                    //Find the variables node (should be a child of the output file - it will find and use the first one
                    Component constantsComponent = null;
                    foreach (Component c in comp.ChildNodes)
                    {
                        if (c.Type == "variables")
                        {
                            constantsComponent = c;
                        }
                    }
                    if (constantsComponent == null)
                    {
                        throw new Exception("No variables in outputfile!");
                    }

                    XmlNode componentNode = constantsComponent.ContentsAsXML;
                    XmlNode constantsNode = componentNode.SelectSingleNode("//constants");
                    if (constantsNode == null)
                    {
                        constantsNode = componentNode.OwnerDocument.CreateElement("constants");
                        componentNode.AppendChild(constantsNode);
                    }
                    else
                    {
                        //clean out existing nodes - will remove existing constants
                        constantsNode.RemoveAll();
                    }

                    foreach (string factor in factorsList.Keys)
                    {
                        XmlNode factorNode = constantsNode.OwnerDocument.CreateElement("constant");
                        constantsNode.AppendChild(factorNode);
                        XmlHelper.SetAttribute(factorNode, "name", factor);
                        factorNode.InnerText = factorsList[factor];
                    }
                    constantsComponent.Contents = componentNode.OuterXml;
                }
                comp.Contents = compNode.OuterXml;
            }
            string currDirectory = Directory.GetCurrentDirectory();

            if (destFolder != "" && Directory.Exists(destFolder))
            {
                Directory.SetCurrentDirectory(destFolder);
            }

            string SimFileName = ApsimToSim.WriteSimFile(Simulation, Configuration.getArchitecture(), false);

            Directory.SetCurrentDirectory(currDirectory);

            return(SimFileName);
        }