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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Clients.Common;
|
---|
27 | using HeuristicLab.Collections;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Persistence.Default.Xml;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Clients.OKB {
|
---|
36 | [Item("OKBClient", "Client for accessing the OKB.")]
|
---|
37 | public sealed class OKBClient : IContent {
|
---|
38 | private static OKBClient instance;
|
---|
39 | public static OKBClient Instance {
|
---|
40 | get {
|
---|
41 | if (instance == null) instance = new OKBClient();
|
---|
42 | return instance;
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | #region Properties
|
---|
47 | private ItemCollection<Platform> platforms;
|
---|
48 | public ItemCollection<Platform> Platforms {
|
---|
49 | get { return platforms; }
|
---|
50 | }
|
---|
51 | private ItemCollection<DataType> dataTypes;
|
---|
52 | public ItemCollection<DataType> DataTypes {
|
---|
53 | get { return dataTypes; }
|
---|
54 | }
|
---|
55 | private IEnumerable<User> users;
|
---|
56 | public IEnumerable<User> Users {
|
---|
57 | get { return users; }
|
---|
58 | }
|
---|
59 | private ItemCollection<AlgorithmClass> algorithmClasses;
|
---|
60 | public ItemCollection<AlgorithmClass> AlgorithmClasses {
|
---|
61 | get { return algorithmClasses; }
|
---|
62 | }
|
---|
63 | private ItemCollection<Algorithm> algorithms;
|
---|
64 | public ItemCollection<Algorithm> Algorithms {
|
---|
65 | get { return algorithms; }
|
---|
66 | }
|
---|
67 | private ItemCollection<ProblemClass> problemClasses;
|
---|
68 | public ItemCollection<ProblemClass> ProblemClasses {
|
---|
69 | get { return problemClasses; }
|
---|
70 | }
|
---|
71 | private ItemCollection<Problem> problems;
|
---|
72 | public ItemCollection<Problem> Problems {
|
---|
73 | get { return problems; }
|
---|
74 | }
|
---|
75 | #endregion
|
---|
76 |
|
---|
77 | private OKBClient() {
|
---|
78 | platforms = new ItemCollection<Platform>();
|
---|
79 | platforms.ItemsRemoved += new CollectionItemsChangedEventHandler<Platform>(platforms_ItemsRemoved);
|
---|
80 | dataTypes = new ItemCollection<DataType>();
|
---|
81 | dataTypes.ItemsRemoved += new CollectionItemsChangedEventHandler<DataType>(dataTypes_ItemsRemoved);
|
---|
82 | algorithmClasses = new ItemCollection<AlgorithmClass>();
|
---|
83 | algorithmClasses.ItemsRemoved += new CollectionItemsChangedEventHandler<AlgorithmClass>(algorithmClasses_ItemsRemoved);
|
---|
84 | algorithms = new ItemCollection<Algorithm>();
|
---|
85 | algorithms.ItemsRemoved += new CollectionItemsChangedEventHandler<Algorithm>(algorithms_ItemsRemoved);
|
---|
86 | problemClasses = new ItemCollection<ProblemClass>();
|
---|
87 | problemClasses.ItemsRemoved += new CollectionItemsChangedEventHandler<ProblemClass>(problemClasses_ItemsRemoved);
|
---|
88 | problems = new ItemCollection<Problem>();
|
---|
89 | problems.ItemsRemoved += new CollectionItemsChangedEventHandler<Problem>(problems_ItemsRemoved);
|
---|
90 | }
|
---|
91 |
|
---|
92 | #region Refresh
|
---|
93 | public void Refresh() {
|
---|
94 | OnRefreshing();
|
---|
95 |
|
---|
96 | platforms.Clear();
|
---|
97 | dataTypes.Clear();
|
---|
98 | algorithmClasses.Clear();
|
---|
99 | algorithms.Clear();
|
---|
100 | problemClasses.Clear();
|
---|
101 | problems.Clear();
|
---|
102 |
|
---|
103 | var call = new Func<Exception>(delegate() {
|
---|
104 | try {
|
---|
105 | platforms.AddRange(CallAdminService<Platform[]>(s => s.GetPlatforms()).OrderBy(x => x.Name));
|
---|
106 | dataTypes.AddRange(CallAdminService<DataType[]>(s => s.GetDataTypes()).OrderBy(x => x.Name));
|
---|
107 | users = CallAuthenticationService<User[]>(s => s.GetUsers()).OrderBy(x => x.Name);
|
---|
108 | algorithmClasses.AddRange(CallAdminService<AlgorithmClass[]>(s => s.GetAlgorithmClasses()).OrderBy(x => x.Name));
|
---|
109 | algorithms.AddRange(CallAdminService<Algorithm[]>(s => s.GetAlgorithms()).OrderBy(x => x.Name));
|
---|
110 | problemClasses.AddRange(CallAdminService<ProblemClass[]>(s => s.GetProblemClasses()).OrderBy(x => x.Name));
|
---|
111 | problems.AddRange(CallAdminService<Problem[]>(s => s.GetProblems()).OrderBy(x => x.Name));
|
---|
112 | return null;
|
---|
113 | }
|
---|
114 | catch (Exception ex) {
|
---|
115 | return ex;
|
---|
116 | }
|
---|
117 | });
|
---|
118 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
119 | Exception ex = call.EndInvoke(result);
|
---|
120 | if (ex != null) ErrorHandling.ShowErrorDialog("Refresh failed.", ex);
|
---|
121 | OnRefreshed();
|
---|
122 | }, null);
|
---|
123 | }
|
---|
124 | #endregion
|
---|
125 |
|
---|
126 | #region Store
|
---|
127 | public bool Store(IOKBItem item) {
|
---|
128 | try {
|
---|
129 | if (item.Id == 0) {
|
---|
130 | if (item is Platform)
|
---|
131 | item.Id = CallAdminService<long>(s => s.AddPlatform((Platform)item));
|
---|
132 | else if (item is DataType)
|
---|
133 | item.Id = CallAdminService<long>(s => s.AddDataType((DataType)item));
|
---|
134 | else if (item is AlgorithmClass)
|
---|
135 | item.Id = CallAdminService<long>(s => s.AddAlgorithmClass((AlgorithmClass)item));
|
---|
136 | else if (item is Algorithm)
|
---|
137 | item.Id = CallAdminService<long>(s => s.AddAlgorithm((Algorithm)item));
|
---|
138 | else if (item is AlgorithmParameter)
|
---|
139 | item.Id = CallAdminService<long>(s => s.AddAlgorithmParameter((AlgorithmParameter)item));
|
---|
140 | else if (item is ProblemClass)
|
---|
141 | item.Id = CallAdminService<long>(s => s.AddProblemClass((ProblemClass)item));
|
---|
142 | else if (item is Problem)
|
---|
143 | item.Id = CallAdminService<long>(s => s.AddProblem((Problem)item));
|
---|
144 | else if (item is ProblemParameter)
|
---|
145 | item.Id = CallAdminService<long>(s => s.AddProblemParameter((ProblemParameter)item));
|
---|
146 | else if (item is Result)
|
---|
147 | item.Id = CallAdminService<long>(s => s.AddResult((Result)item));
|
---|
148 | else if (item is Experiment)
|
---|
149 | item.Id = CallAdminService<long>(s => s.AddExperiment((Experiment)item));
|
---|
150 | else if (item is Run)
|
---|
151 | item.Id = CallAdminService<long>(s => s.AddRun((Run)item));
|
---|
152 | } else {
|
---|
153 | if (item is Platform)
|
---|
154 | CallAdminService(s => s.UpdatePlatform((Platform)item));
|
---|
155 | else if (item is DataType)
|
---|
156 | CallAdminService(s => s.UpdateDataType((DataType)item));
|
---|
157 | else if (item is AlgorithmClass)
|
---|
158 | CallAdminService(s => s.UpdateAlgorithmClass((AlgorithmClass)item));
|
---|
159 | else if (item is Algorithm)
|
---|
160 | CallAdminService(s => s.UpdateAlgorithm((Algorithm)item));
|
---|
161 | else if (item is AlgorithmParameter)
|
---|
162 | CallAdminService(s => s.UpdateAlgorithmParameter((AlgorithmParameter)item));
|
---|
163 | else if (item is ProblemClass)
|
---|
164 | CallAdminService(s => s.UpdateProblemClass((ProblemClass)item));
|
---|
165 | else if (item is Problem)
|
---|
166 | CallAdminService(s => s.UpdateProblem((Problem)item));
|
---|
167 | else if (item is ProblemParameter)
|
---|
168 | CallAdminService(s => s.UpdateProblemParameter((ProblemParameter)item));
|
---|
169 | else if (item is Result)
|
---|
170 | CallAdminService(s => s.UpdateResult((Result)item));
|
---|
171 | else if (item is Experiment)
|
---|
172 | item.Id = CallAdminService<long>(s => s.AddExperiment((Experiment)item));
|
---|
173 | else if (item is Run)
|
---|
174 | item.Id = CallAdminService<long>(s => s.AddRun((Run)item));
|
---|
175 | }
|
---|
176 | return true;
|
---|
177 | }
|
---|
178 | catch (Exception ex) {
|
---|
179 | ErrorHandling.ShowErrorDialog("Store failed.", ex);
|
---|
180 | return false;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | #endregion
|
---|
184 |
|
---|
185 | #region DataType Methods
|
---|
186 | public DataType ConvertToDataType(Type type) {
|
---|
187 | DataType dataType = DataTypes.FirstOrDefault(x => x.Name == type.AssemblyQualifiedName);
|
---|
188 | if (dataType == null) {
|
---|
189 | dataType = new DataType();
|
---|
190 | dataType.Name = type.AssemblyQualifiedName;
|
---|
191 | dataType.PlatformId = Platforms.FirstOrDefault(x => x.Name == "HeuristicLab 3.3").Id;
|
---|
192 |
|
---|
193 | if (typeof(BoolValue).IsAssignableFrom(type))
|
---|
194 | dataType.SqlName = "bit";
|
---|
195 | else if (typeof(IntValue).IsAssignableFrom(type))
|
---|
196 | dataType.SqlName = "bigint";
|
---|
197 | else if (typeof(DoubleValue).IsAssignableFrom(type))
|
---|
198 | dataType.SqlName = "float";
|
---|
199 | else if (typeof(StringValue).IsAssignableFrom(type) || typeof(IStringConvertibleValue).IsAssignableFrom(type))
|
---|
200 | dataType.SqlName = "nvarchar";
|
---|
201 | else
|
---|
202 | dataType.SqlName = "varbinary";
|
---|
203 |
|
---|
204 | dataType.Store();
|
---|
205 | DataTypes.Add(dataType);
|
---|
206 | }
|
---|
207 | return dataType;
|
---|
208 | }
|
---|
209 | #endregion
|
---|
210 |
|
---|
211 | #region Algorithm Methods
|
---|
212 | public Guid[] GetAlgorithmUsers(long algorithmId) {
|
---|
213 | try {
|
---|
214 | return CallAdminService<Guid[]>(s => s.GetAlgorithmUsers(algorithmId));
|
---|
215 | }
|
---|
216 | catch (Exception ex) {
|
---|
217 | ErrorHandling.ShowErrorDialog("Refresh authorized algorithm users failed.", ex);
|
---|
218 | return null;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | public bool UpdateAlgorithmUsers(long algorithmId, Guid[] users) {
|
---|
222 | try {
|
---|
223 | CallAdminService(s => s.UpdateAlgorithmUsers(algorithmId, users));
|
---|
224 | return true;
|
---|
225 | }
|
---|
226 | catch (Exception ex) {
|
---|
227 | ErrorHandling.ShowErrorDialog("Update authorized algorithm users failed.", ex);
|
---|
228 | return false;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | public AlgorithmData GetAlgorithmData(long algorithmId) {
|
---|
232 | try {
|
---|
233 | return CallAdminService<AlgorithmData>(s => s.GetAlgorithmData(algorithmId));
|
---|
234 | }
|
---|
235 | catch (Exception ex) {
|
---|
236 | ErrorHandling.ShowErrorDialog("Refresh algorithm data failed.", ex);
|
---|
237 | return null;
|
---|
238 | }
|
---|
239 | }
|
---|
240 | public bool UpdateAlgorithmData(AlgorithmData algorithmData) {
|
---|
241 | try {
|
---|
242 | CallAdminService(s => s.UpdateAlgorithmData(algorithmData));
|
---|
243 | return true;
|
---|
244 | }
|
---|
245 | catch (Exception ex) {
|
---|
246 | ErrorHandling.ShowErrorDialog("Update algorithm data failed.", ex);
|
---|
247 | return false;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | #endregion
|
---|
251 |
|
---|
252 | #region Problem Methods
|
---|
253 | public Guid[] GetProblemUsers(long problemId) {
|
---|
254 | try {
|
---|
255 | return CallAdminService<Guid[]>(s => s.GetProblemUsers(problemId));
|
---|
256 | }
|
---|
257 | catch (Exception ex) {
|
---|
258 | ErrorHandling.ShowErrorDialog("Refresh authorized problem users failed.", ex);
|
---|
259 | return null;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | public bool UpdateProblemUsers(long problemId, Guid[] users) {
|
---|
263 | try {
|
---|
264 | CallAdminService(s => s.UpdateProblemUsers(problemId, users));
|
---|
265 | return true;
|
---|
266 | }
|
---|
267 | catch (Exception ex) {
|
---|
268 | ErrorHandling.ShowErrorDialog("Update authorized problem users failed.", ex);
|
---|
269 | return false;
|
---|
270 | }
|
---|
271 | }
|
---|
272 | public ProblemData GetProblemData(long problemId) {
|
---|
273 | try {
|
---|
274 | return CallAdminService<ProblemData>(s => s.GetProblemData(problemId));
|
---|
275 | }
|
---|
276 | catch (Exception ex) {
|
---|
277 | ErrorHandling.ShowErrorDialog("Refresh problem data failed.", ex);
|
---|
278 | return null;
|
---|
279 | }
|
---|
280 | }
|
---|
281 | public bool UpdateProblemData(ProblemData problemData) {
|
---|
282 | try {
|
---|
283 | CallAdminService(s => s.UpdateProblemData(problemData));
|
---|
284 | return true;
|
---|
285 | }
|
---|
286 | catch (Exception ex) {
|
---|
287 | ErrorHandling.ShowErrorDialog("Update problem data failed.", ex);
|
---|
288 | return false;
|
---|
289 | }
|
---|
290 | }
|
---|
291 | #endregion
|
---|
292 |
|
---|
293 | #region AlgorithmParameter Methods
|
---|
294 | public AlgorithmParameter GetAlgorithmParameter(long id) {
|
---|
295 | try {
|
---|
296 | return CallAdminService<AlgorithmParameter>(s => s.GetAlgorithmParameter(id));
|
---|
297 | }
|
---|
298 | catch (Exception ex) {
|
---|
299 | ErrorHandling.ShowErrorDialog("Refresh algorithm parameter failed.", ex);
|
---|
300 | return null;
|
---|
301 | }
|
---|
302 | }
|
---|
303 | public ItemCollection<AlgorithmParameter> GetAlgorithmParameters(long algorithmId) {
|
---|
304 | try {
|
---|
305 | ItemCollection<AlgorithmParameter> parameters = new ItemCollection<AlgorithmParameter>();
|
---|
306 | parameters.AddRange(CallAdminService<AlgorithmParameter[]>(s => s.GetAlgorithmParameters(algorithmId)).OrderBy(x => x.Name));
|
---|
307 | return parameters;
|
---|
308 | }
|
---|
309 | catch (Exception ex) {
|
---|
310 | ErrorHandling.ShowErrorDialog("Refresh algorithm parameters failed.", ex);
|
---|
311 | return null;
|
---|
312 | }
|
---|
313 | }
|
---|
314 | #endregion
|
---|
315 |
|
---|
316 | #region ProblemParameter Methods
|
---|
317 | public ProblemParameter GetProblemParameter(long id) {
|
---|
318 | try {
|
---|
319 | return CallAdminService<ProblemParameter>(s => s.GetProblemParameter(id));
|
---|
320 | }
|
---|
321 | catch (Exception ex) {
|
---|
322 | ErrorHandling.ShowErrorDialog("Refresh problem parameter failed.", ex);
|
---|
323 | return null;
|
---|
324 | }
|
---|
325 | }
|
---|
326 | public ItemCollection<ProblemParameter> GetProblemParameters(long problemId) {
|
---|
327 | try {
|
---|
328 | ItemCollection<ProblemParameter> parameters = new ItemCollection<ProblemParameter>();
|
---|
329 | parameters.AddRange(CallAdminService<ProblemParameter[]>(s => s.GetProblemParameters(problemId)).OrderBy(x => x.Name));
|
---|
330 | return parameters;
|
---|
331 | }
|
---|
332 | catch (Exception ex) {
|
---|
333 | ErrorHandling.ShowErrorDialog("Refresh problem parameters failed.", ex);
|
---|
334 | return null;
|
---|
335 | }
|
---|
336 | }
|
---|
337 | #endregion
|
---|
338 |
|
---|
339 | #region Result Methods
|
---|
340 | public Result GetResult(long id) {
|
---|
341 | try {
|
---|
342 | return CallAdminService<Result>(s => s.GetResult(id));
|
---|
343 | }
|
---|
344 | catch (Exception ex) {
|
---|
345 | ErrorHandling.ShowErrorDialog("Refresh result failed.", ex);
|
---|
346 | return null;
|
---|
347 | }
|
---|
348 | }
|
---|
349 | public ItemCollection<Result> GetResults(long algorithmId) {
|
---|
350 | try {
|
---|
351 | ItemCollection<Result> results = new ItemCollection<Result>();
|
---|
352 | results.AddRange(CallAdminService<Result[]>(s => s.GetResults(algorithmId)).OrderBy(x => x.Name));
|
---|
353 | return results;
|
---|
354 | }
|
---|
355 | catch (Exception ex) {
|
---|
356 | ErrorHandling.ShowErrorDialog("Refresh results failed.", ex);
|
---|
357 | return null;
|
---|
358 | }
|
---|
359 | }
|
---|
360 | #endregion
|
---|
361 |
|
---|
362 | #region Experiment Methods
|
---|
363 | public Experiment GetExperiment(long id) {
|
---|
364 | try {
|
---|
365 | return CallAdminService<Experiment>(s => s.GetExperiment(id));
|
---|
366 | }
|
---|
367 | catch (Exception ex) {
|
---|
368 | ErrorHandling.ShowErrorDialog("Refresh experiment failed.", ex);
|
---|
369 | return null;
|
---|
370 | }
|
---|
371 | }
|
---|
372 | public ItemCollection<Experiment> GetExperiments(long algorithmId, long problemId) {
|
---|
373 | try {
|
---|
374 | ItemCollection<Experiment> experiments = new ItemCollection<Experiment>();
|
---|
375 | experiments.AddRange(CallAdminService<Experiment[]>(s => s.GetExperiments(algorithmId, problemId)));
|
---|
376 | experiments.ItemsRemoved += new CollectionItemsChangedEventHandler<Experiment>(experiments_ItemsRemoved);
|
---|
377 | return experiments;
|
---|
378 | }
|
---|
379 | catch (Exception ex) {
|
---|
380 | ErrorHandling.ShowErrorDialog("Refresh experiments failed.", ex);
|
---|
381 | return null;
|
---|
382 | }
|
---|
383 | }
|
---|
384 | #endregion
|
---|
385 |
|
---|
386 | #region Run Methods
|
---|
387 | public ItemCollection<Run> GetRuns(long experimentId) {
|
---|
388 | try {
|
---|
389 | ItemCollection<Run> runs = new ItemCollection<Run>();
|
---|
390 | runs.AddRange(CallAdminService<Run[]>(s => s.GetRuns(experimentId)).OrderByDescending(x => x.FinishedDate));
|
---|
391 | runs.ItemsRemoved += new CollectionItemsChangedEventHandler<Run>(runs_ItemsRemoved);
|
---|
392 | return runs;
|
---|
393 | }
|
---|
394 | catch (Exception ex) {
|
---|
395 | ErrorHandling.ShowErrorDialog("Refresh runs failed.", ex);
|
---|
396 | return null;
|
---|
397 | }
|
---|
398 | }
|
---|
399 | public ItemCollection<Run> QueryRuns(string query) {
|
---|
400 | try {
|
---|
401 | ItemCollection<Run> runs = new ItemCollection<Run>();
|
---|
402 | runs.AddRange(CallAdminService<Run[]>(s => s.QueryRuns(query)).OrderByDescending(x => x.FinishedDate));
|
---|
403 | return runs;
|
---|
404 | }
|
---|
405 | catch (Exception ex) {
|
---|
406 | ErrorHandling.ShowErrorDialog("Refresh runs failed.", ex);
|
---|
407 | return null;
|
---|
408 | }
|
---|
409 | }
|
---|
410 | public bool AddRun(long algorithmId, long problemId, IAlgorithm algorithm) {
|
---|
411 | try {
|
---|
412 | IProblem problem = algorithm.Problem;
|
---|
413 |
|
---|
414 | ItemCollection<AlgorithmParameter> algorithmParameters = GetAlgorithmParameters(algorithmId);
|
---|
415 | List<AlgorithmParameterValue> algorithmParameterValues = CollectAlgorithmParameterValues(algorithmId, algorithmParameters, algorithm, "");
|
---|
416 | ItemCollection<ProblemParameter> problemParameters = GetProblemParameters(problemId);
|
---|
417 | List<ProblemParameterValue> problemParameterValues = CollectProblemParamterValues(problemId, problemParameters, problem, "");
|
---|
418 | ItemCollection<Result> results = GetResults(algorithmId);
|
---|
419 | List<ResultValue> resultValues = CollectResultValues(algorithmId, results, algorithm);
|
---|
420 |
|
---|
421 | Experiment exp = new Experiment();
|
---|
422 | exp.AlgorithmId = algorithmId;
|
---|
423 | exp.ProblemId = problemId;
|
---|
424 | exp.AlgorithmParameterValues = algorithmParameterValues.ToArray();
|
---|
425 | exp.ProblemParameterValues = problemParameterValues.ToArray();
|
---|
426 | exp.Store();
|
---|
427 |
|
---|
428 | Run r = new Run();
|
---|
429 | r.ExperimentId = exp.Id;
|
---|
430 | r.ClientId = Guid.NewGuid();
|
---|
431 | r.FinishedDate = DateTime.Now;
|
---|
432 | r.RandomSeed = ((IntValue)((IValueParameter)algorithm.Parameters["Seed"]).Value).Value;
|
---|
433 | r.ResultValues = resultValues.ToArray();
|
---|
434 | r.Store();
|
---|
435 |
|
---|
436 | return true;
|
---|
437 | }
|
---|
438 | catch (Exception ex) {
|
---|
439 | ErrorHandling.ShowErrorDialog("Store run failed.", ex);
|
---|
440 | return false;
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | private List<AlgorithmParameterValue> CollectAlgorithmParameterValues(long algorithmId, ItemCollection<AlgorithmParameter> parameters, IParameterizedItem item, string prefix) {
|
---|
445 | List<AlgorithmParameterValue> values = new List<AlgorithmParameterValue>();
|
---|
446 | foreach (IValueParameter param in item.Parameters.OfType<IValueParameter>()) {
|
---|
447 | if (param.GetsCollected && (param.Value != null) && (param.Name != "Seed")) {
|
---|
448 | AlgorithmParameter p = parameters.FirstOrDefault(x => x.Name == prefix + param.Name);
|
---|
449 | if (p == null) {
|
---|
450 | p = new AlgorithmParameter();
|
---|
451 | p.Name = prefix + param.Name;
|
---|
452 | p.Alias = prefix + param.Name;
|
---|
453 | p.Description = param.Description;
|
---|
454 | p.AlgorithmId = algorithmId;
|
---|
455 | p.DataTypeId = ConvertToDataType(param.DataType).Id;
|
---|
456 | p.Store();
|
---|
457 | parameters.Add(p);
|
---|
458 | }
|
---|
459 | AlgorithmParameterValue value = CreateAlgorithmParameterValue(param.Value);
|
---|
460 | value.AlgorithmParameterId = p.Id;
|
---|
461 | value.DataTypeId = ConvertToDataType(param.Value.GetType()).Id;
|
---|
462 | values.Add(value);
|
---|
463 | }
|
---|
464 |
|
---|
465 | if (param.Value is IParameterizedItem)
|
---|
466 | values.AddRange(CollectAlgorithmParameterValues(algorithmId, parameters, (IParameterizedItem)param.Value, (string.IsNullOrEmpty(prefix) ? param.Name : prefix + param.Name) + "."));
|
---|
467 | }
|
---|
468 | return values;
|
---|
469 | }
|
---|
470 | private AlgorithmParameterValue CreateAlgorithmParameterValue(IItem item) {
|
---|
471 | if (item is BoolValue) {
|
---|
472 | AlgorithmParameterBoolValue value = new AlgorithmParameterBoolValue();
|
---|
473 | value.Value = ((BoolValue)item).Value;
|
---|
474 | return value;
|
---|
475 | } else if (item is DoubleValue) {
|
---|
476 | AlgorithmParameterFloatValue value = new AlgorithmParameterFloatValue();
|
---|
477 | value.Value = ((DoubleValue)item).Value;
|
---|
478 | return value;
|
---|
479 | } else if (item is IntValue) {
|
---|
480 | AlgorithmParameterIntValue value = new AlgorithmParameterIntValue();
|
---|
481 | value.Value = ((IntValue)item).Value;
|
---|
482 | return value;
|
---|
483 | } else if (item is StringValue) {
|
---|
484 | AlgorithmParameterStringValue value = new AlgorithmParameterStringValue();
|
---|
485 | value.Value = ((StringValue)item).Value;
|
---|
486 | return value;
|
---|
487 | } else {
|
---|
488 | AlgorithmParameterBlobValue value = new AlgorithmParameterBlobValue();
|
---|
489 | try {
|
---|
490 | using (MemoryStream stream = new MemoryStream()) {
|
---|
491 | XmlGenerator.Serialize(item, stream);
|
---|
492 | stream.Close();
|
---|
493 | value.Value = stream.ToArray();
|
---|
494 | }
|
---|
495 | }
|
---|
496 | catch (Exception ex) {
|
---|
497 | ErrorHandling.ShowErrorDialog(ex);
|
---|
498 | }
|
---|
499 | return value;
|
---|
500 | }
|
---|
501 | }
|
---|
502 | private List<ProblemParameterValue> CollectProblemParamterValues(long problemId, ItemCollection<ProblemParameter> parameters, IParameterizedItem item, string prefix) {
|
---|
503 | List<ProblemParameterValue> values = new List<ProblemParameterValue>();
|
---|
504 | foreach (IValueParameter param in item.Parameters.OfType<IValueParameter>()) {
|
---|
505 | if (param.GetsCollected && (param.Value != null)) {
|
---|
506 | ProblemParameter p = parameters.FirstOrDefault(x => x.Name == prefix + param.Name);
|
---|
507 | if (p == null) {
|
---|
508 | p = new ProblemParameter();
|
---|
509 | p.Name = prefix + param.Name;
|
---|
510 | p.Alias = prefix + param.Name;
|
---|
511 | p.Description = param.Description;
|
---|
512 | p.ProblemId = problemId;
|
---|
513 | p.DataTypeId = ConvertToDataType(param.DataType).Id;
|
---|
514 | p.Store();
|
---|
515 | parameters.Add(p);
|
---|
516 | }
|
---|
517 | ProblemParameterValue value = CreateProblemParameterValue(param.Value);
|
---|
518 | value.ProblemParameterId = p.Id;
|
---|
519 | value.DataTypeId = ConvertToDataType(param.Value.GetType()).Id;
|
---|
520 | values.Add(value);
|
---|
521 | }
|
---|
522 |
|
---|
523 | if (param.Value is IParameterizedItem)
|
---|
524 | values.AddRange(CollectProblemParamterValues(problemId, parameters, (IParameterizedItem)param.Value, (string.IsNullOrEmpty(prefix) ? param.Name : prefix + param.Name) + "."));
|
---|
525 | }
|
---|
526 | return values;
|
---|
527 | }
|
---|
528 | private ProblemParameterValue CreateProblemParameterValue(IItem item) {
|
---|
529 | if (item is BoolValue) {
|
---|
530 | ProblemParameterBoolValue value = new ProblemParameterBoolValue();
|
---|
531 | value.Value = ((BoolValue)item).Value;
|
---|
532 | return value;
|
---|
533 | } else if (item is DoubleValue) {
|
---|
534 | ProblemParameterFloatValue value = new ProblemParameterFloatValue();
|
---|
535 | value.Value = ((DoubleValue)item).Value;
|
---|
536 | return value;
|
---|
537 | } else if (item is IntValue) {
|
---|
538 | ProblemParameterIntValue value = new ProblemParameterIntValue();
|
---|
539 | value.Value = ((IntValue)item).Value;
|
---|
540 | return value;
|
---|
541 | } else if (item is StringValue) {
|
---|
542 | ProblemParameterStringValue value = new ProblemParameterStringValue();
|
---|
543 | value.Value = ((StringValue)item).Value;
|
---|
544 | return value;
|
---|
545 | } else {
|
---|
546 | ProblemParameterBlobValue value = new ProblemParameterBlobValue();
|
---|
547 | try {
|
---|
548 | using (MemoryStream stream = new MemoryStream()) {
|
---|
549 | XmlGenerator.Serialize(item, stream);
|
---|
550 | stream.Close();
|
---|
551 | value.Value = stream.ToArray();
|
---|
552 | }
|
---|
553 | }
|
---|
554 | catch (Exception ex) {
|
---|
555 | ErrorHandling.ShowErrorDialog(ex);
|
---|
556 | }
|
---|
557 | return value;
|
---|
558 | }
|
---|
559 | }
|
---|
560 | private List<ResultValue> CollectResultValues(long algorithmId, ItemCollection<Result> results, IAlgorithm algorithm) {
|
---|
561 | List<ResultValue> values = new List<ResultValue>();
|
---|
562 | foreach (IResult result in algorithm.Results) {
|
---|
563 | if (result.Value != null) {
|
---|
564 | Result r = results.FirstOrDefault(x => x.Name == result.Name);
|
---|
565 | if (r == null) {
|
---|
566 | r = new Result();
|
---|
567 | r.Name = result.Name;
|
---|
568 | r.Alias = result.Name;
|
---|
569 | r.Description = result.Description;
|
---|
570 | r.AlgorithmId = algorithmId;
|
---|
571 | r.DataTypeId = ConvertToDataType(result.DataType).Id;
|
---|
572 | r.Store();
|
---|
573 | results.Add(r);
|
---|
574 | }
|
---|
575 | ResultValue value = CreateResultValue(result.Value);
|
---|
576 | value.ResultId = r.Id;
|
---|
577 | value.DataTypeId = ConvertToDataType(result.Value.GetType()).Id;
|
---|
578 | values.Add(value);
|
---|
579 | }
|
---|
580 | }
|
---|
581 | return values;
|
---|
582 | }
|
---|
583 | private ResultValue CreateResultValue(IItem item) {
|
---|
584 | if (item is BoolValue) {
|
---|
585 | ResultBoolValue value = new ResultBoolValue();
|
---|
586 | value.Value = ((BoolValue)item).Value;
|
---|
587 | return value;
|
---|
588 | } else if (item is DoubleValue) {
|
---|
589 | ResultFloatValue value = new ResultFloatValue();
|
---|
590 | value.Value = ((DoubleValue)item).Value;
|
---|
591 | return value;
|
---|
592 | } else if (item is IntValue) {
|
---|
593 | ResultIntValue value = new ResultIntValue();
|
---|
594 | value.Value = ((IntValue)item).Value;
|
---|
595 | return value;
|
---|
596 | } else if (item is StringValue) {
|
---|
597 | ResultStringValue value = new ResultStringValue();
|
---|
598 | value.Value = ((StringValue)item).Value;
|
---|
599 | return value;
|
---|
600 | } else {
|
---|
601 | ResultBlobValue value = new ResultBlobValue();
|
---|
602 | try {
|
---|
603 | using (MemoryStream stream = new MemoryStream()) {
|
---|
604 | XmlGenerator.Serialize(item, stream);
|
---|
605 | stream.Close();
|
---|
606 | value.Value = stream.ToArray();
|
---|
607 | }
|
---|
608 | }
|
---|
609 | catch (Exception ex) {
|
---|
610 | ErrorHandling.ShowErrorDialog(ex);
|
---|
611 | }
|
---|
612 | return value;
|
---|
613 | }
|
---|
614 | }
|
---|
615 | public RunCollection ConvertOKBRunsToOptimizationRuns(IItemCollection<Run> runs) {
|
---|
616 | RunCollection runCollection = new RunCollection();
|
---|
617 | foreach (Run run in runs) {
|
---|
618 | Optimization.Run r = new Optimization.Run();
|
---|
619 | foreach (ResultValue resultValue in run.ResultValues) {
|
---|
620 | Result result = GetResult(resultValue.ResultId);
|
---|
621 | if (resultValue is ResultBlobValue) {
|
---|
622 | IItem item = null;
|
---|
623 | using (MemoryStream stream = new MemoryStream(((ResultBlobValue)resultValue).Value)) {
|
---|
624 | try {
|
---|
625 | item = XmlParser.Deserialize<IItem>(stream);
|
---|
626 | }
|
---|
627 | catch (Exception ex) {
|
---|
628 | ErrorHandling.ShowErrorDialog(ex);
|
---|
629 | }
|
---|
630 | stream.Close();
|
---|
631 | }
|
---|
632 | r.Results.Add(result.Name, item);
|
---|
633 | } else if (resultValue is ResultBoolValue) {
|
---|
634 | r.Results.Add(result.Name, new BoolValue(((ResultBoolValue)resultValue).Value));
|
---|
635 | } else if (resultValue is ResultFloatValue) {
|
---|
636 | r.Results.Add(result.Name, new DoubleValue(((ResultFloatValue)resultValue).Value));
|
---|
637 | } else if (resultValue is ResultIntValue) {
|
---|
638 | r.Results.Add(result.Name, new IntValue((int)((ResultIntValue)resultValue).Value));
|
---|
639 | } else if (resultValue is ResultStringValue) {
|
---|
640 | r.Results.Add(result.Name, new StringValue(((ResultStringValue)resultValue).Value));
|
---|
641 | }
|
---|
642 | }
|
---|
643 |
|
---|
644 | Experiment exp = GetExperiment(run.ExperimentId);
|
---|
645 | foreach (AlgorithmParameterValue algorithmParameterValue in exp.AlgorithmParameterValues) {
|
---|
646 | AlgorithmParameter algorithmParameter = GetAlgorithmParameter(algorithmParameterValue.AlgorithmParameterId);
|
---|
647 | if (algorithmParameterValue is AlgorithmParameterBlobValue) {
|
---|
648 | IItem item = null;
|
---|
649 | using (MemoryStream stream = new MemoryStream(((AlgorithmParameterBlobValue)algorithmParameterValue).Value)) {
|
---|
650 | try {
|
---|
651 | item = XmlParser.Deserialize<IItem>(stream);
|
---|
652 | }
|
---|
653 | catch (Exception ex) {
|
---|
654 | ErrorHandling.ShowErrorDialog(ex);
|
---|
655 | }
|
---|
656 | stream.Close();
|
---|
657 | }
|
---|
658 | r.Parameters.Add(algorithmParameter.Name, item);
|
---|
659 | } else if (algorithmParameterValue is AlgorithmParameterBoolValue) {
|
---|
660 | r.Parameters.Add(algorithmParameter.Name, new BoolValue(((AlgorithmParameterBoolValue)algorithmParameterValue).Value));
|
---|
661 | } else if (algorithmParameterValue is AlgorithmParameterFloatValue) {
|
---|
662 | r.Parameters.Add(algorithmParameter.Name, new DoubleValue(((AlgorithmParameterFloatValue)algorithmParameterValue).Value));
|
---|
663 | } else if (algorithmParameterValue is AlgorithmParameterIntValue) {
|
---|
664 | r.Parameters.Add(algorithmParameter.Name, new IntValue((int)((AlgorithmParameterIntValue)algorithmParameterValue).Value));
|
---|
665 | } else if (algorithmParameterValue is AlgorithmParameterStringValue) {
|
---|
666 | r.Parameters.Add(algorithmParameter.Name, new StringValue(((AlgorithmParameterStringValue)algorithmParameterValue).Value));
|
---|
667 | }
|
---|
668 | }
|
---|
669 | foreach (ProblemParameterValue problemParameterValue in exp.ProblemParameterValues) {
|
---|
670 | ProblemParameter problemParameter = GetProblemParameter(problemParameterValue.ProblemParameterId);
|
---|
671 | if (problemParameterValue is ProblemParameterBlobValue) {
|
---|
672 | IItem item = null;
|
---|
673 | using (MemoryStream stream = new MemoryStream(((ProblemParameterBlobValue)problemParameterValue).Value)) {
|
---|
674 | try {
|
---|
675 | item = XmlParser.Deserialize<IItem>(stream);
|
---|
676 | }
|
---|
677 | catch (Exception ex) {
|
---|
678 | ErrorHandling.ShowErrorDialog(ex);
|
---|
679 | }
|
---|
680 | stream.Close();
|
---|
681 | }
|
---|
682 | r.Parameters.Add(problemParameter.Name, item);
|
---|
683 | } else if (problemParameterValue is ProblemParameterBoolValue) {
|
---|
684 | r.Parameters.Add(problemParameter.Name, new BoolValue(((ProblemParameterBoolValue)problemParameterValue).Value));
|
---|
685 | } else if (problemParameterValue is ProblemParameterFloatValue) {
|
---|
686 | r.Parameters.Add(problemParameter.Name, new DoubleValue(((ProblemParameterFloatValue)problemParameterValue).Value));
|
---|
687 | } else if (problemParameterValue is ProblemParameterIntValue) {
|
---|
688 | r.Parameters.Add(problemParameter.Name, new IntValue((int)((ProblemParameterIntValue)problemParameterValue).Value));
|
---|
689 | } else if (problemParameterValue is ProblemParameterStringValue) {
|
---|
690 | r.Parameters.Add(problemParameter.Name, new StringValue(((ProblemParameterStringValue)problemParameterValue).Value));
|
---|
691 | }
|
---|
692 | }
|
---|
693 | runCollection.Add(r);
|
---|
694 | }
|
---|
695 | return runCollection;
|
---|
696 | }
|
---|
697 | #endregion
|
---|
698 |
|
---|
699 | #region Events
|
---|
700 | public event EventHandler Refreshing;
|
---|
701 | private void OnRefreshing() {
|
---|
702 | EventHandler handler = Refreshing;
|
---|
703 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
704 | }
|
---|
705 | public event EventHandler Refreshed;
|
---|
706 | private void OnRefreshed() {
|
---|
707 | EventHandler handler = Refreshed;
|
---|
708 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
709 | }
|
---|
710 |
|
---|
711 | private void platforms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Platform> e) {
|
---|
712 | try {
|
---|
713 | foreach (Platform p in e.Items)
|
---|
714 | CallAdminService(s => s.DeletePlatform(p.Id));
|
---|
715 | }
|
---|
716 | catch (Exception ex) {
|
---|
717 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
718 | }
|
---|
719 | }
|
---|
720 | private void dataTypes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataType> e) {
|
---|
721 | try {
|
---|
722 | foreach (DataType d in e.Items)
|
---|
723 | CallAdminService(s => s.DeleteDataType(d.Id));
|
---|
724 | }
|
---|
725 | catch (Exception ex) {
|
---|
726 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
727 | }
|
---|
728 | }
|
---|
729 | private void algorithmClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<AlgorithmClass> e) {
|
---|
730 | try {
|
---|
731 | foreach (AlgorithmClass a in e.Items)
|
---|
732 | CallAdminService(s => s.DeleteAlgorithmClass(a.Id));
|
---|
733 | }
|
---|
734 | catch (Exception ex) {
|
---|
735 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
736 | }
|
---|
737 | }
|
---|
738 | private void algorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Algorithm> e) {
|
---|
739 | try {
|
---|
740 | foreach (Algorithm a in e.Items)
|
---|
741 | CallAdminService(s => s.DeleteAlgorithm(a.Id));
|
---|
742 | }
|
---|
743 | catch (Exception ex) {
|
---|
744 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
745 | }
|
---|
746 | }
|
---|
747 | private void problemClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ProblemClass> e) {
|
---|
748 | try {
|
---|
749 | foreach (ProblemClass p in e.Items)
|
---|
750 | CallAdminService(s => s.DeleteProblemClass(p.Id));
|
---|
751 | }
|
---|
752 | catch (Exception ex) {
|
---|
753 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
754 | }
|
---|
755 | }
|
---|
756 | private void problems_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Problem> e) {
|
---|
757 | try {
|
---|
758 | foreach (Problem p in e.Items)
|
---|
759 | CallAdminService(s => s.DeleteProblem(p.Id));
|
---|
760 | }
|
---|
761 | catch (Exception ex) {
|
---|
762 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
763 | }
|
---|
764 | }
|
---|
765 | private void experiments_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Experiment> e) {
|
---|
766 | try {
|
---|
767 | foreach (Experiment exp in e.Items)
|
---|
768 | CallAdminService(s => s.DeleteExperiment(exp.Id));
|
---|
769 | }
|
---|
770 | catch (Exception ex) {
|
---|
771 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
772 | }
|
---|
773 | }
|
---|
774 | private void runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Run> e) {
|
---|
775 | try {
|
---|
776 | foreach (Run r in e.Items)
|
---|
777 | CallAdminService(s => s.DeleteRun(r.Id));
|
---|
778 | }
|
---|
779 | catch (Exception ex) {
|
---|
780 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
781 | }
|
---|
782 | }
|
---|
783 | #endregion
|
---|
784 |
|
---|
785 | #region Helpers
|
---|
786 | private void CallAdminService(Action<IOKBService> call) {
|
---|
787 | OKBServiceClient client = ClientFactory.CreateClient<OKBServiceClient, IOKBService>();
|
---|
788 | try {
|
---|
789 | call(client);
|
---|
790 | }
|
---|
791 | finally {
|
---|
792 | try {
|
---|
793 | client.Close();
|
---|
794 | }
|
---|
795 | catch (Exception) {
|
---|
796 | client.Abort();
|
---|
797 | }
|
---|
798 | }
|
---|
799 | }
|
---|
800 | private T CallAdminService<T>(Func<IOKBService, T> call) {
|
---|
801 | OKBServiceClient client = ClientFactory.CreateClient<OKBServiceClient, IOKBService>();
|
---|
802 | try {
|
---|
803 | return call(client);
|
---|
804 | }
|
---|
805 | finally {
|
---|
806 | try {
|
---|
807 | client.Close();
|
---|
808 | }
|
---|
809 | catch (Exception) {
|
---|
810 | client.Abort();
|
---|
811 | }
|
---|
812 | }
|
---|
813 | }
|
---|
814 | private T CallAuthenticationService<T>(Func<IAuthenticationService, T> call) {
|
---|
815 | AuthenticationServiceClient client = ClientFactory.CreateClient<AuthenticationServiceClient, IAuthenticationService>();
|
---|
816 | try {
|
---|
817 | return call(client);
|
---|
818 | }
|
---|
819 | finally {
|
---|
820 | try {
|
---|
821 | client.Close();
|
---|
822 | }
|
---|
823 | catch (Exception) {
|
---|
824 | client.Abort();
|
---|
825 | }
|
---|
826 | }
|
---|
827 | }
|
---|
828 | #endregion
|
---|
829 | }
|
---|
830 | }
|
---|