Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBRun.cs @ 14082

Last change on this file since 14082 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

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