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
RevLine 
[5355]1#region License Information
2/* HeuristicLab
[7331]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5355]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;
[5667]25using System.IO;
[7535]26using HeuristicLab.Clients.Access;
[5355]27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[5667]31using HeuristicLab.Persistence.Default.Xml;
[5355]32
[5640]33namespace HeuristicLab.Clients.OKB.RunCreation {
[5355]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
[5902]39    public override Image ItemImage {
40      get { return Stored ? HeuristicLab.Common.Resources.VSImageLibrary.Database : HeuristicLab.Common.Resources.VSImageLibrary.DatabaseModified; }
41    }
42
[5667]43    private long algorithmId;
44    private long problemId;
45    private DateTime createdDate;
[5902]46
[5667]47    private bool stored;
48    public bool Stored {
49      get { return stored; }
[5902]50      private set {
51        if (value != stored) {
52          stored = value;
53          OnStoredChanged();
54          OnItemImageChanged();
55        }
56      }
[5355]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    }
[5902]68    public IRun WrappedRun {
69      get { return WrappedItem; }
70    }
[5355]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
[7554]82    [Storable]
83    private Guid UserId;
84
[5667]85    [Storable(Name = "Stored")]
86    private bool StorableStored {
87      get { return stored; }
88      set { stored = value; }
[5355]89    }
[5902]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    }
[5355]105    #endregion
106
107    [StorableConstructor]
108    private OKBRun(bool deserializing) : base(deserializing) { }
109    private OKBRun(OKBRun original, Cloner cloner)
110      : base(original, cloner) {
[5902]111      algorithmId = original.algorithmId;
112      problemId = original.problemId;
113      createdDate = original.createdDate;
[5667]114      stored = original.stored;
[7554]115      UserId = original.UserId;
[5355]116    }
[7554]117    public OKBRun(long algorithmId, long problemId, IRun run, Guid userId)
[5355]118      : base(run) {
[5667]119      this.algorithmId = algorithmId;
120      this.problemId = problemId;
121      this.createdDate = DateTime.Now;
122      this.stored = false;
[7554]123      this.UserId = userId;
[5355]124    }
125
126    public override IDeepCloneable Clone(Cloner cloner) {
127      return new OKBRun(this, cloner);
128    }
129
[5667]130    public void Store() {
[5902]131      if (Stored) throw new InvalidOperationException("Cannot store already stored run.");
[5667]132
133      Run run = new Run();
134      run.AlgorithmId = algorithmId;
135      run.ProblemId = problemId;
[7535]136      //TODO: should there be some error handling? at this point it should already be checked that the user and client are registred
[7554]137      run.UserId = UserId;
[7535]138      run.ClientId = ClientInformation.Instance.ClientInfo.Id;
[5667]139      run.CreatedDate = createdDate;
140      run.ParameterValues = ConvertToValues(Parameters);
141      run.ResultValues = ConvertToValues(Results);
142      RunCreationClient.Instance.AddRun(run);
143
[5902]144      Stored = true;
[5667]145    }
146
[5355]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    }
[5902]153    public event EventHandler StoredChanged;
154    private void OnStoredChanged() {
155      var handler = StoredChanged;
156      if (handler != null) handler(this, EventArgs.Empty);
157    }
[5355]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
[5667]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
[5355]212  }
213}
Note: See TracBrowser for help on using the repository browser.