Changeset 2451


Ignore:
Timestamp:
10/27/09 11:51:31 (13 years ago)
Author:
mkommend
Message:

added possibility to reload specific models and added readonly connection to HeuristicLab.Modeling (ticket #792)

Location:
trunk/sources
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/ExecuterBase.cs

    r2391 r2451  
    7171      this.dispatcher = dispatcher;
    7272      this.databaseService = databaseService;
     73      this.databaseService.Connect();
    7374      StoredJobs = databaseService.GetAllModels().Count();
     75      this.databaseService.Disconnect();
    7476    }
    7577
     
    9496    protected void StoreResults(HeuristicLab.Modeling.IAlgorithm finishedAlgorithm) {
    9597      CalculatedJobs++;
     98      databaseService.Connect();
    9699      databaseService.Persist(finishedAlgorithm);
     100      databaseService.Disconnect();
    97101      StoredJobs++;
    98102      OnChanged();
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/Server.cs

    r2398 r2451  
    3838  public class Server : IViewable {
    3939    private static readonly string sqlServerCompactFile = AppDomain.CurrentDomain.BaseDirectory + "HeuristicLab.Modeling.database.sdf";
    40     private static readonly string sqlServerCompactConnectionString = @"Data Source=" + sqlServerCompactFile;
    4140
    4241    private DatabaseService database;
     
    5756
    5857    public Server() {
    59       database = new DatabaseService(sqlServerCompactConnectionString);
     58      database = new DatabaseService(sqlServerCompactFile);
     59      database.Connect();
    6060      dataset = database.GetDataset();
    6161      if (dataset == null)
    62         dataset = new Dataset();     
     62        dataset = new Dataset();
     63      database.Disconnect();
    6364    }
    6465
  • trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/ModelData.cs

    r2382 r2451  
    3636    public ModelData(Model model, byte[] data)
    3737      : base() {
    38       this.modelId = model.Id;
     38      if (model.Id == -1)
     39        this.model.Entity = model;
     40      else
     41        this.modelId = model.Id;
    3942      this.data = data;
    4043    }
  • trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs

    r2390 r2451  
    3232namespace HeuristicLab.Modeling.Database.SQLServerCompact {
    3333  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      }
    4065    }
    4166
     
    4570      Connect();
    4671      ctx.CreateDatabase();
     72      Disconnect();
    4773    }
    4874
     
    5278        Disconnect();
    5379
    54       ctx = new ModelingDataContext(connection);
     80      ctx = new ModelingDataContext(this.ConnectionString);
    5581      DataLoadOptions dlo = new DataLoadOptions();
    5682      dlo.LoadWith<ModelResult>(mr => mr.Result);
     
    6187      dlo.LoadWith<Model>(m => m.Algorithm);
    6288      ctx.LoadOptions = dlo;
     89
     90      if (!ctx.DatabaseExists())
     91        ctx.CreateDatabase();
    6392    }
    6493
     
    73102    public IEnumerable<IModel> GetAllModels() {
    74103      return ctx.Models.ToList().Cast<IModel>();
     104    }
     105
     106    public IEnumerable<int> GetAllModelIds() {
     107      return from m in ctx.Models
     108             select m.Id;
    75109    }
    76110
     
    118152    }
    119153
     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
    120161    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();
    133172    }
    134173
     
    157196    public void PersistProblem(Dataset dataset) {
    158197      Problem problem;
    159       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     198      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    160199        if (ctx.Problems.Count() != 0)
    161200          throw new InvalidOperationException("Could not persist dataset. A dataset is already saved in the database.");
     
    187226    public void PersistPredictor(IModel model, IPredictor predictor) {
    188227      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();
    201238    }
    202239
     
    214251    public IAlgorithm GetOrPersistAlgorithm(string algorithmName) {
    215252      Algorithm algorithm;
    216       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     253      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    217254        var algorithms = ctx.Algorithms.Where(algo => algo.Name == algorithmName);
    218255        if (algorithms.Count() == 0) {
     
    230267    public IResult GetOrPersistResult(string resultName) {
    231268      Result result;
    232       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     269      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    233270        var results = ctx.Results.Where(r => r.Name == resultName);
    234271        if (results.Count() == 0) {
     
    246283    public IMetaData GetOrPersistMetaData(string metaDataName) {
    247284      MetaData metadata;
    248       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     285      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    249286        var md = ctx.MetaData.Where(r => r.Name == metaDataName);
    250287        if (md.Count() == 0) {
     
    277314
    278315    public void PersistModelResults(IModel model, IEnumerable<IModelResult> modelResults) {
    279       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     316      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    280317        ctx.ModelResults.DeleteAllOnSubmit(GetModelResults(model).Cast<ModelResult>());
    281318        ctx.ModelResults.InsertAllOnSubmit(modelResults.Cast<ModelResult>());
     
    296333
    297334    public void PersistInputVariableResults(IModel model, IEnumerable<IInputVariableResult> inputVariableResults) {
    298       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     335      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    299336        ctx.InputVariableResults.DeleteAllOnSubmit(GetInputVariableResults(model).Cast<InputVariableResult>());
    300337        ctx.InputVariableResults.InsertAllOnSubmit(inputVariableResults.Cast<InputVariableResult>());
     
    310347
    311348    public void PersistModelMetaData(IModel model, IEnumerable<IModelMetaData> modelMetaData) {
    312       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     349      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
    313350        ctx.ModelMetaData.DeleteAllOnSubmit(GetModelMetaData(model).Cast<ModelMetaData>());
    314351        ctx.ModelMetaData.InsertAllOnSubmit(modelMetaData.Cast<ModelMetaData>());
     
    326363      Algorithm algorithm = (Algorithm)GetOrPersistAlgorithm(algorithmName);
    327364      Variable targetVariable = (Variable)GetVariable(model.TargetVariable);
    328 
    329365      Model m = (Model)CreateModel(null, model.Type, algorithm, targetVariable, model.TrainingSamplesStart, model.TrainingSamplesEnd,
    330366        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
    335375        foreach (KeyValuePair<string, double> pair in model.MetaData) {
    336376          MetaData metaData = (MetaData)GetOrPersistMetaData(pair.Key);
    337377          ctx.ModelMetaData.InsertOnSubmit(new ModelMetaData(m, metaData, pair.Value));
    338378        }
    339         ctx.SubmitChanges();
    340       }
    341 
    342       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     379
    343380        foreach (KeyValuePair<ModelingResult, double> pair in model.Results) {
    344381          Result result = (Result)GetOrPersistResult(pair.Key.ToString());
    345382          ctx.ModelResults.InsertOnSubmit(new ModelResult(m, result, pair.Value));
    346383        }
    347         ctx.SubmitChanges();
    348       }
    349 
    350       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     384
    351385        foreach (InputVariable variable in ctx.InputVariables.Where(iv => iv.Model == m)) {
    352386          foreach (KeyValuePair<ModelingResult, double> variableResult in model.GetVariableResults(variable.Variable.Name)) {
     
    355389          }
    356390        }
     391
    357392        ctx.SubmitChanges();
    358393      }
  • trunk/sources/HeuristicLab.Modeling.Database/3.2/IModelingDatabase.cs

    r2389 r2451  
    2828namespace HeuristicLab.Modeling.Database {
    2929  public interface IModelingDatabase {
     30    bool ReadOnly { get; set; }
    3031    void Connect();
    3132    void EmptyDatabase();
     
    3334
    3435    IEnumerable<IModel> GetAllModels();
     36    IEnumerable<int> GetAllModelIds();
    3537    IEnumerable<IVariable> GetAllVariables();
    3638    IEnumerable<IResult> GetAllResults();
     
    5355    IVariable GetVariable(string variableName);
    5456
     57    IModel GetModel(int id);
    5558    IPredictor GetModelPredictor(IModel model);
    5659    void PersistPredictor(IModel model, IPredictor predictor);
Note: See TracChangeset for help on using the changeset viewer.