Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Services.OKB.DataAccess/3.3/OKB_Methods.cs @ 4945

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

Integrated OKB data access layer (#1166)

File size: 17.0 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.Data.Linq;
24using System.Linq;
25
26namespace HeuristicLab.Services.OKB.DataAccess {
27
28  #region TypeAbstraction
29  public interface IParameterValue {
30    int ParameterId { get; set; }
31    Parameter Parameter { get; set; }
32    int ExperimentId { get; set; }
33    Experiment Experiment { get; set; }
34    object Value { get; set; }
35  }
36  public partial class IntParameterValue : IParameterValue {
37    object IParameterValue.Value {
38      get { return this.Value; }
39      set { this.Value = (int)value; }
40    }
41  }
42  public partial class FloatParameterValue : IParameterValue {
43    object IParameterValue.Value {
44      get { return this.Value; }
45      set { this.Value = (double)value; }
46    }
47  }
48  public partial class CharParameterValue : IParameterValue {
49    object IParameterValue.Value {
50      get { return this.Value; }
51      set { this.Value = (string)value; }
52    }
53  }
54  public partial class OperatorParameterValue : IParameterValue {
55    object IParameterValue.Value {
56      get { return this.Value; }
57      set { this.Value = (Binary)value; }
58    }
59  }
60  public partial class Experiment {
61    public IQueryable<IParameterValue> ParameterValues {
62      get {
63        return IntParameterValues
64          .AsQueryable()
65          .Cast<IParameterValue>()
66          .Concat(FloatParameterValues.Cast<IParameterValue>())
67          .Concat(CharParameterValues.Cast<IParameterValue>())
68          .Concat(OperatorParameterValues.Cast<IParameterValue>());
69      }
70      set {
71        if (value == null) return;
72        foreach (IParameterValue pv in value) {
73          if (pv == null) continue;
74          if (pv is IntParameterValue) {
75            IntParameterValues.Add(new IntParameterValue() { ParameterId = pv.ParameterId, Value = (int)pv.Value });
76          } else if (pv is FloatParameterValue) {
77            FloatParameterValues.Add(new FloatParameterValue() { ParameterId = pv.ParameterId, Value = (double)pv.Value });
78          } else if (pv is CharParameterValue) {
79            CharParameterValues.Add(new CharParameterValue() { ParameterId = pv.ParameterId, Value = (string)pv.Value });
80          } else if (pv is OperatorParameterValue) {
81            OperatorParameterValues.Add(new OperatorParameterValue() {
82              ParameterId = pv.ParameterId,
83              Value = (Binary)pv.Value,
84              DataTypeId = ((OperatorParameterValue)pv).DataTypeId,
85            });
86          } else {
87            throw new ArgumentException("Invalid Parameter type" + pv.GetType());
88          }
89        }
90      }
91    }
92  }
93
94  public interface IResultValue {
95    int ResultId { get; set; }
96    Result Result { get; set; }
97    int RunId { get; set; }
98    Run Run { get; set; }
99    object Value { get; set; }
100  }
101  public partial class IntResultValue : IResultValue {
102    object IResultValue.Value {
103      get { return this.Value; }
104      set { this.Value = (int)value; }
105    }
106  }
107  public partial class FloatResultValue : IResultValue {
108    object IResultValue.Value {
109      get { return this.Value; }
110      set { this.Value = (double)value; }
111    }
112  }
113  public partial class CharResultValue : IResultValue {
114    object IResultValue.Value {
115      get { return this.Value; }
116      set { this.Value = (string)value; }
117    }
118  }
119  public partial class BlobResultValue : IResultValue {
120    object IResultValue.Value {
121      get { return this.Value; }
122      set { this.Value = (Binary)value; }
123    }
124  }
125  public partial class Run {
126    public IQueryable<IResultValue> ResultValues {
127      get {
128        return IntResultValues
129          .AsQueryable()
130          .Cast<IResultValue>()
131          .Concat(FloatResultValues.Cast<IResultValue>())
132          .Concat(CharResultValues.Cast<IResultValue>())
133          .Concat(BlobResultValues.Cast<IResultValue>());
134      }
135      set {
136        foreach (IResultValue rv in value) {
137          if (rv == null) continue;
138          if (rv is IntResultValue) {
139            IntResultValues.Add(new IntResultValue() { ResultId = rv.ResultId, Value = (int)rv.Value });
140          } else if (rv is FloatResultValue) {
141            FloatResultValues.Add(new FloatResultValue() { ResultId = rv.ResultId, Value = (double)rv.Value });
142          } else if (rv is CharResultValue) {
143            CharResultValues.Add(new CharResultValue() { ResultId = rv.ResultId, Value = (string)rv.Value });
144          } else if (rv is BlobResultValue) {
145            BlobResultValues.Add(new BlobResultValue() { ResultId = rv.ResultId, Value = (Binary)rv.Value });
146          } else {
147            throw new ArgumentException("Invalid result value type " + rv.GetType());
148          }
149        }
150      }
151    }
152  }
153  public partial class Result {
154    public IQueryable<IResultValue> ResultValues {
155      get {
156        return IntResultValues
157          .AsQueryable()
158          .Cast<IResultValue>()
159          .Concat(FloatResultValues.Cast<IResultValue>())
160          .Concat(CharResultValues.Cast<IResultValue>())
161          .Concat(BlobResultValues.Cast<IResultValue>());
162      }
163    }
164  }
165
166  public interface IProblemCharacteristicValue {
167    int ProblemId { get; set; }
168    Problem Problem { get; set; }
169    int ProblemCharacteristicId { get; set; }
170    ProblemCharacteristic ProblemCharacteristic { get; set; }
171    object Value { get; set; }
172  }
173  public partial class IntProblemCharacteristicValue : IProblemCharacteristicValue {
174    object IProblemCharacteristicValue.Value {
175      get { return this.Value; }
176      set { this.Value = (int)value; }
177    }
178  }
179  public partial class FloatProblemCharacteristicValue : IProblemCharacteristicValue {
180    object IProblemCharacteristicValue.Value {
181      get { return this.Value; }
182      set { this.Value = (double)value; }
183    }
184  }
185  public partial class CharProblemCharacteristicValue : IProblemCharacteristicValue {
186    object IProblemCharacteristicValue.Value {
187      get { return this.Value; }
188      set { this.Value = (string)value; }
189    }
190  }
191  public partial class Problem {
192    public IQueryable<IProblemCharacteristicValue> ProblemCharacteristicValues {
193      get {
194        return IntProblemCharacteristicValues
195          .AsQueryable()
196          .Cast<IProblemCharacteristicValue>()
197          .Concat(FloatProblemCharacteristicValues.Cast<IProblemCharacteristicValue>())
198          .Concat(CharProblemCharacteristicValues.Cast<IProblemCharacteristicValue>());
199      }
200    }
201  }
202  public partial class ProblemCharacteristic {
203    public IQueryable<IProblemCharacteristicValue> ProblemCharacteristicValues {
204      get {
205        return IntProblemCharacteristicValues
206          .AsQueryable()
207          .Cast<IProblemCharacteristicValue>()
208          .Concat(FloatProblemCharacteristicValues.Cast<IProblemCharacteristicValue>())
209          .Concat(CharProblemCharacteristicValues.Cast<IProblemCharacteristicValue>());
210      }
211    }
212  }
213  #endregion
214
215  #region Abuse entities to store parameter values for a single (not yet existing experiment)
216  public partial class Parameter {
217    public IParameterValue ParameterValue {
218      get {
219        try {
220          return IntParameterValues
221            .AsQueryable()
222            .Cast<IParameterValue>()
223            .Concat(FloatParameterValues.Cast<IParameterValue>())
224            .Concat(CharParameterValues.Cast<IParameterValue>())
225            .Concat(OperatorParameterValues.Cast<IParameterValue>())
226            .ToList().Single();
227        }
228        catch {
229          return null;
230        }
231      }
232      set {
233        if (value == ParameterValue) // access to ParameterValue ensures that there is a single value
234          return;
235        if (value == null)
236          throw new ArgumentNullException("ParameterValue");
237        IParameterValue oldValue = ParameterValue;
238        Type t = value.GetType();
239        if (oldValue.GetType() != t)
240          throw new ArgumentException("cannot assign value with different type");
241        if (t == typeof(IntParameterValue)) {
242          IntParameterValues.Clear();
243          IntParameterValues.Add((IntParameterValue)value);
244        } else if (t == typeof(FloatParameterValue)) {
245          FloatParameterValues.Clear();
246          FloatParameterValues.Add((FloatParameterValue)value);
247        } else if (t == typeof(CharParameterValue)) {
248          CharParameterValues.Clear();
249          CharParameterValues.Add((CharParameterValue)value);
250        } else if (t == typeof(OperatorParameterValue)) {
251          OperatorParameterValues.Clear();
252          OperatorParameterValues.Add((OperatorParameterValue)value);
253        } else throw new ArgumentException("invalid parameter value type " + t.Name);
254      }
255    }
256  }
257  public partial class Result {
258    public IResultValue ResultValue {
259      get {
260        try {
261          return IntResultValues
262            .AsQueryable()
263            .Cast<IResultValue>()
264            .Concat(FloatResultValues.Cast<IResultValue>())
265            .Concat(CharResultValues.Cast<IResultValue>())
266            .Concat(BlobResultValues.Cast<IResultValue>()).ToList().Single();
267        }
268        catch {
269          return null;
270        }
271      }
272      set {
273        if (value == ResultValue)
274          return;
275        if (value == null)
276          throw new ArgumentNullException("ResultValue");
277        IResultValue oldValue = ResultValue;
278        Type t = value.GetType();
279        if (oldValue.GetType() != t)
280          throw new ArgumentException("cannot assign value with different type");
281        if (t == typeof(IntResultValue)) {
282          IntResultValues.Clear();
283          IntResultValues.Add((IntResultValue)value);
284        } else if (t == typeof(FloatResultValue)) {
285          FloatResultValues.Clear();
286          FloatResultValues.Add((FloatResultValue)value);
287        } else if (t == typeof(CharResultValue)) {
288          CharResultValues.Clear();
289          CharResultValues.Add((CharResultValue)value);
290        } else if (t == typeof(BlobResultValue)) {
291          BlobResultValues.Clear();
292          BlobResultValues.Add((BlobResultValue)value);
293        } else throw new ArgumentException("invalid result value type " + t.Name);
294      }
295    }
296  }
297  public partial class Algorithm {
298    public IQueryable<Parameter> Parameters {
299      get {
300        return Algorithm_Parameters.AsQueryable().Select(ap => ap.Parameter);
301      }
302    }
303    public IQueryable<IntParameterValue> IntParameterValues {
304      get {
305        return Parameters.AsQueryable()
306          .Where(p => p.IntParameterValues.Count > 0)
307          .Select(p => p.IntParameterValues.Single());
308      }
309    }
310    public IQueryable<FloatParameterValue> FloatParameterValues {
311      get {
312        return Parameters.AsQueryable()
313          .Where(p => p.FloatParameterValues.Count > 0)
314          .Select(p => p.FloatParameterValues.Single());
315      }
316    }
317    public IQueryable<CharParameterValue> CharParameterValues {
318      get {
319        return Parameters.AsQueryable()
320          .Where(p => p.CharParameterValues.Count > 0)
321          .Select(p => p.CharParameterValues.Single());
322      }
323    }
324    public IQueryable<OperatorParameterValue> OperatorParameterValues {
325      get {
326        return Parameters.AsQueryable()
327          .Where(p => p.OperatorParameterValues.Count > 0)
328          .Select(p => p.OperatorParameterValues.Single());
329      }
330    }
331    public IQueryable<IParameterValue> ParameterValues {
332      get {
333        return Parameters.AsQueryable().Select(p => p.ParameterValue);
334      }
335    }
336
337    public IQueryable<Result> Results {
338      get {
339        return Algorithm_Results.AsQueryable().Select(ar => ar.Result);
340      }
341    }
342    public IQueryable<IntResultValue> IntResultValues {
343      get {
344        return Results.AsQueryable()
345          .Where(r => r.IntResultValues.Count > 0)
346          .Select(r => r.IntResultValues.Single());
347      }
348    }
349    public IQueryable<FloatResultValue> FloatResultValues {
350      get {
351        return Results.AsQueryable()
352          .Where(r => r.FloatResultValues.Count > 0)
353          .Select(r => r.FloatResultValues.Single());
354      }
355    }
356    public IQueryable<CharResultValue> CharResultValues {
357      get {
358
359        return Results.AsQueryable()
360          .Where(r => r.CharResultValues.Count > 0)
361          .Select(r => r.CharResultValues.Single());
362      }
363    }
364    public IQueryable<BlobResultValue> BlobResultValues {
365      get {
366        return Results.AsQueryable()
367          .Where(r => r.BlobResultValues.Count > 0)
368          .Select(r => r.BlobResultValues.Single());
369      }
370    }
371    public IQueryable<IResultValue> ResultValues {
372      get {
373        return Results.AsQueryable().Select(r => r.ResultValue);
374      }
375    }
376  }
377  #endregion
378
379  #region Type access
380  public partial class DataType {
381    public Type Type {
382      get {
383        return Type.GetType(ClrName, false) ?? typeof(object);
384      }
385    }
386  }
387  #endregion
388
389  #region NamedEntities
390  public interface INamedEntity {
391    int Id { get; }
392    string Name { get; }
393    string Description { get; }
394  }
395  public partial class AlgorithmClass : INamedEntity { }
396  public partial class Algorithm : INamedEntity { }
397  public partial class ProblemClass : INamedEntity { }
398  public partial class Problem : INamedEntity { }
399  public partial class SolutionRepresentation : INamedEntity { }
400  public partial class ProblemCharacteristic : INamedEntity { }
401  public partial class Parameter : INamedEntity { }
402  public partial class Result : INamedEntity { }
403  public partial class Project : INamedEntity { }
404  public partial class Platform : INamedValue { }
405  #endregion
406
407  #region DataTypes
408  public interface IIntValue {
409    int Value { get; }
410  }
411  public partial class IntResultValue : IIntValue { }
412  public partial class IntParameterValue : IIntValue { }
413  public partial class IntProblemCharacteristicValue : IIntValue { }
414  public interface IFloatValue {
415    double Value { get; }
416  }
417  public partial class FloatResultValue : IFloatValue { }
418  public partial class FloatParameterValue : IFloatValue { }
419  public partial class FloatProblemCharacteristicValue : IFloatValue { }
420  public interface ICharValue {
421    string Value { get; }
422  }
423  public partial class CharResultValue : ICharValue { }
424  public partial class CharParameterValue : ICharValue { }
425  public partial class CharProblemCharacteristicValue : ICharValue { }
426  #endregion
427
428  #region NamedValues
429  public interface INamedValue {
430    string Name { get; }
431  }
432  public interface IValue<T> : INamedValue {
433    T Value { get; }
434  }
435  public partial class IntResultValue : IValue<int> {
436    public string Name { get { return Result.Name; } }
437  }
438  public partial class FloatResultValue : IValue<double> {
439    public string Name { get { return Result.Name; } }
440  }
441  public partial class CharResultValue : IValue<string> {
442    public string Name { get { return Result.Name; } }
443  }
444  public partial class BlobResultValue : IValue<Binary> {
445    public string Name { get { return Result.Name; } }
446  }
447  public partial class IntParameterValue : IValue<int> {
448    public string Name { get { return Parameter.Name; } }
449  }
450  public partial class FloatParameterValue : IValue<double> {
451    public string Name { get { return Parameter.Name; } }
452  }
453  public partial class CharParameterValue : IValue<string> {
454    public string Name { get { return Parameter.Name; } }
455  }
456  public partial class OperatorParameterValue : IValue<Binary> {
457    public string Name { get { return Parameter.Name; } }
458  }
459  public partial class IntProblemCharacteristicValue : IValue<int> {
460    public string Name { get { return ProblemCharacteristic.Name; } }
461  }
462  public partial class FloatProblemCharacteristicValue : IValue<double> {
463    public string Name { get { return ProblemCharacteristic.Name; } }
464  }
465  public partial class CharProblemCharacteristicValue : IValue<string> {
466    public string Name { get { return ProblemCharacteristic.Name; } }
467  }
468  #endregion
469
470  #region DynamicTypeInformation
471  public interface IDynamicParent : INamedEntity {
472    DataType DataType { get; }
473  }
474  public partial class ProblemCharacteristic : IDynamicParent { }
475  public partial class Parameter : IDynamicParent { }
476  public partial class Result : IDynamicParent { }
477  #endregion
478}
Note: See TracBrowser for help on using the repository browser.