[4426] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7331] | 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System;
|
---|
[4466] | 23 | using System.Collections.Generic;
|
---|
[4433] | 24 | using System.Linq;
|
---|
[4426] | 25 | using HeuristicLab.Clients.Common;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 |
|
---|
[5606] | 29 | namespace 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 | }
|
---|
[7589] | 45 | private List<ValueName> valueNames;
|
---|
| 46 | public IEnumerable<ValueName> ValueNames {
|
---|
| 47 | get { return valueNames; }
|
---|
| 48 | }
|
---|
[4481] | 49 | #endregion
|
---|
[4426] | 50 |
|
---|
[5606] | 51 | private QueryClient() {
|
---|
[5611] | 52 | filters = new List<Filter>();
|
---|
[7589] | 53 | valueNames = new List<ValueName>();
|
---|
[4549] | 54 | }
|
---|
[4426] | 55 |
|
---|
[4481] | 56 | #region Refresh
|
---|
[4433] | 57 | public void Refresh() {
|
---|
| 58 | OnRefreshing();
|
---|
[5611] | 59 | filters = new List<Filter>();
|
---|
| 60 | try {
|
---|
| 61 | filters.AddRange(CallQueryService<List<Filter>>(s => s.GetFilters()));
|
---|
[7589] | 62 | valueNames.AddRange(CallQueryService<List<ValueName>>(s => s.GetValueNames()));
|
---|
[5611] | 63 | }
|
---|
| 64 | finally {
|
---|
| 65 | OnRefreshed();
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | public void RefreshAsync(Action<Exception> exceptionCallback) {
|
---|
[4441] | 69 | var call = new Func<Exception>(delegate() {
|
---|
| 70 | try {
|
---|
[5611] | 71 | Refresh();
|
---|
[4441] | 72 | }
|
---|
| 73 | catch (Exception ex) {
|
---|
| 74 | return ex;
|
---|
| 75 | }
|
---|
[5611] | 76 | return null;
|
---|
[4433] | 77 | });
|
---|
[4441] | 78 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
| 79 | Exception ex = call.EndInvoke(result);
|
---|
[5611] | 80 | if (ex != null) exceptionCallback(ex);
|
---|
[4433] | 81 | }, null);
|
---|
[4426] | 82 | }
|
---|
[4481] | 83 | #endregion
|
---|
[4426] | 84 |
|
---|
[5269] | 85 | #region Query Methods
|
---|
[5611] | 86 | public long GetNumberOfRuns(Filter filter) {
|
---|
| 87 | return CallQueryService<long>(x => x.GetNumberOfRuns(filter));
|
---|
[5295] | 88 | }
|
---|
[5611] | 89 | public IEnumerable<long> GetRunIds(Filter filter) {
|
---|
| 90 | return CallQueryService<IEnumerable<long>>(x => x.GetRunIds(filter));
|
---|
[5295] | 91 | }
|
---|
[5611] | 92 | public IEnumerable<Run> GetRuns(IEnumerable<long> ids, bool includeBinaryValues) {
|
---|
| 93 | return CallQueryService<IEnumerable<Run>>(s => s.GetRuns(ids.ToList(), includeBinaryValues));
|
---|
[5269] | 94 | }
|
---|
[7589] | 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 | }
|
---|
[5269] | 98 | #endregion
|
---|
| 99 |
|
---|
[4481] | 100 | #region Events
|
---|
[4433] | 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 | }
|
---|
[4481] | 111 | #endregion
|
---|
[4433] | 112 |
|
---|
| 113 | #region Helpers
|
---|
[5299] | 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 | }
|
---|
[4433] | 128 | #endregion
|
---|
[4426] | 129 | }
|
---|
| 130 | }
|
---|