Class: RDF::Normalize::Carroll2001

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rdf/normalize/carroll2001.rb

Instance Attribute Summary

Attributes inherited from Base

#dataset

Instance Method Summary collapse

Methods included from Enumerable

#canonicalize

Methods inherited from Base

#to_hash

Constructor Details

#initialize(enumerable, **options) ⇒ RDF::Enumerable

Create an enumerable with grounded nodes

Parameters:



10
11
12
# File 'lib/rdf/normalize/carroll2001.rb', line 10

def initialize(enumerable, **options)
  @dataset = enumerable
end

Instance Method Details

#each(&block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rdf/normalize/carroll2001.rb', line 14

def each(&block)
  ground_statements, anon_statements = [], []
  dataset.each_statement do |statement|
    (statement.has_blank_nodes? ? anon_statements : ground_statements) << statement
  end

  nodes = anon_statements.map(&:to_quad).flatten.compact.select(&:node?).uniq

  # Create a hash signature of every node, based on the signature of
  # statements it exists in.  
  # We also save hashes of nodes that cannot be reliably known; we will use
  # that information to eliminate possible recursion combinations.
  # 
  # Any mappings given in the method parameters are considered grounded.
  hashes, ungrounded_hashes = hash_nodes(anon_statements, nodes, {})

  # FIXME: likely need to iterate until hashes and ungrounded_hashes are the same size
  while hashes.size != ungrounded_hashes.size
    raise "Not done"
  end

  # Enumerate all statements, replacing nodes with new ground nodes using the hash as an identifier
  ground_statements.each(&block)
  anon_statements.each do |statement|
    quad = statement.to_quad.compact.map do |term|
      term.node? ? RDF::Node.intern(hashes[term]) : term
    end
    block.call RDF::Statement.from(quad)
  end
end