Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5667 was 5667, checked in by swagner, 12 years ago

Worked on OKB (#1174)

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System.Linq;
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    private long algorithmId;
40    private long problemId;
41    private DateTime createdDate;
42    private bool stored;
43    public bool Stored {
44      get { return stored; }
45    }
46
47    public IAlgorithm Algorithm {
48      get { return WrappedItem.Algorithm; }
49    }
50    public IDictionary<string, IItem> Parameters {
51      get { return WrappedItem.Parameters; }
52    }
53    public IDictionary<string, IItem> Results {
54      get { return WrappedItem.Results; }
55    }
56
57    public Color Color {
58      get { return WrappedItem.Color; }
59      set { WrappedItem.Color = value; }
60    }
61    public bool Visible {
62      get { return WrappedItem.Visible; }
63      set { WrappedItem.Visible = value; }
64    }
65
66    #region Persistence Properties
67    [Storable(Name = "Stored")]
68    private bool StorableStored {
69      get { return stored; }
70      set { stored = value; }
71    }
72    #endregion
73
74    [StorableConstructor]
75    private OKBRun(bool deserializing) : base(deserializing) { }
76    private OKBRun(OKBRun original, Cloner cloner)
77      : base(original, cloner) {
78      stored = original.stored;
79    }
80    public OKBRun(long algorithmId, long problemId, IRun run)
81      : base(run) {
82      this.algorithmId = algorithmId;
83      this.problemId = problemId;
84      this.createdDate = DateTime.Now;
85      this.stored = false;
86    }
87
88    public override IDeepCloneable Clone(Cloner cloner) {
89      return new OKBRun(this, cloner);
90    }
91
92    public void Store() {
93      if (stored) throw new InvalidOperationException("Cannot store already stored run.");
94
95      Run run = new Run();
96      run.AlgorithmId = algorithmId;
97      run.ProblemId = problemId;
98      run.UserId = Guid.Empty;
99      run.ClientId = Guid.Empty;
100      run.CreatedDate = createdDate;
101      run.RandomSeed = Parameters.Where(x => (x.Key == "Seed") && (x.Value is Data.IntValue)).Select(x => ((Data.IntValue)x.Value).Value).FirstOrDefault();
102      run.ParameterValues = ConvertToValues(Parameters);
103      run.ResultValues = ConvertToValues(Results);
104      RunCreationClient.Instance.AddRun(run);
105
106      stored = true;
107    }
108
109    #region Events
110    public event EventHandler Changed;
111    private void OnChanged() {
112      var handler = Changed;
113      if (handler != null) handler(this, EventArgs.Empty);
114    }
115
116    protected override void RegisterWrappedItemEvents() {
117      base.RegisterWrappedItemEvents();
118      WrappedItem.Changed += new EventHandler(WrappedItem_Changed);
119    }
120    protected override void DeregisterWrappedItemEvents() {
121      WrappedItem.Changed -= new EventHandler(WrappedItem_Changed);
122      base.DeregisterWrappedItemEvents();
123    }
124
125    private void WrappedItem_Changed(object sender, EventArgs e) {
126      OnChanged();
127    }
128    #endregion
129
130    #region Helpers
131    List<Value> ConvertToValues(IDictionary<string, IItem> items) {
132      List<Value> values = new List<Value>();
133      foreach (var item in items) {
134        Value value;
135        if (item.Value is Data.BoolValue) {
136          BoolValue v = new BoolValue();
137          v.Value = ((Data.BoolValue)item.Value).Value;
138          value = v;
139        } else if (item.Value is Data.IntValue) {
140          IntValue v = new IntValue();
141          v.Value = ((Data.IntValue)item.Value).Value;
142          value = v;
143        } else if (item.Value is Data.DoubleValue) {
144          DoubleValue v = new DoubleValue();
145          v.Value = ((Data.DoubleValue)item.Value).Value;
146          value = v;
147        } else if (item.Value is Data.StringValue) {
148          StringValue v = new StringValue();
149          v.Value = ((Data.StringValue)item.Value).Value;
150          value = v;
151        } else if (item.Value is Data.IntValue) {
152          IntValue v = new IntValue();
153          v.Value = ((Data.IntValue)item.Value).Value;
154          value = v;
155        } else {
156          BinaryValue v = new BinaryValue();
157          using (MemoryStream stream = new MemoryStream()) {
158            XmlGenerator.Serialize(item.Value, stream);
159            stream.Close();
160            v.Value = stream.ToArray();
161          }
162          value = v;
163        }
164        value.Name = item.Key;
165        value.DataType = new DataType();
166        value.DataType.Name = item.Value.GetType().Name;
167        value.DataType.TypeName = item.Value.GetType().AssemblyQualifiedName;
168        values.Add(value);
169      }
170      return values;
171    }
172    #endregion
173  }
174}
Note: See TracBrowser for help on using the repository browser.