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

Last change on this file since 13501 was 13501, checked in by abeham, 5 years ago

#2560: added service methods

File size: 8.7 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 DA.CharacteristicValue ToEntity(DT.Value source, DA.OKBDataContext okb, DA.Problem problem, string characteristicName, DA.CharacteristicType type) {
73      if (okb == null || problem == null || string.IsNullOrEmpty(characteristicName) || source == null) throw new ArgumentNullException();
74      var entity = new DA.CharacteristicValue();
75      entity.Problem = problem;
76      entity.DataType = Convert.ToEntity(source.DataType, okb);
77      entity.Characteristic = Convert.ToEntity(characteristicName, type, okb);
78      if (source is DT.BoolValue) {
79        entity.BoolValue = ((DT.BoolValue)source).Value;
80      } else if (source is DT.IntValue) {
81        entity.IntValue = ((DT.IntValue)source).Value;
82      } else if (source is DT.TimeSpanValue) {
83        entity.LongValue = ((DT.TimeSpanValue)source).Value;
84      } else if (source is DT.LongValue) {
85        entity.LongValue = ((DT.LongValue)source).Value;
86      } else if (source is DT.FloatValue) {
87        entity.FloatValue = ((DT.FloatValue)source).Value;
88      } else if (source is DT.DoubleValue) {
89        entity.DoubleValue = ((DT.DoubleValue)source).Value;
90      } else if (source is DT.PercentValue) {
91        entity.DoubleValue = ((DT.PercentValue)source).Value;
92      } else if (source is DT.StringValue) {
93        entity.StringValue = ((DT.StringValue)source).Value;
94      } else {
95        throw new ArgumentException("Unknown characteristic type.", "source");
96      }
97      return entity;
98    }
99
100    private static DA.Characteristic ToEntity(string name, DA.CharacteristicType type, DA.OKBDataContext okb) {
101      if (string.IsNullOrEmpty(name)) return null;
102      var entity = okb.Characteristics.FirstOrDefault(x => (x.Name == name) && (x.Type == type));
103      return entity ?? new DA.Characteristic() { Id = 0, Name = name, Type = type };
104    }
105
106    private static DA.Value ToEntity(DT.Value source, DA.Run run, DA.ValueNameCategory category, DA.OKBDataContext okb, List<DA.BinaryData> binCache) {
107      if (source == null) return null;
108      var entity = new DA.Value();
109      entity.Run = run;
110      entity.DataType = Convert.ToEntity(source.DataType, okb);
111      if (source is DT.BoolValue) {
112        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Bool, okb);
113        entity.BoolValue = ((DT.BoolValue)source).Value;
114      } else if (source is DT.IntValue) {
115        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Int, okb);
116        entity.IntValue = ((DT.IntValue)source).Value;
117      } else if (source is DT.TimeSpanValue) {
118        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.TimeSpan, okb);
119        entity.LongValue = ((DT.TimeSpanValue)source).Value;
120      } else if (source is DT.LongValue) {
121        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Long, okb);
122        entity.LongValue = ((DT.LongValue)source).Value;
123      } else if (source is DT.FloatValue) {
124        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Float, okb);
125        entity.FloatValue = ((DT.FloatValue)source).Value;
126      } else if (source is DT.DoubleValue) {
127        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Double, okb);
128        entity.DoubleValue = ((DT.DoubleValue)source).Value;
129      } else if (source is DT.PercentValue) {
130        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Percent, okb);
131        entity.DoubleValue = ((DT.PercentValue)source).Value;
132      } else if (source is DT.StringValue) {
133        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.String, okb);
134        entity.StringValue = ((DT.StringValue)source).Value;
135      } else if (source is DT.BinaryValue) {
136        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Binary, okb);
137        entity.BinaryData = Convert.ToEntity(((DT.BinaryValue)source).Value, okb, binCache);
138      } else {
139        throw new ArgumentException("Unknown value type.", "source");
140      }
141      return entity;
142    }
143
144    private static DA.DataType ToEntity(DT.DataType source, DA.OKBDataContext okb) {
145      if (source == null) return null;
146      var entity = okb.DataTypes.Where(x => (x.Name == source.Name) && (x.TypeName == source.TypeName)).FirstOrDefault();
147      if (entity == null)
148        entity = new DA.DataType() { Id = 0, Name = source.Name, TypeName = source.TypeName };
149      return entity;
150    }
151
152    private static DA.ValueName ToEntity(string name, DA.ValueNameCategory category, DA.ValueNameType type, DA.OKBDataContext okb) {
153      if (string.IsNullOrEmpty(name)) return null;
154      var entity = okb.ValueNames.Where(x => (x.Name == name) && (x.Category == category) && (x.Type == type)).FirstOrDefault();
155      if (entity == null)
156        entity = new DA.ValueName() { Id = 0, Name = name, Category = category, Type = type };
157      return entity;
158    }
159
160    private static DA.BinaryData ToEntity(byte[] data, DA.OKBDataContext okb, List<DA.BinaryData> binCache) {
161      if (data == null) return null;
162      byte[] hash;
163      using (SHA1 sha1 = SHA1.Create()) {
164        hash = sha1.ComputeHash(data);
165      }
166
167      var cachedBinaryData = binCache.Where(x => x.Hash.SequenceEqual(hash)).FirstOrDefault();
168      if (cachedBinaryData != null)
169        return cachedBinaryData;
170
171      var entity = okb.BinaryDatas.Where(x => x.Hash.Equals(hash)).FirstOrDefault();
172      if (entity == null) {
173        entity = new DA.BinaryData() { Id = 0, Data = data, Hash = hash };
174        binCache.Add(entity);
175      }
176
177      return entity;
178    }
179    #endregion
180  }
181}
Note: See TracBrowser for help on using the repository browser.