Class: RdfContext::BNode

Inherits:
Resource show all
Defined in:
lib/rdf_context/bnode.rb

Overview

The BNode class creates RDF blank nodes.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Resource

#graph?, #literal?, #resource?, #uri?

Constructor Details

- (BNode) initialize(identifier = nil, context = {})

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

- (Object) identifier

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

+ (Object) parse(str)

Parse a BNode



31
32
33
# File 'lib/rdf_context/bnode.rb', line 31

def self.parse(str)
  BNode.new($1) if str =~ /^_:(.*)$/
end

Instance Method Details

- (Object) <=>(other)



86
87
88
# File 'lib/rdf_context/bnode.rb', line 86

def <=>(other)
  self.to_s <=> other.to_s
end

- (Boolean) bnode?

Returns `true`



39
40
41
# File 'lib/rdf_context/bnode.rb', line 39

def bnode?
  true
end

- (Boolean) eql?(other) 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

- (Object) generate_bn_identifier(name = nil) (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

- (Object) hash

Needed for uniq



91
# File 'lib/rdf_context/bnode.rb', line 91

def hash; self.to_s.hash; end

- (Object) inspect



93
94
95
# File 'lib/rdf_context/bnode.rb', line 93

def inspect
  "#{self.class}[#{self.to_n3}]"
end

- (String) to_n3 Also known as: to_ntriples

Exports the BNode in N-Triples form.

Example

b = BNode.new; b.to_n3  # => returns a string of the BNode in n3 form

Author:

  • Tom Morris



57
58
59
# File 'lib/rdf_context/bnode.rb', line 57

def to_n3
  "_:#{self.identifier}"
end

- (Object) to_s

Return BNode identifier



44
45
46
# File 'lib/rdf_context/bnode.rb', line 44

def to_s
  return self.identifier.to_s
end

- (Boolean) valid_id?(name) (protected)



98
99
100
# File 'lib/rdf_context/bnode.rb', line 98

def valid_id?(name)
  NC_REGEXP.match(name) || name.empty?
end

- (Object) xml_args

Output URI as resource reference for RDF/XML

Example

b = BNode.new("foo"); b.xml_args  # => [{"rdf:nodeID" => "foo"}]


66
67
68
# File 'lib/rdf_context/bnode.rb', line 66

def xml_args
  [{"rdf:nodeID" => self.identifier}]
end