Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5461 was 5304, checked in by swagner, 14 years ago

Worked on OKB (#1174)

File size: 18.2 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.DataTransfer;
29
30namespace HeuristicLab.Services.OKB {
31  /// <summary>
32  /// Implementation of the OKB query service (interface <see cref="IQueryService"/>).
33  /// </summary>
34  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
35  public class QueryService : IQueryService {
36    public IEnumerable<Filter> GetFilters() {
37      List<Filter> filters = new List<Filter>();
38      using (OKBDataContext okb = new OKBDataContext()) {
39        // run filters
40        filters.Add(new OrdinalComparisonLongFilter(typeof(RunRandomSeedFilter).AssemblyQualifiedName, "Run Random Seed"));
41        filters.Add(new OrdinalComparisonDateTimeFilter(typeof(RunCreatedDateFilter).AssemblyQualifiedName, "Run Created Date"));
42        filters.Add(new SetComparisonStringFilter(typeof(RunUserNameFilter).AssemblyQualifiedName, "Run User Name"));
43        filters.Add(new SetComparisonStringFilter(typeof(RunClientNameFilter).AssemblyQualifiedName, "Run Client Name"));
44
45        // result filters
46        filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(ResultNameFilter).AssemblyQualifiedName, "Result Name", okb.Results.Select(x => x.Name).Distinct().ToArray()));
47        foreach (string name in okb.Results.Where(x => x.DataType.SqlName == "varbinary").Select(x => x.Name).Distinct())
48          filters.Add(new NameSetComparisonStringAvailableValuesFilter(typeof(ResultBlobValueDataTypeNameFilter).AssemblyQualifiedName, "Result " + name + " Value Data Type Name", name, okb.ResultBlobValues.Where(x => x.Result.Name == name).Select(x => x.DataType.Name).Distinct().ToArray()));
49        foreach (string name in okb.Results.Where(x => x.DataType.SqlName == "varbinary").Select(x => x.Name).Distinct())
50          filters.Add(new NameEqualityComparisonByteArrayFilter(typeof(ResultBlobValueValueFilter).AssemblyQualifiedName, "Result " + name + " Value", name));
51        foreach (string name in okb.Results.Where(x => x.DataType.SqlName == "bit").Select(x => x.Name).Distinct())
52          filters.Add(new NameEqualityComparisonBoolFilter(typeof(ResultBoolValueValueFilter).AssemblyQualifiedName, "Result " + name + " Value", name));
53        foreach (string name in okb.Results.Where(x => x.DataType.SqlName == "float").Select(x => x.Name).Distinct())
54          filters.Add(new NameOrdinalComparisonDoubleFilter(typeof(ResultFloatValueValueFilter).AssemblyQualifiedName, "Result " + name + " Value", name));
55        foreach (string name in okb.Results.Where(x => x.DataType.SqlName == "bigint").Select(x => x.Name).Distinct())
56          filters.Add(new NameOrdinalComparisonLongFilter(typeof(ResultIntValueValueFilter).AssemblyQualifiedName, "Result " + name + " Value", name));
57        foreach (string name in okb.Results.Where(x => x.DataType.SqlName == "nvarchar").Select(x => x.Name).Distinct())
58          filters.Add(new NameSetComparisonStringFilter(typeof(ResultStringValueValueFilter).AssemblyQualifiedName, "Result " + name + " Value", name));
59
60        // algorithm parameter filters
61        filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(AlgorithmParameterNameFilter).AssemblyQualifiedName, "Algorithm Parameter Name", okb.AlgorithmParameters.Select(x => x.Name).Distinct().ToArray()));
62        foreach (string name in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "varbinary").Select(x => x.Name).Distinct())
63          filters.Add(new NameSetComparisonStringAvailableValuesFilter(typeof(AlgorithmParameterBlobValueDataTypeNameFilter).AssemblyQualifiedName, "Algorithm Parameter " + name + " Value Data Type Name", name, okb.AlgorithmParameterBlobValues.Where(x => x.AlgorithmParameter.Name == name).Select(x => x.DataType.Name).Distinct().ToArray()));
64        foreach (string name in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "varbinary").Select(x => x.Name).Distinct())
65          filters.Add(new NameEqualityComparisonByteArrayFilter(typeof(AlgorithmParameterBlobValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + name + " Value", name));
66        foreach (string name in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "bit").Select(x => x.Name).Distinct())
67          filters.Add(new NameEqualityComparisonBoolFilter(typeof(AlgorithmParameterBoolValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + name + " Value", name));
68        foreach (string name in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "float").Select(x => x.Name).Distinct())
69          filters.Add(new NameOrdinalComparisonDoubleFilter(typeof(AlgorithmParameterFloatValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + name + " Value", name));
70        foreach (string name in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "bigint").Select(x => x.Name).Distinct())
71          filters.Add(new NameOrdinalComparisonLongFilter(typeof(AlgorithmParameterIntValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + name + " Value", name));
72        foreach (string name in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "nvarchar").Select(x => x.Name).Distinct())
73          filters.Add(new NameSetComparisonStringFilter(typeof(AlgorithmParameterStringValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + name + " Value", name));
74
75        // algorithm filters
76        filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(AlgorithmNameFilter).AssemblyQualifiedName, "Algorithm Name", okb.Algorithms.Select(x => x.Name).Distinct().ToArray()));
77        filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(AlgorithmClassNameFilter).AssemblyQualifiedName, "Algorithm Class Name", okb.AlgorithmClasses.Select(x => x.Name).Distinct().ToArray()));
78        filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(AlgorithmPlatformNameFilter).AssemblyQualifiedName, "Algorithm Platform Name", okb.Platforms.Select(x => x.Name).Distinct().ToArray()));
79        filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(AlgorithmDataDataTypeNameFilter).AssemblyQualifiedName, "Algorithm Data Type Name", okb.Algorithms.Select(x => x.AlgorithmData.DataType.Name).Distinct().ToArray()));
80
81        // problem parameter filters
82        filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(ProblemParameterNameFilter).AssemblyQualifiedName, "Problem Parameter Name", okb.ProblemParameters.Select(x => x.Name).Distinct().ToArray()));
83        foreach (string name in okb.ProblemParameters.Where(x => x.DataType.SqlName == "varbinary").Select(x => x.Name).Distinct())
84          filters.Add(new NameSetComparisonStringAvailableValuesFilter(typeof(ProblemParameterBlobValueDataTypeNameFilter).AssemblyQualifiedName, "Problem Parameter " + name + " Value Data Type Name", name, okb.ProblemParameterBlobValues.Where(x => x.ProblemParameter.Name == name).Select(x => x.DataType.Name).Distinct().ToArray()));
85        foreach (string name in okb.ProblemParameters.Where(x => x.DataType.SqlName == "varbinary").Select(x => x.Name).Distinct())
86          filters.Add(new NameEqualityComparisonByteArrayFilter(typeof(ProblemParameterBlobValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + name + " Value", name));
87        foreach (string name in okb.ProblemParameters.Where(x => x.DataType.SqlName == "bit").Select(x => x.Name).Distinct())
88          filters.Add(new NameEqualityComparisonBoolFilter(typeof(ProblemParameterBoolValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + name + " Value", name));
89        foreach (string name in okb.ProblemParameters.Where(x => x.DataType.SqlName == "float").Select(x => x.Name).Distinct())
90          filters.Add(new NameOrdinalComparisonDoubleFilter(typeof(ProblemParameterFloatValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + name + " Value", name));
91        foreach (string name in okb.ProblemParameters.Where(x => x.DataType.SqlName == "bigint").Select(x => x.Name).Distinct())
92          filters.Add(new NameOrdinalComparisonLongFilter(typeof(ProblemParameterIntValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + name + " Value", name));
93        foreach (string name in okb.ProblemParameters.Where(x => x.DataType.SqlName == "nvarchar").Select(x => x.Name).Distinct())
94          filters.Add(new NameSetComparisonStringFilter(typeof(ProblemParameterStringValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + name + " Value", name));
95
96        // problem filters
97        filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(ProblemNameFilter).AssemblyQualifiedName, "Problem Name", okb.Problems.Select(x => x.Name).Distinct().ToArray()));
98        filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(ProblemClassNameFilter).AssemblyQualifiedName, "Problem Class Name", okb.ProblemClasses.Select(x => x.Name).Distinct().ToArray()));
99        filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(ProblemPlatformNameFilter).AssemblyQualifiedName, "Problem Platform Name", okb.Platforms.Select(x => x.Name).Distinct().ToArray()));
100        filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(ProblemDataDataTypeNameFilter).AssemblyQualifiedName, "Problem Data Type Name", okb.Problems.Select(x => x.ProblemData.DataType.Name).Distinct().ToArray()));
101
102        // and/or filters
103        filters.Add(new CombinedFilter(typeof(AndFilter).AssemblyQualifiedName, "AND", BooleanOperation.And));
104        filters.Add(new CombinedFilter(typeof(OrFilter).AssemblyQualifiedName, "OR", BooleanOperation.Or));
105      }
106      return filters.OrderBy(x => x.Label);
107    }
108
109    public long GetNumberOfQueryResults(Filter filter) {
110      using (OKBDataContext okb = new OKBDataContext()) {
111        return FilterRuns(okb.Runs, filter).LongCount();
112      }
113    }
114
115    public IEnumerable<long> GetQueryResultIds(Filter filter) {
116      using (OKBDataContext okb = new OKBDataContext()) {
117        return FilterRuns(okb.Runs, filter).Select(x => x.Id).ToArray();
118      }
119    }
120
121    public IEnumerable<QueryResult> GetQueryResults(IEnumerable<long> ids) {
122      using (OKBDataContext okb = new OKBDataContext()) {
123        DataLoadOptions dlo = new DataLoadOptions();
124        dlo.LoadWith<DataAccess.Run>(x => x.ResultBlobValues);
125        dlo.LoadWith<DataAccess.Run>(x => x.ResultBoolValues);
126        dlo.LoadWith<DataAccess.Run>(x => x.ResultFloatValues);
127        dlo.LoadWith<DataAccess.Run>(x => x.ResultIntValues);
128        dlo.LoadWith<DataAccess.Run>(x => x.ResultStringValues);
129        dlo.LoadWith<DataAccess.Run>(x => x.Experiment);
130        dlo.LoadWith<DataAccess.ResultBlobValue>(x => x.Result);
131        dlo.LoadWith<DataAccess.ResultBlobValue>(x => x.DataType);
132        dlo.LoadWith<DataAccess.ResultBoolValue>(x => x.Result);
133        dlo.LoadWith<DataAccess.ResultFloatValue>(x => x.Result);
134        dlo.LoadWith<DataAccess.ResultIntValue>(x => x.Result);
135        dlo.LoadWith<DataAccess.ResultStringValue>(x => x.Result);
136        dlo.LoadWith<DataAccess.Experiment>(x => x.AlgorithmParameterBlobValues);
137        dlo.LoadWith<DataAccess.Experiment>(x => x.AlgorithmParameterBoolValues);
138        dlo.LoadWith<DataAccess.Experiment>(x => x.AlgorithmParameterFloatValues);
139        dlo.LoadWith<DataAccess.Experiment>(x => x.AlgorithmParameterIntValues);
140        dlo.LoadWith<DataAccess.Experiment>(x => x.AlgorithmParameterStringValues);
141        dlo.LoadWith<DataAccess.Experiment>(x => x.ProblemParameterBlobValues);
142        dlo.LoadWith<DataAccess.Experiment>(x => x.ProblemParameterBoolValues);
143        dlo.LoadWith<DataAccess.Experiment>(x => x.ProblemParameterFloatValues);
144        dlo.LoadWith<DataAccess.Experiment>(x => x.ProblemParameterIntValues);
145        dlo.LoadWith<DataAccess.Experiment>(x => x.ProblemParameterStringValues);
146        //dlo.LoadWith<DataAccess.Experiment>(x => x.Algorithm);
147        //dlo.LoadWith<DataAccess.Experiment>(x => x.Problem);
148        dlo.LoadWith<DataAccess.AlgorithmParameterBlobValue>(x => x.AlgorithmParameter);
149        dlo.LoadWith<DataAccess.AlgorithmParameterBlobValue>(x => x.DataType);
150        dlo.LoadWith<DataAccess.AlgorithmParameterBoolValue>(x => x.AlgorithmParameter);
151        dlo.LoadWith<DataAccess.AlgorithmParameterFloatValue>(x => x.AlgorithmParameter);
152        dlo.LoadWith<DataAccess.AlgorithmParameterIntValue>(x => x.AlgorithmParameter);
153        dlo.LoadWith<DataAccess.AlgorithmParameterStringValue>(x => x.AlgorithmParameter);
154        dlo.LoadWith<DataAccess.ProblemParameterBlobValue>(x => x.ProblemParameter);
155        dlo.LoadWith<DataAccess.ProblemParameterBlobValue>(x => x.DataType);
156        dlo.LoadWith<DataAccess.ProblemParameterBoolValue>(x => x.ProblemParameter);
157        dlo.LoadWith<DataAccess.ProblemParameterFloatValue>(x => x.ProblemParameter);
158        dlo.LoadWith<DataAccess.ProblemParameterIntValue>(x => x.ProblemParameter);
159        dlo.LoadWith<DataAccess.ProblemParameterStringValue>(x => x.ProblemParameter);
160        //dlo.LoadWith<DataAccess.Algorithm>(x => x.AlgorithmParameters);
161        //dlo.LoadWith<DataAccess.Algorithm>(x => x.Results);
162        //dlo.LoadWith<DataAccess.Algorithm>(x => x.AlgorithmClass);
163        //dlo.LoadWith<DataAccess.Algorithm>(x => x.Platform);
164        //dlo.LoadWith<DataAccess.Problem>(x => x.ProblemParameters);
165        //dlo.LoadWith<DataAccess.Problem>(x => x.ProblemClass);
166        okb.LoadOptions = dlo;
167
168        return okb.Runs.Where(x => ids.Contains(x.Id)).Select(x => ConvertToQueryResult(x)).ToArray();
169      }
170    }
171
172    private IQueryable<DataAccess.Run> FilterRuns(IQueryable<DataAccess.Run> runs, Filter filter) {
173      IFilter f = (IFilter)Activator.CreateInstance(Type.GetType(filter.FilterTypeName), filter);
174      return runs.Where(f.Expression);
175    }
176
177    private QueryResult ConvertToQueryResult(DataAccess.Run run) {
178      List<QueryValue> algorithmParameters = new List<QueryValue>();
179      List<QueryValue> problemParameters = new List<QueryValue>();
180      List<QueryValue> results = new List<QueryValue>();
181
182      foreach (DataAccess.AlgorithmParameterBlobValue value in run.Experiment.AlgorithmParameterBlobValues)
183        algorithmParameters.Add(new QueryBlobValue { Name = value.AlgorithmParameter.Name, DataTypeName = value.DataType.Name + " (" + value.DataType.TypeName + ")", Value = value.Value.ToArray() });
184      foreach (DataAccess.AlgorithmParameterBoolValue value in run.Experiment.AlgorithmParameterBoolValues)
185        algorithmParameters.Add(new QueryBoolValue { Name = value.AlgorithmParameter.Name, Value = value.Value });
186      foreach (DataAccess.AlgorithmParameterFloatValue value in run.Experiment.AlgorithmParameterFloatValues)
187        algorithmParameters.Add(new QueryFloatValue { Name = value.AlgorithmParameter.Name, Value = value.Value });
188      foreach (DataAccess.AlgorithmParameterIntValue value in run.Experiment.AlgorithmParameterIntValues)
189        algorithmParameters.Add(new QueryIntValue { Name = value.AlgorithmParameter.Name, Value = value.Value });
190      foreach (DataAccess.AlgorithmParameterStringValue value in run.Experiment.AlgorithmParameterStringValues)
191        algorithmParameters.Add(new QueryStringValue { Name = value.AlgorithmParameter.Name, Value = value.Value });
192
193      foreach (DataAccess.ProblemParameterBlobValue value in run.Experiment.ProblemParameterBlobValues)
194        problemParameters.Add(new QueryBlobValue { Name = value.ProblemParameter.Name, DataTypeName = value.DataType.Name + " (" + value.DataType.TypeName + ")", Value = value.Value.ToArray() });
195      foreach (DataAccess.ProblemParameterBoolValue value in run.Experiment.ProblemParameterBoolValues)
196        problemParameters.Add(new QueryBoolValue { Name = value.ProblemParameter.Name, Value = value.Value });
197      foreach (DataAccess.ProblemParameterFloatValue value in run.Experiment.ProblemParameterFloatValues)
198        problemParameters.Add(new QueryFloatValue { Name = value.ProblemParameter.Name, Value = value.Value });
199      foreach (DataAccess.ProblemParameterIntValue value in run.Experiment.ProblemParameterIntValues)
200        problemParameters.Add(new QueryIntValue { Name = value.ProblemParameter.Name, Value = value.Value });
201      foreach (DataAccess.ProblemParameterStringValue value in run.Experiment.ProblemParameterStringValues)
202        problemParameters.Add(new QueryStringValue { Name = value.ProblemParameter.Name, Value = value.Value });
203
204      foreach (DataAccess.ResultBlobValue value in run.ResultBlobValues)
205        results.Add(new QueryBlobValue { Name = value.Result.Name, DataTypeName = value.DataType.Name + " (" + value.DataType.TypeName + ")", Value = value.Value.ToArray() });
206      foreach (DataAccess.ResultBoolValue value in run.ResultBoolValues)
207        results.Add(new QueryBoolValue { Name = value.Result.Name, Value = value.Value });
208      foreach (DataAccess.ResultFloatValue value in run.ResultFloatValues)
209        results.Add(new QueryFloatValue { Name = value.Result.Name, Value = value.Value });
210      foreach (DataAccess.ResultIntValue value in run.ResultIntValues)
211        results.Add(new QueryIntValue { Name = value.Result.Name, Value = value.Value });
212      foreach (DataAccess.ResultStringValue value in run.ResultStringValues)
213        results.Add(new QueryStringValue { Name = value.Result.Name, Value = value.Value });
214
215      QueryResult result = new QueryResult();
216      result.Id = run.Id;
217      result.AlgorithmParameters = algorithmParameters;
218      result.ProblemParameters = problemParameters;
219      result.Results = results;
220      return result;
221    }
222  }
223}
Note: See TracBrowser for help on using the repository browser.