Пример #1
0
        /// <summary>Retrieve the current value and store it in our array of values.</summary>
        public virtual object GetValue()
        {
            object value = null;

            // If we're at the end of the capture window, apply the aggregation.
            if (this.aggregationFunction != null)
            {
                if (!this.inCaptureWindow)
                {
                    value = ApplyAggregation();
                }
            }
            else
            {
                value = locator.Get(variableName);

                if (value == null)
                {
                    Values.Add(null);
                }
                else
                {
                    if (value != null && value is IFunction)
                    {
                        value = (value as IFunction).Value();
                    }
                    else if (value.GetType().IsArray || value.GetType().IsClass)
                    {
                        try
                        {
                            value = ReflectionUtilities.Clone(value);
                        }
                        catch (Exception)
                        {
                            throw new Exception("Cannot report variable " + this.variableName +
                                                ". Variable is not of a reportable type. Perhaps " +
                                                " it is a PMF Function that needs a .Value appended to the name.");
                        }
                    }

                    if (!haveGotUnits)
                    {
                        IVariable var = locator.GetObject(variableName);
                        if (var != null)
                        {
                            Units = var.UnitsLabel;
                            if (Units != null && Units.StartsWith("(") && Units.EndsWith(")"))
                            {
                                Units = Units.Substring(1, Units.Length - 2);
                            }
                        }
                        haveGotUnits = true;
                    }

                    Values.Add(value);
                }
            }
            return(value);
        }
Пример #2
0
        /// <summary>
        /// Constructor for an aggregated column.
        /// </summary>
        /// <param name="aggregationFunction">The aggregation function</param>
        /// <param name="variableName">The name of the APSIM variable to retrieve</param>
        /// <param name="columnName">The column name to write to the output</param>
        /// <param name="from">The beginning of the capture window</param>
        /// <param name="to">The end of the capture window</param>
        /// <param name="clock">An instance of a clock model</param>
        /// <param name="storage">An instance of a storage service</param>
        /// <param name="locator">An instance of a locator service</param>
        /// <param name="events">An instance of an events service</param>
        /// <returns>The newly created ReportColumn</returns>
        private ReportColumn(string aggregationFunction, string variableName, string columnName, object from, object to,
                             IClock clock, IStorageWriter storage, ILocator locator, IEvent events)
        {
            Values = new List <object>();

            this.aggregationFunction = aggregationFunction;
            this.variableName        = variableName;
            this.Name            = columnName;
            this.inCaptureWindow = false;
            this.storage         = storage;
            this.locator         = locator;
            this.events          = events;
            this.clock           = clock;
            try
            {
                IVariable var = locator.GetObject(variableName);
                if (var != null)
                {
                    Units = var.UnitsLabel;
                    if (Units != null && Units.StartsWith("(") && Units.EndsWith(")"))
                    {
                        Units = Units.Substring(1, Units.Length - 2);
                    }
                }
            }
            catch (Exception) { }

            events.Subscribe("[Clock].StartOfDay", this.OnStartOfDay);
            events.Subscribe("[Clock].DoReportCalculations", this.OnEndOfDay);

            if (DateTime.TryParse(from.ToString(), out this.fromDate))
            {
                this.fromHasNoYear = !from.ToString().Contains(this.fromDate.Year.ToString());
            }
            else if (from is IVariable)
            {
                this.fromVariable = from as IVariable;
            }
            else
            {
                events.Subscribe(from.ToString(), this.OnBeginCapture);
            }

            if (DateTime.TryParse(to.ToString(), out this.toDate))
            {
                this.toHasNoYear = !to.ToString().Contains(this.toDate.Year.ToString());
            }
            else if (to is IVariable)
            {
                this.toVariable = to as IVariable;
            }
            else
            {
                events.Subscribe(to.ToString(), this.OnEndCapture);
            }
        }
Пример #3
0
 /// <summary>
 /// Constructor for a plain report variable.
 /// </summary>
 /// <param name="variableName">The name of the APSIM variable to retrieve</param>
 /// <param name="columnName">The column name to write to the output</param>
 /// <param name="clock">An instance of a clock model</param>
 /// <param name="storage">An instance of a storage service</param>
 /// <param name="locator">An instance of a locator service</param>
 /// <param name="events">An instance of an events service</param>
 private ReportColumn(string variableName, string columnName,
                      IClock clock, IStorageWriter storage, ILocator locator, IEvent events)
 {
     this.variableName = variableName.Trim();
     this.Name         = columnName;
     this.locator      = locator;
     this.clock        = clock;
     try
     {
         IVariable var = locator.GetObject(variableName);
         if (var != null)
         {
             Units = var.UnitsLabel;
             if (Units != null && Units.StartsWith("(") && Units.EndsWith(")"))
             {
                 Units = Units.Substring(1, Units.Length - 2);
             }
         }
     }
     catch (Exception) { }
 }
