Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.DataPreprocessing/3.4/PreprocessingContext.cs @ 16674

Last change on this file since 16674 was 16559, checked in by jkarder, 6 years ago

#2520: renamed Fossil to Attic and set version to 1.0.0-pre01

File size: 5.7 KB
RevLine 
[10219]1#region License Information
2/* HeuristicLab
[16453]3 * Copyright (C) 2002-2019 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;
[16559]27using HEAL.Attic;
[10221]28using HeuristicLab.Problems.DataAnalysis;
[10219]29
30namespace HeuristicLab.DataPreprocessing {
31  [Item("PreprocessingContext", "PreprocessingContext")]
[16462]32  [StorableType("52D31B2B-7D48-482B-B875-5FCE0F8397A8")]
[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]
[16462]52    protected PreprocessingContext(StorableConstructorFlag _) : base(_) { }
[11114]53    protected PreprocessingContext(PreprocessingContext original, Cloner cloner)
[10219]54      : base(original, cloner) {
[13502]55      Source = cloner.Clone(original.Source);
[10219]56      Data = cloner.Clone(original.Data);
57    }
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new PreprocessingContext(this, cloner);
60    }
[13508]61    #endregion
[10383]62
[13502]63    #region Import
[13508]64    public void Import(IDataAnalysisProblemData problemData, IItem source = null) {
65      if (problemData == null) throw new ArgumentNullException("problemData");
66      if (source != null && ExtractProblemData(source) != problemData)
[14332]67        source = null; // ignore the source if the source's problem data is different
[13508]68      Source = source ?? problemData;
69      var namedSource = Source as INamedItem;
70      if (namedSource != null)
71        Name = "Preprocessing " + namedSource.Name;
[15518]72      Data = new FilteredPreprocessingData(new PreprocessingData(problemData));
[13508]73      OnReset();
74      // Reset GUI:
75      // - OnContentChanged for PreprocessingView!
76      // event? task(async import)?
[13502]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
[13508]87    public bool CanExport {
88      get { return Source is IAlgorithm || Source is IDataAnalysisProblem || Source is IDataAnalysisProblemData; }
[10383]89    }
[13508]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
[13502]110    private IAlgorithm ExportAlgorithm(IAlgorithm source) {
111      var preprocessedAlgorithm = (IAlgorithm)source.Clone();
[10990]112      preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
[13502]113      preprocessedAlgorithm.Runs.Clear();
[10990]114      var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
[13508]115      problem.ProblemDataParameter.ActualValue = CreateNewProblemData();
[10990]116      return preprocessedAlgorithm;
[10614]117    }
[13502]118    private IDataAnalysisProblem ExportProblem(IDataAnalysisProblem source) {
119      var preprocessedProblem = (IDataAnalysisProblem)source.Clone();
[13508]120      preprocessedProblem.ProblemDataParameter.ActualValue = CreateNewProblemData();
[10990]121      return preprocessedProblem;
122    }
[13502]123    private IDataAnalysisProblemData ExportProblemData(IDataAnalysisProblemData source) {
[13508]124      return CreateNewProblemData();
125    }
126
127    public IDataAnalysisProblemData CreateNewProblemData() {
[13502]128      var creator = new ProblemDataCreator(this);
[13508]129      var oldProblemData = ExtractProblemData(Source);
130      var newProblemData = creator.CreateProblemData(oldProblemData);
131      newProblemData.Name = "Preprocessed " + oldProblemData.Name;
132      return newProblemData;
[10383]133    }
[13508]134    #endregion
135
136    protected virtual void OnReset() {
137      if (Reset != null)
138        Reset(this, EventArgs.Empty);
[10383]139    }
[10219]140  }
141}
Note: See TracBrowser for help on using the repository browser.