1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Data.Linq;
|
---|
5 | using System.Data.Linq.Mapping;
|
---|
6 | using System.Text;
|
---|
7 |
|
---|
8 | namespace 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 | public Model(Variable targetVariable, Algorithm algorithm, object data)
|
---|
17 | : this() {
|
---|
18 | this.TargetVariable = targetVariable;
|
---|
19 | this.Algorithm = algorithm;
|
---|
20 | this.data = data;
|
---|
21 | }
|
---|
22 |
|
---|
23 | private int id;
|
---|
24 | [Column(Storage = "id", IsPrimaryKey = true, IsDbGenerated = true)]
|
---|
25 | public int Id {
|
---|
26 | get { return this.id; }
|
---|
27 | private set { this.id = value; }
|
---|
28 | }
|
---|
29 |
|
---|
30 | private object data;
|
---|
31 | [Column(Storage = "data", DbType = "varbinary(8000)", CanBeNull = false)]
|
---|
32 | public object Data {
|
---|
33 | get { return this.data; }
|
---|
34 | set { this.data = value; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | private int algorithmId;
|
---|
38 | [Column(Storage = "algorithmId", CanBeNull = false)]
|
---|
39 | public int AlgorithmId {
|
---|
40 | get { return this.algorithmId; }
|
---|
41 | private set {
|
---|
42 | if (algorithmId != value) {
|
---|
43 | if (algorithm.HasLoadedOrAssignedValue)
|
---|
44 | throw new ForeignKeyReferenceAlreadyHasValueException();
|
---|
45 | algorithmId = value;
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | private EntityRef<Algorithm> algorithm;
|
---|
51 | [Association(Storage = "algorithm", ThisKey = "AlgorithmId", OtherKey = "Id", IsForeignKey = true)]
|
---|
52 | public Algorithm Algorithm {
|
---|
53 | get { return this.algorithm.Entity; }
|
---|
54 | set {
|
---|
55 | Algorithm previousValue = algorithm.Entity;
|
---|
56 | if (previousValue != value || (!algorithm.HasLoadedOrAssignedValue)) {
|
---|
57 | if (previousValue != null) {
|
---|
58 | algorithm.Entity = null;
|
---|
59 | }
|
---|
60 | algorithm.Entity = value;
|
---|
61 | if (value != null) {
|
---|
62 | algorithmId = value.Id;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | private int targetVariableId;
|
---|
69 | [Column(Storage = "targetVariableId", CanBeNull = false)]
|
---|
70 | public int TargetVariableId {
|
---|
71 | get { return this.targetVariableId; }
|
---|
72 | private set {
|
---|
73 | if (targetVariableId != value) {
|
---|
74 | if (targetVariable.HasLoadedOrAssignedValue)
|
---|
75 | throw new ForeignKeyReferenceAlreadyHasValueException();
|
---|
76 | targetVariableId = value;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | private EntityRef<Variable> targetVariable;
|
---|
82 | [Association(Storage = "targetVariable", ThisKey = "TargetVariableId", OtherKey = "Id", IsForeignKey = true)]
|
---|
83 | public Variable TargetVariable {
|
---|
84 | get { return this.targetVariable.Entity; }
|
---|
85 | set {
|
---|
86 | Variable previousValue = targetVariable.Entity;
|
---|
87 | if (previousValue != value || (!targetVariable.HasLoadedOrAssignedValue)) {
|
---|
88 | if (previousValue != null) {
|
---|
89 | targetVariable.Entity = null;
|
---|
90 | }
|
---|
91 | targetVariable.Entity = value;
|
---|
92 | if (value != null) {
|
---|
93 | targetVariableId = value.Id;
|
---|
94 | }
|
---|
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 | public Dictionary<string, double> ResultValues {
|
---|
111 | get {
|
---|
112 | using (ModelingDataContext ctx = new ModelingDataContext()) {
|
---|
113 | var x = (from modelResult in ctx.ModelResults
|
---|
114 | where modelResult.Model == this
|
---|
115 | select modelResult)
|
---|
116 | .ToDictionary(
|
---|
117 | modelResult => modelResult.Result.Name,
|
---|
118 | modelResult => modelResult.Value);
|
---|
119 | return x;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|