Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13503 was 13503, checked in by abeham, 8 years ago

#2560:

  • Added clients methods for the new service methods
  • Added partial class for the dto value classes of the run creation client so that one doesn't have to specify the DataType field
  • Added missing license header to UnknownCharacteristicType
File size: 6.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.IO;
25using System.Linq;
26using HeuristicLab.Clients.Common;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.Xml;
30
31namespace HeuristicLab.Clients.OKB.Query {
32  [Item("QueryClient", "OKB query client.")]
33  public sealed class QueryClient : IContent {
34    private static QueryClient instance;
35    public static QueryClient Instance {
36      get {
37        if (instance == null) instance = new QueryClient();
38        return instance;
39      }
40    }
41
42    #region Properties
43    private List<Filter> filters;
44    public IEnumerable<Filter> Filters {
45      get { return filters; }
46    }
47    private List<ValueName> valueNames;
48    public IEnumerable<ValueName> ValueNames {
49      get { return valueNames; }
50    }
51    #endregion
52
53    private QueryClient() {
54      filters = new List<Filter>();
55      valueNames = new List<ValueName>();
56    }
57
58    #region Refresh
59    public void Refresh() {
60      OnRefreshing();
61      filters = new List<Filter>();
62      try {
63        filters.AddRange(CallQueryService<List<Filter>>(s => s.GetFilters()));
64        valueNames.AddRange(CallQueryService<List<ValueName>>(s => s.GetValueNames()));
65      } finally {
66        OnRefreshed();
67      }
68    }
69    public void RefreshAsync(Action<Exception> exceptionCallback) {
70      var call = new Func<Exception>(delegate() {
71        try {
72          Refresh();
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 Characteristic Methods
101    public List<Value> GetCharacteristics(long problemId) {
102      return CallQueryService(s => s.GetCharacteristics(problemId));
103    }
104    #endregion
105
106    #region OKB-Item Conversion
107    public Optimization.IRun ConvertToOptimizationRun(Run run) {
108      Optimization.Run optRun = new Optimization.Run();
109      foreach (Value value in run.ParameterValues)
110        optRun.Parameters.Add(value.Name, ConvertToItem(value));
111      foreach (Value value in run.ResultValues)
112        optRun.Results.Add(value.Name, ConvertToItem(value));
113      return optRun;
114    }
115
116    public IItem ConvertToItem(Value value) {
117      if (value is BinaryValue) {
118        IItem item = null;
119        var binaryValue = (BinaryValue)value;
120        if (binaryValue.Value != null) {
121          using (var stream = new MemoryStream(binaryValue.Value)) {
122            try {
123              item = XmlParser.Deserialize<IItem>(stream);
124            } catch (Exception) { }
125            stream.Close();
126          }
127        }
128        return item ?? new Data.StringValue(value.DataType.Name);
129      } else if (value is BoolValue) {
130        return new Data.BoolValue(((BoolValue)value).Value);
131      } else if (value is FloatValue) {
132        return new Data.DoubleValue(((FloatValue)value).Value);
133      } else if (value is PercentValue) {
134        return new Data.PercentValue(((PercentValue)value).Value);
135      } else if (value is DoubleValue) {
136        return new Data.DoubleValue(((DoubleValue)value).Value);
137      } else if (value is IntValue) {
138        return new Data.IntValue((int)((IntValue)value).Value);
139      } else if (value is LongValue) {
140        return new Data.IntValue((int)((LongValue)value).Value);
141      } else if (value is StringValue) {
142        return new Data.StringValue(((StringValue)value).Value);
143      } else if (value is TimeSpanValue) {
144        return new Data.TimeSpanValue(TimeSpan.FromSeconds((long)((TimeSpanValue)value).Value));
145      }
146      return null;
147    }
148    #endregion
149
150    #region Events
151    public event EventHandler Refreshing;
152    private void OnRefreshing() {
153      EventHandler handler = Refreshing;
154      if (handler != null) handler(this, EventArgs.Empty);
155    }
156    public event EventHandler Refreshed;
157    private void OnRefreshed() {
158      EventHandler handler = Refreshed;
159      if (handler != null) handler(this, EventArgs.Empty);
160    }
161    #endregion
162
163    #region Helpers
164    private T CallQueryService<T>(Func<IQueryService, T> call) {
165      QueryServiceClient client = ClientFactory.CreateClient<QueryServiceClient, IQueryService>();
166      try {
167        return call(client);
168      } finally {
169        try {
170          client.Close();
171        } catch (Exception) {
172          client.Abort();
173        }
174      }
175    }
176    #endregion
177  }
178}
Note: See TracBrowser for help on using the repository browser.