Example #1
0
        /// <summary>
        /// Imports factor information from a csv file, saves the data to this.simulations, then updates the TreeView.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="args">Event arguments.</param>
        private void OnImportCsv(object sender, FileActionArgs args)
        {
            try
            {
                if (string.IsNullOrEmpty(args.Path))
                {
                    throw new ArgumentNullException("Unable to import csv file: path is null.");
                }
                if (!File.Exists(args.Path))
                {
                    throw new ArgumentException("Unable to import {0}: file does not exist.");
                }

                using (StreamReader file = new StreamReader(args.Path))
                {
                    string        line = file.ReadLine();
                    List <string> data = line.Split(',').ToList();
                    if (!data.SequenceEqual(headers))
                    {
                        throw new Exception("Column Headers in " + args.Path + " do not match current headers. Are you sure you selected the correct .csv file?");
                    }

                    simulations = new List <Tuple <string, List <string>, bool> >();

                    int i = 2;
                    while ((line = file.ReadLine()) != null)
                    {
                        data = line.Split(',').ToList();

                        string name = data[0];
                        if (data.Count == headers.Count)
                        {
                            bool enabled;
                            if (!bool.TryParse(data[data.Count - 1], out enabled))
                            {
                                throw new Exception("Unable to parse " + data[data.Count - 1] + " to bool on line " + i + ".");
                            }
                            simulations.Add(new Tuple <string, List <string>, bool>(data[0], data.Skip(1).Take(data.Count - 2).ToList(), enabled));
                        }
                        else if (data.Count > headers.Count)
                        {
                            throw new Exception("Too many elements in row " + i + ".");
                        }
                        else
                        {
                            throw new Exception("Too few elements in row " + i + ".");
                        }
                    }
                }
                UpdateView();
                model.DisabledSimNames = GetDisabledSimNames();
                presenter.MainPresenter.ShowMessage("Successfully imported data from " + args.Path, Simulation.MessageType.Information);
            }
            catch (Exception e)
            {
                presenter.MainPresenter.ShowError(e);
            }
        }
Example #2
0
        /// <summary>
        /// Generates a .csv file containing the factor information displayed in the grid.
        /// The user can edit this file to more efficiently enable or disable factors in bulk.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="args">Event arguments.</param>
        private void OnExportCsv(object sender, FileActionArgs args)
        {
            try
            {
                if (string.IsNullOrEmpty(args.Path))
                {
                    throw new ArgumentNullException("Unable to generate csv file: path is null.");
                }

                StringBuilder data = new StringBuilder();
                if (headers == null || !headers.Any())
                {
                    throw new Exception("No data to export.");
                }

                // The first line contains the column headers.
                string currentLine = headers.Aggregate((a, b) => a + "," + b);
                data.AppendLine(currentLine);

                // The rest of the file contains the factor information.
                foreach (Tuple <string, List <string>, bool> sim in GetTableData(model.AllCombinations(), true))
                {
                    // The first item on each line is the simulation name.
                    currentLine = sim.Item1 + ",";
                    // The rest of the line (except for the last item) contains the factor levels.
                    currentLine += sim.Item2.Aggregate((a, b) => a + "," + b);
                    // The final item on each line is the enabled status of the simulation.
                    currentLine += "," + sim.Item3.ToString();
                    data.AppendLine(currentLine);
                }

                File.WriteAllText(args.Path, data.ToString());
                presenter.MainPresenter.ShowMessage(string.Format("Successfully generated {0}.", args.Path), Simulation.MessageType.Information);
            }
            catch (Exception e)
            {
                presenter.MainPresenter.ShowError(e);
            }
        }