Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11171 was 11171, checked in by ascheibe, 8 years ago

#2115 merged r11170 (copyright update) into trunk

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