Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisSolution.cs @ 16722

Last change on this file since 16722 was 16559, checked in by jkarder, 6 years ago

#2520: renamed Fossil to Attic and set version to 1.0.0-pre01

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  /// <summary>
31  /// Abstract base class for data analysis solutions
32  /// </summary>
33  [StorableType("339E0EAD-07D7-44C5-8E1D-AE9B2AA9A67D")]
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 (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();
69      }
70    }
71    #endregion
72
73    [StorableConstructor]
74    protected DataAnalysisSolution(StorableConstructorFlag _) : base(_) { }
75    protected DataAnalysisSolution(DataAnalysisSolution original, Cloner cloner)
76      : base(original, cloner) {
77      name = original.Name;
78      description = original.Description;
79    }
80    public DataAnalysisSolution(IDataAnalysisModel model, IDataAnalysisProblemData problemData)
81      : base() {
82      name = ItemName;
83      description = ItemDescription;
84      Add(new Result(ModelResultName, "The data analysis model.", model));
85      Add(new Result(ProblemDataResultName, "The data analysis problem data.", problemData));
86
87      problemData.Changed += new EventHandler(ProblemData_Changed);
88    }
89
90    protected abstract void RecalculateResults();
91
92    private void ProblemData_Changed(object sender, EventArgs e) {
93      OnProblemDataChanged();
94    }
95
96    public event EventHandler ModelChanged;
97    protected virtual void OnModelChanged() {
98      RecalculateResults();
99      var listeners = ModelChanged;
100      if (listeners != null) listeners(this, EventArgs.Empty);
101    }
102
103    public event EventHandler ProblemDataChanged;
104    protected virtual void OnProblemDataChanged() {
105      RecalculateResults();
106      var listeners = ProblemDataChanged;
107      if (listeners != null) listeners(this, EventArgs.Empty);
108    }
109
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
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.