Class: Reddy::BNode

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

Overview

The BNode class creates RDF blank nodes.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • identifier:: (String)

    Legal NCName or nil for a named BNode

  • context:: (Hash)

    Context used to store named BNodes



20
21
22
23
24
25
26
27
28
# File 'lib/reddy/bnode.rb', line 20

def initialize(identifier = nil, context = {})
  if identifier != nil && self.valid_id?(identifier)
    identifier = identifier.sub(/nbn\d+[a-z]+N/, '')  # creating a named BNode from a named BNode
    # Generate a name if it's blank. Always prepend "named" to avoid generation overlap
    @identifier = context[identifier] ||= generate_bn_identifier(identifier)
  else
    @identifier = generate_bn_identifier
  end
end

Instance Attribute Details

#identifierObject (readonly)

The identifier used used for this BNode.



65
66
67
# File 'lib/reddy/bnode.rb', line 65

def identifier
  @identifier
end

Instance Method Details

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

Compare BNodes. BNodes are equivalent if they have the same identifier

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/reddy/bnode.rb', line 70

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

#hashObject

Needed for uniq



77
# File 'lib/reddy/bnode.rb', line 77

def hash; self.to_s.hash; end

#inspectObject



79
80
81
# File 'lib/reddy/bnode.rb', line 79

def inspect
  "[bn:#{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:

  • (String)

    The BNode in n3.



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

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

#to_ntriplesObject

Exports the BNode in N-Triples form.

Syonym for to_n3



52
53
54
# File 'lib/reddy/bnode.rb', line 52

def to_ntriples
  self.to_n3
end

#to_sObject

Return BNode identifier



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

def to_s
  return self.identifier.to_s
end

#xml_argsObject

Output URI as resource reference for RDF/XML

Example

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


60
61
62
# File 'lib/reddy/bnode.rb', line 60

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