Class: RdfContext::BNode
Overview
The BNode class creates RDF blank nodes.
Instance Attribute Summary collapse
-
#identifier ⇒ Object
The identifier used used for this BNode.
Class Method Summary collapse
-
.parse(str) ⇒ Object
Parse a BNode.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#bnode? ⇒ Boolean
Returns ‘true`.
-
#eql?(other) ⇒ Boolean
(also: #==)
Compare BNodes.
-
#generate_bn_identifier(name = nil) ⇒ Object
protected
Generate a unique identifier (time with milliseconds plus generated increment).
-
#hash ⇒ Object
Needed for uniq.
-
#initialize(identifier = nil, context = {}) ⇒ BNode
constructor
Create a new BNode, optionally accept a identifier for the BNode.
- #inspect ⇒ Object
-
#to_n3 ⇒ String
(also: #to_ntriples)
Exports the BNode in N-Triples form.
-
#to_s ⇒ Object
Return BNode identifier.
- #valid_id?(name) ⇒ Boolean protected
-
#xml_args ⇒ Object
Output URI as resource reference for RDF/XML.
Methods inherited from Resource
#graph?, #literal?, #resource?, #uri?
Constructor Details
#initialize(identifier = nil, context = {}) ⇒ BNode
Create a new BNode, optionally accept a identifier for the BNode. Otherwise, generated sequentially.
A BNode may have a bank (empty string) identifier, which will be equivalent to another blank identified BNode.
Identifiers only have meaning within a particular parsing context, and are used to lookup previoiusly defined BNodes using the same identifier. Names are not preserved within the underlying storage model.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rdf_context/bnode.rb', line 18 def initialize(identifier = nil, context = {}) if identifier.nil? @identifier = generate_bn_identifier elsif identifier.match(/n?bn\d+[a-z]+(N\w+)?$/) @identifier = context[identifier] || identifier elsif self.valid_id?(identifier) @identifier = context[identifier] ||= generate_bn_identifier(identifier) else @identifier = generate_bn_identifier end end |
Instance Attribute Details
#identifier ⇒ Object
The identifier used used for this BNode.
71 72 73 |
# File 'lib/rdf_context/bnode.rb', line 71 def identifier @identifier end |
Class Method Details
Instance Method Details
#<=>(other) ⇒ Object
86 87 88 |
# File 'lib/rdf_context/bnode.rb', line 86 def <=>(other) self.to_s <=> other.to_s end |
#bnode? ⇒ Boolean
Returns ‘true`
39 40 41 |
# File 'lib/rdf_context/bnode.rb', line 39 def bnode? true end |
#eql?(other) ⇒ Boolean Also known as: ==
Compare BNodes. BNodes are equivalent if they have the same identifier
76 77 78 79 80 81 82 83 |
# File 'lib/rdf_context/bnode.rb', line 76 def eql?(other) case other when BNode other.identifier == self.identifier else self.identifier == other.to_s end end |
#generate_bn_identifier(name = nil) ⇒ Object (protected)
Generate a unique identifier (time with milliseconds plus generated increment)
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rdf_context/bnode.rb', line 103 def generate_bn_identifier(name = nil) @@base ||= "bn#{(Time.now.to_f * 1000).to_i}" @@next_generated ||= "a" if name bn = "n#{@@base}#{@@next_generated}N#{name}" else bn = "#{@@base}#{@@next_generated}" end @@next_generated = @@next_generated.succ bn end |
#hash ⇒ Object
Needed for uniq
91 |
# File 'lib/rdf_context/bnode.rb', line 91 def hash; self.to_s.hash; end |
#inspect ⇒ Object
93 94 95 |
# File 'lib/rdf_context/bnode.rb', line 93 def inspect "#{self.class}[#{self.to_n3}]" end |
#to_n3 ⇒ String Also known as: to_ntriples
57 58 59 |
# File 'lib/rdf_context/bnode.rb', line 57 def to_n3 "_:#{self.identifier}" end |
#to_s ⇒ Object
Return BNode identifier
44 45 46 |
# File 'lib/rdf_context/bnode.rb', line 44 def to_s return self.identifier.to_s end |
#valid_id?(name) ⇒ Boolean (protected)
98 99 100 |
# File 'lib/rdf_context/bnode.rb', line 98 def valid_id?(name) NC_REGEXP.match(name) || name.empty? end |