Пример #1
0
 internal FluentQueryBuilder(
     IStorageReader <TDataModel> storage,
     DataFilter <TDataModel> filter,
     DataLimiter <TDataModel> limiter,
     DataIncludes <TDataModel> includes)
     : base(storage, filter, limiter, includes)
 {
 }
 /// <summary>Attach the model and view to this presenter and populate the view.</summary>
 /// <param name="model">The data store model to work with.</param>
 /// <param name="view">Data store view to work with.</param>
 /// <param name="explorerPresenter">Parent explorer presenter.</param>
 public void Attach(object model, object view, ExplorerPresenter explorerPresenter)
 {
     dataStore = model as IStorageReader;
     this.Grid = view as ActivityLedgerGridView;
     this.explorerPresenter = explorerPresenter;
     this.Grid.ReadOnly     = true;
     PopulateGrid();
 }
Пример #3
0
        /// <summary>Gets the definitions to graph.</summary>
        /// <returns>A list of series definitions.</returns>
        /// <param name="storage">Storage service</param>
        /// <param name="simulationFilter">(Optional) Simulation name filter.</param>
        public IEnumerable <SeriesDefinition> GetDefinitionsToGraph(IStorageReader storage, List <string> simulationFilter = null)
        {
            EnsureAllAxesExist();

            return(FindAllChildren <IGraphable>()
                   .Where(g => g.Enabled)
                   .SelectMany(g => g.GetSeriesDefinitions(storage, simulationFilter)));
        }
Пример #4
0
 internal FluentQueryBuilderWithFilter(IStorageReader <TDataModel> storage,
                                       DataFilter <TDataModel> filter,
                                       DataLimiter <TDataModel> limiter,
                                       DataProjection <TDataModel, TGroup, TSelect> projection)
     : base(storage, filter, limiter, projection.GetDataIncludes())
 {
     _projection = projection;
 }
Пример #5
0
 /// <summary>Called by the graph presenter to get a list of all actual series to put on the graph.</summary>
 /// <param name="definitions">A list of definitions to add to.</param>
 /// <param name="storage">Storage service</param>
 public void GetSeriesToPutOnGraph(IStorageReader storage, List <SeriesDefinition> definitions)
 {
     if (definitions.Count > 0)
     {
         data       = definitions[0].data;
         xFieldName = definitions[0].xFieldName;
     }
 }
Пример #6
0
        /// <summary>Called by the graph presenter to get a list of all actual series to put on the graph.</summary>
        /// <param name="definitions">A list of definitions to add to.</param>
        /// <param name="storage">Storage service</param>
        /// <param name="simulationsFilter">Unused simulation names filter.</param>
        public void GetSeriesToPutOnGraph(IStorageReader storage, List <SeriesDefinition> definitions, List <string> simulationsFilter = null)
        {
            stats.Clear();
            equationColours.Clear();

            int checkpointNumber = 0;

            foreach (var checkpointName in storage.CheckpointNames)
            {
                // Get all x/y data
                List <double> x = new List <double>();
                List <double> y = new List <double>();
                foreach (SeriesDefinition definition in definitions)
                {
                    if (definition.CheckpointName == checkpointName)
                    {
                        if (definition.X is double[] && definition.Y is double[])
                        {
                            x.AddRange(definition.X as IEnumerable <double>);
                            y.AddRange(definition.Y as IEnumerable <double>);
                        }
                    }
                }

                if (ForEachSeries)
                {
                    // Display a regression line for each series.
                    int numDefinitions = definitions.Count;
                    for (int i = 0; i < numDefinitions; i++)
                    {
                        if (definitions[i].X is double[] && definitions[i].Y is double[])
                        {
                            PutRegressionLineOnGraph(definitions, definitions[i].X, definitions[i].Y, definitions[i].Colour, null);
                            equationColours.Add(definitions[i].Colour);
                        }
                    }
                }
                else
                {
                    var regresionLineName = "Regression line";
                    if (checkpointName != "Current")
                    {
                        regresionLineName = "Regression line (" + checkpointName + ")";
                    }

                    // Display a single regression line for all data.
                    PutRegressionLineOnGraph(definitions, x, y, ColourUtilities.ChooseColour(checkpointNumber), regresionLineName);
                    equationColours.Add(ColourUtilities.ChooseColour(checkpointNumber));
                }

                if (showOneToOne)
                {
                    Put1To1LineOnGraph(definitions, x, y);
                }

                checkpointNumber++;
            }
        }
