This repository has been archived by the owner on Dec 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
turtle_serializer.rb
218 lines (182 loc) · 5.87 KB
/
turtle_serializer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
module RdfContext
# Abstract serializer
class TurtleSerializer < RecursiveSerializer
SUBJECT = 0
VERB = 1
OBJECT = 2
# Serialize the graph
#
# @param [IO, StreamIO] stream Stream in which to place serialized graph
# @option options [URIRef, String] :base (nil) Base URI of graph, used to shorting URI references
# @return [void]
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
protected
def reset
super
@shortNames = {}
@started = false
end
def get_qname(uri)
if uri.is_a?(URIRef)
md = relativize(uri)
return "<#{md}>" unless md == uri.to_s
super(uri)
end
end
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
def label(node)
get_qname(node) || node.to_n3
end
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
def end_document; end
# Checks if l is a valid RDF list, i.e. no nodes have other properties.
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
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
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
def p_squared?(node, position)
node.is_a?(BNode) &&
!@serialized.has_key?(node) &&
ref_count(node) <= 1
end
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
def p_default(node, position)
#puts "p_default: #{node.inspect}, #{position}" if ::RdfContext::debug?
l = (position == SUBJECT ? "" : " ") + label(node)
write(l)
end
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
def verb(node)
puts "verb: #{node.inspect}" if ::RdfContext::debug?
if node == RDF_TYPE
write(" a")
else
path(node, VERB)
end
end
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
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
def s_squared?(subject)
ref_count(subject) == 0 && subject.is_a?(BNode) && !is_valid_list(subject)
end
def s_squared(subject)
return false unless s_squared?(subject)
write("\n#{indent} [")
@depth += 1
predicate_list(subject)
@depth -= 1
write("] .")
true
end
def s_default(subject)
write("\n#{indent}")
path(subject, SUBJECT)
predicate_list(subject)
write(" .")
true
end
def statement(subject)
puts "statement: #{subject.inspect}, s2?: #{s_squared(subject)}" if ::RdfContext::debug?
subject_done(subject)
s_squared(subject) || s_default(subject)
end
end
end