#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.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.ServiceModel;
using HeuristicLab.Services.OKB.DataAccess;
namespace HeuristicLab.Services.OKB {
///
/// Implementation of
///
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class AdminService : IAdminService {
public AlgorithmClass GetAlgorithmClass(long algorithmClassId) {
using (OKBDataContext okb = new OKBDataContext()) {
return okb.AlgorithmClasses.FirstOrDefault(a => a.Id == algorithmClassId);
}
}
public IEnumerable GetAlgorithmClasses() {
using (OKBDataContext okb = new OKBDataContext()) {
return okb.AlgorithmClasses.ToArray();
}
}
public void StoreAlgorithmClass(AlgorithmClass algorithmClass) {
using (OKBDataContext okb = new OKBDataContext()) {
AlgorithmClass original = okb.AlgorithmClasses.FirstOrDefault(a => a.Id == algorithmClass.Id);
if (original == null) {
okb.AlgorithmClasses.InsertOnSubmit(new AlgorithmClass() {
Name = algorithmClass.Name,
Description = algorithmClass.Description
});
} else {
original.Name = algorithmClass.Name;
original.Description = algorithmClass.Description;
}
okb.SubmitChanges();
}
}
public void DeleteAlgorithmClass(long algorithmClassId) {
using (OKBDataContext okb = new OKBDataContext()) {
okb.AlgorithmClasses.DeleteOnSubmit(okb.AlgorithmClasses.First(a => a.Id == algorithmClassId));
okb.SubmitChanges();
}
}
public Algorithm GetAlgorithm(long algorithmId) {
using (OKBDataContext okb = new OKBDataContext()) {
return okb.Algorithms.FirstOrDefault(a => a.Id == algorithmId);
}
}
public IEnumerable GetAlgorithms() {
using (OKBDataContext okb = new OKBDataContext()) {
return okb.Algorithms.ToArray();
}
}
public void StoreAlgorithm(Algorithm algorithm) {
using (OKBDataContext okb = new OKBDataContext()) {
Algorithm original = okb.Algorithms.FirstOrDefault(a => a.Id == algorithm.Id);
if (original == null) {
okb.Algorithms.InsertOnSubmit(new Algorithm() {
Name = algorithm.Name,
Description = algorithm.Description
});
} else {
original.Name = algorithm.Name;
original.Description = algorithm.Description;
}
okb.SubmitChanges();
}
}
public void DeleteAlgorithm(long algorithmId) {
using (OKBDataContext okb = new OKBDataContext()) {
okb.Algorithms.DeleteOnSubmit(okb.Algorithms.First(a => a.Id == algorithmId));
okb.SubmitChanges();
}
}
///
/// Gets all available platforms.
///
/// A list of s.
public Platform[] GetPlatforms() {
using (OKBDataContext okb = new OKBDataContext()) {
return okb.Platforms.ToArray();
}
}
///
/// 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
});
}
}
}
}