Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.OKB/3.3/RunCreation/RunCreationService.cs @ 13683

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

#2588:

  • Added service method to add solutions
File size: 12.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 HeuristicLab.Services.Access;
23using HeuristicLab.Services.OKB.DataAccess;
24using System;
25using System.Collections.Generic;
26using System.Data.Linq;
27using System.Linq;
28using System.ServiceModel;
29
30namespace HeuristicLab.Services.OKB.RunCreation {
31  /// <summary>
32  /// Implementation of the OKB run creation service (interface <see cref="IRunCreationService"/>).
33  /// </summary>
34  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
35  public class RunCreationService : IRunCreationService {
36    IRoleVerifier roleVerifier = AccessServiceLocator.Instance.RoleVerifier;
37    IUserManager userManager = AccessServiceLocator.Instance.UserManager;
38
39    public IEnumerable<DataTransfer.Algorithm> GetAlgorithms(string platformName) {
40      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
41
42      using (OKBDataContext okb = new OKBDataContext()) {
43        DataLoadOptions dlo = new DataLoadOptions();
44        dlo.LoadWith<Algorithm>(x => x.AlgorithmClass);
45        dlo.LoadWith<Algorithm>(x => x.DataType);
46        dlo.LoadWith<Algorithm>(x => x.AlgorithmUsers);
47        okb.LoadOptions = dlo;
48
49        var query = okb.Algorithms.Where(x => x.Platform.Name == platformName);
50        List<Algorithm> results = new List<Algorithm>();
51
52        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
53          results.AddRange(query);
54        } else {
55          foreach (var alg in query) {
56            if (alg.AlgorithmUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, alg.AlgorithmUsers.Select(y => y.UserGroupId).ToList())) {
57              results.Add(alg);
58            }
59          }
60        }
61        return results.Select(x => Convert.ToDto(x)).ToArray();
62      }
63    }
64
65    public byte[] GetAlgorithmData(long algorithmId) {
66      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
67
68      using (OKBDataContext okb = new OKBDataContext()) {
69        var result = okb.Algorithms.Where(x => x.Id == algorithmId).Select(x => x.BinaryData.Data.ToArray()).FirstOrDefault();
70
71        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
72          return result;
73        } else {
74          var algUsers = okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId);
75          if (algUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, algUsers.Select(y => y.UserGroupId).ToList())) {
76            return result;
77          } else {
78            return null;
79          }
80        }
81      }
82    }
83
84    public IEnumerable<DataTransfer.Problem> GetProblems(string platformName) {
85      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
86
87      using (OKBDataContext okb = new OKBDataContext()) {
88        DataLoadOptions dlo = new DataLoadOptions();
89        dlo.LoadWith<Problem>(x => x.ProblemClass);
90        dlo.LoadWith<Problem>(x => x.DataType);
91        dlo.LoadWith<Problem>(x => x.ProblemUsers);
92        okb.LoadOptions = dlo;
93
94        var query = okb.Problems.Where(x => x.Platform.Name == platformName);
95        List<Problem> results = new List<Problem>();
96
97        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
98          results.AddRange(query);
99        } else {
100          foreach (var problem in query) {
101            if (problem.ProblemUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, problem.ProblemUsers.Select(y => y.UserGroupId).ToList())) {
102              results.Add(problem);
103            }
104          }
105        }
106        return results.Select(x => Convert.ToDto(x)).ToArray();
107      }
108    }
109
110    public byte[] GetProblemData(long problemId) {
111      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
112
113      using (OKBDataContext okb = new OKBDataContext()) {
114        var result = okb.Problems.Where(x => x.Id == problemId).Select(x => x.BinaryData.Data.ToArray()).FirstOrDefault();
115
116        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
117          return result;
118        } else {
119          var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == problemId);
120          if (problemUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers.Select(y => y.UserGroupId).ToList())) {
121            return result;
122          } else {
123            return null;
124          }
125        }
126      }
127    }
128
129    public IEnumerable<DataTransfer.Solution> GetSolutions(long problemId) {
130      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
131
132      using (OKBDataContext okb = new OKBDataContext()) {
133        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
134        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem(problemId));
135        // TODO: In case of multi-objective problems one has to check whether it contains single- or multi-objective problems
136        var result = problem.SingleObjectiveSolutions.Select(x => Convert.ToDto(x)).ToList();
137        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
138          return result;
139        } else {
140          var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == problemId).ToList();
141          if (problemUsers.Count == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers.Select(y => y.UserGroupId).ToList())) {
142            return result;
143          } else {
144            return null;
145          }
146        }
147      }
148    }
149
150    public byte[] GetSolutionData(long solutionId) {
151      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
152
153      using (OKBDataContext okb = new OKBDataContext()) {
154        var solution = okb.SingleObjectiveSolutions.SingleOrDefault(x => x.Id == solutionId);
155        if (solution == null) throw new FaultException<MissingSolution>(new MissingSolution(solutionId));
156
157        var result = solution.BinaryData.Data.ToArray();
158        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
159          return result;
160        } else {
161          var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == solution.ProblemId).ToList();
162          if (problemUsers.Count == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers.Select(y => y.UserGroupId).ToList())) {
163            return result;
164          } else {
165            return null;
166          }
167        }
168      }
169    }
170
171    public void AddSolution(DataTransfer.Solution solution, byte[] data) {
172      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
173
174      using (OKBDataContext okb = new OKBDataContext()) {
175        var soSolution = solution as DataTransfer.SingleObjectiveSolution;
176        if (soSolution != null) {
177          DataAccess.SingleObjectiveSolution entity = Convert.ToEntity(soSolution, data, okb);
178          okb.SingleObjectiveSolutions.InsertOnSubmit(entity);
179          okb.SubmitChanges();
180        }
181      }
182    }
183
184    public void AddRun(DataTransfer.Run run) {
185      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
186
187      using (OKBDataContext okb = new OKBDataContext()) {
188        DataAccess.Run entity = Convert.ToEntity(run, okb);
189        okb.Runs.InsertOnSubmit(entity);
190        okb.SubmitChanges();
191      }
192    }
193
194    public IEnumerable<DataTransfer.Value> GetCharacteristicValues(long problemId) {
195      using (OKBDataContext okb = new OKBDataContext()) {
196        var prob = okb.Problems.SingleOrDefault(x => x.Id == problemId);
197        if (prob == null) return Enumerable.Empty<DataTransfer.Value>();
198        return prob.CharacteristicValues.Select(Convert.ToDto).ToArray();
199      }
200    }
201
202    public void SetCharacteristicValue(long problemId, DataTransfer.Value value) {
203      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
204
205      using (OKBDataContext okb = new OKBDataContext()) {
206        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
207        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem(problemId));
208
209        DoSetCharacteristicValue(okb, problem, value);
210        okb.SubmitChanges();
211      }
212    }
213
214    public void SetCharacteristicValues(long problemId, DataTransfer.Value[] values) {
215      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
216
217      using (OKBDataContext okb = new OKBDataContext()) {
218        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
219        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem(problemId));
220
221        foreach (var v in values) {
222          DoSetCharacteristicValue(okb, problem, v);
223        }
224        okb.SubmitChanges();
225      }
226    }
227
228    private void DoSetCharacteristicValue(OKBDataContext okb, Problem problem, DataTransfer.Value value) {
229      CharacteristicType characteristicType;
230      try {
231        characteristicType = GetCharacteristicType(value);
232      } catch (ArgumentException ex) {
233        throw new FaultException<UnknownCharacteristicType>(new UnknownCharacteristicType(ex.Message));
234      }
235
236      var entity = problem.CharacteristicValues.SingleOrDefault(x => x.Characteristic.Name == value.Name && x.Characteristic.Type == characteristicType);
237      if (entity != null) {
238        // Update
239        switch (characteristicType) {
240          case CharacteristicType.Bool: entity.BoolValue = ((DataTransfer.BoolValue)value).Value; break;
241          case CharacteristicType.Int: entity.IntValue = ((DataTransfer.IntValue)value).Value; break;
242          case CharacteristicType.Long: entity.LongValue = ((DataTransfer.LongValue)value).Value; break;
243          case CharacteristicType.Float: entity.FloatValue = ((DataTransfer.FloatValue)value).Value; break;
244          case CharacteristicType.Double: entity.DoubleValue = ((DataTransfer.DoubleValue)value).Value; break;
245          case CharacteristicType.Percent: entity.DoubleValue = ((DataTransfer.PercentValue)value).Value; break;
246          case CharacteristicType.String: entity.StringValue = ((DataTransfer.StringValue)value).Value; break;
247          case CharacteristicType.TimeSpan: entity.LongValue = ((DataTransfer.TimeSpanValue)value).Value; break;
248        }
249      } else {
250        // Insert
251        entity = Convert.ToEntity(value, okb, problem, characteristicType);
252        okb.CharacteristicValues.InsertOnSubmit(entity);
253      }
254
255    }
256
257    private CharacteristicType GetCharacteristicType(DataTransfer.Value source) {
258      if (source is DataTransfer.BoolValue) {
259        return CharacteristicType.Bool;
260      } else if (source is DataTransfer.IntValue) {
261        return CharacteristicType.Int;
262      } else if (source is DataTransfer.TimeSpanValue) {
263        return CharacteristicType.TimeSpan;
264      } else if (source is DataTransfer.LongValue) {
265        return CharacteristicType.Long;
266      } else if (source is DataTransfer.FloatValue) {
267        return CharacteristicType.Float;
268      } else if (source is DataTransfer.DoubleValue) {
269        return CharacteristicType.Double;
270      } else if (source is DataTransfer.PercentValue) {
271        return CharacteristicType.Percent;
272      } else if (source is DataTransfer.StringValue) {
273        return CharacteristicType.String;
274      } else {
275        throw new ArgumentException("Unknown characteristic type.", "source");
276      }
277    }
278  }
279}
Note: See TracBrowser for help on using the repository browser.