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,22 @@
1
+ #
2
+ # Author: Karsten Huneycutt
3
+ # Copyright 2007 Valkeir Corporation
4
+ # License: LGPL
5
+ #
6
+ module LuceneARQ
7
+
8
+ class << self
9
+ bool_accessor :lucene_available
10
+ end
11
+
12
+ KEYWORD_PREDICATE = "http://jena.hpl.hp.com/ARQ/property#textMatch"
13
+
14
+ begin
15
+ include_class('com.hp.hpl.jena.query.larq.LARQ')
16
+ include_package('com.hp.hpl.jena.query.larq')
17
+ self.lucene_available = true
18
+ rescue
19
+ self.lucene_available = false
20
+ end
21
+
22
+ end
@@ -0,0 +1,50 @@
1
+ # Author:: Benjamin Heitmann
2
+ # Copyright:: (c) 2007 DERI
3
+ # License:: LGPL
4
+
5
+ require 'java'
6
+
7
+ module NG4J
8
+
9
+ module Internal
10
+ include_package('de.fuberlin.wiwiss.ng4j')
11
+ # This contains: NamedGraphSet, Quad
12
+ end
13
+
14
+ module DB
15
+
16
+ include_package('de.fuberlin.wiwiss.ng4j.db')
17
+
18
+ include_class('java.sql.DriverManager')
19
+
20
+ # this maps downcased Jena database types into drivers
21
+ DRIVER_MAP = {
22
+ 'mysql' => 'com.mysql.jdbc.Driver',
23
+ 'postgresql' => 'org.postgresql.Driver',
24
+ 'hsql' => 'org.hsqldb.jdbcDriver',
25
+ }
26
+
27
+ DRIVER_MAP.each do |name, driver|
28
+ av = "#{name}_available"
29
+ (class << self ; self ; end).send(:bool_accessor, av.to_sym)
30
+ begin
31
+ java.lang.Class.forName driver
32
+ Jena::DB.send("#{av}=", true)
33
+ rescue
34
+ Jena::DB.send("#{av}=", false)
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ module Sparql
41
+ include_package('de.fuberlin.wiwiss.ng4j.sparql')
42
+ # This contains: NamedGraphDataset
43
+ end
44
+
45
+ module Impl
46
+ include_package('de.fuberlin.wiwiss.ng4j.impl')
47
+ # This contains: NamedGraphSetImpl
48
+ end
49
+
50
+ end
@@ -0,0 +1,379 @@
1
+ # Author:: Benjamin Heitmann
2
+ # Copyright:: (c) 2007 DERI
3
+ # License:: LGPL
4
+
5
+ class NG4JAdapter < ActiveRdfAdapter
6
+
7
+ ConnectionPool.register_adapter(:ng4j, self)
8
+
9
+ class NG4JAdapterConfigurationError < StandardError
10
+ end
11
+
12
+ bool_accessor :keyword_search, :reasoning
13
+ bool_accessor :lucene_index_behind
14
+ attr_accessor :ontology_type, :model_name, :reasoner, :connection
15
+ attr_accessor :model_maker, :base_model, :model, :lucene_index
16
+ attr_accessor :root_directory
17
+ attr_accessor :graphset
18
+
19
+ # :database
20
+ # either use :url, :type, :username, AND :password (for a
21
+ # regular connection) OR :datasource AND :type (for a container
22
+ # connection), default to memory data store
23
+ # example for a derby connection:
24
+ # :database => {:url => "jdbc:hsqldb:file:/some/location/on/your/drive", :type => "hsql", :username => "sa", :password => ""}
25
+ # :model
26
+ # name of model to use, default is jena's default
27
+ # :lucene
28
+ # set to true to enable true lucene indexing of this store, default false
29
+ def initialize(params = {})
30
+ dbparams = params[:database]
31
+ self.keyword_search = params[:lucene]
32
+
33
+ # if the model name is not provided and file persistence is used, then jena just
34
+ # creates random files in the tmp dir. not good, as we need to know the model name
35
+ # to have persistence
36
+ if params[:model]
37
+ self.model_name = params[:model]
38
+ else
39
+ self.model_name = "http://deri.org/defaultgraph"
40
+ end
41
+
42
+ # do some sanity checking
43
+ if self.keyword_search? && !LuceneARQ.lucene_available?
44
+ raise NG4JAdapterConfigurationError, "Lucene requested but is not available"
45
+ end
46
+
47
+ # NG4J only supports in-memory store and database storage with HSQL, mysql and postgresql
48
+ if dbparams
49
+
50
+ # check if the jdbc driver of the requested database is available
51
+ begin
52
+ if !Jena::DB.send("#{dbparams[:type].downcase}_available?")
53
+ raise NG4JAdapterConfigurationError, "database type #{dbparams[:type]} not available"
54
+ end
55
+ rescue NameError
56
+ raise NG4JAdapterConfigurationError, "database type #{dbparams[:type]} not recognized"
57
+ end
58
+
59
+ self.connection = NG4J::DB::DriverManager.getConnection(dbparams[:url],
60
+ dbparams[:username],
61
+ dbparams[:password])
62
+
63
+ self.graphset = NG4J::DB::NamedGraphSetDB.new(self.connection)
64
+
65
+ else
66
+ self.graphset = NG4J::Impl::NamedGraphSetImpl.new()
67
+ end
68
+
69
+ self.model = self.graphset.asJenaModel(self.model_name)
70
+
71
+ self.reads = true
72
+ self.writes = true
73
+
74
+ self
75
+ end
76
+
77
+
78
+ def size
79
+ self.graphset.countQuads
80
+ end
81
+
82
+ # TODO: add quad support
83
+ def dump
84
+ it = self.model.listStatements
85
+ res = ""
86
+ while it.hasNext
87
+ res += it.nextStatement.asTriple.toString
88
+ res += " . \n"
89
+ end
90
+ res
91
+ end
92
+
93
+ def close
94
+ ConnectionPool.remove_data_source(self)
95
+ self.model.close
96
+ self.graphset.close
97
+ self.connection.close unless self.connection.nil?
98
+ end
99
+
100
+ def clear
101
+ self.model.removeAll
102
+ self.model.prepare if self.model.respond_to? :prepare
103
+ self.model.rebind if self.model.respond_to? :rebind
104
+ end
105
+
106
+ def delete(subject, predicate, object, context = nil)
107
+ self.lucene_index_behind = true
108
+
109
+ if context.nil?
110
+ context = RDFS::Resource.new(self.model_name)
111
+ end
112
+
113
+ c = (is_wildcard?(context) ? Jena::Graph::Node.create("??") : build_context(context))
114
+ s = (is_wildcard?(subject) ? Jena::Graph::Node.create("??") : build_subject(subject))
115
+ p = (is_wildcard?(predicate) ? Jena::Graph::Node.create("??") : build_predicate(predicate))
116
+ o = (is_wildcard?(object) ? Jena::Graph::Node.create("??") : build_object(object))
117
+
118
+ self.graphset.removeQuad(NG4J::Internal::Quad.new(c, s, p, o))
119
+
120
+ self.model.prepare if self.model.respond_to? :prepare
121
+ self.model.rebind if self.model.respond_to? :rebind
122
+ end
123
+
124
+ def add(subject, predicate, object, context = nil)
125
+ self.lucene_index_behind = true
126
+
127
+ if context.nil?
128
+ context = RDFS::Resource.new(self.model_name)
129
+ end
130
+
131
+ self.graphset.addQuad(NG4J::Internal::Quad.new(build_context(context), build_subject(subject),
132
+ build_predicate(predicate), build_object(object)))
133
+
134
+ self.model.prepare if self.model.respond_to? :prepare
135
+ self.model.rebind if self.model.respond_to? :rebind
136
+ end
137
+
138
+ def flush
139
+ # no-op
140
+ end
141
+
142
+
143
+ # :format
144
+ # format -- :ntriples, :n3, or :rdfxml, default :rdfxml
145
+ # :into
146
+ # :default_model for the main model, otherwise the contents of the uri get loaded
147
+ # into a context with the same uri
148
+ # TODO: add quad support
149
+ def load(uri_as_string, params = {})
150
+ format = params[:format] ? params[:format] : :rdfxml
151
+
152
+ jena_format =
153
+ case format
154
+ when :rdfxml
155
+ "RDF/XML"
156
+ when :ntriples
157
+ "N-TRIPLE"
158
+ when :n3
159
+ "N3"
160
+ end
161
+
162
+
163
+ if params[:into] == :default_model
164
+ self.model.read(uri_as_string, jena_format)
165
+ else
166
+ self.graphset.read(uri_as_string, jena_format)
167
+ end
168
+
169
+ self.lucene_index_behind = true
170
+
171
+ end
172
+
173
+ # this method gets called by the ActiveRDF query engine
174
+ # TODO: add quad support
175
+ def query(query, params = {})
176
+
177
+ if self.keyword_search? && query.keyword?
178
+
179
+ # duplicate the query
180
+ query_with_keywords = query.dup
181
+
182
+ # now duplicate the where stuff so we can fiddle with it...
183
+ # this is GROSS -- fix this if Query ever sprouts a proper
184
+ # deep copy or a where_clauses setter
185
+ query_with_keywords.instance_variable_set("@where_clauses", query.where_clauses.dup)
186
+
187
+ # now, for each of the keyword clauses, set up the search
188
+ query.keywords.each do |var, keyword|
189
+ # use this if activerdf expects the subject to come back and not the
190
+ # literal and using indexbuilderstring
191
+ #query.where("lucene_literal_#{var}".to_sym, LuceneARQ::KEYWORD_PREDICATE, keyword)
192
+ #query.where(var, "lucene_property_#{var}".to_sym, "lucene_literal_#{var}".to_sym)
193
+
194
+ # use this if activerdf expects the literal to come back, not the
195
+ # subject, or if using indexbuildersubject (which makes the subject
196
+ # come back instead of the literal
197
+ query_with_keywords.where(var, RDFS::Resource.new(LuceneARQ::KEYWORD_PREDICATE), keyword)
198
+
199
+ end
200
+
201
+ else
202
+ query_with_keywords = query
203
+ end
204
+
205
+ # jena knows about lucene, so use the query object that has the keyword
206
+ # search requests expanded.
207
+ results = query_jena(query_with_keywords)
208
+
209
+ if query.ask?
210
+ return [[true]] if results.size > 0
211
+ return [[false]]
212
+ end
213
+
214
+ if query.count?
215
+ return results.size
216
+ end
217
+
218
+ results
219
+
220
+ end
221
+
222
+ # ==========================================================================
223
+ # put private methods here to seperate api methods from the
224
+ # inner workings of the adapter
225
+ private
226
+
227
+ def build_object(object)
228
+ if object.respond_to? :uri
229
+ o = Jena::Graph::Node.createURI(object.uri)
230
+ else
231
+ #xlate to literal
232
+ if !object.kind_of? Literal
233
+ objlit = Literal.new object
234
+ else
235
+ objlit = object
236
+ end
237
+
238
+ if objlit.type
239
+ type = Jena::Datatypes::TypeMapper.getInstance.getTypeByName(objlit.type.uri)
240
+ o = Jena::Graph::Node.createLiteral(objlit.value.to_s, nil, type)
241
+ elsif objlit.language
242
+ o = Jena::Graph::Node.createLiteral(objlit.value.to_s, objlit.language, nil)
243
+ else
244
+ o = Jena::Graph::Node.createLiteral(objlit.value.to_s)
245
+ end
246
+ end
247
+ return o
248
+ end
249
+
250
+ def build_subject(subject)
251
+ # TODO: raise error if not URI
252
+ return Jena::Graph::Node.createURI(subject.uri)
253
+ end
254
+
255
+ def build_predicate(predicate)
256
+ # TODO: raise error if not URI
257
+ return Jena::Graph::Node.createURI(predicate.uri)
258
+ end
259
+
260
+ def build_context(context)
261
+ # TODO: raise error if not URI
262
+ return Jena::Graph::Node.createURI(context.uri)
263
+ end
264
+
265
+ # def build_model_object(object)
266
+ # if object.respond_to? :uri
267
+ # o = self.model.getResource(object.uri)
268
+ # else
269
+ # #xlate to literal
270
+ # if !object.kind_of? Literal
271
+ # objlit = Literal.new object
272
+ # else
273
+ # objlit = object
274
+ # end
275
+ #
276
+ # if objlit.type
277
+ # type = Jena::Datatypes::TypeMapper.getInstance.getTypeByName(objlit.type.uri)
278
+ # o = self.model.createTypedLiteral(objlit.value, type)
279
+ # elsif objlit.language
280
+ # o = self.model.createLiteral(objlit.value, objlit.language)
281
+ # else
282
+ # o = self.model.createTypedLiteral(objlit.value, nil)
283
+ # end
284
+ # end
285
+ # return o
286
+ # end
287
+ #
288
+ # def build_model_subject(subject)
289
+ # self.model.getResource(subject.uri)
290
+ # end
291
+ #
292
+ # def build_model_predicate(predicate)
293
+ # self.model.getProperty(predicate.uri)
294
+ # end
295
+
296
+ def is_wildcard?(thing)
297
+ (thing == nil) || thing.kind_of?(Symbol)
298
+ end
299
+
300
+ def query_jena(query)
301
+ query_sparql = translate(query)
302
+
303
+ # jena_query = Jena::Query::QueryFactory.create(query_sparql)
304
+ #
305
+ # puts jena_query
306
+ #
307
+
308
+ if query_sparql =~ /GRAPH/
309
+ # query contains GRAPH keyword and has to be executed against an NG4J NamedGraphDataset
310
+ qexec = Jena::Query::QueryExecutionFactory.create(query_sparql,
311
+ NG4J::Sparql::NamedGraphDataset.new(self.graphset, Jena::Graph::Node.createURI(self.model_name)))
312
+ else
313
+ # query without GRAPH keyword gets executed against the union model
314
+ qexec = Jena::Query::QueryExecutionFactory.create(query_sparql, self.model)
315
+ end
316
+
317
+ # PROBABLY A VERY EXPENSIVE OPERATION (rebuilds lucene index if ANYTHING
318
+ # changed -- this seems to be the only way, since you have to close
319
+ # the index after you build it...
320
+ if query.keyword? && self.keyword_search?
321
+ LuceneARQ::LARQ.setDefaultIndex(qexec.getContext, retrieve_lucene_index)
322
+ end
323
+
324
+ begin
325
+ results = perform_query(query, qexec)
326
+ ensure
327
+ qexec.close
328
+ end
329
+
330
+ results
331
+ end
332
+
333
+
334
+ def perform_query(query, qexec)
335
+ results = qexec.execSelect
336
+ arr_results = []
337
+
338
+ while results.hasNext
339
+ row = results.nextSolution
340
+ res_row = []
341
+ query.select_clauses.each do |kw|
342
+ thing = row.get(kw.to_s)
343
+ if thing.kind_of? Jena::Model::Resource
344
+ next if thing.isAnon
345
+ res_row << RDFS::Resource.new(thing.to_s)
346
+ elsif thing.kind_of? Jena::Model::Literal
347
+ if thing.getLanguage == "" and thing.getDatatypeURI.nil?
348
+ # plain literal
349
+ res_row << Literal.new(thing.getString)
350
+ elsif thing.getLanguage == ""
351
+ # datatyped literal
352
+ res_row << Literal.new(thing.getValue, RDFS::Resource.new(thing.getDatatypeURI))
353
+ elsif thing.getDatatypeURI.nil?
354
+ # language tagged literal
355
+ res_row << Literal.new(thing.getLexicalForm, "@" + thing.getLanguage)
356
+ else
357
+ raise ActiveRdfError, "Jena Sparql returned a strange literal"
358
+ end
359
+ else
360
+ raise ActiveRdfError, "Returned thing other than resource or literal"
361
+ end
362
+ end
363
+ arr_results << res_row
364
+ end
365
+ arr_results
366
+ end
367
+
368
+ def retrieve_lucene_index
369
+ if self.lucene_index_behind?
370
+ builder = LuceneARQ::IndexBuilderSubject.new
371
+ builder.indexStatements(self.model.listStatements)
372
+ builder.closeForWriting
373
+ self.lucene_index = builder.getIndex
374
+ self.lucene_index_behind = false
375
+ end
376
+ self.lucene_index
377
+ end
378
+
379
+ end