Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBClient.cs @ 5533

Last change on this file since 5533 was 5533, checked in by swagner, 14 years ago

Worked on OKB (#1174)

File size: 31.3 KB
RevLine 
[4426]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
22using System;
[4466]23using System.Collections.Generic;
[4943]24using System.IO;
[4433]25using System.Linq;
[4426]26using HeuristicLab.Clients.Common;
27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
[4566]30using HeuristicLab.Data;
[4559]31using HeuristicLab.Optimization;
[4943]32using HeuristicLab.Persistence.Default.Xml;
[4426]33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Clients.OKB {
[4549]36  [Item("OKBClient", "Client for accessing the OKB.")]
37  public sealed class OKBClient : IContent {
38    private static OKBClient instance;
39    public static OKBClient Instance {
[4433]40      get {
[4549]41        if (instance == null) instance = new OKBClient();
[4433]42        return instance;
43      }
44    }
45
[4481]46    #region Properties
[4441]47    private ItemCollection<Platform> platforms;
48    public ItemCollection<Platform> Platforms {
49      get { return platforms; }
50    }
[4481]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    }
[4426]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    }
[4481]67    private ItemCollection<ProblemClass> problemClasses;
68    public ItemCollection<ProblemClass> ProblemClasses {
69      get { return problemClasses; }
[4466]70    }
[4481]71    private ItemCollection<Problem> problems;
72    public ItemCollection<Problem> Problems {
73      get { return problems; }
[4466]74    }
[4481]75    #endregion
[4426]76
[4549]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    }
[4426]91
[4481]92    #region Refresh
[4433]93    public void Refresh() {
94      OnRefreshing();
[4549]95
[4441]96      platforms.Clear();
[4481]97      dataTypes.Clear();
[4441]98      algorithmClasses.Clear();
[4433]99      algorithms.Clear();
[4481]100      problemClasses.Clear();
101      problems.Clear();
[4433]102
[4441]103      var call = new Func<Exception>(delegate() {
104        try {
[5299]105          platforms.AddRange(CallAdministrationService<List<Platform>>(s => s.GetPlatforms()).OrderBy(x => x.Name));
106          dataTypes.AddRange(CallAdministrationService<List<DataType>>(s => s.GetDataTypes()).OrderBy(x => x.Name));
[5286]107          users = CallAuthenticationService<List<User>>(s => s.GetUsers()).OrderBy(x => x.Name);
[5299]108          algorithmClasses.AddRange(CallAdministrationService<List<AlgorithmClass>>(s => s.GetAlgorithmClasses()).OrderBy(x => x.Name));
109          algorithms.AddRange(CallAdministrationService<List<Algorithm>>(s => s.GetAlgorithms()).OrderBy(x => x.Name));
110          problemClasses.AddRange(CallAdministrationService<List<ProblemClass>>(s => s.GetProblemClasses()).OrderBy(x => x.Name));
111          problems.AddRange(CallAdministrationService<List<Problem>>(s => s.GetProblems()).OrderBy(x => x.Name));
[4441]112          return null;
113        }
114        catch (Exception ex) {
115          return ex;
116        }
[4433]117      });
[4441]118      call.BeginInvoke(delegate(IAsyncResult result) {
119        Exception ex = call.EndInvoke(result);
120        if (ex != null) ErrorHandling.ShowErrorDialog("Refresh failed.", ex);
[4433]121        OnRefreshed();
122      }, null);
[4426]123    }
[4481]124    #endregion
[4426]125
[4481]126    #region Store
[4441]127    public bool Store(IOKBItem item) {
128      try {
[4481]129        if (item.Id == 0) {
130          if (item is Platform)
[5299]131            item.Id = CallAdministrationService<long>(s => s.AddPlatform((Platform)item));
[4481]132          else if (item is DataType)
[5299]133            item.Id = CallAdministrationService<long>(s => s.AddDataType((DataType)item));
[4481]134          else if (item is AlgorithmClass)
[5299]135            item.Id = CallAdministrationService<long>(s => s.AddAlgorithmClass((AlgorithmClass)item));
[4481]136          else if (item is Algorithm)
[5299]137            item.Id = CallAdministrationService<long>(s => s.AddAlgorithm((Algorithm)item));
[4566]138          else if (item is AlgorithmParameter)
[5299]139            item.Id = CallAdministrationService<long>(s => s.AddAlgorithmParameter((AlgorithmParameter)item));
[4481]140          else if (item is ProblemClass)
[5299]141            item.Id = CallAdministrationService<long>(s => s.AddProblemClass((ProblemClass)item));
[4481]142          else if (item is Problem)
[5299]143            item.Id = CallAdministrationService<long>(s => s.AddProblem((Problem)item));
[4566]144          else if (item is ProblemParameter)
[5299]145            item.Id = CallAdministrationService<long>(s => s.AddProblemParameter((ProblemParameter)item));
[4566]146          else if (item is Result)
[5299]147            item.Id = CallAdministrationService<long>(s => s.AddResult((Result)item));
[4591]148          else if (item is Experiment)
[5299]149            item.Id = CallAdministrationService<long>(s => s.AddExperiment((Experiment)item));
[4591]150          else if (item is Run)
[5299]151            item.Id = CallAdministrationService<long>(s => s.AddRun((Run)item));
[4481]152        } else {
153          if (item is Platform)
[5299]154            CallAdministrationService(s => s.UpdatePlatform((Platform)item));
[4481]155          else if (item is DataType)
[5299]156            CallAdministrationService(s => s.UpdateDataType((DataType)item));
[4481]157          else if (item is AlgorithmClass)
[5299]158            CallAdministrationService(s => s.UpdateAlgorithmClass((AlgorithmClass)item));
[4481]159          else if (item is Algorithm)
[5299]160            CallAdministrationService(s => s.UpdateAlgorithm((Algorithm)item));
[4566]161          else if (item is AlgorithmParameter)
[5299]162            CallAdministrationService(s => s.UpdateAlgorithmParameter((AlgorithmParameter)item));
[4481]163          else if (item is ProblemClass)
[5299]164            CallAdministrationService(s => s.UpdateProblemClass((ProblemClass)item));
[4481]165          else if (item is Problem)
[5299]166            CallAdministrationService(s => s.UpdateProblem((Problem)item));
[4566]167          else if (item is ProblemParameter)
[5299]168            CallAdministrationService(s => s.UpdateProblemParameter((ProblemParameter)item));
[4566]169          else if (item is Result)
[5299]170            CallAdministrationService(s => s.UpdateResult((Result)item));
[4591]171          else if (item is Experiment)
[5299]172            item.Id = CallAdministrationService<long>(s => s.AddExperiment((Experiment)item));
[4591]173          else if (item is Run)
[5299]174            item.Id = CallAdministrationService<long>(s => s.AddRun((Run)item));
[4481]175        }
[4441]176        return true;
177      }
178      catch (Exception ex) {
179        ErrorHandling.ShowErrorDialog("Store failed.", ex);
180        return false;
181      }
[4433]182    }
[4587]183    #endregion
[4566]184
[4587]185    #region DataType Methods
186    public DataType ConvertToDataType(Type type) {
[5295]187      DataType dataType = DataTypes.FirstOrDefault(x => x.TypeName == type.AssemblyQualifiedName);
[4566]188      if (dataType == null) {
189        dataType = new DataType();
[5295]190        dataType.Name = type.Name;
191        dataType.TypeName = type.AssemblyQualifiedName;
[4566]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    }
[4481]210    #endregion
[4433]211
[4481]212    #region Algorithm Methods
[5286]213    public List<Guid> GetAlgorithmUsers(long algorithmId) {
[4466]214      try {
[5299]215        return CallAdministrationService<List<Guid>>(s => s.GetAlgorithmUsers(algorithmId));
[4466]216      }
217      catch (Exception ex) {
218        ErrorHandling.ShowErrorDialog("Refresh authorized algorithm users failed.", ex);
219        return null;
220      }
221    }
[5286]222    public bool UpdateAlgorithmUsers(long algorithmId, List<Guid> users) {
[4466]223      try {
[5299]224        CallAdministrationService(s => s.UpdateAlgorithmUsers(algorithmId, users));
[4466]225        return true;
226      }
227      catch (Exception ex) {
[4481]228        ErrorHandling.ShowErrorDialog("Update authorized algorithm users failed.", ex);
[4466]229        return false;
230      }
231    }
[4481]232    public AlgorithmData GetAlgorithmData(long algorithmId) {
233      try {
[5299]234        return CallAdministrationService<AlgorithmData>(s => s.GetAlgorithmData(algorithmId));
[4481]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 {
[5299]243        CallAdministrationService(s => s.UpdateAlgorithmData(algorithmData));
[4481]244        return true;
245      }
246      catch (Exception ex) {
247        ErrorHandling.ShowErrorDialog("Update algorithm data failed.", ex);
248        return false;
249      }
250    }
251    #endregion
[4466]252
[4481]253    #region Problem Methods
[5286]254    public List<Guid> GetProblemUsers(long problemId) {
[4466]255      try {
[5299]256        return CallAdministrationService<List<Guid>>(s => s.GetProblemUsers(problemId));
[4481]257      }
258      catch (Exception ex) {
259        ErrorHandling.ShowErrorDialog("Refresh authorized problem users failed.", ex);
260        return null;
261      }
262    }
[5286]263    public bool UpdateProblemUsers(long problemId, List<Guid> users) {
[4481]264      try {
[5299]265        CallAdministrationService(s => s.UpdateProblemUsers(problemId, users));
[4466]266        return true;
267      }
268      catch (Exception ex) {
[4481]269        ErrorHandling.ShowErrorDialog("Update authorized problem users failed.", ex);
[4466]270        return false;
271      }
272    }
[4481]273    public ProblemData GetProblemData(long problemId) {
274      try {
[5299]275        return CallAdministrationService<ProblemData>(s => s.GetProblemData(problemId));
[4481]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 {
[5299]284        CallAdministrationService(s => s.UpdateProblemData(problemData));
[4481]285        return true;
286      }
287      catch (Exception ex) {
288        ErrorHandling.ShowErrorDialog("Update problem data failed.", ex);
289        return false;
290      }
291    }
[4943]292    #endregion
293
294    #region AlgorithmParameter Methods
295    public AlgorithmParameter GetAlgorithmParameter(long id) {
296      try {
[5299]297        return CallAdministrationService<AlgorithmParameter>(s => s.GetAlgorithmParameter(id));
[4943]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>();
[5299]307        parameters.AddRange(CallAdministrationService<List<AlgorithmParameter>>(s => s.GetAlgorithmParameters(algorithmId)).OrderBy(x => x.Name));
[4943]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 {
[5299]320        return CallAdministrationService<ProblemParameter>(s => s.GetProblemParameter(id));
[4943]321      }
322      catch (Exception ex) {
323        ErrorHandling.ShowErrorDialog("Refresh problem parameter failed.", ex);
324        return null;
325      }
326    }
[4566]327    public ItemCollection<ProblemParameter> GetProblemParameters(long problemId) {
328      try {
329        ItemCollection<ProblemParameter> parameters = new ItemCollection<ProblemParameter>();
[5299]330        parameters.AddRange(CallAdministrationService<List<ProblemParameter>>(s => s.GetProblemParameters(problemId)).OrderBy(x => x.Name));
[4566]331        return parameters;
332      }
333      catch (Exception ex) {
334        ErrorHandling.ShowErrorDialog("Refresh problem parameters failed.", ex);
335        return null;
336      }
337    }
[4481]338    #endregion
[4466]339
[4943]340    #region Result Methods
341    public Result GetResult(long id) {
342      try {
[5299]343        return CallAdministrationService<Result>(s => s.GetResult(id));
[4943]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>();
[5299]353        results.AddRange(CallAdministrationService<List<Result>>(s => s.GetResults(algorithmId)).OrderBy(x => x.Name));
[4943]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
[5073]364    public Experiment GetExperiment(long id) {
365      try {
[5299]366        return CallAdministrationService<Experiment>(s => s.GetExperiment(id));
[5073]367      }
368      catch (Exception ex) {
369        ErrorHandling.ShowErrorDialog("Refresh experiment failed.", ex);
370        return null;
371      }
372    }
[4943]373    public ItemCollection<Experiment> GetExperiments(long algorithmId, long problemId) {
374      try {
375        ItemCollection<Experiment> experiments = new ItemCollection<Experiment>();
[5299]376        experiments.AddRange(CallAdministrationService<List<Experiment>>(s => s.GetExperiments(algorithmId, problemId)));
[5071]377        experiments.ItemsRemoved += new CollectionItemsChangedEventHandler<Experiment>(experiments_ItemsRemoved);
[4943]378        return experiments;
379      }
380      catch (Exception ex) {
381        ErrorHandling.ShowErrorDialog("Refresh experiments failed.", ex);
382        return null;
383      }
384    }
385    #endregion
386
[4587]387    #region Run Methods
[4943]388    public ItemCollection<Run> GetRuns(long experimentId) {
389      try {
390        ItemCollection<Run> runs = new ItemCollection<Run>();
[5299]391        runs.AddRange(CallAdministrationService<List<Run>>(s => s.GetRuns(experimentId)).OrderByDescending(x => x.CreatedDate));
[5071]392        runs.ItemsRemoved += new CollectionItemsChangedEventHandler<Run>(runs_ItemsRemoved);
[4943]393        return runs;
394      }
395      catch (Exception ex) {
396        ErrorHandling.ShowErrorDialog("Refresh runs failed.", ex);
397        return null;
398      }
399    }
[4929]400    public bool AddRun(long algorithmId, long problemId, IAlgorithm algorithm) {
[4587]401      try {
402        IProblem problem = algorithm.Problem;
403
404        ItemCollection<AlgorithmParameter> algorithmParameters = GetAlgorithmParameters(algorithmId);
[4591]405        List<AlgorithmParameterValue> algorithmParameterValues = CollectAlgorithmParameterValues(algorithmId, algorithmParameters, algorithm, "");
[4587]406        ItemCollection<ProblemParameter> problemParameters = GetProblemParameters(problemId);
[4591]407        List<ProblemParameterValue> problemParameterValues = CollectProblemParamterValues(problemId, problemParameters, problem, "");
408        ItemCollection<Result> results = GetResults(algorithmId);
409        List<ResultValue> resultValues = CollectResultValues(algorithmId, results, algorithm);
[4587]410
[4591]411        Experiment exp = new Experiment();
412        exp.AlgorithmId = algorithmId;
413        exp.ProblemId = problemId;
[5286]414        exp.AlgorithmParameterValues = algorithmParameterValues;
415        exp.ProblemParameterValues = problemParameterValues;
[4591]416        exp.Store();
417
418        Run r = new Run();
419        r.ExperimentId = exp.Id;
420        r.ClientId = Guid.NewGuid();
[5295]421        r.CreatedDate = DateTime.Now;
[4929]422        r.RandomSeed = ((IntValue)((IValueParameter)algorithm.Parameters["Seed"]).Value).Value;
[5286]423        r.ResultValues = resultValues;
[4591]424        r.Store();
425
[4587]426        return true;
427      }
428      catch (Exception ex) {
429        ErrorHandling.ShowErrorDialog("Store run failed.", ex);
430        return false;
431      }
432    }
433
[4591]434    private List<AlgorithmParameterValue> CollectAlgorithmParameterValues(long algorithmId, ItemCollection<AlgorithmParameter> parameters, IParameterizedItem item, string prefix) {
435      List<AlgorithmParameterValue> values = new List<AlgorithmParameterValue>();
[4587]436      foreach (IValueParameter param in item.Parameters.OfType<IValueParameter>()) {
[4929]437        if (param.GetsCollected && (param.Value != null) && (param.Name != "Seed")) {
[4591]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          }
[4918]449          AlgorithmParameterValue value = CreateAlgorithmParameterValue(param.Value);
[4591]450          value.AlgorithmParameterId = p.Id;
451          value.DataTypeId = ConvertToDataType(param.Value.GetType()).Id;
[4929]452          values.Add(value);
[4587]453        }
[4591]454
[4587]455        if (param.Value is IParameterizedItem)
[4591]456          values.AddRange(CollectAlgorithmParameterValues(algorithmId, parameters, (IParameterizedItem)param.Value, (string.IsNullOrEmpty(prefix) ? param.Name : prefix + param.Name) + "."));
[4587]457      }
[4591]458      return values;
[4587]459    }
[4918]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();
[4943]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        }
[4918]489        return value;
490      }
491    }
[4591]492    private List<ProblemParameterValue> CollectProblemParamterValues(long problemId, ItemCollection<ProblemParameter> parameters, IParameterizedItem item, string prefix) {
493      List<ProblemParameterValue> values = new List<ProblemParameterValue>();
[4587]494      foreach (IValueParameter param in item.Parameters.OfType<IValueParameter>()) {
[4918]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;
[4929]510          values.Add(value);
[4587]511        }
[4918]512
[4587]513        if (param.Value is IParameterizedItem)
[4591]514          values.AddRange(CollectProblemParamterValues(problemId, parameters, (IParameterizedItem)param.Value, (string.IsNullOrEmpty(prefix) ? param.Name : prefix + param.Name) + "."));
[4587]515      }
[4591]516      return values;
[4587]517    }
[4918]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();
[4943]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        }
[4918]547        return value;
548      }
549    }
[4591]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) {
[4929]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);
[4591]569        }
570      }
571      return values;
572    }
[4929]573    private ResultValue CreateResultValue(IItem item) {
[4918]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();
[4943]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        }
[4918]602        return value;
603      }
604    }
[4587]605    #endregion
606
[5269]607    #region Query Methods
[5295]608    private IEnumerable<Filter> filters;
609    public IEnumerable<Filter> GetFilters(bool refresh) {
610      if (refresh || (filters == null)) {
611        try {
[5299]612          filters = CallQueryService<List<Filter>>(s => s.GetFilters());
[5295]613        }
614        catch (Exception ex) {
[5304]615          ErrorHandling.ShowErrorDialog("Get filters failed.", ex);
616          return Enumerable.Empty<Filter>();
[5295]617        }
618      }
619      return filters;
620    }
[5269]621    public IEnumerable<Filter> GetFilters() {
[5295]622      return GetFilters(false);
623    }
[5304]624    public long GetNumberOfQueryResults(Filter filter) {
[5269]625      try {
[5304]626        return CallQueryService<long>(x => x.GetNumberOfQueryResults(filter));
[5269]627      }
628      catch (Exception ex) {
[5304]629        ErrorHandling.ShowErrorDialog("Get number of query results failed.", ex);
[5295]630        return -1;
[5269]631      }
632    }
[5304]633    public IEnumerable<long> GetQueryResultIds(Filter filter) {
[5269]634      try {
[5304]635        return CallQueryService<IEnumerable<long>>(x => x.GetQueryResultIds(filter));
[5269]636      }
637      catch (Exception ex) {
[5304]638        ErrorHandling.ShowErrorDialog("Get query result ids failed.", ex);
639        return Enumerable.Empty<long>();
640      }
641    }
642    public IEnumerable<QueryResult> GetQueryResults(IEnumerable<long> ids) {
643      try {
644        return CallQueryService<IEnumerable<QueryResult>>(s => s.GetQueryResults(ids.ToList()));
645      }
646      catch (Exception ex) {
647        ErrorHandling.ShowErrorDialog("Get query results failed.", ex);
[5269]648        return null;
649      }
650    }
651    #endregion
652
[4481]653    #region Events
[4433]654    public event EventHandler Refreshing;
655    private void OnRefreshing() {
656      EventHandler handler = Refreshing;
657      if (handler != null) handler(this, EventArgs.Empty);
658    }
659    public event EventHandler Refreshed;
660    private void OnRefreshed() {
661      EventHandler handler = Refreshed;
662      if (handler != null) handler(this, EventArgs.Empty);
663    }
664
[4441]665    private void platforms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Platform> e) {
666      try {
667        foreach (Platform p in e.Items)
[5299]668          CallAdministrationService(s => s.DeletePlatform(p.Id));
[4441]669      }
670      catch (Exception ex) {
671        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
672      }
673    }
[4481]674    private void dataTypes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataType> e) {
675      try {
676        foreach (DataType d in e.Items)
[5299]677          CallAdministrationService(s => s.DeleteDataType(d.Id));
[4481]678      }
679      catch (Exception ex) {
680        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
681      }
682    }
[4433]683    private void algorithmClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<AlgorithmClass> e) {
[4441]684      try {
685        foreach (AlgorithmClass a in e.Items)
[5299]686          CallAdministrationService(s => s.DeleteAlgorithmClass(a.Id));
[4441]687      }
688      catch (Exception ex) {
689        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
690      }
[4433]691    }
692    private void algorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Algorithm> e) {
[4441]693      try {
694        foreach (Algorithm a in e.Items)
[5299]695          CallAdministrationService(s => s.DeleteAlgorithm(a.Id));
[4441]696      }
697      catch (Exception ex) {
698        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
699      }
[4433]700    }
[4481]701    private void problemClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ProblemClass> e) {
[4466]702      try {
[4481]703        foreach (ProblemClass p in e.Items)
[5299]704          CallAdministrationService(s => s.DeleteProblemClass(p.Id));
[4466]705      }
706      catch (Exception ex) {
707        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
708      }
709    }
[4481]710    private void problems_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Problem> e) {
711      try {
712        foreach (Problem p in e.Items)
[5299]713          CallAdministrationService(s => s.DeleteProblem(p.Id));
[4481]714      }
715      catch (Exception ex) {
716        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
717      }
718    }
[5071]719    private void experiments_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Experiment> e) {
720      try {
721        foreach (Experiment exp in e.Items)
[5299]722          CallAdministrationService(s => s.DeleteExperiment(exp.Id));
[5071]723      }
724      catch (Exception ex) {
725        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
726      }
727    }
728    private void runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Run> e) {
729      try {
730        foreach (Run r in e.Items)
[5299]731          CallAdministrationService(s => s.DeleteRun(r.Id));
[5071]732      }
733      catch (Exception ex) {
734        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
735      }
736    }
[4481]737    #endregion
[4433]738
739    #region Helpers
[5299]740    private void CallAdministrationService(Action<IAdministrationService> call) {
741      AdministrationServiceClient client = ClientFactory.CreateClient<AdministrationServiceClient, IAdministrationService>();
[4433]742      try {
743        call(client);
744      }
745      finally {
[4426]746        try {
[4433]747          client.Close();
[4426]748        }
[4433]749        catch (Exception) {
750          client.Abort();
[4426]751        }
752      }
753    }
[5299]754    private T CallAdministrationService<T>(Func<IAdministrationService, T> call) {
755      AdministrationServiceClient client = ClientFactory.CreateClient<AdministrationServiceClient, IAdministrationService>();
[4433]756      try {
757        return call(client);
[4426]758      }
[4433]759      finally {
760        try {
761          client.Close();
762        }
763        catch (Exception) {
764          client.Abort();
765        }
766      }
[4426]767    }
[5299]768    private void CallQueryService(Action<IQueryService> call) {
769      QueryServiceClient client = ClientFactory.CreateClient<QueryServiceClient, IQueryService>();
770      try {
771        call(client);
772      }
773      finally {
774        try {
775          client.Close();
776        }
777        catch (Exception) {
778          client.Abort();
779        }
780      }
781    }
782    private T CallQueryService<T>(Func<IQueryService, T> call) {
783      QueryServiceClient client = ClientFactory.CreateClient<QueryServiceClient, IQueryService>();
784      try {
785        return call(client);
786      }
787      finally {
788        try {
789          client.Close();
790        }
791        catch (Exception) {
792          client.Abort();
793        }
794      }
795    }
[4481]796    private T CallAuthenticationService<T>(Func<IAuthenticationService, T> call) {
[4466]797      AuthenticationServiceClient client = ClientFactory.CreateClient<AuthenticationServiceClient, IAuthenticationService>();
798      try {
799        return call(client);
800      }
801      finally {
802        try {
803          client.Close();
804        }
805        catch (Exception) {
806          client.Abort();
807        }
808      }
809    }
[4433]810    #endregion
[4426]811  }
812}
Note: See TracBrowser for help on using the repository browser.