Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/Query/QueryClient.cs @ 5611

Last change on this file since 5611 was 5611, checked in by swagner, 13 years ago

Worked on OKB (#1174)

File size: 3.6 KB
Line 
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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Clients.Common;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28
29namespace HeuristicLab.Clients.OKB.Query {
30  [Item("QueryClient", "Client for accessing the OKB.")]
31  public sealed class QueryClient : IContent {
32    private static QueryClient instance;
33    public static QueryClient Instance {
34      get {
35        if (instance == null) instance = new QueryClient();
36        return instance;
37      }
38    }
39
40    #region Properties
41    private List<Filter> filters;
42    public IEnumerable<Filter> Filters {
43      get { return filters; }
44    }
45    #endregion
46
47    private QueryClient() {
48      filters = new List<Filter>();
49    }
50
51    #region Refresh
52    public void Refresh() {
53      OnRefreshing();
54      filters = new List<Filter>();
55      try {
56        filters.AddRange(CallQueryService<List<Filter>>(s => s.GetFilters()));
57      }
58      finally {
59        OnRefreshed();
60      }
61    }
62    public void RefreshAsync(Action<Exception> exceptionCallback) {
63      var call = new Func<Exception>(delegate() {
64        try {
65          Refresh();
66        }
67        catch (Exception ex) {
68          return ex;
69        }
70        return null;
71      });
72      call.BeginInvoke(delegate(IAsyncResult result) {
73        Exception ex = call.EndInvoke(result);
74        if (ex != null) exceptionCallback(ex);
75      }, null);
76    }
77    #endregion
78
79    #region Query Methods
80    public long GetNumberOfRuns(Filter filter) {
81      return CallQueryService<long>(x => x.GetNumberOfRuns(filter));
82    }
83    public IEnumerable<long> GetRunIds(Filter filter) {
84      return CallQueryService<IEnumerable<long>>(x => x.GetRunIds(filter));
85    }
86    public IEnumerable<Run> GetRuns(IEnumerable<long> ids, bool includeBinaryValues) {
87      return CallQueryService<IEnumerable<Run>>(s => s.GetRuns(ids.ToList(), includeBinaryValues));
88    }
89    #endregion
90
91    #region Events
92    public event EventHandler Refreshing;
93    private void OnRefreshing() {
94      EventHandler handler = Refreshing;
95      if (handler != null) handler(this, EventArgs.Empty);
96    }
97    public event EventHandler Refreshed;
98    private void OnRefreshed() {
99      EventHandler handler = Refreshed;
100      if (handler != null) handler(this, EventArgs.Empty);
101    }
102    #endregion
103
104    #region Helpers
105    private T CallQueryService<T>(Func<IQueryService, T> call) {
106      QueryServiceClient client = ClientFactory.CreateClient<QueryServiceClient, IQueryService>();
107      try {
108        return call(client);
109      }
110      finally {
111        try {
112          client.Close();
113        }
114        catch (Exception) {
115          client.Abort();
116        }
117      }
118    }
119    #endregion
120  }
121}
Note: See TracBrowser for help on using the repository browser.