activerdf_net7 1.6.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. data/CHANGELOG +79 -0
  2. data/LICENSE +504 -0
  3. data/README.rdoc +34 -0
  4. data/activerdf-jena/CHANGELOG +14 -0
  5. data/activerdf-jena/LICENSE +504 -0
  6. data/activerdf-jena/README +57 -0
  7. data/activerdf-jena/Rakefile +87 -0
  8. data/activerdf-jena/TODO +18 -0
  9. data/activerdf-jena/VERSION +1 -0
  10. data/activerdf-jena/ext/antlr-2.7.5.jar +0 -0
  11. data/activerdf-jena/ext/arq-extra.jar +0 -0
  12. data/activerdf-jena/ext/arq.jar +0 -0
  13. data/activerdf-jena/ext/aterm-java-1.6.jar +0 -0
  14. data/activerdf-jena/ext/commons-logging-1.1.jar +0 -0
  15. data/activerdf-jena/ext/concurrent.jar +0 -0
  16. data/activerdf-jena/ext/icu4j_3_4.jar +0 -0
  17. data/activerdf-jena/ext/iri.jar +0 -0
  18. data/activerdf-jena/ext/jena.jar +0 -0
  19. data/activerdf-jena/ext/jenatest.jar +0 -0
  20. data/activerdf-jena/ext/json.jar +0 -0
  21. data/activerdf-jena/ext/junit.jar +0 -0
  22. data/activerdf-jena/ext/log4j-1.2.12.jar +0 -0
  23. data/activerdf-jena/ext/lucene-core-2.0.0.jar +0 -0
  24. data/activerdf-jena/ext/ng4j.jar +0 -0
  25. data/activerdf-jena/ext/pellet.jar +0 -0
  26. data/activerdf-jena/ext/relaxngDatatype.jar +0 -0
  27. data/activerdf-jena/ext/stax-api-1.0.jar +0 -0
  28. data/activerdf-jena/ext/wstx-asl-3.0.0.jar +0 -0
  29. data/activerdf-jena/ext/xercesImpl.jar +0 -0
  30. data/activerdf-jena/ext/xml-apis.jar +0 -0
  31. data/activerdf-jena/ext/xsdlib.jar +0 -0
  32. data/activerdf-jena/lib/activerdf_jena/init.rb +26 -0
  33. data/activerdf-jena/lib/activerdf_jena/jena.rb +59 -0
  34. data/activerdf-jena/lib/activerdf_jena/jena_adapter.rb +515 -0
  35. data/activerdf-jena/lib/activerdf_jena/lucene.rb +22 -0
  36. data/activerdf-jena/lib/activerdf_jena/ng4j.rb +50 -0
  37. data/activerdf-jena/lib/activerdf_jena/ng4j_adapter.rb +379 -0
  38. data/activerdf-jena/lib/activerdf_jena/pellet.rb +25 -0
  39. data/activerdf-jena/test/bnode_org_rss.rdf +793 -0
  40. data/activerdf-jena/test/eyal-foaf.nt +39 -0
  41. data/activerdf-jena/test/fun_with_bnodes.nt +2 -0
  42. data/activerdf-jena/test/s1.n3 +18 -0
  43. data/activerdf-jena/test/test_data.nt +32 -0
  44. data/activerdf-jena/test/test_jena_adapter.rb +451 -0
  45. data/activerdf-jena/test/test_ng4j_adapter.rb +354 -0
  46. data/activerdf-rdflite/CHANGELOG +31 -0
  47. data/activerdf-rdflite/LICENSE +504 -0
  48. data/activerdf-rdflite/README +16 -0
  49. data/activerdf-rdflite/Rakefile +73 -0
  50. data/activerdf-rdflite/VERSION +1 -0
  51. data/activerdf-rdflite/lib/activerdf_rdflite/fetching.rb +34 -0
  52. data/activerdf-rdflite/lib/activerdf_rdflite/init.rb +13 -0
  53. data/activerdf-rdflite/lib/activerdf_rdflite/rdflite.rb +582 -0
  54. data/activerdf-rdflite/lib/activerdf_rdflite/suggesting.rb +87 -0
  55. data/activerdf-rdflite/test/test_bnode_data.nt +5 -0
  56. data/activerdf-rdflite/test/test_data.nt +32 -0
  57. data/activerdf-rdflite/test/test_escaped_data.nt +2 -0
  58. data/activerdf-rdflite/test/test_fetching.rb +33 -0
  59. data/activerdf-rdflite/test/test_rdflite.rb +277 -0
  60. data/activerdf-redland/CHANGELOG +12 -0
  61. data/activerdf-redland/LICENSE +504 -0
  62. data/activerdf-redland/README +9 -0
  63. data/activerdf-redland/Rakefile +72 -0
  64. data/activerdf-redland/VERSION +1 -0
  65. data/activerdf-redland/lib/activerdf_redland/init.rb +10 -0
  66. data/activerdf-redland/lib/activerdf_redland/redland.rb +362 -0
  67. data/activerdf-redland/test/test_person_data.nt +42 -0
  68. data/activerdf-redland/test/test_redland_adapter.rb +242 -0
  69. data/activerdf-sesame/CHANGELOG +6 -0
  70. data/activerdf-sesame/LICENSE +10 -0
  71. data/activerdf-sesame/LICENSE-aduna +10 -0
  72. data/activerdf-sesame/LICENSE-lgpl +504 -0
  73. data/activerdf-sesame/README +33 -0
  74. data/activerdf-sesame/Rakefile +77 -0
  75. data/activerdf-sesame/VERSION +1 -0
  76. data/activerdf-sesame/ext/commons-codec-1.3.jar +0 -0
  77. data/activerdf-sesame/ext/commons-dbcp-1.2.2.jar +0 -0
  78. data/activerdf-sesame/ext/commons-httpclient-3.1.jar +0 -0
  79. data/activerdf-sesame/ext/commons-logging-1.1.1.jar +0 -0
  80. data/activerdf-sesame/ext/commons-pool-1.3.jar +0 -0
  81. data/activerdf-sesame/ext/commons-pool-1.5.2.jar +0 -0
  82. data/activerdf-sesame/ext/junit-3.8.2.jar +0 -0
  83. data/activerdf-sesame/ext/openrdf-sesame-2.0-onejar.jar +0 -0
  84. data/activerdf-sesame/ext/openrdf-sesame-2.3-pr1-onejar.jar +0 -0
  85. data/activerdf-sesame/ext/slf4j-api-1.4.3.jar +0 -0
  86. data/activerdf-sesame/ext/slf4j-nop-1.4.3.jar +0 -0
  87. data/activerdf-sesame/ext/wrapper-sesame2.jar +0 -0
  88. data/activerdf-sesame/java/build.number +3 -0
  89. data/activerdf-sesame/java/build.xml +313 -0
  90. data/activerdf-sesame/java/javadoc/allclasses-frame.html +31 -0
  91. data/activerdf-sesame/java/javadoc/allclasses-noframe.html +31 -0
  92. data/activerdf-sesame/java/javadoc/constant-values.html +146 -0
  93. data/activerdf-sesame/java/javadoc/deprecated-list.html +146 -0
  94. data/activerdf-sesame/java/javadoc/help-doc.html +223 -0
  95. data/activerdf-sesame/java/javadoc/index-files/index-1.html +150 -0
  96. data/activerdf-sesame/java/javadoc/index-files/index-10.html +145 -0
  97. data/activerdf-sesame/java/javadoc/index-files/index-2.html +157 -0
  98. data/activerdf-sesame/java/javadoc/index-files/index-3.html +146 -0
  99. data/activerdf-sesame/java/javadoc/index-files/index-4.html +145 -0
  100. data/activerdf-sesame/java/javadoc/index-files/index-5.html +145 -0
  101. data/activerdf-sesame/java/javadoc/index-files/index-6.html +142 -0
  102. data/activerdf-sesame/java/javadoc/index-files/index-7.html +145 -0
  103. data/activerdf-sesame/java/javadoc/index-files/index-8.html +152 -0
  104. data/activerdf-sesame/java/javadoc/index-files/index-9.html +146 -0
  105. data/activerdf-sesame/java/javadoc/index.html +36 -0
  106. data/activerdf-sesame/java/javadoc/org/activerdf/wrapper/sesame2/WrapperForSesame2.html +665 -0
  107. data/activerdf-sesame/java/javadoc/org/activerdf/wrapper/sesame2/class-use/WrapperForSesame2.html +144 -0
  108. data/activerdf-sesame/java/javadoc/org/activerdf/wrapper/sesame2/package-frame.html +32 -0
  109. data/activerdf-sesame/java/javadoc/org/activerdf/wrapper/sesame2/package-summary.html +157 -0
  110. data/activerdf-sesame/java/javadoc/org/activerdf/wrapper/sesame2/package-tree.html +150 -0
  111. data/activerdf-sesame/java/javadoc/org/activerdf/wrapper/sesame2/package-use.html +144 -0
  112. data/activerdf-sesame/java/javadoc/overview-summary.html +156 -0
  113. data/activerdf-sesame/java/javadoc/overview-tree.html +152 -0
  114. data/activerdf-sesame/java/javadoc/package-list +1 -0
  115. data/activerdf-sesame/java/javadoc/resources/inherit.gif +0 -0
  116. data/activerdf-sesame/java/javadoc/stylesheet.css +29 -0
  117. data/activerdf-sesame/java/lib/commons-codec-1.3.jar +0 -0
  118. data/activerdf-sesame/java/lib/commons-dbcp-1.2.2.jar +0 -0
  119. data/activerdf-sesame/java/lib/commons-httpclient-3.1.jar +0 -0
  120. data/activerdf-sesame/java/lib/commons-logging-1.1.1.jar +0 -0
  121. data/activerdf-sesame/java/lib/commons-pool-1.3.jar +0 -0
  122. data/activerdf-sesame/java/lib/commons-pool-1.5.2.jar +0 -0
  123. data/activerdf-sesame/java/lib/junit-3.8.2.jar +0 -0
  124. data/activerdf-sesame/java/lib/openrdf-sesame-2.0-onejar.jar +0 -0
  125. data/activerdf-sesame/java/lib/openrdf-sesame-2.3-pr1-onejar.jar +0 -0
  126. data/activerdf-sesame/java/lib/slf4j-api-1.4.3.jar +0 -0
  127. data/activerdf-sesame/java/lib/slf4j-nop-1.4.3.jar +0 -0
  128. data/activerdf-sesame/java/manifest.mf +3 -0
  129. data/activerdf-sesame/java/settings.xml +135 -0
  130. data/activerdf-sesame/java/src/org/activerdf/wrapper/sesame2/WrapperForSesame2.java +145 -0
  131. data/activerdf-sesame/java/test-src/org/activerdf/wrapper/sesame2/TestWrapperForSesame2.java +41 -0
  132. data/activerdf-sesame/lib/activerdf_sesame/init.rb +11 -0
  133. data/activerdf-sesame/lib/activerdf_sesame/sesame.rb +400 -0
  134. data/activerdf-sesame/test/eyal-foaf.nt +39 -0
  135. data/activerdf-sesame/test/eyal-foaf.rdf +65 -0
  136. data/activerdf-sesame/test/test_sesame_adapter.rb +341 -0
  137. data/activerdf-sparql/CHANGELOG +35 -0
  138. data/activerdf-sparql/LICENSE +504 -0
  139. data/activerdf-sparql/README +10 -0
  140. data/activerdf-sparql/Rakefile +78 -0
  141. data/activerdf-sparql/VERSION +1 -0
  142. data/activerdf-sparql/lib/activerdf_sparql/init.rb +10 -0
  143. data/activerdf-sparql/lib/activerdf_sparql/sparql.rb +212 -0
  144. data/activerdf-sparql/lib/activerdf_sparql/sparql_result_parser.rb +55 -0
  145. data/activerdf-sparql/test/test_sparql_adapter.rb +108 -0
  146. data/activerdf-yars/LICENSE +504 -0
  147. data/activerdf-yars/README +10 -0
  148. data/activerdf-yars/Rakefile +38 -0
  149. data/activerdf-yars/lib/activerdf_yars/init.rb +10 -0
  150. data/activerdf-yars/lib/activerdf_yars/jars2.rb +119 -0
  151. data/lib/active_rdf.rb +85 -0
  152. data/lib/active_rdf/directaccess/direct_access.rb +49 -0
  153. data/lib/active_rdf/federation/active_rdf_adapter.rb +47 -0
  154. data/lib/active_rdf/federation/connection_pool.rb +156 -0
  155. data/lib/active_rdf/federation/federation_manager.rb +112 -0
  156. data/lib/active_rdf/instance_exec.rb +13 -0
  157. data/lib/active_rdf/objectmanager/bnode.rb +7 -0
  158. data/lib/active_rdf/objectmanager/literal.rb +71 -0
  159. data/lib/active_rdf/objectmanager/namespace.rb +106 -0
  160. data/lib/active_rdf/objectmanager/object_manager.rb +119 -0
  161. data/lib/active_rdf/objectmanager/ordered_set.rb +116 -0
  162. data/lib/active_rdf/objectmanager/property_list.rb +76 -0
  163. data/lib/active_rdf/objectmanager/resource.rb +609 -0
  164. data/lib/active_rdf/objectmanager/resource_like.rb +28 -0
  165. data/lib/active_rdf/queryengine/ntriples_parser.rb +90 -0
  166. data/lib/active_rdf/queryengine/query.rb +245 -0
  167. data/lib/active_rdf/queryengine/query2jars2.rb +22 -0
  168. data/lib/active_rdf/queryengine/query2sparql.rb +139 -0
  169. data/lib/active_rdf_helpers.rb +30 -0
  170. data/lib/active_rdf_log.rb +100 -0
  171. data/test/common.rb +119 -0
  172. data/test/directaccess/test_direct_access.rb +64 -0
  173. data/test/federation/test_connection_pool.rb +86 -0
  174. data/test/federation/test_federation_manager.rb +145 -0
  175. data/test/objectmanager/test_literal.rb +52 -0
  176. data/test/objectmanager/test_namespace.rb +83 -0
  177. data/test/objectmanager/test_object_manager.rb +96 -0
  178. data/test/objectmanager/test_ordered_set.rb +110 -0
  179. data/test/objectmanager/test_resource_reading.rb +150 -0
  180. data/test/objectmanager/test_resource_writing.rb +39 -0
  181. data/test/objectmanager/test_talia_syntax.rb +68 -0
  182. data/test/queryengine/my_external_resource.rb +24 -0
  183. data/test/queryengine/test_external_resource_class.rb +49 -0
  184. data/test/queryengine/test_ntriples_parser.rb +71 -0
  185. data/test/queryengine/test_query.rb +55 -0
  186. data/test/queryengine/test_query2jars2.rb +51 -0
  187. data/test/queryengine/test_query2sparql.rb +76 -0
  188. data/test/queryengine/test_query_engine.rb +52 -0
  189. data/test/test_adapters.rb +58 -0
  190. metadata +266 -0
