Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2179


Ignore:
Timestamp:
07/23/09 11:01:40 (15 years ago)
Author:
mkommend
Message:

implemented final prototyp of LinqToSql mapping (ticket #712)

Location:
branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.SQLiteBackend/3.2
Files:
1 deleted
10 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.SQLiteBackend/3.2/Algorithm.cs

    r2178 r2179  
    11using System;
    22using System.Collections.Generic;
     3using System.Linq;
    34using System.Data.Linq;
    45using System.Data.Linq.Mapping;
     
    910  public class Algorithm {
    1011    public Algorithm() {
    11       models = new EntitySet<Model>(attach_Model, detach_Model);
     12    }
     13
     14    public Algorithm(string name)
     15      : this() {
     16      this.name = name;
     17    }
     18
     19    public Algorithm(string name, string description)
     20      : this(name) {
     21      this.description = description;
    1222    }
    1323
    1424    private int id;
    15     [Column(Storage = "id", DbType = "integer", IsPrimaryKey = true, IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
     25    [Column(Storage = "id", IsPrimaryKey = true, IsDbGenerated = true)]
    1626    public int Id {
    1727      get { return this.id; }
     
    2030
    2131    private string name;
    22     [Column(Storage = "name", DbType = "VarChar(50) NOT NULL", CanBeNull = false)]
     32    [Column(Storage = "name", CanBeNull = false)]
    2333    public string Name {
    2434      get { return this.name; }
     
    2737
    2838    private string description;
    29     [Column(Storage = "description", DbType = "VarChar(200) NOT NULL", CanBeNull = true)]
     39    [Column(Storage = "description", CanBeNull = true)]
    3040    public string Description {
    3141      get { return this.description; }
     
    3343    }
    3444
    35     private EntitySet<Model> models;
    36     [Association(Storage = "models", ThisKey = "Id", OtherKey = "AlgorithmId")]
    37     public EntitySet<Model> Models {
     45    public IEnumerable<Model> ModelsCreatedByThisAlgorithm {
    3846      get {
    39         if (models.HasLoadedOrAssignedValues)
    40           return models;
    41         return null;
     47        using (ModelingDataContext ctx = new ModelingDataContext()) {
     48          var x = from model in ctx.Models
     49                  where model.Algorithm == this
     50                  select model;
     51          return x.AsEnumerable();
     52        }
    4253      }
    43       set {
    44         models.Assign(value);
    45       }
    46     }
    47 
    48     private void attach_Model(Model model) {
    49       model.Algorithm = this;
    50     }
    51 
    52     private void detach_Model(Model model) {
    53       model.Algorithm = null;
    5454    }
    5555  }
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.SQLiteBackend/3.2/HeuristicLab.Modeling.SQLiteBackend-3.2.csproj

    r2178 r2179  
    7373      <RequiredTargetFramework>3.5</RequiredTargetFramework>
    7474    </Reference>
     75    <Reference Include="System.Data.SQLite, Version=1.0.63.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=AMD64">
     76      <SpecificVersion>False</SpecificVersion>
     77      <HintPath>C:\Program Files (x86)\SQLite.NET\bin\x64\System.Data.SQLite.DLL</HintPath>
     78    </Reference>
    7579    <Reference Include="System.Xml.Linq">
    7680      <RequiredTargetFramework>3.5</RequiredTargetFramework>
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.SQLiteBackend/3.2/InputVariable.cs

    r2178 r2179  
    1313    }
    1414
    15     public InputVariable(Model model, Variable variable) :base() {
    16       this.modelId = model.Id;
     15    public InputVariable(Model model, Variable variable)
     16      : base() {
    1717      this.model.Entity = model;
    18       this.variableId = variable.Id;
    1918      this.variable.Entity = variable;
    2019    }
    2120
    2221    private int variableId;
    23     [Column(Storage = "variableId", DbType = "Int not null", IsPrimaryKey = true)]
    24     public int TargetVariableId {
     22    [Column(Storage = "variableId", IsPrimaryKey = true)]
     23    public int VariableId {
    2524      get { return this.variableId; }
    2625      private set {
    2726        if (variableId != value) {
     27          if (variable.HasLoadedOrAssignedValue)
     28            throw new ForeignKeyReferenceAlreadyHasValueException();
    2829          variableId = value;
    2930        }
     
    3233
    3334    private EntityRef<Variable> variable;
    34     [Association(Storage="variable", ThisKey="variableId", OtherKey="Id", IsForeignKey=true)]
     35    [Association(Storage = "variable", ThisKey = "VariableId", OtherKey = "Id", IsForeignKey = true)]
    3536    public Variable Variable {
    3637      get { return variable.Entity; }
     
    3839
    3940    private int modelId;
    40     [Column(Storage = "modelId", DbType = "Int not null", IsPrimaryKey = true)]
     41    [Column(Storage = "modelId", IsPrimaryKey = true)]
    4142    public int ModelId {
    4243      get { return this.modelId; }
    4344      private set {
    4445        if (modelId != value) {
     46          if (model.HasLoadedOrAssignedValue)
     47            throw new ForeignKeyReferenceAlreadyHasValueException();
    4548          modelId = value;
    4649        }
     
    4952
    5053    private EntityRef<Model> model;
    51     [Association(Storage = "model", ThisKey = "modelId", OtherKey = "Id", IsForeignKey = true)]
     54    [Association(Storage = "model", ThisKey = "ModelId", OtherKey = "Id", IsForeignKey = true)]
    5255    public Model Model {
    5356      get { return model.Entity; }
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.SQLiteBackend/3.2/InputVariableResult.cs

    r2178 r2179  
    11using System;
    22using System.Collections.Generic;
    3 using System.Linq;
     3using System.Data.Linq;
     4using System.Data.Linq.Mapping;
    45using System.Text;
    56
    67namespace HeuristicLab.Modeling.SQLiteBackend {
     8  [Table(Name="InputVariableResult")]
    79  public class InputVariableResult {
     10    public InputVariableResult() {
     11      this.model = default(EntityRef<Model>);
     12      this.variable = default(EntityRef<Variable>);
     13      this.result = default(EntityRef<Result>);
     14    }
     15
     16    private int variableId;
     17    [Column(Storage = "variableId", IsPrimaryKey = true)]
     18    public int VariableId {
     19      get { return this.variableId; }
     20      private set {
     21        if (variableId != value) {
     22          if (variable.HasLoadedOrAssignedValue)
     23            throw new ForeignKeyReferenceAlreadyHasValueException();
     24          variableId = value;
     25        }
     26      }
     27    }
     28
     29    private EntityRef<Variable> variable;
     30    [Association(Storage = "variable", ThisKey = "VariableId", OtherKey = "Id", IsForeignKey = true)]
     31    public Variable Variable {
     32      get { return variable.Entity; }
     33    }
     34
     35    private int modelId;
     36    [Column(Storage = "modelId", IsPrimaryKey = true)]
     37    public int ModelId {
     38      get { return this.modelId; }
     39      private set {
     40        if (modelId != value) {
     41          if (model.HasLoadedOrAssignedValue)
     42            throw new ForeignKeyReferenceAlreadyHasValueException();
     43          modelId = value;
     44        }
     45      }
     46    }
     47
     48    private EntityRef<Model> model;
     49    [Association(Storage = "model", ThisKey = "ModelId", OtherKey = "Id", IsForeignKey = true)]
     50    public Model Model {
     51      get { return model.Entity; }
     52    }
     53
     54    private int resultId;
     55    [Column(Storage = "resultId", IsPrimaryKey = true)]
     56    public int ResultId {
     57      get { return this.resultId; }
     58      private set {
     59        if (resultId != value) {
     60          if (result.HasLoadedOrAssignedValue)
     61            throw new ForeignKeyReferenceAlreadyHasValueException();
     62          resultId = value;
     63        }
     64      }
     65    }
     66
     67    private EntityRef<Result> result;
     68    [Association(Storage = "result", ThisKey = "ResultId", OtherKey = "Id", IsForeignKey = true)]
     69    public Result Result {
     70      get { return result.Entity; }
     71    }
    872  }
    973}
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.SQLiteBackend/3.2/Model.cs

    r2178 r2179  
    66using System.Text;
    77
    8 namespace HeuristicLab.Modeling.SQLiteBackend { 
    9   [Table(Name="Model")]
     8namespace HeuristicLab.Modeling.SQLiteBackend {
     9  [Table(Name = "Model")]
    1010  public class Model {
    1111    public Model() {
     
    1414    }
    1515
     16    public Model(Variable targetVariable, Algorithm algorithm, object data)
     17      : this() {
     18      this.TargetVariable = targetVariable;
     19      this.Algorithm = algorithm;
     20      this.data = data;
     21    }
     22
    1623    private int id;
    17     [Column(Storage = "id", DbType = "integer", IsPrimaryKey = true, IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
     24    [Column(Storage = "id", IsPrimaryKey = true, IsDbGenerated = true)]
    1825    public int Id {
    1926      get { return this.id; }
     
    2229
    2330    private object data;
    24     [Column(Storage = "data", DbType = "text", CanBeNull = false)]
     31    [Column(Storage = "data", DbType = "varbinary(8000)", CanBeNull = false)]
    2532    public object Data {
    2633      get { return this.data; }
     
    2936
    3037    private int algorithmId;
    31     [Column(Storage="algorithmId", DbType="Int not null")]
     38    [Column(Storage = "algorithmId", CanBeNull = false)]
    3239    public int AlgorithmId {
    33       get { return this.algorithmId;}
    34       private set { 
    35         if(algorithmId != value) {
    36           if(algorithm.HasLoadedOrAssignedValue)
     40      get { return this.algorithmId; }
     41      private set {
     42        if (algorithmId != value) {
     43          if (algorithm.HasLoadedOrAssignedValue)
    3744            throw new ForeignKeyReferenceAlreadyHasValueException();
    38           algorithmId =value;
     45          algorithmId = value;
    3946        }
    4047      }
    4148    }
    4249
    43 
    4450    private EntityRef<Algorithm> algorithm;
    45     [Association(Storage="algorithm",ThisKey="AlgorithmId",OtherKey="Id",IsForeignKey=true)]
     51    [Association(Storage = "algorithm", ThisKey = "AlgorithmId", OtherKey = "Id", IsForeignKey = true)]
    4652    public Algorithm Algorithm {
    4753      get { return this.algorithm.Entity; }
     
    4955        Algorithm previousValue = algorithm.Entity;
    5056        if (previousValue != value || (!algorithm.HasLoadedOrAssignedValue)) {
    51           if (previousValue == null) {
     57          if (previousValue != null) {
    5258            algorithm.Entity = null;
    53             previousValue.Models.Remove(this);
    5459          }
    5560          algorithm.Entity = value;
    5661          if (value != null) {
    57             value.Models.Add(this);
    5862            algorithmId = value.Id;
    59           } else
    60             throw new NullReferenceException("Null not allowed as value for Model.Algorithm");
     63          }
    6164        }
    6265      }
     
    6467
    6568    private int targetVariableId;
    66     [Column(Storage = "targetVariableId", DbType = "Int not null")]
     69    [Column(Storage = "targetVariableId", CanBeNull = false)]
    6770    public int TargetVariableId {
    6871      get { return this.targetVariableId; }
     
    8386        Variable previousValue = targetVariable.Entity;
    8487        if (previousValue != value || (!targetVariable.HasLoadedOrAssignedValue)) {
    85           if (previousValue == null) {
     88          if (previousValue != null) {
    8689            targetVariable.Entity = null;
    87             previousValue.ModelsPredictingThisVariable.Remove(this);
    8890          }
    8991          targetVariable.Entity = value;
    9092          if (value != null) {
    91             value.ModelsPredictingThisVariable.Add(this);
    9293            targetVariableId = value.Id;
    93           } else
    94             throw new NullReferenceException("Null not allowed as value for Model.TargetVariable");
     94          }
    9595        }
    9696      }
    9797    }
    9898
    99     public IEnumerable<Variable> inputVariables {
     99    public IEnumerable<Variable> InputVariables {
    100100      get {
    101101        using (ModelingDataContext ctx = new ModelingDataContext()) {
     
    107107      }
    108108    }
     109
     110    public Dictionary<string, double> ResultValues {
     111      get {
     112        using (ModelingDataContext ctx = new ModelingDataContext()) {
     113          var x = (from modelResult in ctx.ModelResults
     114                   where modelResult.Model == this
     115                   select modelResult)
     116                  .ToDictionary(
     117                    modelResult => modelResult.Result.Name,
     118                    modelResult => modelResult.Value);
     119          return x;
     120        }
     121      }
     122    }
    109123  }
    110124}
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.SQLiteBackend/3.2/ModelResult.cs

    r2178 r2179  
    11using System;
    22using System.Collections.Generic;
    3 using System.Linq;
     3using System.Data.Linq;
     4using System.Data.Linq.Mapping;
    45using System.Text;
    56
    67namespace HeuristicLab.Modeling.SQLiteBackend {
     8  [Table(Name = "ModelResult")]
    79  public class ModelResult {
     10    public ModelResult() {
     11      this.model = default(EntityRef<Model>);
     12      this.result = default(EntityRef<Result>);
     13    }
     14
     15    public ModelResult(Model model, Result result, double value)
     16      : this() {
     17      this.Model = model;
     18      this.Result = result;
     19      this.value = value;
     20    }
     21
     22    private double value;
     23    [Column(Storage = "value", CanBeNull = false)]
     24    public double Value {
     25      get { return this.value; }
     26      set { this.value = value; }
     27    }
     28
     29    private int modelId;
     30    [Column(Storage = "modelId", CanBeNull = false)]
     31    public int ModelId {
     32      get { return this.modelId; }
     33      private set {
     34        if (modelId != value) {
     35          if (model.HasLoadedOrAssignedValue)
     36            throw new ForeignKeyReferenceAlreadyHasValueException();
     37        }
     38        modelId = value;
     39      }
     40    }
     41
     42    private EntityRef<Model> model;
     43    [Association(Storage = "model", ThisKey = "ModelId", OtherKey = "Id", IsForeignKey = true)]
     44    public Model Model {
     45      get { return this.model.Entity; }
     46      set {
     47        Model previousValue = model.Entity;
     48        if (previousValue != value || (!model.HasLoadedOrAssignedValue)) {
     49          if (previousValue != null) {
     50            model.Entity = null;
     51          }
     52          model.Entity = value;
     53          if (value != null) {
     54            modelId = value.Id;
     55          }
     56        }
     57      }
     58    }
     59
     60    private int resultId;
     61    [Column(Storage = "resultId", CanBeNull = false)]
     62    public int ResultId {
     63      get { return this.resultId; }
     64      private set {
     65        if (resultId != value) {
     66          if (result.HasLoadedOrAssignedValue)
     67            throw new ForeignKeyReferenceAlreadyHasValueException();
     68        }
     69        modelId = value;
     70      }
     71    }
     72
     73    private EntityRef<Result> result;
     74    [Association(Storage = "result", ThisKey = "ResultId", OtherKey = "Id", IsForeignKey = true)]
     75    public Result Result {
     76      get { return this.result.Entity; }
     77      set {
     78        Result previousValue = result.Entity;
     79        if (previousValue != value || (!model.HasLoadedOrAssignedValue)) {
     80          if (previousValue != null) {
     81            result.Entity = null;
     82          }
     83          result.Entity = value;
     84          if (value != null) {
     85            resultId = value.Id;
     86          }
     87        }
     88      }
     89    }
     90
    891  }
    992}
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.SQLiteBackend/3.2/ModelingDataContext.cs

    r2178 r2179  
    33using System.Data.Linq;
    44using System.Data.Linq.Mapping;
     5using System.Data.Sql;
    56using System.Text;
    67
     
    89  public class ModelingDataContext : DataContext{
    910
    10     public static string connectionString = @"data source=D:\Reps\HL3-Source\branches\HeuristicLab.Modeling Database Backend\sources\HeuristicLab.Modeling.SQLiteBackend\3.2\HeuristicLab.Modeling.SQLiteBackend.database";
     11    public static string connectionString;
    1112
    12     public ModelingDataContext() : this(connectionString) {
     13    public ModelingDataContext() : this(connectionString) {     
    1314    }
    1415
     
    2223    }
    2324
    24     public Table<InputVariable> InputVariables {
    25       get { return GetTable<InputVariable>(); }
    26     }
    27 
    28     public Table<InputVariableResult> InputVariableResults {
    29       get { return GetTable<InputVariableResult>(); }
    30     }
    31     public Table<Model> Models {
    32       get { return GetTable<Model>(); }
    33     }
    34 
    35     public Table<ModelResult> ModelResults {
    36       get { return GetTable<ModelResult>(); }
     25    public Table<Variable> Variables {
     26      get { return GetTable<Variable>(); }
    3727    }
    3828
     
    4434      get { return GetTable<Result>(); }
    4535    }
     36
     37    public Table<Model> Models {
     38      get { return GetTable<Model>(); }
     39    }
     40
     41
     42    public Table<InputVariableResult> InputVariableResults {
     43      get { return GetTable<InputVariableResult>(); }
     44    }
     45
     46    public Table<ModelResult> ModelResults {
     47      get { return GetTable<ModelResult>(); }
     48    }
     49
     50    public Table<InputVariable> InputVariables {
     51      get { return GetTable<InputVariable>(); }
     52    }
    4653    #endregion
    4754  }
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.SQLiteBackend/3.2/Problem.cs

    r2178 r2179  
    1313    }
    1414
     15    public Problem(Dataset dataset)
     16      : this() {
     17      this.dataset = dataset;
     18    }
     19
    1520    private int id;
    16     [Column(Storage = "id", DbType = "integer", IsPrimaryKey = true, IsDbGenerated = true)]
     21    [Column(Storage = "id", IsPrimaryKey = true, IsDbGenerated = true)]
    1722    public int Id {
    1823      get { return this.id; }
     
    2126
    2227    private Dataset dataset;
    23     [Column(Storage = "dataset", DbType = "text", CanBeNull = false)]
     28    [Column(Storage = "dataset", DbType = "varbinary(8000)", CanBeNull = false)]
    2429    public Dataset Dataset {
    2530      get { return this.dataset; }
    26       set { this.dataset = value; }
    27     }   
     31      private set { this.dataset = value; }
     32    }
    2833  }
    2934}
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.SQLiteBackend/3.2/Result.cs

    r2178 r2179  
    11using System;
    22using System.Collections.Generic;
     3using System.Linq;
    34using System.Data.Linq;
    45using System.Data.Linq.Mapping;
     
    1112    }
    1213
     14    public Result(string name)
     15      : this() {
     16      this.name = name;
     17    }
     18
    1319    private int id;
    14     [Column(Storage = "id", DbType = "integer", IsPrimaryKey = true, IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
     20    [Column(Storage = "id", IsPrimaryKey = true, IsDbGenerated = true)]
    1521    public int Id {
    1622      get { return this.id; }
     
    1925
    2026    private string name;
    21     [Column(Storage = "name", DbType = "VarChar(50) NOT NULL", CanBeNull = false)]
     27    [Column(Storage = "name", CanBeNull = false)]
    2228    public string Name {
    2329      get { return this.name; }
     
    2531    }
    2632
     33    public Dictionary<Model, double> ModelResultValues {
     34      get {
     35        using (ModelingDataContext ctx = new ModelingDataContext()) {
     36          var x = (from modelResult in ctx.ModelResults
     37                   where modelResult.Result == this
     38                   select modelResult).ToDictionary(
     39                    mr => mr.Model,
     40                    mr => mr.Value);
     41          return x;
     42        }
     43      }
     44    }
     45
    2746  }
    2847}
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.SQLiteBackend/3.2/Variable.cs

    r2178 r2179  
    1010  public class Variable {
    1111    public Variable() {
    12       modelsPredictingThisVariable = new EntitySet<Model>(attach_Model, detach_Model);
     12    }
     13
     14    public Variable(string name)
     15      : this() {
     16      this.name = name;
    1317    }
    1418
    1519    private int id;
    16     [Column(Storage = "id", DbType = "integer", IsPrimaryKey = true, IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
     20    [Column(Storage = "id", IsPrimaryKey = true, IsDbGenerated = true)]
    1721    public int Id {
    1822      get { return this.id; }
     
    2125
    2226    private string name;
    23     [Column(Storage = "name", DbType = "VarChar(50) NOT NULL", CanBeNull = false)]
     27    [Column(Storage = "name", CanBeNull = false)]
    2428    public string Name {
    2529      get { return this.name; }
     
    2731    }
    2832
    29     private EntitySet<Model> modelsPredictingThisVariable;
    30     [Association(Storage = "modelsPredictingThisVariable", ThisKey = "Id", OtherKey = "TargetVariableId")]
    31     public EntitySet<Model> ModelsPredictingThisVariable {
     33    public IEnumerable<Model> ModelsPredictingThisVariable {
    3234      get {
    33         if (modelsPredictingThisVariable.HasLoadedOrAssignedValues)
    34           return modelsPredictingThisVariable;
    35         return null;
     35        using (ModelingDataContext ctx = new ModelingDataContext()) {
     36          var x = from model in ctx.Models
     37                  where model.TargetVariable == this
     38                  select model;
     39          return x.AsEnumerable();
     40        }
    3641      }
    37       set {
    38         modelsPredictingThisVariable.Assign(value);
    39       }
    40     }
    41 
    42     private void attach_Model(Model model) {
    43       model.TargetVariable = this;
    44     }
    45 
    46     private void detach_Model(Model model) {
    47       model.TargetVariable = null;
    4842    }
    4943
Note: See TracChangeset for help on using the changeset viewer.