class RDF::Node

An RDF blank node, also known as an anonymous or unlabeled node.

@example Creating a blank node with an implicit identifier

bnode = RDF::Node.new

@example Creating a blank node with an UUID identifier

bnode = RDF::Node.uuid
bnode.to_s #=> "_:504c0a30-0d11-012d-3f50-001b63cac539"

Constants

CACHE_SIZE

Defines the maximum number of interned Node references that can be held cached in memory at any one time.

@note caching interned nodes means that two different invocations using the same symbol will result in the same node, which may not be appropriate depending on the graph from which it is used. RDF requires that bnodes with the same label are, in fact, different bnodes, unless they are used within the same document.

Attributes

id[RW]

@return [String]

original[RW]

Originally instantiated node, if any @return [RDF::Node]

Public Class Methods

cache() click to toggle source

@return [RDF::Util::Cache] @private

# File lib/rdf/model/node.rb, line 25
def self.cache
  require 'rdf/util/cache' unless defined?(::RDF::Util::Cache)
  @cache ||= RDF::Util::Cache.new(CACHE_SIZE)
end
intern(id) click to toggle source

Alias for `RDF::Node.new`, at the moment.

@private @param [#to_s] id @return [RDF::Node] @since 0.2.0

# File lib/rdf/model/node.rb, line 57
def self.intern(id)
  (cache[(id = id.to_s).to_sym] ||= self.new(id)).freeze
end
new(id = nil) click to toggle source

@param [#to_s] id

# File lib/rdf/model/node.rb, line 83
def initialize(id = nil)
  id = nil if id.to_s.empty?
  @id = (id || "g#{__id__.to_i.abs}").to_s.freeze
end
uuid(grammar: nil) click to toggle source

Returns a blank node with a random UUID-based identifier.

@param [Regexp] grammar (nil)

a grammar specification that the generated UUID must match

@return [RDF::Node]

# File lib/rdf/model/node.rb, line 36
def self.uuid(grammar: nil)
  case
    when grammar
      # The UUID is generated such that its initial part is guaranteed
      # to match the given `grammar`, e.g. `/^[A-Za-z][A-Za-z0-9]*/`.
      # Some RDF storage systems (e.g. AllegroGraph) require this.
      # @see http://github.com/bendiken/rdf/pull/43
      uuid = RDF::Util::UUID.generate(options) until uuid =~ grammar
    else
      uuid = RDF::Util::UUID.generate(options)
  end
  self.new(uuid)
end

Public Instance Methods

==(other) click to toggle source

Checks whether this blank node is equal to `other` (type checking).

In this case, different nodes having the same id are considered the same.

Per SPARQL data-r2/expr-equal/eq-2-2, numeric can't be compared with other types

@param [Object] other @return [Boolean] @see www.w3.org/TR/rdf-sparql-query/#func-RDFterm-equal

# File lib/rdf/model/node.rb, line 143
def ==(other)
  if other.is_a?(Literal)
    # If other is a Literal, reverse test to consolodate complex type checking logic
    other == self
  else
    other.respond_to?(:node?) && other.node? &&
      self.hash == other.to_term.hash &&
      other.respond_to?(:id) && @id == other.to_term.id
  end
end
Also aliased as: ===
===(other)
Alias for: ==
anonymous?() click to toggle source

Returns `true`.

@return [Boolean]

# File lib/rdf/model/node.rb, line 100
def anonymous?
  true
end
Also aliased as: unlabeled?
dup() click to toggle source

Override dup to remember original object. This allows .eql? to determine that two nodes are the same thing, and not different nodes instantiated with the same identifier. @return [RDF::Node]

Calls superclass method
# File lib/rdf/model/node.rb, line 67
def dup
  node = super
  node.original = self.original || self
  node
end
eql?(other) click to toggle source

Determines if `self` is the same term as `other`.

In this case, nodes must be the same object

@param [Node] other @return [Boolean]

# File lib/rdf/model/node.rb, line 129
def eql?(other)
  other.is_a?(RDF::Node) && (self.original || self).equal?(other.original || other)
end
hash() click to toggle source

Returns a hash code for this blank node.

@return [Fixnum]

# File lib/rdf/model/node.rb, line 118
def hash
  @id.hash
end
labeled?() click to toggle source

Returns `false`.

@return [Boolean]

# File lib/rdf/model/node.rb, line 110
def labeled?
  !unlabeled?
end
make_unique!() click to toggle source

Make this term identifier unique, if it is found to be shared with another node having the same identifier @return [self]

# File lib/rdf/model/node.rb, line 166
def make_unique!
  @id = to_unique_base[2..-1]
  self
end
node?() click to toggle source

Returns `true`.

@return [Boolean]

# File lib/rdf/model/node.rb, line 92
def node?
  true
end
to_s() click to toggle source

Returns a string representation of this blank node.

@return [String]

# File lib/rdf/model/node.rb, line 175
def to_s
  "_:%s" % @id.to_s
end
to_sym() click to toggle source

Returns a symbol representation of this blank node.

@return [Symbol] @since 0.2.0

# File lib/rdf/model/node.rb, line 184
def to_sym
  @id.to_s.to_sym
end
to_unique_base() click to toggle source

Returns a representation of this node independent of any identifier used to initialize it

@return [String]

# File lib/rdf/model/node.rb, line 159
def to_unique_base
  original ? original.to_unique_base :  "_:g#{__id__.to_i.abs}"
end
unlabeled?()
Alias for: anonymous?