Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.DataPreprocessing/3.4/PreprocessingContext.cs @ 16003

Last change on this file since 16003 was 15018, checked in by gkronber, 8 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

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