Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.OKB/3.3/RunCreation/Convert.cs @ 13682

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

#2588:

  • Added table, FK constraints, and FK indexes to database
  • Updated Linq2Sql mapping
  • Added service methods and dtos for downloading
File size: 11.1 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.Linq;
25using System.Security.Cryptography;
26using DA = HeuristicLab.Services.OKB.DataAccess;
27using DT = HeuristicLab.Services.OKB.RunCreation.DataTransfer;
28
29namespace 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      };
50    }
51
52    private static DT.DataType ToDto(DA.DataType source) {
53      if (source == null) return null;
54      return new DT.DataType { Name = source.Name, TypeName = source.TypeName };
55    }
56
57    private static DT.AlgorithmClass ToDto(DA.AlgorithmClass source) {
58      if (source == null) return null;
59      return new DT.AlgorithmClass { Name = source.Name, Description = source.Description };
60    }
61
62    private static DT.ProblemClass ToDto(DA.ProblemClass source) {
63      if (source == null) return null;
64      return new DT.ProblemClass { Name = source.Name, Description = source.Description };
65    }
66    #endregion
67
68    #region ToEntity
69    public static DA.Run ToEntity(DT.Run source, DA.OKBDataContext okb) {
70      if (source == null) return null;
71
72      List<DA.BinaryData> binCache = new List<DA.BinaryData>();
73
74      DA.Run entity = new DA.Run { Id = 0, AlgorithmId = source.AlgorithmId, ProblemId = source.ProblemId, CreatedDate = source.CreatedDate, UserId = source.UserId, ClientId = source.ClientId };
75      foreach (var value in source.ParameterValues)
76        entity.Values.Add(Convert.ToEntity(value, entity, DA.ValueNameCategory.Parameter, okb, binCache));
77      foreach (var value in source.ResultValues)
78        entity.Values.Add(Convert.ToEntity(value, entity, DA.ValueNameCategory.Result, okb, binCache));
79      return entity;
80    }
81
82    public static DT.Value ToDto(DA.CharacteristicValue source) {
83      if (source == null) return null;
84      if (source.Characteristic.Type == DA.CharacteristicType.Bool) {
85        return new DT.BoolValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.BoolValue.GetValueOrDefault() };
86      } else if (source.Characteristic.Type == DA.CharacteristicType.Int) {
87        return new DT.IntValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.IntValue.GetValueOrDefault() };
88      } else if (source.Characteristic.Type == DA.CharacteristicType.TimeSpan) {
89        return new DT.TimeSpanValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.LongValue.GetValueOrDefault() };
90      } else if (source.Characteristic.Type == DA.CharacteristicType.Long) {
91        return new DT.LongValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.LongValue.GetValueOrDefault() };
92      } else if (source.Characteristic.Type == DA.CharacteristicType.Float) {
93        return new DT.FloatValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.FloatValue.GetValueOrDefault() };
94      } else if (source.Characteristic.Type == DA.CharacteristicType.Double) {
95        return new DT.DoubleValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.DoubleValue.GetValueOrDefault() };
96      } else if (source.Characteristic.Type == DA.CharacteristicType.Percent) {
97        return new DT.PercentValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.DoubleValue.GetValueOrDefault() };
98      } else if (source.Characteristic.Type == DA.CharacteristicType.String) {
99        return new DT.StringValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.StringValue };
100      } else {
101        throw new ArgumentException("Unknown characteristic type.", "source");
102      }
103    }
104
105    public static DA.CharacteristicValue ToEntity(DT.Value source, DA.OKBDataContext okb, DA.Problem problem, DA.CharacteristicType type) {
106      if (okb == null || problem == null || source == null || string.IsNullOrEmpty(source.Name)) throw new ArgumentNullException();
107      var entity = new DA.CharacteristicValue();
108      entity.Problem = problem;
109      entity.DataType = Convert.ToEntity(source.DataType, okb);
110      entity.Characteristic = Convert.ToEntity(source.Name, type, okb);
111      if (source is DT.BoolValue) {
112        entity.BoolValue = ((DT.BoolValue)source).Value;
113      } else if (source is DT.IntValue) {
114        entity.IntValue = ((DT.IntValue)source).Value;
115      } else if (source is DT.TimeSpanValue) {
116        entity.LongValue = ((DT.TimeSpanValue)source).Value;
117      } else if (source is DT.LongValue) {
118        entity.LongValue = ((DT.LongValue)source).Value;
119      } else if (source is DT.FloatValue) {
120        entity.FloatValue = ((DT.FloatValue)source).Value;
121      } else if (source is DT.DoubleValue) {
122        entity.DoubleValue = ((DT.DoubleValue)source).Value;
123      } else if (source is DT.PercentValue) {
124        entity.DoubleValue = ((DT.PercentValue)source).Value;
125      } else if (source is DT.StringValue) {
126        entity.StringValue = ((DT.StringValue)source).Value;
127      } else {
128        throw new ArgumentException("Unknown characteristic type.", "source");
129      }
130      return entity;
131    }
132
133    private static DA.Characteristic ToEntity(string name, DA.CharacteristicType type, DA.OKBDataContext okb) {
134      if (string.IsNullOrEmpty(name)) return null;
135      var entity = okb.Characteristics.FirstOrDefault(x => (x.Name == name) && (x.Type == type));
136      return entity ?? new DA.Characteristic() { Id = 0, Name = name, Type = type };
137    }
138
139    private static DA.Value ToEntity(DT.Value source, DA.Run run, DA.ValueNameCategory category, DA.OKBDataContext okb, List<DA.BinaryData> binCache) {
140      if (source == null) return null;
141      var entity = new DA.Value();
142      entity.Run = run;
143      entity.DataType = Convert.ToEntity(source.DataType, okb);
144      if (source is DT.BoolValue) {
145        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Bool, okb);
146        entity.BoolValue = ((DT.BoolValue)source).Value;
147      } else if (source is DT.IntValue) {
148        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Int, okb);
149        entity.IntValue = ((DT.IntValue)source).Value;
150      } else if (source is DT.TimeSpanValue) {
151        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.TimeSpan, okb);
152        entity.LongValue = ((DT.TimeSpanValue)source).Value;
153      } else if (source is DT.LongValue) {
154        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Long, okb);
155        entity.LongValue = ((DT.LongValue)source).Value;
156      } else if (source is DT.FloatValue) {
157        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Float, okb);
158        entity.FloatValue = ((DT.FloatValue)source).Value;
159      } else if (source is DT.DoubleValue) {
160        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Double, okb);
161        entity.DoubleValue = ((DT.DoubleValue)source).Value;
162      } else if (source is DT.PercentValue) {
163        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Percent, okb);
164        entity.DoubleValue = ((DT.PercentValue)source).Value;
165      } else if (source is DT.StringValue) {
166        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.String, okb);
167        entity.StringValue = ((DT.StringValue)source).Value;
168      } else if (source is DT.BinaryValue) {
169        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Binary, okb);
170        entity.BinaryData = Convert.ToEntity(((DT.BinaryValue)source).Value, okb, binCache);
171      } else {
172        throw new ArgumentException("Unknown value type.", "source");
173      }
174      return entity;
175    }
176
177    private static DA.DataType ToEntity(DT.DataType source, DA.OKBDataContext okb) {
178      if (source == null) return null;
179      var entity = okb.DataTypes.Where(x => (x.Name == source.Name) && (x.TypeName == source.TypeName)).FirstOrDefault();
180      if (entity == null)
181        entity = new DA.DataType() { Id = 0, Name = source.Name, TypeName = source.TypeName };
182      return entity;
183    }
184
185    private static DA.ValueName ToEntity(string name, DA.ValueNameCategory category, DA.ValueNameType type, DA.OKBDataContext okb) {
186      if (string.IsNullOrEmpty(name)) return null;
187      var entity = okb.ValueNames.Where(x => (x.Name == name) && (x.Category == category) && (x.Type == type)).FirstOrDefault();
188      if (entity == null)
189        entity = new DA.ValueName() { Id = 0, Name = name, Category = category, Type = type };
190      return entity;
191    }
192
193    private static DA.BinaryData ToEntity(byte[] data, DA.OKBDataContext okb, List<DA.BinaryData> binCache) {
194      if (data == null) return null;
195      byte[] hash;
196      using (SHA1 sha1 = SHA1.Create()) {
197        hash = sha1.ComputeHash(data);
198      }
199
200      var cachedBinaryData = binCache.Where(x => x.Hash.SequenceEqual(hash)).FirstOrDefault();
201      if (cachedBinaryData != null)
202        return cachedBinaryData;
203
204      var entity = okb.BinaryDatas.Where(x => x.Hash.Equals(hash)).FirstOrDefault();
205      if (entity == null) {
206        entity = new DA.BinaryData() { Id = 0, Data = data, Hash = hash };
207        binCache.Add(entity);
208      }
209
210      return entity;
211    }
212    #endregion
213  }
214}
Note: See TracBrowser for help on using the repository browser.