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 ItemCollection<Experiment> GetExperiments(long algorithmId, long problemId) {
|
---|
364 | try {
|
---|
365 | ItemCollection<Experiment> experiments = new ItemCollection<Experiment>();
|
---|
366 | experiments.AddRange(CallAdminService<Experiment[]>(s => s.GetExperiments(algorithmId, problemId)));
|
---|
367 | return experiments;
|
---|
368 | }
|
---|
369 | catch (Exception ex) {
|
---|
370 | ErrorHandling.ShowErrorDialog("Refresh experiments failed.", ex);
|
---|
371 | return null;
|
---|
372 | }
|
---|
373 | }
|
---|
374 | #endregion
|
---|
375 |
|
---|
376 | #region Run Methods
|
---|
377 | public ItemCollection<Run> GetRuns(long experimentId) {
|
---|
378 | try {
|
---|
379 | ItemCollection<Run> runs = new ItemCollection<Run>();
|
---|
380 | runs.AddRange(CallAdminService<Run[]>(s => s.GetRuns(experimentId)).OrderByDescending(x => x.FinishedDate));
|
---|
381 | return runs;
|
---|
382 | }
|
---|
383 | catch (Exception ex) {
|
---|
384 | ErrorHandling.ShowErrorDialog("Refresh runs failed.", ex);
|
---|
385 | return null;
|
---|
386 | }
|
---|
387 | }
|
---|
388 | public bool AddRun(long algorithmId, long problemId, IAlgorithm algorithm) {
|
---|
389 | try {
|
---|
390 | IProblem problem = algorithm.Problem;
|
---|
391 |
|
---|
392 | ItemCollection<AlgorithmParameter> algorithmParameters = GetAlgorithmParameters(algorithmId);
|
---|
393 | List<AlgorithmParameterValue> algorithmParameterValues = CollectAlgorithmParameterValues(algorithmId, algorithmParameters, algorithm, "");
|
---|
394 | ItemCollection<ProblemParameter> problemParameters = GetProblemParameters(problemId);
|
---|
395 | List<ProblemParameterValue> problemParameterValues = CollectProblemParamterValues(problemId, problemParameters, problem, "");
|
---|
396 | ItemCollection<Result> results = GetResults(algorithmId);
|
---|
397 | List<ResultValue> resultValues = CollectResultValues(algorithmId, results, algorithm);
|
---|
398 |
|
---|
399 | Experiment exp = new Experiment();
|
---|
400 | exp.AlgorithmId = algorithmId;
|
---|
401 | exp.ProblemId = problemId;
|
---|
402 | exp.AlgorithmParameterValues = algorithmParameterValues.ToArray();
|
---|
403 | exp.ProblemParameterValues = problemParameterValues.ToArray();
|
---|
404 | exp.Store();
|
---|
405 |
|
---|
406 | Run r = new Run();
|
---|
407 | r.ExperimentId = exp.Id;
|
---|
408 | r.ClientId = Guid.NewGuid();
|
---|
409 | r.FinishedDate = DateTime.Now;
|
---|
410 | r.RandomSeed = ((IntValue)((IValueParameter)algorithm.Parameters["Seed"]).Value).Value;
|
---|
411 | r.ResultValues = resultValues.ToArray();
|
---|
412 | r.Store();
|
---|
413 |
|
---|
414 | return true;
|
---|
415 | }
|
---|
416 | catch (Exception ex) {
|
---|
417 | ErrorHandling.ShowErrorDialog("Store run failed.", ex);
|
---|
418 | return false;
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 | private List<AlgorithmParameterValue> CollectAlgorithmParameterValues(long algorithmId, ItemCollection<AlgorithmParameter> parameters, IParameterizedItem item, string prefix) {
|
---|
423 | List<AlgorithmParameterValue> values = new List<AlgorithmParameterValue>();
|
---|
424 | foreach (IValueParameter param in item.Parameters.OfType<IValueParameter>()) {
|
---|
425 | if (param.GetsCollected && (param.Value != null) && (param.Name != "Seed")) {
|
---|
426 | AlgorithmParameter p = parameters.FirstOrDefault(x => x.Name == prefix + param.Name);
|
---|
427 | if (p == null) {
|
---|
428 | p = new AlgorithmParameter();
|
---|
429 | p.Name = prefix + param.Name;
|
---|
430 | p.Alias = prefix + param.Name;
|
---|
431 | p.Description = param.Description;
|
---|
432 | p.AlgorithmId = algorithmId;
|
---|
433 | p.DataTypeId = ConvertToDataType(param.DataType).Id;
|
---|
434 | p.Store();
|
---|
435 | parameters.Add(p);
|
---|
436 | }
|
---|
437 | AlgorithmParameterValue value = CreateAlgorithmParameterValue(param.Value);
|
---|
438 | value.AlgorithmParameterId = p.Id;
|
---|
439 | value.DataTypeId = ConvertToDataType(param.Value.GetType()).Id;
|
---|
440 | values.Add(value);
|
---|
441 | }
|
---|
442 |
|
---|
443 | if (param.Value is IParameterizedItem)
|
---|
444 | values.AddRange(CollectAlgorithmParameterValues(algorithmId, parameters, (IParameterizedItem)param.Value, (string.IsNullOrEmpty(prefix) ? param.Name : prefix + param.Name) + "."));
|
---|
445 | }
|
---|
446 | return values;
|
---|
447 | }
|
---|
448 | private AlgorithmParameterValue CreateAlgorithmParameterValue(IItem item) {
|
---|
449 | if (item is BoolValue) {
|
---|
450 | AlgorithmParameterBoolValue value = new AlgorithmParameterBoolValue();
|
---|
451 | value.Value = ((BoolValue)item).Value;
|
---|
452 | return value;
|
---|
453 | } else if (item is DoubleValue) {
|
---|
454 | AlgorithmParameterFloatValue value = new AlgorithmParameterFloatValue();
|
---|
455 | value.Value = ((DoubleValue)item).Value;
|
---|
456 | return value;
|
---|
457 | } else if (item is IntValue) {
|
---|
458 | AlgorithmParameterIntValue value = new AlgorithmParameterIntValue();
|
---|
459 | value.Value = ((IntValue)item).Value;
|
---|
460 | return value;
|
---|
461 | } else if (item is StringValue) {
|
---|
462 | AlgorithmParameterStringValue value = new AlgorithmParameterStringValue();
|
---|
463 | value.Value = ((StringValue)item).Value;
|
---|
464 | return value;
|
---|
465 | } else {
|
---|
466 | AlgorithmParameterBlobValue value = new AlgorithmParameterBlobValue();
|
---|
467 | try {
|
---|
468 | using (MemoryStream stream = new MemoryStream()) {
|
---|
469 | XmlGenerator.Serialize(item, stream);
|
---|
470 | stream.Close();
|
---|
471 | value.Value = stream.ToArray();
|
---|
472 | }
|
---|
473 | }
|
---|
474 | catch (Exception ex) {
|
---|
475 | ErrorHandling.ShowErrorDialog(ex);
|
---|
476 | }
|
---|
477 | return value;
|
---|
478 | }
|
---|
479 | }
|
---|
480 | private List<ProblemParameterValue> CollectProblemParamterValues(long problemId, ItemCollection<ProblemParameter> parameters, IParameterizedItem item, string prefix) {
|
---|
481 | List<ProblemParameterValue> values = new List<ProblemParameterValue>();
|
---|
482 | foreach (IValueParameter param in item.Parameters.OfType<IValueParameter>()) {
|
---|
483 | if (param.GetsCollected && (param.Value != null)) {
|
---|
484 | ProblemParameter p = parameters.FirstOrDefault(x => x.Name == prefix + param.Name);
|
---|
485 | if (p == null) {
|
---|
486 | p = new ProblemParameter();
|
---|
487 | p.Name = prefix + param.Name;
|
---|
488 | p.Alias = prefix + param.Name;
|
---|
489 | p.Description = param.Description;
|
---|
490 | p.ProblemId = problemId;
|
---|
491 | p.DataTypeId = ConvertToDataType(param.DataType).Id;
|
---|
492 | p.Store();
|
---|
493 | parameters.Add(p);
|
---|
494 | }
|
---|
495 | ProblemParameterValue value = CreateProblemParameterValue(param.Value);
|
---|
496 | value.ProblemParameterId = p.Id;
|
---|
497 | value.DataTypeId = ConvertToDataType(param.Value.GetType()).Id;
|
---|
498 | values.Add(value);
|
---|
499 | }
|
---|
500 |
|
---|
501 | if (param.Value is IParameterizedItem)
|
---|
502 | values.AddRange(CollectProblemParamterValues(problemId, parameters, (IParameterizedItem)param.Value, (string.IsNullOrEmpty(prefix) ? param.Name : prefix + param.Name) + "."));
|
---|
503 | }
|
---|
504 | return values;
|
---|
505 | }
|
---|
506 | private ProblemParameterValue CreateProblemParameterValue(IItem item) {
|
---|
507 | if (item is BoolValue) {
|
---|
508 | ProblemParameterBoolValue value = new ProblemParameterBoolValue();
|
---|
509 | value.Value = ((BoolValue)item).Value;
|
---|
510 | return value;
|
---|
511 | } else if (item is DoubleValue) {
|
---|
512 | ProblemParameterFloatValue value = new ProblemParameterFloatValue();
|
---|
513 | value.Value = ((DoubleValue)item).Value;
|
---|
514 | return value;
|
---|
515 | } else if (item is IntValue) {
|
---|
516 | ProblemParameterIntValue value = new ProblemParameterIntValue();
|
---|
517 | value.Value = ((IntValue)item).Value;
|
---|
518 | return value;
|
---|
519 | } else if (item is StringValue) {
|
---|
520 | ProblemParameterStringValue value = new ProblemParameterStringValue();
|
---|
521 | value.Value = ((StringValue)item).Value;
|
---|
522 | return value;
|
---|
523 | } else {
|
---|
524 | ProblemParameterBlobValue value = new ProblemParameterBlobValue();
|
---|
525 | try {
|
---|
526 | using (MemoryStream stream = new MemoryStream()) {
|
---|
527 | XmlGenerator.Serialize(item, stream);
|
---|
528 | stream.Close();
|
---|
529 | value.Value = stream.ToArray();
|
---|
530 | }
|
---|
531 | }
|
---|
532 | catch (Exception ex) {
|
---|
533 | ErrorHandling.ShowErrorDialog(ex);
|
---|
534 | }
|
---|
535 | return value;
|
---|
536 | }
|
---|
537 | }
|
---|
538 | private List<ResultValue> CollectResultValues(long algorithmId, ItemCollection<Result> results, IAlgorithm algorithm) {
|
---|
539 | List<ResultValue> values = new List<ResultValue>();
|
---|
540 | foreach (IResult result in algorithm.Results) {
|
---|
541 | if (result.Value != null) {
|
---|
542 | Result r = results.FirstOrDefault(x => x.Name == result.Name);
|
---|
543 | if (r == null) {
|
---|
544 | r = new Result();
|
---|
545 | r.Name = result.Name;
|
---|
546 | r.Alias = result.Name;
|
---|
547 | r.Description = result.Description;
|
---|
548 | r.AlgorithmId = algorithmId;
|
---|
549 | r.DataTypeId = ConvertToDataType(result.DataType).Id;
|
---|
550 | r.Store();
|
---|
551 | results.Add(r);
|
---|
552 | }
|
---|
553 | ResultValue value = CreateResultValue(result.Value);
|
---|
554 | value.ResultId = r.Id;
|
---|
555 | value.DataTypeId = ConvertToDataType(result.Value.GetType()).Id;
|
---|
556 | values.Add(value);
|
---|
557 | }
|
---|
558 | }
|
---|
559 | return values;
|
---|
560 | }
|
---|
561 | private ResultValue CreateResultValue(IItem item) {
|
---|
562 | if (item is BoolValue) {
|
---|
563 | ResultBoolValue value = new ResultBoolValue();
|
---|
564 | value.Value = ((BoolValue)item).Value;
|
---|
565 | return value;
|
---|
566 | } else if (item is DoubleValue) {
|
---|
567 | ResultFloatValue value = new ResultFloatValue();
|
---|
568 | value.Value = ((DoubleValue)item).Value;
|
---|
569 | return value;
|
---|
570 | } else if (item is IntValue) {
|
---|
571 | ResultIntValue value = new ResultIntValue();
|
---|
572 | value.Value = ((IntValue)item).Value;
|
---|
573 | return value;
|
---|
574 | } else if (item is StringValue) {
|
---|
575 | ResultStringValue value = new ResultStringValue();
|
---|
576 | value.Value = ((StringValue)item).Value;
|
---|
577 | return value;
|
---|
578 | } else {
|
---|
579 | ResultBlobValue value = new ResultBlobValue();
|
---|
580 | try {
|
---|
581 | using (MemoryStream stream = new MemoryStream()) {
|
---|
582 | XmlGenerator.Serialize(item, stream);
|
---|
583 | stream.Close();
|
---|
584 | value.Value = stream.ToArray();
|
---|
585 | }
|
---|
586 | }
|
---|
587 | catch (Exception ex) {
|
---|
588 | ErrorHandling.ShowErrorDialog(ex);
|
---|
589 | }
|
---|
590 | return value;
|
---|
591 | }
|
---|
592 | }
|
---|
593 | #endregion
|
---|
594 |
|
---|
595 | #region Events
|
---|
596 | public event EventHandler Refreshing;
|
---|
597 | private void OnRefreshing() {
|
---|
598 | EventHandler handler = Refreshing;
|
---|
599 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
600 | }
|
---|
601 | public event EventHandler Refreshed;
|
---|
602 | private void OnRefreshed() {
|
---|
603 | EventHandler handler = Refreshed;
|
---|
604 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
605 | }
|
---|
606 |
|
---|
607 | private void platforms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Platform> e) {
|
---|
608 | try {
|
---|
609 | foreach (Platform p in e.Items)
|
---|
610 | CallAdminService(s => s.DeletePlatform(p.Id));
|
---|
611 | }
|
---|
612 | catch (Exception ex) {
|
---|
613 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
614 | }
|
---|
615 | }
|
---|
616 | private void dataTypes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataType> e) {
|
---|
617 | try {
|
---|
618 | foreach (DataType d in e.Items)
|
---|
619 | CallAdminService(s => s.DeleteDataType(d.Id));
|
---|
620 | }
|
---|
621 | catch (Exception ex) {
|
---|
622 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
623 | }
|
---|
624 | }
|
---|
625 | private void algorithmClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<AlgorithmClass> e) {
|
---|
626 | try {
|
---|
627 | foreach (AlgorithmClass a in e.Items)
|
---|
628 | CallAdminService(s => s.DeleteAlgorithmClass(a.Id));
|
---|
629 | }
|
---|
630 | catch (Exception ex) {
|
---|
631 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
632 | }
|
---|
633 | }
|
---|
634 | private void algorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Algorithm> e) {
|
---|
635 | try {
|
---|
636 | foreach (Algorithm a in e.Items)
|
---|
637 | CallAdminService(s => s.DeleteAlgorithm(a.Id));
|
---|
638 | }
|
---|
639 | catch (Exception ex) {
|
---|
640 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
641 | }
|
---|
642 | }
|
---|
643 | private void problemClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ProblemClass> e) {
|
---|
644 | try {
|
---|
645 | foreach (ProblemClass p in e.Items)
|
---|
646 | CallAdminService(s => s.DeleteProblemClass(p.Id));
|
---|
647 | }
|
---|
648 | catch (Exception ex) {
|
---|
649 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
650 | }
|
---|
651 | }
|
---|
652 | private void problems_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Problem> e) {
|
---|
653 | try {
|
---|
654 | foreach (Problem p in e.Items)
|
---|
655 | CallAdminService(s => s.DeleteProblem(p.Id));
|
---|
656 | }
|
---|
657 | catch (Exception ex) {
|
---|
658 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
659 | }
|
---|
660 | }
|
---|
661 | #endregion
|
---|
662 |
|
---|
663 | #region Helpers
|
---|
664 | private void CallAdminService(Action<IOKBService> call) {
|
---|
665 | OKBServiceClient client = ClientFactory.CreateClient<OKBServiceClient, IOKBService>();
|
---|
666 | try {
|
---|
667 | call(client);
|
---|
668 | }
|
---|
669 | finally {
|
---|
670 | try {
|
---|
671 | client.Close();
|
---|
672 | }
|
---|
673 | catch (Exception) {
|
---|
674 | client.Abort();
|
---|
675 | }
|
---|
676 | }
|
---|
677 | }
|
---|
678 | private T CallAdminService<T>(Func<IOKBService, T> call) {
|
---|
679 | OKBServiceClient client = ClientFactory.CreateClient<OKBServiceClient, IOKBService>();
|
---|
680 | try {
|
---|
681 | return call(client);
|
---|
682 | }
|
---|
683 | finally {
|
---|
684 | try {
|
---|
685 | client.Close();
|
---|
686 | }
|
---|
687 | catch (Exception) {
|
---|
688 | client.Abort();
|
---|
689 | }
|
---|
690 | }
|
---|
691 | }
|
---|
692 | private T CallAuthenticationService<T>(Func<IAuthenticationService, T> call) {
|
---|
693 | AuthenticationServiceClient client = ClientFactory.CreateClient<AuthenticationServiceClient, IAuthenticationService>();
|
---|
694 | try {
|
---|
695 | return call(client);
|
---|
696 | }
|
---|
697 | finally {
|
---|
698 | try {
|
---|
699 | client.Close();
|
---|
700 | }
|
---|
701 | catch (Exception) {
|
---|
702 | client.Abort();
|
---|
703 | }
|
---|
704 | }
|
---|
705 | }
|
---|
706 | #endregion
|
---|
707 | }
|
---|
708 | }
|
---|