#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Clients.Common;
using HeuristicLab.Common;
using HeuristicLab.Core;
namespace HeuristicLab.Clients.OKB.Query {
[Item("QueryClient", "OKB query client.")]
public sealed class QueryClient : IContent {
private static QueryClient instance;
public static QueryClient Instance {
get {
if (instance == null) instance = new QueryClient();
return instance;
}
}
#region Properties
private List filters;
public IEnumerable Filters {
get { return filters; }
}
private List valueNames;
public IEnumerable ValueNames {
get { return valueNames; }
}
#endregion
private QueryClient() {
filters = new List();
valueNames = new List();
}
#region Refresh
public void Refresh() {
OnRefreshing();
filters = new List();
try {
filters.AddRange(CallQueryService>(s => s.GetFilters()));
valueNames.AddRange(CallQueryService>(s => s.GetValueNames()));
}
finally {
OnRefreshed();
}
}
public void RefreshAsync(Action exceptionCallback) {
var call = new Func(delegate() {
try {
Refresh();
}
catch (Exception ex) {
return ex;
}
return null;
});
call.BeginInvoke(delegate(IAsyncResult result) {
Exception ex = call.EndInvoke(result);
if (ex != null) exceptionCallback(ex);
}, null);
}
#endregion
#region Query Methods
public long GetNumberOfRuns(Filter filter) {
return CallQueryService(x => x.GetNumberOfRuns(filter));
}
public IEnumerable GetRunIds(Filter filter) {
return CallQueryService>(x => x.GetRunIds(filter));
}
public IEnumerable GetRuns(IEnumerable ids, bool includeBinaryValues) {
return CallQueryService>(s => s.GetRuns(ids.ToList(), includeBinaryValues));
}
public IEnumerable GetRunsWithValues(IEnumerable ids, bool includeBinaryValues, IEnumerable vn) {
return CallQueryService>(s => s.GetRunsWithValues(ids.ToList(), includeBinaryValues, vn.ToList()));
}
#endregion
#region Events
public event EventHandler Refreshing;
private void OnRefreshing() {
EventHandler handler = Refreshing;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Refreshed;
private void OnRefreshed() {
EventHandler handler = Refreshed;
if (handler != null) handler(this, EventArgs.Empty);
}
#endregion
#region Helpers
private T CallQueryService(Func call) {
QueryServiceClient client = ClientFactory.CreateClient();
try {
return call(client);
}
finally {
try {
client.Close();
}
catch (Exception) {
client.Abort();
}
}
}
#endregion
}
}