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.
Files changed (85) hide show
  1. data/CHANGELOG +63 -0
  2. data/activerdf-jena/lib/activerdf_jena/jena.rb +4 -4
  3. data/activerdf-jena/lib/activerdf_jena/jena_adapter.rb +55 -55
  4. data/activerdf-jena/lib/activerdf_jena/lucene.rb +1 -1
  5. data/activerdf-jena/lib/activerdf_jena/ng4j.rb +7 -7
  6. data/activerdf-jena/lib/activerdf_jena/ng4j_adapter.rb +47 -47
  7. data/activerdf-jena/lib/activerdf_jena/pellet.rb +1 -1
  8. data/activerdf-jena/test/test_jena_adapter.rb +121 -120
  9. data/activerdf-jena/test/test_ng4j_adapter.rb +111 -110
  10. data/activerdf-rdflite/lib/activerdf_rdflite/fetching.rb +23 -19
  11. data/activerdf-rdflite/lib/activerdf_rdflite/rdflite.rb +153 -277
  12. data/activerdf-rdflite/lib/activerdf_rdflite/suggesting.rb +2 -2
  13. data/activerdf-rdflite/test/test_fetching.rb +7 -22
  14. data/activerdf-rdflite/test/test_rdflite.rb +44 -257
  15. data/activerdf-redland/lib/activerdf_redland/redland.rb +246 -282
  16. data/activerdf-redland/test/test_redland_adapter.rb +62 -224
  17. data/activerdf-sesame/ext/wrapper-sesame2.jar +0 -0
  18. data/activerdf-sesame/java/build.number +2 -2
  19. data/activerdf-sesame/java/build.xml +0 -0
  20. data/activerdf-sesame/java/lib/junit-3.8.2.jar +0 -0
  21. data/activerdf-sesame/java/settings.xml +0 -0
  22. data/activerdf-sesame/java/src/org/activerdf/wrapper/sesame2/WrapperForSesame2.java +0 -0
  23. data/activerdf-sesame/java/temp/build/org/activerdf/wrapper/sesame2/WrapperForSesame2.class +0 -0
  24. data/activerdf-sesame/java/temp/manifest/MANIFEST.MF +2 -2
  25. data/activerdf-sesame/java/test-src/org/activerdf/wrapper/sesame2/TestWrapperForSesame2.java +0 -0
  26. data/activerdf-sesame/lib/activerdf_sesame/sesame.rb +360 -364
  27. data/activerdf-sesame/test/test_sesame_adapter.rb +85 -83
  28. data/activerdf-sparql/lib/activerdf_sparql/sparql.rb +147 -148
  29. data/activerdf-sparql/lib/activerdf_sparql/sparql_result_parser.rb +5 -5
  30. data/activerdf-sparql/test/test_sparql_adapter.rb +2 -0
  31. data/activerdf-yars/lib/activerdf_yars/jars2.rb +85 -83
  32. data/lib/active_rdf/federation/active_rdf_adapter.rb +26 -39
  33. data/lib/active_rdf/federation/connection_pool.rb +119 -110
  34. data/lib/active_rdf/federation/federation_manager.rb +51 -51
  35. data/lib/active_rdf/objectmanager/bnode.rb +8 -2
  36. data/lib/active_rdf/objectmanager/literal.rb +81 -50
  37. data/lib/active_rdf/objectmanager/namespace.rb +117 -84
  38. data/lib/active_rdf/objectmanager/object_manager.rb +101 -96
  39. data/lib/active_rdf/objectmanager/ordered_set.rb +1 -1
  40. data/lib/active_rdf/objectmanager/property.rb +345 -0
  41. data/lib/active_rdf/objectmanager/property_list.rb +4 -4
  42. data/lib/active_rdf/objectmanager/property_lookup.rb +57 -0
  43. data/lib/active_rdf/objectmanager/resource.rb +293 -501
  44. data/lib/active_rdf/objectmanager/resource_like.rb +2 -2
  45. data/lib/active_rdf/objectmanager/resource_query.rb +85 -0
  46. data/lib/active_rdf/queryengine/ntriples_parser.rb +75 -68
  47. data/lib/active_rdf/queryengine/query.rb +237 -183
  48. data/lib/active_rdf/queryengine/query2jars2.rb +17 -15
  49. data/lib/active_rdf/queryengine/query2sparql.rb +107 -101
  50. data/lib/active_rdf.rb +28 -17
  51. data/lib/active_rdf_helpers.rb +37 -5
  52. data/lib/active_rdf_log.rb +11 -11
  53. data/test/adapters/test_activerdf_adapter.rb +138 -0
  54. data/test/{test_adapters.rb → adapters/test_adapters.rb} +6 -24
  55. data/test/adapters/test_bnode_capable_adapter.rb +31 -0
  56. data/test/adapters/test_context_aware_adapter.rb +31 -0
  57. data/test/adapters/test_network_aware_adapter.rb +29 -0
  58. data/test/adapters/test_persistent_adapter.rb +21 -0
  59. data/test/adapters/test_read_only_adapter.rb +15 -0
  60. data/test/adapters/test_reasoning_adapter.rb +11 -0
  61. data/test/adapters/test_writable_adapter.rb +163 -0
  62. data/test/common.rb +78 -96
  63. data/test/federation/test_connection_pool.rb +25 -44
  64. data/test/federation/test_federation_manager.rb +45 -45
  65. data/test/objectmanager/test_literal.rb +47 -26
  66. data/test/objectmanager/test_namespace.rb +3 -1
  67. data/test/objectmanager/test_object_manager.rb +35 -45
  68. data/test/objectmanager/test_ordered_set.rb +1 -1
  69. data/test/objectmanager/test_property.rb +261 -0
  70. data/test/objectmanager/test_resource_reading.rb +196 -104
  71. data/test/objectmanager/test_resource_reasoning.rb +26 -0
  72. data/test/objectmanager/test_resource_writing.rb +34 -25
  73. data/test/queryengine/my_external_resource.rb +5 -1
  74. data/test/queryengine/test_external_resource_class.rb +1 -8
  75. data/test/queryengine/test_ntriples_parser.rb +5 -3
  76. data/test/queryengine/test_query.rb +3 -3
  77. data/test/queryengine/test_query2jars2.rb +2 -2
  78. data/test/queryengine/test_query2sparql.rb +2 -2
  79. data/test/queryengine/test_query_engine.rb +46 -28
  80. metadata +16 -8
  81. data/activerdf-rdflite/test/test_bnode_data.nt +0 -5
  82. data/activerdf-rdflite/test/test_data.nt +0 -32
  83. data/activerdf-rdflite/test/test_escaped_data.nt +0 -2
  84. data/activerdf-redland/test/test_person_data.nt +0 -42
  85. data/test/objectmanager/test_talia_syntax.rb +0 -68
