Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.CEDMA.DB/Store.cs @ 3494

Last change on this file since 3494 was 557, checked in by gkronber, 16 years ago

added very coarse grained locking in the Store as a quick fix to support distributed execution model generating engines

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Data.Linq;
27using HeuristicLab.CEDMA.DB.Interfaces;
28using System.ServiceModel;
29using System.Data;
30using System.Data.SQLite;
31using System.Data.Common;
32using System.Threading;
33using HeuristicLab.Data;
34using HeuristicLab.Core;
35using System.Xml;
36using System.IO;
37
38namespace HeuristicLab.CEDMA.DB {
39  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
40  public class Store : IStore {
41    private string connectionString;
42    private SemWeb.Store store;
43    private object bigLock = new object();
44    public Store(string connectionString) {
45      lock(bigLock) {
46        this.connectionString = connectionString;
47        store = SemWeb.Store.Create(connectionString);
48      }
49    }
50
51    public void Add(Statement statement) {
52      lock(bigLock) {
53        store.Add(Translate(statement));
54      }
55    }
56
57    public IList<Statement> Select(Statement template) {
58      SemWeb.SelectResult result;
59      lock(bigLock) {
60        result = store.Select(Translate(template));
61      }
62      List<Statement> r = new List<Statement>();
63      foreach(SemWeb.Statement resultStatement in result) {
64        r.Add(Translate(resultStatement));
65      }
66      return r;
67    }
68
69    public IList<Statement> Select(SelectFilter filter) {
70      SemWeb.SelectResult result;
71      lock(bigLock) {
72        result = store.Select(Translate(filter));
73      }
74      List<Statement> r = new List<Statement>();
75      foreach(SemWeb.Statement resultStatement in result) {
76        r.Add(Translate(resultStatement));
77      }
78      return r;
79    }
80
81    private SemWeb.SelectFilter Translate(SelectFilter filter) {
82      SemWeb.SelectFilter f = new SemWeb.SelectFilter();
83      f.Subjects = Array.ConvertAll(filter.Subjects, s => Translate(s));
84      f.Predicates = Array.ConvertAll(filter.Predicates, p => Translate(p));
85      f.Objects = Array.ConvertAll(filter.Properties, prop => Translate(prop));
86      return f;
87    }
88
89    private SemWeb.Entity Translate(Entity e) {
90      return e.Uri == null ? null : new SemWeb.Entity(e.Uri);
91    }
92
93    private SemWeb.Resource Translate(Resource prop) {
94      if(prop is Literal) {
95        return TranslateLiteral((Literal)prop);
96      } else if(prop is SerializedLiteral) {
97        return TranslateLiteral((SerializedLiteral)prop);
98      } else {
99        return Translate((Entity)prop);
100      }
101    }
102
103    private Statement Translate(SemWeb.Statement statement) {
104      if(statement.Object is SemWeb.Literal) {
105        return new Statement(
106          new Entity(statement.Subject.Uri),
107          new Entity(statement.Predicate.Uri),
108          TranslateLiteral((SemWeb.Literal)statement.Object));
109      } else {
110        return new Statement(
111          new Entity(statement.Subject.Uri),
112          new Entity(statement.Predicate.Uri),
113          new Entity(((SemWeb.Entity)statement.Object).Uri));
114      }
115    }
116
117    private SemWeb.Statement Translate(Statement statement) {
118      return new SemWeb.Statement(
119        Translate(statement.Subject),
120        Translate(statement.Predicate),
121        Translate(statement.Property));
122    }
123
124    private SemWeb.Literal TranslateLiteral(SerializedLiteral l) {
125      if(l.RawData == null) return null;
126      return new SemWeb.Literal(l.RawData, null, "serializedItem");
127    }
128
129    private SemWeb.Literal TranslateLiteral(Literal l) {
130      if(l.Value == null) return null;
131      if(l.Value is double) return SemWeb.Literal.FromValue((double)l.Value);
132      else if(l.Value is bool) return SemWeb.Literal.FromValue((bool)l.Value);
133      else if(l.Value is int) return SemWeb.Literal.FromValue((int)l.Value);
134      else if(l.Value is long) return SemWeb.Literal.FromValue((long)l.Value);
135      else if(l.Value is string) return SemWeb.Literal.FromValue((string)l.Value);
136      else return new SemWeb.Literal(l.Value.ToString());
137    }
138
139    private Resource TranslateLiteral(SemWeb.Literal l) {
140      if(l.DataType == "serializedItem") {
141        return new SerializedLiteral(l.Value);
142      } else if(l.DataType != null) {
143        return new Literal(l.ParseValue());
144      } else {
145        return new Literal(l.Value);
146      }
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.