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.Linq;
|
---|
25 | using HeuristicLab.Clients.Common;
|
---|
26 | using HeuristicLab.Collections;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.PluginInfrastructure;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Clients.OKB {
|
---|
34 | [Item("OKBClient", "Client for accessing the OKB.")]
|
---|
35 | public sealed class OKBClient : IContent {
|
---|
36 | private static OKBClient instance;
|
---|
37 | public static OKBClient Instance {
|
---|
38 | get {
|
---|
39 | if (instance == null) instance = new OKBClient();
|
---|
40 | return instance;
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | #region Properties
|
---|
45 | private ItemCollection<Platform> platforms;
|
---|
46 | public ItemCollection<Platform> Platforms {
|
---|
47 | get { return platforms; }
|
---|
48 | }
|
---|
49 | private ItemCollection<DataType> dataTypes;
|
---|
50 | public ItemCollection<DataType> DataTypes {
|
---|
51 | get { return dataTypes; }
|
---|
52 | }
|
---|
53 | private IEnumerable<User> users;
|
---|
54 | public IEnumerable<User> Users {
|
---|
55 | get { return users; }
|
---|
56 | }
|
---|
57 | private ItemCollection<AlgorithmClass> algorithmClasses;
|
---|
58 | public ItemCollection<AlgorithmClass> AlgorithmClasses {
|
---|
59 | get { return algorithmClasses; }
|
---|
60 | }
|
---|
61 | private ItemCollection<Algorithm> algorithms;
|
---|
62 | public ItemCollection<Algorithm> Algorithms {
|
---|
63 | get { return algorithms; }
|
---|
64 | }
|
---|
65 | private ItemCollection<ProblemClass> problemClasses;
|
---|
66 | public ItemCollection<ProblemClass> ProblemClasses {
|
---|
67 | get { return problemClasses; }
|
---|
68 | }
|
---|
69 | private ItemCollection<Problem> problems;
|
---|
70 | public ItemCollection<Problem> Problems {
|
---|
71 | get { return problems; }
|
---|
72 | }
|
---|
73 | #endregion
|
---|
74 |
|
---|
75 | private OKBClient() {
|
---|
76 | platforms = new ItemCollection<Platform>();
|
---|
77 | platforms.ItemsRemoved += new CollectionItemsChangedEventHandler<Platform>(platforms_ItemsRemoved);
|
---|
78 | dataTypes = new ItemCollection<DataType>();
|
---|
79 | dataTypes.ItemsRemoved += new CollectionItemsChangedEventHandler<DataType>(dataTypes_ItemsRemoved);
|
---|
80 | algorithmClasses = new ItemCollection<AlgorithmClass>();
|
---|
81 | algorithmClasses.ItemsRemoved += new CollectionItemsChangedEventHandler<AlgorithmClass>(algorithmClasses_ItemsRemoved);
|
---|
82 | algorithms = new ItemCollection<Algorithm>();
|
---|
83 | algorithms.ItemsRemoved += new CollectionItemsChangedEventHandler<Algorithm>(algorithms_ItemsRemoved);
|
---|
84 | problemClasses = new ItemCollection<ProblemClass>();
|
---|
85 | problemClasses.ItemsRemoved += new CollectionItemsChangedEventHandler<ProblemClass>(problemClasses_ItemsRemoved);
|
---|
86 | problems = new ItemCollection<Problem>();
|
---|
87 | problems.ItemsRemoved += new CollectionItemsChangedEventHandler<Problem>(problems_ItemsRemoved);
|
---|
88 | }
|
---|
89 |
|
---|
90 | #region Refresh
|
---|
91 | public void Refresh() {
|
---|
92 | OnRefreshing();
|
---|
93 |
|
---|
94 | platforms.Clear();
|
---|
95 | dataTypes.Clear();
|
---|
96 | algorithmClasses.Clear();
|
---|
97 | algorithms.Clear();
|
---|
98 | problemClasses.Clear();
|
---|
99 | problems.Clear();
|
---|
100 |
|
---|
101 | var call = new Func<Exception>(delegate() {
|
---|
102 | try {
|
---|
103 | platforms.AddRange(CallAdminService<Platform[]>(s => s.GetPlatforms()).OrderBy(x => x.Name));
|
---|
104 | dataTypes.AddRange(CallAdminService<DataType[]>(s => s.GetDataTypes()).OrderBy(x => x.Name));
|
---|
105 | users = CallAuthenticationService<User[]>(s => s.GetUsers()).OrderBy(x => x.Name);
|
---|
106 | algorithmClasses.AddRange(CallAdminService<AlgorithmClass[]>(s => s.GetAlgorithmClasses()).OrderBy(x => x.Name));
|
---|
107 | algorithms.AddRange(CallAdminService<Algorithm[]>(s => s.GetAlgorithms()).OrderBy(x => x.Name));
|
---|
108 | problemClasses.AddRange(CallAdminService<ProblemClass[]>(s => s.GetProblemClasses()).OrderBy(x => x.Name));
|
---|
109 | problems.AddRange(CallAdminService<Problem[]>(s => s.GetProblems()).OrderBy(x => x.Name));
|
---|
110 | return null;
|
---|
111 | }
|
---|
112 | catch (Exception ex) {
|
---|
113 | return ex;
|
---|
114 | }
|
---|
115 | });
|
---|
116 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
117 | Exception ex = call.EndInvoke(result);
|
---|
118 | if (ex != null) ErrorHandling.ShowErrorDialog("Refresh failed.", ex);
|
---|
119 | OnRefreshed();
|
---|
120 | }, null);
|
---|
121 | }
|
---|
122 | #endregion
|
---|
123 |
|
---|
124 | #region Store
|
---|
125 | public bool Store(IOKBItem item) {
|
---|
126 | try {
|
---|
127 | if (item.Id == 0) {
|
---|
128 | if (item is Platform)
|
---|
129 | item.Id = CallAdminService<long>(s => s.AddPlatform((Platform)item));
|
---|
130 | else if (item is DataType)
|
---|
131 | item.Id = CallAdminService<long>(s => s.AddDataType((DataType)item));
|
---|
132 | else if (item is AlgorithmClass)
|
---|
133 | item.Id = CallAdminService<long>(s => s.AddAlgorithmClass((AlgorithmClass)item));
|
---|
134 | else if (item is Algorithm)
|
---|
135 | item.Id = CallAdminService<long>(s => s.AddAlgorithm((Algorithm)item));
|
---|
136 | else if (item is AlgorithmParameter)
|
---|
137 | item.Id = CallAdminService<long>(s => s.AddAlgorithmParameter((AlgorithmParameter)item));
|
---|
138 | else if (item is ProblemClass)
|
---|
139 | item.Id = CallAdminService<long>(s => s.AddProblemClass((ProblemClass)item));
|
---|
140 | else if (item is Problem)
|
---|
141 | item.Id = CallAdminService<long>(s => s.AddProblem((Problem)item));
|
---|
142 | else if (item is ProblemParameter)
|
---|
143 | item.Id = CallAdminService<long>(s => s.AddProblemParameter((ProblemParameter)item));
|
---|
144 | else if (item is Result)
|
---|
145 | item.Id = CallAdminService<long>(s => s.AddResult((Result)item));
|
---|
146 | else if (item is Experiment)
|
---|
147 | item.Id = CallAdminService<long>(s => s.AddExperiment((Experiment)item));
|
---|
148 | else if (item is Run)
|
---|
149 | item.Id = CallAdminService<long>(s => s.AddRun((Run)item));
|
---|
150 | } else {
|
---|
151 | if (item is Platform)
|
---|
152 | CallAdminService(s => s.UpdatePlatform((Platform)item));
|
---|
153 | else if (item is DataType)
|
---|
154 | CallAdminService(s => s.UpdateDataType((DataType)item));
|
---|
155 | else if (item is AlgorithmClass)
|
---|
156 | CallAdminService(s => s.UpdateAlgorithmClass((AlgorithmClass)item));
|
---|
157 | else if (item is Algorithm)
|
---|
158 | CallAdminService(s => s.UpdateAlgorithm((Algorithm)item));
|
---|
159 | else if (item is AlgorithmParameter)
|
---|
160 | CallAdminService(s => s.UpdateAlgorithmParameter((AlgorithmParameter)item));
|
---|
161 | else if (item is ProblemClass)
|
---|
162 | CallAdminService(s => s.UpdateProblemClass((ProblemClass)item));
|
---|
163 | else if (item is Problem)
|
---|
164 | CallAdminService(s => s.UpdateProblem((Problem)item));
|
---|
165 | else if (item is ProblemParameter)
|
---|
166 | CallAdminService(s => s.UpdateProblemParameter((ProblemParameter)item));
|
---|
167 | else if (item is Result)
|
---|
168 | CallAdminService(s => s.UpdateResult((Result)item));
|
---|
169 | else if (item is Experiment)
|
---|
170 | item.Id = CallAdminService<long>(s => s.AddExperiment((Experiment)item));
|
---|
171 | else if (item is Run)
|
---|
172 | item.Id = CallAdminService<long>(s => s.AddRun((Run)item));
|
---|
173 | }
|
---|
174 | return true;
|
---|
175 | }
|
---|
176 | catch (Exception ex) {
|
---|
177 | ErrorHandling.ShowErrorDialog("Store failed.", ex);
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | #endregion
|
---|
182 |
|
---|
183 | #region DataType Methods
|
---|
184 | public DataType ConvertToDataType(Type type) {
|
---|
185 | DataType dataType = DataTypes.FirstOrDefault(x => x.Name == type.AssemblyQualifiedName);
|
---|
186 | if (dataType == null) {
|
---|
187 | dataType = new DataType();
|
---|
188 | dataType.Name = type.AssemblyQualifiedName;
|
---|
189 | dataType.PlatformId = Platforms.FirstOrDefault(x => x.Name == "HeuristicLab 3.3").Id;
|
---|
190 |
|
---|
191 | if (typeof(BoolValue).IsAssignableFrom(type))
|
---|
192 | dataType.SqlName = "bit";
|
---|
193 | else if (typeof(IntValue).IsAssignableFrom(type))
|
---|
194 | dataType.SqlName = "bigint";
|
---|
195 | else if (typeof(DoubleValue).IsAssignableFrom(type))
|
---|
196 | dataType.SqlName = "float";
|
---|
197 | else if (typeof(StringValue).IsAssignableFrom(type) || typeof(IStringConvertibleValue).IsAssignableFrom(type))
|
---|
198 | dataType.SqlName = "nvarchar";
|
---|
199 | else
|
---|
200 | dataType.SqlName = "varbinary";
|
---|
201 |
|
---|
202 | dataType.Store();
|
---|
203 | DataTypes.Add(dataType);
|
---|
204 | }
|
---|
205 | return dataType;
|
---|
206 | }
|
---|
207 | #endregion
|
---|
208 |
|
---|
209 | #region Algorithm Methods
|
---|
210 | public Guid[] GetAlgorithmUsers(long algorithmId) {
|
---|
211 | try {
|
---|
212 | return CallAdminService<Guid[]>(s => s.GetAlgorithmUsers(algorithmId));
|
---|
213 | }
|
---|
214 | catch (Exception ex) {
|
---|
215 | ErrorHandling.ShowErrorDialog("Refresh authorized algorithm users failed.", ex);
|
---|
216 | return null;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | public bool UpdateAlgorithmUsers(long algorithmId, Guid[] users) {
|
---|
220 | try {
|
---|
221 | CallAdminService(s => s.UpdateAlgorithmUsers(algorithmId, users));
|
---|
222 | return true;
|
---|
223 | }
|
---|
224 | catch (Exception ex) {
|
---|
225 | ErrorHandling.ShowErrorDialog("Update authorized algorithm users failed.", ex);
|
---|
226 | return false;
|
---|
227 | }
|
---|
228 | }
|
---|
229 | public AlgorithmData GetAlgorithmData(long algorithmId) {
|
---|
230 | try {
|
---|
231 | return CallAdminService<AlgorithmData>(s => s.GetAlgorithmData(algorithmId));
|
---|
232 | }
|
---|
233 | catch (Exception ex) {
|
---|
234 | ErrorHandling.ShowErrorDialog("Refresh algorithm data failed.", ex);
|
---|
235 | return null;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | public bool UpdateAlgorithmData(AlgorithmData algorithmData) {
|
---|
239 | try {
|
---|
240 | CallAdminService(s => s.UpdateAlgorithmData(algorithmData));
|
---|
241 | return true;
|
---|
242 | }
|
---|
243 | catch (Exception ex) {
|
---|
244 | ErrorHandling.ShowErrorDialog("Update algorithm data failed.", ex);
|
---|
245 | return false;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | public ItemCollection<AlgorithmParameter> GetAlgorithmParameters(long algorithmId) {
|
---|
249 | try {
|
---|
250 | ItemCollection<AlgorithmParameter> parameters = new ItemCollection<AlgorithmParameter>();
|
---|
251 | parameters.AddRange(CallAdminService<AlgorithmParameter[]>(s => s.GetAlgorithmParameters(algorithmId)).OrderBy(x => x.Name));
|
---|
252 | return parameters;
|
---|
253 | }
|
---|
254 | catch (Exception ex) {
|
---|
255 | ErrorHandling.ShowErrorDialog("Refresh algorithm parameters failed.", ex);
|
---|
256 | return null;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | public ItemCollection<Result> GetResults(long algorithmId) {
|
---|
260 | try {
|
---|
261 | ItemCollection<Result> results = new ItemCollection<Result>();
|
---|
262 | results.AddRange(CallAdminService<Result[]>(s => s.GetResults(algorithmId)).OrderBy(x => x.Name));
|
---|
263 | return results;
|
---|
264 | }
|
---|
265 | catch (Exception ex) {
|
---|
266 | ErrorHandling.ShowErrorDialog("Refresh results failed.", ex);
|
---|
267 | return null;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | #endregion
|
---|
271 |
|
---|
272 | #region Problem Methods
|
---|
273 | public Guid[] GetProblemUsers(long problemId) {
|
---|
274 | try {
|
---|
275 | return CallAdminService<Guid[]>(s => s.GetProblemUsers(problemId));
|
---|
276 | }
|
---|
277 | catch (Exception ex) {
|
---|
278 | ErrorHandling.ShowErrorDialog("Refresh authorized problem users failed.", ex);
|
---|
279 | return null;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | public bool UpdateProblemUsers(long problemId, Guid[] users) {
|
---|
283 | try {
|
---|
284 | CallAdminService(s => s.UpdateProblemUsers(problemId, users));
|
---|
285 | return true;
|
---|
286 | }
|
---|
287 | catch (Exception ex) {
|
---|
288 | ErrorHandling.ShowErrorDialog("Update authorized problem users failed.", ex);
|
---|
289 | return false;
|
---|
290 | }
|
---|
291 | }
|
---|
292 | public ProblemData GetProblemData(long problemId) {
|
---|
293 | try {
|
---|
294 | return CallAdminService<ProblemData>(s => s.GetProblemData(problemId));
|
---|
295 | }
|
---|
296 | catch (Exception ex) {
|
---|
297 | ErrorHandling.ShowErrorDialog("Refresh problem data failed.", ex);
|
---|
298 | return null;
|
---|
299 | }
|
---|
300 | }
|
---|
301 | public bool UpdateProblemData(ProblemData problemData) {
|
---|
302 | try {
|
---|
303 | CallAdminService(s => s.UpdateProblemData(problemData));
|
---|
304 | return true;
|
---|
305 | }
|
---|
306 | catch (Exception ex) {
|
---|
307 | ErrorHandling.ShowErrorDialog("Update problem data failed.", ex);
|
---|
308 | return false;
|
---|
309 | }
|
---|
310 | }
|
---|
311 | public ItemCollection<ProblemParameter> GetProblemParameters(long problemId) {
|
---|
312 | try {
|
---|
313 | ItemCollection<ProblemParameter> parameters = new ItemCollection<ProblemParameter>();
|
---|
314 | parameters.AddRange(CallAdminService<ProblemParameter[]>(s => s.GetProblemParameters(problemId)).OrderBy(x => x.Name));
|
---|
315 | return parameters;
|
---|
316 | }
|
---|
317 | catch (Exception ex) {
|
---|
318 | ErrorHandling.ShowErrorDialog("Refresh problem parameters failed.", ex);
|
---|
319 | return null;
|
---|
320 | }
|
---|
321 | }
|
---|
322 | #endregion
|
---|
323 |
|
---|
324 | #region Run Methods
|
---|
325 | public bool AddRun(long algorithmId, long problemId, HeuristicLab.Optimization.Run run) {
|
---|
326 | try {
|
---|
327 | IAlgorithm algorithm = run.Algorithm;
|
---|
328 | IProblem problem = algorithm.Problem;
|
---|
329 |
|
---|
330 | ItemCollection<AlgorithmParameter> algorithmParameters = GetAlgorithmParameters(algorithmId);
|
---|
331 | List<AlgorithmParameterValue> algorithmParameterValues = CollectAlgorithmParameterValues(algorithmId, algorithmParameters, algorithm, "");
|
---|
332 | ItemCollection<ProblemParameter> problemParameters = GetProblemParameters(problemId);
|
---|
333 | List<ProblemParameterValue> problemParameterValues = CollectProblemParamterValues(problemId, problemParameters, problem, "");
|
---|
334 | ItemCollection<Result> results = GetResults(algorithmId);
|
---|
335 | List<ResultValue> resultValues = CollectResultValues(algorithmId, results, algorithm);
|
---|
336 |
|
---|
337 | Experiment exp = new Experiment();
|
---|
338 | exp.AlgorithmId = algorithmId;
|
---|
339 | exp.ProblemId = problemId;
|
---|
340 | exp.AlgorithmParameterValues = algorithmParameterValues.ToArray();
|
---|
341 | exp.ProblemParameterValues = problemParameterValues.ToArray();
|
---|
342 | exp.Store();
|
---|
343 |
|
---|
344 | Run r = new Run();
|
---|
345 | r.ExperimentId = exp.Id;
|
---|
346 | r.ClientId = Guid.NewGuid();
|
---|
347 | r.FinishedDate = DateTime.Now;
|
---|
348 | r.RandomSeed = ((IntValue)((IValueParameter)run.Parameters["Seed"]).Value).Value;
|
---|
349 | r.ResultValues = resultValues.ToArray();
|
---|
350 | r.Store();
|
---|
351 |
|
---|
352 | return true;
|
---|
353 | }
|
---|
354 | catch (Exception ex) {
|
---|
355 | ErrorHandling.ShowErrorDialog("Store run failed.", ex);
|
---|
356 | return false;
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | private List<AlgorithmParameterValue> CollectAlgorithmParameterValues(long algorithmId, ItemCollection<AlgorithmParameter> parameters, IParameterizedItem item, string prefix) {
|
---|
361 | List<AlgorithmParameterValue> values = new List<AlgorithmParameterValue>();
|
---|
362 | foreach (IValueParameter param in item.Parameters.OfType<IValueParameter>()) {
|
---|
363 | if (param.GetsCollected && (param.Value != null)) {
|
---|
364 | AlgorithmParameter p = parameters.FirstOrDefault(x => x.Name == prefix + param.Name);
|
---|
365 | if (p == null) {
|
---|
366 | p = new AlgorithmParameter();
|
---|
367 | p.Name = prefix + param.Name;
|
---|
368 | p.Alias = prefix + param.Name;
|
---|
369 | p.Description = param.Description;
|
---|
370 | p.AlgorithmId = algorithmId;
|
---|
371 | p.DataTypeId = ConvertToDataType(param.DataType).Id;
|
---|
372 | p.Store();
|
---|
373 | parameters.Add(p);
|
---|
374 | }
|
---|
375 | AlgorithmParameterValue value = CreateAlgorithmParameterValue(param.Value);
|
---|
376 | value.AlgorithmParameterId = p.Id;
|
---|
377 | value.DataTypeId = ConvertToDataType(param.Value.GetType()).Id;
|
---|
378 | }
|
---|
379 |
|
---|
380 | if (param.Value is IParameterizedItem)
|
---|
381 | values.AddRange(CollectAlgorithmParameterValues(algorithmId, parameters, (IParameterizedItem)param.Value, (string.IsNullOrEmpty(prefix) ? param.Name : prefix + param.Name) + "."));
|
---|
382 | }
|
---|
383 | return values;
|
---|
384 | }
|
---|
385 | private AlgorithmParameterValue CreateAlgorithmParameterValue(IItem item) {
|
---|
386 | if (item is BoolValue) {
|
---|
387 | AlgorithmParameterBoolValue value = new AlgorithmParameterBoolValue();
|
---|
388 | value.Value = ((BoolValue)item).Value;
|
---|
389 | return value;
|
---|
390 | } else if (item is DoubleValue) {
|
---|
391 | AlgorithmParameterFloatValue value = new AlgorithmParameterFloatValue();
|
---|
392 | value.Value = ((DoubleValue)item).Value;
|
---|
393 | return value;
|
---|
394 | } else if (item is IntValue) {
|
---|
395 | AlgorithmParameterIntValue value = new AlgorithmParameterIntValue();
|
---|
396 | value.Value = ((IntValue)item).Value;
|
---|
397 | return value;
|
---|
398 | } else if (item is StringValue) {
|
---|
399 | AlgorithmParameterStringValue value = new AlgorithmParameterStringValue();
|
---|
400 | value.Value = ((StringValue)item).Value;
|
---|
401 | return value;
|
---|
402 | } else {
|
---|
403 | AlgorithmParameterBlobValue value = new AlgorithmParameterBlobValue();
|
---|
404 | value.Value = null;
|
---|
405 | return value;
|
---|
406 | }
|
---|
407 | }
|
---|
408 | private List<ProblemParameterValue> CollectProblemParamterValues(long problemId, ItemCollection<ProblemParameter> parameters, IParameterizedItem item, string prefix) {
|
---|
409 | List<ProblemParameterValue> values = new List<ProblemParameterValue>();
|
---|
410 | foreach (IValueParameter param in item.Parameters.OfType<IValueParameter>()) {
|
---|
411 | if (param.GetsCollected && (param.Value != null)) {
|
---|
412 | ProblemParameter p = parameters.FirstOrDefault(x => x.Name == prefix + param.Name);
|
---|
413 | if (p == null) {
|
---|
414 | p = new ProblemParameter();
|
---|
415 | p.Name = prefix + param.Name;
|
---|
416 | p.Alias = prefix + param.Name;
|
---|
417 | p.Description = param.Description;
|
---|
418 | p.ProblemId = problemId;
|
---|
419 | p.DataTypeId = ConvertToDataType(param.DataType).Id;
|
---|
420 | p.Store();
|
---|
421 | parameters.Add(p);
|
---|
422 | }
|
---|
423 | ProblemParameterValue value = CreateProblemParameterValue(param.Value);
|
---|
424 | value.ProblemParameterId = p.Id;
|
---|
425 | value.DataTypeId = ConvertToDataType(param.Value.GetType()).Id;
|
---|
426 | }
|
---|
427 |
|
---|
428 | if (param.Value is IParameterizedItem)
|
---|
429 | values.AddRange(CollectProblemParamterValues(problemId, parameters, (IParameterizedItem)param.Value, (string.IsNullOrEmpty(prefix) ? param.Name : prefix + param.Name) + "."));
|
---|
430 | }
|
---|
431 | return values;
|
---|
432 | }
|
---|
433 | private ProblemParameterValue CreateProblemParameterValue(IItem item) {
|
---|
434 | if (item is BoolValue) {
|
---|
435 | ProblemParameterBoolValue value = new ProblemParameterBoolValue();
|
---|
436 | value.Value = ((BoolValue)item).Value;
|
---|
437 | return value;
|
---|
438 | } else if (item is DoubleValue) {
|
---|
439 | ProblemParameterFloatValue value = new ProblemParameterFloatValue();
|
---|
440 | value.Value = ((DoubleValue)item).Value;
|
---|
441 | return value;
|
---|
442 | } else if (item is IntValue) {
|
---|
443 | ProblemParameterIntValue value = new ProblemParameterIntValue();
|
---|
444 | value.Value = ((IntValue)item).Value;
|
---|
445 | return value;
|
---|
446 | } else if (item is StringValue) {
|
---|
447 | ProblemParameterStringValue value = new ProblemParameterStringValue();
|
---|
448 | value.Value = ((StringValue)item).Value;
|
---|
449 | return value;
|
---|
450 | } else {
|
---|
451 | ProblemParameterBlobValue value = new ProblemParameterBlobValue();
|
---|
452 | value.Value = null;
|
---|
453 | return value;
|
---|
454 | }
|
---|
455 | }
|
---|
456 | private List<ResultValue> CollectResultValues(long algorithmId, ItemCollection<Result> results, IAlgorithm algorithm) {
|
---|
457 | List<ResultValue> values = new List<ResultValue>();
|
---|
458 | foreach (IResult result in algorithm.Results) {
|
---|
459 | if ((result.Value != null) && (results.FirstOrDefault(x => x.Name == result.Name) == null)) {
|
---|
460 | Result r = new Result();
|
---|
461 | r.Name = result.Name;
|
---|
462 | r.Alias = result.Name;
|
---|
463 | r.Description = result.Description;
|
---|
464 | r.AlgorithmId = algorithmId;
|
---|
465 | r.DataTypeId = ConvertToDataType(result.DataType).Id;
|
---|
466 | r.Store();
|
---|
467 | results.Add(r);
|
---|
468 | }
|
---|
469 | }
|
---|
470 | return values;
|
---|
471 | }
|
---|
472 | private ResultValue CreateResiötValue(IItem item) {
|
---|
473 | if (item is BoolValue) {
|
---|
474 | ResultBoolValue value = new ResultBoolValue();
|
---|
475 | value.Value = ((BoolValue)item).Value;
|
---|
476 | return value;
|
---|
477 | } else if (item is DoubleValue) {
|
---|
478 | ResultFloatValue value = new ResultFloatValue();
|
---|
479 | value.Value = ((DoubleValue)item).Value;
|
---|
480 | return value;
|
---|
481 | } else if (item is IntValue) {
|
---|
482 | ResultIntValue value = new ResultIntValue();
|
---|
483 | value.Value = ((IntValue)item).Value;
|
---|
484 | return value;
|
---|
485 | } else if (item is StringValue) {
|
---|
486 | ResultStringValue value = new ResultStringValue();
|
---|
487 | value.Value = ((StringValue)item).Value;
|
---|
488 | return value;
|
---|
489 | } else {
|
---|
490 | ResultBlobValue value = new ResultBlobValue();
|
---|
491 | value.Value = null;
|
---|
492 | return value;
|
---|
493 | }
|
---|
494 | }
|
---|
495 | #endregion
|
---|
496 |
|
---|
497 | #region Events
|
---|
498 | public event EventHandler Refreshing;
|
---|
499 | private void OnRefreshing() {
|
---|
500 | EventHandler handler = Refreshing;
|
---|
501 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
502 | }
|
---|
503 | public event EventHandler Refreshed;
|
---|
504 | private void OnRefreshed() {
|
---|
505 | EventHandler handler = Refreshed;
|
---|
506 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
507 | }
|
---|
508 |
|
---|
509 | private void platforms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Platform> e) {
|
---|
510 | try {
|
---|
511 | foreach (Platform p in e.Items)
|
---|
512 | CallAdminService(s => s.DeletePlatform(p.Id));
|
---|
513 | }
|
---|
514 | catch (Exception ex) {
|
---|
515 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
516 | }
|
---|
517 | }
|
---|
518 | private void dataTypes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataType> e) {
|
---|
519 | try {
|
---|
520 | foreach (DataType d in e.Items)
|
---|
521 | CallAdminService(s => s.DeleteDataType(d.Id));
|
---|
522 | }
|
---|
523 | catch (Exception ex) {
|
---|
524 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
525 | }
|
---|
526 | }
|
---|
527 | private void algorithmClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<AlgorithmClass> e) {
|
---|
528 | try {
|
---|
529 | foreach (AlgorithmClass a in e.Items)
|
---|
530 | CallAdminService(s => s.DeleteAlgorithmClass(a.Id));
|
---|
531 | }
|
---|
532 | catch (Exception ex) {
|
---|
533 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
534 | }
|
---|
535 | }
|
---|
536 | private void algorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Algorithm> e) {
|
---|
537 | try {
|
---|
538 | foreach (Algorithm a in e.Items)
|
---|
539 | CallAdminService(s => s.DeleteAlgorithm(a.Id));
|
---|
540 | }
|
---|
541 | catch (Exception ex) {
|
---|
542 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
543 | }
|
---|
544 | }
|
---|
545 | private void problemClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ProblemClass> e) {
|
---|
546 | try {
|
---|
547 | foreach (ProblemClass p in e.Items)
|
---|
548 | CallAdminService(s => s.DeleteProblemClass(p.Id));
|
---|
549 | }
|
---|
550 | catch (Exception ex) {
|
---|
551 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
552 | }
|
---|
553 | }
|
---|
554 | private void problems_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Problem> e) {
|
---|
555 | try {
|
---|
556 | foreach (Problem p in e.Items)
|
---|
557 | CallAdminService(s => s.DeleteProblem(p.Id));
|
---|
558 | }
|
---|
559 | catch (Exception ex) {
|
---|
560 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
561 | }
|
---|
562 | }
|
---|
563 | #endregion
|
---|
564 |
|
---|
565 | #region Helpers
|
---|
566 | private void CallAdminService(Action<IOKBService> call) {
|
---|
567 | OKBServiceClient client = ClientFactory.CreateClient<OKBServiceClient, IOKBService>();
|
---|
568 | try {
|
---|
569 | call(client);
|
---|
570 | }
|
---|
571 | finally {
|
---|
572 | try {
|
---|
573 | client.Close();
|
---|
574 | }
|
---|
575 | catch (Exception) {
|
---|
576 | client.Abort();
|
---|
577 | }
|
---|
578 | }
|
---|
579 | }
|
---|
580 | private T CallAdminService<T>(Func<IOKBService, T> call) {
|
---|
581 | OKBServiceClient client = ClientFactory.CreateClient<OKBServiceClient, IOKBService>();
|
---|
582 | try {
|
---|
583 | return call(client);
|
---|
584 | }
|
---|
585 | finally {
|
---|
586 | try {
|
---|
587 | client.Close();
|
---|
588 | }
|
---|
589 | catch (Exception) {
|
---|
590 | client.Abort();
|
---|
591 | }
|
---|
592 | }
|
---|
593 | }
|
---|
594 | private T CallAuthenticationService<T>(Func<IAuthenticationService, T> call) {
|
---|
595 | AuthenticationServiceClient client = ClientFactory.CreateClient<AuthenticationServiceClient, IAuthenticationService>();
|
---|
596 | try {
|
---|
597 | return call(client);
|
---|
598 | }
|
---|
599 | finally {
|
---|
600 | try {
|
---|
601 | client.Close();
|
---|
602 | }
|
---|
603 | catch (Exception) {
|
---|
604 | client.Abort();
|
---|
605 | }
|
---|
606 | }
|
---|
607 | }
|
---|
608 | #endregion
|
---|
609 | }
|
---|
610 | }
|
---|