Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBProblem.cs @ 5667

Last change on this file since 5667 was 5667, checked in by swagner, 12 years ago

Worked on OKB (#1174)

File size: 9.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System.IO;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Persistence.Default.Xml;
31
32namespace HeuristicLab.Clients.OKB.RunCreation {
33  [Item("OKB Problem", "A base class for problems which are stored in the OKB.")]
34  [StorableClass]
35  public abstract class OKBProblem : Item, IProblem {
36    public virtual Type ProblemType {
37      get { return typeof(IProblem); }
38    }
39    private long problemId;
40    public long ProblemId {
41      get { return problemId; }
42    }
43    private IProblem problem;
44    protected IProblem Problem {
45      get { return problem; }
46      private set {
47        if (value == null) throw new ArgumentNullException("Problem", "Problem cannot be null.");
48        if (value != problem) {
49          CancelEventArgs<string> e = new CancelEventArgs<string>(value.Name);
50          OnNameChanging(e);
51          if (!e.Cancel) {
52            DeregisterProblemEvents();
53            problem = value;
54            RegisterProblemEvents();
55
56            OnToStringChanged();
57            OnItemImageChanged();
58            OnNameChanged();
59            OnDescriptionChanged();
60            OnProblemChanged();
61            OnSolutionCreatorChanged();
62            OnEvaluatorChanged();
63            OnOperatorsChanged();
64            OnReset();
65          }
66        }
67      }
68    }
69
70    public override Image ItemImage {
71      get { return Problem.ItemImage; }
72    }
73
74    public string Name {
75      get { return Problem.Name; }
76      set { throw new NotSupportedException("Name cannot be changed."); }
77    }
78    public string Description {
79      get { return Problem.Description; }
80      set { throw new NotSupportedException("Description cannot be changed."); }
81    }
82    public bool CanChangeName {
83      get { return false; }
84    }
85    public bool CanChangeDescription {
86      get { return false; }
87    }
88
89    public IKeyedItemCollection<string, IParameter> Parameters {
90      get { return Problem.Parameters; }
91    }
92
93    public IParameter SolutionCreatorParameter {
94      get { return Problem.SolutionCreatorParameter; }
95    }
96    public ISolutionCreator SolutionCreator {
97      get { return Problem.SolutionCreator; }
98    }
99    public IParameter EvaluatorParameter {
100      get { return Problem.EvaluatorParameter; }
101    }
102    public IEvaluator Evaluator {
103      get { return Problem.Evaluator; }
104    }
105    public IEnumerable<IOperator> Operators {
106      get { return Problem.Operators; }
107    }
108
109    #region Persistence Properties
110    [Storable(Name = "ProblemId")]
111    private long StorableProblemId {
112      get { return problemId; }
113      set { problemId = value; }
114    }
115    [Storable(Name = "Problem")]
116    private IProblem StorableProblem {
117      get { return problem; }
118      set {
119        problem = value;
120        RegisterProblemEvents();
121      }
122    }
123    #endregion
124
125    [StorableConstructor]
126    protected OKBProblem(bool deserializing) : base(deserializing) { }
127    protected OKBProblem(OKBProblem original, Cloner cloner)
128      : base(original, cloner) {
129      problemId = original.problemId;
130      problem = cloner.Clone(original.problem);
131      RegisterProblemEvents();
132    }
133    protected OKBProblem(IProblem initialProblem)
134      : base() {
135      if (initialProblem == null) throw new ArgumentNullException("initialProblem", "Initial problem cannot be null.");
136      problemId = -1;
137      problem = initialProblem;
138      RegisterProblemEvents();
139    }
140
141    public void Load(long problemId) {
142      if (this.problemId != problemId) {
143        IProblem problem;
144        byte[] problemData = RunCreationClient.GetProblemData(problemId);
145        using (MemoryStream stream = new MemoryStream(problemData)) {
146          problem = XmlParser.Deserialize<IProblem>(stream);
147        }
148        if (ProblemType.IsAssignableFrom(problem.GetType())) {
149          this.problemId = problemId;
150          Problem = problem;
151        }
152      }
153    }
154
155    public IProblem CloneProblem() {
156      return (IProblem)Problem.Clone();
157    }
158
159    public void CollectParameterValues(IDictionary<string, IItem> values) {
160      Problem.CollectParameterValues(values);
161    }
162
163    #region Events
164    public event EventHandler ProblemChanged;
165    protected virtual void OnProblemChanged() {
166      EventHandler handler = ProblemChanged;
167      if (handler != null) handler(this, EventArgs.Empty);
168    }
169    public event EventHandler<CancelEventArgs<string>> NameChanging;
170    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
171      var handler = NameChanging;
172      if (handler != null) handler(this, e);
173    }
174    public event EventHandler NameChanged;
175    protected virtual void OnNameChanged() {
176      var handler = NameChanged;
177      if (handler != null) handler(this, EventArgs.Empty);
178    }
179    public event EventHandler DescriptionChanged;
180    protected virtual void OnDescriptionChanged() {
181      var handler = DescriptionChanged;
182      if (handler != null) handler(this, EventArgs.Empty);
183    }
184    public event EventHandler SolutionCreatorChanged;
185    protected virtual void OnSolutionCreatorChanged() {
186      var handler = SolutionCreatorChanged;
187      if (handler != null) handler(this, EventArgs.Empty);
188    }
189    public event EventHandler EvaluatorChanged;
190    protected virtual void OnEvaluatorChanged() {
191      var handler = EvaluatorChanged;
192      if (handler != null) handler(this, EventArgs.Empty);
193    }
194    public event EventHandler OperatorsChanged;
195    protected virtual void OnOperatorsChanged() {
196      var handler = OperatorsChanged;
197      if (handler != null) handler(this, EventArgs.Empty);
198    }
199    public event EventHandler Reset;
200    protected virtual void OnReset() {
201      var handler = Reset;
202      if (handler != null) handler(this, EventArgs.Empty);
203    }
204
205    protected virtual void RegisterProblemEvents() {
206      if (Problem != null) {
207        Problem.ToStringChanged += new EventHandler(Problem_ToStringChanged);
208        Problem.ItemImageChanged += new EventHandler(Problem_ItemImageChanged);
209        Problem.NameChanging += new EventHandler<CancelEventArgs<string>>(Problem_NameChanging);
210        Problem.NameChanged += new EventHandler(Problem_NameChanged);
211        Problem.DescriptionChanged += new EventHandler(Problem_DescriptionChanged);
212        Problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
213        Problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
214        Problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
215        Problem.Reset += new EventHandler(Problem_Reset);
216      }
217    }
218    protected virtual void DeregisterProblemEvents() {
219      if (Problem != null) {
220        Problem.ToStringChanged -= new EventHandler(Problem_ToStringChanged);
221        Problem.ItemImageChanged -= new EventHandler(Problem_ItemImageChanged);
222        Problem.NameChanging -= new EventHandler<CancelEventArgs<string>>(Problem_NameChanging);
223        Problem.NameChanged -= new EventHandler(Problem_NameChanged);
224        Problem.DescriptionChanged -= new EventHandler(Problem_DescriptionChanged);
225        Problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
226        Problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
227        Problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
228        Problem.Reset -= new EventHandler(Problem_Reset);
229      }
230    }
231
232    private void Problem_ToStringChanged(object sender, EventArgs e) {
233      OnToStringChanged();
234    }
235    private void Problem_ItemImageChanged(object sender, EventArgs e) {
236      OnItemImageChanged();
237    }
238    private void Problem_NameChanging(object sender, CancelEventArgs<string> e) {
239      OnNameChanging(e);
240    }
241    private void Problem_NameChanged(object sender, EventArgs e) {
242      OnNameChanged();
243    }
244    private void Problem_DescriptionChanged(object sender, EventArgs e) {
245      OnDescriptionChanged();
246    }
247    private void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
248      OnSolutionCreatorChanged();
249    }
250    private void Problem_EvaluatorChanged(object sender, EventArgs e) {
251      OnEvaluatorChanged();
252    }
253    private void Problem_OperatorsChanged(object sender, EventArgs e) {
254      OnOperatorsChanged();
255    }
256    private void Problem_Reset(object sender, EventArgs e) {
257      OnReset();
258    }
259    #endregion
260  }
261}
Note: See TracBrowser for help on using the repository browser.