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
@@ -2,413 +2,409 @@
2
2
  # Copyright:: (c) 2005-2006 Eyal Oren
3
3
  # License:: LGPL
4
4
 
5
- # require 'active_rdf'
6
-
7
- ActiveRdfLogger::log_info "Loading Sesame adapter", self
8
-
9
-
10
- # ----- java imports and extentsions
11
- require 'java'
12
-
13
- begin
14
- # Import the jars
15
- Dir[File.join(File.dirname(__FILE__), '..', '..', 'ext', '*.jar')].each { |jar| require jar}
16
-
17
- StringWriter = java.io.StringWriter
18
- JFile = java.io.File
19
- URLClassLoader = java.net.URLClassLoader
20
- JURL = java.net.URL
21
- JClass = java.lang.Class
22
- JObject = java.lang.Object
23
- JIOException = java.io.IOException
24
-
25
- # sesame specific classes:
26
- WrapperForSesame2 = org.activerdf.wrapper.sesame2.WrapperForSesame2
27
- QueryLanguage = org.openrdf.query.QueryLanguage
28
- NTriplesWriter = org.openrdf.rio.ntriples.NTriplesWriter
29
- RDFFormat = org.openrdf.rio.RDFFormat
30
- rescue Exception => e
31
- puts e.backtrace
32
- raise
33
- end
34
-
35
- # TODO: about this adapter
36
- class SesameAdapter < ActiveRdfAdapter
37
-
38
- # This adapter supports context operations
39
- supports_context
40
-
41
- ConnectionPool.register_adapter(:sesame,self)
42
-
43
- # Create a sesame adapter. The parameter array must contain a :backend that will identify
44
- # the backend that Sesame will use for the storage. All backends (except the HTTP repositories)
45
- # take the parameter :inferencing, which will turn on the internal inferencing engine in Sesame
46
- # (default is off).
47
- #
48
- # For compatibility, this will use the native driver if no type is given.
49
- #
50
- # = :memory
51
- # The in-memory store. No parameters.
52
- #
53
- # = :native
54
- # The "Native" store that saves data to a file. This backend finally employs locking on the database,
55
- # which means that the database can only be used from one script or program at a time.
56
- # [*location*] - Path to the data file for Sesame
57
- # [*indexes*] - Optional index for Sesame, example "spoc,posc,cosp"
58
- #
59
- # = :rdbms
60
- # The RDBMS backend store. You need to give the JDBC driver class, and obviously the JDBC driver
61
- # for your database needs to be installed.
62
- # [*driver*] - JDBC driver to use
63
- # [*url*] - URL for JDBC connection
64
- # [*user*] - Username for database connection (optional)
65
- # [*pass*] - Password for database connection (optional)
66
- #
67
- # = :http
68
- #
69
- # Connect to a repository on a sesame server through http.
70
- # [*url*] - The repository url
71
- # [*user*] - User name for HTTP authentication
72
- # [*pass*] - Password for HTTP auth
73
- def initialize(params = {})
74
- super()
75
- ActiveRdfLogger::log_info "Initializing Sesame Adapter with params #{params.to_s}", self
76
-
77
- @reads = true
78
- @writes = true
79
-
80
- # Use native type by default
81
- backend = params[:backend] || 'native'
82
-
83
- @myWrapperInstance = WrapperForSesame2.new
84
- @db = case(backend)
85
- when 'native'
86
- init_native_store(params)
87
- when 'memory'
88
- init_memory_store(params)
89
- when 'rdbms'
90
- init_rdbms_store(params)
91
- when 'http'
92
- init_http_store(params)
93
- else
94
- raise(ArgumentError, "Unknown backend type for Sesame: #{backend}")
5
+ require 'active_rdf'
6
+
7
+ module ActiveRDF
8
+ # TODO: about this adapter
9
+ class SesameAdapter < ActiveRdfAdapter
10
+ ActiveRdfLogger::log_info "Loading Sesame adapter", self
11
+
12
+ # ----- java imports and extentsions
13
+ require 'java'
14
+
15
+ begin
16
+ # Import the jars
17
+ Dir[File.join(File.dirname(__FILE__), '..', '..', 'ext', '*.jar')].each { |jar| require jar}
18
+
19
+ StringWriter = java.io.StringWriter
20
+ JFile = java.io.File
21
+ URLClassLoader = java.net.URLClassLoader
22
+ JURL = java.net.URL
23
+ JClass = java.lang.Class
24
+ JObject = java.lang.Object
25
+ JIOException = java.io.IOException
26
+
27
+ # sesame specific classes:
28
+ WrapperForSesame2 = org.activerdf.wrapper.sesame2.WrapperForSesame2
29
+ QueryLanguage = org.openrdf.query.QueryLanguage
30
+ NTriplesWriter = org.openrdf.rio.ntriples.NTriplesWriter
31
+ RDFFormat = org.openrdf.rio.RDFFormat
32
+ rescue Exception => e
33
+ puts e.backtrace
34
+ raise
95
35
  end
