Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.OKB/3.3/AdminService.cs @ 4279

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

Integrated OKB services (#1166)

File size: 9.8 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.Data.Linq;
23using System.Linq;
24using System.ServiceModel;
25using HeuristicLab.Services.OKB.DataAccess;
26using log4net;
27
28namespace HeuristicLab.Services.OKB {
29
30  /// <summary>
31  /// Implementation of <see cref="IAdminService"/>
32  /// </summary>
33  [ServiceBehavior(
34    InstanceContextMode = InstanceContextMode.PerSession,
35    IncludeExceptionDetailInFaults = true)]
36  public class AdminService : IAdminService {
37
38    private static ILog logger = LogManager.GetLogger(typeof(AdminService));
39
40    private OKBDataContext GetDataContext() {
41      return new OKBDataContext();
42    }
43
44    /// <summary>
45    /// Gets all available platforms.
46    /// </summary>
47    /// <returns>A list of <see cref="Platform"/>s.</returns>
48    public Platform[] GetPlatformNames() {
49      OKBDataContext okb = GetDataContext();
50      return okb.Platforms.ToArray();
51    }
52
53    /// <summary>
54    /// Gets the complete algorithm object graph up to the following entities:
55    /// <list type="bullet">
56    ///     <item>Parameter</item>
57    ///     <item>Algorithm_Paramters.Parameter.DataType</item>
58    ///     <item>Algorithm_Results.Result.DataType</item>
59    ///   </list>
60    /// </summary>
61    /// <param name="id">The algorithm id.</param>
62    /// <returns>An <see cref="Algorithm"/></returns>
63    public Algorithm GetCompleteAlgorithm(int id) {
64      OKBDataContext okb = GetDataContext();
65      var dlo = new DataLoadOptions();
66      dlo.LoadWith<Algorithm>(a => a.Platform);
67      dlo.LoadWith<Algorithm>(a => a.Algorithm_Parameters);
68      dlo.LoadWith<Algorithm>(a => a.Algorithm_Results);
69      dlo.LoadWith<Algorithm_Parameter>(ap => ap.Parameter);
70      dlo.LoadWith<Algorithm_Result>(ar => ar.Result);
71      dlo.LoadWith<Parameter>(p => p.DataType);
72      dlo.LoadWith<Result>(r => r.DataType);
73      okb.LoadOptions = dlo;
74      return okb.Algorithms.Single(a => a.Id == id);
75    }
76
77    /// <summary>
78    /// Gets the complete problem object graph up to the following entities:
79    /// <list type="bullet">
80    ///     <item>Platform</item>
81    ///     <item>SolutionRepresentation</item>
82    ///     <item>Problem_Parameters.Parameter</item>
83    ///     <item>IntProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
84    ///     <item>FloatProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
85    ///     <item>CharProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
86    ///   </list>
87    /// </summary>
88    /// <param name="id">The problem id.</param>
89    /// <returns>A <see cref="Problem"/></returns>
90    public Problem GetCompleteProblem(int id) {
91      OKBDataContext okb = GetDataContext();
92      var dlo = new DataLoadOptions();
93      dlo.LoadWith<Problem>(p => p.Platform);
94      dlo.LoadWith<Problem>(p => p.SolutionRepresentation);
95      dlo.LoadWith<Problem>(p => p.Problem_Parameters);
96      dlo.LoadWith<Problem>(p => p.IntProblemCharacteristicValues);
97      dlo.LoadWith<Problem>(p => p.FloatProblemCharacteristicValues);
98      dlo.LoadWith<Problem>(p => p.CharProblemCharacteristicValues);
99      dlo.LoadWith<Problem_Parameter>(ap => ap.Parameter);
100      dlo.LoadWith<IntProblemCharacteristicValue>(ipcv => ipcv.ProblemCharacteristic);
101      dlo.LoadWith<FloatProblemCharacteristicValue>(fpcv => fpcv.ProblemCharacteristic);
102      dlo.LoadWith<CharProblemCharacteristicValue>(cpcv => cpcv.ProblemCharacteristic);
103      dlo.LoadWith<Parameter>(p => p.DataType);
104      dlo.LoadWith<ProblemCharacteristic>(pc => pc.DataType);
105      okb.LoadOptions = dlo;
106      return okb.Problems.Single(p => p.Id == id);
107    }
108
109    /// <summary>
110    /// Updates the algorithm object graph including the following properties and linked entitites:
111    /// <list type="bullet">
112    ///     <item>Name</item>
113    ///     <item>Description</item>
114    ///     <item>AlgorithmClassId</item>
115    ///     <item>PlatformId</item>
116    ///     <item>Algorithm_Parameters</item>
117    ///     <item>Algorithm_Results</item>
118    ///   </list>
119    ///   <remarks>
120    /// New <see cref="Parameter"/>s or <see cref="Result"/>s will not be
121    /// created but have to be pre-existing.
122    /// </remarks>
123    /// </summary>
124    /// <param name="algorithm">The algorithm.</param>
125    public void UpdateCompleteAlgorithm(Algorithm algorithm) {
126      OKBDataContext okb = GetDataContext();
127      Algorithm original = okb.Algorithms.Single(a => a.Id == algorithm.Id);
128      original.Name = algorithm.Name;
129      original.Description = algorithm.Description;
130      original.AlgorithmClassId = algorithm.AlgorithmClassId;
131      original.PlatformId = algorithm.PlatformId;
132      okb.Algorithm_Parameters.DeleteAllOnSubmit(original.Algorithm_Parameters);
133      foreach (var a_p in algorithm.Algorithm_Parameters)
134        original.Algorithm_Parameters.Add(new Algorithm_Parameter() {
135          AlgorithmId = original.Id,
136          ParameterId = a_p.ParameterId,
137        });
138      okb.Algorithm_Results.DeleteAllOnSubmit(original.Algorithm_Results);
139      foreach (var a_r in algorithm.Algorithm_Results)
140        original.Algorithm_Results.Add(new Algorithm_Result() {
141          AlgorithmId = original.Id,
142          ResultId = a_r.ResultId,
143        });
144      logger.Info("Updating algorithm implementation for " + original.Name);
145      okb.SubmitChanges();
146    }
147
148    /// <summary>
149    /// Updates the problem object graph including the following properties and linked entities:
150    /// <list type="bullet">
151    ///     <item>Name</item>
152    ///     <item>Description</item>
153    ///     <item>ProblemClassId</item>
154    ///     <item>PlatformId</item>
155    ///     <item>SolutionRepresentationId</item>
156    ///     <item>IntProblemCharacteristicValues.Value</item>
157    ///     <item>FloatProblemCharacteristicValues.Value</item>
158    ///     <item>CharProblemCharacteristicValues.Value</item>
159    ///     <item>Problem_Parameters</item>
160    ///   </list>
161    ///   <remarks>
162    /// New <see cref="ProblemCharacteristic"/>s or <see cref="Parameter"/>s will
163    /// not be created but have to be pre-existing.
164    /// </remarks>
165    /// </summary>
166    /// <param name="problem">The problem.</param>
167    public void UpdateCompleteProblem(Problem problem) {
168      OKBDataContext okb = GetDataContext();
169      Problem originalProblem = okb.Problems.Single(p => p.Id == problem.Id);
170      UpdateProblemMasterInfo(problem, originalProblem);
171      UpdateProblemCharacteristics(problem, okb, originalProblem);
172      UpdateProblemParameters(problem, okb, originalProblem);
173      logger.Info("Updating problem " + originalProblem.Name);
174      okb.SubmitChanges();
175    }
176
177    private static void UpdateProblemParameters(Problem problem, OKBDataContext okb, Problem originalProblem) {
178      okb.Problem_Parameters.DeleteAllOnSubmit(originalProblem.Problem_Parameters);
179      originalProblem.Problem_Parameters.Clear();
180      foreach (var p_p in problem.Problem_Parameters) {
181        originalProblem.Problem_Parameters.Add(new Problem_Parameter() {
182          ProblemId = originalProblem.Id,
183          ParameterId = p_p.ParameterId,
184        });
185      }
186    }
187
188    private static void UpdateProblemCharacteristics(Problem problem, OKBDataContext okb, Problem originalProblem) {
189      okb.IntProblemCharacteristicValues.DeleteAllOnSubmit(originalProblem.IntProblemCharacteristicValues);
190      okb.FloatProblemCharacteristicValues.DeleteAllOnSubmit(originalProblem.FloatProblemCharacteristicValues);
191      okb.CharProblemCharacteristicValues.DeleteAllOnSubmit(originalProblem.CharProblemCharacteristicValues);
192      originalProblem.IntProblemCharacteristicValues.Clear();
193      originalProblem.FloatProblemCharacteristicValues.Clear();
194      originalProblem.CharProblemCharacteristicValues.Clear();
195      foreach (var ipc in problem.IntProblemCharacteristicValues) {
196        originalProblem.IntProblemCharacteristicValues.Add(new IntProblemCharacteristicValue() {
197          ProblemId = originalProblem.Id,
198          ProblemCharacteristicId = ipc.ProblemCharacteristicId,
199          Value = ipc.Value,
200        });
201      }
202      foreach (var fpc in problem.FloatProblemCharacteristicValues) {
203        originalProblem.FloatProblemCharacteristicValues.Add(new FloatProblemCharacteristicValue() {
204          ProblemId = originalProblem.Id,
205          ProblemCharacteristicId = fpc.ProblemCharacteristicId,
206          Value = fpc.Value,
207        });
208      }
209      foreach (var cpc in problem.CharProblemCharacteristicValues) {
210        originalProblem.CharProblemCharacteristicValues.Add(new CharProblemCharacteristicValue() {
211          ProblemId = originalProblem.Id,
212          ProblemCharacteristicId = cpc.ProblemCharacteristicId,
213          Value = cpc.Value,
214        });
215      }
216    }
217
218    private static void UpdateProblemMasterInfo(Problem problem, Problem originalProblem) {
219      originalProblem.Name = problem.Name;
220      originalProblem.Description = problem.Description;
221      originalProblem.ProblemClassId = problem.ProblemClassId;
222      originalProblem.PlatformId = problem.PlatformId;
223      originalProblem.SolutionRepresentationId = problem.SolutionRepresentationId;
224    }
225  }
226}
Note: See TracBrowser for help on using the repository browser.