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