Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisSolution.cs @ 17912

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

#2875: Removed years in copyrights

File size: 6.2 KB
RevLine 
[5620]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5620]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
[5777]22using System;
[11330]23using System.Collections.Generic;
[5777]24using System.Drawing;
[5620]25using HeuristicLab.Common;
[5777]26using HeuristicLab.Optimization;
[16565]27using HEAL.Attic;
[5620]28
29namespace HeuristicLab.Problems.DataAnalysis {
30  /// <summary>
31  /// Abstract base class for data analysis solutions
32  /// </summary>
[16565]33  [StorableType("339E0EAD-07D7-44C5-8E1D-AE9B2AA9A67D")]
[5620]34  public abstract class DataAnalysisSolution : ResultCollection, IDataAnalysisSolution {
35    private const string ModelResultName = "Model";
36    private const string ProblemDataResultName = "ProblemData";
[5624]37
[5909]38    public string Filename { get; set; }
39
[7201]40    public static new Image StaticItemImage {
[5624]41      get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
42    }
43
[5620]44    #region properties
45    public IDataAnalysisModel Model {
46      get { return (IDataAnalysisModel)this[ModelResultName].Value; }
[5717]47      protected set {
48        if (this[ModelResultName].Value != value) {
49          if (value != null) {
50            this[ModelResultName].Value = value;
[6606]51            OnModelChanged();
[5717]52          }
53        }
54      }
[5620]55    }
56
57    public IDataAnalysisProblemData ProblemData {
58      get { return (IDataAnalysisProblemData)this[ProblemDataResultName].Value; }
[6653]59      set {
[16244]60        if (value == null) throw new ArgumentNullException("The problemData must not be null.");
61        if (this[ProblemDataResultName].Value == value) return;
62        string errorMessage = string.Empty;
63        if (!Model.IsProblemDataCompatible(value, out errorMessage)) throw new ArgumentException(errorMessage);
64
65        ProblemData.Changed -= new EventHandler(ProblemData_Changed);
66        this[ProblemDataResultName].Value = value;
67        ProblemData.Changed += new EventHandler(ProblemData_Changed);
68        OnProblemDataChanged();
[5717]69      }
[5620]70    }
71    #endregion
72
73    [StorableConstructor]
[16565]74    protected DataAnalysisSolution(StorableConstructorFlag _) : base(_) { }
[5620]75    protected DataAnalysisSolution(DataAnalysisSolution original, Cloner cloner)
76      : base(original, cloner) {
77      name = original.Name;
78      description = original.Description;
79    }
[5624]80    public DataAnalysisSolution(IDataAnalysisModel model, IDataAnalysisProblemData problemData)
[5620]81      : base() {
[5649]82      name = ItemName;
83      description = ItemDescription;
[6588]84      Add(new Result(ModelResultName, "The data analysis model.", model));
85      Add(new Result(ProblemDataResultName, "The data analysis problem data.", problemData));
[5624]86
[5914]87      problemData.Changed += new EventHandler(ProblemData_Changed);
[5620]88    }
89
[6411]90    protected abstract void RecalculateResults();
91
[5624]92    private void ProblemData_Changed(object sender, EventArgs e) {
[6606]93      OnProblemDataChanged();
[5624]94    }
95
[5620]96    public event EventHandler ModelChanged;
[6606]97    protected virtual void OnModelChanged() {
[6411]98      RecalculateResults();
[5620]99      var listeners = ModelChanged;
[6606]100      if (listeners != null) listeners(this, EventArgs.Empty);
[5620]101    }
102
103    public event EventHandler ProblemDataChanged;
[6606]104    protected virtual void OnProblemDataChanged() {
[6411]105      RecalculateResults();
[5620]106      var listeners = ProblemDataChanged;
[6606]107      if (listeners != null) listeners(this, EventArgs.Empty);
[5620]108    }
109
[11330]110    //mkommend avoid unnecessary event registration for result name changes
111    protected override void RegisterItemEvents(IEnumerable<IResult> items) { }
112    protected override void DeregisterItemEvents(IEnumerable<IResult> items) { }
113
[5620]114    #region INamedItem Members
115    [Storable]
116    protected string name;
117    public string Name {
118      get { return name; }
119      set {
120        if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
121        if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
122          CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
123          OnNameChanging(e);
124          if (!e.Cancel) {
125            name = value == null ? string.Empty : value;
126            OnNameChanged();
127          }
128        }
129      }
130    }
131    public virtual bool CanChangeName {
132      get { return true; }
133    }
134    [Storable]
135    protected string description;
136    public string Description {
137      get { return description; }
138      set {
139        if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
140        if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
141          description = value == null ? string.Empty : value;
142          OnDescriptionChanged();
143        }
144      }
145    }
146    public virtual bool CanChangeDescription {
147      get { return true; }
148    }
149
150    public override string ToString() {
151      return Name;
152    }
153
154    public event EventHandler<CancelEventArgs<string>> NameChanging;
155    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
156      var handler = NameChanging;
157      if (handler != null) handler(this, e);
158    }
159
160    public event EventHandler NameChanged;
161    protected virtual void OnNameChanged() {
162      var handler = NameChanged;
163      if (handler != null) handler(this, EventArgs.Empty);
164      OnToStringChanged();
165    }
166
167    public event EventHandler DescriptionChanged;
168    protected virtual void OnDescriptionChanged() {
169      var handler = DescriptionChanged;
170      if (handler != null) handler(this, EventArgs.Empty);
171    }
172    #endregion
173  }
174}
Note: See TracBrowser for help on using the repository browser.