jena 1.0.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +29 -0
  5. data/LICENSE +13 -0
  6. data/README.md +78 -0
  7. data/Rakefile +37 -0
  8. data/bin/update_jena +37 -0
  9. data/javalib/collection-0.7.jar +0 -0
  10. data/javalib/commons-cli-1.4.jar +0 -0
  11. data/javalib/commons-codec-1.11.jar +0 -0
  12. data/javalib/commons-csv-1.5.jar +0 -0
  13. data/javalib/commons-io-2.6.jar +0 -0
  14. data/javalib/commons-lang3-3.4.jar +0 -0
  15. data/javalib/httpclient-4.5.3.jar +0 -0
  16. data/javalib/httpclient-cache-4.5.3.jar +0 -0
  17. data/javalib/httpcore-4.4.6.jar +0 -0
  18. data/javalib/jackson-annotations-2.9.0.jar +0 -0
  19. data/javalib/jackson-core-2.9.0.jar +0 -0
  20. data/javalib/jackson-databind-2.9.0.jar +0 -0
  21. data/javalib/jcl-over-slf4j-1.7.25.jar +0 -0
  22. data/javalib/jena-arq-3.6.0.jar +0 -0
  23. data/javalib/jena-base-3.6.0.jar +0 -0
  24. data/javalib/jena-cmds-3.6.0.jar +0 -0
  25. data/javalib/jena-core-3.6.0.jar +0 -0
  26. data/javalib/jena-dboe-base-3.6.0.jar +0 -0
  27. data/javalib/jena-dboe-index-3.6.0.jar +0 -0
  28. data/javalib/jena-dboe-trans-data-3.6.0.jar +0 -0
  29. data/javalib/jena-dboe-transaction-3.6.0.jar +0 -0
  30. data/javalib/jena-iri-3.6.0.jar +0 -0
  31. data/javalib/jena-rdfconnection-3.6.0.jar +0 -0
  32. data/javalib/jena-shaded-guava-3.6.0.jar +0 -0
  33. data/javalib/jena-tdb-3.6.0.jar +0 -0
  34. data/javalib/jena-tdb2-3.6.0.jar +0 -0
  35. data/javalib/jsonld-java-0.11.1.jar +0 -0
  36. data/javalib/libthrift-0.10.0.jar +0 -0
  37. data/javalib/log4j-1.2.17.jar +0 -0
  38. data/javalib/slf4j-api-1.7.25.jar +0 -0
  39. data/javalib/slf4j-log4j12-1.7.25.jar +0 -0
  40. data/javalib/xercesImpl-2.11.0.jar +0 -0
  41. data/javalib/xml-apis-1.4.01.jar +0 -0
  42. data/jena-jruby.gemspec +30 -0
  43. data/lib/jena_jruby/jars.rb +33 -0
  44. data/lib/jena_jruby/namespace.rb +34 -0
  45. data/lib/jena_jruby/node_utils.rb +109 -0
  46. data/lib/jena_jruby/query_utils.rb +205 -0
  47. data/lib/jena_jruby/statement_utils.rb +11 -0
  48. data/lib/jena_jruby/utils.rb +27 -0
  49. data/lib/jena_jruby/version.rb +4 -0
  50. data/lib/jena_jruby.rb +107 -0
  51. data/test/node_utils_test.rb +80 -0
  52. data/test/query_utils_test.rb +125 -0
  53. metadata +104 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f91dbc086c7e3e00243ac6cdfb40da948d9cdfab151c8346b8737644f433973c
