Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 552 was 551, checked in by gkronber, 16 years ago

added a method to select based on a set of templates. #200 (Simple indexer for results and matching search-frontend for solution-quality)

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