Esempio n. 1
0
    /// <summary>
    /// Writes a sim file for the specified component. Will throw on error.
    /// </summary>
    ///
    public static XmlDocument GetSimDoc(Component Child, Configuration.architecture arch)
    {
        // See if there is an overriding plugins component within scope of the Child passed in.
        // If so then tell PlugIns to load the plugins.
        Component PluginsOverride = ComponentUtility.FindComponentWithinScopeOf(Child, "PlugIns");

        if (PluginsOverride != null)
        {
            PlugIns.LoadAllFromComponent(PluginsOverride);
        }

        XmlDocument SimXML  = new XmlDocument();
        string      simText = WriteSimScript(Child, arch);

        SimXML.LoadXml(simText);
        SortSimContents(SimXML.DocumentElement);

        // Reinstate the original plugins if we overrode them at the start of this method.
        if (PluginsOverride != null)
        {
            PlugIns.LoadAll();
        }

        SimXML.InsertBefore(SimXML.CreateXmlDeclaration("1.0", "UTF-8", null), SimXML.DocumentElement);
        return(SimXML);
    }
Esempio n. 2
0
    private static string WriteSimFile(Component Child, string FolderName, Configuration.architecture arch, bool dontWriteSimFiles)
    {
        string SimFileName = FolderName + Path.DirectorySeparatorChar + Child.Name + ".sim";

        if (!dontWriteSimFiles)
        {
            StreamWriter fp = new StreamWriter(SimFileName);
            GetSimDoc(Child, arch).Save(fp);
            fp.Close();
        }
        return(Path.GetFullPath(SimFileName));
    }
Esempio n. 3
0
    private static string WriteSimScript(Component Child, Configuration.architecture arch)
    {
        // Write and return the .sim file contents for the specified
        // Child component.
        if (Child.Enabled)
        {
            XmlNode ApsimToSim = Types.Instance.ApsimToSim(Child.Type);
            if (ApsimToSim != null)
            {
                ApsimToSim = ApsimToSim.CloneNode(deep: true);
                if (ApsimToSim.FirstChild.Name == "component" && XmlHelper.Attribute(ApsimToSim.FirstChild, "class") == "")
                {
                    string dllName = Types.Instance.MetaData(Child.Type, "dll");
                    // Make sure we're using the correct path separators for our current platform
                    // before making use of GetFileNameWithoutExtension to extract the name
                    dllName = dllName.Replace('/', Path.DirectorySeparatorChar);
                    dllName = dllName.Replace('\\', Path.DirectorySeparatorChar);
                    dllName = Path.GetFileNameWithoutExtension(dllName);
                    string className = Types.Instance.ProxyClassName(Child.Type, dllName);
                    if (className.ToLower() != dllName.ToLower())
                    {
                        if (className == "")
                        {
                            className = dllName;
                        }
                        else if (dllName != "")
                        {
                            className = dllName + "." + className;
                        }
                    }
                    if (className != "" && className != null)
                    {
                        XmlHelper.SetAttribute(ApsimToSim.FirstChild, "class", className);
                    }
                }
                else if (ApsimToSim.FirstChild.Name == "system" && Child.Type == "area" && XmlHelper.Attribute(ApsimToSim.FirstChild, "class") == "")
                {
                    XmlHelper.SetAttribute(ApsimToSim.FirstChild, "class", "ProtocolManager");
                }
                string ApsimToSimContents = ApsimToSim.InnerXml;

                // Replace any occurrences of our macros with appropriate text.
                // e.g. [Soil.] is replaced by the appropriate soil value.
                //      [Model] is replaced by the contents of the [Model] node i.e. the ini contents.
                //      [Dll] is replaced by the name of the model dll.
                //      [Children] is replaced by the sim script for all children of this component.
                //      [InstanceName] is replaced by the instance name.
                ApsimToSimContents = ApsimToSimContents.Replace("[InstanceName]", Child.Name);
                ApsimToSimContents = ReplaceSoilMacros(ApsimToSimContents, Child);
                ApsimToSimContents = ReplaceModelMacro(ApsimToSimContents, Child);
                ApsimToSimContents = ReplaceDllMacro(ApsimToSimContents, Child);
                ApsimToSimContents = ReplaceDllExtMacro(ApsimToSimContents, Child, arch);
                ApsimToSimContents = ReplaceChildrenMacro(ApsimToSimContents, Child, arch);

                // Any other macros in the <ApsimToSim> will be removed by using the
                // APSIM macro language.
                XmlDocument ChildValues = new XmlDocument();
                ChildValues.LoadXml(Child.Contents);

                // Add in any child components that don't have anything in their <ApsimToSim>
                foreach (Component SubChild in Child.ChildNodes)
                {
                    if (SubChild.Enabled)
                    {
                        // This next if stmt is for SoilTemperature 1 and 2 which have a <ApsimToSim> for when they
                        // are not under a <soil>
                        if (Child.Type == "Soil")
                        {
                            XmlNode soilNode = SubChild.ContentsAsXML;
                            ChildValues.DocumentElement.AppendChild(ChildValues.ImportNode(soilNode, true));
                        }
                        else
                        {
                            RecursivelyAddChildContent(SubChild, ChildValues.DocumentElement);
                        }
                    }
                }
                Macro Macro = new Macro();
                return(Macro.Go(ChildValues.DocumentElement, XmlHelper.FormattedXML(ApsimToSimContents)));
            }
        }
        return("");
    }