96
-
97
- @backend = backend
98
- @inferencing = (params[:inferencing] && (backend != 'http'))
99
-
100
- @valueFactory = if(backend == 'http')
36
+
37
+ ConnectionPool.register_adapter(:sesame,self)
38
+
39
+ # Create a sesame adapter. The parameter array must contain a :backend that will identify
40
+ # the backend that Sesame will use for the storage. All backends (except the HTTP repositories)
41
+ # take the parameter :inferencing, which will turn on the internal inferencing engine in Sesame
42
+ # (default is off).
43
+ #
44
+ # For compatibility, this will use the native driver if no type is given.
45
+ #
46
+ # = :memory
47
+ # The in-memory store. No parameters.
48
+ #
49
+ # = :native
50
+ # The "Native" store that saves data to a file. This backend finally employs locking on the database,
51
+ # which means that the database can only be used from one script or program at a time.
52
+ # [*location*] - Path to the data file for Sesame
53
+ # [*indexes*] - Optional index for Sesame, example "spoc,posc,cosp"
54
+ #
55
+ # = :rdbms
56
+ # The RDBMS backend store. You need to give the JDBC driver class, and obviously the JDBC driver
57
+ # for your database needs to be installed.
58
+ # [*driver*] - JDBC driver to use
59
+ # [*url*] - URL for JDBC connection
60
+ # [*user*] - Username for database connection (optional)
61
+ # [*pass*] - Password for database connection (optional)
62
+ #
63
+ # = :http
64
+ #
65
+ # Connect to a repository on a sesame server through http.
66
+ # [*url*] - The repository url
67
+ # [*user*] - User name for HTTP authentication
68
+ # [*pass*] - Password for HTTP auth
69
+ def initialize(params = {})
70
+ super()
71
+ ActiveRdfLogger::log_info "Initializing Sesame Adapter with params #{params.to_s}", self
72
+
73
+ @reads = true
74
+ @writes = true
75
+
76
+ # Use native type by default
77
+ backend = params[:backend] || 'native'
78
+
79
+ @myWrapperInstance = WrapperForSesame2.new
80
+ @db = case(backend)
81
+ when 'native'
82
+ init_native_store(params)
83
+ when 'memory'
84
+ init_memory_store(params)
85
+ when 'rdbms'
86
+ init_rdbms_store(params)
87
+ when 'http'
88
+ init_http_store(params)
89
+ else
90
+ raise(ArgumentError, "Unknown backend type for Sesame: #{backend}")
91
+ end
92
+
93
+ @backend = backend
94
+ @inferencing = (params[:inferencing] && (backend != 'http'))
95
+
96
+ @valueFactory = if(backend == 'http')
101
97
  @db.getRepository.getValueFactory
102
98
  else
103
99
  @db.getRepository.getSail.getValueFactory
104
100
  end
105
-
106
- end
107
101
 
108
- attr_reader :backend
109
-
110
- def inferencing?
111
- @inferencing
112
- end
102
+ end
113
103
 
114
- # returns the number of triples in the datastore (incl. possible duplicates)
115
- # * context => context (optional)
116
- def size(context = nil)
117
- @db.size(wrap_contexts(context))
118
- end
104
+ attr_reader :backend
119
105
 
120
- # deletes all triples from datastore
121
- # * context => context (optional)
122
- def clear(context = nil)
123
- @db.clear(wrap_contexts(context))
124
- end
106
+ def inferencing?
107
+ @inferencing
108
+ end
125
109
 
