activerdf_net7 1.6.16 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +63 -0
- data/activerdf-jena/lib/activerdf_jena/jena.rb +4 -4
- data/activerdf-jena/lib/activerdf_jena/jena_adapter.rb +55 -55
- data/activerdf-jena/lib/activerdf_jena/lucene.rb +1 -1
- data/activerdf-jena/lib/activerdf_jena/ng4j.rb +7 -7
- data/activerdf-jena/lib/activerdf_jena/ng4j_adapter.rb +47 -47
- data/activerdf-jena/lib/activerdf_jena/pellet.rb +1 -1
- data/activerdf-jena/test/test_jena_adapter.rb +121 -120
- data/activerdf-jena/test/test_ng4j_adapter.rb +111 -110
- data/activerdf-rdflite/lib/activerdf_rdflite/fetching.rb +23 -19
- data/activerdf-rdflite/lib/activerdf_rdflite/rdflite.rb +153 -277
- data/activerdf-rdflite/lib/activerdf_rdflite/suggesting.rb +2 -2
- data/activerdf-rdflite/test/test_fetching.rb +7 -22
- data/activerdf-rdflite/test/test_rdflite.rb +44 -257
- data/activerdf-redland/lib/activerdf_redland/redland.rb +246 -282
- data/activerdf-redland/test/test_redland_adapter.rb +62 -224
- data/activerdf-sesame/ext/wrapper-sesame2.jar +0 -0
- data/activerdf-sesame/java/build.number +2 -2
- data/activerdf-sesame/java/build.xml +0 -0
- data/activerdf-sesame/java/lib/junit-3.8.2.jar +0 -0
- data/activerdf-sesame/java/settings.xml +0 -0
- data/activerdf-sesame/java/src/org/activerdf/wrapper/sesame2/WrapperForSesame2.java +0 -0
- data/activerdf-sesame/java/temp/build/org/activerdf/wrapper/sesame2/WrapperForSesame2.class +0 -0
- data/activerdf-sesame/java/temp/manifest/MANIFEST.MF +2 -2
- data/activerdf-sesame/java/test-src/org/activerdf/wrapper/sesame2/TestWrapperForSesame2.java +0 -0
- data/activerdf-sesame/lib/activerdf_sesame/sesame.rb +360 -364
- data/activerdf-sesame/test/test_sesame_adapter.rb +85 -83
- data/activerdf-sparql/lib/activerdf_sparql/sparql.rb +147 -148
- data/activerdf-sparql/lib/activerdf_sparql/sparql_result_parser.rb +5 -5
- data/activerdf-sparql/test/test_sparql_adapter.rb +2 -0
- data/activerdf-yars/lib/activerdf_yars/jars2.rb +85 -83
- data/lib/active_rdf/federation/active_rdf_adapter.rb +26 -39
- data/lib/active_rdf/federation/connection_pool.rb +119 -110
- data/lib/active_rdf/federation/federation_manager.rb +51 -51
- data/lib/active_rdf/objectmanager/bnode.rb +8 -2
- data/lib/active_rdf/objectmanager/literal.rb +81 -50
- data/lib/active_rdf/objectmanager/namespace.rb +117 -84
- data/lib/active_rdf/objectmanager/object_manager.rb +101 -96
- data/lib/active_rdf/objectmanager/ordered_set.rb +1 -1
- data/lib/active_rdf/objectmanager/property.rb +345 -0
- data/lib/active_rdf/objectmanager/property_list.rb +4 -4
- data/lib/active_rdf/objectmanager/property_lookup.rb +57 -0
- data/lib/active_rdf/objectmanager/resource.rb +293 -501
- data/lib/active_rdf/objectmanager/resource_like.rb +2 -2
- data/lib/active_rdf/objectmanager/resource_query.rb +85 -0
- data/lib/active_rdf/queryengine/ntriples_parser.rb +75 -68
- data/lib/active_rdf/queryengine/query.rb +237 -183
- data/lib/active_rdf/queryengine/query2jars2.rb +17 -15
- data/lib/active_rdf/queryengine/query2sparql.rb +107 -101
- data/lib/active_rdf.rb +28 -17
- data/lib/active_rdf_helpers.rb +37 -5
- data/lib/active_rdf_log.rb +11 -11
- data/test/adapters/test_activerdf_adapter.rb +138 -0
- data/test/{test_adapters.rb → adapters/test_adapters.rb} +6 -24
- data/test/adapters/test_bnode_capable_adapter.rb +31 -0
- data/test/adapters/test_context_aware_adapter.rb +31 -0
- data/test/adapters/test_network_aware_adapter.rb +29 -0
- data/test/adapters/test_persistent_adapter.rb +21 -0
- data/test/adapters/test_read_only_adapter.rb +15 -0
- data/test/adapters/test_reasoning_adapter.rb +11 -0
- data/test/adapters/test_writable_adapter.rb +163 -0
- data/test/common.rb +78 -96
- data/test/federation/test_connection_pool.rb +25 -44
- data/test/federation/test_federation_manager.rb +45 -45
- data/test/objectmanager/test_literal.rb +47 -26
- data/test/objectmanager/test_namespace.rb +3 -1
- data/test/objectmanager/test_object_manager.rb +35 -45
- data/test/objectmanager/test_ordered_set.rb +1 -1
- data/test/objectmanager/test_property.rb +261 -0
- data/test/objectmanager/test_resource_reading.rb +196 -104
- data/test/objectmanager/test_resource_reasoning.rb +26 -0
- data/test/objectmanager/test_resource_writing.rb +34 -25
- data/test/queryengine/my_external_resource.rb +5 -1
- data/test/queryengine/test_external_resource_class.rb +1 -8
- data/test/queryengine/test_ntriples_parser.rb +5 -3
- data/test/queryengine/test_query.rb +3 -3
- data/test/queryengine/test_query2jars2.rb +2 -2
- data/test/queryengine/test_query2sparql.rb +2 -2
- data/test/queryengine/test_query_engine.rb +46 -28
- metadata +16 -8
- data/activerdf-rdflite/test/test_bnode_data.nt +0 -5
- data/activerdf-rdflite/test/test_data.nt +0 -32
- data/activerdf-rdflite/test/test_escaped_data.nt +0 -2
- data/activerdf-redland/test/test_person_data.nt +0 -42
- data/test/objectmanager/test_talia_syntax.rb +0 -68
@@ -8,11 +8,12 @@ require 'rdf/redland'
|
|
8
8
|
|
9
9
|
# Adapter to Redland database
|
10
10
|
# uses SPARQL for querying
|
11
|
-
|
11
|
+
module ActiveRDF
|
12
|
+
class RedlandAdapter < ActiveRdfAdapter
|
12
13
|
ActiveRdfLogger::log_info "Loading Redland adapter", self
|
13
|
-
|
14
|
+
ConnectionPool.register_adapter(:redland,self)
|
14
15
|
|
15
|
-
|
16
|
+
# instantiate connection to Redland database
|
16
17
|
# * location: Data location (:memory, :mysql, :postgresql)
|
17
18
|
# * database: Database name
|
18
19
|
# * new: Create new database
|
@@ -21,342 +22,305 @@ class RedlandAdapter < ActiveRdfAdapter
|
|
21
22
|
# * port: Database server port
|
22
23
|
# * reconnect: Set automatic reconnect to database server
|
23
24
|
# * user: Username
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
#
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
25
|
+
def initialize(params = {})
|
26
|
+
super
|
27
|
+
location = params[:location]
|
28
|
+
name = params[:name] || ''
|
29
|
+
options = {}
|
30
|
+
options[:write],options[:new],options[:contexts] = [@writes,@new,@contexts].collect{|bool| bool ? 'yes' : 'no'}
|
31
|
+
|
32
|
+
# supported storage modules: mysql, postgresql, sqlite, hashes(as 'memory' or 'bdb')
|
33
|
+
# unsupported storage modules: uri, file, memory, tstore, trees
|
34
|
+
# see http://librdf.org/docs/api/redland-storage-modules.html
|
35
|
+
case location
|
36
|
+
when 'postgresql','mysql','sqlite'
|
37
|
+
store_type = location
|
38
|
+
if location == 'postgresql' or location == 'mysql'
|
39
|
+
[:host, :port, :database, :user, :password].each{|k| options[k] = params[k] if params[k]}
|
40
|
+
options[:host] ||= 'localhost'
|
41
|
+
end
|
42
|
+
when 'memory',nil
|
43
|
+
# use storage module hashes with hash-type 'memory' instead of non-indexing storage module memory
|
44
|
+
store_type = 'hashes'
|
45
|
+
options[:hash_type] = 'memory';
|
46
|
+
options.delete(:new) # not used with this hash-type
|
47
|
+
else
|
48
|
+
# use storage module hashes with hash-type 'bdb' instead of non-indexing storage module file
|
49
|
+
store_type = 'hashes'
|
50
|
+
options[:hash_type] = 'bdb'
|
48
51
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
52
|
+
if location.include?('/')
|
53
|
+
options[:dir], name = File.split(location)
|
54
|
+
else
|
55
|
+
options[:dir] = '.'
|
56
|
+
name = location
|
57
|
+
end
|
54
58
|
end
|
55
|
-
else
|
56
|
-
# fall back to in-memory redland
|
57
|
-
type = 'memory'; path = ''; file = '.'; want_new = false; write = true; contexts = true
|
58
|
-
end
|
59
59
|
|
60
|
-
|
60
|
+
hash_type = options.delete(:hash_type)
|
61
|
+
options = options.collect{|k,v| "#{k}='#{v}'"}.join(',')
|
62
|
+
options << "hash-type='#{hash_type}'" if hash_type # convert option key from hash_type to hash-type. :hash-type is an invalid symbol
|
63
|
+
@model = Redland::Model.new Redland::TripleStore.new(store_type, name, options)
|
64
|
+
@options = options
|
65
|
+
ActiveRDFLogger::log_inf(self) { "RedlandAdapter: initialized adapter with type=\'#{store_type}\', name=\'#{name}\' options: #{options} => #{@model.inspect}" }
|
61
66
|
|
62
|
-
|
63
|
-
|
64
|
-
@model = Redland::Model.new @store
|
65
|
-
@reads = true
|
66
|
-
@writes = true
|
67
|
-
ActiveRdfLogger.log_info(self) { "Initialised Redland adapter to #{@model.inspect}" }
|
68
|
-
|
69
|
-
rescue Redland::RedlandError => e
|
70
|
-
raise ActiveRdfError, "Could not initialise Redland database: #{e.message}"
|
67
|
+
rescue Redland::RedlandError => e
|
68
|
+
raise ActiveRdfError, "RedlandAdapter: could not initialise Redland database: #{e.message}\nstore_type=\'#{store_type}\', name=\'#{name}\' options: #{options}"
|
71
69
|
end
|
72
|
-
end
|
73
70
|
|
74
|
-
|
75
|
-
|
76
|
-
# * new: Create new database
|
77
|
-
# * host: Database server address
|
78
|
-
# * password: Password
|
79
|
-
# * port: Database server port
|
80
|
-
# * reconnect: Set automatic reconnect to database server
|
81
|
-
# * user: Username
|
82
|
-
def initialize_postgresql(params = {})
|
83
|
-
# author: Richard Dale
|
84
|
-
type = 'postgresql'
|
85
|
-
name = params[:name]
|
86
|
-
|
87
|
-
options = []
|
88
|
-
options << "new='#{params[:new]}'" if params[:new]
|
89
|
-
options << "bulk='#{params[:bulk]}'" if params[:bulk]
|
90
|
-
options << "merge='#{params[:merge]}'" if params[:merge]
|
91
|
-
options << "host='#{params[:host]}'" if params[:host]
|
92
|
-
options << "database='#{params[:database]}'" if params[:database]
|
93
|
-
options << "user='#{params[:user]}'" if params[:user]
|
94
|
-
options << "password='#{params[:password]}'" if params[:password]
|
95
|
-
options << "port='#{params[:port]}'" if params[:port]
|
96
|
-
|
97
|
-
|
98
|
-
ActiveRdfLogger::log_info "Initializing with type: #{type} name: #{name} options: #{options.join(',')}", self
|
99
|
-
|
100
|
-
begin
|
101
|
-
@store = Redland::TripleStore.new(type, name, options.join(','))
|
102
|
-
@model = Redland::Model.new @store
|
103
|
-
@reads = true
|
104
|
-
@writes = true
|
105
|
-
rescue Redland::RedlandError => e
|
106
|
-
raise ActiveRdfError, "Could not initialise Redland database: #{e.message}"
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
# load a file from the given location with the given syntax into the model.
|
111
|
-
# use Redland syntax strings, e.g. "ntriples" or "rdfxml", defaults to "ntriples"
|
71
|
+
# load a file from the given location with the given syntax into the model.
|
72
|
+
# use Redland syntax strings, e.g. "ntriples" or "rdfxml", defaults to "ntriples"
|
112
73
|
# * location: location of file to load.
|
113
74
|
# * syntax: syntax of file
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
75
|
+
def load(location, syntax="ntriples")
|
76
|
+
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
|
77
|
+
parser = Redland::Parser.new(syntax, "", nil)
|
78
|
+
unless location =~ /^http/
|
79
|
+
location = "file:#{location}"
|
80
|
+
end
|
81
|
+
|
82
|
+
context = @contexts ? Redland::Uri.new(location) : nil
|
83
|
+
parser.parse_into_model(@model, location, nil, context)
|
84
|
+
|
85
|
+
save if ConnectionPool.auto_flush?
|
86
|
+
rescue Redland::RedlandError => e
|
87
|
+
$activerdflog.warn "RedlandAdapter: loading #{location} failed in Redland library: #{e}"
|
88
|
+
return false
|
120
89
|
end
|
121
|
-
end
|
122
90
|
|
123
|
-
|
124
|
-
|
125
|
-
|
91
|
+
# yields query results (as many as requested in select clauses) executed on data source
|
92
|
+
def execute(query, &block)
|
93
|
+
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
|
94
|
+
qs = Query2SPARQL.translate(query)
|
126
95
|
ActiveRdfLogger::log_debug(self) { "Executing SPARQL query #{qs}" }
|
127
96
|
|
128
|
-
|
129
|
-
|
130
|
-
query_results = @model.query_execute(redland_query)
|
97
|
+
redland_query = Redland::Query.new(qs, 'sparql')
|
98
|
+
query_results = @model.query_execute(redland_query)
|
131
99
|
|
132
|
-
|
133
|
-
|
100
|
+
# return Redland's answer without parsing if ASK query
|
101
|
+
return [[query_results.get_boolean?]] if query.ask?
|
134
102
|
|
135
103
|
ActiveRdfLogger::log_debug(self) { "Found #{query_results.size} query results" }
|
136
104
|
|
137
|
-
|
138
|
-
|
105
|
+
# verify if the query has failed
|
106
|
+
if query_results.nil?
|
139
107
|
ActiveRdfLogger::log_debug "Query has failed with nil result", self
|
140
|
-
|
141
|
-
|
108
|
+
return false
|
109
|
+
end
|
142
110
|
|
143
|
-
|
111
|
+
if not query_results.is_bindings?
|
144
112
|
ActiveRdfLogger::log_debug "Query has failed without bindings", self
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
# convert the result to array
|
149
|
-
#TODO: if block is given we should not parse all results into array first
|
150
|
-
results = query_result_to_array(query_results, false, query.resource_class)
|
113
|
+
return false
|
114
|
+
end
|
151
115
|
|
152
|
-
|
153
|
-
|
154
|
-
|
116
|
+
if query.count?
|
117
|
+
while not query_results.finished?
|
118
|
+
query_results.next
|
119
|
+
end
|
120
|
+
[[query_results.count]]
|
121
|
+
else
|
122
|
+
# convert the results to array
|
123
|
+
query_result_to_array(query_results, &block)
|
155
124
|
end
|
156
|
-
else
|
157
|
-
results
|
158
125
|
end
|
159
|
-
end
|
160
126
|
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
127
|
+
# executes query and returns results as SPARQL JSON or XML results
|
128
|
+
# requires svn version of redland-ruby bindings
|
129
|
+
# * query: ActiveRDF Query object
|
130
|
+
# * result_format: :json or :xml
|
131
|
+
def get_query_results(query, result_format=nil)
|
132
|
+
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
|
166
133
|
get_sparql_query_results(Query2SPARQL.translate(query), result_format, query.resource_class)
|
167
|
-
|
134
|
+
end
|
168
135
|
|
169
|
-
|
170
|
-
|
171
|
-
|
136
|
+
# executes sparql query and returns results as SPARQL JSON or XML results
|
137
|
+
# * query: sparql query string
|
138
|
+
# * result_format: :json or :xml
|
172
139
|
# * result_type: Is the type that is used for "resource" results
|
173
140
|
def get_sparql_query_results(qs, result_type, result_format=nil)
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
141
|
+
# author: Eric Hanson
|
142
|
+
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
|
143
|
+
|
144
|
+
# set uri for result formatting
|
145
|
+
result_uri =
|
146
|
+
case result_format
|
147
|
+
when :json
|
148
|
+
Redland::Uri.new('http://www.w3.org/2001/sw/DataAccess/json-sparql/')
|
149
|
+
when :xml
|
150
|
+
Redland::Uri.new('http://www.w3.org/TR/2004/WD-rdf-sparql-XMLres-20041221/')
|
151
|
+
end
|
184
152
|
|
185
|
-
|
186
|
-
|
187
|
-
|
153
|
+
# query redland
|
154
|
+
redland_query = Redland::Query.new(qs, 'sparql')
|
155
|
+
query_results = @model.query_execute(redland_query)
|
188
156
|
|
189
157
|
if (result_format != :array)
|
190
158
|
# get string representation in requested result_format (json or xml)
|
191
|
-
query_results.to_string()
|
192
|
-
else
|
193
|
-
# get array result
|
194
|
-
query_result_to_array(query_results, true, result_type)
|
159
|
+
query_results.to_string(result_uri)
|
195
160
|
end
|
196
161
|
end
|
197
162
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
result = false
|
204
|
-
ActiveRdfLogger::log_debug(self) { "Adding triple #{s} #{p} #{o}" }
|
163
|
+
# add triple to datamodel
|
164
|
+
def add(s,p,o,c=nil)
|
165
|
+
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
|
166
|
+
ActiveRdfLogger::log_warn(self) { "Adapter does not support contexts" } if (!@contexts and !c.nil?)
|
167
|
+
ActiveRdfLogger::log_debug(self) { "Adding triple #{s} #{p} #{o} #{c}" }
|
205
168
|
|
206
|
-
|
207
|
-
|
169
|
+
# verify input
|
170
|
+
if s.nil? || p.nil? || o.nil?
|
208
171
|
ActiveRdfLogger::log_debug "Cannot add triple with empty subject, exiting", self
|
209
|
-
|
210
|
-
end
|
211
|
-
|
212
|
-
unless (((s.class == String) && (p.class == String) && (o.class == String)) &&
|
213
|
-
((s[0..0] == '<') && (s[-1..-1] == '>')) &&
|
214
|
-
((p[0..0] == '<') && (p[-1..-1] == '>'))) || (s.respond_to?(:uri) && p.respond_to?(:uri))
|
215
|
-
ActiveRdfLogger::log_debug "Cannot add triple where s/p are not resources, exiting", self
|
216
|
-
return false
|
217
|
-
end
|
218
|
-
|
219
|
-
begin
|
220
|
-
if ((s.class != String) || (p.class != String) || (o.class != String))
|
221
|
-
result = (@model.add(wrap(s), wrap(p), wrap(o)) == 0)
|
222
|
-
else
|
223
|
-
result = (@model.add(wrapString(s), wrapString(p), wrapString(o)) == 0)
|
172
|
+
return false
|
224
173
|
end
|
225
|
-
|
226
|
-
|
174
|
+
|
175
|
+
unless s.respond_to?(:uri) && p.respond_to?(:uri)
|
176
|
+
ActiveRdfLogger::log_info(self) { "RedlandAdapter: cannot add triple where s/p are not resources, exiting" }
|
177
|
+
return false
|
227
178
|
end
|
228
|
-
|
229
|
-
rescue Redland::RedlandError => e
|
230
|
-
ActiveRdfLogger::log_warn "Adding triple failed in Redland library: #{e}", self
|
231
|
-
return false
|
232
|
-
end
|
233
|
-
end
|
179
|
+
quad = [s,p,o,c].collect{|e| to_redland(e)}
|
234
180
|
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
if
|
248
|
-
|
181
|
+
@model.add(*quad)
|
182
|
+
save if ConnectionPool.auto_flush?
|
183
|
+
rescue Redland::RedlandError => e
|
184
|
+
ActiveRdfLogger::log_warn "Adding triple (#{quad}) failed in Redland library: #{e}", self
|
185
|
+
return false
|
186
|
+
end
|
187
|
+
|
188
|
+
# deletes triple(s,p,o) from datastore
|
189
|
+
# nil parameters match anything: delete(nil,nil,nil) will delete all triples
|
190
|
+
def delete(s,p,o,c=nil)
|
191
|
+
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
|
192
|
+
quad = [s,p,o,c].collect{|e| to_redland(e)}
|
193
|
+
if quad.all?{|t| t.nil?}
|
194
|
+
clear
|
195
|
+
elsif quad[0..2].any?{|t| t.nil?}
|
196
|
+
@model.find(*quad).each{|stmt| @model.delete_statement(stmt,c)}
|
249
197
|
else
|
250
|
-
@model.delete(
|
198
|
+
@model.delete(*quad)
|
251
199
|
end
|
200
|
+
save if ConnectionPool.auto_flush?
|
201
|
+
rescue Redland::RedlandError => e
|
202
|
+
$activerdflog.warn "RedlandAdapter: deleting triple failed in Redland library: #{e}"
|
203
|
+
return false
|
252
204
|
end
|
253
205
|
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
206
|
+
# saves updates to the model into the redland file location
|
207
|
+
def save
|
208
|
+
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
|
209
|
+
Redland::librdf_model_sync(@model.model).nil?
|
210
|
+
end
|
211
|
+
alias flush save
|
212
|
+
|
213
|
+
# returns all triples in the datastore
|
214
|
+
def dump
|
215
|
+
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
|
216
|
+
arr = []
|
217
|
+
@model.triples{|s,p,o| arr << [s.to_s,p.to_s,o.to_s]}
|
218
|
+
arr
|
219
|
+
end
|
262
220
|
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
Redland.librdf_model_to_string(@model.model, nil, type)
|
267
|
-
end
|
221
|
+
def contexts
|
222
|
+
@model.contexts
|
223
|
+
end
|
268
224
|
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
225
|
+
# returns size of datasources as number of triples
|
226
|
+
# warning: expensive method as it iterates through all statements
|
227
|
+
def size
|
228
|
+
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
|
229
|
+
# we cannot use @model.size, because redland does not allow counting of
|
230
|
+
# file-based models (@model.size raises an error if used on a file)
|
231
|
+
# instead, we just dump all triples, and count them
|
232
|
+
@model.triples.size
|
233
|
+
end
|
277
234
|
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
235
|
+
# clear all real triples of adapter
|
236
|
+
def clear
|
237
|
+
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
|
238
|
+
@model.triples.each{|stmt| @model.delete_statement(stmt)}
|
239
|
+
end
|
282
240
|
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
241
|
+
# close adapter and remove it from the ConnectionPool
|
242
|
+
def close
|
243
|
+
if @enabled
|
244
|
+
ConnectionPool.remove_data_source(self)
|
245
|
+
flush # sync model with datastore
|
246
|
+
@model = nil # remove reference to model for removal by GC
|
247
|
+
@enabled = false
|
248
|
+
end
|
249
|
+
end
|
287
250
|
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
results << (0..number_bindings-1).collect do |i|
|
304
|
-
# node is the query result for one binding
|
305
|
-
node = query_results.binding_value(i)
|
306
|
-
|
307
|
-
# we determine the node type
|
308
|
-
if node.literal?
|
309
|
-
# for literal nodes we just return the value
|
310
|
-
Redland.librdf_node_get_literal_value(node.node)
|
311
|
-
elsif node.blank?
|
312
|
-
# blank nodes we ignore
|
313
|
-
if to_string == false
|
251
|
+
private
|
252
|
+
################ helper methods ####################
|
253
|
+
def query_result_to_array(query_results, &block)
|
254
|
+
results = []
|
255
|
+
number_bindings = query_results.binding_names.size
|
256
|
+
|
257
|
+
# redland results are set that needs to be iterated
|
258
|
+
while not query_results.finished?
|
259
|
+
# we collect the bindings in each row and add them to results
|
260
|
+
row = (0..number_bindings-1).collect do |i|
|
261
|
+
# node is the query result for one binding
|
262
|
+
node = query_results.binding_value(i)
|
263
|
+
|
264
|
+
# we determine the node type
|
265
|
+
if node.nil?
|
314
266
|
nil
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
267
|
+
elsif node.literal?
|
268
|
+
value = Redland.librdf_node_get_literal_value(node.node)
|
269
|
+
|
270
|
+
lang_uri_ref = Redland.librdf_node_get_literal_value_language(node.node)
|
271
|
+
dt_uri_ref = Redland.librdf_node_get_literal_value_datatype_uri(node.node)
|
272
|
+
if lang_uri_ref
|
273
|
+
LocalizedString.new(value,Redland::Uri.new(lang_uri_ref).to_s)
|
274
|
+
elsif dt_uri_ref
|
275
|
+
type = RDFS::Resource.new(Redland::Uri.new(dt_uri_ref).to_s)
|
276
|
+
RDFS::Literal.typed(value,type)
|
277
|
+
elsif lang_uri_ref and dt_uri_ref
|
278
|
+
raise ActiveRdfError, "cannot have both datatype and lang set"
|
319
279
|
else
|
320
|
-
|
280
|
+
RDFS::Literal.typed(value,XSD::string) # string is default type if none specified
|
321
281
|
end
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
if to_string == false
|
326
|
-
result_type.new(node.uri.to_s)
|
282
|
+
elsif node.blank?
|
283
|
+
# blank nodes are not currently supported
|
284
|
+
nil
|
327
285
|
else
|
328
|
-
|
286
|
+
# other nodes are rdfs:resources
|
287
|
+
RDFS::Resource.new(node.uri.to_s)
|
329
288
|
end
|
330
289
|
end
|
290
|
+
if block_given?
|
291
|
+
yield row
|
292
|
+
else
|
293
|
+
results << row
|
294
|
+
end
|
295
|
+
# iterate through result set
|
296
|
+
query_results.next
|
331
297
|
end
|
332
|
-
|
333
|
-
query_results.next
|
298
|
+
results unless block_given?
|
334
299
|
end
|
335
300
|
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
301
|
+
def to_redland(obj)
|
302
|
+
case obj
|
303
|
+
when RDFS::Resource
|
304
|
+
Redland::Uri.new(obj.uri)
|
305
|
+
when RDFS::Literal
|
306
|
+
str = obj.kind_of?(Time) ? obj.xmlschema : obj.to_s
|
307
|
+
if not $activerdf_without_datatype
|
308
|
+
if obj.kind_of?(LocalizedString)
|
309
|
+
Redland::Literal.new(str, obj.lang)
|
310
|
+
else
|
311
|
+
Redland::Literal.new(str,nil,Redland::Uri.new(obj.datatype.uri))
|
312
|
+
end
|
313
|
+
else
|
314
|
+
Redland::Literal.new(str)
|
315
|
+
end
|
316
|
+
when Class
|
317
|
+
raise ActiveRdfError, "RedlandAdapter: class must inherit from RDFS::Resource" unless obj.ancestors.include?(RDFS::Resource)
|
318
|
+
Redland::Uri.new(obj.class_uri.to_s)
|
319
|
+
when Symbol, nil
|
320
|
+
nil
|
321
|
+
else
|
322
|
+
Redland::Literal.new(obj.to_s)
|
356
323
|
end
|
357
|
-
|
358
|
-
return Redland::Literal.new(node)
|
359
|
-
end
|
324
|
+
end
|
360
325
|
end
|
361
|
-
|
362
326
|
end
|