source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.DB/Store.cs @ 1129

Last change on this file since 1129 was 1129, checked in by gkronber, 12 years ago

worked on RDF backend. Implemented query support to load datasets. Queries for dispatcher and results still missing. (#419)

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