Пример #1
0
        /// <summary>Perform the actual replacement.</summary>
        /// <param name="simulation">The simulation to perform the replacements on.</param>
        public void Replace(IModel simulation)
        {
            if (path == null)
            {
                throw new Exception("No path specified for property replacement.");
            }

            Apsim.Set(simulation, path, replacement);
        }
Пример #2
0
        public void SetTest()
        {
            Assert.AreEqual(this.simulation.Get("[Weather].Rain"), 0.0);
            this.simulation.Set("[Weather].Rain", 111.0);
            Assert.AreEqual(this.simulation.Get("[Weather].Rain"), 111.0);

            double[] thicknessBefore = (double[])Apsim.Get(simulation, "[Physical].Thickness");
            Assert.AreEqual(6, thicknessBefore.Length); // If APITest.xml is modified, this test will fail and must be updated.
            Apsim.Set(simulation, "[Physical].Thickness[1]", "20");
            double[] thicknessAfter = (double[])Apsim.Get(simulation, "[Physical].Thickness");

            Assert.AreEqual(thicknessBefore.Length, thicknessAfter.Length);
            Assert.AreEqual(20, thicknessAfter[0]);
        }
Пример #3
0
        /// <summary>Perform the actual replacement.</summary>
        /// <param name="simulation">The simulation to perform the replacements on.</param>
        public void Replace(IModel simulation)
        {
            if (path == null)
            {
                throw new Exception("No path specified for property replacement.");
            }

            Apsim.Set(simulation, path, replacement);

            // In a multi-paddock context, we want to attempt to
            // change the property value in all paddocks.
            foreach (Zone paddock in Apsim.ChildrenRecursively(simulation, typeof(Zone)))
            {
                IVariable variable = Apsim.GetVariableObject(paddock, path);
                if (variable != null)
                {
                    variable.Value = replacement;
                }
            }
        }
Пример #4
0
        private void OnDoManagement(object sender, EventArgs e)
        {
            DateTime operationDate;

            foreach (Operation operation in Schedule.Where(o => o.Enabled))
            {
                operationDate = DateUtilities.validateDateString(operation.Date, Clock.Today.Year);
                if (operationDate == Clock.Today)
                {
                    string st = operation.Action;

                    // If the action contains a comment anywhere, we ignore everything after (and including) the comment.
                    int commentPosition = st.IndexOf("//");
                    if (commentPosition >= 0)
                    {
                        st = st.Substring(0, commentPosition);
                    }

                    if (st.Contains("="))
                    {
                        string variableName = st;
                        string value        = StringUtilities.SplitOffAfterDelimiter(ref variableName, "=").Trim();
                        variableName = variableName.Trim();
                        Apsim.Set(this, variableName, value);
                    }
                    else if (st.Trim() != string.Empty)
                    {
                        string   argumentsString = StringUtilities.SplitOffBracketedValue(ref st, '(', ')');
                        string[] arguments       = argumentsString.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                        int posPeriod = st.LastIndexOf('.');
                        if (posPeriod == -1)
                        {
                            throw new ApsimXException(this, "Bad operations action found: " + operation.Action);
                        }
                        string modelName  = st.Substring(0, posPeriod);
                        string methodName = st.Substring(posPeriod + 1).Replace(";", "").Trim();

                        Model model = Apsim.Get(this, modelName) as Model;
                        if (model == null)
                        {
                            throw new ApsimXException(this, "Cannot find model: " + modelName);
                        }


                        MethodInfo[] methods = model.GetType().GetMethods();
                        if (methods == null)
                        {
                            throw new ApsimXException(this, "Cannot find method: " + methodName + " in model: " + modelName);
                        }

                        object[] parameterValues = null;
                        foreach (MethodInfo method in methods)
                        {
                            if (method.Name.Equals(methodName, StringComparison.CurrentCultureIgnoreCase))
                            {
                                parameterValues = GetArgumentsForMethod(arguments, method);

                                // invoke method.
                                if (parameterValues != null)
                                {
                                    try
                                    {
                                        method.Invoke(model, parameterValues);
                                    }
                                    catch (Exception err)
                                    {
                                        throw err.InnerException;
                                    }
                                    break;
                                }
                            }
                        }

                        if (parameterValues == null)
                        {
                            throw new ApsimXException(this, "Cannot find method: " + methodName + " in model: " + modelName);
                        }
                    }
                }
            }
        }