Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBProblem.cs @ 14853

Last change on this file since 14853 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 9.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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    public IEnumerable<IParameterizedItem> ExecutionContextItems { get { return new[] { this }; } }
114
115    #region Persistence Properties
116    [Storable(Name = "ProblemId")]
117    private long StorableProblemId {
118      get { return problemId; }
119      set { problemId = value; }
120    }
121    [Storable(Name = "Problem")]
122    private IHeuristicOptimizationProblem StorableProblem {
123      get { return problem; }
124      set {
125        problem = value;
126        RegisterProblemEvents();
127      }
128    }
129    #endregion
130
131    [StorableConstructor]
132    protected OKBProblem(bool deserializing) : base(deserializing) { }
133    protected OKBProblem(OKBProblem original, Cloner cloner)
134      : base(original, cloner) {
135      problemId = original.problemId;
136      problem = cloner.Clone(original.problem);
137      RegisterProblemEvents();
138    }
139    protected OKBProblem(IHeuristicOptimizationProblem initialProblem)
140      : base() {
141      if (initialProblem == null) throw new ArgumentNullException("initialProblem", "Initial problem cannot be null.");
142      problemId = -1;
143      problem = initialProblem;
144      RegisterProblemEvents();
145    }
146
147    public void Load(long problemId) {
148      if (this.problemId != problemId) {
149        IHeuristicOptimizationProblem problem;
150        byte[] problemData = RunCreationClient.GetProblemData(problemId);
151        using (MemoryStream stream = new MemoryStream(problemData)) {
152          problem = XmlParser.Deserialize<IHeuristicOptimizationProblem>(stream);
153        }
154        if (ProblemType.IsAssignableFrom(problem.GetType())) {
155          this.problemId = problemId;
156          Problem = problem;
157        }
158      }
159    }
160
161    public IProblem CloneProblem() {
162      return (IProblem)Problem.Clone();
163    }
164
165    public void CollectParameterValues(IDictionary<string, IItem> values) {
166      Problem.CollectParameterValues(values);
167    }
168
169    #region Events
170    public event EventHandler ProblemChanged;
171    protected virtual void OnProblemChanged() {
172      EventHandler handler = ProblemChanged;
173      if (handler != null) handler(this, EventArgs.Empty);
174    }
175    public event EventHandler<CancelEventArgs<string>> NameChanging;
176    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
177      var handler = NameChanging;
178      if (handler != null) handler(this, e);
179    }
180    public event EventHandler NameChanged;
181    protected virtual void OnNameChanged() {
182      var handler = NameChanged;
183      if (handler != null) handler(this, EventArgs.Empty);
184    }
185    public event EventHandler DescriptionChanged;
186    protected virtual void OnDescriptionChanged() {
187      var handler = DescriptionChanged;
188      if (handler != null) handler(this, EventArgs.Empty);
189    }
190    public event EventHandler SolutionCreatorChanged;
191    protected virtual void OnSolutionCreatorChanged() {
192      var handler = SolutionCreatorChanged;
193      if (handler != null) handler(this, EventArgs.Empty);
194    }
195    public event EventHandler EvaluatorChanged;
196    protected virtual void OnEvaluatorChanged() {
197      var handler = EvaluatorChanged;
198      if (handler != null) handler(this, EventArgs.Empty);
199    }
200    public event EventHandler OperatorsChanged;
201    protected virtual void OnOperatorsChanged() {
202      var handler = OperatorsChanged;
203      if (handler != null) handler(this, EventArgs.Empty);
204    }
205    public event EventHandler Reset;
206    protected virtual void OnReset() {
207      var handler = Reset;
208      if (handler != null) handler(this, EventArgs.Empty);
209    }
210
211    protected virtual void RegisterProblemEvents() {
212      if (Problem != null) {
213        Problem.ToStringChanged += new EventHandler(Problem_ToStringChanged);
214        Problem.ItemImageChanged += new EventHandler(Problem_ItemImageChanged);
215        Problem.NameChanging += new EventHandler<CancelEventArgs<string>>(Problem_NameChanging);
216        Problem.NameChanged += new EventHandler(Problem_NameChanged);
217        Problem.DescriptionChanged += new EventHandler(Problem_DescriptionChanged);
218        Problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
219        Problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
220        Problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
221        Problem.Reset += new EventHandler(Problem_Reset);
222      }
223    }
224    protected virtual void DeregisterProblemEvents() {
225      if (Problem != null) {
226        Problem.ToStringChanged -= new EventHandler(Problem_ToStringChanged);
227        Problem.ItemImageChanged -= new EventHandler(Problem_ItemImageChanged);
228        Problem.NameChanging -= new EventHandler<CancelEventArgs<string>>(Problem_NameChanging);
229        Problem.NameChanged -= new EventHandler(Problem_NameChanged);
230        Problem.DescriptionChanged -= new EventHandler(Problem_DescriptionChanged);
231        Problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
232        Problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
233        Problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
234        Problem.Reset -= new EventHandler(Problem_Reset);
235      }
236    }
237
238    private void Problem_ToStringChanged(object sender, EventArgs e) {
239      OnToStringChanged();
240    }
241    private void Problem_ItemImageChanged(object sender, EventArgs e) {
242      OnItemImageChanged();
243    }
244    private void Problem_NameChanging(object sender, CancelEventArgs<string> e) {
245      OnNameChanging(e);
246    }
247    private void Problem_NameChanged(object sender, EventArgs e) {
248      OnNameChanged();
249    }
250    private void Problem_DescriptionChanged(object sender, EventArgs e) {
251      OnDescriptionChanged();
252    }
253    private void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
254      OnSolutionCreatorChanged();
255    }
256    private void Problem_EvaluatorChanged(object sender, EventArgs e) {
257      OnEvaluatorChanged();
258    }
259    private void Problem_OperatorsChanged(object sender, EventArgs e) {
260      OnOperatorsChanged();
261    }
262    private void Problem_Reset(object sender, EventArgs e) {
263      OnReset();
264    }
265    #endregion
266  }
267}
Note: See TracBrowser for help on using the repository browser.