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<List<Platform>>(s => s.GetPlatforms()).OrderBy(x => x.Name));
|
---|
106 | dataTypes.AddRange(CallAdminService<List<DataType>>(s => s.GetDataTypes()).OrderBy(x => x.Name));
|
---|
107 | users = CallAuthenticationService<List<User>>(s => s.GetUsers()).OrderBy(x => x.Name);
|
---|
108 | algorithmClasses.AddRange(CallAdminService<List<AlgorithmClass>>(s => s.GetAlgorithmClasses()).OrderBy(x => x.Name));
|
---|
109 | algorithms.AddRange(CallAdminService<List<Algorithm>>(s => s.GetAlgorithms()).OrderBy(x => x.Name));
|
---|
110 | problemClasses.AddRange(CallAdminService<List<ProblemClass>>(s => s.GetProblemClasses()).OrderBy(x => x.Name));
|
---|
111 | problems.AddRange(CallAdminService<List<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.TypeName == type.AssemblyQualifiedName);
|
---|
188 | if (dataType == null) {
|
---|
189 | dataType = new DataType();
|
---|
190 | dataType.Name = type.Name;
|
---|
191 | dataType.TypeName = type.AssemblyQualifiedName;
|
---|
192 | dataType.PlatformId = Platforms.FirstOrDefault(x => x.Name == "HeuristicLab 3.3").Id;
|
---|
193 |
|
---|
194 | if (typeof(BoolValue).IsAssignableFrom(type))
|
---|
195 | dataType.SqlName = "bit";
|
---|
196 | else if (typeof(IntValue).IsAssignableFrom(type))
|
---|
197 | dataType.SqlName = "bigint";
|
---|
198 | else if (typeof(DoubleValue).IsAssignableFrom(type))
|
---|
199 | dataType.SqlName = "float";
|
---|
200 | else if (typeof(StringValue).IsAssignableFrom(type) || typeof(IStringConvertibleValue).IsAssignableFrom(type))
|
---|
201 | dataType.SqlName = "nvarchar";
|
---|
202 | else
|
---|
203 | dataType.SqlName = "varbinary";
|
---|
204 |
|
---|
205 | dataType.Store();
|
---|
206 | DataTypes.Add(dataType);
|
---|
207 | }
|
---|
208 | return dataType;
|
---|
209 | }
|
---|
210 | #endregion
|
---|
211 |
|
---|
212 | #region Algorithm Methods
|
---|
213 | public List<Guid> GetAlgorithmUsers(long algorithmId) {
|
---|
214 | try {
|
---|
215 | return CallAdminService<List<Guid>>(s => s.GetAlgorithmUsers(algorithmId));
|
---|
216 | }
|
---|
217 | catch (Exception ex) {
|
---|
218 | ErrorHandling.ShowErrorDialog("Refresh authorized algorithm users failed.", ex);
|
---|
219 | return null;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | public bool UpdateAlgorithmUsers(long algorithmId, List<Guid> users) {
|
---|
223 | try {
|
---|
224 | CallAdminService(s => s.UpdateAlgorithmUsers(algorithmId, users));
|
---|
225 | return true;
|
---|
226 | }
|
---|
227 | catch (Exception ex) {
|
---|
228 | ErrorHandling.ShowErrorDialog("Update authorized algorithm users failed.", ex);
|
---|
229 | return false;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | public AlgorithmData GetAlgorithmData(long algorithmId) {
|
---|
233 | try {
|
---|
234 | return CallAdminService<AlgorithmData>(s => s.GetAlgorithmData(algorithmId));
|
---|
235 | }
|
---|
236 | catch (Exception ex) {
|
---|
237 | ErrorHandling.ShowErrorDialog("Refresh algorithm data failed.", ex);
|
---|
238 | return null;
|
---|
239 | }
|
---|
240 | }
|
---|
241 | public bool UpdateAlgorithmData(AlgorithmData algorithmData) {
|
---|
242 | try {
|
---|
243 | CallAdminService(s => s.UpdateAlgorithmData(algorithmData));
|
---|
244 | return true;
|
---|
245 | }
|
---|
246 | catch (Exception ex) {
|
---|
247 | ErrorHandling.ShowErrorDialog("Update algorithm data failed.", ex);
|
---|
248 | return false;
|
---|
249 | }
|
---|
250 | }
|
---|
251 | #endregion
|
---|
252 |
|
---|
253 | #region Problem Methods
|
---|
254 | public List<Guid> GetProblemUsers(long problemId) {
|
---|
255 | try {
|
---|
256 | return CallAdminService<List<Guid>>(s => s.GetProblemUsers(problemId));
|
---|
257 | }
|
---|
258 | catch (Exception ex) {
|
---|
259 | ErrorHandling.ShowErrorDialog("Refresh authorized problem users failed.", ex);
|
---|
260 | return null;
|
---|
261 | }
|
---|
262 | }
|
---|
263 | public bool UpdateProblemUsers(long problemId, List<Guid> users) {
|
---|
264 | try {
|
---|
265 | CallAdminService(s => s.UpdateProblemUsers(problemId, users));
|
---|
266 | return true;
|
---|
267 | }
|
---|
268 | catch (Exception ex) {
|
---|
269 | ErrorHandling.ShowErrorDialog("Update authorized problem users failed.", ex);
|
---|
270 | return false;
|
---|
271 | }
|
---|
272 | }
|
---|
273 | public ProblemData GetProblemData(long problemId) {
|
---|
274 | try {
|
---|
275 | return CallAdminService<ProblemData>(s => s.GetProblemData(problemId));
|
---|
276 | }
|
---|
277 | catch (Exception ex) {
|
---|
278 | ErrorHandling.ShowErrorDialog("Refresh problem data failed.", ex);
|
---|
279 | return null;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | public bool UpdateProblemData(ProblemData problemData) {
|
---|
283 | try {
|
---|
284 | CallAdminService(s => s.UpdateProblemData(problemData));
|
---|
285 | return true;
|
---|
286 | }
|
---|
287 | catch (Exception ex) {
|
---|
288 | ErrorHandling.ShowErrorDialog("Update problem data failed.", ex);
|
---|
289 | return false;
|
---|
290 | }
|
---|
291 | }
|
---|
292 | #endregion
|
---|
293 |
|
---|
294 | #region AlgorithmParameter Methods
|
---|
295 | public AlgorithmParameter GetAlgorithmParameter(long id) {
|
---|
296 | try {
|
---|
297 | return CallAdminService<AlgorithmParameter>(s => s.GetAlgorithmParameter(id));
|
---|
298 | }
|
---|
299 | catch (Exception ex) {
|
---|
300 | ErrorHandling.ShowErrorDialog("Refresh algorithm parameter failed.", ex);
|
---|
301 | return null;
|
---|
302 | }
|
---|
303 | }
|
---|
304 | public ItemCollection<AlgorithmParameter> GetAlgorithmParameters(long algorithmId) {
|
---|
305 | try {
|
---|
306 | ItemCollection<AlgorithmParameter> parameters = new ItemCollection<AlgorithmParameter>();
|
---|
307 | parameters.AddRange(CallAdminService<List<AlgorithmParameter>>(s => s.GetAlgorithmParameters(algorithmId)).OrderBy(x => x.Name));
|
---|
308 | return parameters;
|
---|
309 | }
|
---|
310 | catch (Exception ex) {
|
---|
311 | ErrorHandling.ShowErrorDialog("Refresh algorithm parameters failed.", ex);
|
---|
312 | return null;
|
---|
313 | }
|
---|
314 | }
|
---|
315 | #endregion
|
---|
316 |
|
---|
317 | #region ProblemParameter Methods
|
---|
318 | public ProblemParameter GetProblemParameter(long id) {
|
---|
319 | try {
|
---|
320 | return CallAdminService<ProblemParameter>(s => s.GetProblemParameter(id));
|
---|
321 | }
|
---|
322 | catch (Exception ex) {
|
---|
323 | ErrorHandling.ShowErrorDialog("Refresh problem parameter failed.", ex);
|
---|
324 | return null;
|
---|
325 | }
|
---|
326 | }
|
---|
327 | public ItemCollection<ProblemParameter> GetProblemParameters(long problemId) {
|
---|
328 | try {
|
---|
329 | ItemCollection<ProblemParameter> parameters = new ItemCollection<ProblemParameter>();
|
---|
330 | parameters.AddRange(CallAdminService<List<ProblemParameter>>(s => s.GetProblemParameters(problemId)).OrderBy(x => x.Name));
|
---|
331 | return parameters;
|
---|
332 | }
|
---|
333 | catch (Exception ex) {
|
---|
334 | ErrorHandling.ShowErrorDialog("Refresh problem parameters failed.", ex);
|
---|
335 | return null;
|
---|
336 | }
|
---|
337 | }
|
---|
338 | #endregion
|
---|
339 |
|
---|
340 | #region Result Methods
|
---|
341 | public Result GetResult(long id) {
|
---|
342 | try {
|
---|
343 | return CallAdminService<Result>(s => s.GetResult(id));
|
---|
344 | }
|
---|
345 | catch (Exception ex) {
|
---|
346 | ErrorHandling.ShowErrorDialog("Refresh result failed.", ex);
|
---|
347 | return null;
|
---|
348 | }
|
---|
349 | }
|
---|
350 | public ItemCollection<Result> GetResults(long algorithmId) {
|
---|
351 | try {
|
---|
352 | ItemCollection<Result> results = new ItemCollection<Result>();
|
---|
353 | results.AddRange(CallAdminService<List<Result>>(s => s.GetResults(algorithmId)).OrderBy(x => x.Name));
|
---|
354 | return results;
|
---|
355 | }
|
---|
356 | catch (Exception ex) {
|
---|
357 | ErrorHandling.ShowErrorDialog("Refresh results failed.", ex);
|
---|
358 | return null;
|
---|
359 | }
|
---|
360 | }
|
---|
361 | #endregion
|
---|
362 |
|
---|
363 | #region Experiment Methods
|
---|
364 | public Experiment GetExperiment(long id) {
|
---|
365 | try {
|
---|
366 | return CallAdminService<Experiment>(s => s.GetExperiment(id));
|
---|
367 | }
|
---|
368 | catch (Exception ex) {
|
---|
369 | ErrorHandling.ShowErrorDialog("Refresh experiment failed.", ex);
|
---|
370 | return null;
|
---|
371 | }
|
---|
372 | }
|
---|
373 | public ItemCollection<Experiment> GetExperiments(long algorithmId, long problemId) {
|
---|
374 | try {
|
---|
375 | ItemCollection<Experiment> experiments = new ItemCollection<Experiment>();
|
---|
376 | experiments.AddRange(CallAdminService<List<Experiment>>(s => s.GetExperiments(algorithmId, problemId)));
|
---|
377 | experiments.ItemsRemoved += new CollectionItemsChangedEventHandler<Experiment>(experiments_ItemsRemoved);
|
---|
378 | return experiments;
|
---|
379 | }
|
---|
380 | catch (Exception ex) {
|
---|
381 | ErrorHandling.ShowErrorDialog("Refresh experiments failed.", ex);
|
---|
382 | return null;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | #endregion
|
---|
386 |
|
---|
387 | #region Run Methods
|
---|
388 | public ItemCollection<Run> GetRuns(long experimentId) {
|
---|
389 | try {
|
---|
390 | ItemCollection<Run> runs = new ItemCollection<Run>();
|
---|
391 | runs.AddRange(CallAdminService<List<Run>>(s => s.GetRuns(experimentId)).OrderByDescending(x => x.CreatedDate));
|
---|
392 | runs.ItemsRemoved += new CollectionItemsChangedEventHandler<Run>(runs_ItemsRemoved);
|
---|
393 | return runs;
|
---|
394 | }
|
---|
395 | catch (Exception ex) {
|
---|
396 | ErrorHandling.ShowErrorDialog("Refresh runs failed.", ex);
|
---|
397 | return null;
|
---|
398 | }
|
---|
399 | }
|
---|
400 | public bool AddRun(long algorithmId, long problemId, IAlgorithm algorithm) {
|
---|
401 | try {
|
---|
402 | IProblem problem = algorithm.Problem;
|
---|
403 |
|
---|
404 | ItemCollection<AlgorithmParameter> algorithmParameters = GetAlgorithmParameters(algorithmId);
|
---|
405 | List<AlgorithmParameterValue> algorithmParameterValues = CollectAlgorithmParameterValues(algorithmId, algorithmParameters, algorithm, "");
|
---|
406 | ItemCollection<ProblemParameter> problemParameters = GetProblemParameters(problemId);
|
---|
407 | List<ProblemParameterValue> problemParameterValues = CollectProblemParamterValues(problemId, problemParameters, problem, "");
|
---|
408 | ItemCollection<Result> results = GetResults(algorithmId);
|
---|
409 | List<ResultValue> resultValues = CollectResultValues(algorithmId, results, algorithm);
|
---|
410 |
|
---|
411 | Experiment exp = new Experiment();
|
---|
412 | exp.AlgorithmId = algorithmId;
|
---|
413 | exp.ProblemId = problemId;
|
---|
414 | exp.AlgorithmParameterValues = algorithmParameterValues;
|
---|
415 | exp.ProblemParameterValues = problemParameterValues;
|
---|
416 | exp.Store();
|
---|
417 |
|
---|
418 | Run r = new Run();
|
---|
419 | r.ExperimentId = exp.Id;
|
---|
420 | r.ClientId = Guid.NewGuid();
|
---|
421 | r.CreatedDate = DateTime.Now;
|
---|
422 | r.RandomSeed = ((IntValue)((IValueParameter)algorithm.Parameters["Seed"]).Value).Value;
|
---|
423 | r.ResultValues = resultValues;
|
---|
424 | r.Store();
|
---|
425 |
|
---|
426 | return true;
|
---|
427 | }
|
---|
428 | catch (Exception ex) {
|
---|
429 | ErrorHandling.ShowErrorDialog("Store run failed.", ex);
|
---|
430 | return false;
|
---|
431 | }
|
---|
432 | }
|
---|
433 |
|
---|
434 | private List<AlgorithmParameterValue> CollectAlgorithmParameterValues(long algorithmId, ItemCollection<AlgorithmParameter> parameters, IParameterizedItem item, string prefix) {
|
---|
435 | List<AlgorithmParameterValue> values = new List<AlgorithmParameterValue>();
|
---|
436 | foreach (IValueParameter param in item.Parameters.OfType<IValueParameter>()) {
|
---|
437 | if (param.GetsCollected && (param.Value != null) && (param.Name != "Seed")) {
|
---|
438 | AlgorithmParameter p = parameters.FirstOrDefault(x => x.Name == prefix + param.Name);
|
---|
439 | if (p == null) {
|
---|
440 | p = new AlgorithmParameter();
|
---|
441 | p.Name = prefix + param.Name;
|
---|
442 | p.Alias = prefix + param.Name;
|
---|
443 | p.Description = param.Description;
|
---|
444 | p.AlgorithmId = algorithmId;
|
---|
445 | p.DataTypeId = ConvertToDataType(param.DataType).Id;
|
---|
446 | p.Store();
|
---|
447 | parameters.Add(p);
|
---|
448 | }
|
---|
449 | AlgorithmParameterValue value = CreateAlgorithmParameterValue(param.Value);
|
---|
450 | value.AlgorithmParameterId = p.Id;
|
---|
451 | value.DataTypeId = ConvertToDataType(param.Value.GetType()).Id;
|
---|
452 | values.Add(value);
|
---|
453 | }
|
---|
454 |
|
---|
455 | if (param.Value is IParameterizedItem)
|
---|
456 | values.AddRange(CollectAlgorithmParameterValues(algorithmId, parameters, (IParameterizedItem)param.Value, (string.IsNullOrEmpty(prefix) ? param.Name : prefix + param.Name) + "."));
|
---|
457 | }
|
---|
458 | return values;
|
---|
459 | }
|
---|
460 | private AlgorithmParameterValue CreateAlgorithmParameterValue(IItem item) {
|
---|
461 | if (item is BoolValue) {
|
---|
462 | AlgorithmParameterBoolValue value = new AlgorithmParameterBoolValue();
|
---|
463 | value.Value = ((BoolValue)item).Value;
|
---|
464 | return value;
|
---|
465 | } else if (item is DoubleValue) {
|
---|
466 | AlgorithmParameterFloatValue value = new AlgorithmParameterFloatValue();
|
---|
467 | value.Value = ((DoubleValue)item).Value;
|
---|
468 | return value;
|
---|
469 | } else if (item is IntValue) {
|
---|
470 | AlgorithmParameterIntValue value = new AlgorithmParameterIntValue();
|
---|
471 | value.Value = ((IntValue)item).Value;
|
---|
472 | return value;
|
---|
473 | } else if (item is StringValue) {
|
---|
474 | AlgorithmParameterStringValue value = new AlgorithmParameterStringValue();
|
---|
475 | value.Value = ((StringValue)item).Value;
|
---|
476 | return value;
|
---|
477 | } else {
|
---|
478 | AlgorithmParameterBlobValue value = new AlgorithmParameterBlobValue();
|
---|
479 | try {
|
---|
480 | using (MemoryStream stream = new MemoryStream()) {
|
---|
481 | XmlGenerator.Serialize(item, stream);
|
---|
482 | stream.Close();
|
---|
483 | value.Value = stream.ToArray();
|
---|
484 | }
|
---|
485 | }
|
---|
486 | catch (Exception ex) {
|
---|
487 | ErrorHandling.ShowErrorDialog(ex);
|
---|
488 | }
|
---|
489 | return value;
|
---|
490 | }
|
---|
491 | }
|
---|
492 | private List<ProblemParameterValue> CollectProblemParamterValues(long problemId, ItemCollection<ProblemParameter> parameters, IParameterizedItem item, string prefix) {
|
---|
493 | List<ProblemParameterValue> values = new List<ProblemParameterValue>();
|
---|
494 | foreach (IValueParameter param in item.Parameters.OfType<IValueParameter>()) {
|
---|
495 | if (param.GetsCollected && (param.Value != null)) {
|
---|
496 | ProblemParameter p = parameters.FirstOrDefault(x => x.Name == prefix + param.Name);
|
---|
497 | if (p == null) {
|
---|
498 | p = new ProblemParameter();
|
---|
499 | p.Name = prefix + param.Name;
|
---|
500 | p.Alias = prefix + param.Name;
|
---|
501 | p.Description = param.Description;
|
---|
502 | p.ProblemId = problemId;
|
---|
503 | p.DataTypeId = ConvertToDataType(param.DataType).Id;
|
---|
504 | p.Store();
|
---|
505 | parameters.Add(p);
|
---|
506 | }
|
---|
507 | ProblemParameterValue value = CreateProblemParameterValue(param.Value);
|
---|
508 | value.ProblemParameterId = p.Id;
|
---|
509 | value.DataTypeId = ConvertToDataType(param.Value.GetType()).Id;
|
---|
510 | values.Add(value);
|
---|
511 | }
|
---|
512 |
|
---|
513 | if (param.Value is IParameterizedItem)
|
---|
514 | values.AddRange(CollectProblemParamterValues(problemId, parameters, (IParameterizedItem)param.Value, (string.IsNullOrEmpty(prefix) ? param.Name : prefix + param.Name) + "."));
|
---|
515 | }
|
---|
516 | return values;
|
---|
517 | }
|
---|
518 | private ProblemParameterValue CreateProblemParameterValue(IItem item) {
|
---|
519 | if (item is BoolValue) {
|
---|
520 | ProblemParameterBoolValue value = new ProblemParameterBoolValue();
|
---|
521 | value.Value = ((BoolValue)item).Value;
|
---|
522 | return value;
|
---|
523 | } else if (item is DoubleValue) {
|
---|
524 | ProblemParameterFloatValue value = new ProblemParameterFloatValue();
|
---|
525 | value.Value = ((DoubleValue)item).Value;
|
---|
526 | return value;
|
---|
527 | } else if (item is IntValue) {
|
---|
528 | ProblemParameterIntValue value = new ProblemParameterIntValue();
|
---|
529 | value.Value = ((IntValue)item).Value;
|
---|
530 | return value;
|
---|
531 | } else if (item is StringValue) {
|
---|
532 | ProblemParameterStringValue value = new ProblemParameterStringValue();
|
---|
533 | value.Value = ((StringValue)item).Value;
|
---|
534 | return value;
|
---|
535 | } else {
|
---|
536 | ProblemParameterBlobValue value = new ProblemParameterBlobValue();
|
---|
537 | try {
|
---|
538 | using (MemoryStream stream = new MemoryStream()) {
|
---|
539 | XmlGenerator.Serialize(item, stream);
|
---|
540 | stream.Close();
|
---|
541 | value.Value = stream.ToArray();
|
---|
542 | }
|
---|
543 | }
|
---|
544 | catch (Exception ex) {
|
---|
545 | ErrorHandling.ShowErrorDialog(ex);
|
---|
546 | }
|
---|
547 | return value;
|
---|
548 | }
|
---|
549 | }
|
---|
550 | private List<ResultValue> CollectResultValues(long algorithmId, ItemCollection<Result> results, IAlgorithm algorithm) {
|
---|
551 | List<ResultValue> values = new List<ResultValue>();
|
---|
552 | foreach (IResult result in algorithm.Results) {
|
---|
553 | if (result.Value != null) {
|
---|
554 | Result r = results.FirstOrDefault(x => x.Name == result.Name);
|
---|
555 | if (r == null) {
|
---|
556 | r = new Result();
|
---|
557 | r.Name = result.Name;
|
---|
558 | r.Alias = result.Name;
|
---|
559 | r.Description = result.Description;
|
---|
560 | r.AlgorithmId = algorithmId;
|
---|
561 | r.DataTypeId = ConvertToDataType(result.DataType).Id;
|
---|
562 | r.Store();
|
---|
563 | results.Add(r);
|
---|
564 | }
|
---|
565 | ResultValue value = CreateResultValue(result.Value);
|
---|
566 | value.ResultId = r.Id;
|
---|
567 | value.DataTypeId = ConvertToDataType(result.Value.GetType()).Id;
|
---|
568 | values.Add(value);
|
---|
569 | }
|
---|
570 | }
|
---|
571 | return values;
|
---|
572 | }
|
---|
573 | private ResultValue CreateResultValue(IItem item) {
|
---|
574 | if (item is BoolValue) {
|
---|
575 | ResultBoolValue value = new ResultBoolValue();
|
---|
576 | value.Value = ((BoolValue)item).Value;
|
---|
577 | return value;
|
---|
578 | } else if (item is DoubleValue) {
|
---|
579 | ResultFloatValue value = new ResultFloatValue();
|
---|
580 | value.Value = ((DoubleValue)item).Value;
|
---|
581 | return value;
|
---|
582 | } else if (item is IntValue) {
|
---|
583 | ResultIntValue value = new ResultIntValue();
|
---|
584 | value.Value = ((IntValue)item).Value;
|
---|
585 | return value;
|
---|
586 | } else if (item is StringValue) {
|
---|
587 | ResultStringValue value = new ResultStringValue();
|
---|
588 | value.Value = ((StringValue)item).Value;
|
---|
589 | return value;
|
---|
590 | } else {
|
---|
591 | ResultBlobValue value = new ResultBlobValue();
|
---|
592 | try {
|
---|
593 | using (MemoryStream stream = new MemoryStream()) {
|
---|
594 | XmlGenerator.Serialize(item, stream);
|
---|
595 | stream.Close();
|
---|
596 | value.Value = stream.ToArray();
|
---|
597 | }
|
---|
598 | }
|
---|
599 | catch (Exception ex) {
|
---|
600 | ErrorHandling.ShowErrorDialog(ex);
|
---|
601 | }
|
---|
602 | return value;
|
---|
603 | }
|
---|
604 | }
|
---|
605 | public RunCollection ConvertOKBRunsToOptimizationRuns(IItemCollection<Run> runs) {
|
---|
606 | RunCollection runCollection = new RunCollection();
|
---|
607 | foreach (Run run in runs) {
|
---|
608 | Optimization.Run r = new Optimization.Run();
|
---|
609 | foreach (ResultValue resultValue in run.ResultValues) {
|
---|
610 | Result result = GetResult(resultValue.ResultId);
|
---|
611 | if (resultValue is ResultBlobValue) {
|
---|
612 | IItem item = null;
|
---|
613 | using (MemoryStream stream = new MemoryStream(((ResultBlobValue)resultValue).Value)) {
|
---|
614 | try {
|
---|
615 | item = XmlParser.Deserialize<IItem>(stream);
|
---|
616 | }
|
---|
617 | catch (Exception ex) {
|
---|
618 | ErrorHandling.ShowErrorDialog(ex);
|
---|
619 | }
|
---|
620 | stream.Close();
|
---|
621 | }
|
---|
622 | r.Results.Add(result.Name, item);
|
---|
623 | } else if (resultValue is ResultBoolValue) {
|
---|
624 | r.Results.Add(result.Name, new BoolValue(((ResultBoolValue)resultValue).Value));
|
---|
625 | } else if (resultValue is ResultFloatValue) {
|
---|
626 | r.Results.Add(result.Name, new DoubleValue(((ResultFloatValue)resultValue).Value));
|
---|
627 | } else if (resultValue is ResultIntValue) {
|
---|
628 | r.Results.Add(result.Name, new IntValue((int)((ResultIntValue)resultValue).Value));
|
---|
629 | } else if (resultValue is ResultStringValue) {
|
---|
630 | r.Results.Add(result.Name, new StringValue(((ResultStringValue)resultValue).Value));
|
---|
631 | }
|
---|
632 | }
|
---|
633 |
|
---|
634 | Experiment exp = GetExperiment(run.ExperimentId);
|
---|
635 | foreach (AlgorithmParameterValue algorithmParameterValue in exp.AlgorithmParameterValues) {
|
---|
636 | AlgorithmParameter algorithmParameter = GetAlgorithmParameter(algorithmParameterValue.AlgorithmParameterId);
|
---|
637 | if (algorithmParameterValue is AlgorithmParameterBlobValue) {
|
---|
638 | IItem item = null;
|
---|
639 | using (MemoryStream stream = new MemoryStream(((AlgorithmParameterBlobValue)algorithmParameterValue).Value)) {
|
---|
640 | try {
|
---|
641 | item = XmlParser.Deserialize<IItem>(stream);
|
---|
642 | }
|
---|
643 | catch (Exception ex) {
|
---|
644 | ErrorHandling.ShowErrorDialog(ex);
|
---|
645 | }
|
---|
646 | stream.Close();
|
---|
647 | }
|
---|
648 | r.Parameters.Add(algorithmParameter.Name, item);
|
---|
649 | } else if (algorithmParameterValue is AlgorithmParameterBoolValue) {
|
---|
650 | r.Parameters.Add(algorithmParameter.Name, new BoolValue(((AlgorithmParameterBoolValue)algorithmParameterValue).Value));
|
---|
651 | } else if (algorithmParameterValue is AlgorithmParameterFloatValue) {
|
---|
652 | r.Parameters.Add(algorithmParameter.Name, new DoubleValue(((AlgorithmParameterFloatValue)algorithmParameterValue).Value));
|
---|
653 | } else if (algorithmParameterValue is AlgorithmParameterIntValue) {
|
---|
654 | r.Parameters.Add(algorithmParameter.Name, new IntValue((int)((AlgorithmParameterIntValue)algorithmParameterValue).Value));
|
---|
655 | } else if (algorithmParameterValue is AlgorithmParameterStringValue) {
|
---|
656 | r.Parameters.Add(algorithmParameter.Name, new StringValue(((AlgorithmParameterStringValue)algorithmParameterValue).Value));
|
---|
657 | }
|
---|
658 | }
|
---|
659 | foreach (ProblemParameterValue problemParameterValue in exp.ProblemParameterValues) {
|
---|
660 | ProblemParameter problemParameter = GetProblemParameter(problemParameterValue.ProblemParameterId);
|
---|
661 | if (problemParameterValue is ProblemParameterBlobValue) {
|
---|
662 | IItem item = null;
|
---|
663 | using (MemoryStream stream = new MemoryStream(((ProblemParameterBlobValue)problemParameterValue).Value)) {
|
---|
664 | try {
|
---|
665 | item = XmlParser.Deserialize<IItem>(stream);
|
---|
666 | }
|
---|
667 | catch (Exception ex) {
|
---|
668 | ErrorHandling.ShowErrorDialog(ex);
|
---|
669 | }
|
---|
670 | stream.Close();
|
---|
671 | }
|
---|
672 | r.Parameters.Add(problemParameter.Name, item);
|
---|
673 | } else if (problemParameterValue is ProblemParameterBoolValue) {
|
---|
674 | r.Parameters.Add(problemParameter.Name, new BoolValue(((ProblemParameterBoolValue)problemParameterValue).Value));
|
---|
675 | } else if (problemParameterValue is ProblemParameterFloatValue) {
|
---|
676 | r.Parameters.Add(problemParameter.Name, new DoubleValue(((ProblemParameterFloatValue)problemParameterValue).Value));
|
---|
677 | } else if (problemParameterValue is ProblemParameterIntValue) {
|
---|
678 | r.Parameters.Add(problemParameter.Name, new IntValue((int)((ProblemParameterIntValue)problemParameterValue).Value));
|
---|
679 | } else if (problemParameterValue is ProblemParameterStringValue) {
|
---|
680 | r.Parameters.Add(problemParameter.Name, new StringValue(((ProblemParameterStringValue)problemParameterValue).Value));
|
---|
681 | }
|
---|
682 | }
|
---|
683 | runCollection.Add(r);
|
---|
684 | }
|
---|
685 | return runCollection;
|
---|
686 | }
|
---|
687 | #endregion
|
---|
688 |
|
---|
689 | #region Query Methods
|
---|
690 | private IEnumerable<Filter> filters;
|
---|
691 | public IEnumerable<Filter> GetFilters(bool refresh) {
|
---|
692 | if (refresh || (filters == null)) {
|
---|
693 | try {
|
---|
694 | filters = CallAdminService<List<Filter>>(s => s.GetFilters());
|
---|
695 | }
|
---|
696 | catch (Exception ex) {
|
---|
697 | ErrorHandling.ShowErrorDialog("Refresh filters.", ex);
|
---|
698 | return null;
|
---|
699 | }
|
---|
700 | }
|
---|
701 | return filters;
|
---|
702 | }
|
---|
703 | public IEnumerable<Filter> GetFilters() {
|
---|
704 | return GetFilters(false);
|
---|
705 | }
|
---|
706 | public long QueryNumberOfRuns(Filter filter) {
|
---|
707 | try {
|
---|
708 | return CallAdminService<long>(x => x.QueryNumberOfRuns(filter));
|
---|
709 | }
|
---|
710 | catch (Exception ex) {
|
---|
711 | ErrorHandling.ShowErrorDialog("Refresh runs failed.", ex);
|
---|
712 | return -1;
|
---|
713 | }
|
---|
714 | }
|
---|
715 | public ItemCollection<Run> QueryRuns(Filter filter) {
|
---|
716 | try {
|
---|
717 | ItemCollection<Run> runs = new ItemCollection<Run>();
|
---|
718 | runs.AddRange(CallAdminService<List<Run>>(s => s.QueryRuns(filter)).OrderByDescending(x => x.CreatedDate));
|
---|
719 | return runs;
|
---|
720 | }
|
---|
721 | catch (Exception ex) {
|
---|
722 | ErrorHandling.ShowErrorDialog("Refresh runs failed.", ex);
|
---|
723 | return null;
|
---|
724 | }
|
---|
725 | }
|
---|
726 | #endregion
|
---|
727 |
|
---|
728 | #region Events
|
---|
729 | public event EventHandler Refreshing;
|
---|
730 | private void OnRefreshing() {
|
---|
731 | EventHandler handler = Refreshing;
|
---|
732 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
733 | }
|
---|
734 | public event EventHandler Refreshed;
|
---|
735 | private void OnRefreshed() {
|
---|
736 | EventHandler handler = Refreshed;
|
---|
737 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
738 | }
|
---|
739 |
|
---|
740 | private void platforms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Platform> e) {
|
---|
741 | try {
|
---|
742 | foreach (Platform p in e.Items)
|
---|
743 | CallAdminService(s => s.DeletePlatform(p.Id));
|
---|
744 | }
|
---|
745 | catch (Exception ex) {
|
---|
746 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
747 | }
|
---|
748 | }
|
---|
749 | private void dataTypes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataType> e) {
|
---|
750 | try {
|
---|
751 | foreach (DataType d in e.Items)
|
---|
752 | CallAdminService(s => s.DeleteDataType(d.Id));
|
---|
753 | }
|
---|
754 | catch (Exception ex) {
|
---|
755 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
756 | }
|
---|
757 | }
|
---|
758 | private void algorithmClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<AlgorithmClass> e) {
|
---|
759 | try {
|
---|
760 | foreach (AlgorithmClass a in e.Items)
|
---|
761 | CallAdminService(s => s.DeleteAlgorithmClass(a.Id));
|
---|
762 | }
|
---|
763 | catch (Exception ex) {
|
---|
764 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
765 | }
|
---|
766 | }
|
---|
767 | private void algorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Algorithm> e) {
|
---|
768 | try {
|
---|
769 | foreach (Algorithm a in e.Items)
|
---|
770 | CallAdminService(s => s.DeleteAlgorithm(a.Id));
|
---|
771 | }
|
---|
772 | catch (Exception ex) {
|
---|
773 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
774 | }
|
---|
775 | }
|
---|
776 | private void problemClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ProblemClass> e) {
|
---|
777 | try {
|
---|
778 | foreach (ProblemClass p in e.Items)
|
---|
779 | CallAdminService(s => s.DeleteProblemClass(p.Id));
|
---|
780 | }
|
---|
781 | catch (Exception ex) {
|
---|
782 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
783 | }
|
---|
784 | }
|
---|
785 | private void problems_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Problem> e) {
|
---|
786 | try {
|
---|
787 | foreach (Problem p in e.Items)
|
---|
788 | CallAdminService(s => s.DeleteProblem(p.Id));
|
---|
789 | }
|
---|
790 | catch (Exception ex) {
|
---|
791 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
792 | }
|
---|
793 | }
|
---|
794 | private void experiments_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Experiment> e) {
|
---|
795 | try {
|
---|
796 | foreach (Experiment exp in e.Items)
|
---|
797 | CallAdminService(s => s.DeleteExperiment(exp.Id));
|
---|
798 | }
|
---|
799 | catch (Exception ex) {
|
---|
800 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
801 | }
|
---|
802 | }
|
---|
803 | private void runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Run> e) {
|
---|
804 | try {
|
---|
805 | foreach (Run r in e.Items)
|
---|
806 | CallAdminService(s => s.DeleteRun(r.Id));
|
---|
807 | }
|
---|
808 | catch (Exception ex) {
|
---|
809 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
810 | }
|
---|
811 | }
|
---|
812 | #endregion
|
---|
813 |
|
---|
814 | #region Helpers
|
---|
815 | private void CallAdminService(Action<IOKBService> call) {
|
---|
816 | OKBServiceClient client = ClientFactory.CreateClient<OKBServiceClient, IOKBService>();
|
---|
817 | try {
|
---|
818 | call(client);
|
---|
819 | }
|
---|
820 | finally {
|
---|
821 | try {
|
---|
822 | client.Close();
|
---|
823 | }
|
---|
824 | catch (Exception) {
|
---|
825 | client.Abort();
|
---|
826 | }
|
---|
827 | }
|
---|
828 | }
|
---|
829 | private T CallAdminService<T>(Func<IOKBService, T> call) {
|
---|
830 | OKBServiceClient client = ClientFactory.CreateClient<OKBServiceClient, IOKBService>();
|
---|
831 | try {
|
---|
832 | return call(client);
|
---|
833 | }
|
---|
834 | finally {
|
---|
835 | try {
|
---|
836 | client.Close();
|
---|
837 | }
|
---|
838 | catch (Exception) {
|
---|
839 | client.Abort();
|
---|
840 | }
|
---|
841 | }
|
---|
842 | }
|
---|
843 | private T CallAuthenticationService<T>(Func<IAuthenticationService, T> call) {
|
---|
844 | AuthenticationServiceClient client = ClientFactory.CreateClient<AuthenticationServiceClient, IAuthenticationService>();
|
---|
845 | try {
|
---|
846 | return call(client);
|
---|
847 | }
|
---|
848 | finally {
|
---|
849 | try {
|
---|
850 | client.Close();
|
---|
851 | }
|
---|
852 | catch (Exception) {
|
---|
853 | client.Abort();
|
---|
854 | }
|
---|
855 | }
|
---|
856 | }
|
---|
857 | #endregion
|
---|
858 | }
|
---|
859 | }
|
---|