126
- # deletes triple(s,p,o,c) from datastore
127
- # symbol parameters match anything: delete(:s,:p,:o) will delete all triples
128
- # you can specify a context to limit deletion to that context:
129
- # delete(:s,:p,:o, 'http://context') will delete all triples with that context
130
- # * s => subject
131
- # * p => predicate
132
- # * o => object
133
- # * c => context (optional)
134
- # Nil parameters are treated as :s, :p, :o respectively.
135
- def delete(s, p, o, c=nil)
136
- # convert variables
137
- params = activerdf_to_sesame(s, p, o, c, true)
110
+ # returns the number of triples in the datastore (incl. possible duplicates)
111
+ # * context => context (optional)
112
+ def size(context = nil)
113
+ @db.size(wrap_contexts(context))
114
+ end
138
115
 
139
- begin
140
- @db.remove(params[0], params[1], params[2], wrap_contexts(c))
116
+ # deletes all triples from datastore
117
+ # * context => context (optional)
118
+ def clear(context = nil)
119
+ @db.clear(wrap_contexts(context))
120
+ end
121
+
122
+ # deletes triple(s,p,o,c) from datastore
123
+ # symbol parameters match anything: delete(:s,:p,:o) will delete all triples
124
+ # you can specify a context to limit deletion to that context:
125
+ # delete(:s,:p,:o, 'http://context') will delete all triples with that context
126
+ # * s => subject
127
+ # * p => predicate
128
+ # * o => object
129
+ # * c => context (optional)
130
+ # Nil parameters are treated as :s, :p, :o respectively.
131
+ def delete(s, p, o, c=nil)
132
+ # convert variables
133
+ params = activerdf_to_sesame(s, p, o, c, true)
134
+
135
+ begin
136
+ @db.remove(params[0], params[1], params[2], wrap_contexts(c))
137
+ true
138
+ rescue Exception => e
139
+ raise ActiveRdfError, "Sesame delete triple failed: #{e.message}"
140
+ end
141
+ @db
142
+ end
143
+
144
+ # adds triple(s,p,o,c) to datastore
145
+ # s,p must be resources, o can be primitive data or resource
146
+ # * s => subject
147
+ # * p => predicate
148
+ # * o => object
149
+ # * c => context (optional)
150
+ def add(s,p,o,c=nil)
151
+ # TODO: handle context, especially if it is null
152
+ # TODO: do we need to handle errors from the java side ?
153
+
154
+ check_input = [s,p,o]
155
+ raise ActiveRdfError, "cannot add triple with nil or blank node subject, predicate, or object" if check_input.any? {|r| r.nil? || r.is_a?(Symbol) }
156
+
157
+ params = activerdf_to_sesame(s, p, o, c)
158
+ @db.add(params[0], params[1], params[2], wrap_contexts(c))
141
159
  true
142
160
  rescue Exception => e
143
- raise ActiveRdfError, "Sesame delete triple failed: #{e.message}"
161
+ raise ActiveRdfError, "Sesame add triple failed: #{e.message}"
144
162
  end
145
- @db
146
- end
147
163
 
148
- # adds triple(s,p,o,c) to datastore
149
- # s,p must be resources, o can be primitive data or resource
150
- # * s => subject
151
- # * p => predicate
152
- # * o => object
153
- # * c => context (optional)
154
- def add(s,p,o,c=nil)
155
- # TODO: handle context, especially if it is null
156
- # TODO: do we need to handle errors from the java side ?
157
-
158
- check_input = [s,p,o]
159
- raise ActiveRdfError, "cannot add triple with nil or blank node subject, predicate, or object" if check_input.any? {|r| r.nil? || r.is_a?(Symbol) }
160
-
161
- params = activerdf_to_sesame(s, p, o, c)
162
- @db.add(params[0], params[1], params[2], wrap_contexts(c))
163
- true
164
- rescue Exception => e
165
- raise ActiveRdfError, "Sesame add triple failed: #{e.message}"
166
- end
164
+ # flushing is done automatically, because we run sesame2 in autocommit mode
165
+ def flush
166
+ true
167
+ end
167
168
 
168
- # flushing is done automatically, because we run sesame2 in autocommit mode
169
- def flush
170
- true
171
- end
169
+ # saving is done automatically, because we run sesame2 in autocommit mode
170
+ def save
171
+ true
172
+ end
172
173
 
