Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.OKB/3.3/RunCreation/RunCreationClient.cs @ 13684

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

#2588:

  • Returning id in add solution method
  • Added method to delete solutions
  • Added client methods in RunCreationClient that match the service methods
  • Made all service methods non-static in RunCreationClient
File size: 8.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 HeuristicLab.Clients.Common;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Persistence.Default.Xml;
27using System;
28using System.Collections.Generic;
29using System.IO;
30using System.Linq;
31
32namespace HeuristicLab.Clients.OKB.RunCreation {
33  [Item("RunCreationClient", "OKB run creation client.")]
34  public sealed class RunCreationClient : IContent {
35    private static RunCreationClient instance;
36    public static RunCreationClient Instance {
37      get {
38        if (instance == null) instance = new RunCreationClient();
39        return instance;
40      }
41    }
42
43    #region Properties
44    private List<Algorithm> algorithms;
45    public IEnumerable<Algorithm> Algorithms {
46      get { return algorithms; }
47    }
48    private List<Problem> problems;
49    public IEnumerable<Problem> Problems {
50      get { return problems; }
51    }
52    #endregion
53
54    private RunCreationClient() {
55      algorithms = new List<Algorithm>();
56      problems = new List<Problem>();
57    }
58
59    #region Refresh
60    public void Refresh() {
61      OnRefreshing();
62      algorithms = new List<Algorithm>();
63      problems = new List<Problem>();
64      try {
65        algorithms.AddRange(CallRunCreationService<List<Algorithm>>(s => s.GetAlgorithms("HeuristicLab 3.3")));
66        problems.AddRange(CallRunCreationService<List<Problem>>(s => s.GetProblems("HeuristicLab 3.3")));
67      } finally {
68        OnRefreshed();
69      }
70    }
71    public void RefreshAsync(Action<Exception> exceptionCallback) {
72      var call = new Func<Exception>(delegate() {
73        try {
74          Refresh();
75        } catch (Exception ex) {
76          return ex;
77        }
78        return null;
79      });
80      call.BeginInvoke(delegate(IAsyncResult result) {
81        Exception ex = call.EndInvoke(result);
82        if (ex != null) exceptionCallback(ex);
83      }, null);
84    }
85    #endregion
86
87    #region Algorithm Methods
88    public byte[] GetAlgorithmData(long algorithmId) {
89      return CallRunCreationService<byte[]>(s => s.GetAlgorithmData(algorithmId));
90    }
91    #endregion
92
93    #region Problem Methods
94    public byte[] GetProblemData(long problemId) {
95      return CallRunCreationService<byte[]>(s => s.GetProblemData(problemId));
96    }
97    #endregion
98
99    #region Solution Methods
100    public IEnumerable<Solution> GetSolutions(long problemId) {
101      return CallRunCreationService(s => s.GetSolutions(problemId));
102    }
103
104    public byte[] GetSolutionData(long solutionId) {
105      return CallRunCreationService(s => s.GetSolutionData(solutionId));
106    }
107
108    public long AddSolution(Solution solution, byte[] data) {
109      return CallRunCreationService(s => s.AddSolution(solution, data));
110    }
111
112    public void DeleteSolution(Solution solution) {
113      CallRunCreationService(s => s.DeleteSolution(solution));
114    }
115    #endregion
116
117    #region Run Methods
118    public void AddRun(Run run) {
119      CallRunCreationService(s => s.AddRun(run));
120    }
121    #endregion
122
123    #region Characteristic Methods
124    public IEnumerable<Value> GetCharacteristicValues(long problemId) {
125      return CallRunCreationService(s => s.GetCharacteristicValues(problemId));
126    }
127
128    public void SetCharacteristicValue(long problemId, Value v) {
129      CallRunCreationService(s => s.SetCharacteristicValue(problemId, v));
130    }
131
132    public void SetCharacteristicValues(long problemId, IEnumerable<Value> values) {
133      CallRunCreationService(s => s.SetCharacteristicValues(problemId, values.ToList()));
134    }
135    #endregion
136
137    #region OKB-Item Conversion
138    public IItem ConvertToItem(Value value) {
139      if (value is BinaryValue) {
140        IItem item = null;
141        var binaryValue = (BinaryValue)value;
142        if (binaryValue.Value != null) {
143          using (var stream = new MemoryStream(binaryValue.Value)) {
144            try {
145              item = XmlParser.Deserialize<IItem>(stream);
146            } catch (Exception) { }
147            stream.Close();
148          }
149        }
150        return item ?? new Data.StringValue(value.DataType.Name);
151      } else if (value is BoolValue) {
152        return new Data.BoolValue(((BoolValue)value).Value);
153      } else if (value is FloatValue) {
154        return new Data.DoubleValue(((FloatValue)value).Value);
155      } else if (value is PercentValue) {
156        return new Data.PercentValue(((PercentValue)value).Value);
157      } else if (value is DoubleValue) {
158        return new Data.DoubleValue(((DoubleValue)value).Value);
159      } else if (value is IntValue) {
160        return new Data.IntValue((int)((IntValue)value).Value);
161      } else if (value is LongValue) {
162        return new Data.IntValue((int)((LongValue)value).Value);
163      } else if (value is StringValue) {
164        return new Data.StringValue(((StringValue)value).Value);
165      } else if (value is TimeSpanValue) {
166        return new Data.TimeSpanValue(TimeSpan.FromSeconds((long)((TimeSpanValue)value).Value));
167      }
168      return null;
169    }
170
171    public Value ConvertToValue(IItem item, string name) {
172      Value result = null;
173      if (item is ValueTypeValue<bool>) {
174        var boolValue = (ValueTypeValue<bool>)item;
175        result = new BoolValue() { Value = boolValue.Value };
176      } else if (item is ValueTypeValue<int>) {
177        var intValue = (ValueTypeValue<int>)item;
178        result = new IntValue() { Value = intValue.Value };
179      } else if (item is ValueTypeValue<long>) {
180        var longValue = (ValueTypeValue<long>)item;
181        result = new LongValue() { Value = longValue.Value };
182      } else if (item is ValueTypeValue<float>) {
183        var floatValue = (ValueTypeValue<float>)item;
184        result = new FloatValue() { Value = floatValue.Value };
185      } else if (item is ValueTypeValue<double>) {
186        var doubleValue = (ValueTypeValue<double>)item;
187        if (item is Data.PercentValue) result = new PercentValue() { Value = doubleValue.Value };
188        else result = new DoubleValue() { Value = doubleValue.Value };
189      } else if (item is ValueTypeValue<TimeSpan>) {
190        var timeSpanValue = (ValueTypeValue<TimeSpan>)item;
191        result = new TimeSpanValue() { Value = (long)timeSpanValue.Value.TotalSeconds };
192      } else if (item is Data.StringValue) {
193        var stringValue = (Data.StringValue)item;
194        result = new StringValue() { Value = stringValue.Value };
195      }
196      if (result == null) {
197        var binaryValue = new BinaryValue {
198          DataType = new DataType() {
199            Name = item.GetType().Name,
200            TypeName = item.GetType().AssemblyQualifiedName
201          }
202        };
203        using (var memStream = new MemoryStream()) {
204          XmlGenerator.Serialize(item, memStream);
205          binaryValue.Value = memStream.ToArray();
206        }
207        result = binaryValue;
208      }
209      result.Name = name;
210      return result;
211    }
212    #endregion
213
214    #region Events
215    public event EventHandler Refreshing;
216    private void OnRefreshing() {
217      EventHandler handler = Refreshing;
218      if (handler != null) handler(this, EventArgs.Empty);
219    }
220    public event EventHandler Refreshed;
221    private void OnRefreshed() {
222      EventHandler handler = Refreshed;
223      if (handler != null) handler(this, EventArgs.Empty);
224    }
225    #endregion
226
227    #region Helpers
228    private void CallRunCreationService(Action<IRunCreationService> call) {
229      RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>();
230      try {
231        call(client);
232      } finally {
233        try {
234          client.Close();
235        } catch (Exception) {
236          client.Abort();
237        }
238      }
239    }
240    private T CallRunCreationService<T>(Func<IRunCreationService, T> call) {
241      RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>();
242      try {
243        return call(client);
244      } finally {
245        try {
246          client.Close();
247        } catch (Exception) {
248          client.Abort();
249        }
250      }
251    }
252    #endregion
253  }
254}
Note: See TracBrowser for help on using the repository browser.