#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Problems.DataAnalysis.Transformations; namespace HeuristicLab.DataPreprocessing { public class PreprocessingTransformator { private readonly ITransactionalPreprocessingData preprocessingData; public PreprocessingTransformator(IPreprocessingData preprocessingData) { this.preprocessingData = (ITransactionalPreprocessingData)preprocessingData; } public bool ApplyTransformations(IEnumerable transformations, out string errorMsg) { bool success; preprocessingData.BeginTransaction(DataPreprocessingChangedEventType.Transformation); try { var doubleTransformations = transformations.OfType>().ToList(); ApplyDoubleTranformations(doubleTransformations, out success, out errorMsg); } finally { preprocessingData.EndTransaction(); //if (!success) //preprocessingData.Undo(); } return success; } private void ApplyDoubleTranformations(IEnumerable> transformations, out bool success, out string errorMsg) { errorMsg = string.Empty; success = true; foreach (var transformation in transformations) { int colIndex = preprocessingData.GetColumnIndex(transformation.Column); var originalData = preprocessingData.GetValues(colIndex); string errorMsgPart; bool successPart; var transformedData = ApplyDoubleTransformation(transformation, originalData, out successPart, out errorMsgPart); errorMsg += errorMsgPart + Environment.NewLine; //if (!success) return; if (!successPart) success = false; preprocessingData.SetValues(colIndex, transformedData.ToList()); preprocessingData.Transformations.Add(transformation); } } private IEnumerable ApplyDoubleTransformation(Transformation transformation, IEnumerable data, out bool success, out string errorMsg) { success = transformation.Check(data, out errorMsg); return transformation.Apply(data); } } }