Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.CEDMA.DB/3.3/Store.cs @ 6834

Last change on this file since 6834 was 2053, checked in by gkronber, 15 years ago

Removed lazy loading of problem data and made adds of datasets and models atomic. #656 (CEDMA server should handle only one data set (problem) at a time)

File size: 8.0 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.Common;
31using System.Threading;
32using HeuristicLab.Data;
33using HeuristicLab.Core;
34using System.Xml;
35using System.IO;
36
37namespace HeuristicLab.CEDMA.DB {
38  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
39  public class Store : IStore {
40    private string connectionString;
41    private SemWeb.Store store;
42    private object bigLock = new object();
43    public Store(string connectionString) {
44      lock (bigLock) {
45        this.connectionString = connectionString;
46        store = SemWeb.Store.Create(connectionString);
47        InitStore();
48      }
49    }
50
51    private void InitStore() {
52      foreach (Statement s in Ontology.InitialStatements) {
53        Add(s);
54      }
55    }
56
57    public void Add(Statement statement) {
58      lock (bigLock) {
59        store.Add(Translate(statement));
60      }
61    }
62
63    public void AddRange(ICollection<Statement> statements) {
64      lock (bigLock) {
65        foreach (Statement s in statements) {
66          store.Add(Translate(s));
67        }
68      }
69    }
70
71
72    public ICollection<VariableBindings> Query(string query, int page, int pageSize) {
73      lock (bigLock) {
74        MyQueryResultSink resultSink = new MyQueryResultSink();
75        SemWeb.N3Reader n3Reader = new SemWeb.N3Reader(new StringReader(query));
76        SemWeb.Query.GraphMatch matcher = new SemWeb.Query.GraphMatch(n3Reader);
77        matcher.Run(store, resultSink);
78        return resultSink.Bindings.Skip(page * pageSize).Take(pageSize).ToList();
79      }
80    }
81
82    public ICollection<VariableBindings> Query(ICollection<Statement> query, int page, int pageSize) {
83      lock (bigLock) {
84        MyQueryResultSink resultSink = new MyQueryResultSink();
85        Translate(query).Run(store, resultSink);
86        return resultSink.Bindings.Skip(page * pageSize).Take(pageSize).ToList();
87      }
88    }
89
90    private SemWeb.Query.Query Translate(ICollection<Statement> query) {
91      Dictionary<object, object> translatedObjects = new Dictionary<object, object>();
92      SemWeb.MemoryStore queryStore = new SemWeb.MemoryStore(query.Select(st => Translate(st, translatedObjects)).ToArray());
93
94      return new SemWeb.Query.GraphMatch(queryStore);
95    }
96
97    private static SemWeb.Entity Translate(Entity e) {
98      return e.Uri == null ? null : new SemWeb.Entity(e.Uri);
99    }
100
101    private static SemWeb.Resource Translate(Resource prop) {
102      if (prop is Literal) {
103        return TranslateLiteral((Literal)prop);
104      } else if (prop is SerializedLiteral) {
105        return TranslateLiteral((SerializedLiteral)prop);
106      } else {
107        return Translate((Entity)prop);
108      }
109    }
110
111    private static Statement Translate(SemWeb.Statement statement) {
112      return Translate(statement, new Dictionary<object, object>());
113    }
114
115    private static Statement Translate(SemWeb.Statement statement, Dictionary<object, object> translatedObjects) {
116      if (!translatedObjects.ContainsKey(statement.Subject)) {
117        translatedObjects[statement.Subject] = new Entity(statement.Subject.Uri);
118      }
119      if (!translatedObjects.ContainsKey(statement.Predicate)) {
120        translatedObjects[statement.Predicate] = new Entity(statement.Predicate.Uri);
121      }
122      if (!translatedObjects.ContainsKey(statement.Object)) {
123        if (statement.Object is SemWeb.Literal) {
124          translatedObjects[statement.Object] = TranslateLiteral((SemWeb.Literal)statement.Object);
125        } else {
126          translatedObjects[statement.Object] = new Entity(((SemWeb.Entity)statement.Object).Uri);
127        }
128      }
129
130      Entity subjectEntity = (Entity)translatedObjects[statement.Subject];
131      Entity predicateEntity = (Entity)translatedObjects[statement.Predicate];
132      Resource property = (Resource)translatedObjects[statement.Object];
133
134      return new Statement(
135        subjectEntity,
136        predicateEntity,
137        property);
138    }
139
140    private static SemWeb.Statement Translate(Statement statement) {
141      return Translate(statement, new Dictionary<object, object>());
142    }
143
144    private static SemWeb.Statement Translate(Statement statement, Dictionary<object, object> translatedObjects) {
145      if (!translatedObjects.ContainsKey(statement.Subject)) {
146        translatedObjects[statement.Subject] = Translate(statement.Subject);
147      }
148      if (!translatedObjects.ContainsKey(statement.Predicate)) {
149        translatedObjects[statement.Predicate] = Translate(statement.Predicate);
150      }
151      if (!translatedObjects.ContainsKey(statement.Property)) {
152        translatedObjects[statement.Property] = Translate(statement.Property);
153      }
154
155      SemWeb.Entity subject = (SemWeb.Entity)translatedObjects[statement.Subject];
156      SemWeb.Entity predicate = (SemWeb.Entity)translatedObjects[statement.Predicate];
157      SemWeb.Resource property = (SemWeb.Resource)translatedObjects[statement.Property];
158
159      return new SemWeb.Statement(
160        subject,
161        predicate,
162        property);
163    }
164
165    private static SemWeb.Literal TranslateLiteral(SerializedLiteral l) {
166      if (l.RawData == null) return null;
167      return new SemWeb.Literal(l.RawData, null, "serializedItem");
168    }
169
170    private static SemWeb.Literal TranslateLiteral(Literal l) {
171      if (l.Value == null) return null;
172      if (l.Value is double) return SemWeb.Literal.FromValue((double)l.Value);
173      else if (l.Value is bool) return SemWeb.Literal.FromValue((bool)l.Value);
174      else if (l.Value is int) return SemWeb.Literal.FromValue((int)l.Value);
175      else if (l.Value is long) return SemWeb.Literal.FromValue((long)l.Value);
176      else if (l.Value is string) return SemWeb.Literal.FromValue((string)l.Value);
177      else return new SemWeb.Literal(l.Value.ToString());
178    }
179
180    private static Resource TranslateLiteral(SemWeb.Literal l) {
181      if (l.DataType == "serializedItem") {
182        return new SerializedLiteral(l.Value);
183      } else if (l.DataType != null) {
184        return new Literal(l.ParseValue());
185      } else {
186        return new Literal(l.Value);
187      }
188    }
189
190    private class MyQueryResultSink : SemWeb.Query.QueryResultSink {
191
192      private List<VariableBindings> bindings = new List<VariableBindings>();
193      public ICollection<VariableBindings> Bindings {
194        get { return bindings.AsReadOnly(); }
195      }
196
197      public override bool Add(SemWeb.Query.VariableBindings result) {
198        VariableBindings varBindings = new VariableBindings();
199        foreach (SemWeb.Variable var in result.Variables) {
200          if (var.LocalName != null && result[var] != null) {
201            if (result[var] is SemWeb.Literal) {
202              varBindings.Add(var.LocalName, TranslateLiteral((SemWeb.Literal)result[var]));
203            } else {
204              varBindings.Add(var.LocalName, new Entity(((SemWeb.Entity)result[var]).Uri));
205            }
206          }
207          bindings.Add(varBindings);
208        }
209        return true;
210      }
211    }
212  }
213}
Note: See TracBrowser for help on using the repository browser.