Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/23/09 16:23:44 (13 years ago)
Author:
mkommend
Message:

adapted SQLServerCompact database backend to meet new requirements regarding multiple connections (ticket #800)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs

    r2452 r2525  
    160160
    161161    public void PersistModel(IModel model) {
    162       using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    163         Model m = (Model)model;
    164         //check if model has to be updated or inserted
    165         if (ctx.Models.Any(x => x.Id == model.Id)) {
    166           Model orginal = ctx.Models.GetOriginalEntityState(m);
    167           if (orginal == null)
    168             ctx.Models.Attach(m);
    169           ctx.Refresh(RefreshMode.KeepCurrentValues, m);
    170         } else
    171           ctx.Models.InsertOnSubmit(m);
    172         ctx.SubmitChanges();
    173       }
     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();
    174172    }
    175173
     
    198196    public void PersistProblem(Dataset dataset) {
    199197      Problem problem;
    200       using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    201         if (ctx.Problems.Count() != 0)
    202           throw new InvalidOperationException("Could not persist dataset. A dataset is already saved in the database.");
    203         problem = new Problem(dataset);
    204         ctx.Problems.InsertOnSubmit(problem);
    205         foreach (string variable in dataset.VariableNames) {
    206           ctx.Variables.InsertOnSubmit(new Variable(variable));
    207         }
    208         ctx.SubmitChanges();
    209       }
     198      if (ctx.Problems.Count() != 0)
     199        throw new InvalidOperationException("Could not persist dataset. A dataset is already saved in the database.");
     200      problem = new Problem(dataset);
     201      ctx.Problems.InsertOnSubmit(problem);
     202      foreach (string variable in dataset.VariableNames) {
     203        ctx.Variables.InsertOnSubmit(new Variable(variable));
     204      }
     205      ctx.SubmitChanges();
    210206    }
    211207
     
    227223
    228224    public void PersistPredictor(IModel model, IPredictor predictor) {
    229       using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    230         Model m = (Model)model;
    231         ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m));
    232         ctx.ModelResults.DeleteAllOnSubmit(ctx.ModelResults.Where(x => x.Model == m));
    233         ctx.InputVariableResults.DeleteAllOnSubmit(ctx.InputVariableResults.Where(x => x.Model == m));
    234         ctx.InputVariables.DeleteAllOnSubmit(ctx.InputVariables.Where(x => x.Model == m));
    235 
    236         ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(predictor)));
    237         foreach (string variableName in predictor.GetInputVariables())
    238           ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName)));
    239 
    240         ctx.SubmitChanges();
    241       }
     225      Model m = (Model)model;
     226      ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m));
     227      ctx.ModelResults.DeleteAllOnSubmit(ctx.ModelResults.Where(x => x.Model == m));
     228      ctx.InputVariableResults.DeleteAllOnSubmit(ctx.InputVariableResults.Where(x => x.Model == m));
     229      ctx.InputVariables.DeleteAllOnSubmit(ctx.InputVariables.Where(x => x.Model == m));
     230
     231      ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(predictor)));
     232      foreach (string variableName in predictor.GetInputVariables())
     233        ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName)));
     234
     235      ctx.SubmitChanges();
    242236    }
    243237
     
    255249    public IAlgorithm GetOrPersistAlgorithm(string algorithmName) {
    256250      Algorithm algorithm;
    257       using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    258         var algorithms = ctx.Algorithms.Where(algo => algo.Name == algorithmName);
    259         if (algorithms.Count() == 0) {
    260           algorithm = new Algorithm(algorithmName, "");
    261           ctx.Algorithms.InsertOnSubmit(algorithm);
    262           ctx.SubmitChanges();
    263         } else if (algorithms.Count() == 1)
    264           algorithm = algorithms.Single();
    265         else
    266           throw new ArgumentException("Could not get Algorithm. More than one algorithm with the name " + algorithmName + " are saved in database.");
    267       }
     251      var algorithms = ctx.Algorithms.Where(algo => algo.Name == algorithmName);
     252      if (algorithms.Count() == 0) {
     253        algorithm = new Algorithm(algorithmName, "");
     254        ctx.Algorithms.InsertOnSubmit(algorithm);
     255        ctx.SubmitChanges();
     256      } else if (algorithms.Count() == 1)
     257        algorithm = algorithms.Single();
     258      else
     259        throw new ArgumentException("Could not get Algorithm. More than one algorithm with the name " + algorithmName + " are saved in database.");
    268260      return algorithm;
    269261    }
     
    271263    public IResult GetOrPersistResult(string resultName) {
    272264      Result result;
    273       using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    274         var results = ctx.Results.Where(r => r.Name == resultName);
    275         if (results.Count() == 0) {
    276           result = new Result(resultName);
    277           ctx.Results.InsertOnSubmit(result);
    278           ctx.SubmitChanges();
    279         } else if (results.Count() == 1)
    280           result = results.Single();
    281         else
    282           throw new ArgumentException("Could not get result. More than one result with the name " + resultName + " are saved in database.");
    283       }
     265      var results = ctx.Results.Where(r => r.Name == resultName);
     266      if (results.Count() == 0) {
     267        result = new Result(resultName);
     268        ctx.Results.InsertOnSubmit(result);
     269        ctx.SubmitChanges();
     270      } else if (results.Count() == 1)
     271        result = results.Single();
     272      else
     273        throw new ArgumentException("Could not get result. More than one result with the name " + resultName + " are saved in database.");
    284274      return result;
    285275    }
     
    287277    public IMetaData GetOrPersistMetaData(string metaDataName) {
    288278      MetaData metadata;
    289       using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    290         var md = ctx.MetaData.Where(r => r.Name == metaDataName);
    291         if (md.Count() == 0) {
    292           metadata = new MetaData(metaDataName);
    293           ctx.MetaData.InsertOnSubmit(metadata);
    294           ctx.SubmitChanges();
    295         } else if (md.Count() == 1)
    296           metadata = md.Single();
    297         else
    298           throw new ArgumentException("Could not get metadata. More than one metadata with the name " + metaDataName + " are saved in database.");
    299       }
     279      var md = ctx.MetaData.Where(r => r.Name == metaDataName);
     280      if (md.Count() == 0) {
     281        metadata = new MetaData(metaDataName);
     282        ctx.MetaData.InsertOnSubmit(metadata);
     283        ctx.SubmitChanges();
     284      } else if (md.Count() == 1)
     285        metadata = md.Single();
     286      else
     287        throw new ArgumentException("Could not get metadata. More than one metadata with the name " + metaDataName + " are saved in database.");
    300288      return metadata;
    301289    }
     
    318306
    319307    public void PersistModelResults(IModel model, IEnumerable<IModelResult> modelResults) {
    320       using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    321         ctx.ModelResults.DeleteAllOnSubmit(GetModelResults(model).Cast<ModelResult>());
    322         ctx.ModelResults.InsertAllOnSubmit(modelResults.Cast<ModelResult>());
    323         ctx.SubmitChanges();
    324       }
     308      ctx.ModelResults.DeleteAllOnSubmit(GetModelResults(model).Cast<ModelResult>());
     309      ctx.ModelResults.InsertAllOnSubmit(modelResults.Cast<ModelResult>());
     310      ctx.SubmitChanges();
    325311    }
    326312
     
    337323
    338324    public void PersistInputVariableResults(IModel model, IEnumerable<IInputVariableResult> inputVariableResults) {
    339       using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    340         ctx.InputVariableResults.DeleteAllOnSubmit(GetInputVariableResults(model).Cast<InputVariableResult>());
    341         ctx.InputVariableResults.InsertAllOnSubmit(inputVariableResults.Cast<InputVariableResult>());
    342         ctx.SubmitChanges();
    343       }
     325      ctx.InputVariableResults.DeleteAllOnSubmit(GetInputVariableResults(model).Cast<InputVariableResult>());
     326      ctx.InputVariableResults.InsertAllOnSubmit(inputVariableResults.Cast<InputVariableResult>());
     327      ctx.SubmitChanges();
    344328    }
    345329
     
    351335
    352336    public void PersistModelMetaData(IModel model, IEnumerable<IModelMetaData> modelMetaData) {
    353       using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    354         ctx.ModelMetaData.DeleteAllOnSubmit(GetModelMetaData(model).Cast<ModelMetaData>());
    355         ctx.ModelMetaData.InsertAllOnSubmit(modelMetaData.Cast<ModelMetaData>());
    356         ctx.SubmitChanges();
    357       }
     337      ctx.ModelMetaData.DeleteAllOnSubmit(GetModelMetaData(model).Cast<ModelMetaData>());
     338      ctx.ModelMetaData.InsertAllOnSubmit(modelMetaData.Cast<ModelMetaData>());
     339      ctx.SubmitChanges();
    358340    }
    359341
     
    366348    public IModel Persist(HeuristicLab.Modeling.IAnalyzerModel model, string algorithmName, string algorithmDescription) {
    367349      Algorithm algorithm = (Algorithm)GetOrPersistAlgorithm(algorithmName);
    368       Variable targetVariable = (Variable)GetVariable(model.TargetVariable);
     350      Variable targetVariable = (Variable) GetVariable(model.TargetVariable);
    369351      Model m = (Model)CreateModel(null, model.Type, algorithm, targetVariable, model.TrainingSamplesStart, model.TrainingSamplesEnd,
    370352        model.ValidationSamplesStart, model.ValidationSamplesEnd, model.TestSamplesStart, model.TestSamplesEnd);
    371 
    372       using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    373         ctx.Models.InsertOnSubmit(m);
    374         ctx.SubmitChanges();
    375         ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(model.Predictor)));
    376         foreach (string variableName in model.Predictor.GetInputVariables())
    377           ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName)));
    378 
    379         foreach (KeyValuePair<string, double> pair in model.MetaData) {
    380           MetaData metaData = (MetaData)GetOrPersistMetaData(pair.Key);
    381           ctx.ModelMetaData.InsertOnSubmit(new ModelMetaData(m, metaData, pair.Value));
     353      ctx.Models.InsertOnSubmit(m);
     354      ctx.SubmitChanges();
     355      ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(model.Predictor)));
     356
     357      foreach (string variableName in model.Predictor.GetInputVariables())
     358        ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName)));
     359
     360      foreach (KeyValuePair<string, double> pair in model.MetaData) {
     361        MetaData metaData = (MetaData)GetOrPersistMetaData(pair.Key);
     362        ctx.ModelMetaData.InsertOnSubmit(new ModelMetaData(m, metaData, pair.Value));
     363      }
     364
     365      foreach (KeyValuePair<ModelingResult, double> pair in model.Results) {
     366        Result result = (Result)GetOrPersistResult(pair.Key.ToString());
     367        ctx.ModelResults.InsertOnSubmit(new ModelResult(m, result, pair.Value));
     368      }
     369
     370      foreach (InputVariable variable in ctx.InputVariables.Where(iv => iv.Model == m)) {
     371        foreach (KeyValuePair<ModelingResult, double> variableResult in model.GetVariableResults(variable.Variable.Name)) {
     372          Result result = (Result)GetOrPersistResult(variableResult.Key.ToString());
     373          ctx.InputVariableResults.InsertOnSubmit(new InputVariableResult(variable, result, variableResult.Value));
    382374        }
    383 
    384         foreach (KeyValuePair<ModelingResult, double> pair in model.Results) {
    385           Result result = (Result)GetOrPersistResult(pair.Key.ToString());
    386           ctx.ModelResults.InsertOnSubmit(new ModelResult(m, result, pair.Value));
    387         }
    388 
    389         foreach (InputVariable variable in ctx.InputVariables.Where(iv => iv.Model == m)) {
    390           foreach (KeyValuePair<ModelingResult, double> variableResult in model.GetVariableResults(variable.Variable.Name)) {
    391             Result result = (Result)GetOrPersistResult(variableResult.Key.ToString());
    392             ctx.InputVariableResults.InsertOnSubmit(new InputVariableResult(variable, result, variableResult.Value));
    393           }
    394         }
    395 
    396         ctx.SubmitChanges();
    397       }
     375      }
     376      ctx.SubmitChanges();
    398377
    399378      //if connected to database return inserted model
Note: See TracChangeset for help on using the changeset viewer.