Пример #7
0
        private void CreatePageOfGraphs(string sim, Graph[] graphs)
        {
            if (!panel.Cache.ContainsKey(sim))
            {
                panel.Cache.Add(sim, new Dictionary <int, List <SeriesDefinition> >());
            }

            IStorageReader storage   = GetStorage();
            GraphPage      graphPage = new GraphPage();

            // Configure graphs by applying user overrides.
            for (int i = 0; i < graphs.Length; i++)
            {
                if (graphs[i] != null && graphs[i].Enabled)
                {
                    graphPage.Graphs.Add(ConfigureGraph(graphs[i], sim));
                }

                if (cts.Token.IsCancellationRequested)
                {
                    return;
                }
            }

            // If any sims in this tab are missing from the cache, then populate
            // the cache via the GraphPage instance.
            if (panel.Cache[sim].Count != graphs.Length)
            {
                // Read data from storage.
                IReadOnlyList <GraphPage.GraphDefinitionMap> definitions = graphPage.GetAllSeriesDefinitions(panel, storage, new List <string>()
                {
                    sim
                }).ToList();

                // Now populate the cache for this simulation. The definitions
                // should - in theory - be the same length as the graphs array.
                for (int i = 0; i < graphs.Length; i++)
                {
                    GraphPage.GraphDefinitionMap definition = definitions[i];
                    panel.Cache[sim][i] = definition.SeriesDefinitions;
                    if (cts.Token.IsCancellationRequested)
                    {
                        return;
                    }
                }
            }

            // Finally, add the graphs to the tab.
            GraphTab tab = new GraphTab(sim, this.presenter);

            for (int i = 0; i < graphPage.Graphs.Count; i++)
            {
                tab.AddGraph(graphPage.Graphs[i], panel.Cache[sim][i]);
            }

            this.graphs.Add(tab);
            view.AddTab(tab, panel.NumCols);
        }
Пример #8
0
        /// <summary>Called by the graph presenter to get a list of all actual series to put on the graph.</summary>
        /// <param name="definitions">A list of definitions to add to.</param>
        /// <param name="reader">A storage reader.</param>
        /// <param name="simulationFilter"></param>
        public void GetSeriesToPutOnGraph(IStorageReader reader, List<SeriesDefinition> definitions, List<string> simulationFilter = null)
        {
            List<SeriesDefinition> seriesDefinitions = new List<SeriesDefinition>();

            // If this series doesn't have a table name then it must be getting its data from other models.
            if (TableName == null)
            {
                seriesDefinitions.Add(new SeriesDefinition(this));
                seriesDefinitions[0].ReadData(reader, simulationDescriptions);
            }
            else
            {
                // TableName exists so get the vary by fields and the simulation descriptions.
                var varyByFieldNames = GetVaryByFieldNames();
                simulationDescriptions = FindSimulationDescriptions();
                if (simulationFilter == null)
                    simulationFilter = simulationDescriptions.Select(d => d.Name).Distinct().ToList();

                var whereClauseForInScopeData = CreateInScopeWhereClause(reader, simulationFilter);

                if (varyByFieldNames.Count == 0 || varyByFieldNames.Contains("Graph series"))
                {
                    // No vary by fields. Just plot the whole table in a single
                    // series with data that is in scope.
                    seriesDefinitions = new List<SeriesDefinition>() { new SeriesDefinition(this, whereClauseForInScopeData, Filter) };
                }
                else
                {
                    // There are one or more vary by fields. Create series definitions
                    // for each combination of vary by fields.
                    seriesDefinitions = CreateDefinitionsUsingVaryBy(varyByFieldNames, simulationDescriptions, whereClauseForInScopeData);
                }

                // If we don't have any definitions then see if the vary by fields
                // refer to string fields in the database table.
                if (seriesDefinitions.Count == 0)
                    seriesDefinitions = CreateDefinitionsFromFieldInTable(reader, varyByFieldNames, whereClauseForInScopeData);

                // Paint all definitions. 
                var painter = GetSeriesPainter();
                foreach (var seriesDefinition in seriesDefinitions)
                    painter.Paint(seriesDefinition);

                // Tell each series definition to read its data.
                foreach (var seriesDefinition in seriesDefinitions)
                    seriesDefinition.ReadData(reader, simulationDescriptions);

                // Remove series that have no data.
                seriesDefinitions.RemoveAll(d => !MathUtilities.ValuesInArray(d.X) || !MathUtilities.ValuesInArray(d.Y));
            }

            // We might have child models that want to add to our series definitions e.g. regression.
            foreach (IGraphable series in Apsim.Children(this, typeof(IGraphable)))
                series.GetSeriesToPutOnGraph(reader, seriesDefinitions);

            definitions.AddRange(seriesDefinitions);
        }
