Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.OKB/3.3/Query/QueryClient.cs @ 13365

Last change on this file since 13365 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.1 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.Query {
30  [Item("QueryClient", "OKB query client.")]
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    private List<ValueName> valueNames;
46    public IEnumerable<ValueName> ValueNames {
47      get { return valueNames; }
48    }
49    #endregion
50
51    private QueryClient() {
52      filters = new List<Filter>();
53      valueNames = new List<ValueName>();
54    }
55
56    #region Refresh
57    public void Refresh() {
58      OnRefreshing();
59      filters = new List<Filter>();
60      try {
61        filters.AddRange(CallQueryService<List<Filter>>(s => s.GetFilters()));
62        valueNames.AddRange(CallQueryService<List<ValueName>>(s => s.GetValueNames()));
63      }
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        }
73        catch (Exception ex) {
74          return ex;
75        }
76        return null;
77      });
78      call.BeginInvoke(delegate(IAsyncResult result) {
79        Exception ex = call.EndInvoke(result);
80        if (ex != null) exceptionCallback(ex);
81      }, null);
82    }
83    #endregion
84
85    #region Query Methods
86    public long GetNumberOfRuns(Filter filter) {
87      return CallQueryService<long>(x => x.GetNumberOfRuns(filter));
88    }
89    public IEnumerable<long> GetRunIds(Filter filter) {
90      return CallQueryService<IEnumerable<long>>(x => x.GetRunIds(filter));
91    }
92    public IEnumerable<Run> GetRuns(IEnumerable<long> ids, bool includeBinaryValues) {
93      return CallQueryService<IEnumerable<Run>>(s => s.GetRuns(ids.ToList(), includeBinaryValues));
94    }
95    public IEnumerable<Run> GetRunsWithValues(IEnumerable<long> ids, bool includeBinaryValues, IEnumerable<ValueName> vn) {
96      return CallQueryService<IEnumerable<Run>>(s => s.GetRunsWithValues(ids.ToList(), includeBinaryValues, vn.ToList()));
97    }
98    #endregion
99
100    #region Events
101    public event EventHandler Refreshing;
102    private void OnRefreshing() {
103      EventHandler handler = Refreshing;
104      if (handler != null) handler(this, EventArgs.Empty);
105    }
106    public event EventHandler Refreshed;
107    private void OnRefreshed() {
108      EventHandler handler = Refreshed;
109      if (handler != null) handler(this, EventArgs.Empty);
110    }
111    #endregion
112
113    #region Helpers
114    private T CallQueryService<T>(Func<IQueryService, T> call) {
115      QueryServiceClient client = ClientFactory.CreateClient<QueryServiceClient, IQueryService>();
116      try {
117        return call(client);
118      }
119      finally {
120        try {
121          client.Close();
122        }
123        catch (Exception) {
124          client.Abort();
125        }
126      }
127    }
128    #endregion
129  }
130}
Note: See TracBrowser for help on using the repository browser.