#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; namespace HeuristicLab.Services.OKB { /// /// Implementation of the OKB service (interface ). /// [ServiceBehavior(IncludeExceptionDetailInFaults = true)] public class OKBService : IOKBService { #region Platform Methods public DataTransfer.Platform GetPlatform(long id) { using (OKBDataContext okb = new OKBDataContext()) { return Convert.ToDto(okb.Platforms.FirstOrDefault(x => x.Id == id)); } } public IEnumerable GetPlatforms() { using (OKBDataContext okb = new OKBDataContext()) { return okb.Platforms.Select(x => Convert.ToDto(x)).ToArray(); } } public long AddPlatform(DataTransfer.Platform dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Platform entity = Convert.ToEntity(dto); entity.Id = 0; okb.Platforms.InsertOnSubmit(entity); okb.SubmitChanges(); return entity.Id; } } public void UpdatePlatform(DataTransfer.Platform dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == dto.Id); Convert.ToEntity(dto, entity); okb.SubmitChanges(); } } public void DeletePlatform(long id) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == id); if (entity != null) okb.Platforms.DeleteOnSubmit(entity); okb.SubmitChanges(); } } #endregion #region DataType Methods public DataTransfer.DataType GetDataType(long id) { using (OKBDataContext okb = new OKBDataContext()) { return Convert.ToDto(okb.DataTypes.FirstOrDefault(x => x.Id == id)); } } public IEnumerable GetDataTypes() { using (OKBDataContext okb = new OKBDataContext()) { return okb.DataTypes.Select(x => Convert.ToDto(x)).ToArray(); } } public long AddDataType(DataTransfer.DataType dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.DataType entity = Convert.ToEntity(dto); entity.Id = 0; okb.DataTypes.InsertOnSubmit(entity); okb.SubmitChanges(); return entity.Id; } } public void UpdateDataType(DataTransfer.DataType dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.DataType entity = okb.DataTypes.FirstOrDefault(x => x.Id == dto.Id); Convert.ToEntity(dto, entity); okb.SubmitChanges(); } } public void DeleteDataType(long id) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.DataType entity = okb.DataTypes.FirstOrDefault(x => x.Id == id); if (entity != null) okb.DataTypes.DeleteOnSubmit(entity); okb.SubmitChanges(); } } #endregion #region AlgorithmClass Methods public DataTransfer.AlgorithmClass GetAlgorithmClass(long id) { using (OKBDataContext okb = new OKBDataContext()) { return Convert.ToDto(okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id)); } } public IEnumerable GetAlgorithmClasses() { using (OKBDataContext okb = new OKBDataContext()) { return okb.AlgorithmClasses.Select(x => Convert.ToDto(x)).ToArray(); } } public long AddAlgorithmClass(DataTransfer.AlgorithmClass dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.AlgorithmClass entity = Convert.ToEntity(dto); entity.Id = 0; okb.AlgorithmClasses.InsertOnSubmit(entity); okb.SubmitChanges(); return entity.Id; } } public void UpdateAlgorithmClass(DataTransfer.AlgorithmClass dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == dto.Id); Convert.ToEntity(dto, entity); okb.SubmitChanges(); } } public void DeleteAlgorithmClass(long id) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id); if (entity != null) okb.AlgorithmClasses.DeleteOnSubmit(entity); okb.SubmitChanges(); } } #endregion #region Algorithm Methods public DataTransfer.Algorithm GetAlgorithm(long id) { using (OKBDataContext okb = new OKBDataContext()) { return Convert.ToDto(okb.Algorithms.FirstOrDefault(x => x.Id == id)); } } public IEnumerable GetAlgorithms() { using (OKBDataContext okb = new OKBDataContext()) { return okb.Algorithms.Select(x => Convert.ToDto(x)).ToArray(); } } public long AddAlgorithm(DataTransfer.Algorithm dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Algorithm entity = Convert.ToEntity(dto); entity.Id = 0; okb.Algorithms.InsertOnSubmit(entity); okb.SubmitChanges(); return entity.Id; } } public void UpdateAlgorithm(DataTransfer.Algorithm dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == dto.Id); Convert.ToEntity(dto, entity); okb.SubmitChanges(); } } public void DeleteAlgorithm(long id) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == id); if (entity != null) okb.Algorithms.DeleteOnSubmit(entity); okb.SubmitChanges(); } } public IEnumerable GetAlgorithmUsers(long algorithmId) { using (OKBDataContext okb = new OKBDataContext()) { return okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId).Select(x => x.UserId).ToArray(); } } public void UpdateAlgorithmUsers(long algorithmId, IEnumerable users) { using (OKBDataContext okb = new OKBDataContext()) { okb.AlgorithmUsers.DeleteAllOnSubmit(okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId)); okb.AlgorithmUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.AlgorithmUser { AlgorithmId = algorithmId, UserId = x })); okb.SubmitChanges(); } } #endregion #region AlgorithmData Methods public DataTransfer.AlgorithmData GetAlgorithmData(long algorithmId) { using (OKBDataContext okb = new OKBDataContext()) { return Convert.ToDto(okb.AlgorithmDatas.FirstOrDefault(x => x.AlgorithmId == algorithmId)); } } public void UpdateAlgorithmData(DataTransfer.AlgorithmData dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.AlgorithmData entity = okb.AlgorithmDatas.FirstOrDefault(x => x.AlgorithmId == dto.AlgorithmId); if (entity == null) okb.AlgorithmDatas.InsertOnSubmit(Convert.ToEntity(dto)); else Convert.ToEntity(dto, entity); okb.SubmitChanges(); } } #endregion #region AlgorithmParameter Methods public DataTransfer.AlgorithmParameter GetAlgorithmParameter(long id) { using (OKBDataContext okb = new OKBDataContext()) { return Convert.ToDto(okb.AlgorithmParameters.FirstOrDefault(x => x.Id == id)); } } public IEnumerable GetAlgorithmParameters(long algorithmId) { using (OKBDataContext okb = new OKBDataContext()) { return okb.AlgorithmParameters.Where(x => x.AlgorithmId == algorithmId).Select(x => Convert.ToDto(x)).ToArray(); } } public long AddAlgorithmParameter(DataTransfer.AlgorithmParameter dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.AlgorithmParameter entity = Convert.ToEntity(dto); entity.Id = 0; okb.AlgorithmParameters.InsertOnSubmit(entity); okb.SubmitChanges(); return entity.Id; } } public void UpdateAlgorithmParameter(DataTransfer.AlgorithmParameter dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.AlgorithmParameter entity = okb.AlgorithmParameters.FirstOrDefault(x => x.Id == dto.Id); Convert.ToEntity(dto, entity); okb.SubmitChanges(); } } public void DeleteAlgorithmParameter(long id) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.AlgorithmParameter entity = okb.AlgorithmParameters.FirstOrDefault(x => x.Id == id); if (entity != null) okb.AlgorithmParameters.DeleteOnSubmit(entity); okb.SubmitChanges(); } } #endregion #region ProblemClass Methods public DataTransfer.ProblemClass GetProblemClass(long id) { using (OKBDataContext okb = new OKBDataContext()) { return Convert.ToDto(okb.ProblemClasses.FirstOrDefault(x => x.Id == id)); } } public IEnumerable GetProblemClasses() { using (OKBDataContext okb = new OKBDataContext()) { return okb.ProblemClasses.Select(x => Convert.ToDto(x)).ToArray(); } } public long AddProblemClass(DataTransfer.ProblemClass dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.ProblemClass entity = Convert.ToEntity(dto); entity.Id = 0; okb.ProblemClasses.InsertOnSubmit(entity); okb.SubmitChanges(); return entity.Id; } } public void UpdateProblemClass(DataTransfer.ProblemClass dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == dto.Id); Convert.ToEntity(dto, entity); okb.SubmitChanges(); } } public void DeleteProblemClass(long id) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == id); if (entity != null) okb.ProblemClasses.DeleteOnSubmit(entity); okb.SubmitChanges(); } } #endregion #region Problem Methods public DataTransfer.Problem GetProblem(long id) { using (OKBDataContext okb = new OKBDataContext()) { return Convert.ToDto(okb.Problems.FirstOrDefault(x => x.Id == id)); } } public IEnumerable GetProblems() { using (OKBDataContext okb = new OKBDataContext()) { return okb.Problems.Select(x => Convert.ToDto(x)).ToArray(); } } public long AddProblem(DataTransfer.Problem dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Problem entity = Convert.ToEntity(dto); entity.Id = 0; okb.Problems.InsertOnSubmit(entity); okb.SubmitChanges(); return entity.Id; } } public void UpdateProblem(DataTransfer.Problem dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Problem entity = okb.Problems.FirstOrDefault(x => x.Id == dto.Id); Convert.ToEntity(dto, entity); okb.SubmitChanges(); } } public void DeleteProblem(long id) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Problem entity = okb.Problems.FirstOrDefault(x => x.Id == id); if (entity != null) okb.Problems.DeleteOnSubmit(entity); okb.SubmitChanges(); } } public IEnumerable GetProblemUsers(long problemId) { using (OKBDataContext okb = new OKBDataContext()) { return okb.ProblemUsers.Where(x => x.ProblemId == problemId).Select(x => x.UserId).ToArray(); } } public void UpdateProblemUsers(long problemId, IEnumerable users) { using (OKBDataContext okb = new OKBDataContext()) { okb.ProblemUsers.DeleteAllOnSubmit(okb.ProblemUsers.Where(x => x.ProblemId == problemId)); okb.ProblemUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.ProblemUser { ProblemId = problemId, UserId = x })); okb.SubmitChanges(); } } #endregion #region ProblemData Methods public DataTransfer.ProblemData GetProblemData(long problemId) { using (OKBDataContext okb = new OKBDataContext()) { return Convert.ToDto(okb.ProblemDatas.FirstOrDefault(x => x.ProblemId == problemId)); } } public void UpdateProblemData(DataTransfer.ProblemData dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.ProblemData entity = okb.ProblemDatas.FirstOrDefault(x => x.ProblemId == dto.ProblemId); if (entity == null) okb.ProblemDatas.InsertOnSubmit(Convert.ToEntity(dto)); else Convert.ToEntity(dto, entity); okb.SubmitChanges(); } } #endregion #region ProblemParameter Methods public DataTransfer.ProblemParameter GetProblemParameter(long id) { using (OKBDataContext okb = new OKBDataContext()) { return Convert.ToDto(okb.ProblemParameters.FirstOrDefault(x => x.Id == id)); } } public IEnumerable GetProblemParameters(long problemId) { using (OKBDataContext okb = new OKBDataContext()) { return okb.ProblemParameters.Where(x => x.ProblemId == problemId).Select(x => Convert.ToDto(x)).ToArray(); } } public long AddProblemParameter(DataTransfer.ProblemParameter dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.ProblemParameter entity = Convert.ToEntity(dto); entity.Id = 0; okb.ProblemParameters.InsertOnSubmit(entity); okb.SubmitChanges(); return entity.Id; } } public void UpdateProblemParameter(DataTransfer.ProblemParameter dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.ProblemParameter entity = okb.ProblemParameters.FirstOrDefault(x => x.Id == dto.Id); Convert.ToEntity(dto, entity); okb.SubmitChanges(); } } public void DeleteProblemParameter(long id) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.ProblemParameter entity = okb.ProblemParameters.FirstOrDefault(x => x.Id == id); if (entity != null) okb.ProblemParameters.DeleteOnSubmit(entity); okb.SubmitChanges(); } } #endregion #region Result Methods public DataTransfer.Result GetResult(long id) { using (OKBDataContext okb = new OKBDataContext()) { return Convert.ToDto(okb.Results.FirstOrDefault(x => x.Id == id)); } } public IEnumerable GetResults(long algorithmId) { using (OKBDataContext okb = new OKBDataContext()) { return okb.Results.Where(x => x.AlgorithmId == algorithmId).Select(x => Convert.ToDto(x)).ToArray(); } } public long AddResult(DataTransfer.Result dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Result entity = Convert.ToEntity(dto); entity.Id = 0; okb.Results.InsertOnSubmit(entity); okb.SubmitChanges(); return entity.Id; } } public void UpdateResult(DataTransfer.Result dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Result entity = okb.Results.FirstOrDefault(x => x.Id == dto.Id); Convert.ToEntity(dto, entity); okb.SubmitChanges(); } } public void DeleteResult(long id) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Result entity = okb.Results.FirstOrDefault(x => x.Id == id); if (entity != null) okb.Results.DeleteOnSubmit(entity); okb.SubmitChanges(); } } #endregion #region Experiment Methods public DataTransfer.Experiment GetExperiment(long id) { using (OKBDataContext okb = new OKBDataContext()) { DataLoadOptions dlo = new DataLoadOptions(); 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); okb.LoadOptions = dlo; return Convert.ToDto(okb.Experiments.FirstOrDefault(x => x.Id == id)); } } public IEnumerable GetExperiments(long algorithmId, long problemId) { using (OKBDataContext okb = new OKBDataContext()) { DataLoadOptions dlo = new DataLoadOptions(); 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); okb.LoadOptions = dlo; if ((algorithmId != 0) && (problemId != 0)) return okb.Experiments.Where(x => (x.AlgorithmId == algorithmId) && (x.ProblemId == problemId)).Select(x => Convert.ToDto(x)).ToArray(); else if (algorithmId != 0) return okb.Experiments.Where(x => x.AlgorithmId == algorithmId).Select(x => Convert.ToDto(x)).ToArray(); else if (problemId != 0) return okb.Experiments.Where(x => x.ProblemId == problemId).Select(x => Convert.ToDto(x)).ToArray(); else return okb.Experiments.Select(x => Convert.ToDto(x)).ToArray(); } } public long AddExperiment(DataTransfer.Experiment dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Experiment entity = Convert.ToEntity(dto); entity.Id = 0; DataLoadOptions dlo = new DataLoadOptions(); 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); okb.LoadOptions = dlo; var experiments = okb.Experiments.Where(x => ((x.AlgorithmId == entity.AlgorithmId) && (x.ProblemId == entity.ProblemId))).ToArray(); ExperimentEqualityComparer comparer = new ExperimentEqualityComparer(); Experiment exp = experiments.FirstOrDefault(x => comparer.Equals(x, entity)); if (exp != null) { return exp.Id; } else { okb.Experiments.InsertOnSubmit(entity); okb.SubmitChanges(); return entity.Id; } } } public void DeleteExperiment(long id) { foreach (DataTransfer.Run run in GetRuns(id)) DeleteRun(run.Id); using (OKBDataContext okb = new OKBDataContext()) { IEnumerable algorithmParameterBlobValues = okb.AlgorithmParameterBlobValues.Where(x => x.ExperimentId == id); okb.AlgorithmParameterBlobValues.DeleteAllOnSubmit(algorithmParameterBlobValues); IEnumerable algorithmParameterBoolValues = okb.AlgorithmParameterBoolValues.Where(x => x.ExperimentId == id); okb.AlgorithmParameterBoolValues.DeleteAllOnSubmit(algorithmParameterBoolValues); IEnumerable algorithmParameterFloatValues = okb.AlgorithmParameterFloatValues.Where(x => x.ExperimentId == id); okb.AlgorithmParameterFloatValues.DeleteAllOnSubmit(algorithmParameterFloatValues); IEnumerable algorithmParameterIntValues = okb.AlgorithmParameterIntValues.Where(x => x.ExperimentId == id); okb.AlgorithmParameterIntValues.DeleteAllOnSubmit(algorithmParameterIntValues); IEnumerable algorithmParameterStringValues = okb.AlgorithmParameterStringValues.Where(x => x.ExperimentId == id); okb.AlgorithmParameterStringValues.DeleteAllOnSubmit(algorithmParameterStringValues); IEnumerable problemParameterBlobValues = okb.ProblemParameterBlobValues.Where(x => x.ExperimentId == id); okb.ProblemParameterBlobValues.DeleteAllOnSubmit(problemParameterBlobValues); IEnumerable problemParameterBoolValues = okb.ProblemParameterBoolValues.Where(x => x.ExperimentId == id); okb.ProblemParameterBoolValues.DeleteAllOnSubmit(problemParameterBoolValues); IEnumerable problemParameterFloatValues = okb.ProblemParameterFloatValues.Where(x => x.ExperimentId == id); okb.ProblemParameterFloatValues.DeleteAllOnSubmit(problemParameterFloatValues); IEnumerable problemParameterIntValues = okb.ProblemParameterIntValues.Where(x => x.ExperimentId == id); okb.ProblemParameterIntValues.DeleteAllOnSubmit(problemParameterIntValues); IEnumerable problemParameterStringValues = okb.ProblemParameterStringValues.Where(x => x.ExperimentId == id); okb.ProblemParameterStringValues.DeleteAllOnSubmit(problemParameterStringValues); DataAccess.Experiment entity = okb.Experiments.FirstOrDefault(x => x.Id == id); if (entity != null) okb.Experiments.DeleteOnSubmit(entity); okb.SubmitChanges(); } } #endregion #region Run Methods public DataTransfer.Run GetRun(long id) { 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); okb.LoadOptions = dlo; return Convert.ToDto(okb.Runs.FirstOrDefault(x => x.Id == id)); } } public IEnumerable GetRuns(long experimentId) { 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); okb.LoadOptions = dlo; return okb.Runs.Where(x => x.ExperimentId == experimentId).Select(x => Convert.ToDto(x)).ToArray(); } } public long AddRun(DataTransfer.Run dto) { using (OKBDataContext okb = new OKBDataContext()) { DataAccess.Run entity = Convert.ToEntity(dto); entity.Id = 0; okb.Runs.InsertOnSubmit(entity); okb.SubmitChanges(); return entity.Id; } } public void DeleteRun(long id) { using (OKBDataContext okb = new OKBDataContext()) { IEnumerable resultBlobValues = okb.ResultBlobValues.Where(x => x.RunId == id); okb.ResultBlobValues.DeleteAllOnSubmit(resultBlobValues); IEnumerable resultBoolValues = okb.ResultBoolValues.Where(x => x.RunId == id); okb.ResultBoolValues.DeleteAllOnSubmit(resultBoolValues); IEnumerable resultFloatValues = okb.ResultFloatValues.Where(x => x.RunId == id); okb.ResultFloatValues.DeleteAllOnSubmit(resultFloatValues); IEnumerable resultIntValues = okb.ResultIntValues.Where(x => x.RunId == id); okb.ResultIntValues.DeleteAllOnSubmit(resultIntValues); IEnumerable resultStringValues = okb.ResultStringValues.Where(x => x.RunId == id); okb.ResultStringValues.DeleteAllOnSubmit(resultStringValues); DataAccess.Run entity = okb.Runs.FirstOrDefault(x => x.Id == id); if (entity != null) okb.Runs.DeleteOnSubmit(entity); okb.SubmitChanges(); } } #endregion #region Query Methods public IEnumerable GetFilters() { List filters = new List(); using (OKBDataContext okb = new OKBDataContext()) { //DataLoadOptions dlo = new DataLoadOptions(); //dlo.LoadWith(x => x.AlgorithmData); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.ProblemData); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.DataType); //okb.LoadOptions = dlo; // run filters filters.Add(new DataTransfer.OrdinalComparisonLongFilter(typeof(RunRandomSeedFilter).AssemblyQualifiedName, "Run Random Seed")); filters.Add(new DataTransfer.OrdinalComparisonDateTimeFilter(typeof(RunCreatedDateFilter).AssemblyQualifiedName, "Run Created Date")); filters.Add(new DataTransfer.SetComparisonStringFilter(typeof(RunUserNameFilter).AssemblyQualifiedName, "Run User Name")); filters.Add(new DataTransfer.SetComparisonStringFilter(typeof(RunClientNameFilter).AssemblyQualifiedName, "Run Client Name")); // result filters filters.Add(new DataTransfer.SetComparisonStringAvailableValuesFilter(typeof(ResultNameFilter).AssemblyQualifiedName, "Result Name", okb.Results.Select(x => x.Name).Distinct().ToArray())); foreach (Result entity in okb.Results.Where(x => x.DataType.SqlName == "varbinary").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameSetComparisonStringAvailableValuesFilter(typeof(ResultBlobValueDataTypeNameFilter).AssemblyQualifiedName, "Result " + entity.Name + " Value Data Type Name", entity.Name, okb.ResultBlobValues.Where(x => x.Result.Name == entity.Name).Select(x => x.DataType.Name).Distinct().ToArray())); foreach (Result entity in okb.Results.Where(x => x.DataType.SqlName == "varbinary").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameEqualityComparisonByteArrayFilter(typeof(ResultBlobValueValueFilter).AssemblyQualifiedName, "Result " + entity.Name + " Value", entity.Name)); foreach (Result entity in okb.Results.Where(x => x.DataType.SqlName == "bit").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameEqualityComparisonBoolFilter(typeof(ResultBoolValueValueFilter).AssemblyQualifiedName, "Result " + entity.Name + " Value", entity.Name)); foreach (Result entity in okb.Results.Where(x => x.DataType.SqlName == "float").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameOrdinalComparisonDoubleFilter(typeof(ResultFloatValueValueFilter).AssemblyQualifiedName, "Result " + entity.Name + " Value", entity.Name)); foreach (Result entity in okb.Results.Where(x => x.DataType.SqlName == "bigint").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameOrdinalComparisonLongFilter(typeof(ResultIntValueValueFilter).AssemblyQualifiedName, "Result " + entity.Name + " Value", entity.Name)); foreach (Result entity in okb.Results.Where(x => x.DataType.SqlName == "nvarchar").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameSetComparisonStringFilter(typeof(ResultStringValueValueFilter).AssemblyQualifiedName, "Result " + entity.Name + " Value", entity.Name)); // algorithm parameter filters filters.Add(new DataTransfer.SetComparisonStringAvailableValuesFilter(typeof(AlgorithmParameterNameFilter).AssemblyQualifiedName, "Algorithm Parameter Name", okb.AlgorithmParameters.Select(x => x.Name).Distinct().ToArray())); foreach (AlgorithmParameter entity in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "varbinary").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameSetComparisonStringAvailableValuesFilter(typeof(AlgorithmParameterBlobValueDataTypeNameFilter).AssemblyQualifiedName, "Algorithm Parameter " + entity.Name + " Value Data Type Name", entity.Name, okb.AlgorithmParameterBlobValues.Where(x => x.AlgorithmParameter.Name == entity.Name).Select(x => x.DataType.Name).Distinct().ToArray())); foreach (AlgorithmParameter entity in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "varbinary").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameEqualityComparisonByteArrayFilter(typeof(AlgorithmParameterBlobValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + entity.Name + " Value", entity.Name)); foreach (AlgorithmParameter entity in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "bit").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameEqualityComparisonBoolFilter(typeof(AlgorithmParameterBoolValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + entity.Name + " Value", entity.Name)); foreach (AlgorithmParameter entity in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "float").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameOrdinalComparisonDoubleFilter(typeof(AlgorithmParameterFloatValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + entity.Name + " Value", entity.Name)); foreach (AlgorithmParameter entity in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "bigint").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameOrdinalComparisonLongFilter(typeof(AlgorithmParameterIntValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + entity.Name + " Value", entity.Name)); foreach (AlgorithmParameter entity in okb.AlgorithmParameters.Where(x => x.DataType.SqlName == "nvarchar").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameSetComparisonStringFilter(typeof(AlgorithmParameterStringValueValueFilter).AssemblyQualifiedName, "Algorithm Parameter " + entity.Name + " Value", entity.Name)); // algorithm filters filters.Add(new DataTransfer.SetComparisonStringAvailableValuesFilter(typeof(AlgorithmNameFilter).AssemblyQualifiedName, "Algorithm Name", okb.Algorithms.Select(x => x.Name).Distinct().ToArray())); filters.Add(new DataTransfer.SetComparisonStringAvailableValuesFilter(typeof(AlgorithmClassNameFilter).AssemblyQualifiedName, "Algorithm Class Name", okb.AlgorithmClasses.Select(x => x.Name).Distinct().ToArray())); filters.Add(new DataTransfer.SetComparisonStringAvailableValuesFilter(typeof(AlgorithmPlatformNameFilter).AssemblyQualifiedName, "Algorithm Platform Name", okb.Platforms.Select(x => x.Name).Distinct().ToArray())); filters.Add(new DataTransfer.SetComparisonStringAvailableValuesFilter(typeof(AlgorithmDataDataTypeNameFilter).AssemblyQualifiedName, "Algorithm Data Type Name", okb.Algorithms.Select(x => x.AlgorithmData.DataType.Name).Distinct().ToArray())); // problem parameter filters filters.Add(new DataTransfer.SetComparisonStringAvailableValuesFilter(typeof(ProblemParameterNameFilter).AssemblyQualifiedName, "Problem Parameter Name", okb.ProblemParameters.Select(x => x.Name).Distinct().ToArray())); foreach (ProblemParameter entity in okb.ProblemParameters.Where(x => x.DataType.SqlName == "varbinary").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameSetComparisonStringAvailableValuesFilter(typeof(ProblemParameterBlobValueDataTypeNameFilter).AssemblyQualifiedName, "Problem Parameter " + entity.Name + " Value Data Type Name", entity.Name, okb.ProblemParameterBlobValues.Where(x => x.ProblemParameter.Name == entity.Name).Select(x => x.DataType.Name).Distinct().ToArray())); foreach (ProblemParameter entity in okb.ProblemParameters.Where(x => x.DataType.SqlName == "varbinary").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameEqualityComparisonByteArrayFilter(typeof(ProblemParameterBlobValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + entity.Name + " Value", entity.Name)); foreach (ProblemParameter entity in okb.ProblemParameters.Where(x => x.DataType.SqlName == "bit").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameEqualityComparisonBoolFilter(typeof(ProblemParameterBoolValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + entity.Name + " Value", entity.Name)); foreach (ProblemParameter entity in okb.ProblemParameters.Where(x => x.DataType.SqlName == "float").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameOrdinalComparisonDoubleFilter(typeof(ProblemParameterFloatValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + entity.Name + " Value", entity.Name)); foreach (ProblemParameter entity in okb.ProblemParameters.Where(x => x.DataType.SqlName == "bigint").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameOrdinalComparisonLongFilter(typeof(ProblemParameterIntValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + entity.Name + " Value", entity.Name)); foreach (ProblemParameter entity in okb.ProblemParameters.Where(x => x.DataType.SqlName == "nvarchar").AsEnumerable().Distinct(new GenericEqualityComparer((x, y) => x.Name == y.Name))) filters.Add(new DataTransfer.NameSetComparisonStringFilter(typeof(ProblemParameterStringValueValueFilter).AssemblyQualifiedName, "Problem Parameter " + entity.Name + " Value", entity.Name)); // problem filters filters.Add(new DataTransfer.SetComparisonStringAvailableValuesFilter(typeof(ProblemNameFilter).AssemblyQualifiedName, "Problem Name", okb.Problems.Select(x => x.Name).Distinct().ToArray())); filters.Add(new DataTransfer.SetComparisonStringAvailableValuesFilter(typeof(ProblemClassNameFilter).AssemblyQualifiedName, "Problem Class Name", okb.ProblemClasses.Select(x => x.Name).Distinct().ToArray())); filters.Add(new DataTransfer.SetComparisonStringAvailableValuesFilter(typeof(ProblemPlatformNameFilter).AssemblyQualifiedName, "Problem Platform Name", okb.Platforms.Select(x => x.Name).Distinct().ToArray())); filters.Add(new DataTransfer.SetComparisonStringAvailableValuesFilter(typeof(ProblemDataDataTypeNameFilter).AssemblyQualifiedName, "Problem Data Type Name", okb.Problems.Select(x => x.ProblemData.DataType.Name).Distinct().ToArray())); // and/or filters filters.Add(new DataTransfer.AndFilter(typeof(AndFilter).AssemblyQualifiedName, "And")); filters.Add(new DataTransfer.OrFilter(typeof(OrFilter).AssemblyQualifiedName, "Or")); } return filters.OrderBy(x => x.Label); } public long QueryNumberOfRuns(DataTransfer.Filter filter) { using (OKBDataContext okb = new OKBDataContext()) { IFilter convertedFilter = (IFilter)Activator.CreateInstance(Type.GetType(filter.FilterTypeName), filter); return okb.Runs.Where(convertedFilter.Expression).LongCount(); } } public IEnumerable QueryRuns(DataTransfer.Filter filter) { 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.DataType); //dlo.LoadWith(x => x.Result); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.Result); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.Result); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.Algorithm); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.Algorithm); //dlo.LoadWith(x => x.Problem); //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.AlgorithmParameter); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.AlgorithmParameter); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.AlgorithmParameter); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.AlgorithmParameter); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.AlgorithmParameter); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.ProblemParameter); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.ProblemParameter); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.ProblemParameter); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.ProblemParameter); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.ProblemParameter); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.Algorithm); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.Problem); //dlo.LoadWith(x => x.DataType); //dlo.LoadWith(x => x.AlgorithmClass); //dlo.LoadWith(x => x.Platform); //dlo.LoadWith(x => x.ProblemClass); //dlo.LoadWith(x => x.Platform); //okb.LoadOptions = dlo; 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); okb.LoadOptions = dlo; IFilter convertedFilter = (IFilter)Activator.CreateInstance(Type.GetType(filter.FilterTypeName), filter); var runs = okb.Runs.Where(convertedFilter.Expression).ToArray(); var result = runs.Select(x => Convert.ToDto(x)).ToArray(); return result; } } #endregion /* /// /// Gets the complete algorithm object graph up to the following entities: /// /// Parameter /// Algorithm_Paramters.Parameter.DataType /// Algorithm_Results.Result.DataType /// /// /// The algorithm id. /// An public Algorithm GetCompleteAlgorithm(int id) { using (OKBDataContext okb = new OKBDataContext()) { var dlo = new DataLoadOptions(); dlo.LoadWith(a => a.AlgorithmClass); dlo.LoadWith(a => a.Platform); dlo.LoadWith(a => a.AlgorithmUsers); dlo.LoadWith(u => u.User); okb.LoadOptions = dlo; return okb.Algorithms.Single(a => a.Id == id); } } /// /// Gets the complete problem object graph up to the following entities: /// /// Platform /// SolutionRepresentation /// Problem_Parameters.Parameter /// IntProblemCharacteristicValues.ProblemCharacteristic.DataType /// FloatProblemCharacteristicValues.ProblemCharacteristic.DataType /// CharProblemCharacteristicValues.ProblemCharacteristic.DataType /// /// /// The problem id. /// A public Problem GetCompleteProblem(int id) { using (OKBDataContext okb = new OKBDataContext()) { var dlo = new DataLoadOptions(); dlo.LoadWith(p => p.ProblemClass); dlo.LoadWith(p => p.Platform); dlo.LoadWith(p => p.ProblemUsers); dlo.LoadWith(u => u.User); okb.LoadOptions = dlo; return okb.Problems.Single(p => p.Id == id); } } /// /// Updates the algorithm object graph including the following properties and linked entitites: /// /// Name /// Description /// AlgorithmClassId /// PlatformId /// Algorithm_Parameters /// Algorithm_Results /// /// /// New s or s will not be /// created but have to be pre-existing. /// /// /// The algorithm. public void UpdateCompleteAlgorithm(Algorithm algorithm) { using (OKBDataContext okb = new OKBDataContext()) { Algorithm original = okb.Algorithms.Single(a => a.Id == algorithm.Id); UpdateAlgorithmData(algorithm, original, okb); okb.SubmitChanges(); } } /// /// Updates the problem object graph including the following properties and linked entities: /// /// Name /// Description /// ProblemClassId /// PlatformId /// SolutionRepresentationId /// IntProblemCharacteristicValues.Value /// FloatProblemCharacteristicValues.Value /// CharProblemCharacteristicValues.Value /// Problem_Parameters /// /// /// New s or s will /// not be created but have to be pre-existing. /// /// /// The problem. public void UpdateCompleteProblem(Problem problem) { using (OKBDataContext okb = new OKBDataContext()) { Problem originalProblem = okb.Problems.Single(p => p.Id == problem.Id); UpdateProblemData(problem, originalProblem, okb); okb.SubmitChanges(); } } private static void UpdateAlgorithmData(Algorithm algorithm, Algorithm original, OKBDataContext okb) { original.Name = algorithm.Name; original.Description = algorithm.Description; original.AlgorithmClassId = algorithm.AlgorithmClassId; original.PlatformId = algorithm.PlatformId; okb.AlgorithmUsers.DeleteAllOnSubmit(original.AlgorithmUsers); original.AlgorithmUsers.Clear(); foreach (var u in algorithm.AlgorithmUsers) { original.AlgorithmUsers.Add(new AlgorithmUser() { AlgorithmId = original.Id, UserId = u.UserId }); } } private static void UpdateProblemData(Problem problem, Problem original, OKBDataContext okb) { original.Name = problem.Name; original.Description = problem.Description; original.ProblemClassId = problem.ProblemClassId; original.SolutionRepresentationId = problem.SolutionRepresentationId; original.PlatformId = problem.PlatformId; okb.ProblemUsers.DeleteAllOnSubmit(original.ProblemUsers); original.ProblemUsers.Clear(); foreach (var u in problem.ProblemUsers) { original.ProblemUsers.Add(new ProblemUser() { ProblemId = original.Id, UserId = u.UserId }); } }*/ } }