Exemplo n.º 1
0
        public static void MakeSims(BaseController Controller)
        {
            System.Collections.Specialized.StringCollection PathsToConvert = Controller.SelectedPaths;
            List <String> SimsToConvert = new List <String>();

            foreach (String SimulationPath in PathsToConvert)
            {
                ApsimFile.ApsimFile.ExpandSimsToRun(Controller.ApsimData.Find(SimulationPath), ref SimsToConvert);
            }
            String UserMsg = "No simulations selected!";

            if (SimsToConvert.Count > 0)
            {
                if (SimsToConvert.Count == 1)
                {
                    UserMsg = "Created simulation file:";
                }
                else
                {
                    UserMsg = "Created simulation files:";
                }
                FolderBrowserDialog FolderChooser = new FolderBrowserDialog();
                FolderChooser.Description  = "Select the directory in which to save the .sim files";
                FolderChooser.SelectedPath = Directory.GetCurrentDirectory();
                if (FolderChooser.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        foreach (String SimulationPath in SimsToConvert)
                        {
                            try
                            {
                                Component    Simulation  = Controller.ApsimData.Find(SimulationPath);
                                String       SimFileName = FolderChooser.SelectedPath + Path.DirectorySeparatorChar + Simulation.Name + ".sim";
                                StreamWriter fp          = new StreamWriter(SimFileName);
                                ApsimToSim.GetSimDoc(Simulation, Configuration.getArchitecture()).Save(fp);
                                fp.Close();
                                UserMsg += "\n" + SimFileName;
                            }
                            catch (Exception err)
                            {
                                MessageBox.Show("Simulation: " + SimulationPath + ". " + err.Message, "Error generating .sim file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show("Unexpected Error while generating .sim files:\n " + err.Message, "Error generating .sim file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            MessageBox.Show(UserMsg, "Create .SIM", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Exemplo n.º 2
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);
         }
     }
 }
Exemplo n.º 3
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);
        }