Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.OKB/3.3/Query/QueryClient.cs @ 16755

Last change on this file since 16755 was 13534, checked in by abeham, 9 years ago

#2560:

  • Updated clients (moving of all characteristic service methods to run creation service)
  • Adapted OKB problem view to display characteristic values for the instances
File size: 5.9 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 OKB-Item Conversion
101    public Optimization.IRun ConvertToOptimizationRun(Run run) {
102      Optimization.Run optRun = new Optimization.Run();
103      foreach (Value value in run.ParameterValues)
104        optRun.Parameters.Add(value.Name, ConvertToItem(value));
105      foreach (Value value in run.ResultValues)
106        optRun.Results.Add(value.Name, ConvertToItem(value));
107      return optRun;
108    }
109
110    public IItem ConvertToItem(Value value) {
111      if (value is BinaryValue) {
112        IItem item = null;
113        var binaryValue = (BinaryValue)value;
114        if (binaryValue.Value != null) {
115          using (var stream = new MemoryStream(binaryValue.Value)) {
116            try {
117              item = XmlParser.Deserialize<IItem>(stream);
118            } catch (Exception) { }
119            stream.Close();
120          }
121        }
122        return item ?? new Data.StringValue(value.DataType.Name);
123      } else if (value is BoolValue) {
124        return new Data.BoolValue(((BoolValue)value).Value);
125      } else if (value is FloatValue) {
126        return new Data.DoubleValue(((FloatValue)value).Value);
127      } else if (value is PercentValue) {
128        return new Data.PercentValue(((PercentValue)value).Value);
129      } else if (value is DoubleValue) {
130        return new Data.DoubleValue(((DoubleValue)value).Value);
131      } else if (value is IntValue) {
132        return new Data.IntValue((int)((IntValue)value).Value);
133      } else if (value is LongValue) {
134        return new Data.IntValue((int)((LongValue)value).Value);
135      } else if (value is StringValue) {
136        return new Data.StringValue(((StringValue)value).Value);
137      } else if (value is TimeSpanValue) {
138        return new Data.TimeSpanValue(TimeSpan.FromSeconds((long)((TimeSpanValue)value).Value));
139      }
140      return null;
141    }
142    #endregion
143
144    #region Events
145    public event EventHandler Refreshing;
146    private void OnRefreshing() {
147      EventHandler handler = Refreshing;
148      if (handler != null) handler(this, EventArgs.Empty);
149    }
150    public event EventHandler Refreshed;
151    private void OnRefreshed() {
152      EventHandler handler = Refreshed;
153      if (handler != null) handler(this, EventArgs.Empty);
154    }
155    #endregion
156
157    #region Helpers
158    private T CallQueryService<T>(Func<IQueryService, T> call) {
159      QueryServiceClient client = ClientFactory.CreateClient<QueryServiceClient, IQueryService>();
160      try {
161        return call(client);
162      } finally {
163        try {
164          client.Close();
165        } catch (Exception) {
166          client.Abort();
167        }
168      }
169    }
170    #endregion
171  }
172}
Note: See TracBrowser for help on using the repository browser.