- Timestamp:
- 11/24/09 15:15:16 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs
r2525 r2530 87 87 dlo.LoadWith<Model>(m => m.Algorithm); 88 88 ctx.LoadOptions = dlo; 89 90 if (!ctx.DatabaseExists()) 89 //ctx.Log = System.Console.Out; 90 91 if (!ctx.DatabaseExists() && !this.ReadOnly) 91 92 ctx.CreateDatabase(); 92 93 } … … 100 101 } 101 102 103 public void Commit() { 104 if (ctx != null) 105 ctx.SubmitChanges(); 106 } 107 108 private void CheckConnection() { 109 CheckConnection(false); 110 } 111 112 private void CheckConnection(bool writeEnabled) { 113 if (ctx == null) 114 throw new InvalidOperationException("Could not perform operation, when not connected to the database."); 115 if (writeEnabled && this.ReadOnly) 116 throw new InvalidOperationException("Could not perform update operation, when database is in readonly mode."); 117 } 118 102 119 public IEnumerable<IModel> GetAllModels() { 120 this.CheckConnection(); 103 121 return ctx.Models.ToList().Cast<IModel>(); 104 122 } 105 123 106 124 public IEnumerable<int> GetAllModelIds() { 125 this.CheckConnection(); 107 126 return from m in ctx.Models 108 127 select m.Id; … … 110 129 111 130 public IEnumerable<IVariable> GetAllVariables() { 131 this.CheckConnection(); 112 132 return ctx.Variables.ToList().Cast<IVariable>(); 113 133 } 114 134 115 135 public IEnumerable<IResult> GetAllResults() { 136 this.CheckConnection(); 116 137 return ctx.Results.ToList().Cast<IResult>(); 117 138 } 118 139 119 140 public IEnumerable<IResult> GetAllResultsForInputVariables() { 141 this.CheckConnection(); 120 142 return (from ir in ctx.InputVariableResults select ir.Result).Distinct().ToList().Cast<IResult>(); 121 143 } 122 144 123 145 public IEnumerable<IMetaData> GetAllMetaData() { 146 this.CheckConnection(); 124 147 return ctx.MetaData.ToList().Cast<IMetaData>(); 125 148 } 126 149 127 150 public IEnumerable<IAlgorithm> GetAllAlgorithms() { 151 this.CheckConnection(); 128 152 return ctx.Algorithms.ToList().Cast<IAlgorithm>(); 129 153 } … … 153 177 154 178 public IModel GetModel(int id) { 179 this.CheckConnection(); 155 180 var model = ctx.Models.Where(m => m.Id == id); 156 181 if (model.Count() == 1) … … 160 185 161 186 public void PersistModel(IModel model) { 187 this.CheckConnection(true); 162 188 Model m = (Model)model; 163 189 //check if model has to be updated or inserted … … 169 195 } else 170 196 ctx.Models.InsertOnSubmit(m); 171 ctx.SubmitChanges();172 197 } 173 198 174 199 public void DeleteModel(IModel model) { 200 this.CheckConnection(true); 175 201 Model m = (Model)model; 176 202 ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m)); … … 187 213 188 214 public Dataset GetDataset() { 215 this.CheckConnection(); 189 216 if (ctx.Problems.Count() > 1) 190 217 throw new InvalidOperationException("Could not get dataset. More than one problems are persisted in the database."); … … 195 222 196 223 public void PersistProblem(Dataset dataset) { 224 this.CheckConnection(true); 197 225 Problem problem; 198 226 if (ctx.Problems.Count() != 0) … … 203 231 ctx.Variables.InsertOnSubmit(new Variable(variable)); 204 232 } 205 ctx.SubmitChanges();206 233 } 207 234 208 235 public IVariable GetVariable(string variableName) { 236 this.CheckConnection(); 209 237 var variables = ctx.Variables.Where(v => v.Name == variableName); 210 238 if (variables.Count() != 1) … … 214 242 215 243 public IPredictor GetModelPredictor(IModel model) { 244 this.CheckConnection(); 216 245 var data = (from md in ctx.ModelData 217 246 where md.Model == model … … 223 252 224 253 public void PersistPredictor(IModel model, IPredictor predictor) { 254 this.CheckConnection(true); 225 255 Model m = (Model)model; 226 256 ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m)); … … 232 262 foreach (string variableName in predictor.GetInputVariables()) 233 263 ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName))); 234 235 ctx.SubmitChanges();236 264 } 237 265 238 266 public IInputVariable GetInputVariable(IModel model, string inputVariableName) { 267 this.CheckConnection(); 239 268 var inputVariables = ctx.InputVariables.Where(i => i.Model == model && i.Variable.Name == inputVariableName); 240 269 if (inputVariables.Count() == 1) … … 248 277 249 278 public IAlgorithm GetOrPersistAlgorithm(string algorithmName) { 279 this.CheckConnection(); 250 280 Algorithm algorithm; 251 281 var algorithms = ctx.Algorithms.Where(algo => algo.Name == algorithmName); 252 282 if (algorithms.Count() == 0) { 253 283 algorithm = new Algorithm(algorithmName, ""); 284 this.CheckConnection(true); 254 285 ctx.Algorithms.InsertOnSubmit(algorithm); 255 286 ctx.SubmitChanges(); … … 262 293 263 294 public IResult GetOrPersistResult(string resultName) { 295 this.CheckConnection(); 264 296 Result result; 265 297 var results = ctx.Results.Where(r => r.Name == resultName); 266 298 if (results.Count() == 0) { 299 this.CheckConnection(true); 267 300 result = new Result(resultName); 268 301 ctx.Results.InsertOnSubmit(result); … … 276 309 277 310 public IMetaData GetOrPersistMetaData(string metaDataName) { 311 this.CheckConnection(); 278 312 MetaData metadata; 279 313 var md = ctx.MetaData.Where(r => r.Name == metaDataName); 280 314 if (md.Count() == 0) { 315 this.CheckConnection(true); 281 316 metadata = new MetaData(metaDataName); 282 317 ctx.MetaData.InsertOnSubmit(metadata); … … 290 325 291 326 public IEnumerable<IModelResult> GetModelResults(IModel model) { 327 this.CheckConnection(); 292 328 return ctx.ModelResults.Where(mr => mr.Model == model).Cast<IModelResult>(); 293 329 } 294 330 public IEnumerable<IInputVariableResult> GetInputVariableResults(IModel model) { 331 this.CheckConnection(); 295 332 return ctx.InputVariableResults.Where(ivr => ivr.Model == model).Cast<IInputVariableResult>(); 296 333 } 297 334 public IEnumerable<IModelMetaData> GetModelMetaData(IModel model) { 335 this.CheckConnection(); 298 336 return ctx.ModelMetaData.Where(md => md.Model == model).Cast<IModelMetaData>(); 299 337 } … … 306 344 307 345 public void PersistModelResults(IModel model, IEnumerable<IModelResult> modelResults) { 346 this.CheckConnection(true); 308 347 ctx.ModelResults.DeleteAllOnSubmit(GetModelResults(model).Cast<ModelResult>()); 309 348 ctx.ModelResults.InsertAllOnSubmit(modelResults.Cast<ModelResult>()); 310 ctx.SubmitChanges();311 349 } 312 350 … … 323 361 324 362 public void PersistInputVariableResults(IModel model, IEnumerable<IInputVariableResult> inputVariableResults) { 363 this.CheckConnection(true); 325 364 ctx.InputVariableResults.DeleteAllOnSubmit(GetInputVariableResults(model).Cast<InputVariableResult>()); 326 365 ctx.InputVariableResults.InsertAllOnSubmit(inputVariableResults.Cast<InputVariableResult>()); 327 ctx.SubmitChanges();328 366 } 329 367 … … 335 373 336 374 public void PersistModelMetaData(IModel model, IEnumerable<IModelMetaData> modelMetaData) { 375 this.CheckConnection(true); 337 376 ctx.ModelMetaData.DeleteAllOnSubmit(GetModelMetaData(model).Cast<ModelMetaData>()); 338 377 ctx.ModelMetaData.InsertAllOnSubmit(modelMetaData.Cast<ModelMetaData>()); 339 ctx.SubmitChanges(); 340 } 341 342 public IModel Persist(HeuristicLab.Modeling.IAlgorithm algorithm) { 343 if (ctx.Problems.Count() == 0) 344 PersistProblem(algorithm.Dataset); 345 return Persist(algorithm.Model, algorithm.Name, algorithm.Description); 346 } 347 348 public IModel Persist(HeuristicLab.Modeling.IAnalyzerModel model, string algorithmName, string algorithmDescription) { 378 } 379 380 public void Persist(HeuristicLab.Modeling.IAnalyzerModel model, string algorithmName, string algorithmDescription) { 381 this.CheckConnection(true); 349 382 Algorithm algorithm = (Algorithm)GetOrPersistAlgorithm(algorithmName); 350 Variable targetVariable = (Variable) 383 Variable targetVariable = (Variable)GetVariable(model.TargetVariable); 351 384 Model m = (Model)CreateModel(null, model.Type, algorithm, targetVariable, model.TrainingSamplesStart, model.TrainingSamplesEnd, 352 385 model.ValidationSamplesStart, model.ValidationSamplesEnd, model.TestSamplesStart, model.TestSamplesEnd); 353 386 ctx.Models.InsertOnSubmit(m); 354 387 ctx.SubmitChanges(); 355 ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(model.Predictor))); 388 ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(model.Predictor))); 356 389 357 390 foreach (string variableName in model.Predictor.GetInputVariables()) … … 374 407 } 375 408 } 376 ctx.SubmitChanges(); 377 378 //if connected to database return inserted model 379 if (this.ctx != null) 380 return this.ctx.Models.Where(x => x.Id == m.Id).Single(); 381 return null; 409 ctx.SubmitChanges(); 382 410 } 383 411 } -
trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/PredictorPersister.cs
r2525 r2530 38 38 db.Connect(); 39 39 Dataset temp = db.GetDataset(); 40 if(temp == null) 41 db.PersistProblem(ds); 40 if (temp == null) { 41 db.PersistProblem(ds); 42 db.Commit(); 43 } 42 44 43 45 IAnalyzerModel model = new AnalyzerModel(); 44 46 DefaultModelAnalyzerOperators.PopulateAnalyzerModel(scope, model, modelType); 45 47 db.Persist(model, algorithm, algorithm); 48 db.Commit(); 46 49 db.Disconnect(); 47 50
Note: See TracChangeset
for help on using the changeset viewer.