Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Services.OKB/3.3/Query/QueryService.cs @ 11617

Last change on this file since 11617 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 16.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Data.Linq;
25using System.Linq;
26using System.ServiceModel;
27using HeuristicLab.Services.Access;
28using HeuristicLab.Services.OKB.DataAccess;
29using HeuristicLab.Services.OKB.Query.DataTransfer;
30using HeuristicLab.Services.OKB.Query.Filters;
31
32namespace HeuristicLab.Services.OKB.Query {
33  /// <summary>
34  /// Implementation of the OKB query service (interface <see cref="IQueryService"/>).
35  /// </summary>
36  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
37  public class QueryService : IQueryService {
38    IRoleVerifier roleVerifier = AccessServiceLocator.Instance.RoleVerifier;
39    IUserManager userManager = AccessServiceLocator.Instance.UserManager;
40
41    public IEnumerable<Filter> GetFilters() {
42      List<Filter> filters = new List<Filter>();
43      using (OKBDataContext okb = new OKBDataContext()) {
44        #region Run Filters
45        filters.Add(new OrdinalComparisonDateTimeFilter(typeof(RunCreatedDateFilter).AssemblyQualifiedName, "Run Created Date"));
46        filters.Add(new StringComparisonFilter(typeof(RunUserNameFilter).AssemblyQualifiedName, "Run User Name"));
47        filters.Add(new StringComparisonFilter(typeof(RunClientNameFilter).AssemblyQualifiedName, "Run Client Name"));
48        #endregion
49
50        #region Parameter Filters
51        filters.Add(new StringComparisonAvailableValuesFilter(
52          typeof(ValueNameFilter).AssemblyQualifiedName,
53          "Parameter Name",
54          okb.ValueNames.Where(x => x.Category == ValueNameCategory.Parameter).Select(x => x.Name).Distinct().ToArray()
55        ));
56        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Binary)).Select(x => x.Name).Distinct())
57          filters.Add(new NameStringComparisonAvailableValuesFilter(
58            typeof(ValueDataTypeNameFilter).AssemblyQualifiedName,
59            "Parameter " + name + " Value Data Type Name",
60            name,
61            okb.Values.Where(x => (x.ValueName.Name == name) && (x.ValueName.Category == ValueNameCategory.Parameter) && (x.ValueName.Type == ValueNameType.Binary)).Select(x => x.DataType.Name).Distinct().ToArray()
62          ));
63        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Bool)).Select(x => x.Name).Distinct())
64          filters.Add(new NameEqualityComparisonBoolFilter(
65            typeof(BoolValueFilter).AssemblyQualifiedName,
66            "Parameter " + name + " Value",
67            name
68          ));
69        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Int)).Select(x => x.Name).Distinct())
70          filters.Add(new NameOrdinalComparisonIntFilter(
71            typeof(IntValueFilter).AssemblyQualifiedName,
72            "Parameter " + name + " Value",
73            name
74          ));
75        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Long)).Select(x => x.Name).Distinct())
76          filters.Add(new NameOrdinalComparisonLongFilter(
77            typeof(LongValueFilter).AssemblyQualifiedName,
78            "Parameter " + name + " Value",
79            name
80          ));
81        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.TimeSpan)).Select(x => x.Name).Distinct())
82          filters.Add(new NameOrdinalComparisonTimeSpanFilter(
83            typeof(TimeSpanValueFilter).AssemblyQualifiedName,
84            "Parameter " + name + " Value",
85            name
86          ));
87        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Float)).Select(x => x.Name).Distinct())
88          filters.Add(new NameOrdinalComparisonFloatFilter(
89            typeof(FloatValueFilter).AssemblyQualifiedName,
90            "Parameter " + name + " Value",
91            name
92          ));
93        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Double)).Select(x => x.Name).Distinct())
94          filters.Add(new NameOrdinalComparisonDoubleFilter(
95            typeof(DoubleValueFilter).AssemblyQualifiedName,
96            "Parameter " + name + " Value",
97            name
98          ));
99        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Percent)).Select(x => x.Name).Distinct())
100          filters.Add(new NameOrdinalComparisonPercentFilter(
101            typeof(PercentValueFilter).AssemblyQualifiedName,
102            "Parameter " + name + " Value",
103            name
104          ));
105        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.String)).Select(x => x.Name).Distinct())
106          filters.Add(new NameStringComparisonFilter(
107            typeof(StringValueFilter).AssemblyQualifiedName,
108            "Parameter " + name + " Value",
109            name
110          ));
111        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Binary)).Select(x => x.Name).Distinct())
112          filters.Add(new NameEqualityComparisonByteArrayFilter(
113            typeof(BinaryValueFilter).AssemblyQualifiedName,
114            "Parameter " + name + " Value",
115            name
116          ));
117        #endregion
118
119        #region Result Filters
120        filters.Add(new StringComparisonAvailableValuesFilter(
121          typeof(ValueNameFilter).AssemblyQualifiedName,
122          "Result Name",
123          okb.ValueNames.Where(x => x.Category == ValueNameCategory.Result).Select(x => x.Name).Distinct().ToArray()
124        ));
125        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Binary)).Select(x => x.Name).Distinct())
126          filters.Add(new NameStringComparisonAvailableValuesFilter(
127            typeof(ValueDataTypeNameFilter).AssemblyQualifiedName,
128            "Result " + name + " Value Data Type Name",
129            name,
130            okb.Values.Where(x => (x.ValueName.Name == name) && (x.ValueName.Category == ValueNameCategory.Result) && (x.ValueName.Type == ValueNameType.Binary)).Select(x => x.DataType.Name).Distinct().ToArray()
131          ));
132        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Bool)).Select(x => x.Name).Distinct())
133          filters.Add(new NameEqualityComparisonBoolFilter(
134            typeof(BoolValueFilter).AssemblyQualifiedName,
135            "Result " + name + " Value",
136            name
137          ));
138        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Int)).Select(x => x.Name).Distinct())
139          filters.Add(new NameOrdinalComparisonIntFilter(
140            typeof(IntValueFilter).AssemblyQualifiedName,
141            "Result " + name + " Value",
142            name
143          ));
144        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Long)).Select(x => x.Name).Distinct())
145          filters.Add(new NameOrdinalComparisonLongFilter(
146            typeof(LongValueFilter).AssemblyQualifiedName,
147            "Result " + name + " Value",
148            name
149          ));
150        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.TimeSpan)).Select(x => x.Name).Distinct())
151          filters.Add(new NameOrdinalComparisonTimeSpanFilter(
152            typeof(TimeSpanValueFilter).AssemblyQualifiedName,
153            "Result " + name + " Value",
154            name
155          ));
156        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Float)).Select(x => x.Name).Distinct())
157          filters.Add(new NameOrdinalComparisonFloatFilter(
158            typeof(FloatValueFilter).AssemblyQualifiedName,
159            "Result " + name + " Value",
160            name
161          ));
162        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Double)).Select(x => x.Name).Distinct())
163          filters.Add(new NameOrdinalComparisonDoubleFilter(
164            typeof(DoubleValueFilter).AssemblyQualifiedName,
165            "Result " + name + " Value",
166            name
167          ));
168        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Percent)).Select(x => x.Name).Distinct())
169          filters.Add(new NameOrdinalComparisonPercentFilter(
170            typeof(PercentValueFilter).AssemblyQualifiedName,
171            "Result " + name + " Value",
172            name
173          ));
174        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.String)).Select(x => x.Name).Distinct())
175          filters.Add(new NameStringComparisonFilter(
176            typeof(StringValueFilter).AssemblyQualifiedName,
177            "Result " + name + " Value",
178            name
179          ));
180        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Binary)).Select(x => x.Name).Distinct())
181          filters.Add(new NameEqualityComparisonByteArrayFilter(
182            typeof(BinaryValueFilter).AssemblyQualifiedName,
183            "Result " + name + " Value",
184            name
185          ));
186        #endregion
187
188        #region Algorithm Filters
189        filters.Add(new StringComparisonAvailableValuesFilter(
190          typeof(AlgorithmNameFilter).AssemblyQualifiedName,
191          "Algorithm Name",
192          okb.Algorithms.Select(x => x.Name).Distinct().ToArray()
193        ));
194        filters.Add(new StringComparisonAvailableValuesFilter(
195          typeof(AlgorithmClassNameFilter).AssemblyQualifiedName,
196          "Algorithm Class Name",
197          okb.AlgorithmClasses.Select(x => x.Name).Distinct().ToArray()
198        ));
199        filters.Add(new StringComparisonAvailableValuesFilter(
200          typeof(AlgorithmPlatformNameFilter).AssemblyQualifiedName,
201          "Algorithm Platform Name",
202          okb.Platforms.Select(x => x.Name).Distinct().ToArray()
203        ));
204        filters.Add(new StringComparisonAvailableValuesFilter(
205          typeof(AlgorithmDataTypeNameFilter).AssemblyQualifiedName,
206          "Algorithm Data Type Name",
207          okb.Algorithms.Select(x => x.DataType.Name).Distinct().ToArray()
208        ));
209        #endregion
210
211        #region Problem Filters
212        filters.Add(new StringComparisonAvailableValuesFilter(
213          typeof(ProblemNameFilter).AssemblyQualifiedName,
214          "Problem Name",
215          okb.Problems.Select(x => x.Name).Distinct().ToArray()
216        ));
217        filters.Add(new StringComparisonAvailableValuesFilter(
218          typeof(ProblemClassNameFilter).AssemblyQualifiedName,
219          "Problem Class Name",
220          okb.ProblemClasses.Select(x => x.Name).Distinct().ToArray()
221        ));
222        filters.Add(new StringComparisonAvailableValuesFilter(
223          typeof(ProblemPlatformNameFilter).AssemblyQualifiedName,
224          "Problem Platform Name",
225          okb.Platforms.Select(x => x.Name).Distinct().ToArray()
226        ));
227        filters.Add(new StringComparisonAvailableValuesFilter(
228          typeof(ProblemDataTypeNameFilter).AssemblyQualifiedName,
229          "Problem Data Type Name",
230          okb.Problems.Select(x => x.DataType.Name).Distinct().ToArray()
231        ));
232        #endregion
233
234        #region Combined Filters
235        filters.Add(new CombinedFilter(typeof(AndFilter).AssemblyQualifiedName, "AND", BooleanOperation.And));
236        filters.Add(new CombinedFilter(typeof(OrFilter).AssemblyQualifiedName, "OR", BooleanOperation.Or));
237        #endregion
238      }
239      return filters.OrderBy(x => x.Label);
240    }
241
242    public long GetNumberOfRuns(Filter filter) {
243      using (OKBDataContext okb = new OKBDataContext()) {
244        return FilterRuns(okb.Runs, filter, okb).LongCount();
245      }
246    }
247
248    public IEnumerable<long> GetRunIds(Filter filter) {
249      using (OKBDataContext okb = new OKBDataContext()) {
250        return FilterRuns(okb.Runs, filter, okb).Select(x => x.Id).ToArray();
251      }
252    }
253
254    public IEnumerable<DataTransfer.Run> GetRuns(IEnumerable<long> ids, bool includeBinaryValues) {
255      using (OKBDataContext okb = new OKBDataContext()) {
256        DataLoadOptions dlo = new DataLoadOptions();
257        // TODO: specify LoadWiths
258        okb.LoadOptions = dlo;
259
260        return FilterUnauthorizedRuns(okb.Runs.Where(x => ids.Contains(x.Id)).ToList(), okb).Select(x => Convert.ToDto(x, includeBinaryValues)).ToArray();
261      }
262    }
263
264    public IEnumerable<DataTransfer.Run> GetRunsWithValues(IEnumerable<long> ids, bool includeBinaryValues, IEnumerable<DataTransfer.ValueName> valueNames) {
265      using (OKBDataContext okb = new OKBDataContext()) {
266        DataLoadOptions dlo = new DataLoadOptions();
267        // TODO: specify LoadWiths
268        okb.LoadOptions = dlo;
269
270        return FilterUnauthorizedRuns(okb.Runs.Where(x => ids.Contains(x.Id)).ToList(), okb).Select(x => Convert.ToDto(x, includeBinaryValues, valueNames)).ToArray();
271      }
272    }
273
274    public IEnumerable<DataTransfer.ValueName> GetValueNames() {
275      using (OKBDataContext okb = new OKBDataContext()) {
276        return okb.ValueNames.Select(x => Convert.ToDto(x)).ToArray();
277      }
278    }
279
280    private List<DataAccess.Run> FilterRuns(IQueryable<DataAccess.Run> runs, Filter filter, OKBDataContext okb) {
281      IFilter f = (IFilter)Activator.CreateInstance(Type.GetType(filter.FilterTypeName), filter);
282
283      var query = runs.Where(f.Expression);
284      List<DataAccess.Run> results = new List<DataAccess.Run>();
285
286      if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
287        results.AddRange(query);
288      } else {
289        foreach (DataAccess.Run r in query) {
290          if (IsAuthorizedForAlgorithm(r.AlgorithmId, okb) && IsAuthorizedForProblem(r.ProblemId, okb)) {
291            results.Add(r);
292          }
293        }
294      }
295      return results;
296    }
297
298    private List<DataAccess.Run> FilterUnauthorizedRuns(List<DataAccess.Run> runs, OKBDataContext okb) {
299      List<DataAccess.Run> results = new List<DataAccess.Run>();
300
301      if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
302        results.AddRange(runs);
303      } else {
304        foreach (DataAccess.Run r in runs) {
305          if (IsAuthorizedForAlgorithm(r.AlgorithmId, okb) && IsAuthorizedForProblem(r.ProblemId, okb)) {
306            results.Add(r);
307          }
308        }
309      }
310      return results;
311    }
312
313    private bool IsAuthorizedForAlgorithm(long algorithmId, OKBDataContext okb) {
314      var algUsers = okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId).Select(x => x.UserGroupId).ToList();
315
316      if (algUsers.Count == 0 || userManager.VerifyUser(userManager.CurrentUserId, algUsers)) {
317        return true;
318      }
319      return false;
320    }
321
322    private bool IsAuthorizedForProblem(long problemId, OKBDataContext okb) {
323      var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == problemId).Select(x => x.UserGroupId).ToList();
324
325      if (problemUsers.Count == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers)) {
326        return true;
327      }
328      return false;
329    }
330  }
331}
Note: See TracBrowser for help on using the repository browser.