source: trunk/sources/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBProblem.cs @ 13693

Last change on this file since 13693 was 13693, checked in by abeham, 6 years ago

#2588: Created view for okb solution

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