Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.SQLiteBackend/3.2/Model.cs @ 2178

Last change on this file since 2178 was 2178, checked in by mkommend, 15 years ago

added first implementation of LinqToSql mapping (ticket #712)

File size: 3.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Data.Linq;
5using System.Data.Linq.Mapping;
6using System.Text;
7
8namespace HeuristicLab.Modeling.SQLiteBackend { 
9  [Table(Name="Model")]
10  public class Model {
11    public Model() {
12      targetVariable = default(EntityRef<Variable>);
13      algorithm = default(EntityRef<Algorithm>);
14    }
15
16    private int id;
17    [Column(Storage = "id", DbType = "integer", IsPrimaryKey = true, IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
18    public int Id {
19      get { return this.id; }
20      private set { this.id = value; }
21    }
22
23    private object data;
24    [Column(Storage = "data", DbType = "text", CanBeNull = false)]
25    public object Data {
26      get { return this.data; }
27      set { this.data = value; }
28    }
29
30    private int algorithmId;
31    [Column(Storage="algorithmId", DbType="Int not null")]
32    public int AlgorithmId {
33      get { return this.algorithmId;}
34      private set {
35        if(algorithmId != value) {
36          if(algorithm.HasLoadedOrAssignedValue)
37            throw new ForeignKeyReferenceAlreadyHasValueException();
38          algorithmId =value;
39        }
40      }
41    }
42
43
44    private EntityRef<Algorithm> algorithm;
45    [Association(Storage="algorithm",ThisKey="AlgorithmId",OtherKey="Id",IsForeignKey=true)]
46    public Algorithm Algorithm {
47      get { return this.algorithm.Entity; }
48      set {
49        Algorithm previousValue = algorithm.Entity;
50        if (previousValue != value || (!algorithm.HasLoadedOrAssignedValue)) {
51          if (previousValue == null) {
52            algorithm.Entity = null;
53            previousValue.Models.Remove(this);
54          }
55          algorithm.Entity = value;
56          if (value != null) {
57            value.Models.Add(this);
58            algorithmId = value.Id;
59          } else
60            throw new NullReferenceException("Null not allowed as value for Model.Algorithm");
61        }
62      }
63    }
64
65    private int targetVariableId;
66    [Column(Storage = "targetVariableId", DbType = "Int not null")]
67    public int TargetVariableId {
68      get { return this.targetVariableId; }
69      private set {
70        if (targetVariableId != value) {
71          if (targetVariable.HasLoadedOrAssignedValue)
72            throw new ForeignKeyReferenceAlreadyHasValueException();
73          targetVariableId = value;
74        }
75      }
76    }
77
78    private EntityRef<Variable> targetVariable;
79    [Association(Storage = "targetVariable", ThisKey = "TargetVariableId", OtherKey = "Id", IsForeignKey = true)]
80    public Variable TargetVariable {
81      get { return this.targetVariable.Entity; }
82      set {
83        Variable previousValue = targetVariable.Entity;
84        if (previousValue != value || (!targetVariable.HasLoadedOrAssignedValue)) {
85          if (previousValue == null) {
86            targetVariable.Entity = null;
87            previousValue.ModelsPredictingThisVariable.Remove(this);
88          }
89          targetVariable.Entity = value;
90          if (value != null) {
91            value.ModelsPredictingThisVariable.Add(this);
92            targetVariableId = value.Id;
93          } else
94            throw new NullReferenceException("Null not allowed as value for Model.TargetVariable");
95        }
96      }
97    }
98
99    public IEnumerable<Variable> inputVariables {
100      get {
101        using (ModelingDataContext ctx = new ModelingDataContext()) {
102          var x = from inputVariable in ctx.InputVariables
103                  where inputVariable.Model == this
104                  select inputVariable.Variable;
105          return x.AsEnumerable();
106        }
107      }
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.