Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBProblem.cs @ 12951

Last change on this file since 12951 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

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