@@ -5,208 +5,207 @@ require 'cgi'
5
5
  require 'rexml/document'
6
6
  require "#{File.dirname(__FILE__)}/sparql_result_parser"
7
7
 
8
- # SPARQL adapter
9
- class SparqlAdapter < ActiveRdfAdapter
8
+ module ActiveRDF
9
+ # SPARQL adapter
10
+ class SparqlAdapter < ActiveRdfAdapter
10
11
  ActiveRdfLogger::log_info "Loading SPARQL adapter", self
11
- ConnectionPool.register_adapter(:sparql, self)
12
+ ConnectionPool.register_adapter(:sparql, self)
12
13
 
13
- attr_reader :engine
14
- attr_reader :caching
14
+ attr_reader :engine
15
+ attr_reader :caching
15
16
 
16
- @@sparql_cache = {}
17
+ @@sparql_cache = {}
17
18
 
18
- def SparqlAdapter.get_cache
19
- return @@sparql_cache
20
- end
19
+ def SparqlAdapter.get_cache
20
+ return @@sparql_cache
21
+ end
21
22
 
22
- # Instantiate the connection with the SPARQL Endpoint.
23
- # available parameters:
24
- # * :url => url: endpoint location e.g. "http://m3pe.org:8080/repositories/test-people"
25
- # * :results => one of :xml, :json, :sparql_xml
26
- # * :request_method => :get (default) or :post
27
- # * :timeout => timeout in seconds to wait for endpoint response
28
- # * :auth => [user, pass]
29
- def initialize(params = {})
30
- super()
31
- @reads = true
32
- @writes = false
33
-
34
- @url = params[:url] || ''
35
- @caching = params[:caching] || false
36
- @timeout = params[:timeout] || 50
37
- @auth = params[:auth] || nil
38
-
39
- @result_format = params[:results] || :json
40
- raise ActiveRdfError, "Result format unsupported" unless [:xml, :json, :sparql_xml].include? @result_format
41
-
42
- @engine = params[:engine]
43
- raise ActiveRdfError, "SPARQL engine unsupported" unless [:yars2, :sesame2, :joseki, :virtuoso].include? @engine
44
-
45
- @request_method = params[:request_method] || :get
46
- raise ActiveRdfError, "Request method unsupported" unless [:get,:post].include? @request_method
23
+ # Instantiate the connection with the SPARQL Endpoint.
24
+ # available parameters:
25
+ # * :url => url: endpoint location e.g. "http://m3pe.org:8080/repositories/test-people"
26
+ # * :results => one of :xml, :json, :sparql_xml
27
+ # * :request_method => :get (default) or :post
28
+ # * :timeout => timeout in seconds to wait for endpoint response
29
+ # * :auth => [user, pass]
30
+ def initialize(params = {})
31
+ super()
32
+ @reads = true
33
+ @writes = false
34
+
35
+ @url = params[:url] || ''
36
+ @caching = params[:caching] || false
37
+ @timeout = params[:timeout] || 50
38
+ @auth = params[:auth] || nil
39
+
40
+ @result_format = params[:results] || :json
41
+ raise ActiveRdfError, "Result format unsupported" unless [:xml, :json, :sparql_xml].include? @result_format
42
+
43
+ @engine = params[:engine]
44
+ raise ActiveRdfError, "SPARQL engine unsupported" unless [:yars2, :sesame2, :joseki, :virtuoso].include? @engine
45
+
46
+ @request_method = params[:request_method] || :get
47
+ raise ActiveRdfError, "Request method unsupported" unless [:get,:post].include? @request_method
47
48
  ActiveRdfLogger::log_info(self) { "Sparql adapter initialised #{inspect}" }