Пример #9
0
 internal FluentQueryBuilderWithProjection(IStorageReader <TDataModel> storage,
                                           DataFilter <TDataModel> filter,
                                           DataLimiter <TDataModel> limiter,
                                           DataIncludes <TDataModel> includes,
                                           Expression <Func <TDataModel, TSelect> > select)
     : base(storage, filter, limiter, includes)
 {
     _projection = new DataProjection <TDataModel, TSelect>(includes._includes, select);
 }
Пример #10
0
        /// <summary>
        /// Create a data view from the specified table and filter.
        /// </summary>
        /// <param name="simulationZones">The list of simulation / zone pairs.</param>
        /// <param name="storage">Storage service</param>
        private DataTable GetBaseData(IStorageReader storage, List <SimulationZone> simulationZones)
        {
            // Get a list of all simulation names in all simulationZones.
            List <string> simulationNames = new List <string>();

            simulationZones.ForEach(sim => simulationNames.AddRange(sim.simulationNames));

            string filter = null;

            foreach (string simulationName in simulationNames.Distinct())
            {
                if (filter != null)
                {
                    filter += ",";
                }
                filter += "'" + simulationName + "'";
            }
            filter = "SimulationName in (" + filter + ")";
            if (Filter != string.Empty)
            {
                filter = AddToFilter(filter, Filter);
            }

            List <string> fieldNames = new List <string>();

            if (storage.ColumnNames(TableName).Contains("Zone"))
            {
                fieldNames.Add("Zone");
            }
            if (XFieldName != null && !XFieldName.Equals("SimulationName"))
            {
                fieldNames.Add(XFieldName);
            }
            if (YFieldName != null && !fieldNames.Contains(YFieldName))
            {
                fieldNames.Add(YFieldName);
            }
            if (X2FieldName != null && !fieldNames.Contains(X2FieldName))
            {
                fieldNames.Add(X2FieldName);
            }
            if (Y2FieldName != null && !fieldNames.Contains(Y2FieldName))
            {
                fieldNames.Add(Y2FieldName);
            }

            // Add in column names from annotation series.
            foreach (EventNamesOnGraph annotation in Apsim.Children(this, typeof(EventNamesOnGraph)))
            {
                fieldNames.Add(annotation.ColumnName);
            }

            return(storage.GetData(tableName: TableName, fieldNames: fieldNames, filter: filter));
        }
        internal ProjectionRootBuilder(IStorageReader <TDataModel> storage,
                                       DataFilter <TDataModel> filter,
                                       DataLimiter <TDataModel> limiter,
                                       DataIncludes <TDataModel> includes,
                                       Expression <Func <TDataModel, TSelect> > select)
            : base(storage, filter, limiter, includes)
        {
            Checker.NotNullArgument(select, nameof(select));

            _select = select;
        }
Пример #12
0
        /// <summary>Create a links object</summary>
        private void CreateLinks()
        {
            List <object>  services = new List <object>();
            IStorageReader storage  = Apsim.Find(this, typeof(IStorageReader)) as IStorageReader;

            if (storage != null)
            {
                services.Add(storage);
            }
            services.Add(this);
            links = new Links(services);
        }
