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 | Dictionary<string, Variable> variables = GetAllVariables();
|
---|
72 | Algorithm algo = GetOrCreateAlgorithm(algorithm.Name, algorithm.Description);
|
---|
73 | Variable target = variables[algorithm.Model.TargetVariable];
|
---|
74 | Model model;
|
---|
75 |
|
---|
76 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
77 | model = new Model(target, algo);
|
---|
78 | model.TrainingSamplesStart = algorithm.Model.TrainingSamplesStart;
|
---|
79 | model.TrainingSamplesEnd = algorithm.Model.TrainingSamplesEnd;
|
---|
80 | model.ValidationSamplesStart = algorithm.Model.ValidationSamplesStart;
|
---|
81 | model.ValidationSamplesEnd = algorithm.Model.ValidationSamplesEnd;
|
---|
82 | model.TestSamplesStart = algorithm.Model.TestSamplesStart;
|
---|
83 | model.TestSamplesEnd = algorithm.Model.TestSamplesEnd;
|
---|
84 |
|
---|
85 | ctx.Models.InsertOnSubmit(model);
|
---|
86 |
|
---|
87 | ctx.SubmitChanges();
|
---|
88 | }
|
---|
89 |
|
---|
90 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
91 | ctx.ModelData.InsertOnSubmit(new ModelData(model, PersistenceManager.SaveToGZip(algorithm.Model.Data)));
|
---|
92 | }
|
---|
93 |
|
---|
94 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
95 | foreach (string inputVariable in algorithm.Model.InputVariables) {
|
---|
96 | ctx.InputVariables.InsertOnSubmit(new InputVariable(model, variables[inputVariable]));
|
---|
97 | }
|
---|
98 | ctx.SubmitChanges();
|
---|
99 | }
|
---|
100 |
|
---|
101 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
102 | //get all double properties to save as modelResult
|
---|
103 | IEnumerable<PropertyInfo> modelResultInfos = algorithm.Model.GetType().GetProperties().Where(
|
---|
104 | info => info.PropertyType == typeof(double));
|
---|
105 | foreach (PropertyInfo modelResultInfo in modelResultInfos) {
|
---|
106 | Result result = GetOrCreateResult(modelResultInfo.Name);
|
---|
107 | double value = (double)modelResultInfo.GetValue(algorithm.Model, null);
|
---|
108 | ctx.ModelResults.InsertOnSubmit(new ModelResult(model, result, value));
|
---|
109 | }
|
---|
110 | ctx.SubmitChanges();
|
---|
111 | }
|
---|
112 |
|
---|
113 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
114 | IEnumerable<MethodInfo> inputVariableResultInfos = algorithm.Model.GetType().GetMethods().Where(
|
---|
115 | info => info.GetParameters().Count() == 1 &&
|
---|
116 | info.GetParameters()[0].ParameterType == typeof(string) &&
|
---|
117 | info.GetParameters()[0].Name == "variableName" &&
|
---|
118 | info.ReturnParameter.ParameterType == typeof(double) &&
|
---|
119 | info.Name.StartsWith("Get"));
|
---|
120 | foreach (MethodInfo inputVariableResultInfo in inputVariableResultInfos) {
|
---|
121 | Result result = GetOrCreateResult(inputVariableResultInfo.Name.Substring(3));
|
---|
122 | foreach (InputVariable variable in ctx.InputVariables.Where(iv => iv.Model == model)) {
|
---|
123 | double value = (double)inputVariableResultInfo.Invoke(algorithm.Model, new object[] { variable.Variable.Name });
|
---|
124 | ctx.InputVariableResults.InsertOnSubmit(new InputVariableResult(variable, result, value));
|
---|
125 | }
|
---|
126 | }
|
---|
127 | ctx.SubmitChanges();
|
---|
128 | }
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | #region Problem
|
---|
133 |
|
---|
134 | public Dataset GetDataset() {
|
---|
135 | if (ctx.Problems.Count() != 1)
|
---|
136 | throw new InvalidOperationException("Could not get dataset. No or more than one problems are persisted in the database.");
|
---|
137 |
|
---|
138 | Problem problem = ctx.Problems.Single();
|
---|
139 | return problem.Dataset;
|
---|
140 |
|
---|
141 | }
|
---|
142 |
|
---|
143 | public Problem GetOrCreateProblem(Dataset dataset) {
|
---|
144 | Problem problem;
|
---|
145 | if (ctx.Problems.Count() == 0)
|
---|
146 | problem = PersistProblem(dataset);
|
---|
147 | else
|
---|
148 | problem = ctx.Problems.Single();
|
---|
149 | if (problem.Dataset.ToString() != dataset.ToString())
|
---|
150 | throw new InvalidOperationException("Could not persist dataset. The database already contains a different dataset.");
|
---|
151 | return problem;
|
---|
152 | }
|
---|
153 |
|
---|
154 | private Problem PersistProblem(Dataset dataset) {
|
---|
155 | Problem problem;
|
---|
156 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
157 | if (ctx.Problems.Count() != 0)
|
---|
158 | throw new InvalidOperationException("Could not persist dataset. A dataset is already saved in the database.");
|
---|
159 | problem = new Problem(dataset);
|
---|
160 | ctx.Problems.InsertOnSubmit(problem);
|
---|
161 | foreach (string variable in dataset.VariableNames) {
|
---|
162 | ctx.Variables.InsertOnSubmit(new Variable(variable));
|
---|
163 | }
|
---|
164 | ctx.SubmitChanges();
|
---|
165 | }
|
---|
166 | return problem;
|
---|
167 | }
|
---|
168 |
|
---|
169 | #endregion
|
---|
170 |
|
---|
171 | #region Algorithm
|
---|
172 | public Algorithm GetOrCreateAlgorithm(string name, string description) {
|
---|
173 | Algorithm algorithm;
|
---|
174 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
175 | var algorithms = from algo in ctx.Algorithms
|
---|
176 | where algo.Name == name && algo.Description == description
|
---|
177 | select algo;
|
---|
178 | if (algorithms.Count() == 0) {
|
---|
179 | algorithm = new Algorithm(name, description);
|
---|
180 | ctx.Algorithms.InsertOnSubmit(algorithm);
|
---|
181 | ctx.SubmitChanges();
|
---|
182 | } else if (algorithms.Count() == 1)
|
---|
183 | algorithm = algorithms.Single();
|
---|
184 | else
|
---|
185 | throw new ArgumentException("Could not get Algorithm. More than one algorithm with the name " + name + " are saved in database.");
|
---|
186 | }
|
---|
187 | return algorithm;
|
---|
188 | }
|
---|
189 | #endregion
|
---|
190 |
|
---|
191 | #region Variables
|
---|
192 | public Dictionary<string, Variable> GetAllVariables() {
|
---|
193 | Dictionary<string, Variable> dict = new Dictionary<string, Variable>();
|
---|
194 | dict = ctx.Variables.ToDictionary<Variable, string>(delegate(Variable v) { return v.Name; });
|
---|
195 | return dict;
|
---|
196 | }
|
---|
197 | #endregion
|
---|
198 |
|
---|
199 | #region Result
|
---|
200 | public Result GetOrCreateResult(string name) {
|
---|
201 | Result result;
|
---|
202 | using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
|
---|
203 | var results = from r in ctx.Results
|
---|
204 | where r.Name == name
|
---|
205 | select r;
|
---|
206 | if (results.Count() == 0) {
|
---|
207 | result = new Result(name);
|
---|
208 | ctx.Results.InsertOnSubmit(result);
|
---|
209 | ctx.SubmitChanges();
|
---|
210 | } else if (results.Count() == 1)
|
---|
211 | result = results.Single();
|
---|
212 | else
|
---|
213 | throw new ArgumentException("Could not get result. More than one result with the name " + name + " are saved in database.");
|
---|
214 | }
|
---|
215 | return result;
|
---|
216 | }
|
---|
217 |
|
---|
218 | public IEnumerable<IResult> GetAllResults() {
|
---|
219 | return ctx.Results.ToList().Cast<IResult>();
|
---|
220 | }
|
---|
221 |
|
---|
222 | public IEnumerable<IResult> GetAllResultsForInputVariables() {
|
---|
223 | return (from ir in ctx.InputVariableResults select ir.Result).Distinct().ToList().Cast<IResult>();
|
---|
224 | }
|
---|
225 |
|
---|
226 | #endregion
|
---|
227 |
|
---|
228 | #region ModelResult
|
---|
229 | public IEnumerable<IModelResult> GetModelResults(IModel model) {
|
---|
230 | var results = from result in ctx.ModelResults
|
---|
231 | where result.Model == model
|
---|
232 | select result;
|
---|
233 | return results.ToList().Cast<IModelResult>();
|
---|
234 | }
|
---|
235 | #endregion
|
---|
236 |
|
---|
237 | #region InputVariableResults
|
---|
238 | public IEnumerable<IInputVariableResult> GetInputVariableResults(IModel model) {
|
---|
239 | var inputResults = from ir in ctx.InputVariableResults
|
---|
240 | where ir.Model == model
|
---|
241 | select ir;
|
---|
242 | return inputResults.ToList().Cast<IInputVariableResult>();
|
---|
243 | }
|
---|
244 |
|
---|
245 | #endregion
|
---|
246 |
|
---|
247 | #region Model
|
---|
248 | public IEnumerable<IModel> GetAllModels() {
|
---|
249 | return ctx.Models.ToList().Cast<IModel>();
|
---|
250 | }
|
---|
251 |
|
---|
252 | public byte[] GetModelData(IModel model) {
|
---|
253 | var data = (from md in ctx.ModelData
|
---|
254 | where md.Model == model
|
---|
255 | select md);
|
---|
256 | if (data.Count() == 0)
|
---|
257 | return null;
|
---|
258 | return data.Single().Data;
|
---|
259 | }
|
---|
260 | #endregion
|
---|
261 |
|
---|
262 | }
|
---|
263 | }
|
---|