48
- end
49
+ end
49
50
 
50
- def size
51
- query(Query.new.select(:s,:p,:o).where(:s,:p,:o)).size
52
- end
51
+ def size
52
+ query(Query.new.select(:s,:p,:o).where(:s,:p,:o)).size
53
+ end
53
54
 
54
- # query datastore with query string (SPARQL), returns array with query results
55
- # may be called with a block
56
- def query(query, &block)
57
- qs = Query2SPARQL.translate(query)
55
+ # query datastore with query string (SPARQL), returns array with query results
56
+ # may be called with a block
57
+ def execute(query, &block)
58
+ qs = Query2SPARQL.translate(query)
58
59
  ActiveRdfLogger::log_debug(self) { "Executing sparql query #{query}" }
59
60
 
60
- if @caching
61
- result = query_cache(qs)
62
- if result.nil?
61
+ if @caching
62
+ result = query_cache(qs)
63
+ if result.nil?
63
64
  ActiveRdfLogger.log_debug(self) { "Cache miss for query #{qs}" }
64
- else
65
+ else
65
66
  ActiveRdfLogger.log_debug(self) { "Cache hit for query #{qs}" }
66
- return result
67
+ return result
68
+ end
67
69
  end
68
- end
69
70
 
70
71
  result = execute_sparql_query(qs, query.resource_class, header(query), &block)
71
- add_to_cache(qs, result) if @caching
72
- result = [] if result == "timeout"
73
- return result
74
- end
72
+ add_to_cache(qs, result) if @caching
73
+ result = [] if result == "timeout"
74
+ return result
75
+ end
75
76
 
76
- # do the real work of executing the sparql query
77
+ # do the real work of executing the sparql query
77
78
  def execute_sparql_query(qs, resource_type, header=nil, &block)
