Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2865-Tourplanning/HeuristicLab.Clients.OKB/3.3/RunCreation/RunCreationClient.cs @ 15529

Last change on this file since 15529 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 9.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 Solution GetSolution(long solutionId) {
105      return CallRunCreationService(s => s.GetSolution(solutionId));
106    }
107
108    public byte[] GetSolutionData(long solutionId) {
109      return CallRunCreationService(s => s.GetSolutionData(solutionId));
110    }
111
112    public long AddSolution(Solution solution, byte[] data) {
113      return CallRunCreationService(s => s.AddSolution(solution, data));
114    }
115
116    public void DeleteSolution(Solution solution) {
117      CallRunCreationService(s => s.DeleteSolution(solution));
118    }
119    #endregion
120
121    #region Run Methods
122    public void AddRun(Run run) {
123      CallRunCreationService(s => s.AddRun(run));
124    }
125    #endregion
126
127    #region Characteristic Methods
128    public IEnumerable<Value> GetCharacteristicValues(long problemId) {
129      return CallRunCreationService(s => s.GetCharacteristicValues(problemId));
130    }
131
132    public void SetCharacteristicValue(long problemId, Value v) {
133      CallRunCreationService(s => s.SetCharacteristicValue(problemId, v));
134    }
135
136    public void SetCharacteristicValues(long problemId, IEnumerable<Value> values) {
137      CallRunCreationService(s => s.SetCharacteristicValues(problemId, values.ToList()));
138    }
139    #endregion
140
141    #region OKB-Item Conversion
142    public IItem ConvertToItem(Value value) {
143      if (value is BinaryValue) {
144        IItem item = null;
145        var binaryValue = (BinaryValue)value;
146        if (binaryValue.Value != null) {
147          using (var stream = new MemoryStream(binaryValue.Value)) {
148            try {
149              item = XmlParser.Deserialize<IItem>(stream);
150            } catch (Exception) { }
151            stream.Close();
152          }
153        }
154        return item ?? new Data.StringValue(value.DataType.Name);
155      } else if (value is BoolValue) {
156        return new Data.BoolValue(((BoolValue)value).Value);
157      } else if (value is FloatValue) {
158        return new Data.DoubleValue(((FloatValue)value).Value);
159      } else if (value is PercentValue) {
160        return new Data.PercentValue(((PercentValue)value).Value);
161      } else if (value is DoubleValue) {
162        return new Data.DoubleValue(((DoubleValue)value).Value);
163      } else if (value is IntValue) {
164        return new Data.IntValue((int)((IntValue)value).Value);
165      } else if (value is LongValue) {
166        return new Data.IntValue((int)((LongValue)value).Value);
167      } else if (value is StringValue) {
168        return new Data.StringValue(((StringValue)value).Value);
169      } else if (value is TimeSpanValue) {
170        return new Data.TimeSpanValue(TimeSpan.FromSeconds((long)((TimeSpanValue)value).Value));
171      }
172      return null;
173    }
174
175    public Value ConvertToValue(IItem item, string name) {
176      Value result = null;
177      if (item is ValueTypeValue<bool>) {
178        var boolValue = (ValueTypeValue<bool>)item;
179        result = new BoolValue() { Value = boolValue.Value };
180      } else if (item is ValueTypeValue<int>) {
181        var intValue = (ValueTypeValue<int>)item;
182        result = new IntValue() { Value = intValue.Value };
183      } else if (item is ValueTypeValue<long>) {
184        var longValue = (ValueTypeValue<long>)item;
185        result = new LongValue() { Value = longValue.Value };
186      } else if (item is ValueTypeValue<float>) {
187        var floatValue = (ValueTypeValue<float>)item;
188        result = new FloatValue() { Value = floatValue.Value };
189      } else if (item is ValueTypeValue<double>) {
190        var doubleValue = (ValueTypeValue<double>)item;
191        if (item is Data.PercentValue) result = new PercentValue() { Value = doubleValue.Value };
192        else result = new DoubleValue() { Value = doubleValue.Value };
193      } else if (item is ValueTypeValue<TimeSpan>) {
194        var timeSpanValue = (ValueTypeValue<TimeSpan>)item;
195        result = new TimeSpanValue() { Value = (long)timeSpanValue.Value.TotalSeconds };
196      } else if (item is Data.StringValue) {
197        var stringValue = (Data.StringValue)item;
198        result = new StringValue() { Value = stringValue.Value };
199      }
200      if (result == null) {
201        var binaryValue = new BinaryValue {
202          DataType = new DataType() {
203            Name = item.GetType().Name,
204            TypeName = item.GetType().AssemblyQualifiedName
205          }
206        };
207        using (var memStream = new MemoryStream()) {
208          XmlGenerator.Serialize(item, memStream);
209          binaryValue.Value = memStream.ToArray();
210        }
211        result = binaryValue;
212      }
213      result.Name = name;
214      return result;
215    }
216    #endregion
217
218    #region Events
219    public event EventHandler Refreshing;
220    private void OnRefreshing() {
221      EventHandler handler = Refreshing;
222      if (handler != null) handler(this, EventArgs.Empty);
223    }
224    public event EventHandler Refreshed;
225    private void OnRefreshed() {
226      EventHandler handler = Refreshed;
227      if (handler != null) handler(this, EventArgs.Empty);
228    }
229    #endregion
230
231    #region Helpers
232    private void CallRunCreationService(Action<IRunCreationService> call) {
233      RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>();
234      try {
235        call(client);
236      } finally {
237        try {
238          client.Close();
239        } catch (Exception) {
240          client.Abort();
241        }
242      }
243    }
244    private T CallRunCreationService<T>(Func<IRunCreationService, T> call) {
245      RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>();
246      try {
247        return call(client);
248      } finally {
249        try {
250          client.Close();
251        } catch (Exception) {
252          client.Abort();
253        }
254      }
255    }
256    #endregion
257  }
258}
Note: See TracBrowser for help on using the repository browser.