Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Services.OKB/3.3/RunCreation/RunCreationService.cs @ 15082

Last change on this file since 15082 was 15082, checked in by gkronber, 7 years ago

#2588 merged r13682, r13683, r13684, r13690:13693, r13709, r13746 from trunk to stable

File size: 13.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 DataTransfer.Solution GetSolution(long solutionId) {
151      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
152
153      using (OKBDataContext okb = new OKBDataContext()) {
154        // TODO: In case of multi-objective problems one has to check whether it contains single- or multi-objective problems
155        var result = Convert.ToDto(okb.SingleObjectiveSolutions.SingleOrDefault(x => x.Id == solutionId));
156        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
157          return result;
158        } else {
159          var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == result.ProblemId).ToList();
160          if (problemUsers.Count == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers.Select(y => y.UserGroupId).ToList())) {
161            return result;
162          } else {
163            return null;
164          }
165        }
166      }
167    }
168
169    public byte[] GetSolutionData(long solutionId) {
170      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
171
172      using (OKBDataContext okb = new OKBDataContext()) {
173        var solution = okb.SingleObjectiveSolutions.SingleOrDefault(x => x.Id == solutionId);
174        if (solution == null) throw new FaultException<MissingSolution>(new MissingSolution(solutionId));
175
176        var result = solution.BinaryData.Data.ToArray();
177        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
178          return result;
179        } else {
180          var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == solution.ProblemId).ToList();
181          if (problemUsers.Count == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers.Select(y => y.UserGroupId).ToList())) {
182            return result;
183          } else {
184            return null;
185          }
186        }
187      }
188    }
189
190    public long AddSolution(DataTransfer.Solution solution, byte[] data) {
191      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
192
193      using (OKBDataContext okb = new OKBDataContext()) {
194        var soSolution = solution as DataTransfer.SingleObjectiveSolution;
195        if (soSolution != null) {
196          DataAccess.SingleObjectiveSolution entity = Convert.ToEntity(soSolution, data, okb);
197          okb.SingleObjectiveSolutions.InsertOnSubmit(entity);
198          okb.SubmitChanges();
199          return entity.Id;
200        }
201      }
202      throw new FaultException(new FaultReason("The solution could not be added."));
203    }
204
205    public void DeleteSolution(DataTransfer.Solution solution) {
206      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
207
208      using (OKBDataContext okb = new OKBDataContext()) {
209        var soSolution = solution as DataTransfer.SingleObjectiveSolution;
210        if (soSolution != null) {
211          okb.SingleObjectiveSolutions.DeleteOnSubmit(okb.SingleObjectiveSolutions.Single(x => x.Id == soSolution.Id));
212          okb.SubmitChanges();
213        }
214      }
215    }
216
217    public void AddRun(DataTransfer.Run run) {
218      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
219
220      using (OKBDataContext okb = new OKBDataContext()) {
221        DataAccess.Run entity = Convert.ToEntity(run, okb);
222        okb.Runs.InsertOnSubmit(entity);
223        okb.SubmitChanges();
224      }
225    }
226
227    public IEnumerable<DataTransfer.Value> GetCharacteristicValues(long problemId) {
228      using (OKBDataContext okb = new OKBDataContext()) {
229        var prob = okb.Problems.SingleOrDefault(x => x.Id == problemId);
230        if (prob == null) return Enumerable.Empty<DataTransfer.Value>();
231        return prob.CharacteristicValues.Select(Convert.ToDto).ToArray();
232      }
233    }
234
235    public void SetCharacteristicValue(long problemId, DataTransfer.Value value) {
236      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
237
238      using (OKBDataContext okb = new OKBDataContext()) {
239        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
240        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem(problemId));
241
242        DoSetCharacteristicValue(okb, problem, value);
243        okb.SubmitChanges();
244      }
245    }
246
247    public void SetCharacteristicValues(long problemId, DataTransfer.Value[] values) {
248      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
249
250      using (OKBDataContext okb = new OKBDataContext()) {
251        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
252        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem(problemId));
253
254        foreach (var v in values) {
255          DoSetCharacteristicValue(okb, problem, v);
256        }
257        okb.SubmitChanges();
258      }
259    }
260
261    private void DoSetCharacteristicValue(OKBDataContext okb, Problem problem, DataTransfer.Value value) {
262      CharacteristicType characteristicType;
263      try {
264        characteristicType = GetCharacteristicType(value);
265      } catch (ArgumentException ex) {
266        throw new FaultException<UnknownCharacteristicType>(new UnknownCharacteristicType(ex.Message));
267      }
268
269      var entity = problem.CharacteristicValues.SingleOrDefault(x => x.Characteristic.Name == value.Name && x.Characteristic.Type == characteristicType);
270      if (entity != null) {
271        // Update
272        switch (characteristicType) {
273          case CharacteristicType.Bool: entity.BoolValue = ((DataTransfer.BoolValue)value).Value; break;
274          case CharacteristicType.Int: entity.IntValue = ((DataTransfer.IntValue)value).Value; break;
275          case CharacteristicType.Long: entity.LongValue = ((DataTransfer.LongValue)value).Value; break;
276          case CharacteristicType.Float: entity.FloatValue = ((DataTransfer.FloatValue)value).Value; break;
277          case CharacteristicType.Double: entity.DoubleValue = ((DataTransfer.DoubleValue)value).Value; break;
278          case CharacteristicType.Percent: entity.DoubleValue = ((DataTransfer.PercentValue)value).Value; break;
279          case CharacteristicType.String: entity.StringValue = ((DataTransfer.StringValue)value).Value; break;
280          case CharacteristicType.TimeSpan: entity.LongValue = ((DataTransfer.TimeSpanValue)value).Value; break;
281        }
282      } else {
283        // Insert
284        entity = Convert.ToEntity(value, okb, problem, characteristicType);
285        okb.CharacteristicValues.InsertOnSubmit(entity);
286      }
287
288    }
289
290    private CharacteristicType GetCharacteristicType(DataTransfer.Value source) {
291      if (source is DataTransfer.BoolValue) {
292        return CharacteristicType.Bool;
293      } else if (source is DataTransfer.IntValue) {
294        return CharacteristicType.Int;
295      } else if (source is DataTransfer.TimeSpanValue) {
296        return CharacteristicType.TimeSpan;
297      } else if (source is DataTransfer.LongValue) {
298        return CharacteristicType.Long;
299      } else if (source is DataTransfer.FloatValue) {
300        return CharacteristicType.Float;
301      } else if (source is DataTransfer.DoubleValue) {
302        return CharacteristicType.Double;
303      } else if (source is DataTransfer.PercentValue) {
304        return CharacteristicType.Percent;
305      } else if (source is DataTransfer.StringValue) {
306        return CharacteristicType.String;
307      } else {
308        throw new ArgumentException("Unknown characteristic type.", "source");
309      }
310    }
311  }
312}
Note: See TracBrowser for help on using the repository browser.