Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/TransactionalPreprocessingData.cs @ 10586

Last change on this file since 10586 was 10586, checked in by tsteinre, 11 years ago
  • divided/refactored PreprocessingData into TransactionalPreprocessingData and preprocessingData
File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Problems.DataAnalysis;
28
29namespace HeuristicLab.DataPreprocessing {
30
31  internal class PDSnapshot {
32    public IDictionary<int, IList> VariableValues { get; set; }
33
34    public IList<string> VariableNames { get; set; }
35
36    public double TrainingToTestRatio { get; set; }
37
38    public DataPreprocessingChangedEventType ChangedType { get; set; }
39
40    public int ChangedColumn { get; set; }
41
42    public int ChangedRow { get; set; }
43  }
44
45  [Item("PreprocessingData", "Represents data used for preprocessing.")]
46  public class TransactionalPreprocessingData : PreprocessingData, ITransactionalPreprocessingData {
47
48    private const int MAX_UNDO_DEPTH = 5;
49
50    private IList<PDSnapshot> undoHistory;
51
52    private int transactionDepth = 0;
53
54    private TransactionalPreprocessingData(TransactionalPreprocessingData original, Cloner cloner)
55      : base(original, cloner) {
56      undoHistory = new List<PDSnapshot>();
57    }
58
59    public TransactionalPreprocessingData(IDataAnalysisProblemData problemData)
60      : base(problemData) {
61      undoHistory = new List<PDSnapshot>();
62    }
63
64    private void SaveSnapshot(DataPreprocessingChangedEventType changedType, int column, int row) {
65      if (transactionDepth > 0) return;
66
67      PDSnapshot currentSnapshot = new PDSnapshot();
68      currentSnapshot.VariableValues = CopyVariableValues(variableValues);
69      currentSnapshot.VariableNames = new List<string>(variableNames);
70      currentSnapshot.TrainingToTestRatio = trainingToTestRatio;
71      currentSnapshot.ChangedType = changedType;
72      currentSnapshot.ChangedColumn = column;
73      currentSnapshot.ChangedRow = row;
74
75      if (undoHistory.Count >= MAX_UNDO_DEPTH)
76        undoHistory.RemoveAt(0);
77
78      undoHistory.Add(currentSnapshot);
79    }
80
81    #region NamedItem abstract Member Implementations
82
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new TransactionalPreprocessingData(this, cloner);
85    }
86
87    #endregion
88
89    #region Overridden IPreprocessingData Members
90
91    public override void SetCell<T>(int columnIndex, int rowIndex, T value) {
92      SaveSnapshot(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex);
93      base.SetCell<T>(columnIndex, rowIndex, value);
94      if (transactionDepth <= 0)
95        OnChanged(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex);
96    }
97
98    public override void SetValues<T>(int columnIndex, IList<T> values) {
99      SaveSnapshot(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
100      base.SetValues<T>(columnIndex, values);
101      if (transactionDepth <= 0)
102        OnChanged(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
103    }
104
105    public override void InsertRow(int rowIndex) {
106      SaveSnapshot(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex);
107      base.InsertRow(rowIndex);
108      if (transactionDepth <= 0)
109        OnChanged(DataPreprocessingChangedEventType.AddRow, -1, rowIndex);
110    }
111
112    public override void DeleteRow(int rowIndex) {
113      SaveSnapshot(DataPreprocessingChangedEventType.AddRow, -1, rowIndex);
114      base.DeleteRow(rowIndex);
115      if (transactionDepth <= 0)
116        OnChanged(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex);
117    }
118
119    public override void InsertColumn<T>(string variableName, int columnIndex) {
120      SaveSnapshot(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1);
121      base.InsertColumn<T>(variableName, columnIndex);
122      if (transactionDepth <= 0)
123        OnChanged(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1);
124    }
125
126    public override void DeleteColumn(int columnIndex) {
127      SaveSnapshot(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1);
128      base.DeleteColumn(columnIndex);
129      if (transactionDepth <= 0)
130        OnChanged(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1);
131    }
132
133    #endregion
134
135    #region TransactionalPreprocessingData members
136
137    public event DataPreprocessingChangedEventHandler Changed;
138    protected virtual void OnChanged(DataPreprocessingChangedEventType type, int column, int row) {
139      var listeners = Changed;
140      if (listeners != null) listeners(this, new DataPreprocessingChangedEventArgs(type, column, row));
141    }
142
143    public bool IsUndoAvailable {
144      get { return undoHistory.Count > 0; }
145    }
146
147    public void Undo() {
148      if (IsUndoAvailable) {
149        PDSnapshot previousSnapshot = undoHistory[undoHistory.Count - 1];
150        variableValues = previousSnapshot.VariableValues;
151        variableNames = previousSnapshot.VariableNames;
152        trainingToTestRatio = previousSnapshot.TrainingToTestRatio;
153        undoHistory.Remove(previousSnapshot);
154        OnChanged(previousSnapshot.ChangedType,
155          previousSnapshot.ChangedColumn,
156          previousSnapshot.ChangedRow);
157      }
158    }
159
160    public void BeginTransaction() {
161      SaveSnapshot(DataPreprocessingChangedEventType.Any, -1, -1);
162      transactionDepth++;
163    }
164
165    public void EndTransaction() {
166      transactionDepth--;
167      if (transactionDepth < 0)
168        throw new InvalidOperationException("There is no open transaction that can be ended.");
169      if (transactionDepth == 0)
170        OnChanged(DataPreprocessingChangedEventType.Any, -1, -1);
171    }
172
173    #endregion
174  }
175}
Note: See TracBrowser for help on using the repository browser.