173
- # saving is done automatically, because we run sesame2 in autocommit mode
174
- def save
175
- true
176
- end
174
+ # close the underlying sesame triple store.
175
+ # if not called there may be open iterators.
176
+ def close
177
+ @db.close
178
+ ConnectionPool.remove_data_source(self)
179
+ end
177
180
 
178
- # close the underlying sesame triple store.
179
- # if not called there may be open iterators.
180
- def close
181
- @db.close
182
- ConnectionPool.remove_data_source(self)
183
- end
181
+ # returns all triples in the datastore
182
+ def dump
183
+ # the sesame connection has an export method, which writes all explicit statements to
184
+ # a to a RDFHandler, which we supply, by constructing a NTriplesWriter, which writes to StringWriter,
185
+ # and we kindly ask that StringWriter to make a string for us. Note, you have to use stringy.to_s,
186
+ # somehow stringy.toString does not work. yes yes, those wacky jruby guys ;)
187
+ _string = StringWriter.new
188
+ sesameWriter = NTriplesWriter.new(_string)
189
+ @db.export(sesameWriter)
190
+ return _string.to_s
191
+ end
184
192
 
185
- # returns all triples in the datastore
186
- def dump
187
- # the sesame connection has an export method, which writes all explicit statements to
188
- # a to a RDFHandler, which we supply, by constructing a NTriplesWriter, which writes to StringWriter,
189
- # and we kindly ask that StringWriter to make a string for us. Note, you have to use stringy.to_s,
190
- # somehow stringy.toString does not work. yes yes, those wacky jruby guys ;)
191
- _string = StringWriter.new
192
- sesameWriter = NTriplesWriter.new(_string)
193
- @db.export(sesameWriter)
194
- return _string.to_s
195
- end
193
+ # loads triples from file in ntriples format
194
+ # * file => file to load
195
+ # * syntax => syntax of file to load. The syntax can be: n3, ntriples, rdfxml, trig, trix, turtle
196
+ # * context => context (optional)
197
+ def load(file, syntax="ntriples", context=nil)
198
+ # rdf syntax type
199
+ case syntax
200
+ when 'n3'
201
+ syntax_type = RDFFormat::N3
202
+ when 'ntriples'
203
+ syntax_type = RDFFormat::NTRIPLES
204
+ when 'rdfxml'
205
+ syntax_type = RDFFormat::RDFXML
206
+ when 'trig'
207
+ syntax_type = RDFFormat::TRIG
208
+ when 'trix'
209
+ syntax_type = RDFFormat::TRIX
210
+ when 'turtle'
211
+ syntax_type = RDFFormat::TURTLE
212
+ else
213
+ raise ActiveRdfError, "Sesame load file failed: syntax not valid."
214
+ end
196
215
 
197
- # loads triples from file in ntriples format
198
- # * file => file to load
199
- # * syntax => syntax of file to load. The syntax can be: n3, ntriples, rdfxml, trig, trix, turtle
200
- # * context => context (optional)
201
- def load(file, syntax="ntriples", context=nil)
202
- # rdf syntax type
203
- case syntax
204
- when 'n3'
205
- syntax_type = RDFFormat::N3
206
- when 'ntriples'
207
- syntax_type = RDFFormat::NTRIPLES
208
- when 'rdfxml'
209
- syntax_type = RDFFormat::RDFXML
210
- when 'trig'
211
- syntax_type = RDFFormat::TRIG
212
- when 'trix'
213
- syntax_type = RDFFormat::TRIX
214
- when 'turtle'
215
- syntax_type = RDFFormat::TURTLE
216
- else
217
- raise ActiveRdfError, "Sesame load file failed: syntax not valid."
216
+ begin
217
+ @myWrapperInstance.load(file, "", syntax_type, wrap_contexts(context))
218
+ rescue Exception => e
219
+ raise ActiveRdfError, "Sesame load file failed: #{e.message}\n#{e.backtrace}"
220
+ end
218
221
  end
219
222
 
220
- begin
221
- @myWrapperInstance.load(file, "", syntax_type, wrap_contexts(context))
222
- rescue Exception => e
223
- raise ActiveRdfError, "Sesame load file failed: #{e.message}\n#{e.backtrace}"
224
- end
225
- end
223
+ # executes ActiveRDF query on the sesame triple store associated with this adapter
224
+ def execute(query)
226
225
 
