Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Services.OKB/3.3/Query/QueryService.cs @ 5656

Last change on this file since 5656 was 5502, checked in by swagner, 14 years ago

Worked on OKB (#1174)

File size: 12.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.OKB.DataAccess;
28using HeuristicLab.Services.OKB.Query.DataTransfer;
29using HeuristicLab.Services.OKB.Query.Filters;
30
31namespace HeuristicLab.Services.OKB.Query {
32  /// <summary>
33  /// Implementation of the OKB query service (interface <see cref="IQueryService"/>).
34  /// </summary>
35  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
36  public class QueryService : IQueryService {
37    public IEnumerable<Filter> GetFilters() {
38      List<Filter> filters = new List<Filter>();
39      using (OKBDataContext okb = new OKBDataContext()) {
40        #region Run Filters
41        filters.Add(new OrdinalComparisonLongFilter(typeof(RunRandomSeedFilter).AssemblyQualifiedName, "Run Random Seed"));
42        filters.Add(new OrdinalComparisonDateTimeFilter(typeof(RunCreatedDateFilter).AssemblyQualifiedName, "Run Created Date"));
43        filters.Add(new StringComparisonFilter(typeof(RunUserNameFilter).AssemblyQualifiedName, "Run User Name"));
44        filters.Add(new StringComparisonFilter(typeof(RunClientNameFilter).AssemblyQualifiedName, "Run Client Name"));
45        #endregion
46
47        #region Parameter Filters
48        filters.Add(new StringComparisonAvailableValuesFilter(
49          typeof(ValueNameFilter).AssemblyQualifiedName,
50          "Parameter Name",
51          okb.ValueNames.Where(x => x.Category == ValueNameCategory.Parameter).Select(x => x.Name).Distinct().ToArray()
52        ));
53        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Binary)).Select(x => x.Name).Distinct())
54          filters.Add(new NameStringComparisonAvailableValuesFilter(
55            typeof(ValueDataTypeNameFilter).AssemblyQualifiedName,
56            "Parameter " + name + " Value Data Type Name",
57            name,
58            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()
59          ));
60        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Bool)).Select(x => x.Name).Distinct())
61          filters.Add(new NameEqualityComparisonBoolFilter(
62            typeof(BoolValueFilter).AssemblyQualifiedName,
63            "Parameter " + name + " Value",
64            name
65          ));
66        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Int)).Select(x => x.Name).Distinct())
67          filters.Add(new NameOrdinalComparisonIntFilter(
68            typeof(IntValueFilter).AssemblyQualifiedName,
69            "Parameter " + name + " Value",
70            name
71          ));
72        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Long)).Select(x => x.Name).Distinct())
73          filters.Add(new NameOrdinalComparisonLongFilter(
74            typeof(LongValueFilter).AssemblyQualifiedName,
75            "Parameter " + name + " Value",
76            name
77          ));
78        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Float)).Select(x => x.Name).Distinct())
79          filters.Add(new NameOrdinalComparisonFloatFilter(
80            typeof(FloatValueFilter).AssemblyQualifiedName,
81            "Parameter " + name + " Value",
82            name
83          ));
84        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Double)).Select(x => x.Name).Distinct())
85          filters.Add(new NameOrdinalComparisonDoubleFilter(
86            typeof(DoubleValueFilter).AssemblyQualifiedName,
87            "Parameter " + name + " Value",
88            name
89          ));
90        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.String)).Select(x => x.Name).Distinct())
91          filters.Add(new NameStringComparisonFilter(
92            typeof(StringValueFilter).AssemblyQualifiedName,
93            "Parameter " + name + " Value",
94            name
95          ));
96        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Parameter) && (x.Type == ValueNameType.Binary)).Select(x => x.Name).Distinct())
97          filters.Add(new NameEqualityComparisonByteArrayFilter(
98            typeof(BinaryValueFilter).AssemblyQualifiedName,
99            "Parameter " + name + " Value",
100            name
101          ));
102        #endregion
103
104        #region Result Filters
105        filters.Add(new StringComparisonAvailableValuesFilter(
106          typeof(ValueNameFilter).AssemblyQualifiedName,
107          "Result Name",
108          okb.ValueNames.Where(x => x.Category == ValueNameCategory.Result).Select(x => x.Name).Distinct().ToArray()
109        ));
110        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Binary)).Select(x => x.Name).Distinct())
111          filters.Add(new NameStringComparisonAvailableValuesFilter(
112            typeof(ValueDataTypeNameFilter).AssemblyQualifiedName,
113            "Result " + name + " Value Data Type Name",
114            name,
115            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()
116          ));
117        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Bool)).Select(x => x.Name).Distinct())
118          filters.Add(new NameEqualityComparisonBoolFilter(
119            typeof(BoolValueFilter).AssemblyQualifiedName,
120            "Result " + name + " Value",
121            name
122          ));
123        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Int)).Select(x => x.Name).Distinct())
124          filters.Add(new NameOrdinalComparisonIntFilter(
125            typeof(IntValueFilter).AssemblyQualifiedName,
126            "Result " + name + " Value",
127            name
128          ));
129        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Long)).Select(x => x.Name).Distinct())
130          filters.Add(new NameOrdinalComparisonLongFilter(
131            typeof(LongValueFilter).AssemblyQualifiedName,
132            "Result " + name + " Value",
133            name
134          ));
135        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Float)).Select(x => x.Name).Distinct())
136          filters.Add(new NameOrdinalComparisonFloatFilter(
137            typeof(FloatValueFilter).AssemblyQualifiedName,
138            "Result " + name + " Value",
139            name
140          ));
141        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Double)).Select(x => x.Name).Distinct())
142          filters.Add(new NameOrdinalComparisonDoubleFilter(
143            typeof(DoubleValueFilter).AssemblyQualifiedName,
144            "Result " + name + " Value",
145            name
146          ));
147        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.String)).Select(x => x.Name).Distinct())
148          filters.Add(new NameStringComparisonFilter(
149            typeof(StringValueFilter).AssemblyQualifiedName,
150            "Result " + name + " Value",
151            name
152          ));
153        foreach (string name in okb.ValueNames.Where(x => (x.Category == ValueNameCategory.Result) && (x.Type == ValueNameType.Binary)).Select(x => x.Name).Distinct())
154          filters.Add(new NameEqualityComparisonByteArrayFilter(
155            typeof(BinaryValueFilter).AssemblyQualifiedName,
156            "Result " + name + " Value",
157            name
158          ));
159        #endregion
160
161        #region Algorithm Filters
162        filters.Add(new StringComparisonAvailableValuesFilter(
163          typeof(AlgorithmNameFilter).AssemblyQualifiedName,
164          "Algorithm Name",
165          okb.Algorithms.Select(x => x.Name).Distinct().ToArray()
166        ));
167        filters.Add(new StringComparisonAvailableValuesFilter(
168          typeof(AlgorithmClassNameFilter).AssemblyQualifiedName,
169          "Algorithm Class Name",
170          okb.AlgorithmClasses.Select(x => x.Name).Distinct().ToArray()
171        ));
172        filters.Add(new StringComparisonAvailableValuesFilter(
173          typeof(AlgorithmPlatformNameFilter).AssemblyQualifiedName,
174          "Algorithm Platform Name",
175          okb.Platforms.Select(x => x.Name).Distinct().ToArray()
176        ));
177        filters.Add(new StringComparisonAvailableValuesFilter(
178          typeof(AlgorithmDataTypeNameFilter).AssemblyQualifiedName,
179          "Algorithm Data Type Name",
180          okb.Algorithms.Select(x => x.DataType.Name).Distinct().ToArray()
181        ));
182        #endregion
183
184        #region Problem Filters
185        filters.Add(new StringComparisonAvailableValuesFilter(
186          typeof(ProblemNameFilter).AssemblyQualifiedName,
187          "Problem Name",
188          okb.Problems.Select(x => x.Name).Distinct().ToArray()
189        ));
190        filters.Add(new StringComparisonAvailableValuesFilter(
191          typeof(ProblemClassNameFilter).AssemblyQualifiedName,
192          "Problem Class Name",
193          okb.ProblemClasses.Select(x => x.Name).Distinct().ToArray()
194        ));
195        filters.Add(new StringComparisonAvailableValuesFilter(
196          typeof(ProblemPlatformNameFilter).AssemblyQualifiedName,
197          "Problem Platform Name",
198          okb.Platforms.Select(x => x.Name).Distinct().ToArray()
199        ));
200        filters.Add(new StringComparisonAvailableValuesFilter(
201          typeof(ProblemDataTypeNameFilter).AssemblyQualifiedName,
202          "Problem Data Type Name",
203          okb.Problems.Select(x => x.DataType.Name).Distinct().ToArray()
204        ));
205        #endregion
206
207        #region Combined Filters
208        filters.Add(new CombinedFilter(typeof(AndFilter).AssemblyQualifiedName, "AND", BooleanOperation.And));
209        filters.Add(new CombinedFilter(typeof(OrFilter).AssemblyQualifiedName, "OR", BooleanOperation.Or));
210        #endregion
211      }
212      return filters.OrderBy(x => x.Label);
213    }
214
215    public long GetNumberOfRuns(Filter filter) {
216      using (OKBDataContext okb = new OKBDataContext()) {
217        return FilterRuns(okb.Runs, filter).LongCount();
218      }
219    }
220
221    public IEnumerable<long> GetRunIds(Filter filter) {
222      using (OKBDataContext okb = new OKBDataContext()) {
223        return FilterRuns(okb.Runs, filter).Select(x => x.Id).ToArray();
224      }
225    }
226
227    public IEnumerable<DataTransfer.Run> GetRuns(IEnumerable<long> ids, bool includeBinaryValues) {
228      using (OKBDataContext okb = new OKBDataContext()) {
229        DataLoadOptions dlo = new DataLoadOptions();
230        // TODO: specify LoadWiths
231        okb.LoadOptions = dlo;
232
233        return okb.Runs.Where(x => ids.Contains(x.Id)).Select(x => Convert.ToDto(x, includeBinaryValues)).ToArray();
234      }
235    }
236
237    private IQueryable<DataAccess.Run> FilterRuns(IQueryable<DataAccess.Run> runs, Filter filter) {
238      IFilter f = (IFilter)Activator.CreateInstance(Type.GetType(filter.FilterTypeName), filter);
239      return runs.Where(f.Expression);
240    }
241  }
242}
Note: See TracBrowser for help on using the repository browser.