source: trunk/sources/HeuristicLab.Services.OKB/3.3/Query/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: 7.9 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 DA = HeuristicLab.Services.OKB.DataAccess;
26using DT = HeuristicLab.Services.OKB.Query.DataTransfer;
27
28namespace HeuristicLab.Services.OKB.Query {
29  public static class Convert {
30    public static DT.Run ToDto(DA.Run source, bool includeBinaryValues) {
31      if (source == null) return null;
32      var dto = new DT.Run { Id = source.Id, Algorithm = Convert.ToDto(source.Algorithm), Problem = Convert.ToDto(source.Problem), CreatedDate = source.CreatedDate, ClientId = source.ClientId, UserId = source.UserId };
33      dto.ParameterValues = source.Values.Where(x => x.ValueName.Category == DA.ValueNameCategory.Parameter).Select(x => Convert.ToDto(x, includeBinaryValues)).ToArray();
34      dto.ResultValues = source.Values.Where(x => x.ValueName.Category == DA.ValueNameCategory.Result).Select(x => Convert.ToDto(x, includeBinaryValues)).ToArray();
35      return dto;
36    }
37
38    public static DT.Run ToDto(DA.Run source, bool includeBinaryValues, IEnumerable<DT.ValueName> valueNames) {
39      if (source == null) return null;
40      var dto = new DT.Run { Id = source.Id, Algorithm = Convert.ToDto(source.Algorithm), Problem = Convert.ToDto(source.Problem), CreatedDate = source.CreatedDate, ClientId = source.ClientId, UserId = source.UserId };
41      dto.ParameterValues = source.Values.Where(x => x.ValueName.Category == DA.ValueNameCategory.Parameter && valueNames.Count(y => y.Name == x.ValueName.Name) > 0).Select(x => Convert.ToDto(x, includeBinaryValues)).ToArray();
42      dto.ResultValues = source.Values.Where(x => x.ValueName.Category == DA.ValueNameCategory.Result && valueNames.Count(y => y.Name == x.ValueName.Name) > 0).Select(x => Convert.ToDto(x, includeBinaryValues)).ToArray();
43      return dto;
44    }
45
46    private static DT.Algorithm ToDto(DA.Algorithm source) {
47      if (source == null) return null;
48      return new DT.Algorithm { Id = source.Id, Name = source.Name, Description = source.Description, AlgorithmClass = source.AlgorithmClass.Name, Platform = source.Platform.Name, DataType = Convert.ToDto(source.DataType) };
49    }
50
51    private static DT.Problem ToDto(DA.Problem source) {
52      if (source == null) return null;
53      return new DT.Problem { Name = source.Name, Description = source.Description, ProblemClass = source.ProblemClass.Name, Platform = source.Platform.Name, DataType = Convert.ToDto(source.DataType) };
54    }
55
56    private static DT.DataType ToDto(DA.DataType source) {
57      if (source == null) return null;
58      return new DT.DataType { Name = source.Name, TypeName = source.TypeName };
59    }
60
61    private static DT.Value ToDto(DA.Value source, bool includeBinaryValues) {
62      if (source == null) return null;
63      if (source.ValueName.Type == DA.ValueNameType.Bool) {
64        return new DT.BoolValue { Name = source.ValueName.Name, DataType = Convert.ToDto(source.DataType), Value = source.BoolValue.GetValueOrDefault() };
65      } else if (source.ValueName.Type == DA.ValueNameType.Int) {
66        return new DT.IntValue { Name = source.ValueName.Name, DataType = Convert.ToDto(source.DataType), Value = source.IntValue.GetValueOrDefault() };
67      } else if (source.ValueName.Type == DA.ValueNameType.TimeSpan) {
68        return new DT.TimeSpanValue { Name = source.ValueName.Name, DataType = Convert.ToDto(source.DataType), Value = source.LongValue.GetValueOrDefault() };
69      } else if (source.ValueName.Type == DA.ValueNameType.Long) {
70        return new DT.LongValue { Name = source.ValueName.Name, DataType = Convert.ToDto(source.DataType), Value = source.LongValue.GetValueOrDefault() };
71      } else if (source.ValueName.Type == DA.ValueNameType.Float) {
72        return new DT.FloatValue { Name = source.ValueName.Name, DataType = Convert.ToDto(source.DataType), Value = source.FloatValue.GetValueOrDefault() };
73      } else if (source.ValueName.Type == DA.ValueNameType.Double) {
74        return new DT.DoubleValue { Name = source.ValueName.Name, DataType = Convert.ToDto(source.DataType), Value = source.DoubleValue.GetValueOrDefault() };
75      } else if (source.ValueName.Type == DA.ValueNameType.Percent) {
76        return new DT.PercentValue { Name = source.ValueName.Name, DataType = Convert.ToDto(source.DataType), Value = source.DoubleValue.GetValueOrDefault() };
77      } else if (source.ValueName.Type == DA.ValueNameType.String) {
78        return new DT.StringValue { Name = source.ValueName.Name, DataType = Convert.ToDto(source.DataType), Value = source.StringValue };
79      } else if (source.ValueName.Type == DA.ValueNameType.Binary) {
80        return new DT.BinaryValue { Name = source.ValueName.Name, DataType = Convert.ToDto(source.DataType), Value = includeBinaryValues ? source.BinaryData.Data.ToArray() : null };
81      } else {
82        throw new ArgumentException("Unknown value type.", "source");
83      }
84    }
85
86    public static DT.ValueName ToDto(DA.ValueName source) {
87      if (source == null) return null;
88      return new DT.ValueName() { Id = source.Id, Category = source.Category, Name = source.Name };
89    }
90
91    public static DT.Value ToDto(DA.CharacteristicValue source) {
92      if (source == null) return null;
93      if (source.Characteristic.Type == DA.CharacteristicType.Bool) {
94        return new DT.BoolValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.BoolValue.GetValueOrDefault() };
95      } else if (source.Characteristic.Type == DA.CharacteristicType.Int) {
96        return new DT.IntValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.IntValue.GetValueOrDefault() };
97      } else if (source.Characteristic.Type == DA.CharacteristicType.TimeSpan) {
98        return new DT.TimeSpanValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.LongValue.GetValueOrDefault() };
99      } else if (source.Characteristic.Type == DA.CharacteristicType.Long) {
100        return new DT.LongValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.LongValue.GetValueOrDefault() };
101      } else if (source.Characteristic.Type == DA.CharacteristicType.Float) {
102        return new DT.FloatValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.FloatValue.GetValueOrDefault() };
103      } else if (source.Characteristic.Type == DA.CharacteristicType.Double) {
104        return new DT.DoubleValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.DoubleValue.GetValueOrDefault() };
105      } else if (source.Characteristic.Type == DA.CharacteristicType.Percent) {
106        return new DT.PercentValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.DoubleValue.GetValueOrDefault() };
107      } else if (source.Characteristic.Type == DA.CharacteristicType.String) {
108        return new DT.StringValue { Name = source.Characteristic.Name, DataType = Convert.ToDto(source.DataType), Value = source.StringValue };
109      } else {
110        throw new ArgumentException("Unknown characteristic type.", "source");
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.