Пример #13
0
        /// <summary>Called by the graph presenter to get a list of all actual series to put on the graph.</summary>
        /// <param name="storage">Storage service</param>
        /// <param name="simulationFilter">(Optional) simulation name filter.</param>
        public IEnumerable <SeriesDefinition> GetSeriesDefinitions(IStorageReader storage, List <string> simulationFilter = null)
        {
            Series seriesAncestor = FindAncestor <Series>();

            if (seriesAncestor == null)
            {
                throw new Exception("EventNamesOnGraph model must be a descendant of a series");
            }
            IEnumerable <SeriesDefinition> definitions = seriesAncestor.GetSeriesDefinitions(storage, simulationFilter);

            return(GetSeriesToPutOnGraph(storage, definitions, simulationFilter));
        }
Пример #14
0
        /// <summary>
        /// The main run method called to fill tables in the specified DataStore.
        /// </summary>
        /// <param name="dataStore">The DataStore to work with</param>
        public void Run(IStorageReader dataStore)
        {
            dataStore.DeleteTable(this.Name);

            DataTable simulationData = dataStore.GetData("*", this.TableName);

            if (simulationData != null)
            {
                // Add all the necessary columns to our data table.
                DataTable probabilityData = new DataTable();
                probabilityData.Columns.Add("Probability", typeof(double));
                foreach (DataColumn column in simulationData.Columns)
                {
                    if (column.DataType == typeof(double))
                    {
                        probabilityData.Columns.Add(column.ColumnName, typeof(double));
                    }
                }

                string[] simulationNames = dataStore.SimulationNames;

                DataView view = new DataView(simulationData);
                foreach (string simulationName in simulationNames)
                {
                    view.RowFilter = "SimulationName = '" + simulationName + "'";

                    int startRow = probabilityData.Rows.Count;

                    // Add in a simulation column.
                    string[] simulationNameColumnValues = StringUtilities.CreateStringArray(simulationName, view.Count);
                    DataTableUtilities.AddColumn(probabilityData, "SimulationName", simulationNameColumnValues, startRow, simulationNameColumnValues.Length);

                    // Add in the probability column
                    double[] probabilityValues = MathUtilities.ProbabilityDistribution(view.Count, this.Exceedence);
                    DataTableUtilities.AddColumn(probabilityData, "Probability", probabilityValues, startRow, view.Count);

                    // Add in all other numeric columns.
                    foreach (DataColumn column in simulationData.Columns)
                    {
                        if (column.DataType == typeof(double))
                        {
                            double[] values = DataTableUtilities.GetColumnAsDoubles(view, column.ColumnName);
                            Array.Sort <double>(values);
                            DataTableUtilities.AddColumn(probabilityData, column.ColumnName, values, startRow, values.Length);
                        }
                    }
                }

                // Write the stats data to the DataStore
                probabilityData.TableName = this.Name;
                dataStore.WriteTableRaw(probabilityData);
            }
        }
Пример #15
0
        /// <summary>
        /// Gets a list of simulation names. One tab of graphs will be generated for each simulation.
        /// </summary>
        /// <param name="storage">Provides access to the datastore.</param>
        /// <param name="panel">Provides access to the graph panel and the simulations tree.</param>
        public string[] GetSimulationNames(IStorageReader reader, GraphPanel panel)
        {
            // Get a list of all descendants of the panel's parent which are runnable
            // (simulations, experiments, etc).
            List <ISimulationDescriptionGenerator> runnables = Apsim.ChildrenRecursively(panel.Parent, typeof(ISimulationDescriptionGenerator)).Cast <ISimulationDescriptionGenerator>().ToList();

            // Remove all simulations which are children of an experiment.
            runnables.RemoveAll(r => r is Simulation && (r as Simulation).Parent is Experiment);

            // Return the names of all simulations generated by these runnables.
            return(runnables.SelectMany(r => r.GenerateSimulationDescriptions().Select(d => d.Name)).ToArray());
        }
Пример #16
0
        /// <summary>Gets the definitions to graph.</summary>
        /// <returns>A list of series definitions.</returns>
        /// <param name="storage">Storage service</param>
        public List <SeriesDefinition> GetDefinitionsToGraph(IStorageReader storage)
        {
            EnsureAllAxesExist();

            List <SeriesDefinition> definitions = new List <SeriesDefinition>();

            foreach (IGraphable series in Apsim.Children(this, typeof(IGraphable)))
            {
                series.GetSeriesToPutOnGraph(storage, definitions);
            }

            return(definitions);
        }
