[2694] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2694] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
[3308] | 24 | using System.Collections.Generic;
|
---|
[3306] | 25 | using System.Drawing;
|
---|
[3308] | 26 | using System.Linq;
|
---|
[2694] | 27 | using System.Text;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Data {
|
---|
[3048] | 33 | [Item("StringMatrix", "Represents a matrix of strings.")]
|
---|
[3017] | 34 | [StorableClass]
|
---|
[3430] | 35 | public class StringMatrix : Item, IEnumerable<string>, IStringConvertibleMatrix {
|
---|
[9433] | 36 | private const int maximumToStringLength = 100;
|
---|
| 37 |
|
---|
[13656] | 38 | public static new Image StaticItemImage
|
---|
| 39 | {
|
---|
| 40 | get { return new Bitmap(25, 25); }
|
---|
[3306] | 41 | }
|
---|
| 42 |
|
---|
[2694] | 43 | [Storable]
|
---|
[3054] | 44 | protected string[,] matrix;
|
---|
[2694] | 45 |
|
---|
[3308] | 46 | [Storable]
|
---|
[3430] | 47 | protected List<string> columnNames;
|
---|
[13656] | 48 | public virtual IEnumerable<string> ColumnNames
|
---|
| 49 | {
|
---|
[3308] | 50 | get { return this.columnNames; }
|
---|
[13656] | 51 | set
|
---|
| 52 | {
|
---|
[3430] | 53 | if (ReadOnly) throw new NotSupportedException("ColumnNames cannot be set. StringMatrix is read-only.");
|
---|
[3308] | 54 | if (value == null || value.Count() == 0)
|
---|
| 55 | columnNames = new List<string>();
|
---|
| 56 | else if (value.Count() != Columns)
|
---|
[3310] | 57 | throw new ArgumentException("A column name must be specified for each column .");
|
---|
[3308] | 58 | else
|
---|
| 59 | columnNames = new List<string>(value);
|
---|
[5150] | 60 | OnColumnNamesChanged();
|
---|
[3308] | 61 | }
|
---|
| 62 | }
|
---|
[3310] | 63 | [Storable]
|
---|
[3430] | 64 | protected List<string> rowNames;
|
---|
[13656] | 65 | public virtual IEnumerable<string> RowNames
|
---|
| 66 | {
|
---|
[3310] | 67 | get { return this.rowNames; }
|
---|
[13656] | 68 | set
|
---|
| 69 | {
|
---|
[3430] | 70 | if (ReadOnly) throw new NotSupportedException("RowNames cannot be set. StringMatrix is read-only.");
|
---|
[3310] | 71 | if (value == null || value.Count() == 0)
|
---|
| 72 | rowNames = new List<string>();
|
---|
| 73 | else if (value.Count() != Rows)
|
---|
| 74 | throw new ArgumentException("A row name must be specified for each row.");
|
---|
| 75 | else
|
---|
| 76 | rowNames = new List<string>(value);
|
---|
[5150] | 77 | OnRowNamesChanged();
|
---|
[3310] | 78 | }
|
---|
| 79 | }
|
---|
[3320] | 80 | [Storable]
|
---|
[3430] | 81 | protected bool sortableView;
|
---|
[13656] | 82 | public virtual bool SortableView
|
---|
| 83 | {
|
---|
[3320] | 84 | get { return sortableView; }
|
---|
[13656] | 85 | set
|
---|
| 86 | {
|
---|
[3430] | 87 | if (ReadOnly) throw new NotSupportedException("SortableView cannot be set. StringMatrix is read-only.");
|
---|
[3320] | 88 | if (value != sortableView) {
|
---|
| 89 | sortableView = value;
|
---|
| 90 | OnSortableViewChanged();
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
[13841] | 94 | public static StringMatrix Parse(string val)
|
---|
| 95 | {
|
---|
| 96 | try
|
---|
| 97 | {
|
---|
| 98 | val = val.Replace("]", string.Empty);
|
---|
[3308] | 99 |
|
---|
[13841] | 100 | string[] parts = val.Split('[');
|
---|
| 101 | var r = parts.Length;
|
---|
| 102 | var c = parts[1].Split(';').Length;
|
---|
| 103 | val = val.Replace("\n", ";");
|
---|
| 104 | val = val.Replace("[", string.Empty);
|
---|
| 105 | string[] arr = System.Array.ConvertAll(val.Trim().Split(';'), p => p.Trim());
|
---|
| 106 | string[] a = arr.Where(s => s != "").ToArray();
|
---|
| 107 | string[,] b = new string[r, c];
|
---|
| 108 | var rowcount = 0;
|
---|
| 109 | for (int i = 0; i < a.Length;)
|
---|
| 110 | {
|
---|
| 111 | for (int j = 0; j < c; j++)
|
---|
| 112 | {
|
---|
| 113 | b[rowcount, j] = a[i];
|
---|
| 114 | i++;
|
---|
| 115 | }
|
---|
| 116 | rowcount++;
|
---|
| 117 | }
|
---|
| 118 | return new StringMatrix(b);
|
---|
| 119 | }
|
---|
| 120 | catch (System.FormatException e)
|
---|
| 121 | {
|
---|
| 122 | return null;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | public virtual int Rows
|
---|
[13656] | 126 | {
|
---|
[3054] | 127 | get { return matrix.GetLength(0); }
|
---|
[13656] | 128 | protected set
|
---|
| 129 | {
|
---|
[3430] | 130 | if (ReadOnly) throw new NotSupportedException("Rows cannot be set. StringMatrix is read-only.");
|
---|
[2694] | 131 | if (value != Rows) {
|
---|
[3054] | 132 | string[,] newMatrix = new string[value, Columns];
|
---|
| 133 | Array.Copy(matrix, newMatrix, Math.Min(value * Columns, matrix.Length));
|
---|
| 134 | matrix = newMatrix;
|
---|
[3320] | 135 | while (rowNames.Count > value)
|
---|
| 136 | rowNames.RemoveAt(rowNames.Count - 1);
|
---|
| 137 | while (rowNames.Count < value)
|
---|
| 138 | rowNames.Add("Row " + rowNames.Count);
|
---|
[5150] | 139 | OnRowsChanged();
|
---|
| 140 | OnRowNamesChanged();
|
---|
[2694] | 141 | OnReset();
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
[13656] | 145 | public virtual int Columns
|
---|
| 146 | {
|
---|
[3054] | 147 | get { return matrix.GetLength(1); }
|
---|
[13656] | 148 | protected set
|
---|
| 149 | {
|
---|
[3430] | 150 | if (ReadOnly) throw new NotSupportedException("Columns cannot be set. StringMatrix is read-only.");
|
---|
[2694] | 151 | if (value != Columns) {
|
---|
[3054] | 152 | string[,] newMatrix = new string[Rows, value];
|
---|
[2694] | 153 | for (int i = 0; i < Rows; i++)
|
---|
[3054] | 154 | Array.Copy(matrix, i * Columns, newMatrix, i * value, Math.Min(value, Columns));
|
---|
| 155 | matrix = newMatrix;
|
---|
[3320] | 156 | while (columnNames.Count > value)
|
---|
| 157 | columnNames.RemoveAt(columnNames.Count - 1);
|
---|
| 158 | while (columnNames.Count < value)
|
---|
| 159 | columnNames.Add("Column " + columnNames.Count);
|
---|
[5150] | 160 | OnColumnsChanged();
|
---|
| 161 | OnColumnNamesChanged();
|
---|
[2694] | 162 | OnReset();
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
[13656] | 166 | public virtual string this[int rowIndex, int columnIndex]
|
---|
| 167 | {
|
---|
[3054] | 168 | get { return matrix[rowIndex, columnIndex]; }
|
---|
[13656] | 169 | set
|
---|
| 170 | {
|
---|
[3430] | 171 | if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringMatrix is read-only.");
|
---|
[3054] | 172 | if (value != matrix[rowIndex, columnIndex]) {
|
---|
| 173 | if ((value != null) || (matrix[rowIndex, columnIndex] != string.Empty)) {
|
---|
| 174 | matrix[rowIndex, columnIndex] = value != null ? value : string.Empty;
|
---|
[2694] | 175 | OnItemChanged(rowIndex, columnIndex);
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[3430] | 181 | [Storable]
|
---|
| 182 | protected bool readOnly;
|
---|
[13656] | 183 | public virtual bool ReadOnly
|
---|
| 184 | {
|
---|
[3430] | 185 | get { return readOnly; }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[4722] | 188 | [StorableConstructor]
|
---|
| 189 | protected StringMatrix(bool deserializing) : base(deserializing) { }
|
---|
| 190 | protected StringMatrix(StringMatrix original, Cloner cloner)
|
---|
| 191 | : base(original, cloner) {
|
---|
| 192 | this.matrix = (string[,])original.matrix.Clone();
|
---|
| 193 | this.columnNames = new List<string>(original.columnNames);
|
---|
| 194 | this.rowNames = new List<string>(original.rowNames);
|
---|
| 195 | this.sortableView = original.sortableView;
|
---|
| 196 | this.readOnly = original.readOnly;
|
---|
| 197 | }
|
---|
[3048] | 198 | public StringMatrix() {
|
---|
[3054] | 199 | matrix = new string[0, 0];
|
---|
[3308] | 200 | columnNames = new List<string>();
|
---|
[3310] | 201 | rowNames = new List<string>();
|
---|
[3320] | 202 | sortableView = false;
|
---|
[3430] | 203 | readOnly = false;
|
---|
[2694] | 204 | }
|
---|
[3048] | 205 | public StringMatrix(int rows, int columns) {
|
---|
[3054] | 206 | matrix = new string[rows, columns];
|
---|
| 207 | for (int i = 0; i < matrix.GetLength(0); i++) {
|
---|
| 208 | for (int j = 0; j < matrix.GetLength(1); j++)
|
---|
| 209 | matrix[i, j] = string.Empty;
|
---|
[2694] | 210 | }
|
---|
[3308] | 211 | columnNames = new List<string>();
|
---|
[3310] | 212 | rowNames = new List<string>();
|
---|
[3320] | 213 | sortableView = false;
|
---|
[3430] | 214 | readOnly = false;
|
---|
[2694] | 215 | }
|
---|
[3308] | 216 | protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames)
|
---|
| 217 | : this(rows, columns) {
|
---|
| 218 | ColumnNames = columnNames;
|
---|
| 219 | }
|
---|
[3310] | 220 | protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
|
---|
[5150] | 221 | : this(rows, columns, columnNames) {
|
---|
[3310] | 222 | RowNames = rowNames;
|
---|
| 223 | }
|
---|
[3048] | 224 | public StringMatrix(string[,] elements) {
|
---|
[2694] | 225 | if (elements == null) throw new ArgumentNullException();
|
---|
[3054] | 226 | matrix = new string[elements.GetLength(0), elements.GetLength(1)];
|
---|
| 227 | for (int i = 0; i < matrix.GetLength(0); i++) {
|
---|
| 228 | for (int j = 0; j < matrix.GetLength(1); j++)
|
---|
| 229 | matrix[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
|
---|
[2694] | 230 | }
|
---|
[3308] | 231 | columnNames = new List<string>();
|
---|
[3310] | 232 | rowNames = new List<string>();
|
---|
[3320] | 233 | sortableView = false;
|
---|
[3430] | 234 | readOnly = false;
|
---|
[2694] | 235 | }
|
---|
[3308] | 236 | protected StringMatrix(string[,] elements, IEnumerable<string> columnNames)
|
---|
| 237 | : this(elements) {
|
---|
| 238 | ColumnNames = columnNames;
|
---|
| 239 | }
|
---|
[5150] | 240 | protected StringMatrix(string[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
|
---|
| 241 | : this(elements, columnNames) {
|
---|
[3310] | 242 | RowNames = rowNames;
|
---|
| 243 | }
|
---|
[2694] | 244 |
|
---|
| 245 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 246 | return new StringMatrix(this, cloner);
|
---|
[2694] | 247 | }
|
---|
| 248 |
|
---|
[3430] | 249 | public virtual StringMatrix AsReadOnly() {
|
---|
| 250 | StringMatrix readOnlyStringMatrix = (StringMatrix)this.Clone();
|
---|
| 251 | readOnlyStringMatrix.readOnly = true;
|
---|
| 252 | return readOnlyStringMatrix;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[2694] | 255 | public override string ToString() {
|
---|
[9433] | 256 | if (matrix.Length == 0) return "[]";
|
---|
| 257 |
|
---|
[2694] | 258 | StringBuilder sb = new StringBuilder();
|
---|
| 259 | sb.Append("[");
|
---|
[9433] | 260 | for (int i = 0; i < Rows; i++) {
|
---|
| 261 | sb.Append("[").Append(matrix[i, 0]);
|
---|
| 262 | for (int j = 1; j < Columns; j++)
|
---|
| 263 | sb.Append(";").Append(matrix[i, j]);
|
---|
| 264 | sb.Append("]");
|
---|
| 265 |
|
---|
| 266 | if (sb.Length > maximumToStringLength) {
|
---|
| 267 | sb.Append("[...]");
|
---|
| 268 | break;
|
---|
[2694] | 269 | }
|
---|
| 270 | }
|
---|
| 271 | sb.Append("]");
|
---|
[9433] | 272 |
|
---|
[2694] | 273 | return sb.ToString();
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[3430] | 276 | public virtual IEnumerator<string> GetEnumerator() {
|
---|
| 277 | return matrix.Cast<string>().GetEnumerator();
|
---|
[2694] | 278 | }
|
---|
| 279 |
|
---|
[3430] | 280 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 281 | return GetEnumerator();
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[3054] | 284 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
[2694] | 285 | if (value == null) {
|
---|
| 286 | errorMessage = "Invalid Value (string must not be null)";
|
---|
| 287 | return false;
|
---|
| 288 | } else {
|
---|
| 289 | errorMessage = string.Empty;
|
---|
| 290 | return true;
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
[3054] | 293 | protected virtual string GetValue(int rowIndex, int columIndex) {
|
---|
[2694] | 294 | return this[rowIndex, columIndex];
|
---|
| 295 | }
|
---|
[3054] | 296 | protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
[2694] | 297 | if (value != null) {
|
---|
| 298 | this[rowIndex, columnIndex] = value;
|
---|
| 299 | return true;
|
---|
| 300 | } else {
|
---|
| 301 | return false;
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
[3054] | 304 |
|
---|
[5150] | 305 | #region events
|
---|
| 306 | public event EventHandler ColumnsChanged;
|
---|
| 307 | protected virtual void OnColumnsChanged() {
|
---|
| 308 | EventHandler handler = ColumnsChanged;
|
---|
| 309 | if (handler != null)
|
---|
| 310 | handler(this, EventArgs.Empty);
|
---|
| 311 | }
|
---|
| 312 | public event EventHandler RowsChanged;
|
---|
| 313 | protected virtual void OnRowsChanged() {
|
---|
| 314 | EventHandler handler = RowsChanged;
|
---|
| 315 | if (handler != null)
|
---|
| 316 | handler(this, EventArgs.Empty);
|
---|
| 317 | }
|
---|
[3320] | 318 | public event EventHandler ColumnNamesChanged;
|
---|
| 319 | protected virtual void OnColumnNamesChanged() {
|
---|
| 320 | EventHandler handler = ColumnNamesChanged;
|
---|
| 321 | if (handler != null)
|
---|
| 322 | handler(this, EventArgs.Empty);
|
---|
| 323 | }
|
---|
| 324 | public event EventHandler RowNamesChanged;
|
---|
| 325 | protected virtual void OnRowNamesChanged() {
|
---|
| 326 | EventHandler handler = RowNamesChanged;
|
---|
| 327 | if (handler != null)
|
---|
| 328 | handler(this, EventArgs.Empty);
|
---|
| 329 | }
|
---|
| 330 | public event EventHandler SortableViewChanged;
|
---|
| 331 | protected virtual void OnSortableViewChanged() {
|
---|
| 332 | EventHandler handler = SortableViewChanged;
|
---|
| 333 | if (handler != null)
|
---|
| 334 | handler(this, EventArgs.Empty);
|
---|
| 335 | }
|
---|
[2973] | 336 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
[3054] | 337 | protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
[2694] | 338 | if (ItemChanged != null)
|
---|
| 339 | ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
[9433] | 340 |
|
---|
| 341 | //approximation to avoid firing of unnecessary ToStringChangedEvents
|
---|
| 342 | //columnIndex is not used, because always full rows are returned in the ToString method
|
---|
| 343 | if (rowIndex * Columns < maximumToStringLength)
|
---|
| 344 | OnToStringChanged();
|
---|
[2694] | 345 | }
|
---|
[2973] | 346 | public event EventHandler Reset;
|
---|
[3054] | 347 | protected virtual void OnReset() {
|
---|
[2694] | 348 | if (Reset != null)
|
---|
| 349 | Reset(this, EventArgs.Empty);
|
---|
[2932] | 350 | OnToStringChanged();
|
---|
[2694] | 351 | }
|
---|
[5150] | 352 | #endregion
|
---|
[3054] | 353 |
|
---|
| 354 | #region IStringConvertibleMatrix Members
|
---|
[13656] | 355 | int IStringConvertibleMatrix.Rows
|
---|
| 356 | {
|
---|
[3054] | 357 | get { return Rows; }
|
---|
| 358 | set { Rows = value; }
|
---|
| 359 | }
|
---|
[13656] | 360 | int IStringConvertibleMatrix.Columns
|
---|
| 361 | {
|
---|
[3054] | 362 | get { return Columns; }
|
---|
| 363 | set { Columns = value; }
|
---|
| 364 | }
|
---|
| 365 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
| 366 | return Validate(value, out errorMessage);
|
---|
| 367 | }
|
---|
| 368 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
|
---|
| 369 | return GetValue(rowIndex, columIndex);
|
---|
| 370 | }
|
---|
| 371 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 372 | return SetValue(value, rowIndex, columnIndex);
|
---|
| 373 | }
|
---|
[2694] | 374 | #endregion
|
---|
| 375 | }
|
---|
| 376 | }
|
---|