Ejemplo n.º 1
0
        public static string simulationName(bool saveExtraInfoInFilename, string rootName, SortedDictionary <string, string> factorsList, int counter, int totalCount)
        {
            string result = rootName;

            if (saveExtraInfoInFilename)
            {
                result += ";" + FactorItem.ToKVString(factorsList);
            }
            else
            {
                //write a spacer to list sims in order eg: 01 or 001 or 0001 depending on count
                string sPad = "";
                double tot  = Math.Floor(Math.Log10(totalCount) + 1);
                double file = Math.Floor(Math.Log10(counter) + 1);
                for (int i = 0; i < (int)(tot - file); ++i)
                {
                    sPad += "0";
                }

                result += "_" + sPad + counter;
            }
            return(result);
        }
Ejemplo n.º 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);
        }