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