Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBRun.cs @ 9426

Last change on this file since 9426 was 8055, checked in by ascheibe, 13 years ago

#1174 integrated okb client parts into trunk

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Clients.Access;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Persistence.Default.Xml;
32
33namespace HeuristicLab.Clients.OKB.RunCreation {
34  [Item("OKB Run", "The parameters and results of an algorithm run which are stored in the OKB.")]
35  [StorableClass]
36  public sealed class OKBRun : NamedItemWrapper<IRun>, IRun, IStorableContent {
37    public string Filename { get; set; }
38
39    public override Image ItemImage {
40      get { return Stored ? HeuristicLab.Common.Resources.VSImageLibrary.Database : HeuristicLab.Common.Resources.VSImageLibrary.DatabaseModified; }
41    }
42
43    private long algorithmId;
44    private long problemId;
45    private DateTime createdDate;
46
47    private bool stored;
48    public bool Stored {
49      get { return stored; }
50      private set {
51        if (value != stored) {
52          stored = value;
53          OnStoredChanged();
54          OnItemImageChanged();
55        }
56      }
57    }
58
59    public IAlgorithm Algorithm {
60      get { return WrappedItem.Algorithm; }
61    }
62    public IDictionary<string, IItem> Parameters {
63      get { return WrappedItem.Parameters; }
64    }
65    public IDictionary<string, IItem> Results {
66      get { return WrappedItem.Results; }
67    }
68    public IRun WrappedRun {
69      get { return WrappedItem; }
70    }
71
72    public Color Color {
73      get { return WrappedItem.Color; }
74      set { WrappedItem.Color = value; }
75    }
76    public bool Visible {
77      get { return WrappedItem.Visible; }
78      set { WrappedItem.Visible = value; }
79    }
80
81    #region Persistence Properties
82    [Storable]
83    private Guid UserId;
84
85    [Storable]
86    private Guid ClientId;
87
88    [Storable(Name = "Stored")]
89    private bool StorableStored {
90      get { return stored; }
91      set { stored = value; }
92    }
93    [Storable(Name = "AlgorithmId")]
94    private long StorableAlgorithmId {
95      get { return algorithmId; }
96      set { algorithmId = value; }
97    }
98    [Storable(Name = "ProblemId")]
99    private long StorableProblemId {
100      get { return problemId; }
101      set { problemId = value; }
102    }
103    [Storable(Name = "CreatedDate")]
104    private DateTime StorableCreatedDate {
105      get { return createdDate; }
106      set { createdDate = value; }
107    }
108    #endregion
109
110    [StorableConstructor]
111    private OKBRun(bool deserializing) : base(deserializing) { }
112    private OKBRun(OKBRun original, Cloner cloner)
113      : base(original, cloner) {
114      algorithmId = original.algorithmId;
115      problemId = original.problemId;
116      createdDate = original.createdDate;
117      stored = original.stored;
118      UserId = original.UserId;
119      ClientId = original.ClientId;
120    }
121    public OKBRun(long algorithmId, long problemId, IRun run, Guid userId)
122      : base(run) {
123      this.algorithmId = algorithmId;
124      this.problemId = problemId;
125      this.createdDate = DateTime.Now;
126      this.stored = false;
127      this.UserId = userId;
128      if (ClientInformation.Instance.ClientExists) {
129        this.ClientId = ClientInformation.Instance.ClientInfo.Id;
130      } else {
131        this.ClientId = Guid.Empty;
132      }
133    }
134
135    public override IDeepCloneable Clone(Cloner cloner) {
136      return new OKBRun(this, cloner);
137    }
138
139    public void Store() {
140      if (Stored) throw new InvalidOperationException("Cannot store already stored run.");
141      if (!ClientInformation.Instance.ClientExists) {
142        throw new MissingClientRegistrationException();
143      }
144
145      //if user has now registered his client...
146      if (ClientId == Guid.Empty) {
147        ClientId = ClientInformation.Instance.ClientInfo.Id;
148      }
149
150      Run run = new Run();
151      run.AlgorithmId = algorithmId;
152      run.ProblemId = problemId;
153      run.UserId = UserId;
154      run.ClientId = ClientId;
155      run.CreatedDate = createdDate;
156      run.ParameterValues = ConvertToValues(Parameters);
157      run.ResultValues = ConvertToValues(Results);
158      RunCreationClient.Instance.AddRun(run);
159
160      Stored = true;
161    }
162
163    #region Events
164    public event EventHandler Changed;
165    private void OnChanged() {
166      var handler = Changed;
167      if (handler != null) handler(this, EventArgs.Empty);
168    }
169    public event EventHandler StoredChanged;
170    private void OnStoredChanged() {
171      var handler = StoredChanged;
172      if (handler != null) handler(this, EventArgs.Empty);
173    }
174
175    protected override void RegisterWrappedItemEvents() {
176      base.RegisterWrappedItemEvents();
177      WrappedItem.Changed += new EventHandler(WrappedItem_Changed);
178    }
179    protected override void DeregisterWrappedItemEvents() {
180      WrappedItem.Changed -= new EventHandler(WrappedItem_Changed);
181      base.DeregisterWrappedItemEvents();
182    }
183
184    private void WrappedItem_Changed(object sender, EventArgs e) {
185      OnChanged();
186    }
187    #endregion
188
189    #region Helpers
190    List<Value> ConvertToValues(IDictionary<string, IItem> items) {
191      List<Value> values = new List<Value>();
192      bool add = true;
193      foreach (var item in items) {
194        Value value;
195        if (item.Value is Data.BoolValue) {
196          BoolValue v = new BoolValue();
197          v.Value = ((Data.BoolValue)item.Value).Value;
198          value = v;
199        } else if (item.Value is Data.IntValue) {
200          IntValue v = new IntValue();
201          v.Value = ((Data.IntValue)item.Value).Value;
202          value = v;
203        } else if (item.Value is Data.TimeSpanValue) {
204          TimeSpanValue v = new TimeSpanValue();
205          v.Value = (long)((Data.TimeSpanValue)item.Value).Value.TotalSeconds;
206          value = v;
207        } else if (item.Value is Data.PercentValue) {
208          PercentValue v = new PercentValue();
209          v.Value = ((Data.PercentValue)item.Value).Value;
210          value = v;
211          if (double.IsNaN(v.Value)) {
212            add = false;
213          }
214        } else if (item.Value is Data.DoubleValue) {
215          DoubleValue v = new DoubleValue();
216          v.Value = ((Data.DoubleValue)item.Value).Value;
217          value = v;
218          if (double.IsNaN(v.Value)) {
219            add = false;
220          }
221        } else if (item.Value is Data.StringValue) {
222          StringValue v = new StringValue();
223          v.Value = ((Data.StringValue)item.Value).Value;
224          value = v;
225        } else {
226          BinaryValue v = new BinaryValue();
227          using (MemoryStream stream = new MemoryStream()) {
228            XmlGenerator.Serialize(item.Value, stream);
229            stream.Close();
230            v.Value = stream.ToArray();
231          }
232          value = v;
233        }
234        if (add) {
235          value.Name = item.Key;
236          value.DataType = new DataType();
237          value.DataType.Name = item.Value.GetType().Name;
238          value.DataType.TypeName = item.Value.GetType().AssemblyQualifiedName;
239          values.Add(value);
240        } else {
241          add = true;
242        }
243      }
244      return values;
245    }
246    #endregion
247  }
248}
Note: See TracBrowser for help on using the repository browser.