Class: RdfContext::TurtleSerializer

Inherits:
RecursiveSerializer show all
Defined in:
lib/rdf_context/serializer/turtle_serializer.rb

Overview

Abstract serializer

Constant Summary collapse

SUBJECT =
0
VERB =
1
OBJECT =
2

Constants inherited from RecursiveSerializer

RecursiveSerializer::INDENT_STRING, RecursiveSerializer::MAX_DEPTH

Instance Attribute Summary

Attributes inherited from AbstractSerializer

#base, #graph

Instance Method Summary collapse

Methods inherited from RecursiveSerializer

#add_namespace, #indent, #initialize, #is_done?, #order_subjects, #predicate_order, #preprocess, #ref_count, #sort_properties, #subject_done, #top_classes, #uri_binding, #write

Methods inherited from AbstractSerializer

#initialize, #relativize

Constructor Details

This class inherits a constructor from RdfContext::RecursiveSerializer

Instance Method Details

#do_list(l) ⇒ Object (protected)



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 100

def do_list(l)
  puts "do_list: #{l.inspect}" if ::RdfContext::debug?
  position = SUBJECT
  while l do
    p = @graph.properties(l)
    item = p.fetch(RDF_NS.first.to_s, []).first
    if item
      path(item, position)
      subject_done(l)
      position = OBJECT
    end
    l = p.fetch(RDF_NS.rest.to_s, []).first
  end
end

#end_documentObject (protected)



80
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 80

def end_document; end

#get_qname(uri) ⇒ Object (protected)



42
43
44
45
46
47
48
49
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 42

def get_qname(uri)
  if uri.is_a?(URIRef)
    md = relativize(uri)
    return "<#{md}>" unless md == uri.to_s
    
    super(uri)
  end
end

#is_valid_list(l) ⇒ Object (protected)

Checks if l is a valid RDF list, i.e. no nodes have other properties.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 83

def is_valid_list(l)
  props = @graph.properties(l)
  #puts "is_valid_list: #{props.inspect}" if ::RdfContext::debug?
  return false unless props.has_key?(RDF_NS.first.to_s) || l == RDF_NS.nil
  while l && l != RDF_NS.nil do
    #puts "is_valid_list(length): #{props.length}" if ::RdfContext::debug?
    return false unless props.has_key?(RDF_NS.first.to_s) && props.has_key?(RDF_NS.rest.to_s)
    n = props[RDF_NS.rest.to_s]
    #puts "is_valid_list(n): #{n.inspect}" if ::RdfContext::debug?
    return false unless n.is_a?(Array) && n.length == 1
    l = n.first
    props = @graph.properties(l)
  end
  #puts "is_valid_list: valid" if ::RdfContext::debug?
  true
end

#label(node) ⇒ Object (protected)



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

def label(node)
  get_qname(node) || node.to_n3
end

#object_list(objects) ⇒ Object (protected)



166
167
168
169
170
171
172
173
174
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 166

def object_list(objects)
  puts "object_list: #{objects.inspect}" if ::RdfContext::debug?
  return if objects.empty?

  objects.each_with_index do |obj, i|
    write(",\n#{indent(4)}") if i > 0
    path(obj, OBJECT)
  end
end

#p_default(node, position) ⇒ Object (protected)



146
147
148
149
150
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 146

def p_default(node, position)
  #puts "p_default: #{node.inspect}, #{position}" if ::RdfContext::debug?
  l = (position == SUBJECT ? "" : " ") + label(node)
  write(l)
end

#p_list(node, position) ⇒ Object (protected)



115
116
117
118
119
120
121
122
123
124
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 115

def p_list(node, position)
  return false if !is_valid_list(node)
  #puts "p_list: #{node.inspect}, #{position}" if ::RdfContext::debug?

  write(position == SUBJECT ? "(" : " (")
  @depth += 2
  do_list(node)
  @depth -= 2
  write(')')
end

#p_squared(node, position) ⇒ Object (protected)



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 132

def p_squared(node, position)
  return false unless p_squared?(node, position)

  #puts "p_squared: #{node.inspect}, #{position}" if ::RdfContext::debug?
  subject_done(node)
  write(position == SUBJECT ? '[' : ' [')
  @depth += 2
  predicate_list(node)
  @depth -= 2
  write(']')
  
  true
end

#p_squared?(node, position) ⇒ Boolean (protected)

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 126