@@ -0,0 +1,10 @@
1
+ == ActiveRDF SPARQL adapter
2
+ This is the ActiveRDF adapter to a SPARQL endpoint serving RDF.
3
+ See http://www.activerdf.org/ for more information.
4
+
5
+ == License
6
+ This adapter is distributed under the LGPL license.
7
+
8
+ == Authors
9
+ * Eyal Oren
10
+ * Sebastian Gerke
@@ -0,0 +1,78 @@
1
+ require 'meta_project'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/clean'
5
+ require 'rake/gempackagetask'
6
+ require 'rake/rdoctask'
7
+ require 'rake/contrib/xforge'
8
+ require 'rubygems'
9
+ require 'fileutils'
10
+ require '../tools/rakehelp'
11
+
12
+ $version = IO.read('VERSION').strip
13
+ $name = 'activerdf_sparql'
14
+ $project = MetaProject::Project::XForge::RubyForge.new('activerdf')
15
+ $distdir = "#$name-#$version"
16
+
17
+ # setup tests
18
+ setup_tests
19
+ setup_clean ["pkg", "lib/*.bundle", "*.gem", ".config"]
20
+
21
+ # setup rdoc
22
+ Rake::RDocTask.new do |rdoc|
23
+ rdoc.rdoc_dir = 'doc/rdoc'
24
+ rdoc.options << '--line-numbers'
25
+ rdoc.rdoc_files.add ['README', 'LICENSE', 'lib/**/*.rb', 'doc/**/*.rdoc']
26
+ end
27
+
28
+ desc "Does a full compile, test run"
29
+ task :default => [:test, :package]
30
+
31
+ setup_gem($name, $version) do |spec|
32
+ spec.summary = "ActiveRDF adapter to SPARQL endpoint"
33
+ spec.description = spec.summary
34
+ spec.author="Eyal Oren <eyal.oren@deri.org"
35
+ spec.add_dependency('gem_plugin', '>= 0.2.1')
36
+ spec.add_dependency('activerdf', '>= 1.6.4')
37
+ spec.add_dependency('json', '>= 1.1.1')
38
+ # if RUBY_PLATFORM =~ /java/
39
+ # # require json_pure which has no native c extensions when running under jruby
40
+ # spec.add_dependency('json_pure', '>= 1.1.1')
41
+ # else
42
+ # spec.add_dependency('json', '>= 1.1.1')
43
+ # end
44
+ end
45
+
46
+ task :verify_rubyforge do
47
+ raise "RUBYFORGE_USER environment variable not set!" unless ENV['RUBYFORGE_USER']
48
+ raise "RUBYFORGE_PASSWORD environment variable not set!" unless ENV['RUBYFORGE_PASSWORD']
49
+ end
50
+
51
+ desc "release #$name-#$version gem on RubyForge"
52
+ task :release => [:clean, :verify_rubyforge, :package ] do
53
+ release_files = FileList["pkg/#$distdir.gem"]
54
+
55
+ Rake::XForge::Release.new($project) do |release|
56
+ release.user_name = ENV['RUBYFORGE_USER']
57
+ release.password = ENV['RUBYFORGE_PASSWORD']
58
+ release.files = release_files.to_a
59
+ release.release_name = "#$name #$version"
60
+ release.package_name = "activerdf-sparql"
61
+ release.release_notes = ""
62
+
63
+ changes = []
64
+ File.open("CHANGELOG") do |file|
65
+ current = true
66
+
67
+ file.each do |line|
68
+ line.chomp!
69
+ if current and line =~ /^==/
70
+ current = false; next
71
+ end
72
+ break if line.empty? and not current
73
+ changes << line
74
+ end
75
+ end
76
+ release.release_changes = changes.join("\n")
77
+ end
78
+ end
@@ -0,0 +1 @@
1
+ 1.3.6
@@ -0,0 +1,10 @@
1
+ # add the directory in which this file is located to the ruby loadpath
2
+ file =
3
+ if File.symlink?(__FILE__)
4
+ File.readlink(__FILE__)
5
+ else
6
+ __FILE__
7
+ end
8
+ $: << File.dirname(File.expand_path(file))
9
+
10
+ require 'sparql'
@@ -0,0 +1,212 @@
1
+ # require 'active_rdf'
2
+ require 'queryengine/query2sparql'
3
+ require 'open-uri'
4
+ require 'cgi'
5
+ require 'rexml/document'
6
+ require "#{File.dirname(__FILE__)}/sparql_result_parser"
7
+
8
+ # SPARQL adapter
9
+ class SparqlAdapter < ActiveRdfAdapter
10
+ ActiveRdfLogger::log_info "Loading SPARQL adapter", self
11
+ ConnectionPool.register_adapter(:sparql, self)
12
+
13
+ attr_reader :engine
14
+ attr_reader :caching
15
+
16
+ @@sparql_cache = {}
17
+
18
+ def SparqlAdapter.get_cache
19
+ return @@sparql_cache
20
+ end
21
+
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
47
+ ActiveRdfLogger::log_info(self) { "Sparql adapter initialised #{inspect}" }
48
+ end
49
+
50
+ def size
51
+ query(Query.new.select(:s,:p,:o).where(:s,:p,:o)).size
52
+ end
53
+
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)
58
+ ActiveRdfLogger::log_debug(self) { "Executing sparql query #{query}" }
59
+
60
+ if @caching
61
+ result = query_cache(qs)
62
+ if result.nil?
63
+ ActiveRdfLogger.log_debug(self) { "Cache miss for query #{qs}" }
64
+ else
65
+ ActiveRdfLogger.log_debug(self) { "Cache hit for query #{qs}" }
66
+ return result
67
+ end
68
+ end
69
+
70
+ 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
75
+
76
+ # do the real work of executing the sparql query
77
+ def execute_sparql_query(qs, resource_type, header=nil, &block)
78
+ 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)}"
89
+ ActiveRdfLogger.log_debug(self) { "GET #{url}" }
90
+ timeout(@timeout) do
91
+ open(url, header) do |f|
92
+ response = f.read
93
+ end
94
+ end
95
+ when :post
96
+ 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
110
+ parse_json(response, resource_type)
111
+ when :xml, :sparql_xml
112
+ parse_xml(response, resource_type)
113
+ end
114
+
115
+ if block_given?
116
+ results.each do |*clauses|
117
+ yield(*clauses)
118
+ end
119
+ else
120
+ results
121
+ end
122
+ end
123
+
124
+ def close
125
+ ConnectionPool.remove_data_source(self)
126
+ end
127
+
128
+ private
129
+ # 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
135
+ ActiveRdfLogger.log_debug(self) { "Adding to sparql cache - query: #{query_string}" }
136
+ @@sparql_cache.store(query_string, result)
137
+ end
138
+ end
139
+ end
140
+
141
+
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
147
+ end
148
+ end
149
+
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
164
+ end
165
+ end
166
+
167
+ # parse json query results into array. resource_type is the type to be used
168
+ # for "resource" objects.
169
+ def parse_json(s, resource_type)
170
+ require 'json'
171
+
172
+ parsed_object = JSON.parse(s)
173
+ return [] if parsed_object.nil?
174
+
175
+ results = []
176
+ vars = parsed_object['head']['vars']
177
+ objects = parsed_object['results']['bindings']
178
+
179
+ objects.each do |obj|
180
+ result = []
181
+ vars.each do |v|
182
+ result << create_node( obj[v]['type'], obj[v]['value'], resource_type)
183
+ end
184
+ results << result
185
+ end
186
+
187
+ results
188
+ end
189
+
190
+ # parse xml stream result into array
191
+ def parse_xml(s, resource_type)
192
+ parser = SparqlResultParser.new(resource_type)
193
+ REXML::Document.parse_stream(s, parser)
194
+ parser.result
195
+ end
196
+
197
+ # create ruby objects for each RDF node. resource_type is the class to be used
198
+ # for "resource" objects.
199
+ def create_node(type, value, resource_type)
200
+ case type
201
+ when 'uri'
202
+ resource_type.new(value)
203
+ when 'bnode'
204
+ BNode.new(value)
205
+ when 'literal','typed-literal'
206
+ value.to_s
207
+ end
208
+ end
209
+
210
+
211
+
212
+ end
@@ -0,0 +1,55 @@
1
+ # Parser for SPARQL XML result set.
2
+ class SparqlResultParser
3
+ attr_reader :result
4
+
5
+ # The resource_type is the class that
6
+ def initialize(resource_type)
7
+ @result = []
8
+ @vars = []
9
+ @current_type = nil
10
+ @resource_type = resource_type
11
+ end
12
+
13
+ def tag_start(name, attrs)
14
+ case name
15
+ when 'variable'
16
+ @vars << attrs['name']
17
+ when 'result'
18
+ @current_result = []
19
+ when 'binding'
20
+ @index = @vars.index(attrs['name'])
21
+ when 'bnode', 'literal', 'typed-literal', 'uri'
22
+ @current_type = name
23
+ end
24
+ end
25
+
26
+ def tag_end(name)
27
+ if name == "result"
28
+ @result << @current_result
29
+ elsif name == 'bnode' || name == 'literal' || name == 'typed-literal' || name == 'uri'
30
+ @current_type = nil
31
+ elsif name == "sparql"
32
+ end
33
+ end
34
+
35
+ def text(text)
36
+ if !@current_type.nil?
37
+ @current_result[@index] = create_node(@current_type, text)
38
+ end
39
+ end
40
+
41
+ # create ruby objects for each RDF node
42
+ def create_node(type, value)
43
+ case type
44
+ when 'uri'
45
+ @resource_type.new(value)
46
+ when 'bnode'
47
+ BNode.new(value)
48
+ when 'literal','typed-literal'
49
+ value.to_s
50
+ end
51
+ end
52
+
53
+ def method_missing (*args)
54
+ end
55
+ end
@@ -0,0 +1,108 @@
1
+ # Author:: Eyal Oren
2
+ # Copyright:: (c) 2005-2006
3
+ # License:: LGPL
4
+
5
+ require "rubygems"
6
+ require 'active_rdf'
7
+ require 'test/unit'
8
+ require 'federation/federation_manager'
9
+ require 'queryengine/query'
10
+
11
+
12
+ class TestSparqlAdapter < Test::Unit::TestCase
13
+ def setup
14
+ ConnectionPool.clear
15
+ @adapter = ConnectionPool.add(:type => :sparql, :url => 'http://dbpedia.org/sparql', :engine => :virtuoso)
16
+ end
17
+
18
+ def teardown
19
+ end
20
+
21
+ def test_registration
22
+ assert_instance_of SparqlAdapter, @adapter
23
+ end
24
+
25
+ def test_language
26
+ sunset = RDFS::Resource.new("http://dbpedia.org/resource/77_Sunset_Strip")
27
+ abstract = RDFS::Resource.new("http://dbpedia.org/property/abstract")
28
+
29
+ german = Query.new.distinct(:o).where(sunset,abstract,:o).limit(1).lang(:o,'de').execute.first
30
+ english = Query.new.distinct(:o).where(sunset,abstract,:o).limit(1).lang(:o,'en').execute.first
31
+
32
+ assert english =~ /^77 Sunset Strip is the first hour-length private detective series in American television history/
33
+ assert german =~ /^77 Sunset Strip ist ein Serienklassiker aus den USA um das gleichnamige, in Los Angeles am Sunset Boulevard angesiedelte Detektivbüro/
34
+ end
35
+
36
+ def test_limit_offset
37
+ one = Query.new.select(:s).where(:s,:p,:o).limit(10).execute
38
+ assert_equal 10, one.size
39
+
40
+ one.all? do |r|
41
+ assert_instance_of RDFS::Resource, r
42
+ end
43
+
44
+ two = Query.new.select(:s).where(:s,:p,:o).limit(10).offset(1).execute
45
+ assert_equal 10, two.size
46
+ assert_equal one[1], two[0]
47
+
48
+ three = Query.new.select(:s).where(:s,:p,:o).limit(10).offset(0).execute
49
+ assert_equal one, three
50
+ end
51
+
52
+ def test_regex_filter
53
+ Namespace.register :yago, 'http://dbpedia.org/class/yago/'
54
+ Namespace.register :dbpedia, 'http://dbpedia.org/property/'
55
+ Namespace.register :dbresource, 'http://dbpedia.org/resource/'
56
+
57
+ movies = Query.new.
58
+ distinct(:title).
59
+ where(DBRESOURCE::Kill_Bill, RDFS.label, :title).
60
+ filter_regex(:title, /^Kill/).limit(10).execute
61
+
62
+ assert !movies.empty?, "regex query returns empty results"
63
+ assert movies.all? {|m| m =~ /^Kill/ }, "regex query returns wrong results"
64
+ end
65
+
66
+ def test_query_with_block
67
+ reached_block = false
68
+ Query.new.select(:s, :p).where(:s,:p,:o).limit(1).execute do |s, p|
69
+ reached_block = true
70
+ assert_equal RDFS::Resource, s.class
71
+ assert_equal RDFS::Resource, p.class
72
+ end
73
+ assert reached_block, "querying with a block does not work"
74
+
75
+ reached_block = false
76
+ Query.new.select(:s, :p).where(:s,:p,:o).limit(3).execute do |s, p|
77
+ reached_block = true
78
+ assert_equal RDFS::Resource, s.class
79
+ assert_equal RDFS::Resource, p.class
80
+ end
81
+ assert reached_block, "querying with a block does not work"
82
+
83
+ reached_block = false
84
+ Query.new.select(:s).where(:s,:p,:o).limit(3).execute do |s|
85
+ reached_block = true
86
+ assert_equal RDFS::Resource, s.class
87
+ end
88
+
89
+ assert reached_block, "querying with a block does not work"
90
+ end
91
+
92
+ def test_refuse_to_write
93
+ eyal = RDFS::Resource.new 'http://activerdf.org/test/eyal'
94
+ age = RDFS::Resource.new 'foaf:age'
95
+ test = RDFS::Resource.new 'test'
96
+
97
+ # NameError gets thown if the method is unknown
98
+ assert_raises NoMethodError do
99
+ @adapter.add(eyal, age, test)
100
+ end
101
+ end
102
+
103
+ def test_literal_conversion
104
+ # test literal conversion
105
+ label = Query.new.distinct(:label).where(:s, RDFS::label, :label).limit(1).execute(:flatten)
106
+ assert_instance_of String, label
107
+ end
108
+ end