1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Reflection;
|
---|
6 |
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.DataAnalysis;
|
---|
9 | using HeuristicLab.GP.StructureIdentification;
|
---|
10 | using HeuristicLab.Data;
|
---|
11 | using System.Data.Linq;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Modeling.Database.SQLServerCompact {
|
---|
14 | public class DatabaseService : IModelingDatabase {
|
---|
15 |
|
---|
16 | private readonly string connection;
|
---|
17 | public DatabaseService(string connection) {
|
---|
18 | this.connection = connection;
|
---|
19 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
20 | if (!ctx.DatabaseExists())
|
---|
21 | ctx.CreateDatabase();
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | private void EmptyDatabase() {
|
---|
26 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
27 | ctx.DeleteDatabase();
|
---|
28 | ctx.CreateDatabase();
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | public void Persist(HeuristicLab.Modeling.IAlgorithm algorithm) {
|
---|
33 | int trainingSamplesStart = ((IntData)algorithm.Engine.GlobalScope.GetVariableValue("TrainingSamplesStart", false)).Data;
|
---|
34 | int trainingSamplesEnd = ((IntData)algorithm.Engine.GlobalScope.GetVariableValue("TrainingSamplesEnd", false)).Data;
|
---|
35 | int validationSamplesStart = ((IntData)algorithm.Engine.GlobalScope.GetVariableValue("ValidationSamplesStart", false)).Data;
|
---|
36 | int validationSamplesEnd = ((IntData)algorithm.Engine.GlobalScope.GetVariableValue("ValidationSamplesEnd", false)).Data;
|
---|
37 | int testSamplesStart = ((IntData)algorithm.Engine.GlobalScope.GetVariableValue("TestSamplesStart", false)).Data;
|
---|
38 | int testSamplesEnd = ((IntData)algorithm.Engine.GlobalScope.GetVariableValue("TestSamplesEnd", false)).Data;
|
---|
39 |
|
---|
40 | GetOrCreateProblem(algorithm.Dataset);
|
---|
41 | Dictionary<string, Variable> variables = GetAllVariables();
|
---|
42 | Algorithm algo = GetOrCreateAlgorithm(algorithm.Name, algorithm.Description);
|
---|
43 | Variable target = variables[algorithm.Model.TargetVariable];
|
---|
44 | Model model;
|
---|
45 |
|
---|
46 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
47 | model = new Model(target, algo);
|
---|
48 | model.TrainingSamplesStart = trainingSamplesStart;
|
---|
49 | model.TrainingSamplesEnd = trainingSamplesEnd;
|
---|
50 | model.ValidationSamplesStart = validationSamplesStart;
|
---|
51 | model.ValidationSamplesEnd = validationSamplesEnd;
|
---|
52 | model.TestSamplesStart = testSamplesStart;
|
---|
53 | model.TestSamplesEnd = testSamplesEnd;
|
---|
54 |
|
---|
55 | ctx.Models.InsertOnSubmit(model);
|
---|
56 |
|
---|
57 | ctx.SubmitChanges();
|
---|
58 | }
|
---|
59 |
|
---|
60 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
61 | ctx.ModelData.InsertOnSubmit(new ModelData(model, PersistenceManager.SaveToGZip(algorithm.Model.Data)));
|
---|
62 | }
|
---|
63 |
|
---|
64 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
65 | foreach (string inputVariable in algorithm.Model.InputVariables) {
|
---|
66 | ctx.InputVariables.InsertOnSubmit(new InputVariable(model, variables[inputVariable]));
|
---|
67 | }
|
---|
68 | ctx.SubmitChanges();
|
---|
69 | }
|
---|
70 |
|
---|
71 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
72 | //get all double properties to save as modelResult
|
---|
73 | IEnumerable<PropertyInfo> modelResultInfos = algorithm.Model.GetType().GetProperties().Where(
|
---|
74 | info => info.PropertyType == typeof(double));
|
---|
75 | foreach (PropertyInfo modelResultInfo in modelResultInfos) {
|
---|
76 | Result result = GetOrCreateResult(modelResultInfo.Name);
|
---|
77 | double value = (double)modelResultInfo.GetValue(algorithm.Model, null);
|
---|
78 | ctx.ModelResults.InsertOnSubmit(new ModelResult(model, result, value));
|
---|
79 | }
|
---|
80 | ctx.SubmitChanges();
|
---|
81 | }
|
---|
82 |
|
---|
83 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
84 | IEnumerable<MethodInfo> inputVariableResultInfos = algorithm.Model.GetType().GetMethods().Where(
|
---|
85 | info => info.GetParameters().Count() == 1 &&
|
---|
86 | info.GetParameters()[0].ParameterType == typeof(string) &&
|
---|
87 | info.GetParameters()[0].Name == "variableName" &&
|
---|
88 | info.ReturnParameter.ParameterType == typeof(double) &&
|
---|
89 | info.Name.StartsWith("Get"));
|
---|
90 | foreach (MethodInfo inputVariableResultInfo in inputVariableResultInfos) {
|
---|
91 | Result result = GetOrCreateResult(inputVariableResultInfo.Name.Substring(3));
|
---|
92 | foreach (InputVariable variable in ctx.InputVariables.Where(iv => iv.Model == model)) {
|
---|
93 | double value = (double)inputVariableResultInfo.Invoke(algorithm.Model, new object[] { variable.Variable.Name });
|
---|
94 | ctx.InputVariableResults.InsertOnSubmit(new InputVariableResult(variable, result, value));
|
---|
95 | }
|
---|
96 | }
|
---|
97 | ctx.SubmitChanges();
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|
101 |
|
---|
102 | #region Problem
|
---|
103 |
|
---|
104 | public Dataset GetDataset() {
|
---|
105 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
106 | if (ctx.Problems.Count() != 1)
|
---|
107 | throw new InvalidOperationException("Could not get dataset. No or more than one problems are persisted in the database.");
|
---|
108 |
|
---|
109 | Problem problem = ctx.Problems.Single();
|
---|
110 | return problem.Dataset;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public Problem GetOrCreateProblem(Dataset dataset) {
|
---|
115 | Problem problem;
|
---|
116 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
117 | if (ctx.Problems.Count() == 0)
|
---|
118 | problem = PersistProblem(dataset);
|
---|
119 | else
|
---|
120 | problem = ctx.Problems.Single();
|
---|
121 | if (problem.Dataset.ToString() != dataset.ToString())
|
---|
122 | throw new InvalidOperationException("Could not persist dataset. The database already contains a different dataset.");
|
---|
123 | }
|
---|
124 | return problem;
|
---|
125 | }
|
---|
126 |
|
---|
127 | private Problem PersistProblem(Dataset dataset) {
|
---|
128 | Problem problem;
|
---|
129 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
130 | if (ctx.Problems.Count() != 0)
|
---|
131 | throw new InvalidOperationException("Could not persist dataset. A dataset is already saved in the database.");
|
---|
132 | problem = new Problem(dataset);
|
---|
133 | ctx.Problems.InsertOnSubmit(problem);
|
---|
134 | foreach (string variable in dataset.VariableNames) {
|
---|
135 | ctx.Variables.InsertOnSubmit(new Variable(variable));
|
---|
136 | }
|
---|
137 | ctx.SubmitChanges();
|
---|
138 | }
|
---|
139 | return problem;
|
---|
140 | }
|
---|
141 |
|
---|
142 | #endregion
|
---|
143 |
|
---|
144 | #region Algorithm
|
---|
145 | public Algorithm GetOrCreateAlgorithm(string name, string description) {
|
---|
146 | Algorithm algorithm;
|
---|
147 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
148 | var algorithms = from algo in ctx.Algorithms
|
---|
149 | where algo.Name == name && algo.Description == description
|
---|
150 | select algo;
|
---|
151 | if (algorithms.Count() == 0) {
|
---|
152 | algorithm = new Algorithm(name, description);
|
---|
153 | ctx.Algorithms.InsertOnSubmit(algorithm);
|
---|
154 | ctx.SubmitChanges();
|
---|
155 | } else if (algorithms.Count() == 1)
|
---|
156 | algorithm = algorithms.Single();
|
---|
157 | else
|
---|
158 | throw new ArgumentException("Could not get Algorithm. More than one algorithm with the name " + name + " are saved in database.");
|
---|
159 | }
|
---|
160 | return algorithm;
|
---|
161 | }
|
---|
162 | #endregion
|
---|
163 |
|
---|
164 | #region Variables
|
---|
165 | public Dictionary<string, Variable> GetAllVariables() {
|
---|
166 | Dictionary<string, Variable> dict = new Dictionary<string, Variable>();
|
---|
167 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
168 | dict = ctx.Variables.ToDictionary<Variable, string>(delegate(Variable v) { return v.Name; });
|
---|
169 | }
|
---|
170 | return dict;
|
---|
171 | }
|
---|
172 | #endregion
|
---|
173 |
|
---|
174 | #region Result
|
---|
175 | public Result GetOrCreateResult(string name) {
|
---|
176 | Result result;
|
---|
177 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
178 | var results = from r in ctx.Results
|
---|
179 | where r.Name == name
|
---|
180 | select r;
|
---|
181 | if (results.Count() == 0) {
|
---|
182 | result = new Result(name);
|
---|
183 | ctx.Results.InsertOnSubmit(result);
|
---|
184 | ctx.SubmitChanges();
|
---|
185 | } else if (results.Count() == 1)
|
---|
186 | result = results.Single();
|
---|
187 | else
|
---|
188 | throw new ArgumentException("Could not get result. More than one result with the name " + name + " are saved in database.");
|
---|
189 | }
|
---|
190 | return result;
|
---|
191 | }
|
---|
192 |
|
---|
193 | public IEnumerable<IResult> GetAllResults() {
|
---|
194 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
195 | return ctx.Results.ToList().Cast<IResult>();
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | public IEnumerable<IResult> GetAllResultsForInputVariables() {
|
---|
200 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
201 | return (from ir in ctx.InputVariableResults select ir.Result).Distinct().ToList().Cast<IResult>();
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | #endregion
|
---|
206 |
|
---|
207 | #region ModelResult
|
---|
208 | public IEnumerable<IModelResult> GetModelResults(IModel model) {
|
---|
209 | ModelingDataContext ctx = new ModelingDataContext(connection);
|
---|
210 | DataLoadOptions dlo = new DataLoadOptions();
|
---|
211 | dlo.LoadWith<ModelResult>(mr => mr.Result);
|
---|
212 | ctx.LoadOptions = dlo;
|
---|
213 |
|
---|
214 | var results = from result in ctx.ModelResults
|
---|
215 | where result.Model == model
|
---|
216 | select result;
|
---|
217 | return results.ToList().Cast<IModelResult>();
|
---|
218 | }
|
---|
219 |
|
---|
220 | public void GetAllModelResults() {
|
---|
221 | ModelingDataContext ctx = new ModelingDataContext(connection);
|
---|
222 | DataLoadOptions dlo = new DataLoadOptions();
|
---|
223 | dlo.LoadWith<ModelResult>(mr => mr.Result);
|
---|
224 | dlo.LoadWith<ModelResult>(mr => mr.Model);
|
---|
225 | ctx.LoadOptions = dlo;
|
---|
226 |
|
---|
227 | var results = from result in ctx.ModelResults
|
---|
228 | select result;
|
---|
229 | results.ToList();
|
---|
230 |
|
---|
231 | }
|
---|
232 | #endregion
|
---|
233 |
|
---|
234 | #region InputVariableResults
|
---|
235 | public IEnumerable<IInputVariableResult> GetInputVariableResults(IModel model) {
|
---|
236 | ModelingDataContext ctx = new ModelingDataContext(connection);
|
---|
237 | DataLoadOptions dlo = new DataLoadOptions();
|
---|
238 | dlo.LoadWith<InputVariableResult>(ir => ir.Variable);
|
---|
239 | dlo.LoadWith<InputVariableResult>(ir => ir.Result);
|
---|
240 | ctx.LoadOptions = dlo;
|
---|
241 |
|
---|
242 | var inputResults = from ir in ctx.InputVariableResults
|
---|
243 | where ir.Model == model
|
---|
244 | select ir;
|
---|
245 | return inputResults.ToList().Cast<IInputVariableResult>();
|
---|
246 | }
|
---|
247 |
|
---|
248 | #endregion
|
---|
249 |
|
---|
250 | #region Model
|
---|
251 | public IEnumerable<IModel> GetAllModels() {
|
---|
252 | ModelingDataContext ctx = new ModelingDataContext(connection);
|
---|
253 | DataLoadOptions dlo = new DataLoadOptions();
|
---|
254 | dlo.LoadWith<Model>(m => m.TargetVariable);
|
---|
255 | ctx.LoadOptions = dlo;
|
---|
256 | return ctx.Models.ToList().Cast<IModel>();
|
---|
257 | }
|
---|
258 |
|
---|
259 | public byte[] GetModelData(IModel model) {
|
---|
260 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
261 | var data = (from md in ctx.ModelData
|
---|
262 | where md.Model == model
|
---|
263 | select md);
|
---|
264 | if (data.Count() == 0)
|
---|
265 | return null;
|
---|
266 | return data.Single().Data;
|
---|
267 | }
|
---|
268 | }
|
---|
269 | #endregion
|
---|
270 |
|
---|
271 | }
|
---|
272 | }
|
---|