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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Security.Cryptography;
|
---|
26 | using DA = HeuristicLab.Services.OKB.DataAccess;
|
---|
27 | using DT = HeuristicLab.Services.OKB.RunCreation.DataTransfer;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Services.OKB.RunCreation {
|
---|
30 | public static class Convert {
|
---|
31 | #region ToDto
|
---|
32 | public static DT.Algorithm ToDto(DA.Algorithm source) {
|
---|
33 | if (source == null) return null;
|
---|
34 | return new DT.Algorithm { Id = source.Id, Name = source.Name, Description = source.Description, AlgorithmClass = Convert.ToDto(source.AlgorithmClass), DataType = Convert.ToDto(source.DataType) };
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static DT.Problem ToDto(DA.Problem source) {
|
---|
38 | if (source == null) return null;
|
---|
39 | return new DT.Problem { Id = source.Id, Name = source.Name, Description = source.Description, ProblemClass = Convert.ToDto(source.ProblemClass), DataType = Convert.ToDto(source.DataType) };
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static DT.SingleObjectiveSolution ToDto(DA.SingleObjectiveSolution source) {
|
---|
43 | if (source == null) return null;
|
---|
44 | return new DT.SingleObjectiveSolution() {
|
---|
45 | Id = source.Id,
|
---|
46 | ProblemId = source.ProblemId.Value,
|
---|
47 | RunId = source.RunId,
|
---|
48 | Quality = source.Quality,
|
---|
49 | DataType = ToDto(source.DataType)
|
---|
50 | };
|
---|
51 | }
|
---|
52 |
|
---|
53 | private static DT.DataType ToDto(DA.DataType source) {
|
---|
54 | if (source == null) return null;
|
---|
55 | return new DT.DataType { Name = source.Name, TypeName = source.TypeName };
|
---|
56 | }
|
---|
57 |
|
---|
58 | private static DT.AlgorithmClass ToDto(DA.AlgorithmClass source) {
|
---|
59 | if (source == null) return null;
|
---|
60 | return new DT.AlgorithmClass { Name = source.Name, Description = source.Description };
|
---|
61 | }
|
---|
62 |
|
---|
63 | private static DT.ProblemClass ToDto(DA.ProblemClass source) {
|
---|
64 | if (source == null) return null;
|
---|
65 | return new DT.ProblemClass { Name = source.Name, Description = source.Description };
|
---|
66 | }
|
---|
67 | #endregion
|
---|
68 |
|
---|
69 | #region ToEntity
|
---|
70 | public static DA.Run ToEntity(DT.Run source, DA.OKBDataContext okb) {
|
---|
71 | if (source == null) return null;
|
---|
72 |
|
---|
73 | List<DA.BinaryData> binCache = new List<DA.BinaryData>();
|
---|
74 |
|
---|
75 | DA.Run entity = new DA.Run { Id = 0, AlgorithmId = source.AlgorithmId, ProblemId = source.ProblemId, CreatedDate = source.CreatedDate, UserId = source.UserId, ClientId = source.ClientId };
|
---|
76 | foreach (var value in source.ParameterValues)
|
---|
77 | entity.Values.Add(Convert.ToEntity(value, entity, DA.ValueNameCategory.Parameter, okb, binCache));
|
---|
78 | foreach (var value in source.ResultValues)
|
---|
79 | entity.Values.Add(Convert.ToEntity(value, entity, DA.ValueNameCategory.Result, okb, binCache));
|
---|
80 | return entity;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public static DT.Value ToDto(DA.CharacteristicValue source) {
|
---|
84 | if (source == null) return null;
|
---|
85 | if (source.Characteristic.Type == DA.CharacteristicType.Bool) {
|
---|
86 | return new DT.BoolValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.BoolValue.GetValueOrDefault() };
|
---|
87 | } else if (source.Characteristic.Type == DA.CharacteristicType.Int) {
|
---|
88 | return new DT.IntValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.IntValue.GetValueOrDefault() };
|
---|
89 | } else if (source.Characteristic.Type == DA.CharacteristicType.TimeSpan) {
|
---|
90 | return new DT.TimeSpanValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.LongValue.GetValueOrDefault() };
|
---|
91 | } else if (source.Characteristic.Type == DA.CharacteristicType.Long) {
|
---|
92 | return new DT.LongValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.LongValue.GetValueOrDefault() };
|
---|
93 | } else if (source.Characteristic.Type == DA.CharacteristicType.Float) {
|
---|
94 | return new DT.FloatValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.FloatValue.GetValueOrDefault() };
|
---|
95 | } else if (source.Characteristic.Type == DA.CharacteristicType.Double) {
|
---|
96 | return new DT.DoubleValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.DoubleValue.GetValueOrDefault() };
|
---|
97 | } else if (source.Characteristic.Type == DA.CharacteristicType.Percent) {
|
---|
98 | return new DT.PercentValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.DoubleValue.GetValueOrDefault() };
|
---|
99 | } else if (source.Characteristic.Type == DA.CharacteristicType.String) {
|
---|
100 | return new DT.StringValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.StringValue };
|
---|
101 | } else {
|
---|
102 | throw new ArgumentException("Unknown characteristic type.", "source");
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | public static DA.CharacteristicValue ToEntity(DT.Value source, DA.OKBDataContext okb, DA.Problem problem, DA.CharacteristicType type) {
|
---|
107 | if (okb == null || problem == null || source == null || string.IsNullOrEmpty(source.Name)) throw new ArgumentNullException();
|
---|
108 | var entity = new DA.CharacteristicValue();
|
---|
109 | entity.Problem = problem;
|
---|
110 | entity.DataType = Convert.ToEntity(source.DataType, okb);
|
---|
111 | entity.Characteristic = Convert.ToEntity(source.Name, type, okb);
|
---|
112 | if (source is DT.BoolValue) {
|
---|
113 | entity.BoolValue = ((DT.BoolValue)source).Value;
|
---|
114 | } else if (source is DT.IntValue) {
|
---|
115 | entity.IntValue = ((DT.IntValue)source).Value;
|
---|
116 | } else if (source is DT.TimeSpanValue) {
|
---|
117 | entity.LongValue = ((DT.TimeSpanValue)source).Value;
|
---|
118 | } else if (source is DT.LongValue) {
|
---|
119 | entity.LongValue = ((DT.LongValue)source).Value;
|
---|
120 | } else if (source is DT.FloatValue) {
|
---|
121 | entity.FloatValue = ((DT.FloatValue)source).Value;
|
---|
122 | } else if (source is DT.DoubleValue) {
|
---|
123 | entity.DoubleValue = ((DT.DoubleValue)source).Value;
|
---|
124 | } else if (source is DT.PercentValue) {
|
---|
125 | entity.DoubleValue = ((DT.PercentValue)source).Value;
|
---|
126 | } else if (source is DT.StringValue) {
|
---|
127 | entity.StringValue = ((DT.StringValue)source).Value;
|
---|
128 | } else {
|
---|
129 | throw new ArgumentException("Unknown characteristic type.", "source");
|
---|
130 | }
|
---|
131 | return entity;
|
---|
132 | }
|
---|
133 |
|
---|
134 | private static DA.Characteristic ToEntity(string name, DA.CharacteristicType type, DA.OKBDataContext okb) {
|
---|
135 | if (string.IsNullOrEmpty(name)) return null;
|
---|
136 | var entity = okb.Characteristics.FirstOrDefault(x => (x.Name == name) && (x.Type == type));
|
---|
137 | return entity ?? new DA.Characteristic() { Id = 0, Name = name, Type = type };
|
---|
138 | }
|
---|
139 |
|
---|
140 | private static DA.Value ToEntity(DT.Value source, DA.Run run, DA.ValueNameCategory category, DA.OKBDataContext okb, List<DA.BinaryData> binCache) {
|
---|
141 | if (source == null) return null;
|
---|
142 | var entity = new DA.Value();
|
---|
143 | entity.Run = run;
|
---|
144 | entity.DataType = Convert.ToEntity(source.DataType, okb);
|
---|
145 | if (source is DT.BoolValue) {
|
---|
146 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Bool, okb);
|
---|
147 | entity.BoolValue = ((DT.BoolValue)source).Value;
|
---|
148 | } else if (source is DT.IntValue) {
|
---|
149 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Int, okb);
|
---|
150 | entity.IntValue = ((DT.IntValue)source).Value;
|
---|
151 | } else if (source is DT.TimeSpanValue) {
|
---|
152 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.TimeSpan, okb);
|
---|
153 | entity.LongValue = ((DT.TimeSpanValue)source).Value;
|
---|
154 | } else if (source is DT.LongValue) {
|
---|
155 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Long, okb);
|
---|
156 | entity.LongValue = ((DT.LongValue)source).Value;
|
---|
157 | } else if (source is DT.FloatValue) {
|
---|
158 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Float, okb);
|
---|
159 | entity.FloatValue = ((DT.FloatValue)source).Value;
|
---|
160 | } else if (source is DT.DoubleValue) {
|
---|
161 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Double, okb);
|
---|
162 | entity.DoubleValue = ((DT.DoubleValue)source).Value;
|
---|
163 | } else if (source is DT.PercentValue) {
|
---|
164 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Percent, okb);
|
---|
165 | entity.DoubleValue = ((DT.PercentValue)source).Value;
|
---|
166 | } else if (source is DT.StringValue) {
|
---|
167 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.String, okb);
|
---|
168 | entity.StringValue = ((DT.StringValue)source).Value;
|
---|
169 | } else if (source is DT.BinaryValue) {
|
---|
170 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Binary, okb);
|
---|
171 | entity.BinaryData = Convert.ToEntity(((DT.BinaryValue)source).Value, okb, binCache);
|
---|
172 | } else {
|
---|
173 | throw new ArgumentException("Unknown value type.", "source");
|
---|
174 | }
|
---|
175 | return entity;
|
---|
176 | }
|
---|
177 |
|
---|
178 | public static DA.DataType ToEntity(DT.DataType source, DA.OKBDataContext okb) {
|
---|
179 | if (source == null) return null;
|
---|
180 | var entity = okb.DataTypes.FirstOrDefault(x => (x.Name == source.Name) && (x.TypeName == source.TypeName));
|
---|
181 | if (entity == null)
|
---|
182 | entity = new DA.DataType() { Id = 0, Name = source.Name, TypeName = source.TypeName };
|
---|
183 | return entity;
|
---|
184 | }
|
---|
185 |
|
---|
186 | private static DA.ValueName ToEntity(string name, DA.ValueNameCategory category, DA.ValueNameType type, DA.OKBDataContext okb) {
|
---|
187 | if (string.IsNullOrEmpty(name)) return null;
|
---|
188 | var entity = okb.ValueNames.FirstOrDefault(x => (x.Name == name) && (x.Category == category) && (x.Type == type));
|
---|
189 | if (entity == null)
|
---|
190 | entity = new DA.ValueName() { Id = 0, Name = name, Category = category, Type = type };
|
---|
191 | return entity;
|
---|
192 | }
|
---|
193 |
|
---|
194 | private static DA.BinaryData ToEntity(byte[] data, DA.OKBDataContext okb, List<DA.BinaryData> binCache) {
|
---|
195 | if (data == null) return null;
|
---|
196 | byte[] hash;
|
---|
197 | using (SHA1 sha1 = SHA1.Create()) {
|
---|
198 | hash = sha1.ComputeHash(data);
|
---|
199 | }
|
---|
200 |
|
---|
201 | var cachedBinaryData = binCache.FirstOrDefault(x => x.Hash.SequenceEqual(hash));
|
---|
202 | if (cachedBinaryData != null)
|
---|
203 | return cachedBinaryData;
|
---|
204 |
|
---|
205 | var entity = okb.BinaryDatas.FirstOrDefault(x => x.Hash.Equals(hash));
|
---|
206 | if (entity == null) {
|
---|
207 | entity = new DA.BinaryData() { Id = 0, Data = data, Hash = hash };
|
---|
208 | binCache.Add(entity);
|
---|
209 | }
|
---|
210 |
|
---|
211 | return entity;
|
---|
212 | }
|
---|
213 |
|
---|
214 | public static DA.SingleObjectiveSolution ToEntity(DT.SingleObjectiveSolution source, byte[] data, DA.OKBDataContext okb) {
|
---|
215 | var sol = okb.SingleObjectiveSolutions.SingleOrDefault(x => x.Id == source.Id) ?? new DA.SingleObjectiveSolution() {
|
---|
216 | ProblemId = source.ProblemId,
|
---|
217 | RunId = source.RunId,
|
---|
218 | Quality = source.Quality
|
---|
219 | };
|
---|
220 | if (source.DataType != null) {
|
---|
221 | sol.DataType = ToEntity(source.DataType, okb);
|
---|
222 | }
|
---|
223 | if (data != null && data.Length > 0) {
|
---|
224 | byte[] hash;
|
---|
225 | using (var sha1 = SHA1.Create()) {
|
---|
226 | hash = sha1.ComputeHash(data);
|
---|
227 | }
|
---|
228 | sol.BinaryData = new DA.BinaryData() {
|
---|
229 | Data = data,
|
---|
230 | Hash = hash
|
---|
231 | };
|
---|
232 | }
|
---|
233 | return sol;
|
---|
234 | }
|
---|
235 | #endregion
|
---|
236 | }
|
---|
237 | }
|
---|