Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5894 was 5880, checked in by gkronber, 14 years ago

#1418 added missing cloning of symbolic expression trees and problem data in data analysis solutions.

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