lib/rdf/isomorphic.rb in rdf-isomorphic-1.0.1 vs lib/rdf/isomorphic.rb in rdf-isomorphic-1.0.2
- old
+ new
@@ -7,11 +7,11 @@
##
# Isomorphism for rdf.rb Enumerables
#
# RDF::Isomorphic provides the functions isomorphic_with and bijection_to for RDF::Enumerable.
#
- # @see http://ruby-rdf.github.com/rdf
+ # @see http://rdf.rubyforge.org
# @see http://www.hpl.hp.com/techreports/2001/HPL-2001-293.pdf
module Isomorphic
autoload :VERSION, 'rdf/isomorphic/version'
# Returns `true` if this RDF::Enumerable is isomorphic with another.
@@ -157,18 +157,14 @@
bijection
end
# Blank nodes appearing in given list of statements
# @private
- # @return [RDF::Node]
+ # @param [Array<RDF::Statement>] blank_stmt_list
+ # @return [Array<RDF::Node>]
def self.blank_nodes_in(blank_stmt_list)
- nodes = []
- blank_stmt_list.each do | statement |
- nodes << statement.object if statement.object.node?
- nodes << statement.subject if statement.subject.node?
- end
- nodes.uniq
+ blank_stmt_list.map {|statement | statement.to_quad.compact.select(&:node?)}.flatten.uniq
end
# Given a set of statements, create a mapping of node => SHA1 for a given
# set of blank nodes. grounded_hashes is a mapping of node => SHA1 pairs
# that we will take as a given, and use those to make more specific
@@ -228,18 +224,22 @@
# member in the given hash.
#
# Returns a tuple consisting of grounded being true or false and the String
# for the hash
# @private
+ # @param [RDF::Node] node
+ # @param [Array<RDF::Statement>] statements
+ # @param [Hash] hashes
+ # @param [Boolean] canonicalize
# @return [Boolean, String]
def self.node_hash_for(node, statements, hashes, canonicalize)
statement_signatures = []
grounded = true
statements.each do | statement |
- if (statement.object == node) || (statement.subject == node)
+ if statement.to_quad.include?(node)
statement_signatures << hash_string_for(statement, hashes, node, canonicalize)
- [statement.subject, statement.object].each do | resource |
+ statement.to_quad.compact.each do | resource |
grounded = false unless grounded?(resource, hashes) || resource == node
end
end
end
# Note that we sort the signatures--without a canonical ordering,
@@ -250,15 +250,11 @@
# Provide a string signature for the given statement, collecting
# string signatures for grounded node elements.
# return [String]
# @private
def self.hash_string_for(statement, hashes, node, canonicalize)
- string = ""
- string << string_for_node(statement.subject, hashes, node, canonicalize)
- string << statement.predicate.to_s
- string << string_for_node(statement.object, hashes, node, canonicalize)
- string
+ statement.to_quad.map {|r| string_for_node(r, hashes, node, canonicalize)}.join("")
end
# Returns true if a given node is grounded
# A node is groundd if it is not a blank node or it is included
# in the given mapping of grounded nodes.
@@ -273,10 +269,12 @@
# nodes will return their hashed form.
# @return [String]
# @private
def self.string_for_node(node, hashes,target, canonicalize)
case
+ when node.nil?
+ ""
when node == target
"itself"
when node.node? && hashes.member?(node)
hashes[node]
when node.node?
@@ -294,9 +292,7 @@
# Extend RDF::Enumerables with these functions.
module Enumerable
include RDF::Isomorphic
end
-
- autoload :VERSION, 'rdf/isomorphic/version'
end