Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.OKB.DataAccess/3.3/Extensions.cs @ 4384

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

Worked on OKB data model (#1174)

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