78
79
  ActiveRdfLogger::log_debug(self) { "Executing query #{qs} on url #@url" }
79
- header = header(nil) if header.nil?
80
-
81
- # querying sparql endpoint
82
- require 'timeout'
83
- response = ''
84
- begin
85
- case @request_method
86
- when :get
87
- # encoding query string in URL
88
- url = "#@url?query=#{CGI.escape(qs)}"
80
+ header = header(nil) if header.nil?
81
+
82
+ # querying sparql endpoint
83
+ require 'timeout'
84
+ response = ''
85
+ begin
86
+ case @request_method
87
+ when :get
88
+ # encoding query string in URL
89
+ url = "#@url?query=#{CGI.escape(qs)}"
89
90
  ActiveRdfLogger.log_debug(self) { "GET #{url}" }
90
- timeout(@timeout) do
91
- open(url, header) do |f|
92
- response = f.read
91
+ timeout(@timeout) do
92
+ open(url, header) do |f|
93
+ response = f.read
94
+ end
93
95
  end
94
- end
95
- when :post
96
+ when :post
96
97
  ActiveRdfLogger.log_debug(self) { "POST #@url with #{qs}" }
97
- response = Net::HTTP.post_form(URI.parse(@url),{'query'=>qs}).body
98
- end
99
- rescue Timeout::Error
100
- raise ActiveRdfError, "timeout on SPARQL endpoint"
101
- rescue OpenURI::HTTPError => e
102
- raise ActiveRdfError, "error on SPARQL endpoint, server said: \n%s:\n%s" % [e,e.io.read]
103
- rescue Errno::ECONNREFUSED
104
- raise ActiveRdfError, "connection refused on SPARQL endpoint #@url"
105
- end
106
-
107
- # we parse content depending on the result format
108
- results = case @result_format
109
- when :json
98
+ response = Net::HTTP.post_form(URI.parse(@url),{'query'=>qs}).body
99
+ end
100
+ rescue Timeout::Error
101
+ raise ActiveRdfError, "timeout on SPARQL endpoint"
102
+ rescue OpenURI::HTTPError => e
103
+ raise ActiveRdfError, "error on SPARQL endpoint, server said: \n%s:\n%s" % [e,e.io.read]
104
+ rescue Errno::ECONNREFUSED
105
+ raise ActiveRdfError, "connection refused on SPARQL endpoint #@url"
106
+ end
107
+
108
+ # we parse content depending on the result format
109
+ results = case @result_format
110
+ when :json
110
111
  parse_json(response, resource_type)
111
- when :xml, :sparql_xml
112
+ when :xml, :sparql_xml
112
113
  parse_xml(response, resource_type)
113
- end
114
+ end
114
115
 
115
- if block_given?
116
- results.each do |*clauses|
117
- yield(*clauses)
116
+ if block_given?
117
+ results.each do |*clauses|
118
+ yield(*clauses)
119
+ end
120
+ else
121
+ results
118
122
  end
119
- else
120
- results
121
123
  end
122
- end
123
124
 
124
- def close
125
- ConnectionPool.remove_data_source(self)
126
- end
125
+ def close
126
+ ConnectionPool.remove_data_source(self)
127
+ end
127
128
 
128
- private
129
+ private
129
130
  # FIXME: Cache not primed for handling res classes!
130
- def add_to_cache(query_string, result)
131
- unless result.nil? or result.empty?
132
- if result == "timeout"
133
- @@sparql_cache.store(query_string, [])
134
- else
131
+ def add_to_cache(query_string, result)
132
+ unless result.nil? or result.empty?
133
+ if result == "timeout"
134
+ @@sparql_cache.store(query_string, [])
135
+ else
135
136
  ActiveRdfLogger.log_debug(self) { "Adding to sparql cache - query: #{query_string}" }
136
- @@sparql_cache.store(query_string, result)
137
+ @@sparql_cache.store(query_string, result)
138
+ end
137
139
  end
138
140
  end
139
- end
140
141
 
141
142
 
