Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13578 was 13511, checked in by abeham, 9 years ago

#2560: Moved both get and set characteristic values functionality to run creation service

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