Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

File size: 35.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 IEnumerable<DataTransfer.Run> QueryRuns(string query) {
537      using (OKBDataContext okb = new OKBDataContext()) {
538        DataLoadOptions dlo = new DataLoadOptions();
539        dlo.LoadWith<Run>(x => x.ResultBlobValues);
540        dlo.LoadWith<Run>(x => x.ResultBoolValues);
541        dlo.LoadWith<Run>(x => x.ResultFloatValues);
542        dlo.LoadWith<Run>(x => x.ResultIntValues);
543        dlo.LoadWith<Run>(x => x.ResultStringValues);
544        dlo.LoadWith<Run>(x => x.Experiment);
545        dlo.LoadWith<ResultBlobValue>(x => x.Result);
546        dlo.LoadWith<ResultBlobValue>(x => x.DataType);
547        dlo.LoadWith<ResultBoolValue>(x => x.Result);
548        dlo.LoadWith<ResultBoolValue>(x => x.DataType);
549        dlo.LoadWith<ResultFloatValue>(x => x.Result);
550        dlo.LoadWith<ResultFloatValue>(x => x.DataType);
551        dlo.LoadWith<ResultIntValue>(x => x.Result);
552        dlo.LoadWith<ResultIntValue>(x => x.DataType);
553        dlo.LoadWith<ResultStringValue>(x => x.Result);
554        dlo.LoadWith<ResultStringValue>(x => x.DataType);
555        dlo.LoadWith<Result>(x => x.Algorithm);
556        dlo.LoadWith<Result>(x => x.DataType);
557        dlo.LoadWith<Experiment>(x => x.Algorithm);
558        dlo.LoadWith<Experiment>(x => x.Problem);
559        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterBlobValues);
560        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterBoolValues);
561        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterFloatValues);
562        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterIntValues);
563        dlo.LoadWith<Experiment>(x => x.AlgorithmParameterStringValues);
564        dlo.LoadWith<Experiment>(x => x.ProblemParameterBlobValues);
565        dlo.LoadWith<Experiment>(x => x.ProblemParameterBoolValues);
566        dlo.LoadWith<Experiment>(x => x.ProblemParameterFloatValues);
567        dlo.LoadWith<Experiment>(x => x.ProblemParameterIntValues);
568        dlo.LoadWith<Experiment>(x => x.ProblemParameterStringValues);
569        dlo.LoadWith<AlgorithmParameterBlobValue>(x => x.AlgorithmParameter);
570        dlo.LoadWith<AlgorithmParameterBlobValue>(x => x.DataType);
571        dlo.LoadWith<AlgorithmParameterBoolValue>(x => x.AlgorithmParameter);
572        dlo.LoadWith<AlgorithmParameterBoolValue>(x => x.DataType);
573        dlo.LoadWith<AlgorithmParameterFloatValue>(x => x.AlgorithmParameter);
574        dlo.LoadWith<AlgorithmParameterFloatValue>(x => x.DataType);
575        dlo.LoadWith<AlgorithmParameterIntValue>(x => x.AlgorithmParameter);
576        dlo.LoadWith<AlgorithmParameterIntValue>(x => x.DataType);
577        dlo.LoadWith<AlgorithmParameterStringValue>(x => x.AlgorithmParameter);
578        dlo.LoadWith<AlgorithmParameterStringValue>(x => x.DataType);
579        dlo.LoadWith<ProblemParameterBlobValue>(x => x.ProblemParameter);
580        dlo.LoadWith<ProblemParameterBlobValue>(x => x.DataType);
581        dlo.LoadWith<ProblemParameterBoolValue>(x => x.ProblemParameter);
582        dlo.LoadWith<ProblemParameterBoolValue>(x => x.DataType);
583        dlo.LoadWith<ProblemParameterFloatValue>(x => x.ProblemParameter);
584        dlo.LoadWith<ProblemParameterFloatValue>(x => x.DataType);
585        dlo.LoadWith<ProblemParameterIntValue>(x => x.ProblemParameter);
586        dlo.LoadWith<ProblemParameterIntValue>(x => x.DataType);
587        dlo.LoadWith<ProblemParameterStringValue>(x => x.ProblemParameter);
588        dlo.LoadWith<ProblemParameterStringValue>(x => x.DataType);
589        dlo.LoadWith<AlgorithmParameter>(x => x.Algorithm);
590        dlo.LoadWith<AlgorithmParameter>(x => x.DataType);
591        dlo.LoadWith<ProblemParameter>(x => x.Problem);
592        dlo.LoadWith<ProblemParameter>(x => x.DataType);
593        dlo.LoadWith<Algorithm>(x => x.AlgorithmClass);
594        dlo.LoadWith<Algorithm>(x => x.Platform);
595        dlo.LoadWith<Problem>(x => x.ProblemClass);
596        dlo.LoadWith<Problem>(x => x.Platform);
597        okb.LoadOptions = dlo;
598        var runs = okb.Runs.Where(x => x.ResultFloatValues.Any(y => y.Result.Name == "BestQuality" && y.Value > double.Parse(query)));
599        return runs.Select(x => Convert.ToDto(x)).ToArray();
600      }
601    }
602    public long AddRun(DataTransfer.Run dto) {
603      using (OKBDataContext okb = new OKBDataContext()) {
604        DataAccess.Run entity = Convert.ToEntity(dto); entity.Id = 0;
605        okb.Runs.InsertOnSubmit(entity);
606        okb.SubmitChanges();
607        return entity.Id;
608      }
609    }
610    public void DeleteRun(long id) {
611      using (OKBDataContext okb = new OKBDataContext()) {
612        IEnumerable<DataAccess.ResultBlobValue> resultBlobValues = okb.ResultBlobValues.Where(x => x.RunId == id);
613        okb.ResultBlobValues.DeleteAllOnSubmit(resultBlobValues);
614        IEnumerable<DataAccess.ResultBoolValue> resultBoolValues = okb.ResultBoolValues.Where(x => x.RunId == id);
615        okb.ResultBoolValues.DeleteAllOnSubmit(resultBoolValues);
616        IEnumerable<DataAccess.ResultFloatValue> resultFloatValues = okb.ResultFloatValues.Where(x => x.RunId == id);
617        okb.ResultFloatValues.DeleteAllOnSubmit(resultFloatValues);
618        IEnumerable<DataAccess.ResultIntValue> resultIntValues = okb.ResultIntValues.Where(x => x.RunId == id);
619        okb.ResultIntValues.DeleteAllOnSubmit(resultIntValues);
620        IEnumerable<DataAccess.ResultStringValue> resultStringValues = okb.ResultStringValues.Where(x => x.RunId == id);
621        okb.ResultStringValues.DeleteAllOnSubmit(resultStringValues);
622
623        DataAccess.Run entity = okb.Runs.FirstOrDefault(x => x.Id == id);
624        if (entity != null) okb.Runs.DeleteOnSubmit(entity);
625        okb.SubmitChanges();
626      }
627    }
628    #endregion
629
630    /*
631
632    /// <summary>
633    /// Gets the complete algorithm object graph up to the following entities:
634    /// <list type="bullet">
635    ///   <item>Parameter</item>
636    ///   <item>Algorithm_Paramters.Parameter.DataType</item>
637    ///   <item>Algorithm_Results.Result.DataType</item>
638    /// </list>
639    /// </summary>
640    /// <param name="id">The algorithm id.</param>
641    /// <returns>An <see cref="Algorithm"/></returns>
642    public Algorithm GetCompleteAlgorithm(int id) {
643      using (OKBDataContext okb = new OKBDataContext()) {
644        var dlo = new DataLoadOptions();
645        dlo.LoadWith<Algorithm>(a => a.AlgorithmClass);
646        dlo.LoadWith<Algorithm>(a => a.Platform);
647        dlo.LoadWith<Algorithm>(a => a.AlgorithmUsers);
648        dlo.LoadWith<AlgorithmUser>(u => u.User);
649        okb.LoadOptions = dlo;
650        return okb.Algorithms.Single(a => a.Id == id);
651      }
652    }
653
654    /// <summary>
655    /// Gets the complete problem object graph up to the following entities:
656    /// <list type="bullet">
657    ///   <item>Platform</item>
658    ///   <item>SolutionRepresentation</item>
659    ///   <item>Problem_Parameters.Parameter</item>
660    ///   <item>IntProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
661    ///   <item>FloatProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
662    ///   <item>CharProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
663    /// </list>
664    /// </summary>
665    /// <param name="id">The problem id.</param>
666    /// <returns>A <see cref="Problem"/></returns>
667    public Problem GetCompleteProblem(int id) {
668      using (OKBDataContext okb = new OKBDataContext()) {
669        var dlo = new DataLoadOptions();
670        dlo.LoadWith<Problem>(p => p.ProblemClass);
671        dlo.LoadWith<Problem>(p => p.Platform);
672        dlo.LoadWith<Problem>(p => p.ProblemUsers);
673        dlo.LoadWith<ProblemUser>(u => u.User);
674        okb.LoadOptions = dlo;
675        return okb.Problems.Single(p => p.Id == id);
676      }
677    }
678
679    /// <summary>
680    /// Updates the algorithm object graph including the following properties and linked entitites:
681    /// <list type="bullet">
682    ///   <item>Name</item>
683    ///   <item>Description</item>
684    ///   <item>AlgorithmClassId</item>
685    ///   <item>PlatformId</item>
686    ///   <item>Algorithm_Parameters</item>
687    ///   <item>Algorithm_Results</item>
688    /// </list>
689    /// <remarks>
690    /// New <see cref="Parameter"/>s or <see cref="Result"/>s will not be
691    /// created but have to be pre-existing.
692    /// </remarks>
693    /// </summary>
694    /// <param name="algorithm">The algorithm.</param>
695    public void UpdateCompleteAlgorithm(Algorithm algorithm) {
696      using (OKBDataContext okb = new OKBDataContext()) {
697        Algorithm original = okb.Algorithms.Single(a => a.Id == algorithm.Id);
698        UpdateAlgorithmData(algorithm, original, okb);
699        okb.SubmitChanges();
700      }
701    }
702
703    /// <summary>
704    /// Updates the problem object graph including the following properties and linked entities:
705    /// <list type="bullet">
706    ///   <item>Name</item>
707    ///   <item>Description</item>
708    ///   <item>ProblemClassId</item>
709    ///   <item>PlatformId</item>
710    ///   <item>SolutionRepresentationId</item>
711    ///   <item>IntProblemCharacteristicValues.Value</item>
712    ///   <item>FloatProblemCharacteristicValues.Value</item>
713    ///   <item>CharProblemCharacteristicValues.Value</item>
714    ///   <item>Problem_Parameters</item>
715    /// </list>
716    /// <remarks>
717    /// New <see cref="ProblemCharacteristic"/>s or <see cref="Parameter"/>s will
718    /// not be created but have to be pre-existing.
719    /// </remarks>
720    /// </summary>
721    /// <param name="problem">The problem.</param>
722    public void UpdateCompleteProblem(Problem problem) {
723      using (OKBDataContext okb = new OKBDataContext()) {
724        Problem originalProblem = okb.Problems.Single(p => p.Id == problem.Id);
725        UpdateProblemData(problem, originalProblem, okb);
726        okb.SubmitChanges();
727      }
728    }
729
730    private static void UpdateAlgorithmData(Algorithm algorithm, Algorithm original, OKBDataContext okb) {
731      original.Name = algorithm.Name;
732      original.Description = algorithm.Description;
733      original.AlgorithmClassId = algorithm.AlgorithmClassId;
734      original.PlatformId = algorithm.PlatformId;
735      okb.AlgorithmUsers.DeleteAllOnSubmit(original.AlgorithmUsers);
736      original.AlgorithmUsers.Clear();
737      foreach (var u in algorithm.AlgorithmUsers) {
738        original.AlgorithmUsers.Add(new AlgorithmUser() {
739          AlgorithmId = original.Id,
740          UserId = u.UserId
741        });
742      }
743    }
744
745    private static void UpdateProblemData(Problem problem, Problem original, OKBDataContext okb) {
746      original.Name = problem.Name;
747      original.Description = problem.Description;
748      original.ProblemClassId = problem.ProblemClassId;
749      original.SolutionRepresentationId = problem.SolutionRepresentationId;
750      original.PlatformId = problem.PlatformId;
751      okb.ProblemUsers.DeleteAllOnSubmit(original.ProblemUsers);
752      original.ProblemUsers.Clear();
753      foreach (var u in problem.ProblemUsers) {
754        original.ProblemUsers.Add(new ProblemUser() {
755          ProblemId = original.Id,
756          UserId = u.UserId
757        });
758      }
759    }*/
760  }
761}
Note: See TracBrowser for help on using the repository browser.