Permalink
Browse files

Adding some basic RDFNode utilities

  • Loading branch information...
1 parent 325ba6d commit f986fbefeef2dbfb0e6f72327cf1603ac2a43d39 @ijdickinson committed Dec 6, 2011
Showing with 93 additions and 8 deletions.
  1. +9 −7 lib/jruby_jena.rb
  2. +61 −0 lib/jruby_jena/node_utils.rb
  3. +23 −0 test/node_utils_test.rb
  4. +0 −1 test/query_utils_test.rb
View
@@ -1,10 +1,5 @@
require 'java'
-%w[
- jars
- version
- utils
- query_utils
-].each {|f| require "jruby_jena/#{f}"}
+require 'jruby_jena/jars'
# Short names for commonly used Java packages
module Jena
@@ -175,4 +170,11 @@ module TDB
java_import com.hp.hpl.jena.tdb.TDBLoader
java_import com.hp.hpl.jena.tdb.TDBFactory
end
-end
+end
+
+%w[
+ version
+ utils
+ query_utils
+ node_utils
+].each {|f| require "jruby_jena/#{f}"}
@@ -0,0 +1,61 @@
+module Jena
+ # A collection of utilities for manipulating Jena RDF Resources
+ # and Literals, collectively known as RDFNodes.
+ module Node
+ # Return the types of a node, which will be an array of the RDF types
+ # for a resource, or an array of the (zero or one) datatypes if the
+ # node is a literal. Ensures that the returned array includes the default_type
+ # f default_type is non-nil
+ def self.types( node, default_type = nil )
+ return self.resource_types( node, default_type ) if node.resource?
+ self.literal_types( node, default_type )
+ end
+
+ # Return the values of `rdf:type` for the given resource
+ def self.resource_types( node, default_type = nil )
+ types = []
+ node.each_property( Jena::Vocab::RDF.type ) {|stmt| types << stmt.getObject}
+ with_default_type( types, default_type )
+ end
+
+ # Return an array of the datatype of the given literal, if defined,
+ # and including the default_type if non-nil
+ def self.literal_types( node, default_type = nil )
+ types = node.getDatatype || []
+ with_default_type( types, default_type )
+ end
+
+ :private
+
+ def self.with_default_type( types, default_type )
+ types << default_type if default_type && !types.include?( default_type )
+ types
+ end
+ end
+end
+
+
+# Add some Ruby-friendly method names to common Jena classes
+module Java
+ module ComHpHplJenaEnhanced
+ class EnhNode
+ alias resource? isResource
+ alias literal? isLiteral
+
+ def types
+ Jena::Node.types self
+ end
+ end
+ end
+
+ module ComHpHplJenaRdfModelImpl
+ class ResourceImpl
+ # Yield to the given block for each statement of this resource with predicate
+ # `property`. If `property` is nil, yield to the block for every statement whose
+ # subject is this resource.
+ def each_property( property = nil, &block )
+ listProperties( property ).each( &block )
+ end
+ end
+ end
+end
View
@@ -0,0 +1,23 @@
+$LOAD_PATH.push "#{File.dirname(__FILE__)}/../lib"
+require "rubygems"
+require "test/unit"
+require "jruby_jena"
+
+class NodeUtilsTest < Test::Unit::TestCase
+
+ def setup
+ @m = Jena::Core::ModelFactory.createDefaultModel
+ @r0 = @m.createResource "http://example.org/foo#r0"
+ @r1 = @m.createResource "http://example.org/foo#r1"
+ @m.add @r0, Jena::Vocab::RDFS.comment, "this is a test"
+ @m.add @r0, Jena::Vocab::RDF.type, @r1
+ end
+
+ def teardown
+ end
+
+ def test_resource_types
+ assert_equal [@r1], @r0.types
+ assert_equal [], @r1.types
+ end
+end
View
@@ -1,7 +1,6 @@
$LOAD_PATH.push "#{File.dirname(__FILE__)}/../lib"
require "rubygems"
require "test/unit"
-#require "test/unit/runner/tk"
require "jruby_jena"
class QueryUtilsTest < Test::Unit::TestCase

0 comments on commit f986fbe

Please sign in to comment.