1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.IO;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Persistence.Default.Xml;
|
---|
32 |
|
---|
33 | namespace 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(Name = "Stored")]
|
---|
83 | private bool StorableStored {
|
---|
84 | get { return stored; }
|
---|
85 | set { stored = value; }
|
---|
86 | }
|
---|
87 | [Storable(Name = "AlgorithmId")]
|
---|
88 | private long StorableAlgorithmId {
|
---|
89 | get { return algorithmId; }
|
---|
90 | set { algorithmId = value; }
|
---|
91 | }
|
---|
92 | [Storable(Name = "ProblemId")]
|
---|
93 | private long StorableProblemId {
|
---|
94 | get { return problemId; }
|
---|
95 | set { problemId = value; }
|
---|
96 | }
|
---|
97 | [Storable(Name = "CreatedDate")]
|
---|
98 | private DateTime StorableCreatedDate {
|
---|
99 | get { return createdDate; }
|
---|
100 | set { createdDate = value; }
|
---|
101 | }
|
---|
102 | #endregion
|
---|
103 |
|
---|
104 | [StorableConstructor]
|
---|
105 | private OKBRun(bool deserializing) : base(deserializing) { }
|
---|
106 | private OKBRun(OKBRun original, Cloner cloner)
|
---|
107 | : base(original, cloner) {
|
---|
108 | algorithmId = original.algorithmId;
|
---|
109 | problemId = original.problemId;
|
---|
110 | createdDate = original.createdDate;
|
---|
111 | stored = original.stored;
|
---|
112 | }
|
---|
113 | public OKBRun(long algorithmId, long problemId, IRun run)
|
---|
114 | : base(run) {
|
---|
115 | this.algorithmId = algorithmId;
|
---|
116 | this.problemId = problemId;
|
---|
117 | this.createdDate = DateTime.Now;
|
---|
118 | this.stored = false;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
122 | return new OKBRun(this, cloner);
|
---|
123 | }
|
---|
124 |
|
---|
125 | public void Store() {
|
---|
126 | if (Stored) throw new InvalidOperationException("Cannot store already stored run.");
|
---|
127 |
|
---|
128 | Run run = new Run();
|
---|
129 | run.AlgorithmId = algorithmId;
|
---|
130 | run.ProblemId = problemId;
|
---|
131 | run.UserId = Guid.Empty;
|
---|
132 | run.ClientId = Guid.Empty;
|
---|
133 | run.CreatedDate = createdDate;
|
---|
134 | run.RandomSeed = Parameters.Where(x => (x.Key == "Seed") && (x.Value is Data.IntValue)).Select(x => ((Data.IntValue)x.Value).Value).FirstOrDefault();
|
---|
135 | run.ParameterValues = ConvertToValues(Parameters);
|
---|
136 | run.ResultValues = ConvertToValues(Results);
|
---|
137 | RunCreationClient.Instance.AddRun(run);
|
---|
138 |
|
---|
139 | Stored = true;
|
---|
140 | }
|
---|
141 |
|
---|
142 | #region Events
|
---|
143 | public event EventHandler Changed;
|
---|
144 | private void OnChanged() {
|
---|
145 | var handler = Changed;
|
---|
146 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
147 | }
|
---|
148 | public event EventHandler StoredChanged;
|
---|
149 | private void OnStoredChanged() {
|
---|
150 | var handler = StoredChanged;
|
---|
151 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
152 | }
|
---|
153 |
|
---|
154 | protected override void RegisterWrappedItemEvents() {
|
---|
155 | base.RegisterWrappedItemEvents();
|
---|
156 | WrappedItem.Changed += new EventHandler(WrappedItem_Changed);
|
---|
157 | }
|
---|
158 | protected override void DeregisterWrappedItemEvents() {
|
---|
159 | WrappedItem.Changed -= new EventHandler(WrappedItem_Changed);
|
---|
160 | base.DeregisterWrappedItemEvents();
|
---|
161 | }
|
---|
162 |
|
---|
163 | private void WrappedItem_Changed(object sender, EventArgs e) {
|
---|
164 | OnChanged();
|
---|
165 | }
|
---|
166 | #endregion
|
---|
167 |
|
---|
168 | #region Helpers
|
---|
169 | List<Value> ConvertToValues(IDictionary<string, IItem> items) {
|
---|
170 | List<Value> values = new List<Value>();
|
---|
171 | foreach (var item in items) {
|
---|
172 | Value value;
|
---|
173 | if (item.Value is Data.BoolValue) {
|
---|
174 | BoolValue v = new BoolValue();
|
---|
175 | v.Value = ((Data.BoolValue)item.Value).Value;
|
---|
176 | value = v;
|
---|
177 | } else if (item.Value is Data.IntValue) {
|
---|
178 | IntValue v = new IntValue();
|
---|
179 | v.Value = ((Data.IntValue)item.Value).Value;
|
---|
180 | value = v;
|
---|
181 | } else if (item.Value is Data.DoubleValue) {
|
---|
182 | DoubleValue v = new DoubleValue();
|
---|
183 | v.Value = ((Data.DoubleValue)item.Value).Value;
|
---|
184 | value = v;
|
---|
185 | } else if (item.Value is Data.StringValue) {
|
---|
186 | StringValue v = new StringValue();
|
---|
187 | v.Value = ((Data.StringValue)item.Value).Value;
|
---|
188 | value = v;
|
---|
189 | } else {
|
---|
190 | BinaryValue v = new BinaryValue();
|
---|
191 | using (MemoryStream stream = new MemoryStream()) {
|
---|
192 | XmlGenerator.Serialize(item.Value, stream);
|
---|
193 | stream.Close();
|
---|
194 | v.Value = stream.ToArray();
|
---|
195 | }
|
---|
196 | value = v;
|
---|
197 | }
|
---|
198 | value.Name = item.Key;
|
---|
199 | value.DataType = new DataType();
|
---|
200 | value.DataType.Name = item.Value.GetType().Name;
|
---|
201 | value.DataType.TypeName = item.Value.GetType().AssemblyQualifiedName;
|
---|
202 | values.Add(value);
|
---|
203 | }
|
---|
204 | return values;
|
---|
205 | }
|
---|
206 | #endregion
|
---|
207 | }
|
---|
208 | }
|
---|