Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

File size: 3.6 KB
RevLine 
[4426]1#region License Information
2/* HeuristicLab
[5902]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4426]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;
[4433]24using System.Linq;
[4426]25using HeuristicLab.Clients.Common;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28
[5606]29namespace HeuristicLab.Clients.OKB.Query {
[5640]30  [Item("QueryClient", "OKB query client.")]
[5606]31  public sealed class QueryClient : IContent {
32    private static QueryClient instance;
33    public static QueryClient Instance {
[4433]34      get {
[5606]35        if (instance == null) instance = new QueryClient();
[4433]36        return instance;
37      }
38    }
39
[4481]40    #region Properties
[5611]41    private List<Filter> filters;
42    public IEnumerable<Filter> Filters {
43      get { return filters; }
[4441]44    }
[4481]45    #endregion
[4426]46
[5606]47    private QueryClient() {
[5611]48      filters = new List<Filter>();
[4549]49    }
[4426]50
[4481]51    #region Refresh
[4433]52    public void Refresh() {
53      OnRefreshing();
[5611]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) {
[4441]63      var call = new Func<Exception>(delegate() {
64        try {
[5611]65          Refresh();
[4441]66        }
67        catch (Exception ex) {
68          return ex;
69        }
[5611]70        return null;
[4433]71      });
[4441]72      call.BeginInvoke(delegate(IAsyncResult result) {
73        Exception ex = call.EndInvoke(result);
[5611]74        if (ex != null) exceptionCallback(ex);
[4433]75      }, null);
[4426]76    }
[4481]77    #endregion
[4426]78
[5269]79    #region Query Methods
[5611]80    public long GetNumberOfRuns(Filter filter) {
81      return CallQueryService<long>(x => x.GetNumberOfRuns(filter));
[5295]82    }
[5611]83    public IEnumerable<long> GetRunIds(Filter filter) {
84      return CallQueryService<IEnumerable<long>>(x => x.GetRunIds(filter));
[5295]85    }
[5611]86    public IEnumerable<Run> GetRuns(IEnumerable<long> ids, bool includeBinaryValues) {
87      return CallQueryService<IEnumerable<Run>>(s => s.GetRuns(ids.ToList(), includeBinaryValues));
[5269]88    }
89    #endregion
90
[4481]91    #region Events
[4433]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    }
[4481]102    #endregion
[4433]103
104    #region Helpers
[5299]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    }
[4433]119    #endregion
[4426]120  }
121}
Note: See TracBrowser for help on using the repository browser.