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,9 @@
1
+ == ActiveRDF Redland adapter
2
+ This is the ActiveRDF adapter to the Redland RDF store.
3
+ See http://www.activerdf.org/ and http://librdf.org for more information.
4
+
5
+ == License
6
+ This adapter is distributed under the LGPL license.
7
+
8
+ == Authors
9
+ * Eyal Oren
@@ -0,0 +1,72 @@
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_redland'
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 Redland RDF store"
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.requirements << 'librdf0 (Redland)'
38
+ spec.requirements << 'librdf-ruby (Redland Ruby bindings)'
39
+ end
40
+
41
+ task :verify_rubyforge do
42
+ raise "RUBYFORGE_USER environment variable not set!" unless ENV['RUBYFORGE_USER']
43
+ raise "RUBYFORGE_PASSWORD environment variable not set!" unless ENV['RUBYFORGE_PASSWORD']
44
+ end
45
+
46
+ desc "release #$name-#$version gem on RubyForge"
47
+ task :release => [ :clean, :verify_rubyforge, :package ] do
48
+ release_files = FileList["pkg/#$distdir.gem"]
49
+ Rake::XForge::Release.new($project) do |release|
50
+ release.user_name = ENV['RUBYFORGE_USER']
51
+ release.password = ENV['RUBYFORGE_PASSWORD']
52
+ release.files = release_files.to_a
53
+ release.release_name = "#$name #$version"
54
+ release.package_name = "activerdf-redland"
55
+ release.release_notes = ""
56
+
57
+ changes = []
58
+ File.open("CHANGELOG") do |file|
59
+ current = true
60
+
61
+ file.each do |line|
62
+ line.chomp!
63
+ if current and line =~ /^==/
64
+ current = false; next
65
+ end
66
+ break if line.empty? and not current
67
+ changes << line
68
+ end
69
+ end
70
+ release.release_changes = changes.join("\n")
71
+ end
72
+ end
@@ -0,0 +1 @@
1
+ 1.2.2
@@ -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 'redland.rb'
@@ -0,0 +1,362 @@
1
+ # Author:: Eyal Oren
2
+ # Copyright:: (c) 2005-2006 Eyal Oren
3
+ # License:: LGPL
4
+ # require 'active_rdf'
5
+ require 'federation/connection_pool'
6
+ require 'queryengine/query2sparql'
7
+ require 'rdf/redland'
8
+
9
+ # Adapter to Redland database
10
+ # uses SPARQL for querying
11
+ class RedlandAdapter < ActiveRdfAdapter
12
+ ActiveRdfLogger::log_info "Loading Redland adapter", self
13
+ ConnectionPool.register_adapter(:redland,self)
14
+
15
+ # instantiate connection to Redland database
16
+ # * location: Data location (:memory, :mysql, :postgresql)
17
+ # * database: Database name
18
+ # * new: Create new database
19
+ # * host: Database server address
20
+ # * password: Password
21
+ # * port: Database server port
22
+ # * reconnect: Set automatic reconnect to database server
23
+ # * user: Username
24
+ def initialize(params = {})
25
+ super()
26
+
27
+ if params[:location] and params[:location] == :postgresql
28
+ initialize_postgresql(params)
29
+ return
30
+ end
31
+
32
+ if params[:location] and params[:location] != :memory
33
+ # setup file defaults for redland database
34
+ type = 'bdb'
35
+ want_new = false # create new or use existing store
36
+ write = true
37
+ contexts = true
38
+
39
+ if params[:want_new] == true
40
+ want_new = true
41
+ end
42
+ if params[:write] == false
43
+ write = false
44
+ end
45
+ if params[:contexts] == false
46
+ contexts = false
47
+ end
48
+
49
+ if params[:location].include?('/')
50
+ path, file = File.split(params[:location])
51
+ else
52
+ path = '.'
53
+ file = params[:location]
54
+ 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
+
60
+ ActiveRdfLogger::log_info(self) { "Initializing with type: #{type} file: #{file} path: #{path}" }
61
+
62
+ begin
63
+ @store = Redland::HashStore.new(type, file, path, want_new, write, contexts)
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}"
71
+ end
72
+ end
73
+
74
+ # instantiate connection to Redland database in Postgres or MySQL
75
+ # * database: Database name
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"
112
+ # * location: location of file to load.
113
+ # * syntax: syntax of file
114
+ def load(location, syntax="ntriples")
115
+ parser = Redland::Parser.new(syntax, "", nil)
116
+ if location =~ /^http/
117
+ raise ActiveRdfError, "Redland load error for #{location}" unless (parser.parse_into_model(@model, location) == 0)
118
+ else
119
+ raise ActiveRdfError, "Redland load error for #{location}" unless (parser.parse_into_model(@model, "file:#{location}") == 0)
120
+ end
121
+ end
122
+
123
+ # yields query results (as many as requested in select clauses) executed on data source
124
+ def query(query)
125
+ qs = Query2SPARQL.translate(query)
126
+ ActiveRdfLogger::log_debug(self) { "Executing SPARQL query #{qs}" }
127
+
128
+ clauses = query.select_clauses.size
129
+ redland_query = Redland::Query.new(qs, 'sparql')
130
+ query_results = @model.query_execute(redland_query)
131
+
132
+ # return Redland's answer without parsing if ASK query
133
+ return [[query_results.get_boolean?]] if query.ask?
134
+
135
+ ActiveRdfLogger::log_debug(self) { "Found #{query_results.size} query results" }
136
+
137
+ # verify if the query has failed
138
+ if query_results.nil?
139
+ ActiveRdfLogger::log_debug "Query has failed with nil result", self
140
+ return false
141
+ end
142
+
143
+ if not query_results.is_bindings?
144
+ ActiveRdfLogger::log_debug "Query has failed without bindings", self
145
+ return false
146
+ end
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)
151
+
152
+ if block_given?
153
+ results.each do |clauses|
154
+ yield(*clauses)
155
+ end
156
+ else
157
+ results
158
+ end
159
+ end
160
+
161
+ # executes query and returns results as SPARQL JSON or XML results
162
+ # requires svn version of redland-ruby bindings
163
+ # * query: ActiveRDF Query object
164
+ # * result_format: :json or :xml
165
+ def get_query_results(query, result_format=nil)
166
+ get_sparql_query_results(Query2SPARQL.translate(query), result_format, query.resource_class)
167
+ end
168
+
169
+ # executes sparql query and returns results as SPARQL JSON or XML results
170
+ # * query: sparql query string
171
+ # * result_format: :json or :xml
172
+ # * result_type: Is the type that is used for "resource" results
173
+ def get_sparql_query_results(qs, result_type, result_format=nil)
174
+ # author: Eric Hanson
175
+
176
+ # set uri for result formatting
177
+ result_uri =
178
+ case result_format
179
+ when :json
180
+ Redland::Uri.new('http://www.w3.org/2001/sw/DataAccess/json-sparql/')
181
+ when :xml
182
+ Redland::Uri.new('http://www.w3.org/TR/2004/WD-rdf-sparql-XMLres-20041221/')
183
+ end
184
+
185
+ # query redland
186
+ redland_query = Redland::Query.new(qs, 'sparql')
187
+ query_results = @model.query_execute(redland_query)
188
+
189
+ if (result_format != :array)
190
+ # 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)
195
+ end
196
+ end
197
+
198
+ # add triple to datamodel
199
+ # * s: subject
200
+ # * p: predicate
201
+ # * o: object
202
+ def add(s, p, o, c=nil)
203
+ result = false
204
+ ActiveRdfLogger::log_debug(self) { "Adding triple #{s} #{p} #{o}" }
205
+
206
+ # verify input
207
+ if s.nil? || p.nil? || o.nil?
208
+ ActiveRdfLogger::log_debug "Cannot add triple with empty subject, exiting", self
209
+ return false
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)
224
+ end
225
+ if (result == true)
226
+ result = save if ConnectionPool.auto_flush?
227
+ end
228
+ return result
229
+ rescue Redland::RedlandError => e
230
+ ActiveRdfLogger::log_warn "Adding triple failed in Redland library: #{e}", self
231
+ return false
232
+ end
233
+ end
234
+
235
+ # deletes triple(s,p,o) from datastore
236
+ # nil parameters match anything: delete(nil,nil,nil) will delete all triples
237
+ # * s: subject
238
+ # * p: predicate
239
+ # * o: object
240
+ def delete(s,p,o,c=nil)
241
+ if ((s.class != String) && (p.class != String) && (o.class != String))
242
+ s = wrap(s) unless s.nil?
243
+ p = wrap(p) unless p.nil?
244
+ o = wrap(o) unless o.nil?
245
+
246
+ # if any parameter is nil we need to iterate over all matching triples
247
+ if (s.nil? or p.nil? or o.nil?)
248
+ @model.find(s,p,o) { |s,p,o| @model.delete(s,p,o) }
249
+ else
250
+ @model.delete(s,p,o)
251
+ end
252
+ end
253
+
254
+ @model.delete(s,p,o) == 0
255
+ end
256
+
257
+ # saves updates to the model into the redland file location
258
+ def save
259
+ Redland::librdf_model_sync(@model.model) == 0
260
+ end
261
+ alias flush save
262
+
263
+ # returns all triples in the datastore
264
+ # * type: dump syntax
265
+ def dump(type = 'ntriples')
266
+ Redland.librdf_model_to_string(@model.model, nil, type)
267
+ end
268
+
269
+ # returns size of datasources as number of triples
270
+ # warning: expensive method as it iterates through all statements
271
+ def size
272
+ # we cannot use @model.size, because redland does not allow counting of
273
+ # file-based models (@model.size raises an error if used on a file)
274
+ # instead, we just dump all triples, and count them
275
+ @model.triples.size
276
+ end
277
+
278
+ # clear all real triples of adapter
279
+ def clear
280
+ @model.find(nil, nil, nil) {|s,p,o| @model.delete(s,p,o)}
281
+ end
282
+
283
+ # close adapter and remove it from the ConnectionPool
284
+ def close
285
+ ConnectionPool.remove_data_source(self)
286
+ end
287
+
288
+ private
289
+ ################ helper methods ####################
290
+ #TODO: if block is given we should not parse all results into array first
291
+ # result_type is the type that should be used for "resource" properties.
292
+ def query_result_to_array(query_results, to_string, result_type)
293
+ results = []
294
+ number_bindings = query_results.binding_names.size
295
+
296
+ # walk through query results, and construct results array
297
+ # by looking up each result (if it is a resource) and adding it to the result-array
298
+ # for literals we only add the values
299
+
300
+ # redland results are set that needs to be iterated
301
+ while not query_results.finished?
302
+ # we collect the bindings in each row and add them to results
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
314
+ nil
315
+ else
316
+ # check blank node id
317
+ if node.blank_identifier
318
+ "_:#{node.blank_identifier}"
319
+ else
320
+ "_:"
321
+ end
322
+ end
323
+ else
324
+ # other nodes are rdfs:resources
325
+ if to_string == false
326
+ result_type.new(node.uri.to_s)
327
+ else
328
+ "<#{node.uri.to_s}>"
329
+ end
330
+ end
331
+ end
332
+ # iterate through result set
333
+ query_results.next
334
+ end
335
+
336
+ results
337
+ end
338
+
339
+ def wrap node
340
+ if(node.respond_to?(:uri))
341
+ Redland::Uri.new(node.uri.to_s)
342
+ else
343
+ Redland::Literal.new(node.to_s)
344
+ end
345
+ end
346
+
347
+ def wrapString node
348
+ node = node.to_s
349
+ if ((node[0..0] == '<') && (node[-1..-1] == '>'))
350
+ return Redland::Uri.new(node[1..-2])
351
+ elsif (node[0..1] == '_:')
352
+ if (node.length > 2)
353
+ return Redland::BNode.new(node[2..-1])
354
+ else
355
+ return Redland::BNode.new
356
+ end
357
+ else
358
+ return Redland::Literal.new(node)
359
+ end
360
+ end
361
+
362
+ end