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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using System.Data.Linq;
|
---|
27 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
28 | using System.ServiceModel;
|
---|
29 | using System.Data;
|
---|
30 | using System.Data.SQLite;
|
---|
31 | using System.Data.Common;
|
---|
32 | using System.Threading;
|
---|
33 | using HeuristicLab.Data;
|
---|
34 | using HeuristicLab.Core;
|
---|
35 | using System.Xml;
|
---|
36 | using System.IO;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.CEDMA.DB {
|
---|
39 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
|
---|
40 | public class Store : IStore {
|
---|
41 | private string connectionString;
|
---|
42 | private SemWeb.Store store;
|
---|
43 | public Store(string connectionString) {
|
---|
44 | this.connectionString = connectionString;
|
---|
45 | store = SemWeb.Store.Create(connectionString);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void Add(Statement statement) {
|
---|
49 | store.Add(Translate(statement));
|
---|
50 | }
|
---|
51 |
|
---|
52 | public IList<Statement> Select(Statement template) {
|
---|
53 | SemWeb.SelectResult result = store.Select(Translate(template));
|
---|
54 | List<Statement> r = new List<Statement>();
|
---|
55 | foreach(SemWeb.Statement resultStatement in result) {
|
---|
56 | r.Add(Translate(resultStatement));
|
---|
57 | }
|
---|
58 | return r;
|
---|
59 | }
|
---|
60 |
|
---|
61 | private Statement Translate(SemWeb.Statement statement) {
|
---|
62 | if(statement.Object is SemWeb.Literal) {
|
---|
63 | return new Statement(
|
---|
64 | new Entity(statement.Subject.Uri),
|
---|
65 | new Entity(statement.Predicate.Uri),
|
---|
66 | TranslateLiteral((SemWeb.Literal)statement.Object));
|
---|
67 | } else {
|
---|
68 | return new Statement(
|
---|
69 | new Entity(statement.Subject.Uri),
|
---|
70 | new Entity(statement.Predicate.Uri),
|
---|
71 | new Entity(((SemWeb.Entity)statement.Object).Uri));
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private SemWeb.Statement Translate(Statement statement) {
|
---|
76 | if(statement.Property is Literal) {
|
---|
77 | return new SemWeb.Statement(
|
---|
78 | statement.Subject.Uri == null ? null : new SemWeb.Entity(statement.Subject.Uri),
|
---|
79 | statement.Predicate.Uri == null ? null : new SemWeb.Entity(statement.Predicate.Uri),
|
---|
80 | TranslateLiteral((Literal)statement.Property));
|
---|
81 | } else if(statement.Property is SerializedLiteral) {
|
---|
82 | return new SemWeb.Statement(
|
---|
83 | statement.Subject.Uri == null ? null : new SemWeb.Entity(statement.Subject.Uri),
|
---|
84 | statement.Predicate.Uri == null ? null : new SemWeb.Entity(statement.Predicate.Uri),
|
---|
85 | TranslateLiteral((SerializedLiteral)statement.Property));
|
---|
86 | } else {
|
---|
87 | return new SemWeb.Statement(
|
---|
88 | statement.Subject.Uri == null ? null : new SemWeb.Entity(statement.Subject.Uri),
|
---|
89 | statement.Predicate.Uri == null ? null : new SemWeb.Entity(statement.Predicate.Uri),
|
---|
90 | statement.Property.Uri == null ? null : new SemWeb.Entity(((Entity)statement.Property).Uri));
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | private SemWeb.Literal TranslateLiteral(SerializedLiteral l) {
|
---|
95 | if(l.RawData == null) return null;
|
---|
96 | return new SemWeb.Literal(l.RawData, null, "serializedItem");
|
---|
97 | }
|
---|
98 |
|
---|
99 | private SemWeb.Literal TranslateLiteral(Literal l) {
|
---|
100 | if(l.Value == null) return null;
|
---|
101 | if(l.Value is double) return SemWeb.Literal.FromValue((double)l.Value);
|
---|
102 | else if(l.Value is bool) return SemWeb.Literal.FromValue((bool)l.Value);
|
---|
103 | else if(l.Value is int) return SemWeb.Literal.FromValue((int)l.Value);
|
---|
104 | else if(l.Value is long) return SemWeb.Literal.FromValue((long)l.Value);
|
---|
105 | else if(l.Value is string) return SemWeb.Literal.FromValue((string)l.Value);
|
---|
106 | else return new SemWeb.Literal(l.Value.ToString());
|
---|
107 | }
|
---|
108 |
|
---|
109 | private Resource TranslateLiteral(SemWeb.Literal l) {
|
---|
110 | if(l.DataType == "serializedItem") {
|
---|
111 | return new SerializedLiteral(l.Value);
|
---|
112 | } else if(l.DataType != null) {
|
---|
113 | return new Literal(l.ParseValue());
|
---|
114 | } else {
|
---|
115 | return new Literal(l.Value);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | } |
---|