Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2922-DataCompletenessChartPerf/HeuristicLab.DataPreprocessing/3.4/PreprocessingContext.cs @ 17149

Last change on this file since 17149 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.DataPreprocessing {
31  [Item("PreprocessingContext", "PreprocessingContext")]
32  [StorableClass]
33  public class PreprocessingContext : NamedItem, IStorableContent {
34    public string Filename { get; set; }
35
36    [Storable]
37    public IFilteredPreprocessingData Data { get; private set; }
38
39    [Storable]
40    private IItem Source { get; set; }
41
42    public event EventHandler Reset;
43
44    #region Constructors
45    public PreprocessingContext(IDataAnalysisProblemData problemData, IItem source = null)
46      : base("Data Preprocessing") {
47      if (problemData == null) throw new ArgumentNullException("problemData");
48      Import(problemData, source);
49    }
50
51    [StorableConstructor]
52    protected PreprocessingContext(bool deserializing)
53      : base(deserializing) { }
54    protected PreprocessingContext(PreprocessingContext original, Cloner cloner)
55      : base(original, cloner) {
56      Source = cloner.Clone(original.Source);
57      Data = cloner.Clone(original.Data);
58    }
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new PreprocessingContext(this, cloner);
61    }
62    #endregion
63
64    #region Import
65    public void Import(IDataAnalysisProblemData problemData, IItem source = null) {
66      if (problemData == null) throw new ArgumentNullException("problemData");
67      if (source != null && ExtractProblemData(source) != problemData)
68        source = null; // ignore the source if the source's problem data is different
69      Source = source ?? problemData;
70      var namedSource = Source as INamedItem;
71      if (namedSource != null)
72        Name = "Preprocessing " + namedSource.Name;
73      Data = new FilteredPreprocessingData(new PreprocessingData(problemData));
74      OnReset();
75      // Reset GUI:
76      // - OnContentChanged for PreprocessingView!
77      // event? task(async import)?
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
88    public bool CanExport {
89      get { return Source is IAlgorithm || Source is IDataAnalysisProblem || Source is IDataAnalysisProblemData; }
90    }
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
111    private IAlgorithm ExportAlgorithm(IAlgorithm source) {
112      var preprocessedAlgorithm = (IAlgorithm)source.Clone();
113      preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
114      preprocessedAlgorithm.Runs.Clear();
115      var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
116      problem.ProblemDataParameter.ActualValue = CreateNewProblemData();
117      return preprocessedAlgorithm;
118    }
119    private IDataAnalysisProblem ExportProblem(IDataAnalysisProblem source) {
120      var preprocessedProblem = (IDataAnalysisProblem)source.Clone();
121      preprocessedProblem.ProblemDataParameter.ActualValue = CreateNewProblemData();
122      return preprocessedProblem;
123    }
124    private IDataAnalysisProblemData ExportProblemData(IDataAnalysisProblemData source) {
125      return CreateNewProblemData();
126    }
127
128    public IDataAnalysisProblemData CreateNewProblemData() {
129      var creator = new ProblemDataCreator(this);
130      var oldProblemData = ExtractProblemData(Source);
131      var newProblemData = creator.CreateProblemData(oldProblemData);
132      newProblemData.Name = "Preprocessed " + oldProblemData.Name;
133      return newProblemData;
134    }
135    #endregion
136
137    protected virtual void OnReset() {
138      if (Reset != null)
139        Reset(this, EventArgs.Empty);
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.