easy_sparql 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/easy_sparql.rb +98 -0
- data/lib/easy_sparql/mock_cache.rb +39 -0
- data/lib/easy_sparql/resource.rb +139 -0
- data/lib/easy_sparql/store.rb +52 -0
- data/lib/easy_sparql/vocab.rb +35 -0
- metadata +98 -0
data/lib/easy_sparql.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# easy-sparql
|
2
|
+
#
|
3
|
+
# Copyright (c) 2011 British Broadcasting Corporation
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), 'easy_sparql', 'store.rb')
|
18
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), 'easy_sparql', 'resource.rb')
|
19
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), 'easy_sparql', 'mock_cache.rb')
|
20
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), 'easy_sparql', 'vocab.rb')
|
21
|
+
require 'securerandom'
|
22
|
+
require 'date'
|
23
|
+
|
24
|
+
module EasySparql
|
25
|
+
|
26
|
+
def self.store=(store)
|
27
|
+
@store = store
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.store
|
31
|
+
raise Exception.new("You need to specify a store using EasySparql.store = ... before trying to access it") unless @store
|
32
|
+
@store
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.query
|
36
|
+
store.sparql_client
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.included(base)
|
40
|
+
base.extend(ClassMethods)
|
41
|
+
end
|
42
|
+
|
43
|
+
module ClassMethods
|
44
|
+
|
45
|
+
def query
|
46
|
+
EasySparql.query
|
47
|
+
end
|
48
|
+
|
49
|
+
def count_all_by_sparql(query)
|
50
|
+
results = query.execute
|
51
|
+
results.empty? ? 0 : query.execute.first[:count].to_i
|
52
|
+
end
|
53
|
+
|
54
|
+
def find_all_by_sparql(query, params = {})
|
55
|
+
to_map = query.values.map { |symbol, var| symbol }
|
56
|
+
results = query.execute
|
57
|
+
objects = {}
|
58
|
+
results.each do |result|
|
59
|
+
if params[:key] and result[params[:key]]
|
60
|
+
key = result[params[:key]]
|
61
|
+
else
|
62
|
+
key = SecureRandom.uuid
|
63
|
+
end
|
64
|
+
object = (objects.has_key? key) ? objects[key] : new
|
65
|
+
to_map.each do |symbol|
|
66
|
+
unless symbol == params[:key]
|
67
|
+
value = result[symbol]
|
68
|
+
if value and value.literal?
|
69
|
+
value = value.object
|
70
|
+
end
|
71
|
+
setter = (symbol.to_s + '=').to_sym
|
72
|
+
old_val = object.send(symbol)
|
73
|
+
if old_val and old_val.kind_of? Array
|
74
|
+
new_val = (old_val + [value])
|
75
|
+
elsif old_val
|
76
|
+
new_val = [old_val, value]
|
77
|
+
else
|
78
|
+
new_val = value
|
79
|
+
end
|
80
|
+
object.send(setter, new_val)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
objects[key] = object
|
84
|
+
end
|
85
|
+
objects.values
|
86
|
+
end
|
87
|
+
|
88
|
+
def find_by_sparql(query)
|
89
|
+
find_all_by_sparql(query)[0]
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
def query
|
95
|
+
EasySparql.query
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# easy-sparql
|
2
|
+
#
|
3
|
+
# Copyright (c) 2011 British Broadcasting Corporation
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module EasySparql
|
18
|
+
|
19
|
+
class MockCache
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@cache = {}
|
23
|
+
end
|
24
|
+
|
25
|
+
def get(key)
|
26
|
+
@cache[key] if @cache.has_key? key
|
27
|
+
end
|
28
|
+
|
29
|
+
def set(key, value)
|
30
|
+
@cache[key] = value
|
31
|
+
end
|
32
|
+
|
33
|
+
def reset!
|
34
|
+
@cache = {}
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
# easy-sparql
|
2
|
+
#
|
3
|
+
# Copyright (c) 2011 British Broadcasting Corporation
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'sparql/client'
|
18
|
+
require 'active_support/inflector'
|
19
|
+
|
20
|
+
module EasySparql
|
21
|
+
|
22
|
+
class Resource
|
23
|
+
|
24
|
+
@@namespaces = {
|
25
|
+
'ws' => 'http://wsarchive.prototype0.net/ontology/',
|
26
|
+
'po' => 'http://purl.org/ontology/po/',
|
27
|
+
'dc' => 'http://purl.org/dc/elements/1.1/',
|
28
|
+
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
|
29
|
+
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
|
30
|
+
}
|
31
|
+
@@property_map = {}
|
32
|
+
|
33
|
+
attr_accessor :uri
|
34
|
+
attr_reader :method_bindings
|
35
|
+
attr_reader :properties
|
36
|
+
attr_reader :namespaces
|
37
|
+
|
38
|
+
@@cache = nil
|
39
|
+
|
40
|
+
def self.cache=(cache)
|
41
|
+
@@cache = cache
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.cache
|
45
|
+
@@cache
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.map(mapping)
|
49
|
+
@@property_map.merge! mapping
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.add_namespace(mappings)
|
53
|
+
mappings.each do |k, v|
|
54
|
+
@@namespaces[k] = v
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.namespaces
|
59
|
+
@@namespaces
|
60
|
+
end
|
61
|
+
|
62
|
+
def initialize(uri = nil, bindings = [])
|
63
|
+
@properties = []
|
64
|
+
@method_bindings = {}
|
65
|
+
@uri = uri
|
66
|
+
bindings.each do |property, value|
|
67
|
+
value = value.object if value.respond_to? :object
|
68
|
+
namespaces_i = @@namespaces.invert
|
69
|
+
namespaces_i.each do |base, short|
|
70
|
+
if property.to_s.start_with?(base)
|
71
|
+
method_name = property.to_s.sub(base, short + '_')
|
72
|
+
# If we already have a binding for that method, this
|
73
|
+
# method now outputs an array and we pluralise the method name
|
74
|
+
# The singular method name gives the first element of the array
|
75
|
+
if @method_bindings.has_key? method_name.pluralize.to_sym
|
76
|
+
@method_bindings[method_name.pluralize.to_sym] << value
|
77
|
+
elsif @method_bindings.has_key? method_name.to_sym
|
78
|
+
old_value = @method_bindings[method_name.to_sym]
|
79
|
+
@method_bindings[method_name.pluralize.to_sym] = [ old_value, value ]
|
80
|
+
else
|
81
|
+
@method_bindings[method_name.to_sym] = value
|
82
|
+
end
|
83
|
+
@properties << method_name.to_sym
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
@properties.uniq!
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.find_by_uri(uri)
|
91
|
+
cached_resource = @@cache.get(uri) if @@cache
|
92
|
+
return cached_resource if cached_resource
|
93
|
+
resource = find_by_uri_from_sparql(uri)
|
94
|
+
@@cache.set(uri, resource) if @@cache
|
95
|
+
resource
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.find_by_uri_from_sparql(uri)
|
99
|
+
uri = RDF::URI.new(uri) if uri.class == String
|
100
|
+
results = sparql.select.where([uri, :p, :o]).execute
|
101
|
+
bindings = []
|
102
|
+
results.each do |result|
|
103
|
+
bindings << [ result.p, result.o ]
|
104
|
+
end
|
105
|
+
resource = new(uri, bindings)
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.find_type(uri)
|
109
|
+
uri = RDF::URI.new(uri) if uri.class == String
|
110
|
+
results = sparql.select.where([uri, RDF.type, :type]).execute
|
111
|
+
results[0][:type] unless results.empty?
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.sparql
|
115
|
+
EasySparql.store.sparql_client
|
116
|
+
end
|
117
|
+
|
118
|
+
protected
|
119
|
+
|
120
|
+
def method_missing(name, *args, &block)
|
121
|
+
if args.empty? && @method_bindings.has_key?(name.to_sym)
|
122
|
+
value = @method_bindings[name.to_sym]
|
123
|
+
if value.class == RDF::URI
|
124
|
+
# Following our nose and caching the result in the object
|
125
|
+
if @@property_map.has_key? name.to_sym
|
126
|
+
@method_bindings[name.to_sym] = @@property_map[name.to_sym].find_by_uri(value)
|
127
|
+
else
|
128
|
+
@method_bindings[name.to_sym] = Resource.find_by_uri(value)
|
129
|
+
end
|
130
|
+
else
|
131
|
+
value
|
132
|
+
end
|
133
|
+
else
|
134
|
+
nil
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# easy-sparql
|
2
|
+
#
|
3
|
+
# Copyright (c) 2011 British Broadcasting Corporation
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'sparql/client'
|
18
|
+
|
19
|
+
module EasySparql
|
20
|
+
|
21
|
+
class Store
|
22
|
+
|
23
|
+
def initialize(sparql_uri = nil, update_uri = nil)
|
24
|
+
@sparql_uri = sparql_uri
|
25
|
+
@update_uri = update_uri
|
26
|
+
end
|
27
|
+
|
28
|
+
def sparql_uri=(uri)
|
29
|
+
@sparql_uri = uri
|
30
|
+
end
|
31
|
+
|
32
|
+
def sparql_uri
|
33
|
+
raise Exception.new "You need to set a valid SPARQL URI using Resource.sparql_uri = <uri>" unless @sparql_uri
|
34
|
+
@sparql_uri
|
35
|
+
end
|
36
|
+
|
37
|
+
def update_uri=(uri)
|
38
|
+
@update_uri = uri
|
39
|
+
end
|
40
|
+
|
41
|
+
def update_uri
|
42
|
+
raise Exception.new "You need to set a valid SPARQL Update URI using Resource.update_uri = <uri>" unless @update_uri
|
43
|
+
@update_uri
|
44
|
+
end
|
45
|
+
|
46
|
+
def sparql_client
|
47
|
+
SPARQL::Client.new @sparql_uri
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# easy-sparql
|
2
|
+
#
|
3
|
+
# Copyright (c) 2011 British Broadcasting Corporation
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module RDF
|
18
|
+
|
19
|
+
class WS < Vocabulary("http://wsarchive.prototype0.net/ontology/")
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
class PO < Vocabulary("http://purl.org/ontology/po/")
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
class O2 < Vocabulary("http://rdf.ontology2.com/vocab#")
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
class OKB < Vocabulary("http://rdf.ookaboo.com/object/")
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_sparql
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Yves Raimond
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sparql-client
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rest-client
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A (very limited) library for exploring SPARQL endpoints simply
|
63
|
+
email: yves.raimond@bbc.co.uk
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- lib/easy_sparql.rb
|
69
|
+
- lib/easy_sparql/resource.rb
|
70
|
+
- lib/easy_sparql/mock_cache.rb
|
71
|
+
- lib/easy_sparql/store.rb
|
72
|
+
- lib/easy_sparql/vocab.rb
|
73
|
+
homepage:
|
74
|
+
licenses: []
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.8.23
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Simple wrapper to explore SPARQL endpoints
|
97
|
+
test_files: []
|
98
|
+
has_rdoc: false
|