Пример #17
0
 /// <summary>
 /// Write a single sumary file for all simulations.
 /// </summary>
 /// <param name="storage">The storage where the summary data is stored</param>
 /// <param name="fileName">The file name to write</param>
 public static void WriteSummaryToTextFiles(IStorageReader storage, string fileName)
 {
     using (StreamWriter report = new StreamWriter(fileName))
     {
         foreach (string simulationName in storage.SimulationNames)
         {
             Summary.WriteReport(storage, simulationName, report, null, outtype: Summary.OutputType.html);
             report.WriteLine();
             report.WriteLine();
             report.WriteLine("############################################################################");
         }
     }
 }
Пример #18
0
        /// <summary>Gets the definitions to graph.</summary>
        /// <returns>A list of series definitions.</returns>
        /// <param name="storage">Storage service</param>
        /// <param name="simulationFilter">(Optional) Simulation name filter.</param>
        public List <SeriesDefinition> GetDefinitionsToGraph(IStorageReader storage, List <string> simulationFilter = null)
        {
            EnsureAllAxesExist();

            List <SeriesDefinition> definitions = new List <SeriesDefinition>();

            foreach (IGraphable series in Apsim.Children(this, typeof(IGraphable)).Where(g => g.Enabled))
            {
                series.GetSeriesToPutOnGraph(storage, definitions, simulationFilter);
            }

            return(definitions);
        }
Пример #19
0
        /// <summary>
        /// Create a data view from the specified table and filter.
        /// </summary>
        /// <param name="factors">The list of simulation / zone pairs.</param>
        /// <param name="storage">Storage service</param>
        private DataTable GetBaseData(IStorageReader storage, List <ISimulationGeneratorFactors> factors)
        {
            List <string> fieldNames = new List <string>();

            foreach (ISimulationGeneratorFactors factor in factors)
            {
                fieldNames.Add(factor.ColumnName);
            }
            if (XFieldName != null)
            {
                fieldNames.Add(XFieldName);
            }
            if (YFieldName != null)
            {
                fieldNames.Add(YFieldName);
            }
            if (YFieldName != null)
            {
                if (storage.ColumnNames(TableName).Contains(YFieldName + "Error"))
                {
                    fieldNames.Add(YFieldName + "Error");
                }
            }
            if (X2FieldName != null)
            {
                fieldNames.Add(X2FieldName);
            }
            if (Y2FieldName != null)
            {
                fieldNames.Add(Y2FieldName);
            }

            // Add in column names from annotation series.
            foreach (EventNamesOnGraph annotation in Apsim.Children(this, typeof(EventNamesOnGraph)))
            {
                fieldNames.Add(annotation.ColumnName);
            }

            string filterToUse;

            if (Filter == null || Filter == string.Empty)
            {
                filterToUse = CreateRowFilter(factors);
            }
            else
            {
                filterToUse = Filter + " AND (" + CreateRowFilter(factors) + ")";
            }

            return(storage.GetData(tableName: TableName, checkpointName: Checkpoint, fieldNames: fieldNames.Distinct(), filter: filterToUse));
        }
Пример #20
0
        private void CreatePageOfGraphs(string sim, Graph[] graphs)
        {
            IStorageReader storage = GetStorage();
            GraphTab tab = new GraphTab(sim, this.presenter);
            for (int i = 0; i < graphs.Length; i++)
            {
                Graph graph = ReflectionUtilities.Clone(graphs[i]) as Graph;
                graph.Parent = panel;
                graph.ParentAllDescendants();

                if (panel.LegendOutsideGraph)
                    graph.LegendOutsideGraph = true;

                if (panel.LegendOrientation != GraphPanel.LegendOrientationType.Default)
                    graph.LegendOrientation = (Graph.LegendOrientationType)Enum.Parse(typeof(Graph.LegendOrientationType), panel.LegendOrientation.ToString());

                if (graph != null && graph.Enabled)
                {
                    // Apply transformation to graph.
                    panel.Script.TransformGraph(graph, sim);

                    if (panel.LegendPosition != GraphPanel.LegendPositionType.Default)
                        graph.LegendPosition = (Graph.LegendPositionType)Enum.Parse(typeof(Graph.LegendPositionType), panel.LegendPosition.ToString());

                    // Create and fill cache entry if it doesn't exist.
                    if (!panel.Cache.ContainsKey(sim) || panel.Cache[sim].Count <= i)
                    {
                        try
                        {
                            int x = storage.GetSimulationID(sim);
                        }
                        catch (KeyNotFoundException)
                        {
                            throw new Exception($"Illegal simulation name: '{sim}'. Try running the simulation, and if that doesn't fix it, there is a problem with your config script.");
                        }
                        List<SeriesDefinition> definitions = graph.GetDefinitionsToGraph(storage, new List<string>() { sim }).ToList();
                        if (!panel.Cache.ContainsKey(sim))
                            panel.Cache.Add(sim, new Dictionary<int, List<SeriesDefinition>>());

                        panel.Cache[sim][i] = definitions;
                    }
                    tab.AddGraph(graph, panel.Cache[sim][i]);
                }

                if (processingThread.CancellationPending)
                    return;
            }

            this.graphs.Add(tab);
            view.AddTab(tab, panel.NumCols);
        }
