Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/14/16 17:51:48 (8 years ago)
Author:
pfleck
Message:

#2559

  • Enabled type selection for creating/importing/exporting/applying.
  • Deleted unnecessary interfaces.
  • Reorganized source files of DataPreprocessing.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.DataPreprocessing/3.4/PreprocessingContext.cs

    r13507 r13508  
    3535    public string Filename { get; set; }
    3636
    37     public IEnumerable<KeyValuePair<string, Func<IItem>>> ExportPossibilities {
    38       get {
    39         var algorithm = Source as IAlgorithm;
    40         if (algorithm != null)
    41           yield return new KeyValuePair<string, Func<IItem>>(algorithm.GetType().GetPrettyName(), () => ExportAlgorithm(algorithm));
    42 
    43         var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : Source as IDataAnalysisProblem;
    44         if (problem != null)
    45           yield return new KeyValuePair<string, Func<IItem>>(problem.GetType().GetPrettyName(), () => ExportProblem(problem));
    46 
    47         var problemData = problem != null ? problem.ProblemData : Source as IDataAnalysisProblemData;
    48         if (problemData != null)
    49           yield return new KeyValuePair<string, Func<IItem>>(problemData.GetType().GetPrettyName(), () => ExportProblemData(problemData));
    50 
    51         // ToDo: Export CSV
    52       }
    53     }
    54     public bool CanExport {
    55       get { return Source is IAlgorithm || Source is IDataAnalysisProblem || Source is IDataAnalysisProblemData; }
    56     }
    57 
    5837    [Storable]
    5938    public IFilteredPreprocessingData Data { get; private set; }
     
    6241    private IItem Source { get; set; }
    6342
     43    public event EventHandler Reset;
    6444
    65     public PreprocessingContext() : this(new RegressionProblemData()) {
    66       Name = "Data Preprocessing";
     45    #region Constructors
     46    public PreprocessingContext()
     47      : this(new RegressionProblemData()) {
    6748    }
    68     public PreprocessingContext(IItem source)
     49    public PreprocessingContext(IDataAnalysisProblemData problemData, IItem source = null)
    6950      : base("Data Preprocessing") {
    70       Import(source);
     51      if (problemData == null) throw new ArgumentNullException("problemData");
     52      Import(problemData, source);
    7153    }
    7254
     
    7961      Data = cloner.Clone(original.Data);
    8062    }
    81 
    8263    public override IDeepCloneable Clone(Cloner cloner) {
    8364      return new PreprocessingContext(this, cloner);
    8465    }
     66    #endregion
    8567
    8668    #region Import
    87     public void Import(IItem source) {
    88       Source = source;
    89       var namedSource = source as INamedItem;
    90       if (namedSource != null) Name = "Preprocessing: " + namedSource.Name;
    91 
    92       var dataSource = ExtractProblemData(source);
    93       Data = new FilteredPreprocessingData(new TransactionalPreprocessingData(dataSource));
     69    public void Import(IDataAnalysisProblemData problemData, IItem source = null) {
     70      if (problemData == null) throw new ArgumentNullException("problemData");
     71      if (source != null && ExtractProblemData(source) != problemData)
     72        throw new ArgumentException("The ProblemData extracted from the Source is different than the given ProblemData.");
     73      Source = source ?? problemData;
     74      var namedSource = Source as INamedItem;
     75      if (namedSource != null)
     76        Name = "Preprocessing " + namedSource.Name;
     77      Data = new FilteredPreprocessingData(new TransactionalPreprocessingData(problemData));
     78      OnReset();
     79      // Reset GUI:
     80      // - OnContentChanged for PreprocessingView!
     81      // event? task(async import)?
    9482    }
    95 
    9683    private IDataAnalysisProblemData ExtractProblemData(IItem source) {
    9784      var algorithm = source as Algorithm;
     
    10390
    10491    #region Export
    105     public IItem Export() {
    106       if (Source is IAlgorithm)
    107         return ExportAlgorithm((IAlgorithm)Source);
    108       if (Source is IDataAnalysisProblem)
    109         return ExportProblem((IDataAnalysisProblem)Source);
    110       if (Source is IDataAnalysisProblemData)
    111         return ExportProblemData((IDataAnalysisProblemData)Source);
    112       return null;
     92    public bool CanExport {
     93      get { return Source is IAlgorithm || Source is IDataAnalysisProblem || Source is IDataAnalysisProblemData; }
    11394    }
     95    public IEnumerable<KeyValuePair<string, Func<IItem>>> GetSourceExportOptions() {
     96      var algorithm = Source as IAlgorithm;
     97      if (algorithm != null)
     98        yield return new KeyValuePair<string, Func<IItem>>(
     99          algorithm.GetType().GetPrettyName(),
     100          () => ExportAlgorithm(algorithm));
     101
     102      var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : Source as IDataAnalysisProblem;
     103      if (problem != null)
     104        yield return new KeyValuePair<string, Func<IItem>>(
     105          problem.GetType().GetPrettyName(),
     106          () => ExportProblem(problem));
     107
     108      var problemData = problem != null ? problem.ProblemData : Source as IDataAnalysisProblemData;
     109      if (problemData != null)
     110        yield return new KeyValuePair<string, Func<IItem>>(
     111          problemData.GetType().GetPrettyName(),
     112          () => ExportProblemData(problemData));
     113    }
     114
    114115    private IAlgorithm ExportAlgorithm(IAlgorithm source) {
    115116      var preprocessedAlgorithm = (IAlgorithm)source.Clone();
     
    117118      preprocessedAlgorithm.Runs.Clear();
    118119      var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
    119       SetNewProblemData(problem);
     120      problem.ProblemDataParameter.ActualValue = CreateNewProblemData();
    120121      return preprocessedAlgorithm;
    121122    }
    122123    private IDataAnalysisProblem ExportProblem(IDataAnalysisProblem source) {
    123124      var preprocessedProblem = (IDataAnalysisProblem)source.Clone();
    124       SetNewProblemData(preprocessedProblem);
     125      preprocessedProblem.ProblemDataParameter.ActualValue = CreateNewProblemData();
    125126      return preprocessedProblem;
    126127    }
    127128    private IDataAnalysisProblemData ExportProblemData(IDataAnalysisProblemData source) {
     129      return CreateNewProblemData();
     130    }
     131
     132    public IDataAnalysisProblemData CreateNewProblemData() {
    128133      var creator = new ProblemDataCreator(this);
    129       var preprocessedProblemData = creator.CreateProblemData(source);
    130       preprocessedProblemData.Name = "Preprocessed " + source.Name;
    131       return preprocessedProblemData;
    132     }
    133     private void SetNewProblemData(IDataAnalysisProblem problem) {
    134       var data = ExtractProblemData(problem.ProblemData);
    135       problem.ProblemDataParameter.ActualValue = data;
    136       problem.Name = "Preprocessed " + problem.Name;
     134      var oldProblemData = ExtractProblemData(Source);
     135      var newProblemData = creator.CreateProblemData(oldProblemData);
     136      newProblemData.Name = "Preprocessed " + oldProblemData.Name;
     137      return newProblemData;
    137138    }
    138139    #endregion
     140
     141    protected virtual void OnReset() {
     142      if (Reset != null)
     143        Reset(this, EventArgs.Empty);
     144    }
    139145  }
    140146}
Note: See TracChangeset for help on using the changeset viewer.