Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBRun.cs @ 7554

Last change on this file since 7554 was 7554, checked in by ascheibe, 12 years ago

#1174 improved access service handling to work with Hive

File size: 6.9 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(Name = "Stored")]
86    private bool StorableStored {
87      get { return stored; }
88      set { stored = value; }
89    }
90    [Storable(Name = "AlgorithmId")]
91    private long StorableAlgorithmId {
92      get { return algorithmId; }
93      set { algorithmId = value; }
94    }
95    [Storable(Name = "ProblemId")]
96    private long StorableProblemId {
97      get { return problemId; }
98      set { problemId = value; }
99    }
100    [Storable(Name = "CreatedDate")]
101    private DateTime StorableCreatedDate {
102      get { return createdDate; }
103      set { createdDate = value; }
104    }
105    #endregion
106
107    [StorableConstructor]
108    private OKBRun(bool deserializing) : base(deserializing) { }
109    private OKBRun(OKBRun original, Cloner cloner)
110      : base(original, cloner) {
111      algorithmId = original.algorithmId;
112      problemId = original.problemId;
113      createdDate = original.createdDate;
114      stored = original.stored;
115      UserId = original.UserId;
116    }
117    public OKBRun(long algorithmId, long problemId, IRun run, Guid userId)
118      : base(run) {
119      this.algorithmId = algorithmId;
120      this.problemId = problemId;
121      this.createdDate = DateTime.Now;
122      this.stored = false;
123      this.UserId = userId;
124    }
125
126    public override IDeepCloneable Clone(Cloner cloner) {
127      return new OKBRun(this, cloner);
128    }
129
130    public void Store() {
131      if (Stored) throw new InvalidOperationException("Cannot store already stored run.");
132
133      Run run = new Run();
134      run.AlgorithmId = algorithmId;
135      run.ProblemId = problemId;
136      //TODO: should there be some error handling? at this point it should already be checked that the user and client are registred
137      run.UserId = UserId;
138      run.ClientId = ClientInformation.Instance.ClientInfo.Id;
139      run.CreatedDate = createdDate;
140      run.ParameterValues = ConvertToValues(Parameters);
141      run.ResultValues = ConvertToValues(Results);
142      RunCreationClient.Instance.AddRun(run);
143
144      Stored = true;
145    }
146
147    #region Events
148    public event EventHandler Changed;
149    private void OnChanged() {
150      var handler = Changed;
151      if (handler != null) handler(this, EventArgs.Empty);
152    }
153    public event EventHandler StoredChanged;
154    private void OnStoredChanged() {
155      var handler = StoredChanged;
156      if (handler != null) handler(this, EventArgs.Empty);
157    }
158
159    protected override void RegisterWrappedItemEvents() {
160      base.RegisterWrappedItemEvents();
161      WrappedItem.Changed += new EventHandler(WrappedItem_Changed);
162    }
163    protected override void DeregisterWrappedItemEvents() {
164      WrappedItem.Changed -= new EventHandler(WrappedItem_Changed);
165      base.DeregisterWrappedItemEvents();
166    }
167
168    private void WrappedItem_Changed(object sender, EventArgs e) {
169      OnChanged();
170    }
171    #endregion
172
173    #region Helpers
174    List<Value> ConvertToValues(IDictionary<string, IItem> items) {
175      List<Value> values = new List<Value>();
176      foreach (var item in items) {
177        Value value;
178        if (item.Value is Data.BoolValue) {
179          BoolValue v = new BoolValue();
180          v.Value = ((Data.BoolValue)item.Value).Value;
181          value = v;
182        } else if (item.Value is Data.IntValue) {
183          IntValue v = new IntValue();
184          v.Value = ((Data.IntValue)item.Value).Value;
185          value = v;
186        } else if (item.Value is Data.DoubleValue) {
187          DoubleValue v = new DoubleValue();
188          v.Value = ((Data.DoubleValue)item.Value).Value;
189          value = v;
190        } else if (item.Value is Data.StringValue) {
191          StringValue v = new StringValue();
192          v.Value = ((Data.StringValue)item.Value).Value;
193          value = v;
194        } else {
195          BinaryValue v = new BinaryValue();
196          using (MemoryStream stream = new MemoryStream()) {
197            XmlGenerator.Serialize(item.Value, stream);
198            stream.Close();
199            v.Value = stream.ToArray();
200          }
201          value = v;
202        }
203        value.Name = item.Key;
204        value.DataType = new DataType();
205        value.DataType.Name = item.Value.GetType().Name;
206        value.DataType.TypeName = item.Value.GetType().AssemblyQualifiedName;
207        values.Add(value);
208      }
209      return values;
210    }
211    #endregion
212  }
213}
Note: See TracBrowser for help on using the repository browser.