Пример #21
0
        /// <summary>
        /// Create an SQL WHERE clause for rows that are in scope.
        /// </summary>
        /// <param name="reader">The reader to read from.</param>
        /// <param name="simulationFilter">The names of simulatiosn that are in scope.</param>
        private IEnumerable <string> CreateInScopeWhereClause(IStorageReader reader, List <string> simulationFilter)
        {
            var fieldsThatExist = reader.ColumnNames(TableName);

            if (fieldsThatExist.Contains("SimulationID") || fieldsThatExist.Contains("SimulationName"))
            {
                // Extract all the simulation names from all descriptions.
                return(simulationFilter.Distinct());
            }
            else
            {
                return(null);
            }
        }
        internal ProjectionInnerBuilder(IStorageReader <TRootDataModel> storage,
                                        DataFilter <TRootDataModel> filter,
                                        DataLimiter <TRootDataModel> limiter,
                                        DataIncludes <TRootDataModel> includes,
                                        Expression <Func <TRootDataModel, TGroup> > group,
                                        Expression <Func <IGrouping <TGroup, TRootDataModel>, TSelect> > select)
            : base(storage, filter, limiter, includes)
        {
            Checker.NotNullArgument(group, nameof(group));
            Checker.NotNullArgument(select, nameof(select));

            _group  = group;
            _select = select;
        }
Пример #23
0
        /// <summary>Called by the graph presenter to get a list of all actual series to put on the graph.</summary>
        /// <param name="storage">Storage service</param>
        /// <param name="simDescriptions">A list of simulation descriptions that are in scope.</param>
        /// <param name="simulationFilter">(Optional) simulation name filter.</param>
        public IEnumerable <SeriesDefinition> CreateSeriesDefinitions(IStorageReader storage,
                                                                      List <SimulationDescription> simDescriptions,
                                                                      List <string> simulationFilter = null)
        {
            Series seriesAncestor = FindAncestor <Series>();

            if (seriesAncestor == null)
            {
                throw new Exception("ShadedBarsOnGraph model must be a descendant of a series");
            }
            IEnumerable <SeriesDefinition> definitions = seriesAncestor.CreateSeriesDefinitions(storage, simDescriptions, simulationFilter);

            return(GetSeriesToPutOnGraph(storage, definitions, simulationFilter).ToList());
        }
Пример #24
0
        /// <summary>
        /// Main run method for performing our calculations and storing data.
        /// </summary>
        public void Run(IStorageReader dataStore)
        {
            string fullFileName = FullFileName;

            if (fullFileName != null)
            {
                Simulations simulations = Apsim.Parent(this, typeof(Simulations)) as Simulations;

                dataStore.DeleteTable(Name);
                DataTable data = GetTable();
                data.TableName = this.Name;
                dataStore.WriteTableRaw(data);
            }
        }
Пример #25
0
 /// <summary>Create a text report from tables in this data store.</summary>
 /// <param name="storage">The data store.</param>
 /// <param name="fileName">Name of the file.</param>
 public static void WriteAllTables(IStorageReader storage, string fileName)
 {
     // Write out each table for this simulation.
     foreach (string tableName in storage.TableNames)
     {
         DataTable data = storage.GetData(tableName);
         if (data != null && data.Rows.Count > 0)
         {
             StreamWriter report = new StreamWriter(Path.ChangeExtension(fileName, "." + tableName + ".csv"));
             DataTableUtilities.DataTableToText(data, 0, ",", true, report);
             report.Close();
         }
     }
 }