227
- # executes ActiveRDF query on the sesame triple store associated with this adapter
228
- # * query => Query object
229
- def query(query)
226
+ # we want to put the results in here
227
+ results = []
230
228
 
231
- # we want to put the results in here
232
- results = []
229
+ # translate the query object into a SPARQL query string
230
+ qs = Query2SPARQL.translate(query)
233
231
 
234
- # translate the query object into a SPARQL query string
235
- qs = Query2SPARQL.translate(query)
232
+ begin
233
+ # evaluate the query on the sesame triple store
234
+ # TODO: if we want to get inferred statements back we have to say so, as third boolean parameter
235
+ tuplequeryresult = @db.prepareTupleQuery(QueryLanguage::SPARQL, qs).evaluate
236
+ rescue Exception => e
237
+ ActiveRdfLogger.log_error(self) { "Error evaluating query (#{e.message}): #{qs}" }
238
+ raise
239
+ end
236
240
 
237
- begin
238
- # evaluate the query on the sesame triple store
239
- # TODO: if we want to get inferred statements back we have to say so, as third boolean parameter
240
- tuplequeryresult = @db.prepareTupleQuery(QueryLanguage::SPARQL, qs).evaluate
241
- rescue Exception => e
242
- ActiveRdfLogger.log_error(self) { "Error evaluating query (#{e.message}): #{qs}" }
243
- raise
244
- end
241
+ # what are the variables of the query ?
242
+ variables = tuplequeryresult.getBindingNames
243
+ size_of_variables = variables.size
244
+
245
+ # the following is plainly ugly. the reason is that JRuby currently does not support
246
+ # using iterators in the ruby way: with "each". it is possible to define "each" for java.util.Iterator
247
+ # using JavaUtilities.extend_proxy but that fails in strange ways. this is ugly but works.
248
+
249
+ # TODO: null handling, if a value is null...
250
+
251
+ # if there only was one variable, then the results array should look like this:
252
+ # results = [ [first Value For The Variable], [second Value], ...]
253
+ if size_of_variables == 1 then
254
+ # the counter keeps track of the number of values, so we can insert them into the results at the right position
255
+ counter = 0
256
+ while tuplequeryresult.hasNext
257
+ solution = tuplequeryresult.next
258
+
259
+ temparray = []
260
+ # get the value associated with a variable in this specific solution
261
+ temparray[0] = convertSesame2ActiveRDF(solution.getValue(variables[0]), query.resource_class)
262
+ results[counter] = temparray
263
+ counter = counter + 1
264
+ end
265
+ else
266
+ # if there is more then one variable the results array looks like this:
267
+ # results = [ [Value From First Solution For First Variable, Value From First Solution For Second Variable, ...],
268
+ # [Value From Second Solution For First Variable, Value From Second Solution for Second Variable, ...], ...]
269
+ counter = 0
270
+ while tuplequeryresult.hasNext
271
+ solution = tuplequeryresult.next
272
+
273
+ temparray = []
274
+ for n in 1..size_of_variables
275
+ value = convertSesame2ActiveRDF(solution.getValue(variables[n-1]), query.resource_class)
276
+ temparray[n-1] = value
277
+ end
278
+ results[counter] = temparray
279
+ counter = counter + 1
280
+ end
281
+ end
245
282
 
