Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5909 was 5909, checked in by gkronber, 13 years ago

#1418 made data analysis solutions storable.

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 string Filename { get; set; }
38
39    public override Image ItemImage {
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(EventArgs.Empty);
51          }
52        }
53      }
54    }
55
56    public IDataAnalysisProblemData ProblemData {
57      get { return (IDataAnalysisProblemData)this[ProblemDataResultName].Value; }
58      protected 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(EventArgs.Empty);
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      IDataAnalysisModel modelClone = (IDataAnalysisModel)model.Clone();
83      IDataAnalysisProblemData problemDataClone = (IDataAnalysisProblemData)problemData.Clone();
84      Add(new Result(ModelResultName, "The symbolic data analysis model.", modelClone));
85      Add(new Result(ProblemDataResultName, "The symbolic data analysis problem data.", problemDataClone));
86
87      problemDataClone.Changed += new EventHandler(ProblemData_Changed);
88    }
89
90    private void ProblemData_Changed(object sender, EventArgs e) {
91      OnProblemDataChanged(e);
92    }
93
94    public event EventHandler ModelChanged;
95    protected virtual void OnModelChanged(EventArgs e) {
96      var listeners = ModelChanged;
97      if (listeners != null) listeners(this, e);
98    }
99
100    public event EventHandler ProblemDataChanged;
101    protected virtual void OnProblemDataChanged(EventArgs e) {
102      var listeners = ProblemDataChanged;
103      if (listeners != null) listeners(this, e);
104    }
105
106    #region INamedItem Members
107    [Storable]
108    protected string name;
109    public string Name {
110      get { return name; }
111      set {
112        if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
113        if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
114          CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
115          OnNameChanging(e);
116          if (!e.Cancel) {
117            name = value == null ? string.Empty : value;
118            OnNameChanged();
119          }
120        }
121      }
122    }
123    public virtual bool CanChangeName {
124      get { return true; }
125    }
126    [Storable]
127    protected string description;
128    public string Description {
129      get { return description; }
130      set {
131        if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
132        if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
133          description = value == null ? string.Empty : value;
134          OnDescriptionChanged();
135        }
136      }
137    }
138    public virtual bool CanChangeDescription {
139      get { return true; }
140    }
141
142    public override string ToString() {
143      return Name;
144    }
145
146    public event EventHandler<CancelEventArgs<string>> NameChanging;
147    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
148      var handler = NameChanging;
149      if (handler != null) handler(this, e);
150    }
151
152    public event EventHandler NameChanged;
153    protected virtual void OnNameChanged() {
154      var handler = NameChanged;
155      if (handler != null) handler(this, EventArgs.Empty);
156      OnToStringChanged();
157    }
158
159    public event EventHandler DescriptionChanged;
160    protected virtual void OnDescriptionChanged() {
161      var handler = DescriptionChanged;
162      if (handler != null) handler(this, EventArgs.Empty);
163    }
164    #endregion
165  }
166}
Note: See TracBrowser for help on using the repository browser.