Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.DB/Store.cs @ 1405

Last change on this file since 1405 was 1287, checked in by gkronber, 16 years ago

Merged change sets from CEDMA branch to trunk:

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