Class: RdfContext::RecursiveSerializer

Inherits:
AbstractSerializer show all
Defined in:
lib/rdf_context/serializer/recursive_serializer.rb

Overview

Recursive serializer

Direct Known Subclasses

TurtleSerializer, XmlSerializer

Constant Summary collapse

MAX_DEPTH =
10
INDENT_STRING =
" "

Instance Attribute Summary

Attributes inherited from AbstractSerializer

#base, #graph

Instance Method Summary collapse

Methods inherited from AbstractSerializer

#relativize, #serialize

Constructor Details

#initialize(graph) ⇒ RecursiveSerializer

New RecursiveSerializer

Parameters:



10
11
12
13
14
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 10

def initialize(graph)
  super(graph)
  @stream = nil
  self.reset
end

Instance Method Details

#add_namespace(ns) ⇒ Object (protected)



78
79
80
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 78

def add_namespace(ns)
  @namespaces[ns.prefix.to_s] = ns
end

#get_qname(uri) ⇒ Object (protected)

Return a QName for the URI, or nil. Adds namespace of QName to defined namespaces



67
68
69
70
71
72
73
74
75
76
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 67

def get_qname(uri)
  if uri.is_a?(URIRef)
    qn = @graph.qname(uri)
    # Local parts with . will mess up serialization
    return false if qn.nil? || qn.index('.')
    
    add_namespace(uri.namespace)
    qn
  end
end

#indent(modifier = 0) ⇒ Object (protected)

Returns indent string multiplied by the depth



127
128
129
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 127

def indent(modifier = 0)
  INDENT_STRING * (@depth + modifier)
end

#is_done?(subject) ⇒ Boolean (protected)

Returns:

  • (Boolean)


20
21
22
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 20

def is_done?(subject)
  @serialized.include?(subject)
end

#order_subjectsObject (protected)



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 29

def order_subjects
  seen = {}
  subjects = []
  
  top_classes.each do |class_uri|
    graph.triples(Triple.new(nil, RDF_TYPE, class_uri)).map {|t| t.subject}.sort.uniq.each do |subject|
      #puts "order_subjects: #{subject.inspect}"
      subjects << subject
      seen[subject] = @top_levels[subject] = true
    end
  end
  
  # Sort subjects by resources over bnodes, ref_counts and the subject URI itself
  recursable = @subjects.keys.
    select {|s| !seen.include?(s)}.
    map {|r| [r.is_a?(BNode) ? 1 : 0, ref_count(r), r]}.
    sort
  
  subjects += recursable.map{|r| r.last}
end

#predicate_orderObject (protected)



18
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 18

def predicate_order; [RDF_TYPE, RDFS_NS.label, DC_NS.title]; end

#preprocessObject (protected)



50
51
52
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 50

def preprocess
  @graph.triples.each {|t| preprocess_triple(t)}
end

#preprocess_triple(triple) ⇒ Object (protected)



54
55
56
57
58
59
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 54

def preprocess_triple(triple)
  #puts "preprocess: #{triple.inspect}"
  references = ref_count(triple.object) + 1
  @references[triple.object] = references
  @subjects[triple.subject] = true
end

#ref_count(node) ⇒ Object (protected)

Return the number of times this node has been referenced in the object position



62
63
64
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 62

def ref_count(node)
  @references.fetch(node, 0)
end

#resetObject (protected)



87
88
89
90
91
92
93
94
95
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 87

def reset
  @depth = 0
  @lists = {}
  @namespaces = {}
  @references = {}
  @serialized = {}
  @subjects = {}
  @top_levels = {}
end

#sort_properties(properties) ⇒ Object (protected)

Take a hash from predicate uris to lists of values. Sort the lists of values. Return a sorted list of properties.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 99

def sort_properties(properties)
  properties.keys.each do |k|
    properties[k] = properties[k].sort do |a, b|
      a_li = a.is_a?(URIRef) && a.short_name =~ /^_\d+$/ ? a.to_i : a.to_s
      b_li = b.is_a?(URIRef) && b.short_name =~ /^_\d+$/ ? b.to_i : b.to_s
      
      a_li <=> b_li
    end
  end
  
  # Make sorted list of properties
  prop_list = []
  
  predicate_order.each do |prop|
    next unless properties[prop]
    prop_list << prop.to_s
  end
  
  properties.keys.sort.each do |prop|
    next if prop_list.include?(prop.to_s)
    prop_list << prop.to_s
  end
  
  puts "sort_properties: #{prop_list.to_sentence}" if ::RdfContext::debug?
  prop_list
end

#subject_done(subject) ⇒ Object (protected)

Mark a subject as done.



25
26
27
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 25

def subject_done(subject)
  @serialized[subject] = true
end

#top_classesObject (protected)



17
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 17

def top_classes; [RDFS_NS.Class]; end

#uri_bindingObject (protected)

URI -> Namespace bindings (similar to graph) for looking up qnames



83
84
85
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 83

def uri_binding
  @uri_binding ||= @namespaces.values.inject({}) {|hash, ns| hash[ns.uri.to_s] = ns; hash}
end

#write(text) ⇒ Object (protected)

Write text



132
133
134
# File 'lib/rdf_context/serializer/recursive_serializer.rb', line 132

def write(text)
  @stream.write(text)
end