source: trunk/sources/HeuristicLab.Clients.OKB/3.3/RunCreation/RunCreationClient.cs @ 13503

Last change on this file since 13503 was 13503, checked in by abeham, 5 years ago

#2560:

  • Added clients methods for the new service methods
  • Added partial class for the dto value classes of the run creation client so that one doesn't have to specify the DataType field
  • Added missing license header to UnknownCharacteristicType
File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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;
23using System.Collections.Generic;
24using HeuristicLab.Clients.Common;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27
28namespace HeuristicLab.Clients.OKB.RunCreation {
29  [Item("RunCreationClient", "OKB run creation client.")]
30  public sealed class RunCreationClient : IContent {
31    private static RunCreationClient instance;
32    public static RunCreationClient Instance {
33      get {
34        if (instance == null) instance = new RunCreationClient();
35        return instance;
36      }
37    }
38
39    #region Properties
40    private List<Algorithm> algorithms;
41    public IEnumerable<Algorithm> Algorithms {
42      get { return algorithms; }
43    }
44    private List<Problem> problems;
45    public IEnumerable<Problem> Problems {
46      get { return problems; }
47    }
48    #endregion
49
50    private RunCreationClient() {
51      algorithms = new List<Algorithm>();
52      problems = new List<Problem>();
53    }
54
55    #region Refresh
56    public void Refresh() {
57      OnRefreshing();
58      algorithms = new List<Algorithm>();
59      problems = new List<Problem>();
60      try {
61        algorithms.AddRange(CallRunCreationService<List<Algorithm>>(s => s.GetAlgorithms("HeuristicLab 3.3")));
62        problems.AddRange(CallRunCreationService<List<Problem>>(s => s.GetProblems("HeuristicLab 3.3")));
63      } finally {
64        OnRefreshed();
65      }
66    }
67    public void RefreshAsync(Action<Exception> exceptionCallback) {
68      var call = new Func<Exception>(delegate() {
69        try {
70          Refresh();
71        } catch (Exception ex) {
72          return ex;
73        }
74        return null;
75      });
76      call.BeginInvoke(delegate(IAsyncResult result) {
77        Exception ex = call.EndInvoke(result);
78        if (ex != null) exceptionCallback(ex);
79      }, null);
80    }
81    #endregion
82
83    #region Algorithm Methods
84    public static byte[] GetAlgorithmData(long algorithmId) {
85      return CallRunCreationService<byte[]>(s => s.GetAlgorithmData(algorithmId));
86    }
87    #endregion
88
89    #region Problem Methods
90    public static byte[] GetProblemData(long problemId) {
91      return CallRunCreationService<byte[]>(s => s.GetProblemData(problemId));
92    }
93    #endregion
94
95    #region Run Methods
96    public void AddRun(Run run) {
97      CallRunCreationService(s => s.AddRun(run));
98    }
99    #endregion
100
101    #region Characteristic Methods
102    public static void SetCharacteristicValue(long problemId, string characteristicName, Value v) {
103      CallRunCreationService(s => s.SetCharacteristicValue(problemId, characteristicName, v));
104    }
105    #endregion
106
107    #region Events
108    public event EventHandler Refreshing;
109    private void OnRefreshing() {
110      EventHandler handler = Refreshing;
111      if (handler != null) handler(this, EventArgs.Empty);
112    }
113    public event EventHandler Refreshed;
114    private void OnRefreshed() {
115      EventHandler handler = Refreshed;
116      if (handler != null) handler(this, EventArgs.Empty);
117    }
118    #endregion
119
120    #region Helpers
121    private static void CallRunCreationService(Action<IRunCreationService> call) {
122      RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>();
123      try {
124        call(client);
125      } finally {
126        try {
127          client.Close();
128        } catch (Exception) {
129          client.Abort();
130        }
131      }
132    }
133    private static T CallRunCreationService<T>(Func<IRunCreationService, T> call) {
134      RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>();
135      try {
136        return call(client);
137      } finally {
138        try {
139          client.Close();
140        } catch (Exception) {
141          client.Abort();
142        }
143      }
144    }
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.