jena-jruby 0.3.0-java
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +10 -0
- data/LICENSE +13 -0
- data/README.md +101 -0
- data/Rakefile +37 -0
- data/bin/update_jars_maven +32 -0
- data/bin/update_jena +30 -0
- data/javalib/commons-codec-1.5.jar +0 -0
- data/javalib/hamcrest-core-1.1.jar +0 -0
- data/javalib/httpclient-4.1.2.jar +0 -0
- data/javalib/httpcore-4.1.3.jar +0 -0
- data/javalib/icu4j-3.4.4.jar +0 -0
- data/javalib/jena-arq-2.9.0-incubating.jar +0 -0
- data/javalib/jena-core-2.7.0-incubating.jar +0 -0
- data/javalib/jena-iri-0.9.0-incubating.jar +0 -0
- data/javalib/jena-tdb-0.9.0-incubating-SNAPSHOT.jar +0 -0
- data/javalib/junit-4.9.jar +0 -0
- data/javalib/log4j-1.2.16.jar +0 -0
- data/javalib/log4j-config.jar +0 -0
- data/javalib/slf4j-api-1.6.4.jar +0 -0
- data/javalib/slf4j-log4j12-1.6.4.jar +0 -0
- data/javalib/xercesImpl-2.10.0.jar +0 -0
- data/javalib/xml-apis-1.4.01.jar +0 -0
- data/jena-jruby.gemspec +25 -0
- data/lib/jena_jruby/jars.rb +19 -0
- data/lib/jena_jruby/namespace.rb +34 -0
- data/lib/jena_jruby/node_utils.rb +107 -0
- data/lib/jena_jruby/query_utils.rb +175 -0
- data/lib/jena_jruby/utils.rb +27 -0
- data/lib/jena_jruby/version.rb +5 -0
- data/lib/jena_jruby.rb +182 -0
- data/log4j.xml +20 -0
- data/test/node_utils_test.rb +71 -0
- data/test/query_utils_test.rb +105 -0
- metadata +112 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
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,101 @@
|
|
1
|
+
# Gem wrapper for Apache Jena
|
2
|
+
|
3
|
+
This Gem provides convenience access to the [Apache Jena](http://incubator.apache.org/jena)
|
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.7.0) and TDB (currently 0.9.0)
|
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-jruby` now includes TDB, the easiest way to get the full set of jars necessary
|
53
|
+
to support Jena is to use Maven to list the dependencies declared in the TDB `pom.xml`.
|
54
|
+
For convenience, this is packaged in a bash script:
|
55
|
+
|
56
|
+
ian@rowan-15 $ export JENA_TDB_ROOT=~/whereever/you/have/the/tdb/source
|
57
|
+
# note: defaults to ~/workspace/apache-jena-tdb
|
58
|
+
|
59
|
+
ian@rowan-15 $ cd ~/workspace/jena-jruby/
|
60
|
+
ian@rowan-15 $ bin/update_jars_maven
|
61
|
+
Changing to TDB directory /home/ian/workspace/apache-jena-tdb
|
62
|
+
~/workspace/apache-jena-tdb ~/workspace/jena-jruby
|
63
|
+
Cleaning old versions and recompiling ...
|
64
|
+
[INFO] Scanning for projects...
|
65
|
+
[INFO] ------------------------------------------------------------------------
|
66
|
+
[INFO] Building TDB
|
67
|
+
[INFO] task-segment: [clean, install]
|
68
|
+
[INFO] ------------------------------------------------------------------------
|
69
|
+
.. lots of maven output ..
|
70
|
+
[INFO] ------------------------------------------------------------------------
|
71
|
+
[INFO] BUILD SUCCESSFUL
|
72
|
+
[INFO] ------------------------------------------------------------------------
|
73
|
+
[INFO] Total time: 17 seconds
|
74
|
+
[INFO] Finished at: Thu Jan 05 00:18:09 GMT 2012
|
75
|
+
[INFO] Final Memory: 68M/718M
|
76
|
+
[INFO] ------------------------------------------------------------------------
|
77
|
+
Collecting dependencies ...
|
78
|
+
[INFO] Scanning for projects...
|
79
|
+
[INFO] Searching repository for plugin with prefix: 'dependency'.
|
80
|
+
[INFO] ------------------------------------------------------------------------
|
81
|
+
[INFO] Building TDB
|
82
|
+
[INFO] task-segment: [dependency:copy-dependencies]
|
83
|
+
[INFO] ------------------------------------------------------------------------
|
84
|
+
[INFO] [dependency:copy-dependencies {execution: default-cli}]
|
85
|
+
[INFO] Copying icu4j-3.4.4.jar to /home/ian/workspace/apache-jena-tdb/target/dependency/icu4j-3.4.4.jar
|
86
|
+
.. more maven output ..
|
87
|
+
[INFO] ------------------------------------------------------------------------
|
88
|
+
[INFO] BUILD SUCCESSFUL
|
89
|
+
[INFO] ------------------------------------------------------------------------
|
90
|
+
[INFO] Total time: 2 seconds
|
91
|
+
[INFO] Finished at: Thu Jan 05 00:18:12 GMT 2012
|
92
|
+
[INFO] Final Memory: 35M/349M
|
93
|
+
[INFO] ------------------------------------------------------------------------
|
94
|
+
~/workspace/jena-jruby
|
95
|
+
Changing directory back to /home/ian/workspace/jena-jruby
|
96
|
+
Copying deps to ./javalib
|
97
|
+
Creating log4j config
|
98
|
+
|
99
|
+
ian@rowan-15 $ rake build
|
100
|
+
rake/rdoctask is deprecated. Use rdoc/task instead (in RDoc 2.4.2+)
|
101
|
+
jena-jruby 0.3.0 built to pkg/jena-jruby-0.3.0-java.gem
|
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
|
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
# Collect the .jar files necessary to build the gem by running
|
3
|
+
# maven in the context of TDB
|
4
|
+
# Execute in root directory of jena_jruby
|
5
|
+
|
6
|
+
# Default TDB location
|
7
|
+
# Can be overridden by exporting JENA_TDB_ROOT
|
8
|
+
tdb_root=${JENA_TDB_ROOT:-~/workspace/apache-jena-tdb}
|
9
|
+
|
10
|
+
echo "Changing to TDB directory $tdb_root"
|
11
|
+
pushd $tdb_root
|
12
|
+
|
13
|
+
echo "Cleaning old versions and recompiling ..."
|
14
|
+
mvn -DskipTests=true clean install
|
15
|
+
|
16
|
+
echo "Collecting dependencies ..."
|
17
|
+
mvn dependency:copy-dependencies
|
18
|
+
|
19
|
+
popd
|
20
|
+
echo "Changing directory back to `pwd`"
|
21
|
+
echo "Copying deps to ./javalib"
|
22
|
+
mkdir -p javalib
|
23
|
+
rm -f javalib/*.jar
|
24
|
+
cp $tdb_root/target/dependency/*.jar javalib
|
25
|
+
cp $tdb_root/target/jena-tdb-*SNAPSHOT.jar $tdb_root/target/tdb-*{0,1,2,3,4,5,6,7,8,9}.jar javalib 2>/dev/null
|
26
|
+
rm -f javalib/*test*.jar
|
27
|
+
|
28
|
+
echo "Creating log4j config"
|
29
|
+
jar -cf javalib/log4j-config.jar log4j.xml
|
30
|
+
|
31
|
+
|
32
|
+
|
data/bin/update_jena
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require 'rubygems'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'open-uri'
|
6
|
+
load 'lib/jruby_jena/version.rb'
|
7
|
+
|
8
|
+
LATEST_JENA="http://sourceforge.net/projects/jena/files/Jena/Jena-#{Jena::JENA_VERSION}/jena-#{Jena::JENA_VERSION}.zip/download"
|
9
|
+
OUTPUT="downloads/jena-#{Jena::JENA_VERSION}.zip"
|
10
|
+
|
11
|
+
FileUtils.mkdir_p "downloads"
|
12
|
+
|
13
|
+
unless File.exist? OUTPUT
|
14
|
+
puts "Downloading Jena #{Jena::JENA_VERSION}..."
|
15
|
+
open( LATEST_JENA ) do |url|
|
16
|
+
File.open( OUTPUT,"wb" ) do |file|
|
17
|
+
file.puts url.read
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
`unzip -jo #{OUTPUT} **/lib/*.jar -x '**/lib/junit*.jar' '**/lib/*tests.jar' -d javalib`
|
23
|
+
|
24
|
+
puts "Writing lib/jruby_jena/jars.rb ..."
|
25
|
+
r = File.open( 'lib/jruby_jena/jars.rb', 'w' )
|
26
|
+
Dir.glob( 'javalib/*.jar' ).each do |f|
|
27
|
+
f =~ /javalib\/(.*)\.jar/
|
28
|
+
r << "require '#{$1}'\n"
|
29
|
+
end
|
30
|
+
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
|
data/jena-jruby.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("./lib", __FILE__)
|
3
|
+
load "lib/jena_jruby/version.rb"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "jena-jruby"
|
7
|
+
s.version = Jena::JENA_JRUBY_GEM_VERSION
|
8
|
+
s.platform = Gem::Platform.new("java")
|
9
|
+
s.authors = ["Ian Dickinson"]
|
10
|
+
s.email = ["ian@epimorphics.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{JRuby wrapper for Apache Jena}
|
13
|
+
s.description = %q{A simple packaging of Apache Jena for JRuby}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n").reject { |f| f.include? '/dev/' }
|
16
|
+
s.files.concat( Dir.glob 'javalib/*.jar' )
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
# s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib", "javalib"]
|
20
|
+
|
21
|
+
s.extra_rdoc_files = ["README.md"]
|
22
|
+
|
23
|
+
s.add_dependency( 'haml' )
|
24
|
+
s.add_dependency( 'builder' )
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
%w[
|
2
|
+
commons-codec-1.5
|
3
|
+
hamcrest-core-1.1
|
4
|
+
httpclient-4.1.2
|
5
|
+
httpcore-4.1.3
|
6
|
+
icu4j-3.4.4
|
7
|
+
jena-arq-2.9.0-incubating
|
8
|
+
jena-core-2.7.0-incubating
|
9
|
+
jena-iri-0.9.0-incubating
|
10
|
+
jena-tdb-0.9.0-incubating-SNAPSHOT
|
11
|
+
junit-4.9
|
12
|
+
log4j-1.2.16
|
13
|
+
slf4j-api-1.6.4
|
14
|
+
slf4j-log4j12-1.6.4
|
15
|
+
xercesImpl-2.10.0
|
16
|
+
xml-apis-1.4.01
|
17
|
+
log4j-config
|
18
|
+
].each {|jar| require jar}
|
19
|
+
|
@@ -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,107 @@
|
|
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
|
+
alias resource? isResource
|
43
|
+
alias literal? isLiteral
|
44
|
+
|
45
|
+
def types
|
46
|
+
Jena::Node.types self
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module ComHpHplJenaRdfModelImpl
|
52
|
+
class ResourceImpl
|
53
|
+
# Yield to the given block for each statement of this resource with predicate
|
54
|
+
# `property`. If `property` is nil, yield to the block for every statement whose
|
55
|
+
# subject is this resource.
|
56
|
+
def each_property( property = nil, &block )
|
57
|
+
listProperties( property ).each( &block )
|
58
|
+
end
|
59
|
+
|
60
|
+
# Return a list of the values of the given properties of this resource. Each property
|
61
|
+
# may be specified as a +Property+ object, a string denoting the URI, or a string
|
62
|
+
# denoting the URI in <tt>prefix:name</tt> format, which will be expanded using the
|
63
|
+
# prefix table attached to the resource's model
|
64
|
+
def property_values( props = nil, model = nil )
|
65
|
+
model ||= getModel
|
66
|
+
return [] unless model
|
67
|
+
|
68
|
+
values = []
|
69
|
+
resolve_properties( props, model ).each do |p|
|
70
|
+
model.listStatements( self, p, nil ).each do |stmt|
|
71
|
+
values << stmt.getObject
|
72
|
+
end
|
73
|
+
end
|
74
|
+
values
|
75
|
+
end
|
76
|
+
|
77
|
+
# Return a list of Jena +Property+ objects corresponding to the list of property
|
78
|
+
# names +props+. Each member of +props+ is either
|
79
|
+
# * a +Property+ object, which is returned unchanged
|
80
|
+
# * a URI string, which is converted to a +Property+
|
81
|
+
# * an abbreviated URI in <tt>prefix:name</tt> form, which is expanded using the prefixes
|
82
|
+
# defined in the current model before being converted to a +Property+ object
|
83
|
+
#
|
84
|
+
# In the special case of <tt>props = nil</tt>, the returned list is all of the distinct
|
85
|
+
# properties attached to this resource
|
86
|
+
def resolve_properties( props = nil, model = nil )
|
87
|
+
props = [props] unless props.is_a? Array
|
88
|
+
model ||= self.getModel
|
89
|
+
|
90
|
+
unless props
|
91
|
+
return model.listStatements( self, nil, nil ).map {|stmt| stmt.getPredicate} .uniq
|
92
|
+
end
|
93
|
+
|
94
|
+
props.map do |p|
|
95
|
+
case
|
96
|
+
when p.is_a?( Jena::Core::Property )
|
97
|
+
p
|
98
|
+
when model
|
99
|
+
model.getProperty( model.expandPrefix( p ) )
|
100
|
+
else
|
101
|
+
Jena::Core::ResourceFactory.getProperty( p )
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,175 @@
|
|
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-empty 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
|
+
qexec = setup_query_execution( m, query, options )
|
75
|
+
|
76
|
+
begin
|
77
|
+
return qexec.execDescribe
|
78
|
+
ensure
|
79
|
+
qexec.close
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Project variables from a result set entry, returning a `Hash` of variable
|
84
|
+
# names to values
|
85
|
+
def self.project_variables( soln, *vars )
|
86
|
+
nn = Hash.new
|
87
|
+
|
88
|
+
vs = vars || []
|
89
|
+
soln.varNames.each {|v| vs << v} if vs.empty?
|
90
|
+
|
91
|
+
vs.each {|v| nn[v.to_sym] = soln.get( v )}
|
92
|
+
|
93
|
+
nn
|
94
|
+
end
|
95
|
+
|
96
|
+
# Return the given bindings object as a `QuerySolutionsMap`. Convert a
|
97
|
+
# hash to a `QuerySolutionsMap` if necessary
|
98
|
+
def self.as_bindings( b )
|
99
|
+
return nil unless b
|
100
|
+
return b if b.is_a? com.hp.hpl.jena.query.QuerySolutionMap
|
101
|
+
qsm = com.hp.hpl.jena.query.QuerySolutionMap.new
|
102
|
+
b.each_pair do |k,v|
|
103
|
+
qsm.add k, v
|
104
|
+
end
|
105
|
+
qsm
|
106
|
+
end
|
107
|
+
|
108
|
+
# A mutable collection of well-known namespace prefixes, which can
|
109
|
+
# be used as a basis for building up a larger collection
|
110
|
+
def self.useful_prefixes
|
111
|
+
pm = com.hp.hpl.jena.shared.impl.PrefixMappingImpl.new
|
112
|
+
pm.setNsPrefixes( com.hp.hpl.jena.shared.PrefixMapping.Standard )
|
113
|
+
pm
|
114
|
+
end
|
115
|
+
|
116
|
+
# Return true if a string represents a valid SPARQL query.
|
117
|
+
def self.valid?( query_string, syntax = Jena::Query::Syntax.syntaxSPARQL_11 )
|
118
|
+
q = Jena::Query::QueryFactory.create
|
119
|
+
|
120
|
+
begin
|
121
|
+
Jena::Query::QueryFactory.parse( q, query_string, nil, syntax )
|
122
|
+
return true
|
123
|
+
rescue
|
124
|
+
end
|
125
|
+
|
126
|
+
false
|
127
|
+
end
|
128
|
+
|
129
|
+
# Return a QueryExecution for executing the given query against a
|
130
|
+
# remote SPARQL service endpoint
|
131
|
+
def self.sparql_service( url, query )
|
132
|
+
Jena::Query::QueryExecutionFactory.sparqlService( url, query )
|
133
|
+
end
|
134
|
+
|
135
|
+
# Return a list of the solutions to executing the given select query
|
136
|
+
# against the given remote SPARQL endpoint
|
137
|
+
def self.service_select( url, query, *vars )
|
138
|
+
results = []
|
139
|
+
select_each_qe( sparql_service( url, query ), *vars ) {|soln| results << soln}
|
140
|
+
results
|
141
|
+
end
|
142
|
+
|
143
|
+
# Yield to the associated block for each solution to the given query against
|
144
|
+
# the given SPARQL service endpoint URL
|
145
|
+
def self.service_select_each( url, query, *vars, &block )
|
146
|
+
select_each_qe( sparql_service( url, query ), *vars, &block )
|
147
|
+
end
|
148
|
+
|
149
|
+
:private
|
150
|
+
|
151
|
+
def self.option( options, opt, default )
|
152
|
+
if has_option?( options, opt ) then options[opt] else default end
|
153
|
+
end
|
154
|
+
|
155
|
+
def self.has_option?( options, opt )
|
156
|
+
options && options.has_key?( opt )
|
157
|
+
end
|
158
|
+
|
159
|
+
# Common part of setting up a query execution
|
160
|
+
def self.setup_query_execution( m, query, options )
|
161
|
+
q = com.hp.hpl.jena.query.Query.new
|
162
|
+
|
163
|
+
q.setPrefixMapping has_option?( options, :ns ) ? Util::as_prefix_map( options[:ns] ) : useful_prefixes
|
164
|
+
|
165
|
+
baseURI = option( options, :baseURI, nil )
|
166
|
+
syntax = option( options, :syntax, com.hp.hpl.jena.query.Syntax.syntaxSPARQL_11 )
|
167
|
+
|
168
|
+
QueryFactory.parse( q, query, baseURI, syntax )
|
169
|
+
qexec = QueryExecutionFactory.create( q, m, as_bindings( option( options, :bindings, nil ) ) )
|
170
|
+
|
171
|
+
qexec
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|
175
|
+
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
|
16
|
+
def self.uuid_resource
|
17
|
+
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
|
data/lib/jena_jruby.rb
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
require 'java'
|
2
|
+
require 'jena_jruby/jars'
|
3
|
+
require 'builder'
|
4
|
+
|
5
|
+
# Short names for commonly used Java packages
|
6
|
+
module Jena
|
7
|
+
JENA = "com.hp.hpl.jena"
|
8
|
+
end
|
9
|
+
|
10
|
+
# Make a selection of key Jena classes available in a convenient module
|
11
|
+
module Jena
|
12
|
+
module Core
|
13
|
+
java_import com.hp.hpl.jena.rdf.model.Alt
|
14
|
+
java_import com.hp.hpl.jena.rdf.model.AnonId
|
15
|
+
java_import com.hp.hpl.jena.rdf.model.Bag
|
16
|
+
java_import com.hp.hpl.jena.rdf.model.Container
|
17
|
+
java_import com.hp.hpl.jena.rdf.model.InfModel
|
18
|
+
java_import com.hp.hpl.jena.rdf.model.Literal
|
19
|
+
java_import com.hp.hpl.jena.rdf.model.ModelChangedListener
|
20
|
+
java_import com.hp.hpl.jena.rdf.model.ModelFactory
|
21
|
+
java_import com.hp.hpl.jena.rdf.model.ModelGetter
|
22
|
+
java_import com.hp.hpl.jena.rdf.model.ModelGraphInterface
|
23
|
+
java_import com.hp.hpl.jena.rdf.model.Model
|
24
|
+
java_import com.hp.hpl.jena.rdf.model.ModelMaker
|
25
|
+
java_import com.hp.hpl.jena.rdf.model.ModelReader
|
26
|
+
java_import com.hp.hpl.jena.rdf.model.ModelSource
|
27
|
+
java_import com.hp.hpl.jena.rdf.model.NodeIterator
|
28
|
+
java_import com.hp.hpl.jena.rdf.model.NsIterator
|
29
|
+
java_import com.hp.hpl.jena.rdf.model.Property
|
30
|
+
java_import com.hp.hpl.jena.rdf.model.RDFErrorHandler
|
31
|
+
java_import com.hp.hpl.jena.rdf.model.RDFList
|
32
|
+
java_import com.hp.hpl.jena.rdf.model.RDFNode
|
33
|
+
java_import com.hp.hpl.jena.rdf.model.RDFReader
|
34
|
+
java_import com.hp.hpl.jena.rdf.model.RDFVisitor
|
35
|
+
java_import com.hp.hpl.jena.rdf.model.RDFWriter
|
36
|
+
java_import com.hp.hpl.jena.rdf.model.ReifiedStatement
|
37
|
+
java_import com.hp.hpl.jena.rdf.model.ResIterator
|
38
|
+
java_import com.hp.hpl.jena.rdf.model.ResourceFactory
|
39
|
+
java_import com.hp.hpl.jena.rdf.model.Resource
|
40
|
+
java_import com.hp.hpl.jena.rdf.model.RSIterator
|
41
|
+
java_import com.hp.hpl.jena.rdf.model.Schema
|
42
|
+
java_import com.hp.hpl.jena.rdf.model.Selector
|
43
|
+
java_import com.hp.hpl.jena.rdf.model.Seq
|
44
|
+
java_import com.hp.hpl.jena.rdf.model.SimpleSelector
|
45
|
+
java_import com.hp.hpl.jena.rdf.model.StatementBoundary
|
46
|
+
java_import com.hp.hpl.jena.rdf.model.Statement
|
47
|
+
java_import com.hp.hpl.jena.rdf.model.StatementTripleBoundary
|
48
|
+
java_import com.hp.hpl.jena.rdf.model.StmtIterator
|
49
|
+
end
|
50
|
+
|
51
|
+
module Util
|
52
|
+
java_import com.hp.hpl.jena.util.CharEncoding
|
53
|
+
java_import com.hp.hpl.jena.util.CollectionFactory
|
54
|
+
java_import com.hp.hpl.jena.util.FileManager
|
55
|
+
java_import com.hp.hpl.jena.util.FileUtils
|
56
|
+
java_import com.hp.hpl.jena.util.IteratorCollection
|
57
|
+
java_import com.hp.hpl.jena.util.LocationMapper
|
58
|
+
java_import com.hp.hpl.jena.util.LocatorClassLoader
|
59
|
+
java_import com.hp.hpl.jena.util.LocatorFile
|
60
|
+
java_import com.hp.hpl.jena.util.Locator
|
61
|
+
java_import com.hp.hpl.jena.util.LocatorURL
|
62
|
+
java_import com.hp.hpl.jena.util.LocatorZip
|
63
|
+
java_import com.hp.hpl.jena.util.Metadata
|
64
|
+
java_import com.hp.hpl.jena.util.ModelLoader
|
65
|
+
java_import com.hp.hpl.jena.util.ModelQueryUtil
|
66
|
+
java_import com.hp.hpl.jena.util.MonitorGraph
|
67
|
+
java_import com.hp.hpl.jena.util.MonitorModel
|
68
|
+
java_import com.hp.hpl.jena.util.OneToManyMap
|
69
|
+
java_import com.hp.hpl.jena.util.PrintUtil
|
70
|
+
java_import com.hp.hpl.jena.util.QueryMapper
|
71
|
+
java_import com.hp.hpl.jena.util.ResourceUtils
|
72
|
+
java_import com.hp.hpl.jena.util.SystemUtils
|
73
|
+
java_import com.hp.hpl.jena.util.Tokenizer
|
74
|
+
java_import com.hp.hpl.jena.util.TypedStream
|
75
|
+
java_import com.hp.hpl.jena.util.URIref
|
76
|
+
java_import com.hp.hpl.jena.shared.Command
|
77
|
+
java_import com.hp.hpl.jena.shared.Lock
|
78
|
+
java_import com.hp.hpl.jena.shared.LockMRSW
|
79
|
+
java_import com.hp.hpl.jena.shared.LockMutex
|
80
|
+
java_import com.hp.hpl.jena.shared.LockNone
|
81
|
+
java_import com.hp.hpl.jena.shared.PrefixMapping
|
82
|
+
java_import com.hp.hpl.jena.shared.RandomOrderGraph
|
83
|
+
java_import com.hp.hpl.jena.shared.ReificationStyle
|
84
|
+
java_import com.hp.hpl.jena.shared.uuid.JenaUUID
|
85
|
+
java_import com.hp.hpl.jena.shared.uuid.UUIDFactory
|
86
|
+
end
|
87
|
+
|
88
|
+
module Vocab
|
89
|
+
java_import com.hp.hpl.jena.vocabulary.DB
|
90
|
+
java_import com.hp.hpl.jena.vocabulary.DC_10
|
91
|
+
java_import com.hp.hpl.jena.vocabulary.DC_11
|
92
|
+
java_import com.hp.hpl.jena.vocabulary.DC
|
93
|
+
java_import com.hp.hpl.jena.vocabulary.DCTerms
|
94
|
+
java_import com.hp.hpl.jena.vocabulary.DCTypes
|
95
|
+
java_import com.hp.hpl.jena.vocabulary.LocationMappingVocab
|
96
|
+
java_import com.hp.hpl.jena.vocabulary.OntDocManagerVocab
|
97
|
+
java_import com.hp.hpl.jena.vocabulary.OntEventsVocab
|
98
|
+
java_import com.hp.hpl.jena.vocabulary.OWL2
|
99
|
+
java_import com.hp.hpl.jena.vocabulary.OWL
|
100
|
+
java_import com.hp.hpl.jena.vocabulary.RDF
|
101
|
+
java_import com.hp.hpl.jena.vocabulary.RDFS
|
102
|
+
java_import com.hp.hpl.jena.vocabulary.RDFSyntax
|
103
|
+
java_import com.hp.hpl.jena.vocabulary.ReasonerVocabulary
|
104
|
+
java_import com.hp.hpl.jena.vocabulary.ResultSet
|
105
|
+
java_import com.hp.hpl.jena.vocabulary.RSS
|
106
|
+
java_import com.hp.hpl.jena.vocabulary.VCARD
|
107
|
+
java_import com.hp.hpl.jena.vocabulary.XSD
|
108
|
+
end
|
109
|
+
|
110
|
+
module Query
|
111
|
+
java_import com.hp.hpl.jena.query.ARQ
|
112
|
+
java_import com.hp.hpl.jena.query.DatasetFactory
|
113
|
+
java_import com.hp.hpl.jena.query.Dataset
|
114
|
+
java_import com.hp.hpl.jena.query.DataSource
|
115
|
+
java_import com.hp.hpl.jena.query.QueryExecutionFactory
|
116
|
+
java_import com.hp.hpl.jena.query.QueryExecution
|
117
|
+
java_import com.hp.hpl.jena.query.QueryFactory
|
118
|
+
java_import com.hp.hpl.jena.query.Query
|
119
|
+
java_import com.hp.hpl.jena.query.QuerySolution
|
120
|
+
java_import com.hp.hpl.jena.query.QuerySolutionMap
|
121
|
+
java_import com.hp.hpl.jena.query.QueryVisitor
|
122
|
+
java_import com.hp.hpl.jena.query.ResultSetFactory
|
123
|
+
java_import com.hp.hpl.jena.query.ResultSetFormatter
|
124
|
+
java_import com.hp.hpl.jena.query.ResultSet
|
125
|
+
java_import com.hp.hpl.jena.query.ResultSetRewindable
|
126
|
+
java_import com.hp.hpl.jena.query.SortCondition
|
127
|
+
java_import com.hp.hpl.jena.query.Syntax
|
128
|
+
end
|
129
|
+
|
130
|
+
module Ont
|
131
|
+
java_import com.hp.hpl.jena.ontology.AllDifferent
|
132
|
+
java_import com.hp.hpl.jena.ontology.AllValuesFromRestriction
|
133
|
+
java_import com.hp.hpl.jena.ontology.AnnotationProperty
|
134
|
+
java_import com.hp.hpl.jena.ontology.BooleanClassDescription
|
135
|
+
java_import com.hp.hpl.jena.ontology.CardinalityQRestriction
|
136
|
+
java_import com.hp.hpl.jena.ontology.CardinalityRestriction
|
137
|
+
java_import com.hp.hpl.jena.ontology.ComplementClass
|
138
|
+
java_import com.hp.hpl.jena.ontology.DataRange
|
139
|
+
java_import com.hp.hpl.jena.ontology.DatatypeProperty
|
140
|
+
java_import com.hp.hpl.jena.ontology.EnumeratedClass
|
141
|
+
java_import com.hp.hpl.jena.ontology.FunctionalProperty
|
142
|
+
java_import com.hp.hpl.jena.ontology.HasValueRestriction
|
143
|
+
java_import com.hp.hpl.jena.ontology.Individual
|
144
|
+
java_import com.hp.hpl.jena.ontology.IntersectionClass
|
145
|
+
java_import com.hp.hpl.jena.ontology.InverseFunctionalProperty
|
146
|
+
java_import com.hp.hpl.jena.ontology.MaxCardinalityQRestriction
|
147
|
+
java_import com.hp.hpl.jena.ontology.MaxCardinalityRestriction
|
148
|
+
java_import com.hp.hpl.jena.ontology.MinCardinalityQRestriction
|
149
|
+
java_import com.hp.hpl.jena.ontology.MinCardinalityRestriction
|
150
|
+
java_import com.hp.hpl.jena.ontology.ObjectProperty
|
151
|
+
java_import com.hp.hpl.jena.ontology.OntClass
|
152
|
+
java_import com.hp.hpl.jena.ontology.OntDocumentManager
|
153
|
+
java_import com.hp.hpl.jena.ontology.OntModel
|
154
|
+
java_import com.hp.hpl.jena.ontology.OntModelSpec
|
155
|
+
java_import com.hp.hpl.jena.ontology.Ontology
|
156
|
+
java_import com.hp.hpl.jena.ontology.OntProperty
|
157
|
+
java_import com.hp.hpl.jena.ontology.OntResource
|
158
|
+
java_import com.hp.hpl.jena.ontology.OntTools
|
159
|
+
java_import com.hp.hpl.jena.ontology.Profile
|
160
|
+
java_import com.hp.hpl.jena.ontology.ProfileRegistry
|
161
|
+
java_import com.hp.hpl.jena.ontology.QualifiedRestriction
|
162
|
+
java_import com.hp.hpl.jena.ontology.Restriction
|
163
|
+
java_import com.hp.hpl.jena.ontology.SomeValuesFromRestriction
|
164
|
+
java_import com.hp.hpl.jena.ontology.SymmetricProperty
|
165
|
+
java_import com.hp.hpl.jena.ontology.TransitiveProperty
|
166
|
+
java_import com.hp.hpl.jena.ontology.UnionClass
|
167
|
+
end
|
168
|
+
|
169
|
+
module TDB
|
170
|
+
java_import com.hp.hpl.jena.tdb.TDB
|
171
|
+
java_import com.hp.hpl.jena.tdb.TDBLoader
|
172
|
+
java_import com.hp.hpl.jena.tdb.TDBFactory
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
%w[
|
177
|
+
version
|
178
|
+
utils
|
179
|
+
query_utils
|
180
|
+
node_utils
|
181
|
+
namespace
|
182
|
+
].each {|f| require "jena_jruby/#{f}"}
|
data/log4j.xml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
|
3
|
+
<!DOCTYPE log4j:configuration PUBLIC "log4j.dtd"
|
4
|
+
"http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
|
5
|
+
|
6
|
+
|
7
|
+
<log4j:configuration>
|
8
|
+
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
|
9
|
+
<layout class="org.apache.log4j.PatternLayout">
|
10
|
+
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
|
11
|
+
</layout>
|
12
|
+
</appender>
|
13
|
+
<logger name="com.epimorphics">
|
14
|
+
<level value="info"/>
|
15
|
+
</logger>
|
16
|
+
<root>
|
17
|
+
<priority value="info"></priority>
|
18
|
+
<appender-ref ref="stdout"/>
|
19
|
+
</root>
|
20
|
+
</log4j:configuration>
|
@@ -0,0 +1,71 @@
|
|
1
|
+
$LOAD_PATH.push "#{File.dirname(__FILE__)}/../lib"
|
2
|
+
require "rubygems"
|
3
|
+
require "test/unit"
|
4
|
+
require "jena_jruby"
|
5
|
+
|
6
|
+
class NodeUtilsTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@m = Jena::Core::ModelFactory.createDefaultModel
|
10
|
+
@r0 = @m.createResource "http://example.org/foo#r0"
|
11
|
+
@r1 = @m.createResource "http://example.org/foo#r1"
|
12
|
+
@m.add @r0, Jena::Vocab::RDFS.comment, "this is a test"
|
13
|
+
@m.add @r0, Jena::Vocab::RDF.type, @r1
|
14
|
+
end
|
15
|
+
|
16
|
+
def teardown
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_resource_types
|
20
|
+
assert_equal [@r1], @r0.types
|
21
|
+
assert_equal [], @r1.types
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_literal_types
|
25
|
+
lit = @m.createTypedLiteral( 10 )
|
26
|
+
types = lit.types
|
27
|
+
assert_equal "http://www.w3.org/2001/XMLSchema#long", types.first.getURI
|
28
|
+
assert_equal 1, types.length
|
29
|
+
|
30
|
+
lit = @m.createLiteral( "foo" )
|
31
|
+
types = lit.types
|
32
|
+
assert_equal 0, types.length
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_default_type
|
36
|
+
assert_equal [@r1, Jena::Vocab::RDF::Property], Jena::Node.types( @r0, Jena::Vocab::RDF::Property )
|
37
|
+
assert_equal [Jena::Vocab::RDF::Property], Jena::Node.types( @r1, Jena::Vocab::RDF::Property )
|
38
|
+
|
39
|
+
assert_equal [:dummy], Jena::Node.types( @m.createLiteral( "foo" ), :dummy )
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_resolve_properties
|
43
|
+
p = @m.createProperty( "http://example.com/p" )
|
44
|
+
assert_equal [p], @r0.resolve_properties( [p] )
|
45
|
+
assert_equal [p], @r0.resolve_properties( ["http://example.com/p"] )
|
46
|
+
@m.setNsPrefix( "eg", "http://example.com/" )
|
47
|
+
assert_equal [p], @r0.resolve_properties( ["eg:p"] )
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_property_values
|
51
|
+
assert_equal [@r1], @r0.property_values( Jena::Vocab::RDF.type )
|
52
|
+
assert_equal [@r1], @r0.property_values( Jena::Vocab::RDF.type.getURI )
|
53
|
+
@m.setNsPrefix( "rdf", Jena::Vocab::RDF.getURI )
|
54
|
+
assert_equal [@r1], @r0.property_values( "rdf:type" )
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_property_values_with_explicit_model
|
58
|
+
@m.setNsPrefix( "rdf", Jena::Vocab::RDF.getURI )
|
59
|
+
|
60
|
+
# r is not attached to any model
|
61
|
+
r = Jena::Core::ResourceFactory.createResource( @r0.getURI )
|
62
|
+
|
63
|
+
assert_equal [], r.property_values( Jena::Vocab::RDF.type )
|
64
|
+
assert_equal [], r.property_values( Jena::Vocab::RDF.type.getURI )
|
65
|
+
assert_equal [], r.property_values( "rdf:type" )
|
66
|
+
|
67
|
+
assert_equal [@r1], r.property_values( Jena::Vocab::RDF.type, @m )
|
68
|
+
assert_equal [@r1], r.property_values( Jena::Vocab::RDF.type.getURI, @m )
|
69
|
+
assert_equal [@r1], r.property_values( "rdf:type", @m )
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
$LOAD_PATH.push "#{File.dirname(__FILE__)}/../lib"
|
2
|
+
require "rubygems"
|
3
|
+
require "test/unit"
|
4
|
+
require "jena_jruby"
|
5
|
+
|
6
|
+
class QueryUtilsTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@m = Jena::Core::ModelFactory.createDefaultModel
|
10
|
+
@r0 = @m.createResource "http://example.org/foo#r0"
|
11
|
+
@m.add @r0, Jena::Vocab::RDFS.comment, "this is a test"
|
12
|
+
end
|
13
|
+
|
14
|
+
def teardown
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_as_prefix_map_0
|
18
|
+
pm = com.hp.hpl.jena.shared.impl.PrefixMappingImpl.new
|
19
|
+
assert_same pm, Jena::Util.as_prefix_map( pm )
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_as_prefix_map_1
|
23
|
+
map = {"foo" => "http://foo.bar"}
|
24
|
+
pm = Jena::Util.as_prefix_map map
|
25
|
+
assert_equal "http://foo.bar", pm.getNsPrefixURI( "foo" )
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_as_bindings_0
|
29
|
+
qsm = Jena::Query::QuerySolutionMap.new
|
30
|
+
assert_same qsm, Jena::Query.as_bindings( qsm )
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_as_bindings_1
|
34
|
+
r = Jena::Core::ResourceFactory.createResource "http://foo.bar#"
|
35
|
+
b = {"r" => r}
|
36
|
+
assert_equal r, Jena::Query.as_bindings( b ).get( "r" )
|
37
|
+
end
|
38
|
+
|
39
|
+
# Generic form of select
|
40
|
+
def test_select_0
|
41
|
+
result = Jena::Query.select @m, "select * {?s ?p ?o}", nil
|
42
|
+
assert_equal 1, result.size
|
43
|
+
|
44
|
+
assert_equal @r0, result[0][:s]
|
45
|
+
assert_equal Jena::Vocab::RDFS.comment, result[0][:p]
|
46
|
+
assert_equal "this is a test", result[0][:o].getLexicalForm
|
47
|
+
end
|
48
|
+
|
49
|
+
# Project variables from select
|
50
|
+
def test_select_1
|
51
|
+
result = Jena::Query.select @m, "select * {?s ?p ?o}", nil, "s"
|
52
|
+
assert_equal 1, result.size
|
53
|
+
|
54
|
+
assert_equal @r0, result[0][:s]
|
55
|
+
assert ! result[0].has_key?( :p )
|
56
|
+
assert ! result[0].has_key?( :o )
|
57
|
+
|
58
|
+
result = Jena::Query.select @m, "select * {?s ?p ?o}", nil, :s
|
59
|
+
assert_equal 1, result.size
|
60
|
+
|
61
|
+
assert_equal @r0, result[0][:s]
|
62
|
+
end
|
63
|
+
|
64
|
+
# Use namespaces in query select
|
65
|
+
def test_select_2
|
66
|
+
ns = {:foo => "http://example.org/foo#"}
|
67
|
+
|
68
|
+
result = Jena::Query.select @m, "select * {foo:r0 ?p ?o}", {:ns => ns}
|
69
|
+
assert_equal 1, result.size
|
70
|
+
|
71
|
+
assert ! result[0].has_key?( :s )
|
72
|
+
assert_equal Jena::Vocab::RDFS.comment, result[0][:p]
|
73
|
+
assert_equal "this is a test", result[0][:o].getLexicalForm
|
74
|
+
end
|
75
|
+
|
76
|
+
# Use variable bindings in query select
|
77
|
+
def test_select_3
|
78
|
+
b = {:s => @r0}
|
79
|
+
|
80
|
+
r1 = @m.createResource "http://example.org/foo#r1"
|
81
|
+
@m.add r1, Jena::Vocab::RDFS.comment, "this is another test"
|
82
|
+
|
83
|
+
result = Jena::Query.select @m, "select * {?s ?p ?o}", {:bindings => b}
|
84
|
+
assert_equal 1, result.size
|
85
|
+
|
86
|
+
assert_equal @r0, result[0][:s]
|
87
|
+
end
|
88
|
+
|
89
|
+
# Get only one result
|
90
|
+
def test_select_first
|
91
|
+
@m.add @r0, Jena::Vocab::RDFS.comment, "this is another test"
|
92
|
+
|
93
|
+
result = Jena::Query.select @m, "select * {?s ?p ?o}"
|
94
|
+
assert_equal 2, result.size
|
95
|
+
|
96
|
+
result = Jena::Query.select @m, "select * {?s ?p 42}"
|
97
|
+
assert_equal 0, result.size
|
98
|
+
|
99
|
+
result = Jena::Query.select_first @m, "select * {?s ?p ?o}"
|
100
|
+
assert_equal @r0, result[:s]
|
101
|
+
|
102
|
+
result = Jena::Query.select_first @m, "select * {?s ?p 42}"
|
103
|
+
assert_nil result
|
104
|
+
end
|
105
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jena-jruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.3.0
|
6
|
+
platform: java
|
7
|
+
authors:
|
8
|
+
- Ian Dickinson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-01-05 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: haml
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: builder
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
description: A simple packaging of Apache Jena for JRuby
|
38
|
+
email:
|
39
|
+
- ian@epimorphics.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files:
|
45
|
+
- README.md
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- Gemfile.lock
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- bin/update_jars_maven
|
54
|
+
- bin/update_jena
|
55
|
+
- jena-jruby.gemspec
|
56
|
+
- lib/jena_jruby.rb
|
57
|
+
- lib/jena_jruby/jars.rb
|
58
|
+
- lib/jena_jruby/namespace.rb
|
59
|
+
- lib/jena_jruby/node_utils.rb
|
60
|
+
- lib/jena_jruby/query_utils.rb
|
61
|
+
- lib/jena_jruby/utils.rb
|
62
|
+
- lib/jena_jruby/version.rb
|
63
|
+
- log4j.xml
|
64
|
+
- test/node_utils_test.rb
|
65
|
+
- test/query_utils_test.rb
|
66
|
+
- javalib/commons-codec-1.5.jar
|
67
|
+
- javalib/jena-iri-0.9.0-incubating.jar
|
68
|
+
- javalib/jena-tdb-0.9.0-incubating-SNAPSHOT.jar
|
69
|
+
- javalib/jena-core-2.7.0-incubating.jar
|
70
|
+
- javalib/httpclient-4.1.2.jar
|
71
|
+
- javalib/xercesImpl-2.10.0.jar
|
72
|
+
- javalib/slf4j-api-1.6.4.jar
|
73
|
+
- javalib/junit-4.9.jar
|
74
|
+
- javalib/slf4j-log4j12-1.6.4.jar
|
75
|
+
- javalib/icu4j-3.4.4.jar
|
76
|
+
- javalib/xml-apis-1.4.01.jar
|
77
|
+
- javalib/log4j-1.2.16.jar
|
78
|
+
- javalib/hamcrest-core-1.1.jar
|
79
|
+
- javalib/log4j-config.jar
|
80
|
+
- javalib/httpcore-4.1.3.jar
|
81
|
+
- javalib/jena-arq-2.9.0-incubating.jar
|
82
|
+
homepage: ""
|
83
|
+
licenses: []
|
84
|
+
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
- javalib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: "0"
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: "0"
|
103
|
+
requirements: []
|
104
|
+
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 1.8.13
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: JRuby wrapper for Apache Jena
|
110
|
+
test_files: []
|
111
|
+
|
112
|
+
has_rdoc:
|