Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13534 was 13534, checked in by abeham, 8 years ago

#2560:

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