Пример #26
0
        private void OnCLEMValidate(object sender, EventArgs e)
        {
            // validation is performed here
            // this event fires after Activity and Resource validation so that resources are available to check in the validation.
            // commencing is too early as Summary has not been created for reporting.
            // some values assigned in commencing will not be checked before processing, but will be caught here
            // each ZoneCLEM and Market will call this validation for all children
            // CLEM components above ZoneCLEM (e.g. RandomNumberGenerator) needs to validate itself
            if (!Validate(this, "", this, Summary))
            {
                string error = "@i:Invalid parameters in model";

                // find IStorageReader of simulation
                IModel         parentSimulation = FindAncestor <Simulation>();
                IStorageReader ds = DataStore.Reader;
                if (ds.GetData(simulationName: parentSimulation.Name, tableName: "_Messages") != null)
                {
                    DataRow[] dataRows = ds.GetData(simulationName: parentSimulation.Name, tableName: "_Messages").Select().OrderBy(a => a[7].ToString()).ToArray();
                    // all all current errors and validation problems to error string.
                    foreach (DataRow dr in dataRows)
                    {
                        error += "\n" + dr[6].ToString();
                    }
                }
                throw new ApsimXException(this, error);
            }

            if (Clock.StartDate.Year > 1) // avoid checking if clock not set.
            {
                if ((int)EcologicalIndicatorsCalculationMonth >= Clock.StartDate.Month)
                {
                    // go back from start month in intervals until
                    DateTime trackDate = new DateTime(Clock.StartDate.Year, (int)EcologicalIndicatorsCalculationMonth, Clock.StartDate.Day);
                    while (trackDate.AddMonths(-EcologicalIndicatorsCalculationInterval) >= Clock.Today)
                    {
                        trackDate = trackDate.AddMonths(-EcologicalIndicatorsCalculationInterval);
                    }
                    EcologicalIndicatorsNextDueDate = trackDate;
                }
                else
                {
                    EcologicalIndicatorsNextDueDate = new DateTime(Clock.StartDate.Year, (int)EcologicalIndicatorsCalculationMonth, Clock.StartDate.Day);
                    while (Clock.StartDate > EcologicalIndicatorsNextDueDate)
                    {
                        EcologicalIndicatorsNextDueDate = EcologicalIndicatorsNextDueDate.AddMonths(EcologicalIndicatorsCalculationInterval);
                    }
                }
            }
        }
Пример #27
0
        /// <summary>A list of all descriptor names that can be listed as 'vary by' in markers/line types etc.</summary>
        public IEnumerable<string> GetDescriptorNames(IStorageReader reader)
        {
            var names = new List<string>();
            foreach (var simulationDescription in FindSimulationDescriptions())
                names.AddRange(simulationDescription.Descriptors.Select(d => d.Name));
            names.Add("Graph series");

            // Add all string and integer fields to descriptor names.
            foreach (var column in reader.GetColumns(TableName))
                if (column.Item2 == typeof(string) || column.Item2 == typeof(int))
                    if (column.Item1 != "CheckpointID" && column.Item1 != "SimulationID")
                        names.Add(column.Item1);

            return names.Distinct();
        }
Пример #28
0
        /// <summary>Gets the definitions to graph.</summary>
        /// <returns>A list of series definitions.</returns>
        /// <param name="storage">Storage service</param>
        /// <param name="simulationFilter">(Optional) Simulation name filter.</param>
        public IEnumerable <SeriesDefinition> GetDefinitionsToGraph(IStorageReader storage, List <string> simulationFilter = null)
        {
            EnsureAllAxesExist();

            var series      = FindAllChildren <Series>().Where(g => g.Enabled);
            var definitions = new List <SeriesDefinition>();

            foreach (var s in series)
            {
                var seriesDefinitions = s.CreateSeriesDefinitions(storage, SimulationDescriptions, simulationFilter);
                definitions.AddRange(seriesDefinitions);
            }

            return(definitions);
        }
