using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using HeuristicLab.Data; namespace HeuristicLab.DataPreprocessing { public class DataGridLogic : IDataGridLogic { private IPreprocessingData preprocessingData; public DataGridLogic(IPreprocessingData preprocessingData) { this.preprocessingData = preprocessingData; } public int Rows { get { return preprocessingData.Rows; } } public int Columns { get { return preprocessingData.Columns; } } public IEnumerable ColumnNames { get { return preprocessingData.VariableNames; } } public IEnumerable RowNames { get { return Enumerable.Range(1, Rows).Select(n => n.ToString()); } } public bool Validate(string value, out string errorMessage, int columnIndex) { if (columnIndex < 0 || columnIndex > preprocessingData.VariableNames.Count()) { throw new ArgumentOutOfRangeException("column index is out of range"); } bool valid = false; if (preprocessingData.IsType(columnIndex)) { double val; valid = double.TryParse(value, out val); errorMessage = string.Empty; if (!valid) { errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")"; } } else if (preprocessingData.IsType(columnIndex)) { valid = value != null; errorMessage = string.Empty; if (!valid) { errorMessage = "Invalid Value (string must not be null)"; } } else if (preprocessingData.IsType(columnIndex)) { DateTime date; valid = DateTime.TryParse(value, out date); errorMessage = string.Empty; if (!valid) { errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\""; } } else { throw new ArgumentException("column " + columnIndex + " contains a non supported type."); } return valid; } public string GetValue(int rowIndex, int columnIndex) { return preprocessingData.GetCellAsString(columnIndex, rowIndex); } public bool SetValue(string value, int rowIndex, int columnIndex) { bool valid = false; if (preprocessingData.IsType(columnIndex)) { double val; valid = double.TryParse(value, out val); if (valid) { preprocessingData.SetCell(columnIndex, rowIndex, val); } } else if (preprocessingData.IsType(columnIndex)) { valid = value != null; if (valid) { preprocessingData.SetCell(columnIndex, rowIndex, value); } } else if (preprocessingData.IsType(columnIndex)) { DateTime date; valid = DateTime.TryParse(value, out date); if (valid) { preprocessingData.SetCell(columnIndex, rowIndex, date); } } else { throw new ArgumentException("column " + columnIndex + " contains a non supported type."); } return valid; } } }