Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.DataPreprocessing/3.4/PreprocessingContext.cs @ 16659

Last change on this file since 16659 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.DataPreprocessing {
31  [Item("PreprocessingContext", "PreprocessingContext")]
32  [StorableType("52D31B2B-7D48-482B-B875-5FCE0F8397A8")]
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(StorableConstructorFlag _) : base(_) { }
53    protected PreprocessingContext(PreprocessingContext original, Cloner cloner)
54      : base(original, cloner) {
55      Source = cloner.Clone(original.Source);
56      Data = cloner.Clone(original.Data);
57    }
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new PreprocessingContext(this, cloner);
60    }
61    #endregion
62
63    #region Import
64    public void Import(IDataAnalysisProblemData problemData, IItem source = null) {
65      if (problemData == null) throw new ArgumentNullException("problemData");
66      if (source != null && ExtractProblemData(source) != problemData)
67        source = null; // ignore the source if the source's problem data is different
68      Source = source ?? problemData;
69      var namedSource = Source as INamedItem;
70      if (namedSource != null)
71        Name = "Preprocessing " + namedSource.Name;
72      Data = new FilteredPreprocessingData(new PreprocessingData(problemData));
73      OnReset();
74      // Reset GUI:
75      // - OnContentChanged for PreprocessingView!
76      // event? task(async import)?
77    }
78    private IDataAnalysisProblemData ExtractProblemData(IItem source) {
79      var algorithm = source as Algorithm;
80      var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : source as IDataAnalysisProblem;
81      var problemData = problem != null ? problem.ProblemData : source as IDataAnalysisProblemData;
82      return problemData;
83    }
84    #endregion
85
86    #region Export
87    public bool CanExport {
88      get { return Source is IAlgorithm || Source is IDataAnalysisProblem || Source is IDataAnalysisProblemData; }
89    }
90    public IEnumerable<KeyValuePair<string, Func<IItem>>> GetSourceExportOptions() {
91      var algorithm = Source as IAlgorithm;
92      if (algorithm != null)
93        yield return new KeyValuePair<string, Func<IItem>>(
94          algorithm.GetType().GetPrettyName(),
95          () => ExportAlgorithm(algorithm));
96
97      var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : Source as IDataAnalysisProblem;
98      if (problem != null)
99        yield return new KeyValuePair<string, Func<IItem>>(
100          problem.GetType().GetPrettyName(),
101          () => ExportProblem(problem));
102
103      var problemData = problem != null ? problem.ProblemData : Source as IDataAnalysisProblemData;
104      if (problemData != null)
105        yield return new KeyValuePair<string, Func<IItem>>(
106          problemData.GetType().GetPrettyName(),
107          () => ExportProblemData(problemData));
108    }
109
110    private IAlgorithm ExportAlgorithm(IAlgorithm source) {
111      var preprocessedAlgorithm = (IAlgorithm)source.Clone();
112      preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
113      preprocessedAlgorithm.Runs.Clear();
114      var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
115      problem.ProblemDataParameter.ActualValue = CreateNewProblemData();
116      return preprocessedAlgorithm;
117    }
118    private IDataAnalysisProblem ExportProblem(IDataAnalysisProblem source) {
119      var preprocessedProblem = (IDataAnalysisProblem)source.Clone();
120      preprocessedProblem.ProblemDataParameter.ActualValue = CreateNewProblemData();
121      return preprocessedProblem;
122    }
123    private IDataAnalysisProblemData ExportProblemData(IDataAnalysisProblemData source) {
124      return CreateNewProblemData();
125    }
126
127    public IDataAnalysisProblemData CreateNewProblemData() {
128      var creator = new ProblemDataCreator(this);
129      var oldProblemData = ExtractProblemData(Source);
130      var newProblemData = creator.CreateProblemData(oldProblemData);
131      newProblemData.Name = "Preprocessed " + oldProblemData.Name;
132      return newProblemData;
133    }
134    #endregion
135
136    protected virtual void OnReset() {
137      if (Reset != null)
138        Reset(this, EventArgs.Empty);
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.