1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Optimization;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace 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 |
|
---|
40 |
|
---|
41 | #region properties
|
---|
42 | public IDataAnalysisModel Model
|
---|
43 | {
|
---|
44 | get { return (IDataAnalysisModel)this[ModelResultName].Value; }
|
---|
45 | protected set
|
---|
46 | {
|
---|
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 | {
|
---|
58 | get { return (IDataAnalysisProblemData)this[ProblemDataResultName].Value; }
|
---|
59 | set
|
---|
60 | {
|
---|
61 | if (this[ProblemDataResultName].Value != value) {
|
---|
62 | if (value != null) {
|
---|
63 | ProblemData.Changed -= new EventHandler(ProblemData_Changed);
|
---|
64 | this[ProblemDataResultName].Value = value;
|
---|
65 | ProblemData.Changed += new EventHandler(ProblemData_Changed);
|
---|
66 | OnProblemDataChanged();
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | #endregion
|
---|
72 |
|
---|
73 | [StorableConstructor]
|
---|
74 | protected DataAnalysisSolution(bool deserializing) : base(deserializing) { }
|
---|
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 | {
|
---|
119 | get { return name; }
|
---|
120 | set
|
---|
121 | {
|
---|
122 | if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
|
---|
123 | if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
|
---|
124 | CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
|
---|
125 | OnNameChanging(e);
|
---|
126 | if (!e.Cancel) {
|
---|
127 | name = value == null ? string.Empty : value;
|
---|
128 | OnNameChanged();
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | public virtual bool CanChangeName
|
---|
134 | {
|
---|
135 | get { return true; }
|
---|
136 | }
|
---|
137 | [Storable]
|
---|
138 | protected string description;
|
---|
139 | public string Description
|
---|
140 | {
|
---|
141 | get { return description; }
|
---|
142 | set
|
---|
143 | {
|
---|
144 | if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
|
---|
145 | if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
|
---|
146 | description = value == null ? string.Empty : value;
|
---|
147 | OnDescriptionChanged();
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 | public virtual bool CanChangeDescription
|
---|
152 | {
|
---|
153 | get { return true; }
|
---|
154 | }
|
---|
155 |
|
---|
156 | public override string ToString() {
|
---|
157 | return Name;
|
---|
158 | }
|
---|
159 |
|
---|
160 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
161 | protected virtual void OnNameChanging(CancelEventArgs<string> e) {
|
---|
162 | var handler = NameChanging;
|
---|
163 | if (handler != null) handler(this, e);
|
---|
164 | }
|
---|
165 |
|
---|
166 | public event EventHandler NameChanged;
|
---|
167 | protected virtual void OnNameChanged() {
|
---|
168 | var handler = NameChanged;
|
---|
169 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
170 | OnToStringChanged();
|
---|
171 | }
|
---|
172 |
|
---|
173 | public event EventHandler DescriptionChanged;
|
---|
174 | protected virtual void OnDescriptionChanged() {
|
---|
175 | var handler = DescriptionChanged;
|
---|
176 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
177 | }
|
---|
178 | #endregion
|
---|
179 | }
|
---|
180 | }
|
---|