246
- # what are the variables of the query ?
247
- variables = tuplequeryresult.getBindingNames
248
- size_of_variables = variables.size
249
-
250
- # the following is plainly ugly. the reason is that JRuby currently does not support
251
- # using iterators in the ruby way: with "each". it is possible to define "each" for java.util.Iterator
252
- # using JavaUtilities.extend_proxy but that fails in strange ways. this is ugly but works.
253
-
254
- # TODO: null handling, if a value is null...
255
-
256
- # if there only was one variable, then the results array should look like this:
257
- # results = [ [first Value For The Variable], [second Value], ...]
258
- if size_of_variables == 1 then
259
- # the counter keeps track of the number of values, so we can insert them into the results at the right position
260
- counter = 0
261
- while tuplequeryresult.hasNext
262
- solution = tuplequeryresult.next
263
-
264
- temparray = []
265
- # get the value associated with a variable in this specific solution
266
- temparray[0] = convertSesame2ActiveRDF(solution.getValue(variables[0]), query.resource_class)
267
- results[counter] = temparray
268
- counter = counter + 1
269
- end
270
- else
271
- # if there is more then one variable the results array looks like this:
272
- # results = [ [Value From First Solution For First Variable, Value From First Solution For Second Variable, ...],
273
- # [Value From Second Solution For First Variable, Value From Second Solution for Second Variable, ...], ...]
274
- counter = 0
275
- while tuplequeryresult.hasNext
276
- solution = tuplequeryresult.next
277
-
278
- temparray = []
279
- for n in 1..size_of_variables
280
- value = convertSesame2ActiveRDF(solution.getValue(variables[n-1]), query.resource_class)
281
- temparray[n-1] = value
282
- end
283
- results[counter] = temparray
284
- counter = counter + 1
285
- end
283
+ return results
286
284
  end
287
285
 
288
- return results
289
- end
286
+ private
290
287
 
291
- private
288
+ # Init a native Sesame backend
289
+ def init_native_store(params)
290
+ # if no inferencing is specified, we don't activate sesame2 rdfs inferencing
291
+ sesame_inferencing = params[:inferencing] || false
292
+ ActiveRdfLogger.log_debug(self) { "Creating Sesame Native Adapter (location: #{params[:location]}, indexes: #{params[:indexes]}, inferencing: #{sesame_inferencing}" }
293
+ sesame_location = JFile.new(params[:location]) if(params[:location])
292
294
 
293
- # Init a native Sesame backend
294
- def init_native_store(params)
295
- # if no inferencing is specified, we don't activate sesame2 rdfs inferencing
296
- sesame_inferencing = params[:inferencing] || false
297
- ActiveRdfLogger.log_debug(self) { "Creating Sesame Native Adapter (location: #{params[:location]}, indexes: #{params[:indexes]}, inferencing: #{sesame_inferencing}" }
298
- sesame_location = JFile.new(params[:location]) if(params[:location])
299
-
300
- @myWrapperInstance.initWithNative(sesame_location, params[:indexes], sesame_inferencing)
301
- end
302
-
303
- # Init a in-memory Sesame backend
304
- def init_memory_store(params)
305
- # if no inferencing is specified, we don't activate sesame2 rdfs inferencing
306
- sesame_inferencing = params[:inferencing] || false
307
- ActiveRdfLogger.log_debug(self) { "Creating Sesame Memory Adapter (inferencing: #{sesame_inferencing}" }
308
-
309
- @myWrapperInstance.initWithMemory(sesame_inferencing)
310
- end
295
+ @myWrapperInstance.initWithNative(sesame_location, params[:indexes], sesame_inferencing)
296
+ end
311
297
 
312
- # Init with an RDBMS backend
313
- def init_rdbms_store(params)
314
- sesame_inferencing = params[:inferencing] || false
315
- ActiveRdfLogger.log_debug(self) { "Creating Sesame RDBMS Adapter (driver: #{params[:driver]}, url: #{params[:url]}, user: #{params[:user]}, pass: #{params[:pass]}, inferencing: #{sesame_inferencing}" }
316
-
317
- @myWrapperInstance.initWithRDBMS(params[:driver], params[:url], params[:user], params[:pass], sesame_inferencing)
318
- end
319
-
320
- # Init the HTTP store
321
- def init_http_store(params)
322
- ActiveRdfLogger.log_debug(self) { "Creating Sesame HTTP Adapter (url: #{params[:url]}, user: #{params[:user]}, pass: #{params[:pass]} (inferencing settings are always ignored)" }
323
-
324
- wrap = @myWrapperInstance.initWithHttp(params[:url], params[:user], params[:pass])
325
- @writes = wrap.getRepository.isWritable
326
- wrap
327
- end
328
-
329
- # check if testee is a java subclass of reference
330
- def jInstanceOf(testee, reference)
331
- # for Java::JavaClass for a <=> b the comparison operator returns: -1 if a is subclass of b,
332
- # 0 if a.jclass = b.jclass, +1 in any other case.
333
- isSubclass = (testee <=> reference)
334
- if isSubclass == -1 or isSubclass == 0
335
- return true
336
- else
337
- return false
298
+ # Init a in-memory Sesame backend
299
+ def init_memory_store(params)
300
+ # if no inferencing is specified, we don't activate sesame2 rdfs inferencing
301
+ sesame_inferencing = params[:inferencing] || false
302
+ ActiveRdfLogger.log_debug(self) { "Creating Sesame Memory Adapter (inferencing: #{sesame_inferencing}" }
303
+
304
+ @myWrapperInstance.initWithMemory(sesame_inferencing)
338
305
  end
