-
Notifications
You must be signed in to change notification settings - Fork 14
/
models.rb
348 lines (291 loc) · 10 KB
/
models.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
require 'rdf'
require 'spira'
require 'rdf/turtle'
require 'sparql/client'
module SPARQL; module Spec
DAWGT = RDF::Vocabulary.new('http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#')
ENT = RDF::Vocabulary.new('http://www.w3.org/ns/entailment/RDF')
MF = RDF::Vocabulary.new('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#')
QT = RDF::Vocabulary.new('http://www.w3.org/2001/sw/DataAccess/tests/test-query#')
RS = RDF::Vocabulary.new('http://www.w3.org/2001/sw/DataAccess/tests/result-set#')
UT = RDF::Vocabulary.new('http://www.w3.org/2009/sparql/tests/test-update#')
class Manifest < Spira::Base
type MF.Manifest
has_many :manifests, :predicate => MF.include
property :entry_list, :predicate => MF.entries
property :comment, :predicate => RDFS.comment
def entries
RDF::List.new(entry_list, self.class.repository).map do |entry|
type = self.class.repository.first_object(:subject => entry, :predicate => RDF.type)
case type
when UT.UpdateEvaluationTest, MF.UpdateEvaluationTest
entry.as(UpdateTest)
when MF.QueryEvaluationTest
entry.as(QueryTest)
# known types to ignore
when MF.PositiveSyntaxTest, MF.PositiveSyntaxTest11, MF.PositiveUpdateSyntaxTest11,
MF.NegativeSyntaxTest, MF.NegativeSyntaxTest11, MF.NegativeUpdateSyntaxTest11
entry.as(SyntaxTest)
when MF.CSVResultFormatTest, MF.ServiceDescriptionTest, MF.ProtocolTest,
MF.GraphStoreProtocolTest
# Ignore
else
warn "Unknown test type for #{entry}: #{type}"
end
end.compact
end
def include_files!
manifests.each do |manifest|
RDF::List.new(manifest, self.class.repository).each do |file|
puts "Loading #{file}"
self.class.repository.load(file, :context => file, :base_uri => file)
end
end
end
end
class Spira::Base
def encode_with(coder)
coder["subject"] = subject
attributes.each {|p,v| coder[p.to_s] = v if v}
end
def init_with(coder)
self.instance_variable_set(:"@subject", coder["subject"])
self.reload
attributes.each {|p,v| self.attribute_set(p, coder.map[p.to_s])}
end
def inspect
"<#{self.class}:#{self.object_id} @subject: #{@subject}>[" + attributes.keys.map do |a|
v = attributes[a]; "#{a}=#{v.inspect}" if v
end.compact.join("\n ") +
"]"
end
end
class SPARQLTest < Spira::Base
property :name, :predicate => MF.name
property :type, :predicate => RDF.type
property :comment, :predicate => RDFS.comment
property :approval, :predicate => DAWGT.approval
property :approved_by, :predicate => DAWGT.approvedBy
property :manifest, :predicate => MF.manifest_file
has_many :tags, :predicate => MF.tag
def approved?
approval == DAWGT.Approved
end
def entry; "??"; end
def form
query_data = begin action.query_string rescue nil end
if query_data =~ /(ASK|CONSTRUCT|DESCRIBE|SELECT|DELETE|LOAD|INSERT|CREATE|CLEAR|DROP)/i
case $1.upcase
when 'ASK', 'SELECT', 'DESCRIBE', 'CONSTRUCT'
$1.downcase.to_sym
when 'DELETE', 'LOAD', 'INSERT', 'CREATE', 'CLEAR', 'DROP'
:update
end
else
raise "Couldn't determine query type for #{File.basename(subject)} (reading #{action.query_file})"
end
end
end
class UpdateTest < SPARQLTest
property :result, :predicate => MF.result, :type => 'UpdateResult'
property :action, :predicate => MF.action, :type => 'UpdateAction'
def query_file
action.request
end
def entry; query_file.to_s.split('/').last; end
def template_file
'update-test.rb.erb'
end
def query
Kernel.open(query_file, &:read)
end
end
class UpdateDataSet < Spira::Base
has_many :graphData, :predicate => UT.graphData, :type => 'UpdateGraphData'
property :data_file, :predicate => UT.data
def data
Kernel.open(data_file, &:read)
end
def data_format
File.extname(data_file).sub(/\./,'').to_sym
end
end
class UpdateAction < UpdateDataSet
property :request, :predicate => UT.request
def query_file
request
end
end
class UpdateResult < UpdateDataSet
end
class UpdateGraphData < Spira::Base
property :graph, :predicate => UT.graph
property :basename, :predicate => RDFS.label, :type => Spira::Types::URI
def data_file
graph
end
def data
Kernel.open(graph, &:read)
end
def data_format
File.extname(data_file).sub(/\./,'').to_sym
end
end
class QueryTest < SPARQLTest
property :action, :predicate => MF.action, :type => 'QueryAction'
property :result, :predicate => MF.result
def query_file
action.query_file
end
def entry; query_file.to_s.split('/').last; end
def template_file
'query-test.rb.erb'
end
# Load and return default and named graphs in a hash
def graphs
@graphs ||= begin
graphs = {}
graphs[:default] = {:data => action.test_data_string, :format => :ttl} if action.test_data
action.graphData.each do |g|
graphs[g] = {:data => Kernel.open(g, &:read), :format => :ttl}
end
graphs
end
end
# Turn results into solutions
def solutions
return nil if !respond_to?(:result) || result.nil?
case form
when :select
if File.extname(result.path) == '.srx'
SPARQL::Client.parse_xml_bindings(Kernel.open(result, &:read))
else
expected_repository = RDF::Repository.new
Spira.add_repository!(:results, expected_repository)
expected_repository.load(result)
SPARQL::Spec::ResultBindings.each.first.solutions
end
when :ask
return true
when :describe, :create, :construct
RDF::Graph.load(result, :base_uri => result, :format => :ttl)
end
end
end
class UpdateTest < SPARQLTest
property :result, :predicate => MF.result, :type => 'UpdateResult'
property :action, :predicate => MF.action, :type => 'UpdateAction'
def query_file
action.request
end
def entry; query_file.to_s.split('/').last; end
def template_file
'update-test.rb.erb'
end
def query
Kernel.open(query_file, &:read)
end
end
class SyntaxTest < SPARQLTest
property :_action, :predicate => MF.action
# Construct an action instance, as this form only uses a simple URI
def action
@action ||= QueryAction.new {|a| a.query_file = _action; a.graphData = []; }
end
def entry; _action.to_s.to_s.split('/').last; end
end
class QueryAction < Spira::Base
property :query_file, :predicate => QT.query
property :test_data, :predicate => QT.data
has_many :graphData, :predicate => QT.graphData
def query_string
Kernel.open(query_file, &:read)
end
def sse_file
file = query_file.to_s.
sub(BASE_URI_10, BASE_DIRECTORY).
sub(BASE_URI_11, BASE_DIRECTORY).
sub(/\.rq$/, ".sse")
RDF::URI(file)
end
def sse_string
IO.read(sse_file.path)
end
def test_data_string
Kernel.open(test_data, &:read)
end
end
class SPARQLBinding < Spira::Base
property :value, :predicate => RS.value, :type => Native
property :variable, :predicate => RS.variable
default_source :results
end
class BindingSet < Spira::Base
default_source :results
has_many :bindings, :predicate => RS.binding, :type => 'SPARQLBinding'
property :index, :predicate => RS.index, :type => Integer
end
class ResultBindings < Spira::Base
type RS.ResultSet
has_many :variables, :predicate => RS.ResultSet
has_many :solution_lists, :predicate => RS.solution, :type => 'BindingSet'
property :boolean, :predicate => RS.boolean, :type => Boolean # for ask queries
default_source :results
# Return bindings as an list of Solutions
# @return [Enumerable<RDF::Query::Solution>]
def solutions
@solutions ||= begin
solution_lists.
sort_by {|solution_list| solution_list.index.to_i}.
map do |solution_list|
bindings = solution_list.bindings.inject({}) { |hash, binding|
hash[binding.variable.to_sym] = binding.value
hash
}
RDF::Query::Solution.new(bindings)
end
end
end
def self.for_solutions(solutions, opts = {})
opts[:uri] ||= RDF::Node.new
index = 1 if opts[:index]
result_bindings = self.for(opts[:uri]) do | binding_graph |
solutions.each do | result_hash |
binding_graph.solution_lists << BindingSet.new do | binding_set |
result_hash.to_hash.each_pair do |hash_variable, hash_value|
binding_set.bindings << SPARQLBinding.new do | sparql_binding |
sparql_binding.variable = hash_variable.to_s
sparql_binding.value = hash_value.respond_to?(:canonicalize) ? hash_value.dup.canonicalize : hash_value
end
end
if opts[:index]
binding_set.index = index
index += 1
end
end
end
end
end
def self.pretty_print
self.each do |result_binding|
log "Result Bindings #{result_binding.subject}"
result_binding.solution_lists.each.sort { |bs, other| bs.index.respond_to?(:'<=>') ? bs.index <=> other.index : 0 }.each do |binding_set|
log " Solution #{binding_set.subject} (# #{binding_set.index})"
binding_set.bindings.sort { |b, other| b.variable.to_s <=> other.variable.to_s }.each do |binding|
log " #{binding.variable}: #{binding.value.inspect}"
end
end
end
end
end
end; end
# Save short version of URI, without all the other stuff.
class RDF::URI
def encode_with(coder)
coder["uri"] = self.to_s
end
def init_with(coder)
self.instance_variable_set(:@value, coder["uri"])
self.instance_variable_set(:@object, nil)
end
end