Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.CEDMA.DB/3.3/Store.cs @ 6004

Last change on this file since 6004 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 7.7 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
64    public ICollection<VariableBindings> Query(string query, int page, int pageSize) {
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.Skip(page*pageSize).Take(pageSize).ToList();
70    }
71
72    public ICollection<VariableBindings> Query(ICollection<Statement> query, int page, int pageSize) {
73      MyQueryResultSink resultSink = new MyQueryResultSink();
74      Translate(query).Run(store, resultSink);
75      return resultSink.Bindings.Skip(page * pageSize).Take(pageSize).ToList();
76    }
77
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);
83    }
84
85    private static SemWeb.Entity Translate(Entity e) {
86      return e.Uri == null ? null : new SemWeb.Entity(e.Uri);
87    }
88
89    private static SemWeb.Resource Translate(Resource prop) {
90      if (prop is Literal) {
91        return TranslateLiteral((Literal)prop);
92      } else if (prop is SerializedLiteral) {
93        return TranslateLiteral((SerializedLiteral)prop);
94      } else {
95        return Translate((Entity)prop);
96      }
97    }
98
99    private static Statement Translate(SemWeb.Statement statement) {
100      return Translate(statement, new Dictionary<object, object>());
101    }
102
103    private static Statement Translate(SemWeb.Statement statement, Dictionary<object, object> translatedObjects) {
104      if (!translatedObjects.ContainsKey(statement.Subject)) {
105        translatedObjects[statement.Subject] = new Entity(statement.Subject.Uri);
106      }
107      if (!translatedObjects.ContainsKey(statement.Predicate)) {
108        translatedObjects[statement.Predicate] = new Entity(statement.Predicate.Uri);
109      }
110      if (!translatedObjects.ContainsKey(statement.Object)) {
111        if (statement.Object is SemWeb.Literal) {
112          translatedObjects[statement.Object] = TranslateLiteral((SemWeb.Literal)statement.Object);
113        } else {
114          translatedObjects[statement.Object] = new Entity(((SemWeb.Entity)statement.Object).Uri);
115        }
116      }
117
118      Entity subjectEntity = (Entity)translatedObjects[statement.Subject];
119      Entity predicateEntity = (Entity)translatedObjects[statement.Predicate];
120      Resource property = (Resource)translatedObjects[statement.Object];
121
122      return new Statement(
123        subjectEntity,
124        predicateEntity,
125        property);
126    }
127
128    private static SemWeb.Statement Translate(Statement statement) {
129      return Translate(statement, new Dictionary<object, object>());
130    }
131
132    private static SemWeb.Statement Translate(Statement statement, Dictionary<object, object> translatedObjects) {
133      if (!translatedObjects.ContainsKey(statement.Subject)) {
134        translatedObjects[statement.Subject] = Translate(statement.Subject);
135      }
136      if (!translatedObjects.ContainsKey(statement.Predicate)) {
137        translatedObjects[statement.Predicate] = Translate(statement.Predicate);
138      }
139      if (!translatedObjects.ContainsKey(statement.Property)) {
140        translatedObjects[statement.Property] = Translate(statement.Property);
141      }
142
143      SemWeb.Entity subject = (SemWeb.Entity)translatedObjects[statement.Subject];
144      SemWeb.Entity predicate = (SemWeb.Entity)translatedObjects[statement.Predicate];
145      SemWeb.Resource property = (SemWeb.Resource)translatedObjects[statement.Property];
146
147      return new SemWeb.Statement(
148        subject,
149        predicate,
150        property);
151    }
152
153    private static SemWeb.Literal TranslateLiteral(SerializedLiteral l) {
154      if (l.RawData == null) return null;
155      return new SemWeb.Literal(l.RawData, null, "serializedItem");
156    }
157
158    private static SemWeb.Literal TranslateLiteral(Literal l) {
159      if (l.Value == null) return null;
160      if (l.Value is double) return SemWeb.Literal.FromValue((double)l.Value);
161      else if (l.Value is bool) return SemWeb.Literal.FromValue((bool)l.Value);
162      else if (l.Value is int) return SemWeb.Literal.FromValue((int)l.Value);
163      else if (l.Value is long) return SemWeb.Literal.FromValue((long)l.Value);
164      else if (l.Value is string) return SemWeb.Literal.FromValue((string)l.Value);
165      else return new SemWeb.Literal(l.Value.ToString());
166    }
167
168    private static Resource TranslateLiteral(SemWeb.Literal l) {
169      if (l.DataType == "serializedItem") {
170        return new SerializedLiteral(l.Value);
171      } else if (l.DataType != null) {
172        return new Literal(l.ParseValue());
173      } else {
174        return new Literal(l.Value);
175      }
176    }
177
178    private class MyQueryResultSink : SemWeb.Query.QueryResultSink {
179
180      private List<VariableBindings> bindings = new List<VariableBindings>();
181      public ICollection<VariableBindings> Bindings {
182        get { return bindings.AsReadOnly(); }
183      }
184
185      public override bool Add(SemWeb.Query.VariableBindings result) {
186        VariableBindings varBindings = new VariableBindings();
187        foreach (SemWeb.Variable var in result.Variables) {
188          if (var.LocalName != null && result[var] != null) {
189            if (result[var] is SemWeb.Literal) {
190              varBindings.Add(var.LocalName, TranslateLiteral((SemWeb.Literal)result[var]));
191            } else {
192              varBindings.Add(var.LocalName, new Entity(((SemWeb.Entity)result[var]).Uri));
193            }
194          }
195          bindings.Add(varBindings);
196        }
197        return true;
198      }
199    }
200  }
201}
Note: See TracBrowser for help on using the repository browser.