activerdf_redland 1.1 → 1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +2 -0
- data/lib/activerdf_redland/redland.rb +31 -21
- data/test/test_redland_adapter.rb +10 -6
- metadata +7 -7
- data/Rakefile +0 -40
data/CHANGELOG
ADDED
@@ -14,8 +14,6 @@ class RedlandAdapter < ActiveRdfAdapter
|
|
14
14
|
|
15
15
|
# instantiate connection to Redland database
|
16
16
|
def initialize(params = {})
|
17
|
-
|
18
|
-
# TODO: check if the given file exists, or at least look for an exception from redland
|
19
17
|
if params[:location] and params[:location] != :memory
|
20
18
|
# setup file locations for redland database
|
21
19
|
path, file = File.split(params[:location])
|
@@ -27,42 +25,52 @@ class RedlandAdapter < ActiveRdfAdapter
|
|
27
25
|
|
28
26
|
$activerdflog.info "RedlandAdapter: initializing with type: #{type} file: #{file} path: #{path}"
|
29
27
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
28
|
+
begin
|
29
|
+
@store = Redland::HashStore.new(type, file, path, false)
|
30
|
+
@model = Redland::Model.new @store
|
31
|
+
@reads = true
|
32
|
+
@writes = true
|
33
|
+
rescue Redland::RedlandError => e
|
34
|
+
raise ActiveRdfError, "could not initialise Redland database: #{e.message}"
|
35
|
+
end
|
35
36
|
end
|
36
37
|
|
37
38
|
# load a file from the given location with the given syntax into the model.
|
38
39
|
# use Redland syntax strings, e.g. "ntriples" or "rdfxml", defaults to "ntriples"
|
39
40
|
def load(location, syntax="ntriples")
|
40
|
-
$activerdflog.debug "Redland: loading file with syntax: #{syntax} and location: #{location}" if $activerdflog.level == Logger::DEBUG
|
41
41
|
parser = Redland::Parser.new(syntax, "", nil)
|
42
|
-
|
42
|
+
if location =~ /^http/
|
43
|
+
parser.parse_into_model(@model, location)
|
44
|
+
else
|
45
|
+
parser.parse_into_model(@model, "file:#{location}")
|
46
|
+
end
|
43
47
|
end
|
44
48
|
|
45
49
|
# yields query results (as many as requested in select clauses) executed on data source
|
46
50
|
def query(query)
|
47
51
|
qs = Query2SPARQL.translate(query)
|
48
|
-
$activerdflog.debug "RedlandAdapter: executing SPARQL query #{qs}"
|
52
|
+
$activerdflog.debug "RedlandAdapter: executing SPARQL query #{qs}"
|
49
53
|
|
50
54
|
clauses = query.select_clauses.size
|
51
55
|
redland_query = Redland::Query.new(qs, 'sparql')
|
52
56
|
query_results = @model.query_execute(redland_query)
|
57
|
+
|
58
|
+
# return Redland's answer without parsing if ASK query
|
59
|
+
return [[query_results.get_boolean?]] if query.ask?
|
53
60
|
|
54
|
-
$activerdflog.debug "RedlandAdapter: found #{query_results.size} query results"
|
61
|
+
$activerdflog.debug "RedlandAdapter: found #{query_results.size} query results"
|
55
62
|
|
56
63
|
# verify if the query has failed
|
57
64
|
if query_results.nil?
|
58
|
-
$activerdflog.debug "RedlandAdapter: query has failed with nil result"
|
65
|
+
$activerdflog.debug "RedlandAdapter: query has failed with nil result"
|
59
66
|
return false
|
60
67
|
end
|
61
68
|
if not query_results.is_bindings?
|
62
|
-
$activerdflog.debug "RedlandAdapter: query has failed without bindings"
|
69
|
+
$activerdflog.debug "RedlandAdapter: query has failed without bindings"
|
63
70
|
return false
|
64
71
|
end
|
65
72
|
|
73
|
+
|
66
74
|
# convert the result to array
|
67
75
|
#TODO: if block is given we should not parse all results into array first
|
68
76
|
results = query_result_to_array(query_results)
|
@@ -109,16 +117,16 @@ class RedlandAdapter < ActiveRdfAdapter
|
|
109
117
|
|
110
118
|
# add triple to datamodel
|
111
119
|
def add(s, p, o)
|
112
|
-
$activerdflog.debug "adding triple #{s} #{p} #{o}"
|
120
|
+
$activerdflog.debug "adding triple #{s} #{p} #{o}"
|
113
121
|
|
114
122
|
# verify input
|
115
123
|
if s.nil? || p.nil? || o.nil?
|
116
|
-
$activerdflog.debug "cannot add triple with empty subject, exiting"
|
124
|
+
$activerdflog.debug "cannot add triple with empty subject, exiting"
|
117
125
|
return false
|
118
126
|
end
|
119
127
|
|
120
128
|
unless s.respond_to?(:uri) && p.respond_to?(:uri)
|
121
|
-
$activerdflog.debug "cannot add triple where s/p are not resources, exiting"
|
129
|
+
$activerdflog.debug "cannot add triple where s/p are not resources, exiting"
|
122
130
|
return false
|
123
131
|
end
|
124
132
|
|
@@ -145,20 +153,22 @@ class RedlandAdapter < ActiveRdfAdapter
|
|
145
153
|
Redland::librdf_model_sync(@model.model).nil?
|
146
154
|
end
|
147
155
|
alias flush save
|
156
|
+
|
157
|
+
# returns all triples in the datastore
|
158
|
+
def dump
|
159
|
+
Redland.librdf_model_to_string(@model.model, nil, 'ntriples')
|
160
|
+
end
|
148
161
|
|
149
162
|
# returns size of datasources as number of triples
|
150
|
-
#
|
151
163
|
# warning: expensive method as it iterates through all statements
|
152
164
|
def size
|
153
165
|
# we cannot use @model.size, because redland does not allow counting of
|
154
166
|
# file-based models (@model.size raises an error if used on a file)
|
155
|
-
|
156
167
|
# instead, we just dump all triples, and count them
|
157
|
-
|
158
|
-
@model.statements{|s,p,o| stats << [s,p,o]}
|
159
|
-
stats.size
|
168
|
+
@model.triples.size
|
160
169
|
end
|
161
170
|
|
171
|
+
private
|
162
172
|
################ helper methods ####################
|
163
173
|
#TODO: if block is given we should not parse all results into array first
|
164
174
|
def query_result_to_array(query_results)
|
@@ -34,7 +34,7 @@ class TestRedlandAdapter < Test::Unit::TestCase
|
|
34
34
|
test = RDFS::Resource.new 'test'
|
35
35
|
|
36
36
|
adapter.add(eyal, age, test)
|
37
|
-
result = Query.new.distinct(:s).where(:s, :p, :o).execute
|
37
|
+
result = Query.new.distinct(:s).where(:s, :p, :o).execute(:flatten)
|
38
38
|
|
39
39
|
assert_instance_of RDFS::Resource, result
|
40
40
|
assert_equal 'eyaloren.org', result.uri
|
@@ -53,7 +53,7 @@ class TestRedlandAdapter < Test::Unit::TestCase
|
|
53
53
|
adapter2.add(eyal, age, test2)
|
54
54
|
|
55
55
|
# assert only one distinct subject is found (same one in both adapters)
|
56
|
-
assert_equal 1, Query.new.distinct(:s).where(:s, :p, :o).execute
|
56
|
+
assert_equal 1, Query.new.distinct(:s).where(:s, :p, :o).execute.size
|
57
57
|
|
58
58
|
# assert two distinct objects are found
|
59
59
|
results = Query.new.distinct(:o).where(:s, :p, :o).execute
|
@@ -84,6 +84,12 @@ class TestRedlandAdapter < Test::Unit::TestCase
|
|
84
84
|
assert_equal 28, adapter.size
|
85
85
|
end
|
86
86
|
|
87
|
+
def test_remote_load
|
88
|
+
adapter = ConnectionPool.add_data_source :type => :redland
|
89
|
+
adapter.load('http://www.eyaloren.org/foaf.rdf', 'rdfxml')
|
90
|
+
assert_equal 39, adapter.size
|
91
|
+
end
|
92
|
+
|
87
93
|
def test_person_data
|
88
94
|
ConnectionPool.add_data_source :type => :redland, :location => 'test/test-person'
|
89
95
|
Namespace.register(:test, 'http://activerdf.org/test/')
|
@@ -116,7 +122,7 @@ class TestRedlandAdapter < Test::Unit::TestCase
|
|
116
122
|
adapter2.add(eyal, age, test2)
|
117
123
|
|
118
124
|
# assert only one distinct subject is found (same one in both adapters)
|
119
|
-
assert_equal 1, Query.new.distinct(:s).where(:s, :p, :o).execute
|
125
|
+
assert_equal 1, Query.new.distinct(:s).where(:s, :p, :o).execute.size
|
120
126
|
|
121
127
|
# assert two distinct objects are found
|
122
128
|
results = Query.new.distinct(:o).where(:s, :p, :o).execute
|
@@ -151,9 +157,7 @@ class TestRedlandAdapter < Test::Unit::TestCase
|
|
151
157
|
type = Namespace.lookup(:rdf, :type)
|
152
158
|
resource = Namespace.lookup(:rdfs,:resource)
|
153
159
|
|
154
|
-
|
155
|
-
assert_equal 'blue', color
|
156
|
-
assert_instance_of String, color
|
160
|
+
assert_equal 'blue', eyal.test::eye
|
157
161
|
|
158
162
|
ObjectManager.construct_classes
|
159
163
|
assert eyal.instance_of?(TEST::Person)
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.2
|
3
3
|
specification_version: 1
|
4
4
|
name: activerdf_redland
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: "1.
|
7
|
-
date:
|
6
|
+
version: "1.2"
|
7
|
+
date: 2007-03-05 00:00:00 +00:00
|
8
8
|
summary: ActiveRDF adapter to Redland RDF store
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -31,12 +31,12 @@ authors:
|
|
31
31
|
files:
|
32
32
|
- LICENSE
|
33
33
|
- README
|
34
|
-
-
|
35
|
-
- test/test_redland_adapter.rb
|
34
|
+
- CHANGELOG
|
36
35
|
- test/test_person_data.nt
|
36
|
+
- test/test_redland_adapter.rb
|
37
37
|
- lib/activerdf_redland
|
38
|
-
- lib/activerdf_redland/redland.rb
|
39
38
|
- lib/activerdf_redland/init.rb
|
39
|
+
- lib/activerdf_redland/redland.rb
|
40
40
|
test_files: []
|
41
41
|
|
42
42
|
rdoc_options: []
|
@@ -67,5 +67,5 @@ dependencies:
|
|
67
67
|
requirements:
|
68
68
|
- - ">="
|
69
69
|
- !ruby/object:Gem::Version
|
70
|
-
version:
|
70
|
+
version: "1.3"
|
71
71
|
version:
|
data/Rakefile
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'rake'
|
2
|
-
require 'rake/testtask'
|
3
|
-
require 'rake/clean'
|
4
|
-
require 'rake/gempackagetask'
|
5
|
-
require 'rake/rdoctask'
|
6
|
-
require '../tools/rakehelp'
|
7
|
-
require 'fileutils'
|
8
|
-
include FileUtils
|
9
|
-
|
10
|
-
setup_tests
|
11
|
-
setup_clean ["pkg", "lib/*.bundle", "*.gem", ".config"]
|
12
|
-
|
13
|
-
setup_rdoc ['README', 'LICENSE', 'lib/**/*.rb', 'doc/**/*.rdoc']
|
14
|
-
|
15
|
-
desc "Does a full compile, test run"
|
16
|
-
task :default => [:test, :package]
|
17
|
-
|
18
|
-
version=ENV['REL']
|
19
|
-
name="activerdf_redland"
|
20
|
-
|
21
|
-
setup_gem(name, version) do |spec|
|
22
|
-
spec.summary = "ActiveRDF adapter to Redland RDF store"
|
23
|
-
spec.description = spec.summary
|
24
|
-
spec.author="Eyal Oren <eyal.oren@deri.org"
|
25
|
-
spec.add_dependency('gem_plugin', '>= 0.2.1')
|
26
|
-
spec.add_dependency('activerdf', '>= 0.9.2')
|
27
|
-
spec.requirements << 'librdf0 (Redland)'
|
28
|
-
spec.requirements << 'librdf-ruby (Redland Ruby bindings)'
|
29
|
-
end
|
30
|
-
|
31
|
-
|
32
|
-
task :install => [:package] do
|
33
|
-
sh %{sudo gem install pkg/#{name}-#{version}.gem}
|
34
|
-
end
|
35
|
-
|
36
|
-
task :uninstall => [:clean] do
|
37
|
-
sh %{sudo gem uninstall #{name}}
|
38
|
-
end
|
39
|
-
|
40
|
-
task :reinstall => [:uninstall, :install]
|