Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 957 was 957, checked in by gkronber, 15 years ago

worked on #419 (Refactor CEDMA plugins)

File size: 5.1 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    public IList<Statement> Select(Statement template) {
57      SemWeb.SelectResult result;
58      lock(bigLock) {
59        result = store.Select(Translate(template));
60      }
61      List<Statement> r = new List<Statement>();
62      foreach(SemWeb.Statement resultStatement in result) {
63        r.Add(Translate(resultStatement));
64      }
65      return r;
66    }
67
68    public IList<Statement> Select(SelectFilter filter) {
69      SemWeb.SelectResult result;
70      lock(bigLock) {
71        result = store.Select(Translate(filter));
72      }
73      List<Statement> r = new List<Statement>();
74      foreach(SemWeb.Statement resultStatement in result) {
75        r.Add(Translate(resultStatement));
76      }
77      return r;
78    }
79
80    private SemWeb.SelectFilter Translate(SelectFilter filter) {
81      SemWeb.SelectFilter f = new SemWeb.SelectFilter();
82      f.Subjects = Array.ConvertAll(filter.Subjects, s => Translate(s));
83      f.Predicates = Array.ConvertAll(filter.Predicates, p => Translate(p));
84      f.Objects = Array.ConvertAll(filter.Properties, prop => Translate(prop));
85      return f;
86    }
87
88    private SemWeb.Entity Translate(Entity e) {
89      return e.Uri == null ? null : new SemWeb.Entity(e.Uri);
90    }
91
92    private SemWeb.Resource Translate(Resource prop) {
93      if(prop is Literal) {
94        return TranslateLiteral((Literal)prop);
95      } else if(prop is SerializedLiteral) {
96        return TranslateLiteral((SerializedLiteral)prop);
97      } else {
98        return Translate((Entity)prop);
99      }
100    }
101
102    private Statement Translate(SemWeb.Statement statement) {
103      if(statement.Object is SemWeb.Literal) {
104        return new Statement(
105          new Entity(statement.Subject.Uri),
106          new Entity(statement.Predicate.Uri),
107          TranslateLiteral((SemWeb.Literal)statement.Object));
108      } else {
109        return new Statement(
110          new Entity(statement.Subject.Uri),
111          new Entity(statement.Predicate.Uri),
112          new Entity(((SemWeb.Entity)statement.Object).Uri));
113      }
114    }
115
116    private SemWeb.Statement Translate(Statement statement) {
117      return new SemWeb.Statement(
118        Translate(statement.Subject),
119        Translate(statement.Predicate),
120        Translate(statement.Property));
121    }
122
123    private SemWeb.Literal TranslateLiteral(SerializedLiteral l) {
124      if(l.RawData == null) return null;
125      return new SemWeb.Literal(l.RawData, null, "serializedItem");
126    }
127
128    private SemWeb.Literal TranslateLiteral(Literal l) {
129      if(l.Value == null) return null;
130      if(l.Value is double) return SemWeb.Literal.FromValue((double)l.Value);
131      else if(l.Value is bool) return SemWeb.Literal.FromValue((bool)l.Value);
132      else if(l.Value is int) return SemWeb.Literal.FromValue((int)l.Value);
133      else if(l.Value is long) return SemWeb.Literal.FromValue((long)l.Value);
134      else if(l.Value is string) return SemWeb.Literal.FromValue((string)l.Value);
135      else return new SemWeb.Literal(l.Value.ToString());
136    }
137
138    private Resource TranslateLiteral(SemWeb.Literal l) {
139      if(l.DataType == "serializedItem") {
140        return new SerializedLiteral(l.Value);
141      } else if(l.DataType != null) {
142        return new Literal(l.ParseValue());
143      } else {
144        return new Literal(l.Value);
145      }
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.