142
- def query_cache(query_string)
143
- if @@sparql_cache.include?(query_string)
144
- return @@sparql_cache.fetch(query_string)
145
- else
146
- return nil
143
+ def query_cache(query_string)
144
+ if @@sparql_cache.include?(query_string)
145
+ return @@sparql_cache.fetch(query_string)
146
+ else
147
+ return nil
148
+ end
147
149
  end
148
- end
149
150
 
150
- # constructs correct HTTP header for selected query-result format
151
- def header(query)
152
- header = case @result_format
153
- when :json
154
- { 'accept' => 'application/sparql-results+json' }
155
- when :xml
156
- { 'accept' => 'application/rdf+xml' }
157
- when :sparql_xml
158
- { 'accept' => 'application/sparql-results+xml' }
159
- end
160
- if @auth
161
- header.merge( :http_basic_authentication => @auth )
162
- else
163
- header
151
+ # constructs correct HTTP header for selected query-result format
152
+ def header(query)
153
+ header = case @result_format
154
+ when :json
155
+ { 'accept' => 'application/sparql-results+json' }
156
+ when :xml
157
+ { 'accept' => 'application/rdf+xml' }
158
+ when :sparql_xml
159
+ { 'accept' => 'application/sparql-results+xml' }
160
+ end
161
+ if @auth
162
+ header.merge( :http_basic_authentication => @auth )
163
+ else
164
+ header
165
+ end
164
166
  end
165
- end
166
167
 
167
168
  # parse json query results into array. resource_type is the type to be used
168
169
  # for "resource" objects.
169
170
  def parse_json(s, resource_type)
170
- require 'json'
171
+ require 'json'
171
172
 
172
- parsed_object = JSON.parse(s)
173
- return [] if parsed_object.nil?
173
+ parsed_object = JSON.parse(s)
174
+ return [] if parsed_object.nil?
174
175
 
175
- results = []
176
- vars = parsed_object['head']['vars']
177
- objects = parsed_object['results']['bindings']
176
+ results = []
177
+ vars = parsed_object['head']['vars']
178
+ objects = parsed_object['results']['bindings']
178
179
 
179
- objects.each do |obj|
180
- result = []
181
- vars.each do |v|
180
+ objects.each do |obj|
181
+ result = []
182
+ vars.each do |v|
182
183
  result << create_node( obj[v]['type'], obj[v]['value'], resource_type)
184
+ end
185
+ results << result
183
186
  end
184
- results << result
185
- end
186
187
 
187
- results
188
- end
188
+ results
189
+ end
189
190
 
190
- # parse xml stream result into array
191
+ # parse xml stream result into array
191
192
  def parse_xml(s, resource_type)
192
193
  parser = SparqlResultParser.new(resource_type)
193
- REXML::Document.parse_stream(s, parser)
194
- parser.result
195
- end
194
+ REXML::Document.parse_stream(s, parser)
195
+ parser.result
196
+ end
196
197
 
197
198
  # create ruby objects for each RDF node. resource_type is the class to be used
198
199
  # for "resource" objects.
199
200
  def create_node(type, value, resource_type)
200
- case type
201
- when 'uri'
201
+ case type
202
+ when 'uri'
202
203
  resource_type.new(value)
203
- when 'bnode'
204
- BNode.new(value)
205
- when 'literal','typed-literal'
206
- value.to_s
204
+ when 'bnode'
205
+ BNode.new(value)
206
+ when 'literal','typed-literal'
207
+ value.to_s
208
+ end
207
209
  end
208
210
  end
209
-
210
-
211
-
212
- end
211
+ end
@@ -9,7 +9,7 @@ class SparqlResultParser
9
9
  @current_type = nil
10
10
  @resource_type = resource_type
11
11
  end
12
-
12
+
13
13
  def tag_start(name, attrs)
14
14
  case name
15
15
  when 'variable'
@@ -22,7 +22,7 @@ class SparqlResultParser
22
22
  @current_type = name
23
23
  end
24
24
  end
25
-
25
+
26
26
  def tag_end(name)
27
27
  if name == "result"
28
28
  @result << @current_result
@@ -31,10 +31,10 @@ class SparqlResultParser
31
31
  elsif name == "sparql"
32
32
  end
33
33
  end
34
-
34
+
35
35
  def text(text)
