Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisSolution.cs @ 11845

Last change on this file since 11845 was 11330, checked in by mkommend, 10 years ago

#2238: Removed unnecessary name changes event registration for result in DataAnalysisSolutions.

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