#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using HeuristicLab.CEDMA.DB.Interfaces;
using System.ServiceModel;
using System.Data;
using System.Data.Common;
using System.Threading;
using HeuristicLab.Data;
using HeuristicLab.Core;
using System.Xml;
using System.IO;
namespace HeuristicLab.CEDMA.DB {
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
public class Store : IStore {
private string connectionString;
private SemWeb.Store store;
private object bigLock = new object();
private SemWeb.Store cachedStore;
public Store(string connectionString) {
lock (bigLock) {
this.connectionString = connectionString;
store = SemWeb.Store.Create(connectionString);
InitStore();
}
}
private void InitStore() {
foreach (Statement s in Ontology.InitialStatements) {
Add(s);
}
}
public void Add(Statement statement) {
lock (bigLock) {
store.Add(Translate(statement));
InvalidateCachedStore();
}
}
public void AddRange(ICollection statements) {
lock (bigLock) {
foreach (Statement s in statements) {
store.Add(Translate(s));
}
InvalidateCachedStore();
}
}
public ICollection Query(string query, int page, int pageSize) {
lock (bigLock) {
MyQueryResultSink resultSink = new MyQueryResultSink();
SemWeb.N3Reader n3Reader = new SemWeb.N3Reader(new StringReader(query));
SemWeb.Query.GraphMatch matcher = new SemWeb.Query.GraphMatch(n3Reader);
if (cachedStore == null) {
CacheStore();
}
matcher.Run(cachedStore, resultSink);
return resultSink.Bindings.Skip(page * pageSize).Take(pageSize).ToList();
}
}
private void CacheStore() {
cachedStore = new SemWeb.MemoryStore();
cachedStore.Import(store);
}
private void InvalidateCachedStore() {
if (cachedStore != null) {
cachedStore.Dispose();
cachedStore = null;
}
}
public ICollection Query(ICollection query, int page, int pageSize) {
lock (bigLock) {
MyQueryResultSink resultSink = new MyQueryResultSink();
if (cachedStore == null) {
CacheStore();
}
Translate(query).Run(cachedStore, resultSink);
return resultSink.Bindings.Skip(page * pageSize).Take(pageSize).ToList();
}
}
private SemWeb.Query.Query Translate(ICollection query) {
Dictionary