#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Data.Linq; using System.Linq; using System.ServiceModel; using HeuristicLab.Services.OKB.DataAccess; using HeuristicLab.Services.OKB.DataTransfer; namespace HeuristicLab.Services.OKB { /// /// Implementation of the OKB query service (interface ). /// [ServiceBehavior(IncludeExceptionDetailInFaults = true)] public class QueryService : IQueryService { public IEnumerable GetFilters() { List filters = new List(); using (OKBDataContext okb = new OKBDataContext()) { // run filters filters.Add(new OrdinalComparisonLongFilter(typeof(RunRandomSeedFilter).AssemblyQualifiedName, "Run Random Seed")); filters.Add(new OrdinalComparisonDateTimeFilter(typeof(RunCreatedDateFilter).AssemblyQualifiedName, "Run Created Date")); filters.Add(new SetComparisonStringFilter(typeof(RunUserNameFilter).AssemblyQualifiedName, "Run User Name")); filters.Add(new SetComparisonStringFilter(typeof(RunClientNameFilter).AssemblyQualifiedName, "Run Client Name")); // result filters filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(ResultNameFilter).AssemblyQualifiedName, "Result Name", okb.Results.Select(x => x.Name).Distinct().ToArray())); foreach (string name in okb.Results.Where(x => x.DataType.SqlName == "varbinary").Select(x => x.Name).Distinct()) 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())); foreach (string name in okb.Results.Where(x => x.DataType.SqlName == "varbinary").Select(x => x.Name).Distinct()) filters.Add(new NameEqualityComparisonByteArrayFilter(typeof(ResultBlobValueValueFilter).AssemblyQualifiedName, "Result " + name + " Value", name)); foreach (string name in okb.Results.Where(x => x.DataType.SqlName == "bit").Select(x => x.Name).Distinct()) filters.Add(new NameEqualityComparisonBoolFilter(typeof(ResultBoolValueValueFilter).AssemblyQualifiedName, "Result " + name + " Value", name)); foreach (string name in okb.Results.Where(x => x.DataType.SqlName == "float").Select(x => x.Name).Distinct()) filters.Add(new NameOrdinalComparisonDoubleFilter(typeof(ResultFloatValueValueFilter).AssemblyQualifiedName, "Result " + name + " Value", name)); foreach (string name in okb.Results.Where(x => x.DataType.SqlName == "bigint").Select(x => x.Name).Distinct()) filters.Add(new NameOrdinalComparisonLongFilter(typeof(ResultIntValueValueFilter).AssemblyQualifiedName, "Result " + name + " Value", name)); foreach (string name in okb.Results.Where(x => x.DataType.SqlName == "nvarchar").Select(x => x.Name).Distinct()) filters.Add(new NameSetComparisonStringFilter(typeof(ResultStringValueValueFilter).AssemblyQualifiedName, "Result " + name + " Value", name)); // algorithm parameter filters filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(AlgorithmParameterNameFilter).AssemblyQualifiedName, "Algorithm Parameter Name", okb.AlgorithmParameters.Select(x => x.Name).Distinct().ToArray())); foreach (string name in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "varbinary").Select(x => x.Name).Distinct()) 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())); foreach (string name in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "varbinary").Select(x => x.Name).Distinct()) filters.Add(new NameEqualityComparisonByteArrayFilter(typeof(AlgorithmParameterBlobValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + name + " Value", name)); foreach (string name in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "bit").Select(x => x.Name).Distinct()) filters.Add(new NameEqualityComparisonBoolFilter(typeof(AlgorithmParameterBoolValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + name + " Value", name)); foreach (string name in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "float").Select(x => x.Name).Distinct()) filters.Add(new NameOrdinalComparisonDoubleFilter(typeof(AlgorithmParameterFloatValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + name + " Value", name)); foreach (string name in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "bigint").Select(x => x.Name).Distinct()) filters.Add(new NameOrdinalComparisonLongFilter(typeof(AlgorithmParameterIntValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + name + " Value", name)); foreach (string name in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "nvarchar").Select(x => x.Name).Distinct()) filters.Add(new NameSetComparisonStringFilter(typeof(AlgorithmParameterStringValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + name + " Value", name)); // algorithm filters filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(AlgorithmNameFilter).AssemblyQualifiedName, "Algorithm Name", okb.Algorithms.Select(x => x.Name).Distinct().ToArray())); filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(AlgorithmClassNameFilter).AssemblyQualifiedName, "Algorithm Class Name", okb.AlgorithmClasses.Select(x => x.Name).Distinct().ToArray())); filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(AlgorithmPlatformNameFilter).AssemblyQualifiedName, "Algorithm Platform Name", okb.Platforms.Select(x => x.Name).Distinct().ToArray())); filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(AlgorithmDataDataTypeNameFilter).AssemblyQualifiedName, "Algorithm Data Type Name", okb.Algorithms.Select(x => x.AlgorithmData.DataType.Name).Distinct().ToArray())); // problem parameter filters filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(ProblemParameterNameFilter).AssemblyQualifiedName, "Problem Parameter Name", okb.ProblemParameters.Select(x => x.Name).Distinct().ToArray())); foreach (string name in okb.ProblemParameters.Where(x => x.DataType.SqlName == "varbinary").Select(x => x.Name).Distinct()) 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())); foreach (string name in okb.ProblemParameters.Where(x => x.DataType.SqlName == "varbinary").Select(x => x.Name).Distinct()) filters.Add(new NameEqualityComparisonByteArrayFilter(typeof(ProblemParameterBlobValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + name + " Value", name)); foreach (string name in okb.ProblemParameters.Where(x => x.DataType.SqlName == "bit").Select(x => x.Name).Distinct()) filters.Add(new NameEqualityComparisonBoolFilter(typeof(ProblemParameterBoolValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + name + " Value", name)); foreach (string name in okb.ProblemParameters.Where(x => x.DataType.SqlName == "float").Select(x => x.Name).Distinct()) filters.Add(new NameOrdinalComparisonDoubleFilter(typeof(ProblemParameterFloatValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + name + " Value", name)); foreach (string name in okb.ProblemParameters.Where(x => x.DataType.SqlName == "bigint").Select(x => x.Name).Distinct()) filters.Add(new NameOrdinalComparisonLongFilter(typeof(ProblemParameterIntValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + name + " Value", name)); foreach (string name in okb.ProblemParameters.Where(x => x.DataType.SqlName == "nvarchar").Select(x => x.Name).Distinct()) filters.Add(new NameSetComparisonStringFilter(typeof(ProblemParameterStringValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + name + " Value", name)); // problem filters filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(ProblemNameFilter).AssemblyQualifiedName, "Problem Name", okb.Problems.Select(x => x.Name).Distinct().ToArray())); filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(ProblemClassNameFilter).AssemblyQualifiedName, "Problem Class Name", okb.ProblemClasses.Select(x => x.Name).Distinct().ToArray())); filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(ProblemPlatformNameFilter).AssemblyQualifiedName, "Problem Platform Name", okb.Platforms.Select(x => x.Name).Distinct().ToArray())); filters.Add(new SetComparisonStringAvailableValuesFilter(typeof(ProblemDataDataTypeNameFilter).AssemblyQualifiedName, "Problem Data Type Name", okb.Problems.Select(x => x.ProblemData.DataType.Name).Distinct().ToArray())); // and/or filters filters.Add(new CombinedFilter(typeof(AndFilter).AssemblyQualifiedName, "AND", BooleanOperation.And)); filters.Add(new CombinedFilter(typeof(OrFilter).AssemblyQualifiedName, "OR", BooleanOperation.Or)); } return filters.OrderBy(x => x.Label); } public long GetNumberOfQueryResults(Filter filter) { using (OKBDataContext okb = new OKBDataContext()) { return FilterRuns(okb.Runs, filter).LongCount(); } } public IEnumerable GetQueryResultIds(Filter filter) { using (OKBDataContext okb = new OKBDataContext()) { return FilterRuns(okb.Runs, filter).Select(x => x.Id).ToArray(); } } public IEnumerable GetQueryResults(IEnumerable ids) { using (OKBDataContext okb = new OKBDataContext()) { DataLoadOptions dlo = new DataLoadOptions(); dlo.LoadWith(x => x.ResultBlobValues); dlo.LoadWith(x => x.ResultBoolValues); dlo.LoadWith(x => x.ResultFloatValues); dlo.LoadWith(x => x.ResultIntValues); dlo.LoadWith(x => x.ResultStringValues); dlo.LoadWith(x => x.Experiment); dlo.LoadWith(x => x.Result); dlo.LoadWith(x => x.DataType); dlo.LoadWith(x => x.Result); dlo.LoadWith(x => x.Result); dlo.LoadWith(x => x.Result); dlo.LoadWith(x => x.Result); dlo.LoadWith(x => x.AlgorithmParameterBlobValues); dlo.LoadWith(x => x.AlgorithmParameterBoolValues); dlo.LoadWith(x => x.AlgorithmParameterFloatValues); dlo.LoadWith(x => x.AlgorithmParameterIntValues); dlo.LoadWith(x => x.AlgorithmParameterStringValues); dlo.LoadWith(x => x.ProblemParameterBlobValues); dlo.LoadWith(x => x.ProblemParameterBoolValues); dlo.LoadWith(x => x.ProblemParameterFloatValues); dlo.LoadWith(x => x.ProblemParameterIntValues); dlo.LoadWith(x => x.ProblemParameterStringValues); //dlo.LoadWith(x => x.Algorithm); //dlo.LoadWith(x => x.Problem); dlo.LoadWith(x => x.AlgorithmParameter); dlo.LoadWith(x => x.DataType); dlo.LoadWith(x => x.AlgorithmParameter); dlo.LoadWith(x => x.AlgorithmParameter); dlo.LoadWith(x => x.AlgorithmParameter); dlo.LoadWith(x => x.AlgorithmParameter); dlo.LoadWith(x => x.ProblemParameter); dlo.LoadWith(x => x.DataType); dlo.LoadWith(x => x.ProblemParameter); dlo.LoadWith(x => x.ProblemParameter); dlo.LoadWith(x => x.ProblemParameter); dlo.LoadWith(x => x.ProblemParameter); //dlo.LoadWith(x => x.AlgorithmParameters); //dlo.LoadWith(x => x.Results); //dlo.LoadWith(x => x.AlgorithmClass); //dlo.LoadWith(x => x.Platform); //dlo.LoadWith(x => x.ProblemParameters); //dlo.LoadWith(x => x.ProblemClass); okb.LoadOptions = dlo; return okb.Runs.Where(x => ids.Contains(x.Id)).Select(x => ConvertToQueryResult(x)).ToArray(); } } private IQueryable FilterRuns(IQueryable runs, Filter filter) { IFilter f = (IFilter)Activator.CreateInstance(Type.GetType(filter.FilterTypeName), filter); return runs.Where(f.Expression); } private QueryResult ConvertToQueryResult(DataAccess.Run run) { List algorithmParameters = new List(); List problemParameters = new List(); List results = new List(); foreach (DataAccess.AlgorithmParameterBlobValue value in run.Experiment.AlgorithmParameterBlobValues) algorithmParameters.Add(new QueryBlobValue { Name = value.AlgorithmParameter.Name, DataTypeName = value.DataType.Name + " (" + value.DataType.TypeName + ")", Value = value.Value.ToArray() }); foreach (DataAccess.AlgorithmParameterBoolValue value in run.Experiment.AlgorithmParameterBoolValues) algorithmParameters.Add(new QueryBoolValue { Name = value.AlgorithmParameter.Name, Value = value.Value }); foreach (DataAccess.AlgorithmParameterFloatValue value in run.Experiment.AlgorithmParameterFloatValues) algorithmParameters.Add(new QueryFloatValue { Name = value.AlgorithmParameter.Name, Value = value.Value }); foreach (DataAccess.AlgorithmParameterIntValue value in run.Experiment.AlgorithmParameterIntValues) algorithmParameters.Add(new QueryIntValue { Name = value.AlgorithmParameter.Name, Value = value.Value }); foreach (DataAccess.AlgorithmParameterStringValue value in run.Experiment.AlgorithmParameterStringValues) algorithmParameters.Add(new QueryStringValue { Name = value.AlgorithmParameter.Name, Value = value.Value }); foreach (DataAccess.ProblemParameterBlobValue value in run.Experiment.ProblemParameterBlobValues) problemParameters.Add(new QueryBlobValue { Name = value.ProblemParameter.Name, DataTypeName = value.DataType.Name + " (" + value.DataType.TypeName + ")", Value = value.Value.ToArray() }); foreach (DataAccess.ProblemParameterBoolValue value in run.Experiment.ProblemParameterBoolValues) problemParameters.Add(new QueryBoolValue { Name = value.ProblemParameter.Name, Value = value.Value }); foreach (DataAccess.ProblemParameterFloatValue value in run.Experiment.ProblemParameterFloatValues) problemParameters.Add(new QueryFloatValue { Name = value.ProblemParameter.Name, Value = value.Value }); foreach (DataAccess.ProblemParameterIntValue value in run.Experiment.ProblemParameterIntValues) problemParameters.Add(new QueryIntValue { Name = value.ProblemParameter.Name, Value = value.Value }); foreach (DataAccess.ProblemParameterStringValue value in run.Experiment.ProblemParameterStringValues) problemParameters.Add(new QueryStringValue { Name = value.ProblemParameter.Name, Value = value.Value }); foreach (DataAccess.ResultBlobValue value in run.ResultBlobValues) results.Add(new QueryBlobValue { Name = value.Result.Name, DataTypeName = value.DataType.Name + " (" + value.DataType.TypeName + ")", Value = value.Value.ToArray() }); foreach (DataAccess.ResultBoolValue value in run.ResultBoolValues) results.Add(new QueryBoolValue { Name = value.Result.Name, Value = value.Value }); foreach (DataAccess.ResultFloatValue value in run.ResultFloatValues) results.Add(new QueryFloatValue { Name = value.Result.Name, Value = value.Value }); foreach (DataAccess.ResultIntValue value in run.ResultIntValues) results.Add(new QueryIntValue { Name = value.Result.Name, Value = value.Value }); foreach (DataAccess.ResultStringValue value in run.ResultStringValues) results.Add(new QueryStringValue { Name = value.Result.Name, Value = value.Value }); QueryResult result = new QueryResult(); result.Id = run.Id; result.AlgorithmParameters = algorithmParameters; result.ProblemParameters = problemParameters; result.Results = results; return result; } } }