4
+ data.tar.gz: 2f68d1a78ddbee9db1c7e30c74d0db0a0c61683e0aa6d8f9a82d1b222e5134b8
5
+ SHA512:
6
+ metadata.gz: ed6a6deb306e5d48d85ab343d2673c31e3c74bcd48d1f6d3f8998d7cf7f0a1dfcaf6aa1e2bbf6fc65231816bcfdf8fd98e0b190b19a8989fb701ebbe3d47131f
7
+ data.tar.gz: d8965607bcbbf6a7e1af44cd569b1f7e37fdf7b1cbf9467327829798151f6cd9a60be35d7542bee322f374ca95ee3760a5fc9b0254697883fd1dac2811dc8031
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ .*
2
+ *~
3
+ *.gem
4
+ *.xml
5
+ javalib
6
+ pkg
7
+ rdoc
8
+ pkg
9
+ temp
10
+ downloads
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gem "rake", group: :test
3
+ gem "minitest", group: :test
4
+ gem "pry", group: :test
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ coderay (1.1.2)
5
+ ffi (1.9.23-java)
6
+ method_source (0.9.0)
7
+ minitest (5.11.3)
8
+ pry (0.11.3)
9
+ coderay (~> 1.1.0)
10
+ method_source (~> 0.9.0)
11
+ pry (0.11.3-java)
12
+ coderay (~> 1.1.0)
13
+ method_source (~> 0.9.0)
14
+ spoon (~> 0.0)
15
+ rake (12.3.1)
16
+ spoon (0.0.6)
17
+ ffi
18
+
19
+ PLATFORMS
20
+ java
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ minitest
25
+ pry
26
+ rake
27
+
28
+ BUNDLED WITH
29
+ 1.16.1
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2011 Epimorphics Ltd
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # Gem wrapper for Apache Jena
2
+
3
+ This Gem provides convenience access to the [Apache Jena](http://jena.apache.org)
4
+ RDF platform. Jena is a widely used Java platform for parsing, storing, manipulating,
5
+ querying and publishing semantic web data and linked data. This gem provides a single
6
+ dependency so that JRuby programs can access all of the capabilities of Jena, including TDB,
7
+ conveniently from JRuby.
8
+
9
+ This gem provides the following benefits:
10
+
11
+ * a single dependency which includes all of the `.jar` files for the latest release
12
+ of Jena (currently 2.13.0) including TDB.
13
+
14
+ * import of some of the key classes used by Jena-based programs into a `Jena::`
15
+ module, to make code more compact
16
+
17
+ * some additional utility and convenience methods
18
+
19
+ ## Using the gem
20
+
21
+ Once installed, just add to your code:
22
+
23
+ require 'jena_jruby'
24
+
25
+ or add a suitable dependency to your bundle spec.
26
+
27
+ ## Access to Jena from Java
28
+
29
+ Create a model:
30
+
31
+ m = Jena::Core::ModelFactory.create_default_model
32
+
33
+ (note that JRuby automagically converts Java CamelCase names to Ruby lower_case names)
34
+
35
+ Add a statement:
36
+
37
+ jruby-1.6.5 :003 > m = Jena::Core::ModelFactory.create_default_model
38
+ => #<Java::ComHpHplJenaRdfModelImpl::ModelCom:0x135a4815>
39
+ jruby-1.6.5 :004 > s = Jena::Core::ResourceFactory.create_resource "http://example.org/s"
40
+ => #<Java::ComHpHplJenaRdfModelImpl::ResourceImpl:0x7b0f62cb>
41
+ jruby-1.6.5 :005 > o = Jena::Core::ResourceFactory.create_resource "http://example.org/o"
42
+ => #<Java::ComHpHplJenaRdfModelImpl::ResourceImpl:0x4879c985>
43
+ jruby-1.6.5 :006 > p = Jena::Core::ResourceFactory.createProperty "http://example.org/p"
44
+ => #<Java::ComHpHplJenaRdfModelImpl::PropertyImpl:0x3f7cd159>
45
+ jruby-1.6.5 :007 > m.add s,p,o
46
+ => #<Java::ComHpHplJenaRdfModelImpl::ModelCom:0x135a4815>
47
+ jruby-1.6.5 :009 > m.toString
48
+ => "<ModelCom {http://example.org/s @http://example.org/p http://example.org/o} | >"
49
+
50
+ ## Building and installing the gem
51
+
52
+ Since jena now includes TDB modules, those modules no longer need to be built locally.
53
+
54
+ To build the gem itself:
55
+
56
+ $ rake build
57
+ jena-jruby 0.8.1 built to pkg/jena-jruby-0.8.1-java.gem.
58
+
59
+ or
60
+
61
+ $ gem build jena-jruby.gemspec
62
+ Successfully built RubyGem
63
+ Name: jena-jruby
64
+ Version: 0.8.1
65
+ File: jena-jruby-0.8.1-java.gem
66
+
67
+ To run the tests:
68
+
69
+ $ bundle exec rake test
70
+ Run options: --seed 45483
71
+
72
+ # Running:
73
+
74
+ ................
75
+
76
+ Finished in 0.351000s, 45.5840 runs/s, 142.4501 assertions/s.
77
+
78
+ 16 runs, 50 assertions, 0 failures, 0 errors, 0 skips
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ ####################################################
7
+ # Testing
8
+ ####################################################
9
+
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << "test" << "."
12
+ t.test_files = FileList['test/*test.rb']
13
+ # t.verbose = true
14
+ end
15
+
16
+ namespace :test do
17
+
18
+ desc 'Measures test coverage'
19
+ task :coverage do
20
+ rm_f "profiling"
21
+ # rcov = "rcov --rails --aggregate coverage.data --text-summary -Ilib"
22
+ rcov = "rcov -I ./test -i '^lib/' -x 'rvm|\.js$|\.html$' --profile --sort coverage"
23
+ system("#{rcov} test/*_test.rb")
24
+ system("gnome-open profiling/index.html")
25
+ end
26
+
27
+ end
28
+
29
+ require 'rdoc/task'
30
+ Rake::RDocTask.new do |rdoc|
31
+ version = File.exist?('lib/rdf_renderer/VERSION') ? File.read('lib/rdf_renderer/VERSION') : ""
32
+
33
+ rdoc.rdoc_dir = 'rdoc'
34
+ rdoc.title = "RDF Renderer #{version}"
35
+ rdoc.rdoc_files.include('README*')
36
+ rdoc.rdoc_files.include('lib/**/*.rb')
37
+ end
data/bin/update_jena ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+ lib = File.expand_path('../../lib/', __FILE__)
3
+ $:.unshift lib unless $:.include?(lib)
4
+
5
+ require 'rubygems'
6
+ require 'fileutils'
7
+ require 'open-uri'
8
+
9
+ require 'jena_jruby/version.rb'
10
+
11
+ LATEST_JENA="http://www-us.apache.org/dist/jena/binaries/apache-jena-#{Jena::JENA_VERSION}.zip"
12
+ OUTPUT="downloads/jena-#{Jena::JENA_VERSION}.zip"
13
+
14
+ FileUtils.mkdir_p "downloads"
15
+
16
+ unless File.exist? OUTPUT
17
+ puts "Downloading Jena #{Jena::JENA_VERSION}..."
18
+ open( LATEST_JENA ) do |url|
19
+ File.open( OUTPUT,"wb" ) do |file|
20
+ file.puts url.read
21
+ end
22
+ end
23
+ puts 'Download completed.'
24
+ else
25
+ puts 'Data file already exists, skipping download.'
26
+ end
27
+
28
+ raise 'Program `unzip\' is not present in the operation system.' if `unzip`.empty?
29
+ `unzip -jo #{OUTPUT} **/lib/*.jar -d javalib`
30
+
31
+ puts "Writing lib/jena_jruby/jars.rb ..."
32
+ r = File.open( 'lib/jena_jruby/jars.rb', 'w' )
33
+ Dir.glob( 'javalib/*.jar' ).each do |f|
34
+ f =~ /javalib\/(.*)\.jar/
35
+ r << "require '#{$1}'\n"
36
+ end
37
+ r.close
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File::expand_path('../lib/', __FILE__)
3
+ $:.unshift(lib) unless $:.include?(lib)
4
+
5
+ require 'jena_jruby/version.rb'
6
+
7
+ # ensure we have the javalibs
8
+ update = File::expand_path('../bin/update_jena', __FILE__)
9
+ Kernel::load update, true
10
+
11
+ Gem::Specification.new do |s|
12
+ s.name = 'jena'
13
+ s.version = Jena::JENA_JRUBY_GEM_VERSION
14
+ s.platform = Gem::Platform.new('java')
15
+ s.authors = ['Ian Dickinson', 'Bruno Ferreira', 'Fábio Almeida']
16
+ s.email = %w( ian@epimorphics.com chalkos@chalkos.net phabio_almeida@hotmail.com )
17
+ s.licenses = 'Apache-2.0'
18
+ s.homepage = 'https://github.com/lysferd/jena-jruby'
19
+ s.summary = %q{JRuby wrapper for Apache Jena}
20
+ s.description = %q{A simple packaging of Apache Jena for JRuby}
21
+
22
+ s.files = `git ls-files`.split(?\n).reject { |f| f.include? '/dev/' }
23
+ s.files.concat Dir.glob('javalib/*.jar')
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split(?\n)
25
+ s.executables = `git ls-files -- bin/*`.split(?\n).map { |f| File::basename f }
26
+ s.require_paths = %w( lib javalib )
27
+
28
+ s.extra_rdoc_files = %w( README.md )
29
+ end
30
+
@@ -0,0 +1,33 @@
1
+ require 'commons-csv-1.5'
2
+ require 'slf4j-log4j12-1.7.25'
3
+ require 'jena-tdb2-3.6.0'
4
+ require 'slf4j-api-1.7.25'
5
+ require 'jena-tdb-3.6.0'
6
+ require 'xercesImpl-2.11.0'
7
+ require 'log4j-1.2.17'
8
+ require 'jena-arq-3.6.0'
9
+ require 'jcl-over-slf4j-1.7.25'
10
+ require 'jackson-annotations-2.9.0'
11
+ require 'httpclient-4.5.3'
12
+ require 'jena-rdfconnection-3.6.0'
13
+ require 'commons-cli-1.4'
14
+ require 'collection-0.7'
15
+ require 'commons-codec-1.11'
16
+ require 'commons-io-2.6'
17
+ require 'jena-cmds-3.6.0'
18
+ require 'jena-dboe-transaction-3.6.0'
19
+ require 'jena-dboe-index-3.6.0'
20
+ require 'jena-shaded-guava-3.6.0'
21
+ require 'httpclient-cache-4.5.3'
22
+ require 'xml-apis-1.4.01'
23
+ require 'jena-core-3.6.0'
24
+ require 'jackson-core-2.9.0'
25
+ require 'jackson-databind-2.9.0'
26
+ require 'jena-dboe-trans-data-3.6.0'
27
+ require 'jena-base-3.6.0'
28
+ require 'httpcore-4.4.6'
29
+ require 'libthrift-0.10.0'
30
+ require 'jsonld-java-0.11.1'
31
+ require 'jena-iri-3.6.0'
32
+ require 'commons-lang3-3.4'
33
+ require 'jena-dboe-base-3.6.0'
@@ -0,0 +1,34 @@
1
+ module Jena
2
+ module Vocab
3
+
4
+ # A namespace is an object for resolving identifiers in scope denoted
5
+ # by a base URI to a full URI. For example,
6
+ #
7
+ # rdfs = Namespace.new( "http://www.w3.org/2000/01/rdf-schema#" )
8
+ # rdfs.comment # => "http://www.w3.org/2000/01/rdf-schema#comment"
9
+ #
10
+ # There is no checking that the resolved name is known in the defined
11
+ # namespace (e.g. <tt>rdfs.commnet</tt> will work without a warning)
12
+ class Namespace
13
+ attr_reader :ns
14
+
15
+ # Initialize this namespace with the given root URI. E.g, for RDFS,
16
+ # this would be
17
+ # rdfs = Namespace.new( "http://www.w3.org/2000/01/rdf-schema#" )
18
+ def initialize( ns )
19
+ @ns = ns
20
+ end
21
+
22
+ :private
23
+
24
+ def method_missing( name, *args)
25
+ prop = !args.empty? && args.first
26
+ n = "#{@ns}#{name}"
27
+ prop ? Jena::Core::ResourceFactory.createProperty(n) : Jena::Core::ResourceFactory.createResource(n)
28
+ end
29
+ end
30
+
31
+ # A namespace object for the SKOS vocabulary, which is not built-in to Jena
32
+ SKOS = Namespace.new( "http://www.w3.org/2004/02/skos/core#" )
33
+ end
34
+ end
@@ -0,0 +1,109 @@
1
+ module Jena
2
+ # A collection of utilities for manipulating Jena RDF Resources
3
+ # and Literals, collectively known as RDFNodes.
4
+ module Node
5
+ # Return the types of a node, which will be an array of the RDF types
6
+ # for a resource, or an array of the (zero or one) datatypes if the
7
+ # node is a literal. Ensures that the returned array includes the default_type
8
+ # f default_type is non-nil
9
+ def self.types( node, default_type = nil )
10
+ return self.resource_types( node, default_type ) if node.resource?
11
+ self.literal_types( node, default_type )
12
+ end
13
+
14
+ # Return the values of `rdf:type` for the given resource
15
+ def self.resource_types( node, default_type = nil )
16
+ types = []
17
+ node.each_property( Jena::Vocab::RDF.type ) {|stmt| types << stmt.getObject}
18
+ with_default_type( types, default_type )
19
+ end
20
+
21
+ # Return an array of the datatype of the given literal, if defined,
22
+ # and including the default_type if non-nil
23
+ def self.literal_types( node, default_type = nil )
24
+ types = node.getDatatype ? [node.getDatatype] : []
25
+ with_default_type( types, default_type )
26
+ end
27
+
28
+ :private
29
+
30
+ def self.with_default_type( types, default_type )
31
+ types << default_type if default_type && !types.include?( default_type )
32
+ types
33
+ end
34
+ end
35
+ end
36
+
37
+
38
+ # Add some Ruby-friendly method names to common Jena classes
39
+ module Java
40
+ module ComHpHplJenaEnhanced
41
+ class EnhNode
42
+ def types
43
+ Jena::Node.types self
44
+ end
45
+ end
46
+ end
47
+
48
+ module ComHpHplJenaRdfModelImpl
49
+ class ResourceImpl
50
+ # Return the resource types
51
+ def types
52
+ Jena::Node.types self
53
+ end
54
+
55
+ # Yield to the given block for each statement of this resource with predicate
56
+ # `property`. If `property` is nil, yield to the block for every statement whose
57
+ # subject is this resource.
58
+ def each_property( property = nil, &block )
59
+ listProperties( property ).each( &block )
60
+ end
61
+
62
+ # Return a list of the values of the given properties of this resource. Each property
63
+ # may be specified as a +Property+ object, a string denoting the URI, or a string
64
+ # denoting the URI in <tt>prefix:name</tt> format, which will be expanded using the
65
+ # prefix table attached to the resource's model
66
+ def property_values( props = nil, model = nil )
67
+ model ||= getModel
68
+ return [] unless model
69
+
70
+ values = []
71
+ resolve_properties( props, model ).each do |p|
72
+ model.listStatements( self, p, nil ).each do |stmt|
73
+ values << stmt.getObject
74
+ end
75
+ end
76
+ values
77
+ end
78
+
79
+ # Return a list of Jena +Property+ objects corresponding to the list of property
80
+ # names +props+. Each member of +props+ is either
81
+ # * a +Property+ object, which is returned unchanged
82
+ # * a URI string, which is converted to a +Property+
83
+ # * an abbreviated URI in <tt>prefix:name</tt> form, which is expanded using the prefixes
84
+ # defined in the current model before being converted to a +Property+ object
85
+ #
86
+ # In the special case of <tt>props = nil</tt>, the returned list is all of the distinct
87
+ # properties attached to this resource
88
+ def resolve_properties( props = nil, model = nil )
89
+ props = [props] unless props.is_a? Array
90
+ model ||= self.getModel
91
+
92
+ unless props
93
+ return model.listStatements( self, nil, nil ).map {|stmt| stmt.getPredicate} .uniq
94
+ end
95
+
96
+ props.map do |p|
97
+ case
98
+ when p.is_a?( Jena::Core::Property )
99
+ p
100
+ when model
101
+ model.getProperty( model.expandPrefix( p ) )
102
+ else
103
+ Jena::Core::ResourceFactory.getProperty( p )
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,205 @@
1
+ module Jena
2
+ module Query
3
+ # Perform a SPARQL select query against the given model. Returns
4
+ # an array of result objects, where each result is a Hash from
5
+ # query variable name to value.
6
+ #
7
+ # By default, all variables from the query are returned. To return
8
+ # just a subset of the query variables, pass the variable name(s) as
9
+ # the last argument to the call.
10
+ #
11
+ # Various options are supported:
12
+ # * `:ns` - a `PrefixMapping` or hash of prefixes to URI's
13
+ # * `:bindings` - a `QuerySolutionMap` or hash of existing var name to value pairs
14
+ #
15
+ # == Examples
16
+ # Jena.querySelect( m, 'select * where {?s ?p ?o}' )
17
+ # Jena.querySelect( m, 'select * where {?s ?p ?o}', {}, "s" )
18
+ # Jena.querySelect( m, 'select * where {?s ?p foo:bar}',
19
+ # {:ns => {"foo" => "http://example.com/foo#"}}, "s" )
20
+ #
21
+ # @param m [Model] The model to run the query against
22
+ # @param query [String] The query to run
23
+ # @param options [Hash] Options (see above)
24
+ # @param *vars [String] Optional variables to project from results
25
+ # @return [Array] Non-nil array of hashes, one per result
26
+ def self.select( m, query, options = nil, *vars )
27
+ results = []
28
+ select_each( m, query, options, *vars ) {|soln| results << soln}
29
+ results
30
+ end
31
+
32
+ # Perform a select query as per {#select}, but returns only the first
33
+ # result, if defined, or nil
34
+ def self.select_first( m, query, options = nil, *vars )
35
+ qexec = setup_query_execution( m, query, options )
36
+ result = nil
37
+
38
+ begin
39
+ rs = qexec.execSelect
40
+ result = project_variables( rs.next, *vars ) if rs.hasNext
41
+ ensure
42
+ qexec.close
43
+ end
44
+
45
+ result
46
+ end
47
+
48
+ # Perform a select query as per {#select}, but rather than accumulate
49
+ # an array of results, we yield to the associated block with each solution
50
+ def self.select_each( m, query, options = nil, *vars, &block )
51
+ select_each_qe( setup_query_execution( m, query, options ), *vars, &block )
52
+ end
53
+
54
+ # Perform a select query using the given query execution, yielding to the
55
+ # associated block for each solution
56
+ def self.select_each_qe( qexec, *vars )
57
+ begin
58
+ qexec.execSelect.each do |soln|
59
+ yield( project_variables( soln, *vars ) )
60
+ end
61
+ ensure
62
+ qexec.close
63
+ end
64
+ end
65
+
66
+ # Perform a SPARQL describe query. Options as per {#select},
67
+ # but this call returns a `Model`.
68
+ #
69
+ # @param m [Model] The model to run the query against
70
+ # @param query [String] The query to run
71
+ # @param options [Hash] Options (see above)
72
+ # @return [Array] Non-empty array of hashes, one per result
73
+ def self.describe( m, query, options = nil )
74
+ describe_qe( setup_query_execution( m, query, options ) )
75
+ end
76
+
77
+ # Perform a describe query using the given query execution object, and
78
+ # return the resulting model
79
+ def self.describe_qe( qexec )
80
+ begin
81
+ return qexec.execDescribe
82
+ ensure
83
+ qexec.close
84
+ end
85
+ end
86
+
87
+ # Project variables from a result set entry, returning a `Hash` of variable
88
+ # names to values
89
+ def self.project_variables( soln, *vars )
90
+ nn = Hash.new
91
+
92
+ vs = vars || []
93
+ soln.varNames.each {|v| vs << v} if vs.empty?
94
+
95
+ vs.each {|v| nn[v.to_sym] = soln.get( v )}
96
+
97
+ nn
98
+ end
99
+
100
+ # Return the given bindings object as a `QuerySolutionsMap`. Convert a
101
+ # hash to a `QuerySolutionsMap` if necessary
102
+ def self.as_bindings( b )
103
+ return nil unless b
104
+ return b if b.is_a? com.hp.hpl.jena.query.QuerySolutionMap
105
+ qsm = com.hp.hpl.jena.query.QuerySolutionMap.new
106
+ b.each_pair do |k,v|
107
+ qsm.add k, v
108
+ end
109
+ qsm
110
+ end
111
+
112
+ # A mutable collection of well-known namespace prefixes, which can
113
+ # be used as a basis for building up a larger collection
114
+ def self.useful_prefixes
115
+ pm = com.hp.hpl.jena.shared.impl.PrefixMappingImpl.new
116
+ pm.setNsPrefixes( com.hp.hpl.jena.shared.PrefixMapping.Standard )
117
+ pm
118
+ end
119
+
120
+ # Return true if a string represents a valid SPARQL query.
121
+ def self.valid?( query_string, syntax = Jena::Query::Syntax.syntaxSPARQL_11 )
122
+ q = Jena::Query::QueryFactory.create
123
+
124
+ begin
125
+ Jena::Query::QueryFactory.parse( q, query_string, nil, syntax )
126
+ return true
127
+ rescue
128
+ end
129
+
130
+ false
131
+ end
132
+
133
+ # Return a QueryExecution for executing the given query against a
134
+ # remote SPARQL service endpoint
135
+ def self.sparql_service( url, query )
136
+ Jena::Query::QueryExecutionFactory.sparqlService( url, query )
137
+ end
138
+
139
+ # Return a list of the solutions to executing the given select query
140
+ # against the given remote SPARQL endpoint
141
+ def self.service_select( url, query, *vars )
142
+ results = []
143
+ select_each_qe( sparql_service( url, query ), *vars ) {|soln| results << soln}
144
+ results
145
+ end
146
+
147
+ # Yield to the associated block for each solution to the given query against
148
+ # the given SPARQL service endpoint URL
149
+ def self.service_select_each( url, query, *vars, &block )
150
+ select_each_qe( sparql_service( url, query ), *vars, &block )
151
+ end
152
+
153
+ # Return a model from a describe query against a remote SPARQL endpoint
154
+ def self.service_describe( url, query )
155
+ describe_qe( sparql_service( url, query ) )
156
+ end
157
+
158
+ # Format a given value in a manner suitable for inclusion in a SPARQL query
159
+ def self.sparql_format( value )
160
+ if value.is_a? Core::Resource
161
+ value.is_anon ? "_:#{value.get_id.to_string.gsub(/[^[[:alnum:]]]/, '_')}" : "<#{value.get_uri}>"
162
+ elsif value.is_a? Core::Literal
163
+ if dt = value.get_datatype_uri
164
+ "\"#{value.get_lexical_form}\"^^<#{dt}>"
165
+ else
166
+ "\"#{value.to_string}\""
167
+ end
168
+ elsif value.to_s =~ /\A(file|http):\/\//
169
+ "<#{value}>"
170
+ elsif value.to_s =~ /\A[-_[[:alnum:]]]*:/
171
+ # looks like a qname
172
+ value.to_s
173
+ else
174
+ # guess
175
+ "\"#{value.to_s}\""
176
+ end
177
+ end
178
+
179
+ :private
180
+
181
+ def self.option( options, opt, default )
182
+ if has_option?( options, opt ) then options[opt] else default end
183
+ end
184
+
185
+ def self.has_option?( options, opt )
186
+ options && options.has_key?( opt )
187
+ end
188
+
189
+ # Common part of setting up a query execution
190
+ def self.setup_query_execution( m, query, options )
191
+ q = com.hp.hpl.jena.query.Query.new
192
+
193
+ q.setPrefixMapping has_option?( options, :ns ) ? Util::as_prefix_map( options[:ns] ) : useful_prefixes
194
+
195
+ baseURI = option( options, :baseURI, nil )
196
+ syntax = option( options, :syntax, com.hp.hpl.jena.query.Syntax.syntaxSPARQL_11 )
197
+
198
+ QueryFactory.parse( q, query, baseURI, syntax )
199
+ bindings = as_bindings( option( options, :bindings, nil ) )
200
+
201
+ bindings ? QueryExecutionFactory.create( q, m, bindings ) : QueryExecutionFactory.create( q, m )
202
+ end
203
+
204
+ end
205
+ end
@@ -0,0 +1,11 @@
1
+
2
+ module Jena::Model::Sta
3
+
4
+ ESPACE_CHAR = "\s"
5
+
6
+ #alias old_get_local_name get_local_name
7
+ def formatted_name
8
+ return self.get_local_name.gsub /_/, ESPACE_CHAR
9
+ end
10
+
11
+ end
@@ -0,0 +1,27 @@
1
+ module Jena
2
+ module Util
3
+ # Return the given prefix map object as a `PrefixMapping`. Convert a hash
4
+ # to a `PrefixMapping` if necessary
5
+ def self.as_prefix_map( map )
6
+ return nil unless map
7
+ return map if map.is_a? com.hp.hpl.jena.shared.PrefixMapping
8
+ pm = com.hp.hpl.jena.shared.impl.PrefixMappingImpl.new
9
+ map.each_pair do |k,v|
10
+ pm.setNsPrefix k.to_s, v.to_s
11
+ end
12
+ pm
13
+ end
14
+
15
+ # Return a new resource URN made from a UUID. If the given
16
+ def self.uuid_resource( model = nil )
17
+ (model || Core::ResourceFactory).createResource( Util::JenaUUID.factory.generate.asURN )
18
+ end
19
+
20
+ # Return the current time as a literal
21
+ def self.now
22
+ cal = java.util.Calendar.getInstance
23
+ cal.setTime( java.util.Date.new )
24
+ Core::ResourceFactory.createTypedLiteral( cal )
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ module Jena
2
+ JENA_VERSION='3.6.0' unless constants.include?(:JENA_VERSION)
3
+ JENA_JRUBY_GEM_VERSION='1.0.0' unless constants.include?(:JENA_JRUBY_GEM_VERSION)
4
+ end
data/lib/jena_jruby.rb ADDED
@@ -0,0 +1,107 @@
1
+ require 'java'
2
+ require 'jena_jruby/jars'
3
+ require 'builder'
4
+
5
+ # Make a selection of key Jena classes available in a convenient module
6
+ module Jena
7
+ include_package org.apache.jena
8
+
9
+ # Import classes under org.apache.jena.*
10
+ module Model
11
+ include_package org.apache.jena.rdf.model
12
+ include_package org.apache.jena.rdf.model.impl
13
+ end
14
+
15
+ module XML
16
+ include_package org.apache.jena.rdfxml.xmlinput
17
+ include_package org.apache.jena.rdfxml.impl
18
+ include_package org.apache.jena.xmlinput.states
19
+ include_package org.apache.jena.xmloutput
20
+ include_package org.apache.jena.xmloutput.impl
21
+ end
22
+
23
+ module Listener
24
+ include_package org.apache.jena.rdf.listeners
25
+ end
26
+
27
+ module Datatypes
28
+ include_package org.apache.jena.datatypes
29
+
30
+ module XSD
31
+ include_package org.apache.jena.xsd
32
+ include_package org.apache.jena.xsd.impl
33
+ end
34
+ end
35
+
36
+ module Enhanced
37
+ include_package org.apache.jena.enhanced
38
+ end
39
+
40
+ module Graph
41
+ include_package org.apache.jena.graph
42
+ include_package org.apache.jena.graph.compose
43
+ include_package org.apache.jena.graph.impl
44
+ end
45
+
46
+ module Mem
47
+ include_package org.apache.jena.mem
48
+ end
49
+
50
+ module Util
51
+ include_package org.apache.jena.util
52
+ include_package org.apache.jena.util.iterator
53
+ end
54
+
55
+ module Shared
56
+ include_package org.apache.jena.shared
57
+ include_package org.apache.jena.shared.impl
58
+ include_package org.apache.jena.shared.uuid
59
+ end
60
+
61
+ module Vocab
62
+ include_package org.apache.jena.vocabulary
63
+ end
64
+
65
+ module Query
66
+ include_package org.apache.jena.query
67
+ end
68
+
69
+ module Ontology
70
+ include_package org.apache.jena.ontology
71
+ include_package org.apache.jena.ontology.impl
72
+ end
73
+
74
+ module TDB
75
+ include_package org.apache.jena.tdb
76
+ end
77
+
78
+ module Reasoner
79
+ include_package org.apache.jena.reasoner
80
+ include_package org.apache.jena.reasoner.rulesys
81
+ include_package org.apache.jena.reasoner.rulesys.builtins
82
+ include_package org.apache.jena.reasoner.rulesys.impl
83
+ include_package org.apache.jena.reasoner.transitiveReasoner
84
+ end
85
+
86
+ module Assembler
87
+ include_package org.apache.jena.assembler
88
+ include_package org.apache.jena.assembler.assemblers
89
+
90
+ module Exceptions
91
+ include_package org.apache.jena.assembler.exceptions
92
+ end
93
+ end
94
+
95
+ module System
96
+ include_package org.apache.system
97
+ end
98
+ end
99
+
100
+ %w[
101
+ version
102
+ utils
103
+ query_utils
104
+ node_utils
105
+ statement_utils
106
+ namespace
107
+ ].each {|f| require "jena_jruby/#{f}"}
@@ -0,0 +1,80 @@
1
+ $LOAD_PATH.push "#{File.dirname(__FILE__)}/../lib"
2
+ $LOAD_PATH.push "#{File.dirname(__FILE__)}/../javalib"
3
+
4
+ require "minitest/autorun"
5
+ require "jena_jruby"
6
+
7
+ class NodeUtilsTest < Minitest::Test
8
+
9
+ def setup
10
+ @m = Jena::Core::ModelFactory.createDefaultModel
11
+ @r0 = @m.createResource "http://example.org/foo#r0"
12
+ @r1 = @m.createResource "http://example.org/foo#r1"
13
+ @m.add @r0, Jena::Vocab::RDFS.comment, "this is a test"
14
+ @m.add @r0, Jena::Vocab::RDF.type, @r1
15
+ end
16
+
17
+ def teardown
18
+ end
19
+
20
+ def test_resource_types
21
+ assert_equal [@r1], @r0.types
22
+ assert_equal [], @r1.types
23
+ end
24
+
25
+ def test_literal_types
26
+ lit = create_typed_literal( 10 )
27
+ types = lit.types
28
+ assert_equal "http://www.w3.org/2001/XMLSchema#long", types.first.getURI
29
+ assert_equal 1, types.length
30
+
31
+ lit = @m.createLiteral( "foo" )
32
+ types = lit.types
33
+ assert_equal 0, types.length
34
+ end
35
+
36
+ def test_default_type
37
+ assert_equal [@r1, Jena::Vocab::RDF::Property], Jena::Node.types( @r0, Jena::Vocab::RDF::Property )
38
+ assert_equal [Jena::Vocab::RDF::Property], Jena::Node.types( @r1, Jena::Vocab::RDF::Property )
39
+
40
+ assert_equal [:dummy], Jena::Node.types( @m.createLiteral( "foo" ), :dummy )
41
+ end
42
+
43
+ def test_resolve_properties
44
+ p = @m.createProperty( "http://example.com/p" )
45
+ assert_equal [p], @r0.resolve_properties( [p] )
46
+ assert_equal [p], @r0.resolve_properties( ["http://example.com/p"] )
47
+ @m.setNsPrefix( "eg", "http://example.com/" )
48
+ assert_equal [p], @r0.resolve_properties( ["eg:p"] )
49
+ end
50
+
51
+ def test_property_values
52
+ assert_equal [@r1], @r0.property_values( Jena::Vocab::RDF.type )
53
+ assert_equal [@r1], @r0.property_values( Jena::Vocab::RDF.type.getURI )
54
+ @m.setNsPrefix( "rdf", Jena::Vocab::RDF.getURI )
55
+ assert_equal [@r1], @r0.property_values( "rdf:type" )
56
+ end
57
+
58
+ def test_property_values_with_explicit_model
59
+ @m.setNsPrefix( "rdf", Jena::Vocab::RDF.getURI )
60
+
61
+ # r is not attached to any model
62
+ r = Jena::Core::ResourceFactory.createResource( @r0.getURI )
63
+
64
+ assert_equal [], r.property_values( Jena::Vocab::RDF.type )
65
+ assert_equal [], r.property_values( Jena::Vocab::RDF.type.getURI )
66
+ assert_equal [], r.property_values( "rdf:type" )
67
+
68
+ assert_equal [@r1], r.property_values( Jena::Vocab::RDF.type, @m )
69
+ assert_equal [@r1], r.property_values( Jena::Vocab::RDF.type.getURI, @m )
70
+ assert_equal [@r1], r.property_values( "rdf:type", @m )
71
+ end
72
+
73
+ :private
74
+
75
+ def create_typed_literal( v )
76
+ method = @m.java_method( :createTypedLiteral, [java.lang.Object] )
77
+ method.call( v )
78
+ end
79
+
80
+ end
@@ -0,0 +1,125 @@
1
+ $LOAD_PATH.push "#{File.dirname(__FILE__)}/../lib"
2
+ $LOAD_PATH.push "#{File.dirname(__FILE__)}/../javalib"
3
+
4
+ require "minitest/autorun"
5
+ require "jena_jruby"
6
+
7
+ class QueryUtilsTest < Minitest::Test
8
+
9
+ def setup
10
+ @m = Jena::Core::ModelFactory.createDefaultModel
11
+ @r0 = @m.createResource "http://example.org/foo#r0"
12
+ @m.add @r0, Jena::Vocab::RDFS.comment, "this is a test"
13
+ end
14
+
15
+ def teardown
16
+ end
17
+
18
+ def test_as_prefix_map_0
19
+ pm = com.hp.hpl.jena.shared.impl.PrefixMappingImpl.new
20
+ assert_same pm, Jena::Util.as_prefix_map( pm )
21
+ end
22
+
23
+ def test_as_prefix_map_1
24
+ map = {"foo" => "http://foo.bar"}
25
+ pm = Jena::Util.as_prefix_map map
26
+ assert_equal "http://foo.bar", pm.getNsPrefixURI( "foo" )
27
+ end
28
+
29
+ def test_as_bindings_0
30
+ qsm = Jena::Query::QuerySolutionMap.new
31
+ assert_same qsm, Jena::Query.as_bindings( qsm )
32
+ end
33
+
34
+ def test_as_bindings_1
35
+ r = Jena::Core::ResourceFactory.createResource "http://foo.bar#"
36
+ b = {"r" => r}
37
+ assert_equal r, Jena::Query.as_bindings( b ).get( "r" )
38
+ end
39
+
40
+ # Generic form of select
41
+ def test_select_0
42
+ result = Jena::Query.select @m, "select * {?s ?p ?o}", nil
43
+ assert_equal 1, result.size
44
+
45
+ assert_equal @r0, result[0][:s]
46
+ assert_equal Jena::Vocab::RDFS.comment, result[0][:p]
47
+ assert_equal "this is a test", result[0][:o].getLexicalForm
48
+ end
49
+
50
+ # Project variables from select
51
+ def test_select_1
52
+ result = Jena::Query.select @m, "select * {?s ?p ?o}", nil, "s"
53
+ assert_equal 1, result.size
54
+
55
+ assert_equal @r0, result[0][:s]
56
+ assert ! result[0].has_key?( :p )
57
+ assert ! result[0].has_key?( :o )
58
+
59
+ result = Jena::Query.select @m, "select * {?s ?p ?o}", nil, :s
60
+ assert_equal 1, result.size
61
+
62
+ assert_equal @r0, result[0][:s]
63
+ end
64
+
65
+ # Use namespaces in query select
66
+ def test_select_2
67
+ ns = {:foo => "http://example.org/foo#"}
68
+
69
+ result = Jena::Query.select @m, "select * {foo:r0 ?p ?o}", {:ns => ns}
70
+ assert_equal 1, result.size
71
+
72
+ assert ! result[0].has_key?( :s )
73
+ assert_equal Jena::Vocab::RDFS.comment, result[0][:p]
74
+ assert_equal "this is a test", result[0][:o].getLexicalForm
75
+ end
76
+
77
+ # Use variable bindings in query select
78
+ def test_select_3
79
+ b = {:s => @r0}
80
+
81
+ r1 = @m.createResource "http://example.org/foo#r1"
82
+ @m.add r1, Jena::Vocab::RDFS.comment, "this is another test"
83
+
84
+ result = Jena::Query.select @m, "select * {?s ?p ?o}", {:bindings => b}
85
+ assert_equal 1, result.size
86
+
87
+ assert_equal @r0, result[0][:s]
88
+ end
89
+
90
+ # Get only one result
91
+ def test_select_first
92
+ @m.add @r0, Jena::Vocab::RDFS.comment, "this is another test"
93
+
94
+ result = Jena::Query.select @m, "select * {?s ?p ?o}"
95
+ assert_equal 2, result.size
96
+
97
+ result = Jena::Query.select @m, "select * {?s ?p 42}"
98
+ assert_equal 0, result.size
99
+
100
+ result = Jena::Query.select_first @m, "select * {?s ?p ?o}"
101
+ assert_equal @r0, result[:s]
102
+
103
+ result = Jena::Query.select_first @m, "select * {?s ?p 42}"
104
+ assert_nil result
105
+ end
106
+
107
+ # SPARQL formatting tests
108
+ def test_sparql_format
109
+ assert_equal "<http://foo.bar>", Jena::Query.sparql_format( @m.create_resource( "http://foo.bar" ))
110
+ assert_equal '"foo"', Jena::Query.sparql_format( @m.create_literal( "foo" ))
111
+
112
+ tl = create_typed_literal( 42 )
113
+ assert_equal '"42"^^<http://www.w3.org/2001/XMLSchema#long>', Jena::Query.sparql_format( tl )
114
+
115
+ assert_equal "foo:bar", Jena::Query.sparql_format( "foo:bar" )
116
+ assert_equal "<http://foo.bar>", Jena::Query.sparql_format( "http://foo.bar" )
117
+ assert Jena::Query.sparql_format( @m.create_resource ).match( /_:([[:alnum:]]*)/ )
118
+ end
119
+
120
+ def create_typed_literal( v )
121
+ method = @m.java_method( :createTypedLiteral, [java.lang.Object] )
122
+ method.call( v )
123
+ end
124
+
125
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jena
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: java
6
+ authors:
7
+ - Ian Dickinson
8
+ - Bruno Ferreira
9
+ - Fábio Almeida
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2018-05-30 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: A simple packaging of Apache Jena for JRuby
16
+ email:
17
+ - ian@epimorphics.com
18
+ - chalkos@chalkos.net
19
+ - phabio_almeida@hotmail.com
20
+ executables:
21
+ - update_jena
22
+ extensions: []
23
+ extra_rdoc_files:
24
+ - README.md
25
+ files:
26
+ - ".gitignore"
27
+ - Gemfile
28
+ - Gemfile.lock
29
+ - LICENSE
30
+ - README.md
31
+ - Rakefile
32
+ - bin/update_jena
33
+ - javalib/collection-0.7.jar
34
+ - javalib/commons-cli-1.4.jar
35
+ - javalib/commons-codec-1.11.jar
36
+ - javalib/commons-csv-1.5.jar
37
+ - javalib/commons-io-2.6.jar
38
+ - javalib/commons-lang3-3.4.jar
39
+ - javalib/httpclient-4.5.3.jar
40
+ - javalib/httpclient-cache-4.5.3.jar
41
+ - javalib/httpcore-4.4.6.jar
42
+ - javalib/jackson-annotations-2.9.0.jar
43
+ - javalib/jackson-core-2.9.0.jar
44
+ - javalib/jackson-databind-2.9.0.jar
45
+ - javalib/jcl-over-slf4j-1.7.25.jar
46
+ - javalib/jena-arq-3.6.0.jar
47
+ - javalib/jena-base-3.6.0.jar
48
+ - javalib/jena-cmds-3.6.0.jar
49
+ - javalib/jena-core-3.6.0.jar
50
+ - javalib/jena-dboe-base-3.6.0.jar
51
+ - javalib/jena-dboe-index-3.6.0.jar
52
+ - javalib/jena-dboe-trans-data-3.6.0.jar
53
+ - javalib/jena-dboe-transaction-3.6.0.jar
54
+ - javalib/jena-iri-3.6.0.jar
55
+ - javalib/jena-rdfconnection-3.6.0.jar
56
+ - javalib/jena-shaded-guava-3.6.0.jar
57
+ - javalib/jena-tdb-3.6.0.jar
58
+ - javalib/jena-tdb2-3.6.0.jar
59
+ - javalib/jsonld-java-0.11.1.jar
60
+ - javalib/libthrift-0.10.0.jar
61
+ - javalib/log4j-1.2.17.jar
62
+ - javalib/slf4j-api-1.7.25.jar
63
+ - javalib/slf4j-log4j12-1.7.25.jar
64
+ - javalib/xercesImpl-2.11.0.jar
65
+ - javalib/xml-apis-1.4.01.jar
66
+ - jena-jruby.gemspec
67
+ - lib/jena_jruby.rb
68
+ - lib/jena_jruby/jars.rb
69
+ - lib/jena_jruby/namespace.rb
70
+ - lib/jena_jruby/node_utils.rb
71
+ - lib/jena_jruby/query_utils.rb
72
+ - lib/jena_jruby/statement_utils.rb
73
+ - lib/jena_jruby/utils.rb
74
+ - lib/jena_jruby/version.rb
75
+ - test/node_utils_test.rb
76
+ - test/query_utils_test.rb
77
+ homepage: https://github.com/lysferd/jena-jruby
78
+ licenses:
79
+ - Apache-2.0
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ - javalib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.13
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: JRuby wrapper for Apache Jena
102
+ test_files:
103
+ - test/node_utils_test.rb
104
+ - test/query_utils_test.rb