Class: RDF::Node

Inherits:
Object show all
Includes:
Resource
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb,
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-reasoner-9fa5bbe5866d/lib/rdf/reasoner/extensions.rb,
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-spec-9aa20fb58f9a/lib/rdf/spec/inspects.rb,
vendor/bundler/ruby/2.5.0/bundler/gems/json-ld-10f0d539c024/lib/json/ld/extensions.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"

Constant Summary

CACHE_SIZE =
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.

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

-1 # unlimited by default
@@entailments =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

new, #resource?

Methods included from Term

#<=>, #aggregate?, #compatible?, #escape, #evaluate, #ndvars, #term?, #to_base, #to_term, #vars

Methods included from SPARQL::Algebra::Expression

cast, #constant?, #evaluate, extension, extensions, for, #invalid?, new, open, #optimize, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Methods included from Value

#canonicalize, #canonicalize!, #constant?, #graph?, #inspect!, #invalid?, #iri?, #list?, #literal?, #resource?, #start_with?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #valid?, #validate!, #variable?

Constructor Details

#initialize(id = nil) ⇒ Node

Returns a new instance of Node

Parameters:

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


81
82
83
84
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 81

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

Instance Attribute Details

#idString

Returns:



77
78
79
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 77

def id
  @id
end

#originalRDF::Node

Originally instantiated node, if any

Returns:



74
75
76
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 74

def original
  @original
end

Class Method Details

.add_entailment(method, proc) ⇒ Object

Add an entailment method. The method accepts no arguments, and returns or yields an array of values associated with the particular entailment method

Parameters:



68
69
70
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-reasoner-9fa5bbe5866d/lib/rdf/reasoner/extensions.rb', line 68

def add_entailment(method, proc)
  @@entailments[method] = proc
end

.uuid(format: :default) ⇒ RDF::Node

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

(Depends on availability of either uuid or uuidtools gems).

Formats supported by the UUID generator:

  • :default Produces 36 characters, including hyphens separating the UUID value parts
  • :compact Produces a 32 digits (hexadecimal) value with no hyphens
  • :urn Adds the prefix urn:uuid: to the default format

Requires that the uuid gem be loadable to use format

Parameters:

  • format (:default, :compact)

    (:default)

Returns:



43
44
45
46
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 43

def self.uuid(format: :default)
  uuid = RDF::Util::UUID.generate(format: format)
  self.new(uuid)
end

Instance Method Details

#+(value) ⇒ Object

Odd case of appending to a BNode identifier



6
7
8
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/json-ld-10f0d539c024/lib/json/ld/extensions.rb', line 6

def +(value)
  Node.new(id + value.to_s)
end

#==(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:

Returns:

  • (Boolean)

See Also:



141
142
143
144
145
146
147
148
149
150
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 141

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

#anonymous?Boolean Also known as: unlabeled?

Returns true.

Returns:

  • (Boolean)


98
99
100
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 98

def anonymous?
  true
end

#domain_compatible?(resource, queryable, options = {}) ⇒ Boolean

Determine if the domain of a property term is consistent with the specified resource in queryable.

Parameters:

Options Hash (options):

Returns:

  • (Boolean)


92
93
94
95
96
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-reasoner-9fa5bbe5866d/lib/rdf/reasoner/extensions.rb', line 92

def domain_compatible?(resource, queryable, options = {})
  %w(owl rdfs schema).map {|r| "domain_compatible_#{r}?".to_sym}.all? do |meth|
    !self.respond_to?(meth) || self.send(meth, resource, queryable, options)
  end
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:



65
66
67
68
69
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 65

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

#entail(method) {|term| ... } ⇒ Array<Term>

Perform an entailment on this term.

Parameters:

  • method (Symbol)

    A registered entailment method

Yields:

  • term

Yield Parameters:

Returns:



80
81
82
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-reasoner-9fa5bbe5866d/lib/rdf/reasoner/extensions.rb', line 80

def entail(method, &block)
  self.send(@@entailments.fetch(method), &block)
end

#eql?(other) ⇒ Boolean

Determines if self is the same term as other.

In this case, nodes must be the same object

Parameters:

Returns:

  • (Boolean)


127
128
129
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 127

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

#hashInteger

Returns a hash code for this blank node.

Returns:



116
117
118
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 116

def hash
  @id.hash
end

#inspectObject



17
18
19
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-spec-9aa20fb58f9a/lib/rdf/spec/inspects.rb', line 17

def inspect
  "RDF::Node(#{to_base})"
end

#labeled?Boolean

Returns false.

Returns:

  • (Boolean)


108
109
110
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 108

def labeled?
  !unlabeled?
end

#make_unique!self

Make this term identifier unique, if it is found to be shared with another node having the same identifier

Returns:

  • (self)


164
165
166
167
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 164

def make_unique!
  @id = to_unique_base[2..-1]
  self
end

#node?Boolean

Returns true.

Returns:

  • (Boolean)


90
91
92
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 90

def node?
  true
end

#range_compatible?(resource, queryable, options = {}) ⇒ Boolean

Determine if the range of a property term is consistent with the specified resource in queryable.

Specific entailment regimes should insert themselves before this to apply the appropriate semantic condition

Parameters:

Options Hash (options):

Returns:

  • (Boolean)


108
109
110
111
112
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-reasoner-9fa5bbe5866d/lib/rdf/reasoner/extensions.rb', line 108

def range_compatible?(resource, queryable, options = {})
  %w(owl rdfs schema).map {|r| "range_compatible_#{r}?".to_sym}.all? do |meth|
    !self.respond_to?(meth) || self.send(meth, resource, queryable, options)
  end
end

#to_sString

Returns a string representation of this blank node.

Returns:



173
174
175
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 173

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

#to_symSymbol

Returns a symbol representation of this blank node.

Returns:

Since:

  • 0.2.0



182
183
184
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 182

def to_sym
  @id.to_s.to_sym
end

#to_unique_baseString

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

Returns:



157
158
159
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/model/node.rb', line 157

def to_unique_base
  original ? original.to_unique_base :  "_:g#{__id__.to_i.abs}"
end