Esempio n. 1
0
        public static ReportColumn Create(string descriptor, IClock clock, IStorageWriter storage, ILocator locator, IEvent events)
        {
            string columnName = StringUtilities.RemoveWordAfter(ref descriptor, "as");
            object to         = StringUtilities.RemoveWordAfter(ref descriptor, "to");
            object from       = StringUtilities.RemoveWordAfter(ref descriptor, "from");

            if (clock is IModel)
            {
                if (from != null)
                {
                    IVariable fromValue = Apsim.GetVariableObject(clock as IModel, from.ToString());
                    if (fromValue != null)
                    {
                        from = fromValue;
                    }
                }
                if (to != null)
                {
                    IVariable toValue = Apsim.GetVariableObject(clock as IModel, to.ToString());
                    if (toValue != null)
                    {
                        to = toValue;
                    }
                }
            }
            string aggregationFunction = StringUtilities.RemoveWordBefore(ref descriptor, "of");

            string variableName = descriptor;  // variable name is what is left over.

            // specify a column heading if alias was not specified.
            if (columnName == null)
            {
                // 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]*\\]");

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

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

                // change any curly brackets back to squares.
                // columnName = columnName.Replace("{", "[").Replace("}", "]");
            }

            if (aggregationFunction != null)
            {
                return(new ReportColumn(aggregationFunction, variableName, columnName, from, to, clock, storage, locator, events));
            }
            else
            {
                return(new ReportColumn(variableName, columnName, clock, storage, locator, events));
            }
        }
Esempio n. 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);
            }
        }
        internal FluentWriterBuilderElements(
            IStorageWriter <TDataModel> storage,
            List <object> identifiers)
        {
            Checker.NotNullArgument(storage, nameof(storage));
            Checker.NotNullArgument(identifiers, nameof(identifiers));

            _storage     = storage;
            _identifiers = identifiers;
        }
Esempio n. 4
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)
 {
     Values            = new List <object>();
     this.variableName = variableName.Trim();
     this.Name         = columnName;
     this.storage      = storage;
     this.locator      = locator;
     this.events       = events;
     this.clock        = clock;
 }
Esempio n. 5
0
 public MonitorService(
     ILogger <MonitorService> logger,
     IStorageWriter storage,
     IEnumerable <IMonitorPlugin> plugins
     )
 {
     _logger  = logger;
     _storage = storage;
     _plugins = plugins.ToList();
     _logger.LogInformation("New performance monitor service was created");
 }
Esempio n. 6
0
        public static ReportColumn Create(string descriptor, IClock clock, IStorageWriter storage, ILocator locator, IEvent events)
        {
            string columnName = RemoveWordAfter(ref descriptor, "as");
            object to         = RemoveWordAfter(ref descriptor, "to");
            object from       = RemoveWordAfter(ref descriptor, "from");

            if (clock is IModel)
            {
                if (from != null)
                {
                    IVariable fromValue = Apsim.GetVariableObject(clock as IModel, from.ToString());
                    if (fromValue != null)
                    {
                        from = fromValue;
                    }
                }
                if (to != null)
                {
                    IVariable toValue = Apsim.GetVariableObject(clock as IModel, to.ToString());
                    if (toValue != null)
                    {
                        to = toValue;
                    }
                }
            }
            string aggregationFunction = RemoveWordBefore(ref descriptor, "of");

            string variableName = descriptor;  // variable name is what is left over.

            // specify a column heading if alias was not specified.
            if (columnName == null)
            {
                columnName = variableName.Replace("[", string.Empty).Replace("]", string.Empty);
            }

            if (aggregationFunction != null)
            {
                return(new ReportColumn(aggregationFunction, variableName, columnName, from, to, clock, storage, locator, events));
            }
            else
            {
                return(new ReportColumn(variableName, columnName, clock, storage, locator, events));
            }
        }
Esempio n. 7
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) { }
 }
Esempio n. 8
0
        public static ReportColumn Create(string descriptor, IClock clock, IStorageWriter storage, ILocator locator, IEvent events)
        {
            string columnName          = RemoveWordAfter(ref descriptor, "as");
            string to                  = RemoveWordAfter(ref descriptor, "to");
            string from                = RemoveWordAfter(ref descriptor, "from");
            string aggregationFunction = RemoveWordBefore(ref descriptor, "of");

            string variableName = descriptor;  // variable name is what is left over.

            // specify a column heading if alias was not specified.
            if (columnName == null)
            {
                columnName = variableName.Replace("[", string.Empty).Replace("]", string.Empty);
            }

            if (aggregationFunction != null)
            {
                return(new ReportColumn(aggregationFunction, variableName, columnName, from, to, clock, storage, locator, events));
            }
            else
            {
                return(new ReportColumn(variableName, columnName, clock, storage, locator, events));
            }
        }
Esempio n. 9
0
        /// <summary>Constructor</summary>
        /// <param name="model">The model the user has selected to run</param>
        /// <param name="presenter">The explorer presenter.</param>
        /// <param name="multiProcess">Use the multi-process runner?</param>
        /// <param name="storage">A storage writer where all data should be stored</param>
        public RunCommand(IModel model, ExplorerPresenter presenter, bool multiProcess, IStorageWriter storage)
        {
            this.jobName           = model.Name;
            this.explorerPresenter = presenter;
            this.explorerPresenter.MainPresenter.AddStopHandler(OnStopSimulation);

            jobManager = Runner.ForSimulations(explorerPresenter.ApsimXFile, model, false);

            if (multiProcess)
            {
                jobRunner = new JobRunnerMultiProcess(storage);
            }
            else
            {
                jobRunner = new JobRunnerAsync();
            }
            jobRunner.JobCompleted     += OnJobCompleded;
            jobRunner.AllJobsCompleted += OnAllJobsCompleted;
        }
Esempio n. 10
0
 internal FluentWriterBuilderWithIdentifier(IStorageWriter <TDataModel> storage,
                                            List <object> identifiers) : base(storage, identifiers)
 {
 }
Esempio n. 11
0
 /// <summary>Constructor</summary>
 /// <param name="writer">The writer where all data should be stored</param>
 public JobRunnerMultiProcess(IStorageWriter writer)
 {
     storageWriter = writer;
 }
Esempio n. 12
0
 /// <summary>
 /// Create fluent API object for writing
 /// </summary>
 /// <param name="storage">Storage object</param>
 /// <returns>Instance of <see cref="FluentWriterBuilder{TDataModel}"/></returns>
 public static FluentWriterBuilder <TDataModel> AsFluentWriter <TDataModel>(
     this IStorageWriter <TDataModel> storage)
     where TDataModel : IIdentifiable
 {
     return(new FluentWriterBuilder <TDataModel>(storage));
 }
 public FluentWriterBuilder(IStorageWriter <TDataModel> storage) : base(storage,
                                                                        new List <object>())
 {
 }
Esempio n. 14
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);
            }
        }