36
36
  if !@current_type.nil?
37
- @current_result[@index] = create_node(@current_type, text)
37
+ @current_result[@index] = create_node(@current_type, text)
38
38
  end
39
39
  end
40
40
 
@@ -49,7 +49,7 @@ class SparqlResultParser
49
49
  value.to_s
50
50
  end
51
51
  end
52
-
52
+
53
53
  def method_missing (*args)
54
54
  end
55
55
  end
@@ -10,6 +10,8 @@ require 'queryengine/query'
10
10
 
11
11
 
12
12
  class TestSparqlAdapter < Test::Unit::TestCase
13
+ include ActiveRDF
14
+
13
15
  def setup
14
16
  ConnectionPool.clear
15
17
  @adapter = ConnectionPool.add(:type => :sparql, :url => 'http://dbpedia.org/sparql', :engine => :virtuoso)
@@ -9,111 +9,113 @@ require 'cgi'
9
9
 
10
10
  # Read-only adapter to jars2
11
11
  # (experimental YARS branch for SWSE engine)
12
- class Jars2Adapter < ActiveRdfAdapter
12
+ module ActiveRDF
13
+ class Jars2Adapter < ActiveRdfAdapter
13
14
  ActiveRdfLogger::log_info "Loading Jars2 adapter", self
14
- ConnectionPool.register_adapter(:jars2, self)
15
-
16
- # initialises connection to jars2 datastore
17
- # available parameters are:
18
- # * :host (default 'm3pe.org')
19
- # * :port (default 2020)
20
- def initialize(params = {})
21
- super()
22
- @reads = true
23
- @writes = false
24
-
25
- @host = params[:host] || 'm3pe.org'
26
- @port = params[:port] || 2020
15
+ ConnectionPool.register_adapter(:jars2, self)
16
+
17
+ # initialises connection to jars2 datastore
18
+ # available parameters are:
19
+ # * :host (default 'm3pe.org')
20
+ # * :port (default 2020)
21
+ def initialize(params = {})
22
+ super()
23
+ @reads = true
24
+ @writes = false
25
+
26
+ @host = params[:host] || 'm3pe.org'
27
+ @port = params[:port] || 2020
27
28
  ActiveRdfLogger::log_info(self) { "Initializing new instance with host: #{@host} port: #{@port}" }
28
- @yars = Net::HTTP.new(@host, @port)
29
- end
29
+ @yars = Net::HTTP.new(@host, @port)
30
+ end
30
31
 
31
- def translate query
32
- Query2Jars2.translate(query)
33
- end
32
+ def translate query
33
+ Query2Jars2.translate(query)
34
+ end
34
35
 
35
- # executes query on jars2 datastore
36
- def query(query)
37
- qs = Query2Jars2.translate(query)
38
- header = { 'Accept' => 'application/rdf+n3' }
36
+ # executes query on jars2 datastore
37
+ def execute(query)
38
+ qs = Query2Jars2.translate(query)
39
+ header = { 'Accept' => 'application/rdf+n3' }
39
40
 
40
- # querying Jars2, adding 'eyal' parameter to get all variable bindings in
41
- # the result
42
- response = @yars.get("/?q=#{CGI.escape(qs)}&eyal", header)
41
+ # querying Jars2, adding 'eyal' parameter to get all variable bindings in
42
+ # the result
43
+ response = @yars.get("/?q=#{CGI.escape(qs)}&eyal", header)
43
44
 
44
45
  ActiveRdfLogger::log_debug(self) { "Jars2Adapter: query executed: #{qs}" }
45
46
 
46
- # return empty array if no content
47
- return [] if response.is_a?(Net::HTTPNoContent)
47
+ # return empty array if no content
48
+ return [] if response.is_a?(Net::HTTPNoContent)
48
49
 
49
- # return false unless HTTP OK returned
50
- return false unless response.is_a?(Net::HTTPOK)
50
+ # return false unless HTTP OK returned
51
+ return false unless response.is_a?(Net::HTTPOK)
51
52
 
52
- # parse the result
53
- results = parse_result(response.body, query)
53
+ # parse the result
54
+ results = parse_result(response.body, query)
54
55
 
