Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10673 was 10665, checked in by tsteinre, 11 years ago

prepared TransactionalPreprocessingData to work with Transformations

File size: 6.5 KB
RevLine 
[10163]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;
[10168]23using System.Collections;
[10163]24using System.Collections.Generic;
[10185]25using HeuristicLab.Common;
[10163]26using HeuristicLab.Core;
27using HeuristicLab.Problems.DataAnalysis;
28
[10182]29namespace HeuristicLab.DataPreprocessing {
[10550]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
[10163]45  [Item("PreprocessingData", "Represents data used for preprocessing.")]
[10586]46  public class TransactionalPreprocessingData : PreprocessingData, ITransactionalPreprocessingData {
[10163]47
[10550]48    private const int MAX_UNDO_DEPTH = 5;
49
50    private IList<PDSnapshot> undoHistory;
51
[10580]52    private int transactionDepth = 0;
53
[10609]54    public TransactionalPreprocessingData(IDataAnalysisProblemData problemData)
55      : base(problemData) {
[10550]56      undoHistory = new List<PDSnapshot>();
[10185]57    }
[10187]58
[10609]59    private TransactionalPreprocessingData(TransactionalPreprocessingData original, Cloner cloner)
60      : base(original, cloner) {
[10550]61      undoHistory = new List<PDSnapshot>();
[10163]62    }
63
[10550]64    private void SaveSnapshot(DataPreprocessingChangedEventType changedType, int column, int row) {
[10580]65      if (transactionDepth > 0) return;
66
[10550]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
[10163]81    #region NamedItem abstract Member Implementations
82
[10185]83    public override IDeepCloneable Clone(Cloner cloner) {
[10586]84      return new TransactionalPreprocessingData(this, cloner);
[10163]85    }
86
87    #endregion
88
[10586]89    #region Overridden IPreprocessingData Members
[10163]90
[10586]91    public override void SetCell<T>(int columnIndex, int rowIndex, T value) {
[10550]92      SaveSnapshot(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex);
[10586]93      base.SetCell<T>(columnIndex, rowIndex, value);
[10580]94      if (transactionDepth <= 0)
95        OnChanged(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex);
[10367]96    }
97
[10586]98    public override void SetValues<T>(int columnIndex, IList<T> values) {
99      SaveSnapshot(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
100      base.SetValues<T>(columnIndex, values);
[10580]101      if (transactionDepth <= 0)
102        OnChanged(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1);
[10181]103    }
104
[10586]105    public override void InsertRow(int rowIndex) {
[10550]106      SaveSnapshot(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex);
[10586]107      base.InsertRow(rowIndex);
[10580]108      if (transactionDepth <= 0)
109        OnChanged(DataPreprocessingChangedEventType.AddRow, -1, rowIndex);
[10163]110    }
111
[10586]112    public override void DeleteRow(int rowIndex) {
[10550]113      SaveSnapshot(DataPreprocessingChangedEventType.AddRow, -1, rowIndex);
[10586]114      base.DeleteRow(rowIndex);
[10580]115      if (transactionDepth <= 0)
116        OnChanged(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex);
[10163]117    }
118
[10586]119    public override void InsertColumn<T>(string variableName, int columnIndex) {
[10550]120      SaveSnapshot(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1);
[10586]121      base.InsertColumn<T>(variableName, columnIndex);
[10580]122      if (transactionDepth <= 0)
123        OnChanged(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1);
[10163]124    }
125
[10586]126    public override void DeleteColumn(int columnIndex) {
[10550]127      SaveSnapshot(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1);
[10586]128      base.DeleteColumn(columnIndex);
[10580]129      if (transactionDepth <= 0)
130        OnChanged(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1);
[10367]131    }
132
[10586]133    #endregion
[10221]134
[10586]135    #region TransactionalPreprocessingData members
[10221]136
[10544]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
[10547]143    public bool IsUndoAvailable {
[10550]144      get { return undoHistory.Count > 0; }
[10547]145    }
146
147    public void Undo() {
[10550]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      }
[10547]158    }
159
[10665]160    public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
161      BeginTransaction(type);
[10612]162      action();
163      EndTransaction();
164    }
165
[10665]166    public void BeginTransaction(DataPreprocessingChangedEventType type) {
167      SaveSnapshot(type, -1, -1);
[10580]168      transactionDepth++;
169    }
170
171    public void EndTransaction() {
172      transactionDepth--;
173      if (transactionDepth < 0)
174        throw new InvalidOperationException("There is no open transaction that can be ended.");
175      if (transactionDepth == 0)
[10581]176        OnChanged(DataPreprocessingChangedEventType.Any, -1, -1);
[10580]177    }
178
[10220]179    #endregion
[10163]180  }
181}
Note: See TracBrowser for help on using the repository browser.