Class: RDF::Node
Overview
An RDF blank node, also known as an anonymous or unlabeled node.
Constant Summary collapse
- 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
Instance Attribute Summary collapse
- #id ⇒ String
-
#original ⇒ RDF::Node
Originally instantiated node, if any.
Class Method Summary collapse
- .cache ⇒ RDF::Util::Cache
-
.intern(id) ⇒ RDF::Node
Alias for
RDF::Node.new, at the moment. -
.uuid(format: :default) ⇒ RDF::Node
Returns a blank node with a random UUID-based identifier.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #===)
Checks whether this blank node is equal to
other(type checking). -
#anonymous? ⇒ Boolean
(also: #unlabeled?)
Returns
true. -
#dup ⇒ RDF::Node
Override #dup to remember original object.
-
#eql?(other) ⇒ Boolean
Determines if
selfis the same term asother. -
#hash ⇒ Integer
Returns a hash code for this blank node.
-
#initialize(id = nil) ⇒ Node
constructor
A new instance of Node.
-
#labeled? ⇒ Boolean
Returns
false. -
#make_unique! ⇒ self
Make this term identifier unique, if it is found to be shared with another node having the same identifier.
-
#node? ⇒ Boolean
Returns
true. -
#to_s ⇒ String
Returns a string representation of this blank node.
-
#to_sym ⇒ Symbol
Returns a symbol representation of this blank node.
-
#to_unique_base ⇒ String
Returns a representation of this node independent of any identifier used to initialize it.
Methods included from Resource
Methods included from Term
#<=>, #compatible?, #term?, #to_base, #to_term
Methods included from Value
#canonicalize, #canonicalize!, #constant?, #graph?, #inspect, #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.
81 82 83 84 |
# File '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
#original ⇒ RDF::Node
Originally instantiated node, if any
74 75 76 |
# File 'lib/rdf/model/node.rb', line 74 def original @original end |
Class Method Details
.cache ⇒ RDF::Util::Cache
25 26 27 28 |
# 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) ⇒ RDF::Node
Alias for RDF::Node.new, at the moment.
55 56 57 |
# File 'lib/rdf/model/node.rb', line 55 def self.intern(id) (cache[(id = id.to_s).to_sym] ||= self.new(id)).freeze 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 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
43 44 45 46 |
# File '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
#==(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
141 142 143 144 145 146 147 148 149 150 |
# File '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.
98 99 100 |
# File 'lib/rdf/model/node.rb', line 98 def anonymous? true end |
#dup ⇒ RDF::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.
65 66 67 68 69 |
# File 'lib/rdf/model/node.rb', line 65 def dup node = super node.original = self.original || self node end |
#eql?(other) ⇒ Boolean
Determines if self is the same term as other.
In this case, nodes must be the same object
127 128 129 |
# File 'lib/rdf/model/node.rb', line 127 def eql?(other) other.is_a?(RDF::Node) && (self.original || self).equal?(other.original || other) end |
#hash ⇒ Integer
Returns a hash code for this blank node.
116 117 118 |
# File 'lib/rdf/model/node.rb', line 116 def hash @id.hash end |
#labeled? ⇒ Boolean
Returns false.
108 109 110 |
# File '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
164 165 166 167 |
# File 'lib/rdf/model/node.rb', line 164 def make_unique! @id = to_unique_base[2..-1] self end |
#node? ⇒ Boolean
Returns true.
90 91 92 |
# File 'lib/rdf/model/node.rb', line 90 def node? true end |
#to_s ⇒ String
Returns a string representation of this blank node.
173 174 175 |
# File 'lib/rdf/model/node.rb', line 173 def to_s "_:%s" % @id.to_s end |
#to_sym ⇒ Symbol
Returns a symbol representation of this blank node.
182 183 184 |
# File 'lib/rdf/model/node.rb', line 182 def to_sym @id.to_s.to_sym end |
#to_unique_base ⇒ String
Returns a representation of this node independent of any identifier used to initialize it
157 158 159 |
# File 'lib/rdf/model/node.rb', line 157 def to_unique_base original ? original.to_unique_base : "_:g#{__id__.to_i.abs}" end |