55
- # remove duplicates if asked for distinct results
56
- if query.distinct?
57
- final_results = results.uniq
58
- else
59
- final_results = results
60
- end
56
+ # remove duplicates if asked for distinct results
57
+ if query.distinct?
58
+ final_results = results.uniq
59
+ else
60
+ final_results = results
61
+ end
61
62
 
62
63
  ActiveRdfLogger::log_debug_pp "Query returned %s", final_results if ActiveRdfLogger::log_level == Logger::DEBUG
63
- final_results
64
- end
64
+ final_results
65
+ end
65
66
 
66
- private
67
- Resource = /<[^>]*>/
68
- Literal = /"[^"]*"/
69
- Node = Regexp.union(Resource,Literal)
70
-
71
- # parses Jars2 results into array of ActiveRDF objects
72
- def parse_result(response, query)
73
- # Jars2 responses contain one result per line
74
- results = response.split("\n")
75
-
76
- # the first line of the response contains the variable bindings of the
77
- # results: we look at that line to figure out which column contains the
78
- # data we are looking for (which is the variables mentioned in the select
79
- # clauses of the query
80
- bindings = results[0].split(' ')
81
-
82
- # array of found answers, will be filled by iterating over the results and
83
- # only including the requested (i.e. selected) clauses
84
- answers = []
85
-
86
- # we iterate over the real results, and extract the clauses that we're
87
- # looking for (i.e. the select clauses from the query)
88
- results[1..-1].each do |result|
89
-
90
- # scan row for occurence of nodes (either resources or literals)
91
- row = result.scan(Node)
92
-
93
- # for each select clause, we find its index, and add the value at that
94
- # location in the result row to our answer
95
- row = query.select_clauses.collect do |clause|
96
- clause_index = bindings.index(clause)
67
+ private
68
+ Resource = /<[^>]*>/
69
+ Literal = /"[^"]*"/
70
+ Node = Regexp.union(Resource,Literal)
71
+
72
+ # parses Jars2 results into array of ActiveRDF objects
73
+ def parse_result(response, query)
74
+ # Jars2 responses contain one result per line
75
+ results = response.split("\n")
76
+
77
+ # the first line of the response contains the variable bindings of the
78
+ # results: we look at that line to figure out which column contains the
79
+ # data we are looking for (which is the variables mentioned in the select
80
+ # clauses of the query
81
+ bindings = results[0].split(' ')
82
+
83
+ # array of found answers, will be filled by iterating over the results and
84
+ # only including the requested (i.e. selected) clauses
85
+ answers = []
86
+
87
+ # we iterate over the real results, and extract the clauses that we're
88
+ # looking for (i.e. the select clauses from the query)
89
+ results[1..-1].each do |result|
90
+
91
+ # scan row for occurence of nodes (either resources or literals)
92
+ row = result.scan(Node)
93
+
94
+ # for each select clause, we find its index, and add the value at that
95
+ # location in the result row to our answer
96
+ row = query.select_clauses.collect do |clause|
97
+ clause_index = bindings.index(clause)
97
98
  convert_into_activerdf(row[clause_index], query.resource_class)
99
+ end
100
+ answers << row
98
101
  end
99
- answers << row
100
- end
101
102
 
102
- answers
103
- end
103
+ answers
104
+ end
104
105
 
105
106
  # converts ntriples serialisation of resource or literal into ActiveRDF object.
106
107
  #
107
108
  # resource_type is the class to be used for "resource" objects.
108
109
  def convert_into_activerdf(string, resource_type)
109
- case string
110
- when /<(.*)>/
111
- # <http://foaf/Person> is a resource
110
+ case string
111
+ when /<(.*)>/
112
+ # <http://foaf/Person> is a resource
112
113
  resource_type.new($1)
113
- when /"(.*)"/
114
- # "30" is a literal
115
- # TODO: handle datatypes
116
- String.new($1)
114
+ when /"(.*)"/
115
+ # "30" is a literal
116
+ # TODO: handle datatypes
117
+ String.new($1)
118
+ end
117
119
  end
118
120
  end
119
- end
121
+ end