Пример #29
0
        /// <summary>Convert a simulation zone object into a series definition</summary>
        /// <param name="storage">Storage service</param>
        /// <param name="simulationZone">The object to convert</param>
        private SeriesDefinition ConvertToSeriesDefinition(IStorageReader storage, SimulationZone simulationZone)
        {
            SeriesDefinition seriesDefinition = new Models.Graph.SeriesDefinition();

            seriesDefinition.type          = Type;
            seriesDefinition.marker        = simulationZone.visualElement.Marker;
            seriesDefinition.line          = simulationZone.visualElement.Line;
            seriesDefinition.markerSize    = simulationZone.visualElement.MarkerSize;
            seriesDefinition.lineThickness = simulationZone.visualElement.LineThickness;
            seriesDefinition.colour        = simulationZone.visualElement.colour;
            seriesDefinition.xFieldName    = XFieldName;
            seriesDefinition.yFieldName    = YFieldName;
            seriesDefinition.xAxis         = XAxis;
            seriesDefinition.yAxis         = YAxis;
            if (simulationZone.simulationNames.Count > 0)
            {
                seriesDefinition.xFieldUnits = storage.GetUnits(TableName, XFieldName);
                seriesDefinition.yFieldUnits = storage.GetUnits(TableName, YFieldName);
            }
            seriesDefinition.showInLegend = ShowInLegend;
            seriesDefinition.title        = simulationZone.GetSeriesTitle();
            if (IncludeSeriesNameInLegend)
            {
                seriesDefinition.title += ": " + Name;
            }
            if (Checkpoint != "Current")
            {
                seriesDefinition.title += " (" + Checkpoint + ")";
            }
            if (simulationZone.data.Count > 0)
            {
                seriesDefinition.data  = simulationZone.data.ToTable();
                seriesDefinition.x     = GetDataFromTable(seriesDefinition.data, XFieldName);
                seriesDefinition.y     = GetDataFromTable(seriesDefinition.data, YFieldName);
                seriesDefinition.x2    = GetDataFromTable(seriesDefinition.data, X2FieldName);
                seriesDefinition.y2    = GetDataFromTable(seriesDefinition.data, Y2FieldName);
                seriesDefinition.error = GetErrorDataFromTable(seriesDefinition.data, YFieldName);
                if (Cumulative)
                {
                    seriesDefinition.y = MathUtilities.Cumulative(seriesDefinition.y as IEnumerable <double>);
                }
                if (CumulativeX)
                {
                    seriesDefinition.x = MathUtilities.Cumulative(seriesDefinition.x as IEnumerable <double>);
                }
            }
            return(seriesDefinition);
        }
Пример #30
0
        /// <summary>
        /// Create a row filter for the specified factors.
        /// </summary>
        /// <param name="storage">Storage service</param>
        /// <param name="factors">A list of factors to build a filter for.</param>
        /// <param name="columnsInTable">Columns in table</param>
        private string CreateRowFilter(IStorageReader storage, IEnumerable <ISimulationGeneratorFactors> factors, IEnumerable <string> columnsInTable)
        {
            string factorFilters = null;

            List <ColumnNameValues> columns = new List <ColumnNameValues>();

            foreach (var factor in factors)
            {
                foreach (var factorColumn in factor.Columns)
                {
                    if (columnsInTable.Contains(factorColumn.Key))
                    {
                        ColumnNameValues column = columns.Find(col => col.ColumnName == factorColumn.Key);
                        if (column == null)
                        {
                            columns.Add(new ColumnNameValues(factorColumn.Key, factorColumn.Value));
                        }
                        else if (!column.ColumnValues.Contains(factorColumn.Value))
                        {
                            column.ColumnValues.Add(factorColumn.Value);
                        }
                    }
                }
            }

            foreach (var column in columns)
            {
                if (factorFilters != null)
                {
                    factorFilters += " AND ";
                }
                if (column.ColumnValues.Count == 1)
                {
                    foreach (var value in column.ColumnValues)
                    {
                        factorFilters += column.ColumnName + " = '" + value + "'";
                    }
                }
                else
                {
                    factorFilters += column.ColumnName + " IN (" +
                                     StringUtilities.Build(column.ColumnValues, ",", "'", "'") +
                                     ")";
                }
            }

            return(factorFilters);
        }