Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Services.OKB/3.3/OKBService.cs @ 5071

Last change on this file since 5071 was 5071, checked in by swagner, 13 years ago

Worked on OKB (#1174)

File size: 31.5 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;
28
29namespace HeuristicLab.Services.OKB {
30  /// <summary>
31  /// Implementation of the OKB service (interface <see cref="IOKBService"/>).
32  /// </summary>
33  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
34  public class OKBService : IOKBService {
35    #region Platform Methods
36    public DataTransfer.Platform GetPlatform(long id) {
37      using (OKBDataContext okb = new OKBDataContext()) {
38        return Convert.ToDto(okb.Platforms.FirstOrDefault(x => x.Id == id));
39      }
40    }
41    public IEnumerable<DataTransfer.Platform> GetPlatforms() {
42      using (OKBDataContext okb = new OKBDataContext()) {
43        return okb.Platforms.Select(x => Convert.ToDto(x)).ToArray();
44      }
45    }
46    public long AddPlatform(DataTransfer.Platform dto) {
47      using (OKBDataContext okb = new OKBDataContext()) {
48        DataAccess.Platform entity = Convert.ToEntity(dto); entity.Id = 0;
49        okb.Platforms.InsertOnSubmit(entity);
50        okb.SubmitChanges();
51        return entity.Id;
52      }
53    }
54    public void UpdatePlatform(DataTransfer.Platform dto) {
55      using (OKBDataContext okb = new OKBDataContext()) {
56        DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == dto.Id);
57        Convert.ToEntity(dto, entity);
58        okb.SubmitChanges();
59      }
60    }
61    public void DeletePlatform(long id) {
62      using (OKBDataContext okb = new OKBDataContext()) {
63        DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == id);
64        if (entity != null) okb.Platforms.DeleteOnSubmit(entity);
65        okb.SubmitChanges();
66      }
67    }
68    #endregion
69
70    #region DataType Methods
71    public DataTransfer.DataType GetDataType(long id) {
72      using (OKBDataContext okb = new OKBDataContext()) {
73        return Convert.ToDto(okb.DataTypes.FirstOrDefault(x => x.Id == id));
74      }
75    }
76    public IEnumerable<DataTransfer.DataType> GetDataTypes() {
77      using (OKBDataContext okb = new OKBDataContext()) {
78        return okb.DataTypes.Select(x => Convert.ToDto(x)).ToArray();
79      }
80    }
81    public long AddDataType(DataTransfer.DataType dto) {
82      using (OKBDataContext okb = new OKBDataContext()) {
83        DataAccess.DataType entity = Convert.ToEntity(dto); entity.Id = 0;
84        okb.DataTypes.InsertOnSubmit(entity);
85        okb.SubmitChanges();
86        return entity.Id;
87      }
88    }
89    public void UpdateDataType(DataTransfer.DataType dto) {
90      using (OKBDataContext okb = new OKBDataContext()) {
91        DataAccess.DataType entity = okb.DataTypes.FirstOrDefault(x => x.Id == dto.Id);
92        Convert.ToEntity(dto, entity);
93        okb.SubmitChanges();
94      }
95    }
96    public void DeleteDataType(long id) {
97      using (OKBDataContext okb = new OKBDataContext()) {
98        DataAccess.DataType entity = okb.DataTypes.FirstOrDefault(x => x.Id == id);
99        if (entity != null) okb.DataTypes.DeleteOnSubmit(entity);
100        okb.SubmitChanges();
101      }
102    }
103    #endregion
104
105    #region AlgorithmClass Methods
106    public DataTransfer.AlgorithmClass GetAlgorithmClass(long id) {
107      using (OKBDataContext okb = new OKBDataContext()) {
108        return Convert.ToDto(okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id));
109      }
110    }
111    public IEnumerable<DataTransfer.AlgorithmClass> GetAlgorithmClasses() {
112      using (OKBDataContext okb = new OKBDataContext()) {
113        return okb.AlgorithmClasses.Select(x => Convert.ToDto(x)).ToArray();
114      }
115    }
116    public long AddAlgorithmClass(DataTransfer.AlgorithmClass dto) {
117      using (OKBDataContext okb = new OKBDataContext()) {
118        DataAccess.AlgorithmClass entity = Convert.ToEntity(dto); entity.Id = 0;
119        okb.AlgorithmClasses.InsertOnSubmit(entity);
120        okb.SubmitChanges();
121        return entity.Id;
122      }
123    }
124    public void UpdateAlgorithmClass(DataTransfer.AlgorithmClass dto) {
125      using (OKBDataContext okb = new OKBDataContext()) {
126        DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == dto.Id);
127        Convert.ToEntity(dto, entity);
128        okb.SubmitChanges();
129      }
130    }
131    public void DeleteAlgorithmClass(long id) {
132      using (OKBDataContext okb = new OKBDataContext()) {
133        DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id);
134        if (entity != null) okb.AlgorithmClasses.DeleteOnSubmit(entity);
135        okb.SubmitChanges();
136      }
137    }
138    #endregion
139
140    #region Algorithm Methods
141    public DataTransfer.Algorithm GetAlgorithm(long id) {
142      using (OKBDataContext okb = new OKBDataContext()) {
143        return Convert.ToDto(okb.Algorithms.FirstOrDefault(x => x.Id == id));
144      }
145    }
146    public IEnumerable<DataTransfer.Algorithm> GetAlgorithms() {
147      using (OKBDataContext okb = new OKBDataContext()) {
148        return okb.Algorithms.Select(x => Convert.ToDto(x)).ToArray();
149      }
150    }
151    public long AddAlgorithm(DataTransfer.Algorithm dto) {
152      using (OKBDataContext okb = new OKBDataContext()) {
153        DataAccess.Algorithm entity = Convert.ToEntity(dto); entity.Id = 0;
154        okb.Algorithms.InsertOnSubmit(entity);
155        okb.SubmitChanges();
156        return entity.Id;
157      }
158    }
159    public void UpdateAlgorithm(DataTransfer.Algorithm dto) {
160      using (OKBDataContext okb = new OKBDataContext()) {
161        DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == dto.Id);
162        Convert.ToEntity(dto, entity);
163        okb.SubmitChanges();
164      }
165    }
166    public void DeleteAlgorithm(long id) {
167      using (OKBDataContext okb = new OKBDataContext()) {
168        DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == id);
169        if (entity != null) okb.Algorithms.DeleteOnSubmit(entity);
170        okb.SubmitChanges();
171      }
172    }
173    public IEnumerable<Guid> GetAlgorithmUsers(long algorithmId) {
174      using (OKBDataContext okb = new OKBDataContext()) {
175        return okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId).Select(x => x.UserId).ToArray();
176      }
177    }
178    public void UpdateAlgorithmUsers(long algorithmId, IEnumerable<Guid> users) {
179      using (OKBDataContext okb = new OKBDataContext()) {
180        okb.AlgorithmUsers.DeleteAllOnSubmit(okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId));
181        okb.AlgorithmUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.AlgorithmUser { AlgorithmId = algorithmId, UserId = x }));
182        okb.SubmitChanges();
183      }
184    }
185    #endregion
186
187    #region AlgorithmData Methods
188    public DataTransfer.AlgorithmData GetAlgorithmData(long algorithmId) {
189      using (OKBDataContext okb = new OKBDataContext()) {
190        return Convert.ToDto(okb.AlgorithmDatas.FirstOrDefault(x => x.AlgorithmId == algorithmId));
191      }
192    }
193    public void UpdateAlgorithmData(DataTransfer.AlgorithmData dto) {
194      using (OKBDataContext okb = new OKBDataContext()) {
195        DataAccess.AlgorithmData entity = okb.AlgorithmDatas.FirstOrDefault(x => x.AlgorithmId == dto.AlgorithmId);
196        if (entity == null) okb.AlgorithmDatas.InsertOnSubmit(Convert.ToEntity(dto));
197        else Convert.ToEntity(dto, entity);
198        okb.SubmitChanges();
199      }
200    }
201    #endregion
202
203    #region AlgorithmParameter Methods
204    public DataTransfer.AlgorithmParameter GetAlgorithmParameter(long id) {
205      using (OKBDataContext okb = new OKBDataContext()) {
206        return Convert.ToDto(okb.AlgorithmParameters.FirstOrDefault(x => x.Id == id));
207      }
208    }
209    public IEnumerable<DataTransfer.AlgorithmParameter> GetAlgorithmParameters(long algorithmId) {
210      using (OKBDataContext okb = new OKBDataContext()) {
211        return okb.AlgorithmParameters.Where(x => x.AlgorithmId == algorithmId).Select(x => Convert.ToDto(x)).ToArray();
212      }
213    }
214    public long AddAlgorithmParameter(DataTransfer.AlgorithmParameter dto) {
215      using (OKBDataContext okb = new OKBDataContext()) {
216        DataAccess.AlgorithmParameter entity = Convert.ToEntity(dto); entity.Id = 0;
217        okb.AlgorithmParameters.InsertOnSubmit(entity);
218        okb.SubmitChanges();
219        return entity.Id;
220      }
221    }
222    public void UpdateAlgorithmParameter(DataTransfer.AlgorithmParameter dto) {
223      using (OKBDataContext okb = new OKBDataContext()) {
224        DataAccess.AlgorithmParameter entity = okb.AlgorithmParameters.FirstOrDefault(x => x.Id == dto.Id);
225        Convert.ToEntity(dto, entity);
226        okb.SubmitChanges();
227      }
228    }
229    public void DeleteAlgorithmParameter(long id) {
230      using (OKBDataContext okb = new OKBDataContext()) {
231        DataAccess.AlgorithmParameter entity = okb.AlgorithmParameters.FirstOrDefault(x => x.Id == id);
232        if (entity != null) okb.AlgorithmParameters.DeleteOnSubmit(entity);
233        okb.SubmitChanges();
234      }
235    }
236    #endregion
237
238    #region ProblemClass Methods
239    public DataTransfer.ProblemClass GetProblemClass(long id) {
240      using (OKBDataContext okb = new OKBDataContext()) {
241        return Convert.ToDto(okb.ProblemClasses.FirstOrDefault(x => x.Id == id));
242      }
243    }
244    public IEnumerable<DataTransfer.ProblemClass> GetProblemClasses() {
245      using (OKBDataContext okb = new OKBDataContext()) {
246        return okb.ProblemClasses.Select(x => Convert.ToDto(x)).ToArray();
247      }
248    }
249    public long AddProblemClass(DataTransfer.ProblemClass dto) {
250      using (OKBDataContext okb = new OKBDataContext()) {
251        DataAccess.ProblemClass entity = Convert.ToEntity(dto); entity.Id = 0;
252        okb.ProblemClasses.InsertOnSubmit(entity);
253        okb.SubmitChanges();
254        return entity.Id;
255      }
256    }
257    public void UpdateProblemClass(DataTransfer.ProblemClass dto) {
258      using (OKBDataContext okb = new OKBDataContext()) {
259        DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == dto.Id);
260        Convert.ToEntity(dto, entity);
261        okb.SubmitChanges();
262      }
263    }
264    public void DeleteProblemClass(long id) {
265      using (OKBDataContext okb = new OKBDataContext()) {
266        DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == id);
267        if (entity != null) okb.ProblemClasses.DeleteOnSubmit(entity);
268        okb.SubmitChanges();
269      }
270    }
271    #endregion
272
273    #region Problem Methods
274    public DataTransfer.Problem GetProblem(long id) {
275      using (OKBDataContext okb = new OKBDataContext()) {
276        return Convert.ToDto(okb.Problems.FirstOrDefault(x => x.Id == id));
277      }
278    }
279    public IEnumerable<DataTransfer.Problem> GetProblems() {
280      using (OKBDataContext okb = new OKBDataContext()) {
281        return okb.Problems.Select(x => Convert.ToDto(x)).ToArray();
282      }
283    }
284    public long AddProblem(DataTransfer.Problem dto) {
285      using (OKBDataContext okb = new OKBDataContext()) {
286        DataAccess.Problem entity = Convert.ToEntity(dto); entity.Id = 0;
287        okb.Problems.InsertOnSubmit(entity);
288        okb.SubmitChanges();
289        return entity.Id;
290      }
291    }
292    public void UpdateProblem(DataTransfer.Problem dto) {
293      using (OKBDataContext okb = new OKBDataContext()) {
294        DataAccess.Problem entity = okb.Problems.FirstOrDefault(x => x.Id == dto.Id);
295        Convert.ToEntity(dto, entity);
296        okb.SubmitChanges();
297      }
298    }
299    public void DeleteProblem(long id) {
300      using (OKBDataContext okb = new OKBDataContext()) {
301        DataAccess.Problem entity = okb.Problems.FirstOrDefault(x => x.Id == id);
302        if (entity != null) okb.Problems.DeleteOnSubmit(entity);
303        okb.SubmitChanges();
304      }
305    }
306    public IEnumerable<Guid> GetProblemUsers(long problemId) {
307      using (OKBDataContext okb = new OKBDataContext()) {
308        return okb.ProblemUsers.Where(x => x.ProblemId == problemId).Select(x => x.UserId).ToArray();
309      }
310    }
311    public void UpdateProblemUsers(long problemId, IEnumerable<Guid> users) {
312      using (OKBDataContext okb = new OKBDataContext()) {
313        okb.ProblemUsers.DeleteAllOnSubmit(okb.ProblemUsers.Where(x => x.ProblemId == problemId));
314        okb.ProblemUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.ProblemUser { ProblemId = problemId, UserId = x }));
315        okb.SubmitChanges();
316      }
317    }
318    #endregion
319
320    #region ProblemData Methods
321    public DataTransfer.ProblemData GetProblemData(long problemId) {
322      using (OKBDataContext okb = new OKBDataContext()) {
323        return Convert.ToDto(okb.ProblemDatas.FirstOrDefault(x => x.ProblemId == problemId));
324      }
325    }
326    public void UpdateProblemData(DataTransfer.ProblemData dto) {
327      using (OKBDataContext okb = new OKBDataContext()) {
328        DataAccess.ProblemData entity = okb.ProblemDatas.FirstOrDefault(x => x.ProblemId == dto.ProblemId);
329        if (entity == null) okb.ProblemDatas.InsertOnSubmit(Convert.ToEntity(dto));
330        else Convert.ToEntity(dto, entity);
331        okb.SubmitChanges();
332      }
333    }
334    #endregion
335
336    #region ProblemParameter Methods
337    public DataTransfer.ProblemParameter GetProblemParameter(long id) {
338      using (OKBDataContext okb = new OKBDataContext()) {
339        return Convert.ToDto(okb.ProblemParameters.FirstOrDefault(x => x.Id == id));
340      }
341    }
342    public IEnumerable<DataTransfer.ProblemParameter> GetProblemParameters(long problemId) {
343      using (OKBDataContext okb = new OKBDataContext()) {
344        return okb.ProblemParameters.Where(x => x.ProblemId == problemId).Select(x => Convert.ToDto(x)).ToArray();
345      }
346    }
347    public long AddProblemParameter(DataTransfer.ProblemParameter dto) {
348      using (OKBDataContext okb = new OKBDataContext()) {
349        DataAccess.ProblemParameter entity = Convert.ToEntity(dto); entity.Id = 0;
350        okb.ProblemParameters.InsertOnSubmit(entity);
351        okb.SubmitChanges();
352        return entity.Id;
353      }
354    }
355    public void UpdateProblemParameter(DataTransfer.ProblemParameter dto) {
356      using (OKBDataContext okb = new OKBDataContext()) {
357        DataAccess.ProblemParameter entity = okb.ProblemParameters.FirstOrDefault(x => x.Id == dto.Id);
358        Convert.ToEntity(dto, entity);
359        okb.SubmitChanges();
360      }
361    }
362    public void DeleteProblemParameter(long id) {
363      using (OKBDataContext okb = new OKBDataContext()) {
364        DataAccess.ProblemParameter entity = okb.ProblemParameters.FirstOrDefault(x => x.Id == id);
365        if (entity != null) okb.ProblemParameters.DeleteOnSubmit(entity);
366        okb.SubmitChanges();
367      }
368    }
369    #endregion
370
371    #region Result Methods
372    public DataTransfer.Result GetResult(long id) {
373      using (OKBDataContext okb = new OKBDataContext()) {
374        return Convert.ToDto(okb.Results.FirstOrDefault(x => x.Id == id));
375      }
376    }
377    public IEnumerable<DataTransfer.Result> GetResults(long algorithmId) {
378      using (OKBDataContext okb = new OKBDataContext()) {
379        return okb.Results.Where(x => x.AlgorithmId == algorithmId).Select(x => Convert.ToDto(x)).ToArray();
380      }
381    }
382    public long AddResult(DataTransfer.Result dto) {
383      using (OKBDataContext okb = new OKBDataContext()) {
384        DataAccess.Result entity = Convert.ToEntity(dto); entity.Id = 0;
385        okb.Results.InsertOnSubmit(entity);
386        okb.SubmitChanges();
387        return entity.Id;
388      }
389    }
390    public void UpdateResult(DataTransfer.Result dto) {
391      using (OKBDataContext okb = new OKBDataContext()) {
392        DataAccess.Result entity = okb.Results.FirstOrDefault(x => x.Id == dto.Id);
393        Convert.ToEntity(dto, entity);
394        okb.SubmitChanges();
395      }
396    }
397    public void DeleteResult(long id) {
398      using (OKBDataContext okb = new OKBDataContext()) {
399        DataAccess.Result entity = okb.Results.FirstOrDefault(x => x.Id == id);
400        if (entity != null) okb.Results.DeleteOnSubmit(entity);
401        okb.SubmitChanges();
402      }
403    }
404    #endregion
405
406    #region Experiment Methods
407    public DataTransfer.Experiment GetExperiment(long id) {
408      using (OKBDataContext okb = new OKBDataContext()) {
409        DataLoadOptions dlo = new DataLoadOptions();
410        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterBlobValues);
411        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterBoolValues);
412        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterFloatValues);
413        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterIntValues);
414        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterStringValues);
415        dlo.LoadWith<Experiment>(x => x.ProblemParameterBlobValues);
416        dlo.LoadWith<Experiment>(x => x.ProblemParameterBoolValues);
417        dlo.LoadWith<Experiment>(x => x.ProblemParameterFloatValues);
418        dlo.LoadWith<Experiment>(x => x.ProblemParameterIntValues);
419        dlo.LoadWith<Experiment>(x => x.ProblemParameterStringValues);
420        okb.LoadOptions = dlo;
421        return Convert.ToDto(okb.Experiments.FirstOrDefault(x => x.Id == id));
422      }
423    }
424    public IEnumerable<DataTransfer.Experiment> GetExperiments(long algorithmId, long problemId) {
425      using (OKBDataContext okb = new OKBDataContext()) {
426        DataLoadOptions dlo = new DataLoadOptions();
427        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterBlobValues);
428        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterBoolValues);
429        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterFloatValues);
430        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterIntValues);
431        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterStringValues);
432        dlo.LoadWith<Experiment>(x => x.ProblemParameterBlobValues);
433        dlo.LoadWith<Experiment>(x => x.ProblemParameterBoolValues);
434        dlo.LoadWith<Experiment>(x => x.ProblemParameterFloatValues);
435        dlo.LoadWith<Experiment>(x => x.ProblemParameterIntValues);
436        dlo.LoadWith<Experiment>(x => x.ProblemParameterStringValues);
437        okb.LoadOptions = dlo;
438        if ((algorithmId != 0) && (problemId != 0))
439          return okb.Experiments.Where(x => (x.AlgorithmId == algorithmId) && (x.ProblemId == problemId)).Select(x => Convert.ToDto(x)).ToArray();
440        else if (algorithmId != 0)
441          return okb.Experiments.Where(x => x.AlgorithmId == algorithmId).Select(x => Convert.ToDto(x)).ToArray();
442        else if (problemId != 0)
443          return okb.Experiments.Where(x => x.ProblemId == problemId).Select(x => Convert.ToDto(x)).ToArray();
444        else
445          return okb.Experiments.Select(x => Convert.ToDto(x)).ToArray();
446      }
447    }
448    public long AddExperiment(DataTransfer.Experiment dto) {
449      using (OKBDataContext okb = new OKBDataContext()) {
450        DataAccess.Experiment entity = Convert.ToEntity(dto); entity.Id = 0;
451
452        DataLoadOptions dlo = new DataLoadOptions();
453        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterBlobValues);
454        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterBoolValues);
455        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterFloatValues);
456        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterIntValues);
457        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterStringValues);
458        dlo.LoadWith<Experiment>(x => x.ProblemParameterBlobValues);
459        dlo.LoadWith<Experiment>(x => x.ProblemParameterBoolValues);
460        dlo.LoadWith<Experiment>(x => x.ProblemParameterFloatValues);
461        dlo.LoadWith<Experiment>(x => x.ProblemParameterIntValues);
462        dlo.LoadWith<Experiment>(x => x.ProblemParameterStringValues);
463        okb.LoadOptions = dlo;
464
465        var experiments = okb.Experiments.Where(x => ((x.AlgorithmId == entity.AlgorithmId) && (x.ProblemId == entity.ProblemId))).ToArray();
466        ExperimentEqualityComparer comparer = new ExperimentEqualityComparer();
467        Experiment exp = experiments.FirstOrDefault(x => comparer.Equals(x, entity));
468        if (exp != null) {
469          return exp.Id;
470        } else {
471          okb.Experiments.InsertOnSubmit(entity);
472          okb.SubmitChanges();
473          return entity.Id;
474        }
475      }
476    }
477    public void DeleteExperiment(long id) {
478      foreach (DataTransfer.Run run in GetRuns(id))
479        DeleteRun(run.Id);
480
481      using (OKBDataContext okb = new OKBDataContext()) {
482        IEnumerable<DataAccess.AlgorithmParameterBlobValue> algorithmParameterBlobValues = okb.AlgorithmParameterBlobValues.Where(x => x.ExperimentId == id);
483        okb.AlgorithmParameterBlobValues.DeleteAllOnSubmit(algorithmParameterBlobValues);
484        IEnumerable<DataAccess.AlgorithmParameterBoolValue> algorithmParameterBoolValues = okb.AlgorithmParameterBoolValues.Where(x => x.ExperimentId == id);
485        okb.AlgorithmParameterBoolValues.DeleteAllOnSubmit(algorithmParameterBoolValues);
486        IEnumerable<DataAccess.AlgorithmParameterFloatValue> algorithmParameterFloatValues = okb.AlgorithmParameterFloatValues.Where(x => x.ExperimentId == id);
487        okb.AlgorithmParameterFloatValues.DeleteAllOnSubmit(algorithmParameterFloatValues);
488        IEnumerable<DataAccess.AlgorithmParameterIntValue> algorithmParameterIntValues = okb.AlgorithmParameterIntValues.Where(x => x.ExperimentId == id);
489        okb.AlgorithmParameterIntValues.DeleteAllOnSubmit(algorithmParameterIntValues);
490        IEnumerable<DataAccess.AlgorithmParameterStringValue> algorithmParameterStringValues = okb.AlgorithmParameterStringValues.Where(x => x.ExperimentId == id);
491        okb.AlgorithmParameterStringValues.DeleteAllOnSubmit(algorithmParameterStringValues);
492
493        IEnumerable<DataAccess.ProblemParameterBlobValue> problemParameterBlobValues = okb.ProblemParameterBlobValues.Where(x => x.ExperimentId == id);
494        okb.ProblemParameterBlobValues.DeleteAllOnSubmit(problemParameterBlobValues);
495        IEnumerable<DataAccess.ProblemParameterBoolValue> problemParameterBoolValues = okb.ProblemParameterBoolValues.Where(x => x.ExperimentId == id);
496        okb.ProblemParameterBoolValues.DeleteAllOnSubmit(problemParameterBoolValues);
497        IEnumerable<DataAccess.ProblemParameterFloatValue> problemParameterFloatValues = okb.ProblemParameterFloatValues.Where(x => x.ExperimentId == id);
498        okb.ProblemParameterFloatValues.DeleteAllOnSubmit(problemParameterFloatValues);
499        IEnumerable<DataAccess.ProblemParameterIntValue> problemParameterIntValues = okb.ProblemParameterIntValues.Where(x => x.ExperimentId == id);
500        okb.ProblemParameterIntValues.DeleteAllOnSubmit(problemParameterIntValues);
501        IEnumerable<DataAccess.ProblemParameterStringValue> problemParameterStringValues = okb.ProblemParameterStringValues.Where(x => x.ExperimentId == id);
502        okb.ProblemParameterStringValues.DeleteAllOnSubmit(problemParameterStringValues);
503
504        DataAccess.Experiment entity = okb.Experiments.FirstOrDefault(x => x.Id == id);
505        if (entity != null) okb.Experiments.DeleteOnSubmit(entity);
506        okb.SubmitChanges();
507      }
508    }
509    #endregion
510
511    #region Run Methods
512    public DataTransfer.Run GetRun(long id) {
513      using (OKBDataContext okb = new OKBDataContext()) {
514        DataLoadOptions dlo = new DataLoadOptions();
515        dlo.LoadWith<Run>(x => x.ResultBlobValues);
516        dlo.LoadWith<Run>(x => x.ResultBoolValues);
517        dlo.LoadWith<Run>(x => x.ResultFloatValues);
518        dlo.LoadWith<Run>(x => x.ResultIntValues);
519        dlo.LoadWith<Run>(x => x.ResultStringValues);
520        okb.LoadOptions = dlo;
521        return Convert.ToDto(okb.Runs.FirstOrDefault(x => x.Id == id));
522      }
523    }
524    public IEnumerable<DataTransfer.Run> GetRuns(long experimentId) {
525      using (OKBDataContext okb = new OKBDataContext()) {
526        DataLoadOptions dlo = new DataLoadOptions();
527        dlo.LoadWith<Run>(x => x.ResultBlobValues);
528        dlo.LoadWith<Run>(x => x.ResultBoolValues);
529        dlo.LoadWith<Run>(x => x.ResultFloatValues);
530        dlo.LoadWith<Run>(x => x.ResultIntValues);
531        dlo.LoadWith<Run>(x => x.ResultStringValues);
532        okb.LoadOptions = dlo;
533        return okb.Runs.Where(x => x.ExperimentId == experimentId).Select(x => Convert.ToDto(x)).ToArray();
534      }
535    }
536    public long AddRun(DataTransfer.Run dto) {
537      using (OKBDataContext okb = new OKBDataContext()) {
538        DataAccess.Run entity = Convert.ToEntity(dto); entity.Id = 0;
539        okb.Runs.InsertOnSubmit(entity);
540        okb.SubmitChanges();
541        return entity.Id;
542      }
543    }
544    public void DeleteRun(long id) {
545      using (OKBDataContext okb = new OKBDataContext()) {
546        IEnumerable<DataAccess.ResultBlobValue> resultBlobValues = okb.ResultBlobValues.Where(x => x.RunId == id);
547        okb.ResultBlobValues.DeleteAllOnSubmit(resultBlobValues);
548        IEnumerable<DataAccess.ResultBoolValue> resultBoolValues = okb.ResultBoolValues.Where(x => x.RunId == id);
549        okb.ResultBoolValues.DeleteAllOnSubmit(resultBoolValues);
550        IEnumerable<DataAccess.ResultFloatValue> resultFloatValues = okb.ResultFloatValues.Where(x => x.RunId == id);
551        okb.ResultFloatValues.DeleteAllOnSubmit(resultFloatValues);
552        IEnumerable<DataAccess.ResultIntValue> resultIntValues = okb.ResultIntValues.Where(x => x.RunId == id);
553        okb.ResultIntValues.DeleteAllOnSubmit(resultIntValues);
554        IEnumerable<DataAccess.ResultStringValue> resultStringValues = okb.ResultStringValues.Where(x => x.RunId == id);
555        okb.ResultStringValues.DeleteAllOnSubmit(resultStringValues);
556
557        DataAccess.Run entity = okb.Runs.FirstOrDefault(x => x.Id == id);
558        if (entity != null) okb.Runs.DeleteOnSubmit(entity);
559        okb.SubmitChanges();
560      }
561    }
562    #endregion
563
564    /*
565
566    /// <summary>
567    /// Gets the complete algorithm object graph up to the following entities:
568    /// <list type="bullet">
569    ///   <item>Parameter</item>
570    ///   <item>Algorithm_Paramters.Parameter.DataType</item>
571    ///   <item>Algorithm_Results.Result.DataType</item>
572    /// </list>
573    /// </summary>
574    /// <param name="id">The algorithm id.</param>
575    /// <returns>An <see cref="Algorithm"/></returns>
576    public Algorithm GetCompleteAlgorithm(int id) {
577      using (OKBDataContext okb = new OKBDataContext()) {
578        var dlo = new DataLoadOptions();
579        dlo.LoadWith<Algorithm>(a => a.AlgorithmClass);
580        dlo.LoadWith<Algorithm>(a => a.Platform);
581        dlo.LoadWith<Algorithm>(a => a.AlgorithmUsers);
582        dlo.LoadWith<AlgorithmUser>(u => u.User);
583        okb.LoadOptions = dlo;
584        return okb.Algorithms.Single(a => a.Id == id);
585      }
586    }
587
588    /// <summary>
589    /// Gets the complete problem object graph up to the following entities:
590    /// <list type="bullet">
591    ///   <item>Platform</item>
592    ///   <item>SolutionRepresentation</item>
593    ///   <item>Problem_Parameters.Parameter</item>
594    ///   <item>IntProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
595    ///   <item>FloatProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
596    ///   <item>CharProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
597    /// </list>
598    /// </summary>
599    /// <param name="id">The problem id.</param>
600    /// <returns>A <see cref="Problem"/></returns>
601    public Problem GetCompleteProblem(int id) {
602      using (OKBDataContext okb = new OKBDataContext()) {
603        var dlo = new DataLoadOptions();
604        dlo.LoadWith<Problem>(p => p.ProblemClass);
605        dlo.LoadWith<Problem>(p => p.Platform);
606        dlo.LoadWith<Problem>(p => p.ProblemUsers);
607        dlo.LoadWith<ProblemUser>(u => u.User);
608        okb.LoadOptions = dlo;
609        return okb.Problems.Single(p => p.Id == id);
610      }
611    }
612
613    /// <summary>
614    /// Updates the algorithm object graph including the following properties and linked entitites:
615    /// <list type="bullet">
616    ///   <item>Name</item>
617    ///   <item>Description</item>
618    ///   <item>AlgorithmClassId</item>
619    ///   <item>PlatformId</item>
620    ///   <item>Algorithm_Parameters</item>
621    ///   <item>Algorithm_Results</item>
622    /// </list>
623    /// <remarks>
624    /// New <see cref="Parameter"/>s or <see cref="Result"/>s will not be
625    /// created but have to be pre-existing.
626    /// </remarks>
627    /// </summary>
628    /// <param name="algorithm">The algorithm.</param>
629    public void UpdateCompleteAlgorithm(Algorithm algorithm) {
630      using (OKBDataContext okb = new OKBDataContext()) {
631        Algorithm original = okb.Algorithms.Single(a => a.Id == algorithm.Id);
632        UpdateAlgorithmData(algorithm, original, okb);
633        okb.SubmitChanges();
634      }
635    }
636
637    /// <summary>
638    /// Updates the problem object graph including the following properties and linked entities:
639    /// <list type="bullet">
640    ///   <item>Name</item>
641    ///   <item>Description</item>
642    ///   <item>ProblemClassId</item>
643    ///   <item>PlatformId</item>
644    ///   <item>SolutionRepresentationId</item>
645    ///   <item>IntProblemCharacteristicValues.Value</item>
646    ///   <item>FloatProblemCharacteristicValues.Value</item>
647    ///   <item>CharProblemCharacteristicValues.Value</item>
648    ///   <item>Problem_Parameters</item>
649    /// </list>
650    /// <remarks>
651    /// New <see cref="ProblemCharacteristic"/>s or <see cref="Parameter"/>s will
652    /// not be created but have to be pre-existing.
653    /// </remarks>
654    /// </summary>
655    /// <param name="problem">The problem.</param>
656    public void UpdateCompleteProblem(Problem problem) {
657      using (OKBDataContext okb = new OKBDataContext()) {
658        Problem originalProblem = okb.Problems.Single(p => p.Id == problem.Id);
659        UpdateProblemData(problem, originalProblem, okb);
660        okb.SubmitChanges();
661      }
662    }
663
664    private static void UpdateAlgorithmData(Algorithm algorithm, Algorithm original, OKBDataContext okb) {
665      original.Name = algorithm.Name;
666      original.Description = algorithm.Description;
667      original.AlgorithmClassId = algorithm.AlgorithmClassId;
668      original.PlatformId = algorithm.PlatformId;
669      okb.AlgorithmUsers.DeleteAllOnSubmit(original.AlgorithmUsers);
670      original.AlgorithmUsers.Clear();
671      foreach (var u in algorithm.AlgorithmUsers) {
672        original.AlgorithmUsers.Add(new AlgorithmUser() {
673          AlgorithmId = original.Id,
674          UserId = u.UserId
675        });
676      }
677    }
678
679    private static void UpdateProblemData(Problem problem, Problem original, OKBDataContext okb) {
680      original.Name = problem.Name;
681      original.Description = problem.Description;
682      original.ProblemClassId = problem.ProblemClassId;
683      original.SolutionRepresentationId = problem.SolutionRepresentationId;
684      original.PlatformId = problem.PlatformId;
685      okb.ProblemUsers.DeleteAllOnSubmit(original.ProblemUsers);
686      original.ProblemUsers.Clear();
687      foreach (var u in problem.ProblemUsers) {
688        original.ProblemUsers.Add(new ProblemUser() {
689          ProblemId = original.Id,
690          UserId = u.UserId
691        });
692      }
693    }*/
694  }
695}
Note: See TracBrowser for help on using the repository browser.