Changeset 2451
- Timestamp:
- 10/27/09 11:51:31 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.CEDMA.Server/3.3/ExecuterBase.cs
r2391 r2451 71 71 this.dispatcher = dispatcher; 72 72 this.databaseService = databaseService; 73 this.databaseService.Connect(); 73 74 StoredJobs = databaseService.GetAllModels().Count(); 75 this.databaseService.Disconnect(); 74 76 } 75 77 … … 94 96 protected void StoreResults(HeuristicLab.Modeling.IAlgorithm finishedAlgorithm) { 95 97 CalculatedJobs++; 98 databaseService.Connect(); 96 99 databaseService.Persist(finishedAlgorithm); 100 databaseService.Disconnect(); 97 101 StoredJobs++; 98 102 OnChanged(); -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/Server.cs
r2398 r2451 38 38 public class Server : IViewable { 39 39 private static readonly string sqlServerCompactFile = AppDomain.CurrentDomain.BaseDirectory + "HeuristicLab.Modeling.database.sdf"; 40 private static readonly string sqlServerCompactConnectionString = @"Data Source=" + sqlServerCompactFile;41 40 42 41 private DatabaseService database; … … 57 56 58 57 public Server() { 59 database = new DatabaseService(sqlServerCompactConnectionString); 58 database = new DatabaseService(sqlServerCompactFile); 59 database.Connect(); 60 60 dataset = database.GetDataset(); 61 61 if (dataset == null) 62 dataset = new Dataset(); 62 dataset = new Dataset(); 63 database.Disconnect(); 63 64 } 64 65 -
trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/ModelData.cs
r2382 r2451 36 36 public ModelData(Model model, byte[] data) 37 37 : base() { 38 this.modelId = model.Id; 38 if (model.Id == -1) 39 this.model.Entity = model; 40 else 41 this.modelId = model.Id; 39 42 this.data = data; 40 43 } -
trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs
r2390 r2451 32 32 namespace HeuristicLab.Modeling.Database.SQLServerCompact { 33 33 public class DatabaseService : IModelingDatabase { 34 private readonly string connection; 35 public DatabaseService(string connection) { 36 this.connection = connection; 37 Connect(); 38 if (!ctx.DatabaseExists()) 39 ctx.CreateDatabase(); 34 private readonly string fileName; 35 36 public DatabaseService(string fileName) { 37 this.fileName = fileName; 38 this.readOnly = false; 39 40 } 41 public DatabaseService(string fileName, bool readOnly) 42 : this(fileName) { 43 this.readOnly = readOnly; 44 } 45 46 private string ConnectionString { 47 get { 48 string connection = "Data Source =" + fileName + ";"; 49 if (this.readOnly) 50 connection += "File Mode = Read Only; Temp Path =" + System.IO.Path.GetTempPath() + ";"; 51 else 52 connection += "File Mode = Shared Read;"; 53 return connection; 54 } 55 } 56 57 private bool readOnly; 58 public bool ReadOnly { 59 get { return this.readOnly; } 60 set { 61 if (ctx != null) 62 throw new InvalidOperationException("Could not change readonly attribute of DatabaseService because connection is opened."); 63 this.readOnly = value; 64 } 40 65 } 41 66 … … 45 70 Connect(); 46 71 ctx.CreateDatabase(); 72 Disconnect(); 47 73 } 48 74 … … 52 78 Disconnect(); 53 79 54 ctx = new ModelingDataContext( connection);80 ctx = new ModelingDataContext(this.ConnectionString); 55 81 DataLoadOptions dlo = new DataLoadOptions(); 56 82 dlo.LoadWith<ModelResult>(mr => mr.Result); … … 61 87 dlo.LoadWith<Model>(m => m.Algorithm); 62 88 ctx.LoadOptions = dlo; 89 90 if (!ctx.DatabaseExists()) 91 ctx.CreateDatabase(); 63 92 } 64 93 … … 73 102 public IEnumerable<IModel> GetAllModels() { 74 103 return ctx.Models.ToList().Cast<IModel>(); 104 } 105 106 public IEnumerable<int> GetAllModelIds() { 107 return from m in ctx.Models 108 select m.Id; 75 109 } 76 110 … … 118 152 } 119 153 154 public IModel GetModel(int id) { 155 var model = ctx.Models.Where(m => m.Id == id); 156 if (model.Count() == 1) 157 return model.Single(); 158 return null; 159 } 160 120 161 public void PersistModel(IModel model) { 121 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 122 Model m = (Model)model; 123 //check if model has to be updated or inserted 124 if (ctx.Models.Any(x => x.Id == model.Id)) { 125 Model orginal = ctx.Models.GetOriginalEntityState(m); 126 if (orginal == null) 127 ctx.Models.Attach(m); 128 ctx.Refresh(RefreshMode.KeepCurrentValues, m); 129 } else 130 ctx.Models.InsertOnSubmit(m); 131 ctx.SubmitChanges(); 132 } 162 Model m = (Model)model; 163 //check if model has to be updated or inserted 164 if (ctx.Models.Any(x => x.Id == model.Id)) { 165 Model orginal = ctx.Models.GetOriginalEntityState(m); 166 if (orginal == null) 167 ctx.Models.Attach(m); 168 ctx.Refresh(RefreshMode.KeepCurrentValues, m); 169 } else 170 ctx.Models.InsertOnSubmit(m); 171 ctx.SubmitChanges(); 133 172 } 134 173 … … 157 196 public void PersistProblem(Dataset dataset) { 158 197 Problem problem; 159 using (ModelingDataContext ctx = new ModelingDataContext( connection)) {198 using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) { 160 199 if (ctx.Problems.Count() != 0) 161 200 throw new InvalidOperationException("Could not persist dataset. A dataset is already saved in the database."); … … 187 226 public void PersistPredictor(IModel model, IPredictor predictor) { 188 227 Model m = (Model)model; 189 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 190 ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m)); 191 ctx.ModelResults.DeleteAllOnSubmit(ctx.ModelResults.Where(x => x.Model == m)); 192 ctx.InputVariableResults.DeleteAllOnSubmit(ctx.InputVariableResults.Where(x => x.Model == m)); 193 ctx.InputVariables.DeleteAllOnSubmit(ctx.InputVariables.Where(x => x.Model == m)); 194 195 ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(predictor))); 196 foreach (string variableName in predictor.GetInputVariables()) 197 ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName))); 198 199 ctx.SubmitChanges(); 200 } 228 ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m)); 229 ctx.ModelResults.DeleteAllOnSubmit(ctx.ModelResults.Where(x => x.Model == m)); 230 ctx.InputVariableResults.DeleteAllOnSubmit(ctx.InputVariableResults.Where(x => x.Model == m)); 231 ctx.InputVariables.DeleteAllOnSubmit(ctx.InputVariables.Where(x => x.Model == m)); 232 233 ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(predictor))); 234 foreach (string variableName in predictor.GetInputVariables()) 235 ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName))); 236 237 ctx.SubmitChanges(); 201 238 } 202 239 … … 214 251 public IAlgorithm GetOrPersistAlgorithm(string algorithmName) { 215 252 Algorithm algorithm; 216 using (ModelingDataContext ctx = new ModelingDataContext( connection)) {253 using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) { 217 254 var algorithms = ctx.Algorithms.Where(algo => algo.Name == algorithmName); 218 255 if (algorithms.Count() == 0) { … … 230 267 public IResult GetOrPersistResult(string resultName) { 231 268 Result result; 232 using (ModelingDataContext ctx = new ModelingDataContext( connection)) {269 using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) { 233 270 var results = ctx.Results.Where(r => r.Name == resultName); 234 271 if (results.Count() == 0) { … … 246 283 public IMetaData GetOrPersistMetaData(string metaDataName) { 247 284 MetaData metadata; 248 using (ModelingDataContext ctx = new ModelingDataContext( connection)) {285 using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) { 249 286 var md = ctx.MetaData.Where(r => r.Name == metaDataName); 250 287 if (md.Count() == 0) { … … 277 314 278 315 public void PersistModelResults(IModel model, IEnumerable<IModelResult> modelResults) { 279 using (ModelingDataContext ctx = new ModelingDataContext( connection)) {316 using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) { 280 317 ctx.ModelResults.DeleteAllOnSubmit(GetModelResults(model).Cast<ModelResult>()); 281 318 ctx.ModelResults.InsertAllOnSubmit(modelResults.Cast<ModelResult>()); … … 296 333 297 334 public void PersistInputVariableResults(IModel model, IEnumerable<IInputVariableResult> inputVariableResults) { 298 using (ModelingDataContext ctx = new ModelingDataContext( connection)) {335 using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) { 299 336 ctx.InputVariableResults.DeleteAllOnSubmit(GetInputVariableResults(model).Cast<InputVariableResult>()); 300 337 ctx.InputVariableResults.InsertAllOnSubmit(inputVariableResults.Cast<InputVariableResult>()); … … 310 347 311 348 public void PersistModelMetaData(IModel model, IEnumerable<IModelMetaData> modelMetaData) { 312 using (ModelingDataContext ctx = new ModelingDataContext( connection)) {349 using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) { 313 350 ctx.ModelMetaData.DeleteAllOnSubmit(GetModelMetaData(model).Cast<ModelMetaData>()); 314 351 ctx.ModelMetaData.InsertAllOnSubmit(modelMetaData.Cast<ModelMetaData>()); … … 326 363 Algorithm algorithm = (Algorithm)GetOrPersistAlgorithm(algorithmName); 327 364 Variable targetVariable = (Variable)GetVariable(model.TargetVariable); 328 329 365 Model m = (Model)CreateModel(null, model.Type, algorithm, targetVariable, model.TrainingSamplesStart, model.TrainingSamplesEnd, 330 366 model.ValidationSamplesStart, model.ValidationSamplesEnd, model.TestSamplesStart, model.TestSamplesEnd); 331 PersistModel(m); 332 PersistPredictor(m, model.Predictor); 333 334 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 367 368 using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) { 369 ctx.Models.InsertOnSubmit(m); 370 ctx.SubmitChanges(); 371 ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(model.Predictor))); 372 foreach (string variableName in model.Predictor.GetInputVariables()) 373 ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName))); 374 335 375 foreach (KeyValuePair<string, double> pair in model.MetaData) { 336 376 MetaData metaData = (MetaData)GetOrPersistMetaData(pair.Key); 337 377 ctx.ModelMetaData.InsertOnSubmit(new ModelMetaData(m, metaData, pair.Value)); 338 378 } 339 ctx.SubmitChanges(); 340 } 341 342 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 379 343 380 foreach (KeyValuePair<ModelingResult, double> pair in model.Results) { 344 381 Result result = (Result)GetOrPersistResult(pair.Key.ToString()); 345 382 ctx.ModelResults.InsertOnSubmit(new ModelResult(m, result, pair.Value)); 346 383 } 347 ctx.SubmitChanges(); 348 } 349 350 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 384 351 385 foreach (InputVariable variable in ctx.InputVariables.Where(iv => iv.Model == m)) { 352 386 foreach (KeyValuePair<ModelingResult, double> variableResult in model.GetVariableResults(variable.Variable.Name)) { … … 355 389 } 356 390 } 391 357 392 ctx.SubmitChanges(); 358 393 } -
trunk/sources/HeuristicLab.Modeling.Database/3.2/IModelingDatabase.cs
r2389 r2451 28 28 namespace HeuristicLab.Modeling.Database { 29 29 public interface IModelingDatabase { 30 bool ReadOnly { get; set; } 30 31 void Connect(); 31 32 void EmptyDatabase(); … … 33 34 34 35 IEnumerable<IModel> GetAllModels(); 36 IEnumerable<int> GetAllModelIds(); 35 37 IEnumerable<IVariable> GetAllVariables(); 36 38 IEnumerable<IResult> GetAllResults(); … … 53 55 IVariable GetVariable(string variableName); 54 56 57 IModel GetModel(int id); 55 58 IPredictor GetModelPredictor(IModel model); 56 59 void PersistPredictor(IModel model, IPredictor predictor);
Note: See TracChangeset
for help on using the changeset viewer.