Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

File size: 28.1 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      using (OKBDataContext okb = new OKBDataContext()) {
479        DataAccess.Experiment entity = okb.Experiments.FirstOrDefault(x => x.Id == id);
480        if (entity != null) okb.Experiments.DeleteOnSubmit(entity);
481        okb.SubmitChanges();
482      }
483    }
484    #endregion
485
486    #region Run Methods
487    public DataTransfer.Run GetRun(long id) {
488      using (OKBDataContext okb = new OKBDataContext()) {
489        DataLoadOptions dlo = new DataLoadOptions();
490        dlo.LoadWith<Run>(x => x.ResultBlobValues);
491        dlo.LoadWith<Run>(x => x.ResultBoolValues);
492        dlo.LoadWith<Run>(x => x.ResultFloatValues);
493        dlo.LoadWith<Run>(x => x.ResultIntValues);
494        dlo.LoadWith<Run>(x => x.ResultStringValues);
495        okb.LoadOptions = dlo;
496        return Convert.ToDto(okb.Runs.FirstOrDefault(x => x.Id == id));
497      }
498    }
499    public IEnumerable<DataTransfer.Run> GetRuns(long experimentId) {
500      using (OKBDataContext okb = new OKBDataContext()) {
501        DataLoadOptions dlo = new DataLoadOptions();
502        dlo.LoadWith<Run>(x => x.ResultBlobValues);
503        dlo.LoadWith<Run>(x => x.ResultBoolValues);
504        dlo.LoadWith<Run>(x => x.ResultFloatValues);
505        dlo.LoadWith<Run>(x => x.ResultIntValues);
506        dlo.LoadWith<Run>(x => x.ResultStringValues);
507        okb.LoadOptions = dlo;
508        return okb.Runs.Where(x => x.ExperimentId == experimentId).Select(x => Convert.ToDto(x)).ToArray();
509      }
510    }
511    public long AddRun(DataTransfer.Run dto) {
512      using (OKBDataContext okb = new OKBDataContext()) {
513        DataAccess.Run entity = Convert.ToEntity(dto); entity.Id = 0;
514        okb.Runs.InsertOnSubmit(entity);
515        okb.SubmitChanges();
516        return entity.Id;
517      }
518    }
519    public void DeleteRun(long id) {
520      using (OKBDataContext okb = new OKBDataContext()) {
521        DataAccess.Run entity = okb.Runs.FirstOrDefault(x => x.Id == id);
522        if (entity != null) okb.Runs.DeleteOnSubmit(entity);
523        okb.SubmitChanges();
524      }
525    }
526    #endregion
527
528    /*
529
530    /// <summary>
531    /// Gets the complete algorithm object graph up to the following entities:
532    /// <list type="bullet">
533    ///   <item>Parameter</item>
534    ///   <item>Algorithm_Paramters.Parameter.DataType</item>
535    ///   <item>Algorithm_Results.Result.DataType</item>
536    /// </list>
537    /// </summary>
538    /// <param name="id">The algorithm id.</param>
539    /// <returns>An <see cref="Algorithm"/></returns>
540    public Algorithm GetCompleteAlgorithm(int id) {
541      using (OKBDataContext okb = new OKBDataContext()) {
542        var dlo = new DataLoadOptions();
543        dlo.LoadWith<Algorithm>(a => a.AlgorithmClass);
544        dlo.LoadWith<Algorithm>(a => a.Platform);
545        dlo.LoadWith<Algorithm>(a => a.AlgorithmUsers);
546        dlo.LoadWith<AlgorithmUser>(u => u.User);
547        okb.LoadOptions = dlo;
548        return okb.Algorithms.Single(a => a.Id == id);
549      }
550    }
551
552    /// <summary>
553    /// Gets the complete problem object graph up to the following entities:
554    /// <list type="bullet">
555    ///   <item>Platform</item>
556    ///   <item>SolutionRepresentation</item>
557    ///   <item>Problem_Parameters.Parameter</item>
558    ///   <item>IntProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
559    ///   <item>FloatProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
560    ///   <item>CharProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
561    /// </list>
562    /// </summary>
563    /// <param name="id">The problem id.</param>
564    /// <returns>A <see cref="Problem"/></returns>
565    public Problem GetCompleteProblem(int id) {
566      using (OKBDataContext okb = new OKBDataContext()) {
567        var dlo = new DataLoadOptions();
568        dlo.LoadWith<Problem>(p => p.ProblemClass);
569        dlo.LoadWith<Problem>(p => p.Platform);
570        dlo.LoadWith<Problem>(p => p.ProblemUsers);
571        dlo.LoadWith<ProblemUser>(u => u.User);
572        okb.LoadOptions = dlo;
573        return okb.Problems.Single(p => p.Id == id);
574      }
575    }
576
577    /// <summary>
578    /// Updates the algorithm object graph including the following properties and linked entitites:
579    /// <list type="bullet">
580    ///   <item>Name</item>
581    ///   <item>Description</item>
582    ///   <item>AlgorithmClassId</item>
583    ///   <item>PlatformId</item>
584    ///   <item>Algorithm_Parameters</item>
585    ///   <item>Algorithm_Results</item>
586    /// </list>
587    /// <remarks>
588    /// New <see cref="Parameter"/>s or <see cref="Result"/>s will not be
589    /// created but have to be pre-existing.
590    /// </remarks>
591    /// </summary>
592    /// <param name="algorithm">The algorithm.</param>
593    public void UpdateCompleteAlgorithm(Algorithm algorithm) {
594      using (OKBDataContext okb = new OKBDataContext()) {
595        Algorithm original = okb.Algorithms.Single(a => a.Id == algorithm.Id);
596        UpdateAlgorithmData(algorithm, original, okb);
597        okb.SubmitChanges();
598      }
599    }
600
601    /// <summary>
602    /// Updates the problem object graph including the following properties and linked entities:
603    /// <list type="bullet">
604    ///   <item>Name</item>
605    ///   <item>Description</item>
606    ///   <item>ProblemClassId</item>
607    ///   <item>PlatformId</item>
608    ///   <item>SolutionRepresentationId</item>
609    ///   <item>IntProblemCharacteristicValues.Value</item>
610    ///   <item>FloatProblemCharacteristicValues.Value</item>
611    ///   <item>CharProblemCharacteristicValues.Value</item>
612    ///   <item>Problem_Parameters</item>
613    /// </list>
614    /// <remarks>
615    /// New <see cref="ProblemCharacteristic"/>s or <see cref="Parameter"/>s will
616    /// not be created but have to be pre-existing.
617    /// </remarks>
618    /// </summary>
619    /// <param name="problem">The problem.</param>
620    public void UpdateCompleteProblem(Problem problem) {
621      using (OKBDataContext okb = new OKBDataContext()) {
622        Problem originalProblem = okb.Problems.Single(p => p.Id == problem.Id);
623        UpdateProblemData(problem, originalProblem, okb);
624        okb.SubmitChanges();
625      }
626    }
627
628    private static void UpdateAlgorithmData(Algorithm algorithm, Algorithm original, OKBDataContext okb) {
629      original.Name = algorithm.Name;
630      original.Description = algorithm.Description;
631      original.AlgorithmClassId = algorithm.AlgorithmClassId;
632      original.PlatformId = algorithm.PlatformId;
633      okb.AlgorithmUsers.DeleteAllOnSubmit(original.AlgorithmUsers);
634      original.AlgorithmUsers.Clear();
635      foreach (var u in algorithm.AlgorithmUsers) {
636        original.AlgorithmUsers.Add(new AlgorithmUser() {
637          AlgorithmId = original.Id,
638          UserId = u.UserId
639        });
640      }
641    }
642
643    private static void UpdateProblemData(Problem problem, Problem original, OKBDataContext okb) {
644      original.Name = problem.Name;
645      original.Description = problem.Description;
646      original.ProblemClassId = problem.ProblemClassId;
647      original.SolutionRepresentationId = problem.SolutionRepresentationId;
648      original.PlatformId = problem.PlatformId;
649      okb.ProblemUsers.DeleteAllOnSubmit(original.ProblemUsers);
650      original.ProblemUsers.Clear();
651      foreach (var u in problem.ProblemUsers) {
652        original.ProblemUsers.Add(new ProblemUser() {
653          ProblemId = original.Id,
654          UserId = u.UserId
655        });
656      }
657    }*/
658  }
659}
Note: See TracBrowser for help on using the repository browser.