339
- end
340
306
 
341
- # takes a part of a sesame statement, and converts it to a RDFS::Resource if it is a URI,
342
- # or to a String if it is a Literal. The assumption currently, is that we will only get stuff out of sesame,
343
- # which we put in there ourselves, and currently we only put URIs or Literals there.
344
- #
345
- # result_type is the class that will be used for "resource" objects.
346
- def convertSesame2ActiveRDF(input, result_type)
347
- jclassURI = Java::JavaClass.for_name("org.openrdf.model.URI")
348
- jclassLiteral = Java::JavaClass.for_name("org.openrdf.model.Literal")
349
- jclassBNode = Java::JavaClass.for_name('org.openrdf.model.BNode')
350
-
351
- if jInstanceOf(input.java_class, jclassURI)
352
- result_type.new(input.toString)
353
- elsif jInstanceOf(input.java_class, jclassLiteral)
354
- # The string is wrapped in quotationn marks. However, there may be a language
355
- # indetifier outside the quotation marks, e.g. "The label"@en
356
- # We try to unwrap this correctly. For now we assume that there may be
357
- # no quotation marks inside the string
358
- input.toString.gsub('"', '')
359
- elsif jInstanceOf(input.java_class, jclassBNode)
360
- RDFS::BNode.new(input.toString)
361
- else
362
- raise ActiveRdfError, "the Sesame Adapter tried to return something which is neither a URI nor a Literal, but is instead a #{input.java_class.name}"
363
- end
364
- end
307
+ # Init with an RDBMS backend
308
+ def init_rdbms_store(params)
309
+ sesame_inferencing = params[:inferencing] || false
310
+ ActiveRdfLogger.log_debug(self) { "Creating Sesame RDBMS Adapter (driver: #{params[:driver]}, url: #{params[:url]}, user: #{params[:user]}, pass: #{params[:pass]}, inferencing: #{sesame_inferencing}" }
365
311
 
366
- # converts spoc input into sesame objects (RDFS::Resource into
367
- # valueFactory.createURI etc.)
368
- def activerdf_to_sesame(s, p, o, c, use_nil = false)
369
- params = []
312
+ @myWrapperInstance.initWithRDBMS(params[:driver], params[:url], params[:user], params[:pass], sesame_inferencing)
313
+ end
370
314
 
371
- # construct sesame parameters from s,p,o,c
372
- [s,p,o].each { |item|
373
- params << wrap(item, use_nil)
374
- }
315
+ # Init the HTTP store
316
+ def init_http_store(params)
317
+ ActiveRdfLogger.log_debug(self) { "Creating Sesame HTTP Adapter (url: #{params[:url]}, user: #{params[:user]}, pass: #{params[:pass]} (inferencing settings are always ignored)" }
375
318
 
376
- # wrap Context
377
- params << wrap_contexts(c) unless (c.nil?)
319
+ wrap = @myWrapperInstance.initWithHttp(params[:url], params[:user], params[:pass])
320
+ @writes = wrap.getRepository.isWritable
321
+ wrap
322
+ end
378
323
 
379
- params
380
- end
324
+ # check if testee is a java subclass of reference
325
+ def jInstanceOf(testee, reference)
326
+ # for Java::JavaClass for a <=> b the comparison operator returns: -1 if a is subclass of b,
327
+ # 0 if a.jclass = b.jclass, +1 in any other case.
328
+ isSubclass = (testee <=> reference)
329
+ if isSubclass == -1 or isSubclass == 0
330
+ return true
331
+ else
332
+ return false
333
+ end
334
+ end
381
335
 
