Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/PreprocessingContext.cs @ 15529

Last change on this file since 15529 was 15270, checked in by pfleck, 7 years ago

#2809: Removed TransactionalPreprocessingData and moved relevant code to PreprocessingData.

File size: 5.7 KB
RevLine 
[10219]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10219]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
[13502]22using System;
23using System.Collections.Generic;
[10219]24using HeuristicLab.Common;
25using HeuristicLab.Core;
[10240]26using HeuristicLab.Optimization;
[13502]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[10221]28using HeuristicLab.Problems.DataAnalysis;
[10219]29
30namespace HeuristicLab.DataPreprocessing {
31  [Item("PreprocessingContext", "PreprocessingContext")]
[13504]32  [StorableClass]
[13502]33  public class PreprocessingContext : NamedItem, IStorableContent {
34    public string Filename { get; set; }
[10990]35
[13502]36    [Storable]
[10783]37    public IFilteredPreprocessingData Data { get; private set; }
[10990]38
[13502]39    [Storable]
40    private IItem Source { get; set; }
[10614]41
[13508]42    public event EventHandler Reset;
[10990]43
[13508]44    #region Constructors
45    public PreprocessingContext(IDataAnalysisProblemData problemData, IItem source = null)
[13507]46      : base("Data Preprocessing") {
[13508]47      if (problemData == null) throw new ArgumentNullException("problemData");
48      Import(problemData, source);
[10219]49    }
50
[13502]51    [StorableConstructor]
[13504]52    protected PreprocessingContext(bool deserializing)
[13502]53      : base(deserializing) { }
[11114]54    protected PreprocessingContext(PreprocessingContext original, Cloner cloner)
[10219]55      : base(original, cloner) {
[13502]56      Source = cloner.Clone(original.Source);
[10219]57      Data = cloner.Clone(original.Data);
58    }
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new PreprocessingContext(this, cloner);
61    }
[13508]62    #endregion
[10383]63
[13502]64    #region Import
[13508]65    public void Import(IDataAnalysisProblemData problemData, IItem source = null) {
66      if (problemData == null) throw new ArgumentNullException("problemData");
67      if (source != null && ExtractProblemData(source) != problemData)
[14332]68        source = null; // ignore the source if the source's problem data is different
[13508]69      Source = source ?? problemData;
70      var namedSource = Source as INamedItem;
71      if (namedSource != null)
72        Name = "Preprocessing " + namedSource.Name;
[15270]73      Data = new FilteredPreprocessingData(new PreprocessingData(problemData));
[13508]74      OnReset();
75      // Reset GUI:
76      // - OnContentChanged for PreprocessingView!
77      // event? task(async import)?
[13502]78    }
79    private IDataAnalysisProblemData ExtractProblemData(IItem source) {
80      var algorithm = source as Algorithm;
81      var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : source as IDataAnalysisProblem;
82      var problemData = problem != null ? problem.ProblemData : source as IDataAnalysisProblemData;
83      return problemData;
84    }
85    #endregion
86
87    #region Export
[13508]88    public bool CanExport {
89      get { return Source is IAlgorithm || Source is IDataAnalysisProblem || Source is IDataAnalysisProblemData; }
[10383]90    }
[13508]91    public IEnumerable<KeyValuePair<string, Func<IItem>>> GetSourceExportOptions() {
92      var algorithm = Source as IAlgorithm;
93      if (algorithm != null)
94        yield return new KeyValuePair<string, Func<IItem>>(
95          algorithm.GetType().GetPrettyName(),
96          () => ExportAlgorithm(algorithm));
97
98      var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : Source as IDataAnalysisProblem;
99      if (problem != null)
100        yield return new KeyValuePair<string, Func<IItem>>(
101          problem.GetType().GetPrettyName(),
102          () => ExportProblem(problem));
103
104      var problemData = problem != null ? problem.ProblemData : Source as IDataAnalysisProblemData;
105      if (problemData != null)
106        yield return new KeyValuePair<string, Func<IItem>>(
107          problemData.GetType().GetPrettyName(),
108          () => ExportProblemData(problemData));
109    }
110
[13502]111    private IAlgorithm ExportAlgorithm(IAlgorithm source) {
112      var preprocessedAlgorithm = (IAlgorithm)source.Clone();
[10990]113      preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
[13502]114      preprocessedAlgorithm.Runs.Clear();
[10990]115      var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
[13508]116      problem.ProblemDataParameter.ActualValue = CreateNewProblemData();
[10990]117      return preprocessedAlgorithm;
[10614]118    }
[13502]119    private IDataAnalysisProblem ExportProblem(IDataAnalysisProblem source) {
120      var preprocessedProblem = (IDataAnalysisProblem)source.Clone();
[13508]121      preprocessedProblem.ProblemDataParameter.ActualValue = CreateNewProblemData();
[10990]122      return preprocessedProblem;
123    }
[13502]124    private IDataAnalysisProblemData ExportProblemData(IDataAnalysisProblemData source) {
[13508]125      return CreateNewProblemData();
126    }
127
128    public IDataAnalysisProblemData CreateNewProblemData() {
[13502]129      var creator = new ProblemDataCreator(this);
[13508]130      var oldProblemData = ExtractProblemData(Source);
131      var newProblemData = creator.CreateProblemData(oldProblemData);
132      newProblemData.Name = "Preprocessed " + oldProblemData.Name;
133      return newProblemData;
[10383]134    }
[13508]135    #endregion
136
137    protected virtual void OnReset() {
138      if (Reset != null)
139        Reset(this, EventArgs.Empty);
[10383]140    }
[10219]141  }
142}
Note: See TracBrowser for help on using the repository browser.