Class: RDF::Node

Inherits:
Object
  • Object
show all
Includes:
Resource
Defined in:
lib/rdf/model/node.rb

Overview

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

Examples:

Creating a blank node with an implicit identifier

bnode = RDF::Node.new

Creating a blank node with an UUID identifier

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

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Resource

new, #resource?

Methods included from Term

#<=>, #constant?, #variable?

Methods included from Value

#graph?, #inspect, #inspect!, #iri?, #literal?, #resource?, #statement?, #to_ntriples, #to_rdf, #uri?, #variable?

Constructor Details

- (Node) initialize(id = nil)

A new instance of Node

Parameters:

  • (#to_s) id (defaults to: nil)


52
53
54
# File 'lib/rdf/model/node.rb', line 52

def initialize(id = nil)
  @id = (id || "g#{__id__.to_i.abs}").to_s
end

Instance Attribute Details

- (String) id

Returns:

  • (String)


48
49
50
# File 'lib/rdf/model/node.rb', line 48

def id
  @id
end

Class Method Details

+ (RDF::Node) uuid(options = {})

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

Parameters:

  • (Hash{Symbol => Object}) options (defaults to: {})

Options Hash (options):

  • (Regexp) :grammar — default: nil

    a grammar specification that the generated UUID must match

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rdf/model/node.rb', line 22

def self.uuid(options = {})
  case
    when options[: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 =~ options[:grammar]
    else
      uuid = RDF::Util::UUID.generate(options)
  end
  self.new(uuid)
end

Instance Method Details

- (Boolean) ==(other)

Checks whether this blank node is equal to other.

Parameters:

  • (Object) other

Returns:

  • (Boolean)


104
105
106
107
# File 'lib/rdf/model/node.rb', line 104

def ==(other)
  other.respond_to?(:node?) && other.node? &&
    other.respond_to?(:id) && @id == other.id
end

- (Boolean) anonymous? Also known as: unlabeled?

Returns true.

Returns:

  • (Boolean)


68
69
70
# File 'lib/rdf/model/node.rb', line 68

def anonymous?
  true
end

- (Boolean) eql?(other)

Checks whether this blank node is equal to other.

Parameters:

Returns:

  • (Boolean)


95
96
97
# File 'lib/rdf/model/node.rb', line 95

def eql?(other)
  other.is_a?(Node) && self == other
end

- (Fixnum) hash

Returns a hash code for this blank node.

Returns:

  • (Fixnum)


86
87
88
# File 'lib/rdf/model/node.rb', line 86

def hash
  @id.hash
end

- (Boolean) labeled?

Returns false.

Returns:

  • (Boolean)


78
79
80
# File 'lib/rdf/model/node.rb', line 78

def labeled?
  !unlabeled?
end

- (Boolean) node?

Returns true.

Returns:

  • (Boolean)


60
61
62
# File 'lib/rdf/model/node.rb', line 60

def node?
  true
end

- (String) to_s

Returns a string representation of this blank node.

Returns:

  • (String)


113
114
115
# File 'lib/rdf/model/node.rb', line 113

def to_s
  "_:%s" % @id.to_s
end

- (Symbol) to_sym

Returns a symbol representation of this blank node.

Returns:

  • (Symbol)

Since:

  • 0.2.0



122
123
124
# File 'lib/rdf/model/node.rb', line 122

def to_sym
  @id.to_s.to_sym
end