382
- # converts item into sesame object (RDFS::Resource into
383
- # valueFactory.createURI etc.). You can opt to preserve the
384
- # nil values, otherwise they'll be transformed
385
- def wrap(item, use_nil = false)
386
- result =
387
- if(item.respond_to?(:uri))
388
- if (item.uri.to_s[0..4].match(/http:/).nil?)
389
- @valueFactory.createLiteral(item.uri.to_s)
336
+ # takes a part of a sesame statement, and converts it to a RDFS::Resource if it is a URI,
337
+ # or to a String if it is a Literal. The assumption currently, is that we will only get stuff out of sesame,
338
+ # which we put in there ourselves, and currently we only put URIs or Literals there.
339
+ #
340
+ # result_type is the class that will be used for "resource" objects.
341
+ def convertSesame2ActiveRDF(input, result_type)
342
+ jclassURI = Java::JavaClass.for_name("org.openrdf.model.URI")
343
+ jclassLiteral = Java::JavaClass.for_name("org.openrdf.model.Literal")
344
+ jclassBNode = Java::JavaClass.for_name('org.openrdf.model.BNode')
345
+
346
+ if jInstanceOf(input.java_class, jclassURI)
347
+ result_type.new(input.toString)
348
+ elsif jInstanceOf(input.java_class, jclassLiteral)
349
+ # The string is wrapped in quotationn marks. However, there may be a language
350
+ # indetifier outside the quotation marks, e.g. "The label"@en
351
+ # We try to unwrap this correctly. For now we assume that there may be
352
+ # no quotation marks inside the string
353
+ input.toString.gsub('"', '')
354
+ elsif jInstanceOf(input.java_class, jclassBNode)
355
+ RDFS::BNode.new(input.toString)
390
356
  else
391
- @valueFactory.createURI(item.uri.to_s)
357
+ raise ActiveRdfError, "the Sesame Adapter tried to return something which is neither a URI nor a Literal, but is instead a #{input.java_class.name}"
392
358
  end
393
- else
394
- case item
395
- when Symbol
396
- @valueFactory.createLiteral('')
397
- when NilClass
398
- use_nil ? nil : @valueFactory.createLiteral('')
359
+ end
360
+
361
+ # converts spoc input into sesame objects (RDFS::Resource into
362
+ # valueFactory.createURI etc.)
363
+ def activerdf_to_sesame(s, p, o, c, use_nil = false)
364
+ params = []
365
+
366
+ # construct sesame parameters from s,p,o,c
367
+ [s,p,o].each { |item|
368
+ params << wrap(item, use_nil)
369
+ }
370
+
371
+ # wrap Context
372
+ params << wrap_contexts(c) unless (c.nil?)
373
+
374
+ params
375
+ end
376
+
377
+ # converts item into sesame object (RDFS::Resource into
378
+ # valueFactory.createURI etc.). You can opt to preserve the
379
+ # nil values, otherwise they'll be transformed
380
+ def wrap(item, use_nil = false)
381
+ result =
382
+ if(item.respond_to?(:uri))
383
+ if (item.uri.to_s[0..4].match(/http:/).nil?)
384
+ @valueFactory.createLiteral(item.uri.to_s)
385
+ else
386
+ @valueFactory.createURI(item.uri.to_s)
387
+ end
399
388
  else
400
- @valueFactory.createLiteral(item.to_s)
389
+ case item
390
+ when Symbol
391
+ @valueFactory.createLiteral('')
392
+ when NilClass
393
+ use_nil ? nil : @valueFactory.createLiteral('')
394
+ else
395
+ @valueFactory.createLiteral(item.to_s)
396
+ end
401
397
  end
398
+ return result
402
399
  end
403
- return result
404
- end
405
400
 
406
- def wrap_contexts(*contexts)
407
- contexts.compact!
408
- contexts.collect! do |context|
409
- raise ActiveRdfError, "context must be a Resource" unless(context.respond_to?(:uri))
410
- @valueFactory.createURI(context.uri)
401
+ def wrap_contexts(*contexts)
402
+ contexts.compact!
403
+ contexts.collect! do |context|
404
+ raise ActiveRdfError, "context must be a Resource" unless(context.respond_to?(:uri))
405
+ @valueFactory.createURI(context.uri)
406
+ end
407
+ contexts.to_java(org.openrdf.model.Resource)
411
408
  end
412
- contexts.to_java(org.openrdf.model.Resource)
413
409
  end
414
- end
410
+ end