[5478] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5902] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5478] | 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.Linq;
|
---|
| 24 | using System.Security.Cryptography;
|
---|
| 25 | using DA = HeuristicLab.Services.OKB.DataAccess;
|
---|
| 26 | using DT = HeuristicLab.Services.OKB.RunCreation.DataTransfer;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Services.OKB.RunCreation {
|
---|
| 29 | public static class Convert {
|
---|
| 30 | #region ToDto
|
---|
| 31 | public static DT.Algorithm ToDto(DA.Algorithm source) {
|
---|
| 32 | if (source == null) return null;
|
---|
| 33 | return new DT.Algorithm { Id = source.Id, Name = source.Name, Description = source.Description, AlgorithmClass = Convert.ToDto(source.AlgorithmClass), DataType = Convert.ToDto(source.DataType) };
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public static DT.Problem ToDto(DA.Problem source) {
|
---|
| 37 | if (source == null) return null;
|
---|
| 38 | return new DT.Problem { Id = source.Id, Name = source.Name, Description = source.Description, ProblemClass = Convert.ToDto(source.ProblemClass), DataType = Convert.ToDto(source.DataType) };
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | private static DT.DataType ToDto(DA.DataType source) {
|
---|
| 42 | if (source == null) return null;
|
---|
| 43 | return new DT.DataType { Name = source.Name, TypeName = source.TypeName };
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | private static DT.AlgorithmClass ToDto(DA.AlgorithmClass source) {
|
---|
| 47 | if (source == null) return null;
|
---|
| 48 | return new DT.AlgorithmClass { Name = source.Name, Description = source.Description };
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | private static DT.ProblemClass ToDto(DA.ProblemClass source) {
|
---|
| 52 | if (source == null) return null;
|
---|
| 53 | return new DT.ProblemClass { Name = source.Name, Description = source.Description };
|
---|
| 54 | }
|
---|
| 55 | #endregion
|
---|
| 56 |
|
---|
| 57 | #region ToEntity
|
---|
| 58 | public static DA.Run ToEntity(DT.Run source, DA.OKBDataContext okb) {
|
---|
| 59 | if (source == null) return null;
|
---|
| 60 | DA.Run entity = new DA.Run { Id = 0, AlgorithmId = source.AlgorithmId, ProblemId = source.ProblemId, RandomSeed = source.RandomSeed, CreatedDate = source.CreatedDate, UserId = source.UserId, ClientId = source.ClientId };
|
---|
| 61 | foreach (var value in source.ParameterValues)
|
---|
| 62 | entity.Values.Add(Convert.ToEntity(value, entity, DA.ValueNameCategory.Parameter, okb));
|
---|
| 63 | foreach (var value in source.ResultValues)
|
---|
| 64 | entity.Values.Add(Convert.ToEntity(value, entity, DA.ValueNameCategory.Result, okb));
|
---|
| 65 | return entity;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | private static DA.Value ToEntity(DT.Value source, DA.Run run, DA.ValueNameCategory category, DA.OKBDataContext okb) {
|
---|
| 69 | if (source == null) return null;
|
---|
| 70 | var entity = new DA.Value();
|
---|
| 71 | entity.Run = run;
|
---|
| 72 | entity.DataType = Convert.ToEntity(source.DataType, okb);
|
---|
| 73 | if (source is DT.BoolValue) {
|
---|
| 74 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Bool, okb);
|
---|
| 75 | entity.BoolValue = ((DT.BoolValue)source).Value;
|
---|
| 76 | } else if (source is DT.IntValue) {
|
---|
| 77 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Int, okb);
|
---|
| 78 | entity.IntValue = ((DT.IntValue)source).Value;
|
---|
| 79 | } else if (source is DT.LongValue) {
|
---|
| 80 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Long, okb);
|
---|
| 81 | entity.LongValue = ((DT.LongValue)source).Value;
|
---|
| 82 | } else if (source is DT.FloatValue) {
|
---|
| 83 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Float, okb);
|
---|
| 84 | entity.FloatValue = ((DT.FloatValue)source).Value;
|
---|
| 85 | } else if (source is DT.DoubleValue) {
|
---|
| 86 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Double, okb);
|
---|
| 87 | entity.DoubleValue = ((DT.DoubleValue)source).Value;
|
---|
| 88 | } else if (source is DT.StringValue) {
|
---|
| 89 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.String, okb);
|
---|
| 90 | entity.StringValue = ((DT.StringValue)source).Value;
|
---|
| 91 | } else if (source is DT.BinaryValue) {
|
---|
| 92 | entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Binary, okb);
|
---|
| 93 | entity.BinaryData = Convert.ToEntity(((DT.BinaryValue)source).Value, okb);
|
---|
| 94 | } else {
|
---|
| 95 | throw new ArgumentException("Unknown value type.", "source");
|
---|
| 96 | }
|
---|
| 97 | return entity;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | private static DA.DataType ToEntity(DT.DataType source, DA.OKBDataContext okb) {
|
---|
| 101 | if (source == null) return null;
|
---|
| 102 | var entity = okb.DataTypes.Where(x => (x.Name == source.Name) && (x.TypeName == source.TypeName)).FirstOrDefault();
|
---|
| 103 | if (entity == null)
|
---|
| 104 | entity = new DA.DataType() { Id = 0, Name = source.Name, TypeName = source.TypeName };
|
---|
| 105 | return entity;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | private static DA.ValueName ToEntity(string name, DA.ValueNameCategory category, DA.ValueNameType type, DA.OKBDataContext okb) {
|
---|
| 109 | if (string.IsNullOrEmpty(name)) return null;
|
---|
| 110 | var entity = okb.ValueNames.Where(x => (x.Name == name) && (x.Category == category) && (x.Type == type)).FirstOrDefault();
|
---|
| 111 | if (entity == null)
|
---|
| 112 | entity = new DA.ValueName() { Id = 0, Name = name, Category = category, Type = type };
|
---|
| 113 | return entity;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | private static DA.BinaryData ToEntity(byte[] data, DA.OKBDataContext okb) {
|
---|
| 117 | if (data == null) return null;
|
---|
| 118 | byte[] hash;
|
---|
| 119 | using (SHA1 sha1 = SHA1.Create()) {
|
---|
| 120 | hash = sha1.ComputeHash(data);
|
---|
| 121 | }
|
---|
| 122 | var entity = okb.BinaryDatas.Where(x => x.Hash.Equals(hash)).FirstOrDefault();
|
---|
| 123 | if (entity == null)
|
---|
[5587] | 124 | entity = new DA.BinaryData() { Id = 0, Data = data, Hash = hash };
|
---|
[5478] | 125 | return entity;
|
---|
| 126 | }
|
---|
| 127 | #endregion
|
---|
| 128 | }
|
---|
| 129 | }
|
---|