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_quad, #to_rdf, #type_error, #uri?, #variable?

Constructor Details

#initialize(id = nil) ⇒ Node

Returns a new instance of Node.

Parameters:

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


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

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

Instance Attribute Details

#idString

Returns:

  • (String)


65
66
67
# File 'lib/rdf/model/node.rb', line 65

def id
  @id
end

#originalRDF::Node

Originally instantiated node, if any

Returns:



62
63
64
# File 'lib/rdf/model/node.rb', line 62

def original
  @original
end

Class Method Details

.intern(id) ⇒ RDF::Node

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

Parameters:

Returns:

Since:

  • 0.2.0



43
44
45
# File 'lib/rdf/model/node.rb', line 43

def self.intern(id)
  self.new(id)
end

.uuid(options = {}) ⇒ RDF::Node

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

Parameters:

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

Options Hash (options):

  • :grammar (Regexp) — 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

#==(other) ⇒ Boolean Also known as: ===

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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

See Also:



128
129
130
131
132
133
134
135
136
# File 'lib/rdf/model/node.rb', line 128

def ==(other)
  case other
  when Literal
    # If other is a Literal, reverse test to consolodate complex type checking logic
    other == self
  else 
    other.respond_to?(:node?) && other.node? && other.respond_to?(:id) && @id == other.id
  end
end

#anonymous?Boolean Also known as: unlabeled?

Returns ‘true`.

Returns:

  • (Boolean)


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

def anonymous?
  true
end

#dupRDF::Node

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.

Returns:



53
54
55
56
57
# File 'lib/rdf/model/node.rb', line 53

def dup
  node = super
  node.original = self.original || self
  node
end

#eql?(other) ⇒ Boolean

Determins if ‘self` is the same term as `other`.

In this case, nodes must be the same object

Parameters:

Returns:

  • (Boolean)


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

def eql?(other)
  other.is_a?(RDF::Node) && (self.original || self).equal?(other.original || other)
end

#hashFixnum

Returns a hash code for this blank node.

Returns:

  • (Fixnum)


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

def hash
  @id.hash
end

#labeled?Boolean

Returns ‘false`.

Returns:

  • (Boolean)


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

def labeled?
  !unlabeled?
end

#node?Boolean

Returns ‘true`.

Returns:

  • (Boolean)


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

def node?
  true
end

#to_sString

Returns a string representation of this blank node.

Returns:

  • (String)


143
144
145
# File 'lib/rdf/model/node.rb', line 143

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

#to_symSymbol

Returns a symbol representation of this blank node.

Returns:

  • (Symbol)

Since:

  • 0.2.0



152
153
154
# File 'lib/rdf/model/node.rb', line 152

def to_sym
  @id.to_s.to_sym
end