Пример #4
0
        /// <summary>
        /// Initialise the column instance.
        /// </summary>
        /// <param name="aggFunction">The aggregation function.</param>
        /// <param name="varName">The name of the variable to get from APSIM.</param>
        /// <param name="on">The collection event.</param>
        /// <param name="alias">The alias.</param>
        /// <param name="from">The from variable.</param>
        /// <param name="to">The to variable.</param>
        private void Initialise(string aggFunction, string varName, string on, string alias,
                                string from, string to)
        {
            aggregationFunction = aggFunction;
            variableName        = varName;
            fromString          = from;
            toString            = to;
            Name = alias;

            // specify a column heading if alias was not specified.
            if (string.IsNullOrEmpty(Name))
            {
                // Look for an array specification. The aim is to encode the starting
                // index of the array into the column name. e.g.
                // for a variableName of [2:4], columnName = [2]
                // for a variableName of [3:], columnName = [3]
                // for a variableName of [:5], columnNamne = [0]

                Regex regex = new Regex("\\[([0-9]+):*[0-9]*\\]");

                Name = regex.Replace(variableName.Replace("[:", "[1:"), "($1)");

                // strip off square brackets.
                Name = Name.Replace("[", string.Empty).Replace("]", string.Empty);
            }

            // Try and get units.
            try
            {
                IVariable var = locator.GetObject(variableName);
                if (var != null)
                {
                    Units = var.UnitsLabel;
                    if (Units != null && Units.StartsWith("(") && Units.EndsWith(")"))
                    {
                        Units = Units.Substring(1, Units.Length - 2);
                    }
                }
            }
            catch (Exception)
            {
            }

            if (string.IsNullOrEmpty(fromString))
            {
                inCaptureWindow = true;
            }
            else
            {
                // temporarly aggregated variable
                // subscribe to the capture event
                var collectionEventName = "[Clock].DoReportCalculations";
                if (!string.IsNullOrEmpty(on))
                {
                    collectionEventName = on;
                }
                events.Subscribe(collectionEventName, OnDoReportCalculations);

                // subscribe to the start of day event so that we can determine if we're in the capture window.
                events.Subscribe("[Clock].DoDailyInitialisation", OnStartOfDay);

                fromVariable = (clock as IModel).FindByPath(fromString);
                toVariable   = (clock as IModel).FindByPath(toString);
                if (fromVariable != null)
                {
                    // A from variable name  was specified.
                }
                else if (DateTime.TryParseExact(fromString, "d-MMM", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date) ||
                         DateTime.TryParse(fromString, out date))
                {
                    // The from date is a static, hardcoded date string. ie 1-Jan, 1/1/2012, etc.
                    fromVariable = new VariableObject(date);

                    // If the date string does not contain a year (ie 1-Jan), we ignore year and
                    fromHasNoYear = !fromString.Contains(date.Year.ToString());
                }
                else
                {
                    // Assume the string is an event name.
                    events.Subscribe(fromString, OnFromEvent);
                    inCaptureWindow = true;
                }

                if (toVariable != null)
                {
                    // A to variable name  was specified.
                }
                else if (DateTime.TryParseExact(toString, "d-MMM", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date) ||
                         DateTime.TryParse(toString, out date))
                {
                    // The from date is a static, hardcoded date string. ie 1-Jan, 1/1/2012, etc.
                    toVariable = new VariableObject(date);

                    // If the date string does not contain a year (ie 1-Jan), we ignore year and
                    toHasNoYear = !toString.Contains(date.Year.ToString());
                }
                else
                {
                    // Assume the string is an event name.
                    events.Subscribe(toString, OnToEvent);
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Constructor for an aggregated column.
        /// </summary>
        /// <param name="aggregationFunction">The aggregation function</param>
        /// <param name="variableName">The name of the APSIM variable to retrieve</param>
        /// <param name="columnName">The column name to write to the output</param>
        /// <param name="from">The beginning of the capture window</param>
        /// <param name="to">The end of the capture window</param>
        /// <param name="clock">An instance of a clock model</param>
        /// <param name="storage">An instance of a storage service</param>
        /// <param name="locator">An instance of a locator service</param>
        /// <param name="events">An instance of an events service</param>
        /// <returns>The newly created ReportColumn</returns>
        private ReportColumn(string aggregationFunction, string variableName, string columnName, object from, object to,
                             IClock clock, IStorageWriter storage, ILocator locator, IEvent events)
        {
            this.aggregationFunction = aggregationFunction;
            this.variableName        = variableName;
            this.Name    = columnName;
            this.locator = locator;
            this.clock   = clock;
            try
            {
                IVariable var = locator.GetObject(variableName);
                if (var != null)
                {
                    Units = var.UnitsLabel;
                    if (Units != null && Units.StartsWith("(") && Units.EndsWith(")"))
                    {
                        Units = Units.Substring(1, Units.Length - 2);
                    }
                }
            }
            catch (Exception)
            {
            }

            events.Subscribe("[Clock].DoReportCalculations", OnDoReportCalculations);
            events.Subscribe("[Clock].DoDailyInitialisation", OnStartOfDay);

            if (from == null || string.IsNullOrEmpty(from.ToString()))
            {
                throw new Exception("No 'from' clause was specified in temporal aggregation");
            }

            if (DateTime.TryParse(from.ToString(), out DateTime date))
            {
                // The from date is a static, hardcoded date string. ie 1-Jan, 1/1/2012, etc.
                this.fromVariable = new VariableObject(date);

                // If the date string does not contain a year (ie 1-Jan), we ignore year and
                this.fromHasNoYear = !from.ToString().Contains(date.Year.ToString());
            }
            else if (from is IVariable)
            {
                this.fromVariable = from as IVariable;
            }
            else
            {
                // Assume the string is an event name.
                events.Subscribe(from.ToString(), OnFromEvent);
                inCaptureWindow = true;
            }

            if (to == null || string.IsNullOrEmpty(to.ToString()))
            {
                throw new Exception("No 'to' clause was specified in temporal aggregation");
            }

            if (DateTime.TryParse(to.ToString(), out date))
            {
                // The from date is a static, hardcoded date string. ie 1-Jan, 1/1/2012, etc.
                this.toVariable = new VariableObject(date);

                // If the date string does not contain a year (ie 1-Jan), we ignore year and
                this.toHasNoYear = !to.ToString().Contains(date.Year.ToString());
            }
            else if (to is IVariable)
            {
                this.toVariable = to as IVariable;
            }
            else
            {
                // Assume the string is an event name.
                events.Subscribe(to.ToString(), OnToEvent);
            }
        }