Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17243 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.9 KB
RevLine 
[8055]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8055]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;
[13474]24using System.IO;
[8055]25using System.Linq;
[14732]26using System.ServiceModel;
[8055]27using HeuristicLab.Clients.Common;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
[13474]30using HeuristicLab.Persistence.Default.Xml;
[8055]31
32namespace HeuristicLab.Clients.OKB.Query {
33  [Item("QueryClient", "OKB query client.")]
34  public sealed class QueryClient : IContent {
35    private static QueryClient instance;
36    public static QueryClient Instance {
37      get {
38        if (instance == null) instance = new QueryClient();
39        return instance;
40      }
41    }
42
[14732]43    private int endpointRetries;
44    private string workingEndpoint;
45
[8055]46    #region Properties
47    private List<Filter> filters;
48    public IEnumerable<Filter> Filters {
49      get { return filters; }
50    }
51    private List<ValueName> valueNames;
52    public IEnumerable<ValueName> ValueNames {
53      get { return valueNames; }
54    }
55    #endregion
56
57    private QueryClient() {
58      filters = new List<Filter>();
59      valueNames = new List<ValueName>();
60    }
61
62    #region Refresh
63    public void Refresh() {
64      OnRefreshing();
65      filters = new List<Filter>();
66      try {
67        filters.AddRange(CallQueryService<List<Filter>>(s => s.GetFilters()));
68        valueNames.AddRange(CallQueryService<List<ValueName>>(s => s.GetValueNames()));
[13474]69      } finally {
[8055]70        OnRefreshed();
71      }
72    }
73    public void RefreshAsync(Action<Exception> exceptionCallback) {
[14732]74      var call = new Func<Exception>(delegate () {
[8055]75        try {
76          Refresh();
[13474]77        } catch (Exception ex) {
[8055]78          return ex;
79        }
80        return null;
81      });
[14732]82      call.BeginInvoke(delegate (IAsyncResult result) {
[8055]83        Exception ex = call.EndInvoke(result);
84        if (ex != null) exceptionCallback(ex);
85      }, null);
86    }
87    #endregion
88
89    #region Query Methods
90    public long GetNumberOfRuns(Filter filter) {
91      return CallQueryService<long>(x => x.GetNumberOfRuns(filter));
92    }
93    public IEnumerable<long> GetRunIds(Filter filter) {
94      return CallQueryService<IEnumerable<long>>(x => x.GetRunIds(filter));
95    }
96    public IEnumerable<Run> GetRuns(IEnumerable<long> ids, bool includeBinaryValues) {
97      return CallQueryService<IEnumerable<Run>>(s => s.GetRuns(ids.ToList(), includeBinaryValues));
98    }
99    public IEnumerable<Run> GetRunsWithValues(IEnumerable<long> ids, bool includeBinaryValues, IEnumerable<ValueName> vn) {
100      return CallQueryService<IEnumerable<Run>>(s => s.GetRunsWithValues(ids.ToList(), includeBinaryValues, vn.ToList()));
101    }
102    #endregion
103
[13474]104    #region OKB-Item Conversion
105    public Optimization.IRun ConvertToOptimizationRun(Run run) {
106      Optimization.Run optRun = new Optimization.Run();
107      foreach (Value value in run.ParameterValues)
108        optRun.Parameters.Add(value.Name, ConvertToItem(value));
109      foreach (Value value in run.ResultValues)
110        optRun.Results.Add(value.Name, ConvertToItem(value));
111      return optRun;
112    }
113
114    public IItem ConvertToItem(Value value) {
115      if (value is BinaryValue) {
116        IItem item = null;
117        var binaryValue = (BinaryValue)value;
118        if (binaryValue.Value != null) {
119          using (var stream = new MemoryStream(binaryValue.Value)) {
120            try {
121              item = XmlParser.Deserialize<IItem>(stream);
122            } catch (Exception) { }
123            stream.Close();
124          }
125        }
126        return item ?? new Data.StringValue(value.DataType.Name);
127      } else if (value is BoolValue) {
128        return new Data.BoolValue(((BoolValue)value).Value);
129      } else if (value is FloatValue) {
130        return new Data.DoubleValue(((FloatValue)value).Value);
131      } else if (value is PercentValue) {
132        return new Data.PercentValue(((PercentValue)value).Value);
133      } else if (value is DoubleValue) {
134        return new Data.DoubleValue(((DoubleValue)value).Value);
135      } else if (value is IntValue) {
136        return new Data.IntValue((int)((IntValue)value).Value);
137      } else if (value is LongValue) {
138        return new Data.IntValue((int)((LongValue)value).Value);
139      } else if (value is StringValue) {
140        return new Data.StringValue(((StringValue)value).Value);
141      } else if (value is TimeSpanValue) {
142        return new Data.TimeSpanValue(TimeSpan.FromSeconds((long)((TimeSpanValue)value).Value));
143      }
144      return null;
145    }
146    #endregion
147
[8055]148    #region Events
149    public event EventHandler Refreshing;
150    private void OnRefreshing() {
151      EventHandler handler = Refreshing;
152      if (handler != null) handler(this, EventArgs.Empty);
153    }
154    public event EventHandler Refreshed;
155    private void OnRefreshed() {
156      EventHandler handler = Refreshed;
157      if (handler != null) handler(this, EventArgs.Empty);
158    }
159    #endregion
160
161    #region Helpers
[14732]162    private QueryServiceClient NewServiceClient() {
163      if (endpointRetries >= Properties.Settings.Default.MaxEndpointRetries)
164        return CreateClient(workingEndpoint);
165
166      var configurations = Properties.Settings.Default.EndpointConfigurationPriorities;
167      Exception exception = null;
168
169      foreach (var endpointConfigurationName in configurations) {
170        try {
171          var cl = CreateClient(endpointConfigurationName);
172          cl.Open();
173          workingEndpoint = endpointConfigurationName;
174          return cl;
175        } catch (EndpointNotFoundException e) {
176          exception = e;
177          ++endpointRetries;
178        }
179      }
180
181      throw exception ?? new EndpointNotFoundException("No endpoint for Query service found.");
182    }
183
184    private QueryServiceClient CreateClient(string endpointConfigurationName) {
185      var cl = ClientFactory.CreateClient<QueryServiceClient, IQueryService>(endpointConfigurationName);
186      return cl;
187    }
188
[8055]189    private T CallQueryService<T>(Func<IQueryService, T> call) {
[14732]190      QueryServiceClient client = NewServiceClient();
191
[8055]192      try {
193        return call(client);
[13474]194      } finally {
[8055]195        try {
196          client.Close();
[13474]197        } catch (Exception) {
[8055]198          client.Abort();
199        }
200      }
201    }
202    #endregion
203  }
204}
Note: See TracBrowser for help on using the repository browser.