コード例 #1
0
 public void LoadPreviousFile()
 {
     foreach (string PreviousFileName in Configuration.Instance.GetFrequentList())
     {
         if (File.Exists(PreviousFileName))
         {
             ApsimData.OpenFile(PreviousFileName);
             break; // TODO: might not be correct. Was : Exit For
         }
     }
 }
コード例 #2
0
    private void ConvertApsimToSim(string ApsimFileName, string SimName, bool dontWriteSimFiles)
    {
        //if the filename is not 'rooted' then assume that the user intends to use the current working directory as the root
        ApsimFileName = ApsimFileName.Replace("\"", "");
        if (!Path.IsPathRooted(ApsimFileName))
        {
            ApsimFileName = Path.Combine(Directory.GetCurrentDirectory(), ApsimFileName);
        }

        //double-check file actually exists before proceeding
        if (!File.Exists(ApsimFileName))
        {
            throw new Exception("Error: Specified APSIM File does not exist!\n\t" + ApsimFileName);
        }

        PlugIns.LoadAll();

        Directory.SetCurrentDirectory(Path.GetDirectoryName(ApsimFileName));

        // convert the specified simulations in the specified apsim file name
        // into a separate .sim file for each.
        ApsimFile.ApsimFile Apsim = new ApsimFile.ApsimFile();
        Apsim.OpenFile(ApsimFileName);

        if (Apsim.FactorComponent == null)
        {
            FindSimsAndConvert(Apsim.RootComponent, SimName, dontWriteSimFiles);
        }
        else
        {
            bool factorialActive = XmlHelper.Value(Apsim.FactorComponent.ContentsAsXML, "active") == "1";
            if (SimName.Contains("@factorial="))
            {
                foreach (string simFileName in Factor.CreateSimFiles(Apsim, new string[] { SimName }, Directory.GetCurrentDirectory()))
                {
                    Console.Error.WriteLine("Written " + simFileName);
                }
            }
            else if (factorialActive)
            {
                List <string> simulationPaths = new List <string>();
                ApsimFile.ApsimFile.ExpandSimsToRun(Apsim.RootComponent, ref simulationPaths);
                foreach (string simXmlPath in simulationPaths)
                {
                    FactorBuilder b             = new FactorBuilder(Apsim.FactorComponent);
                    Component     Simulation    = Apsim.Find(simXmlPath);
                    List <string> allFactorials = Factor.CalcFactorialList(Apsim, simXmlPath);
                    int           totalCount    = allFactorials.Count;
                    Parallel.For(1, totalCount + 1, instanceCount =>
                                 //for (int instanceCount = 0; instanceCount <= totalCount; instanceCount++)
                    {
                        string rootName = Simulation.Name;
                        if (b.SaveExtraInfoInFilename)
                        {
                            rootName += ";" + allFactorials[instanceCount - 1];
                        }
                        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(instanceCount) + 1);
                            for (int i = 0; i < (int)(tot - file); ++i)
                            {
                                sPad += "0";
                            }

                            rootName += "_" + sPad + instanceCount;
                        }

                        string fullSimPath = simXmlPath + "@factorial=" + allFactorials[instanceCount - 1];
                        if (dontWriteSimFiles)
                        {
                            Console.WriteLine("Written " + fullSimPath);
                        }
                        else
                        {
                            Factor.CreateSimFiles(Apsim, new string[] { fullSimPath }, Directory.GetCurrentDirectory());
                        }
                    });
                }
            }
            else
            {
                FindSimsAndConvert(Apsim.RootComponent, SimName, dontWriteSimFiles);
            }
        }
    }
