Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17399 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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