Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.OKB/3.3/RunCreation/RunCreationClient.cs @ 13827

Last change on this file since 13827 was 13550, checked in by abeham, 9 years ago

#2560: changed characteristic calculator to output IItem instead of double

File size: 8.3 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.Data;
30using HeuristicLab.Persistence.Default.Xml;
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 static byte[] GetAlgorithmData(long algorithmId) {
89      return CallRunCreationService<byte[]>(s => s.GetAlgorithmData(algorithmId));
90    }
91    #endregion
92
93    #region Problem Methods
94    public static byte[] GetProblemData(long problemId) {
95      return CallRunCreationService<byte[]>(s => s.GetProblemData(problemId));
96    }
97    #endregion
98
99    #region Run Methods
100    public void AddRun(Run run) {
101      CallRunCreationService(s => s.AddRun(run));
102    }
103    #endregion
104
105    #region Characteristic Methods
106    public static IEnumerable<Value> GetCharacteristicValues(long problemId) {
107      return CallRunCreationService(s => s.GetCharacteristicValues(problemId));
108    }
109
110    public static void SetCharacteristicValue(long problemId, Value v) {
111      CallRunCreationService(s => s.SetCharacteristicValue(problemId, v));
112    }
113
114    public static void SetCharacteristicValues(long problemId, IEnumerable<Value> values) {
115      CallRunCreationService(s => s.SetCharacteristicValues(problemId, values.ToList()));
116    }
117    #endregion
118
119    #region OKB-Item Conversion
120    public IItem ConvertToItem(Value value) {
121      if (value is BinaryValue) {
122        IItem item = null;
123        var binaryValue = (BinaryValue)value;
124        if (binaryValue.Value != null) {
125          using (var stream = new MemoryStream(binaryValue.Value)) {
126            try {
127              item = XmlParser.Deserialize<IItem>(stream);
128            } catch (Exception) { }
129            stream.Close();
130          }
131        }
132        return item ?? new Data.StringValue(value.DataType.Name);
133      } else if (value is BoolValue) {
134        return new Data.BoolValue(((BoolValue)value).Value);
135      } else if (value is FloatValue) {
136        return new Data.DoubleValue(((FloatValue)value).Value);
137      } else if (value is PercentValue) {
138        return new Data.PercentValue(((PercentValue)value).Value);
139      } else if (value is DoubleValue) {
140        return new Data.DoubleValue(((DoubleValue)value).Value);
141      } else if (value is IntValue) {
142        return new Data.IntValue((int)((IntValue)value).Value);
143      } else if (value is LongValue) {
144        return new Data.IntValue((int)((LongValue)value).Value);
145      } else if (value is StringValue) {
146        return new Data.StringValue(((StringValue)value).Value);
147      } else if (value is TimeSpanValue) {
148        return new Data.TimeSpanValue(TimeSpan.FromSeconds((long)((TimeSpanValue)value).Value));
149      }
150      return null;
151    }
152
153    public Value ConvertToValue(IItem item, string name) {
154      Value result = null;
155      if (item is ValueTypeValue<bool>) {
156        var boolValue = (ValueTypeValue<bool>)item;
157        result = new BoolValue() { Value = boolValue.Value };
158      } else if (item is ValueTypeValue<int>) {
159        var intValue = (ValueTypeValue<int>)item;
160        result = new IntValue() { Value = intValue.Value };
161      } else if (item is ValueTypeValue<long>) {
162        var longValue = (ValueTypeValue<long>)item;
163        result = new LongValue() { Value = longValue.Value };
164      } else if (item is ValueTypeValue<float>) {
165        var floatValue = (ValueTypeValue<float>)item;
166        result = new FloatValue() { Value = floatValue.Value };
167      } else if (item is ValueTypeValue<double>) {
168        var doubleValue = (ValueTypeValue<double>)item;
169        if (item is Data.PercentValue) result = new PercentValue() { Value = doubleValue.Value };
170        else result = new DoubleValue() { Value = doubleValue.Value };
171      } else if (item is ValueTypeValue<TimeSpan>) {
172        var timeSpanValue = (ValueTypeValue<TimeSpan>)item;
173        result = new TimeSpanValue() { Value = (long)timeSpanValue.Value.TotalSeconds };
174      } else if (item is Data.StringValue) {
175        var stringValue = (Data.StringValue)item;
176        result = new StringValue() { Value = stringValue.Value };
177      }
178      if (result == null) {
179        var binaryValue = new BinaryValue {
180          DataType = new DataType() {
181            Name = item.GetType().Name,
182            TypeName = item.GetType().AssemblyQualifiedName
183          }
184        };
185        using (var memStream = new MemoryStream()) {
186          XmlGenerator.Serialize(item, memStream);
187          binaryValue.Value = memStream.ToArray();
188        }
189        result = binaryValue;
190      }
191      result.Name = name;
192      return result;
193    }
194    #endregion
195
196    #region Events
197    public event EventHandler Refreshing;
198    private void OnRefreshing() {
199      EventHandler handler = Refreshing;
200      if (handler != null) handler(this, EventArgs.Empty);
201    }
202    public event EventHandler Refreshed;
203    private void OnRefreshed() {
204      EventHandler handler = Refreshed;
205      if (handler != null) handler(this, EventArgs.Empty);
206    }
207    #endregion
208
209    #region Helpers
210    private static void CallRunCreationService(Action<IRunCreationService> call) {
211      RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>();
212      try {
213        call(client);
214      } finally {
215        try {
216          client.Close();
217        } catch (Exception) {
218          client.Abort();
219        }
220      }
221    }
222    private static T CallRunCreationService<T>(Func<IRunCreationService, T> call) {
223      RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>();
224      try {
225        return call(client);
226      } finally {
227        try {
228          client.Close();
229        } catch (Exception) {
230          client.Abort();
231        }
232      }
233    }
234    #endregion
235  }
236}
Note: See TracBrowser for help on using the repository browser.