Changeset 15973 for branches/2522_RefactorPluginInfrastructure/HeuristicLab.Clients.OKB/3.3/RunCreation/RunCreationClient.cs
- Timestamp:
- 06/28/18 11:13:37 (6 years ago)
- Location:
- branches/2522_RefactorPluginInfrastructure
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2522_RefactorPluginInfrastructure
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/2522_RefactorPluginInfrastructure/HeuristicLab.Clients.OKB/3.3/RunCreation/RunCreationClient.cs
r12012 r15973 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 22 using HeuristicLab.Clients.Common; 25 23 using HeuristicLab.Common; 26 24 using HeuristicLab.Core; 25 using HeuristicLab.Data; 26 using HeuristicLab.Persistence.Default.Xml; 27 using System; 28 using System.Collections.Generic; 29 using System.IO; 30 using System.Linq; 27 31 28 32 namespace HeuristicLab.Clients.OKB.RunCreation { … … 61 65 algorithms.AddRange(CallRunCreationService<List<Algorithm>>(s => s.GetAlgorithms("HeuristicLab 3.3"))); 62 66 problems.AddRange(CallRunCreationService<List<Problem>>(s => s.GetProblems("HeuristicLab 3.3"))); 63 } 64 finally { 67 } finally { 65 68 OnRefreshed(); 66 69 } … … 70 73 try { 71 74 Refresh(); 72 } 73 catch (Exception ex) { 75 } catch (Exception ex) { 74 76 return ex; 75 77 } … … 84 86 85 87 #region Algorithm Methods 86 public staticbyte[] GetAlgorithmData(long algorithmId) {88 public byte[] GetAlgorithmData(long algorithmId) { 87 89 return CallRunCreationService<byte[]>(s => s.GetAlgorithmData(algorithmId)); 88 90 } … … 90 92 91 93 #region Problem Methods 92 public staticbyte[] GetProblemData(long problemId) {94 public byte[] GetProblemData(long problemId) { 93 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)); 94 118 } 95 119 #endregion … … 98 122 public void AddRun(Run run) { 99 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; 100 215 } 101 216 #endregion … … 115 230 116 231 #region Helpers 117 private staticvoid CallRunCreationService(Action<IRunCreationService> call) {232 private void CallRunCreationService(Action<IRunCreationService> call) { 118 233 RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>(); 119 234 try { 120 235 call(client); 121 } 122 finally { 236 } finally { 123 237 try { 124 238 client.Close(); 125 } 126 catch (Exception) { 239 } catch (Exception) { 127 240 client.Abort(); 128 241 } 129 242 } 130 243 } 131 private staticT CallRunCreationService<T>(Func<IRunCreationService, T> call) {244 private T CallRunCreationService<T>(Func<IRunCreationService, T> call) { 132 245 RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>(); 133 246 try { 134 247 return call(client); 135 } 136 finally { 248 } finally { 137 249 try { 138 250 client.Close(); 139 } 140 catch (Exception) { 251 } catch (Exception) { 141 252 client.Abort(); 142 253 }
Note: See TracChangeset
for help on using the changeset viewer.