Esempio n. 4
0
    private static string ReplaceChildrenMacro(string ApsimToSimContents, Component ApsimComponent, Configuration.architecture arch)
    {
        // Replace the [Children] macro with child sim script.

        int PosStartMacro = ApsimToSimContents.IndexOf("[Children");

        while (PosStartMacro != -1)
        {
            int PosEndMacro = ApsimToSimContents.IndexOf(']', PosStartMacro);
            if (PosEndMacro != -1)
            {
                string   ChildType  = "";
                string[] MacroWords = ApsimToSimContents.Substring(PosStartMacro + 1, PosEndMacro - PosStartMacro - 1)
                                      .Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (MacroWords.Length == 2)
                {
                    ChildType = MacroWords[1];
                }

                StringBuilder ChildSimContents = new StringBuilder();
                foreach (Component Child in ApsimComponent.ChildNodes)
                {
                    if (ChildType == "" || Child.Type.ToLower() == ChildType.ToLower())
                    {
                        ChildSimContents.Append(WriteSimScript(Child, arch));
                    }
                }
                ApsimToSimContents = ApsimToSimContents.Remove(PosStartMacro, PosEndMacro - PosStartMacro + 1);
                ApsimToSimContents = ApsimToSimContents.Insert(PosStartMacro, ChildSimContents.ToString());
            }
            PosStartMacro = ApsimToSimContents.IndexOf("[Children", PosStartMacro + 1);
        }
        if (ApsimToSimContents.Contains("[HasChildren"))
        {
            StringBuilder ChildSimContents = new StringBuilder();
            foreach (Component Child in ApsimComponent.ChildNodes)
            {
                ChildSimContents.Append(WriteSimScript(Child, arch));
            }
            if (ChildSimContents.Length == 0)
            {
                ApsimToSimContents = ApsimToSimContents.Replace("[HasChildren]", "");
            }
            else
            {
                ApsimToSimContents = ApsimToSimContents.Replace("[HasChildren]", "Yes");
            }
            ApsimToSimContents = ApsimToSimContents.Replace("[Children]", ChildSimContents.ToString());
        }
        return(ApsimToSimContents);
    }
Esempio n. 5
0
 public static string GetSimText(Component Child, Configuration.architecture arch)
 {
     return(GetSimDoc(Child, arch).InnerXml);
 }
Esempio n. 6
0
 public static string WriteSimFile(Component Child, Configuration.architecture arch, bool dontWriteSimFiles)
 {
     return(WriteSimFile(Child, Directory.GetCurrentDirectory(), arch, dontWriteSimFiles));
 }
Esempio n. 7
0
    private static string ReplaceDllExtMacro(string ApsimToSimContents, Component ApsimComponent, Configuration.architecture arch)
    {
        // Replace all occurrences of %dllext%
        if (arch == Configuration.architecture.unix) // ApsimFile.Configuration.amRunningOnUnix()
        {
            return(ApsimToSimContents.Replace("%dllext%", "so"));
        }

        return(ApsimToSimContents.Replace("%dllext%", "dll"));
    }