def p_squared?(node, position)
  node.is_a?(BNode) &&
    !@serialized.has_key?(node) &&
    ref_count(node) <= 1
end

#path(node, position) ⇒ Object (protected)

Raises:



152
153
154
155
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 152

def path(node, position)
  puts "path: #{node.inspect}, pos: #{position}, []: #{is_valid_list(node)}, p2?: #{p_squared?(node, position)}, rc: #{ref_count(node)}" if ::RdfContext::debug?
  raise RdfException, "Cannot serialize node '#{node}'" unless p_list(node, position) || p_squared(node, position) || p_default(node, position)
end

#predicate_list(subject) ⇒ Object (protected)



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 176

def predicate_list(subject)
  properties = @graph.properties(subject)
  prop_list = sort_properties(properties) - [RDF_NS.first.to_s, RDF_NS.rest.to_s]
  puts "predicate_list: #{prop_list.inspect}" if ::RdfContext::debug?
  return if prop_list.empty?

  prop_list.each_with_index do |prop, i|
    write(";\n#{indent(2)}") if i > 0
    verb(URIRef.new(prop))
    object_list(properties[prop])
  end
end

#preprocess_triple(triple) ⇒ Object (protected)



51
52
53
54
55
56
57
58
59
60
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 51

def preprocess_triple(triple)
  super
  
  # Pre-fetch qnames, to fill namespaces
  get_qname(triple.subject)
  get_qname(triple.predicate)
  get_qname(triple.object)

  @references[triple.predicate] = ref_count(triple.predicate) + 1
end

#resetObject (protected)



36
37
38
39
40
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 36

def reset
  super
  @shortNames = {}
  @started = false
end

#s_default(subject) ⇒ Object (protected)



204
205
206
207
208
209
210
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 204

def s_default(subject)
  write("\n#{indent}")
  path(subject, SUBJECT)
  predicate_list(subject)
  write(" .")
  true
end

#s_squared(subject) ⇒ Object (protected)



193
194
195
196
197
198
199
200
201
202
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 193

def s_squared(subject)
  return false unless s_squared?(subject)
  
  write("\n#{indent} [")
  @depth += 1
  predicate_list(subject)
  @depth -= 1
  write("] .")
  true
end

#s_squared?(subject) ⇒ Boolean (protected)

Returns:

  • (Boolean)


189
190
191
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 189

def s_squared?(subject)
  ref_count(subject) == 0 && subject.is_a?(BNode) && !is_valid_list(subject)
end

#serialize(stream, options = {})

This method returns an undefined value.

Serialize the graph

Parameters:

  • stream (IO, StreamIO)

    Stream in which to place serialized graph

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :base (URIRef, String) — default: nil

    Base URI of graph, used to shorting URI references



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 13

def serialize(stream, options = {})
  puts "\nserialize: #{@graph.inspect}" if ::RdfContext::debug?
  reset
  @stream = stream
  @base = options[:base]
  
  @graph.bind(RDF_NS)
  @graph.bind(RDFS_NS)
  
  preprocess
  start_document

  order_subjects.each do |subject|
    #puts "subj: #{subject.inspect}"
    unless is_done?(subject)
      statement(subject)
    end
  end
  
  end_document
end

#start_documentObject (protected)



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

def start_document
  @started = true
  
  write("#{indent}@base <#{@base}> .\n") if @base
  
  ns_list = @namespaces.values.sort_by {|ns| ns.prefix}
  unless ns_list.empty?
    ns_str = ns_list.map do |ns|
      "#{indent}@prefix #{ns.prefix}: <#{ns.uri}> ."
    end.join("\n") + "\n"
    write(ns_str)
  end
end

#statement(subject) ⇒ Object (protected)



212
213
214
215
216
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 212

def statement(subject)
  puts "statement: #{subject.inspect}, s2?: #{s_squared(subject)}" if ::RdfContext::debug?
  subject_done(subject)
  s_squared(subject) || s_default(subject)
end

#verb(node) ⇒ Object (protected)



157
158
159
160
161
162
163
164
# File 'lib/rdf_context/serializer/turtle_serializer.rb', line 157

def verb(node)
  puts "verb: #{node.inspect}" if ::RdfContext::debug?
  if node == RDF_TYPE
    write(" a")
  else
    path(node, VERB)
  end
end