コード例 #3
0
        // Add individual .apsim files to the job
        public void AddFiles(XmlNode job, List <string> FilesToRun, ProgressNotifier Notifier)
        {
            PlugIns.LoadAll();
            foreach (string FileName in FilesToRun)
            {
                if (Notifier != null)
                {
                    Notifier(0, "Reading " + FileName);
                }
                XmlDocument Doc = new XmlDocument();
                Doc.Load(FileName);

                XmlNode apsimFileNode = job.AppendChild(job.OwnerDocument.CreateElement("apsimfile"));
                XmlHelper.SetAttribute(apsimFileNode, "source", FileName);

                List <string> globalInputs = new List <string>();
                foreach (XmlNode pluginNode in Doc.DocumentElement.OwnerDocument.SelectNodes("//PlugIns/PlugIn[@enabled='yes']"))
                {
                    if (pluginNode.InnerText.IndexOf("%apsim%") < 0)
                    {
                        if (!File.Exists(pluginNode.InnerText))
                        {
                            throw new Exception("Plugin file '" + pluginNode.InnerText + "' doesnt exist - cant send it to the cluster.");
                        }
                        File.Copy(pluginNode.InnerText, Path.Combine(WorkingFolder, Path.GetFileName(pluginNode.InnerText)));
                        pluginNode.InnerText = Path.GetFileName(pluginNode.InnerText);
                        if (!globalInputs.Contains(pluginNode.InnerText))
                        {
                            globalInputs.Add(pluginNode.InnerText);
                        }
                    }
                }

                List <XmlNode> simulations = new List <XmlNode>();
                XmlHelper.FindAllRecursivelyByType(Doc.DocumentElement, "simulation", ref simulations);
                foreach (XmlNode simulation in simulations)
                {
                    if (XmlHelper.Attribute(simulation, "enabled") != "no")
                    {
                        List <string> simsToRun     = new List <string>();
                        XmlNode       factorialNode = XmlHelper.FindByType(Doc.DocumentElement, "factorial");
                        if (factorialNode == null)
                        {
                            simsToRun.Add(XmlHelper.FullPath(simulation));
                        }
                        else
                        {
                            ApsimFile F = new ApsimFile();
                            F.OpenFile(FileName);
                            simsToRun = Factor.CreateSimulationNames(F, new string [] { XmlHelper.FullPath(simulation) }, Notifier);
                        }
                        foreach (string simToRun in simsToRun)
                        {
                            XmlNode simulationNode = apsimFileNode.AppendChild(apsimFileNode.OwnerDocument.CreateElement("simulation"));
                            XmlHelper.SetAttribute(simulationNode, "name", simToRun);
                            XmlHelper.SetAttribute(simulationNode, "source", XmlHelper.Attribute(apsimFileNode, "source"));

                            foreach (var g in globalInputs)
                            {
                                XmlNode input = simulationNode.AppendChild(simulationNode.OwnerDocument.CreateElement("input"));
                                XmlHelper.SetAttribute(input, "source", g);
                                XmlHelper.SetAttribute(input, "name", g);
                            }

                            var filenames = new List <XmlNode>();
                            filenames = simulation.SelectNodes(".//filename").Cast <XmlNode>().ToList();
                            if (factorialNode != null)
                            {
                                // Send all input files to each job
                                foreach (XmlNode n in factorialNode.SelectNodes(".//filename"))
                                {
                                    if (n.InnerText.IndexOf(",") > 0)
                                    {
                                        foreach (string f in n.InnerText.Split(','))
                                        {
                                            XmlNode d = n.CloneNode(false);
                                            d.InnerText = f;
                                            filenames.Add(d);
                                        }
                                    }

                                    else
                                    {
                                        filenames.Add(n);
                                    }
                                }
                            }
                            foreach (XmlNode linkedNode in simulation.SelectNodes(".//*[string(@shortcut)]"))
                            {
                                XmlNode target = XmlHelper.Find(linkedNode,
                                                                linkedNode.Attributes["shortcut"].Value);
                                if (target != null)
                                {
                                    foreach (XmlNode targetFilename in target.SelectNodes(".//filename"))
                                    {
                                        filenames.Add(targetFilename);
                                    }
                                }
                            }

                            foreach (XmlNode node in filenames)
                            {
                                if (XmlHelper.Attribute(node, "output") != "yes" &&
                                    XmlHelper.Attribute(node.ParentNode, "enabled") != "no")
                                {
                                    string  src   = Configuration.RemoveMacros(node.InnerText);
                                    string  dest  = Path.GetFileName(src);
                                    XmlNode input = simulationNode.AppendChild(simulationNode.OwnerDocument.CreateElement("input"));

                                    if (!File.Exists(src) && !File.Exists(Path.Combine(WorkingFolder, dest)))
                                    {
                                        // When this is called by web service then can't assume src is relative to working
                                        // directory. Instead see if the file is relative to where the main file file.
                                        src = Path.Combine(Path.GetDirectoryName(FileName), Path.GetFileName(src));
                                        if (!File.Exists(src))
                                        {
                                            throw new Exception("File '" + src + "' doesnt exist - cant send it to the cluster.");
                                        }
                                    }

                                    XmlHelper.SetAttribute(input, "source", src);
                                    XmlHelper.SetAttribute(input, "name", dest);
                                    node.InnerText = dest;
                                    if (!File.Exists(Path.Combine(WorkingFolder, dest)))
                                    {
                                        File.Copy(src, Path.Combine(WorkingFolder, dest));
                                    }
                                }
                                else
                                {
                                    XmlNode output = simulationNode.AppendChild(simulationNode.OwnerDocument.CreateElement("output"));
                                    XmlHelper.SetAttribute(output, "name", node.InnerText);
                                }
                            }
                        }
                    }
                }
                Doc.Save(Path.Combine(WorkingFolder, Path.GetFileName(FileName)));
            }
        }