Class: RdfaParser::BNode

Inherits:
Object
  • Object
show all
Defined in:
lib/rdfa_parser/bnode.rb

Overview

The BNode class creates RDF blank nodes.

Constant Summary collapse

@@next_generated =
"a"
@@named_nodes =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier = nil) ⇒ BNode

Create a new BNode, optionally accept a identifier for the BNode. Otherwise, generated sequentially



8
9
10
11
12
13
14
15
16
17
# File 'lib/rdfa_parser/bnode.rb', line 8

def initialize(identifier = nil)
  if identifier != nil && self.valid_id?(identifier)
    # Generate a name if it's blank
    @identifier = (@@named_nodes[identifier] ||= identifier.to_s.length > 0 ? identifier : self )
  else
    # Don't actually allocate the name until it's used, to save generation space
    # (and make checking test cases easier)
    @identifier = self
  end
end

Class Method Details

.reset(init = "a") ⇒ Object

Start identifier sequence from scratch



82
83
84
85
# File 'lib/rdfa_parser/bnode.rb', line 82

def self.reset(init = "a")
  @@next_generated = init
  @@named_nodes = {}
end

Instance Method Details

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

Returns:

  • (Boolean)


73
74
75
# File 'lib/rdfa_parser/bnode.rb', line 73

def eql?(other)
  other.is_a?(self.class) && self.identifier == other.identifier
end

#hashObject

Needed for uniq



79
# File 'lib/rdfa_parser/bnode.rb', line 79

def hash; to_s.hash; end

#identifierObject

The identifier used used for this BNode. Not evaluated until this is called, which means that BNodes that are never used in a triple won’t polute the sequence.



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rdfa_parser/bnode.rb', line 60

def identifier
  return @identifier unless @identifier.is_a?(BNode)
  if @identifier.equal?(self)
    # Generate from the sequence a..zzz, unless already taken
    @@next_generated = @@next_generated.succ while @@named_nodes.has_key?(@@next_generated)
    @identifier, @@next_generated = @@next_generated, @@next_generated.succ
  else
    # Previously allocated node
    @identifier = @identifier.identifier
  end
  @identifier
end

#to_n3String

Exports the BNode in N-Triples form.

Example

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

Returns

Returns:

  • (String)

    The BNode in n3.

Author:

  • Tom Morris



34
35
36
# File 'lib/rdfa_parser/bnode.rb', line 34

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

#to_ntriplesString

Exports the BNode in N-Triples form.

Example

b = BNode.new; b.to_ntriples  # => returns a string of the BNode in N-Triples form

Returns

Returns:

  • (String)

    The BNode in N-Triples.

Author:

  • Tom Morris



49
50
51
# File 'lib/rdfa_parser/bnode.rb', line 49

def to_ntriples
  self.to_n3
end

#to_sObject



19
20
21
# File 'lib/rdfa_parser/bnode.rb', line 19

def to_s
  return self.identifier.to_s
end

#xml_argsObject

Output URI as resource reference for RDF/XML



54
55
56
# File 'lib/rdfa_parser/bnode.rb', line 54

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