Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBProblem.cs @ 16674

Last change on this file since 16674 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

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