Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBProblem.cs @ 17334

Last change on this file since 17334 was 17334, checked in by mkommend, 4 years ago

#2521: Moved IStorableContent from generic problem implementation to IProblem.

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