| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
require 'getoptlong' |
| 5 |
require 'csv' |
| 6 |
require 'json' |
| 7 |
require 'haml' |
| 8 |
require 'fileutils' |
| 9 |
|
| 10 |
class Manifest |
| 11 |
JSON_STATE = JSON::State.new( |
| 12 |
:indent => " ", |
| 13 |
:space => " ", |
| 14 |
:space_before => "", |
| 15 |
:object_nl => "\n", |
| 16 |
:array_nl => "\n" |
| 17 |
) |
| 18 |
|
| 19 |
TITLE = { |
| 20 |
urgna2012: "RDF Graph Normalization (URGNA2012)", |
| 21 |
urdna2015: "RDF Dataset Normalization (URDNA2015)", |
| 22 |
} |
| 23 |
DESCRIPTION = { |
| 24 |
urgna2012: "Tests the 2012 version of RDF Graph Normalization.", |
| 25 |
urdna2015: "Tests the 2015 version of RDF Dataset Normalization." |
| 26 |
} |
| 27 |
|
| 28 |
Test = Struct.new(:id, :name, :comment, :approval, :action, :urgna2012, :urdna2015) |
| 29 |
|
| 30 |
attr_accessor :tests |
| 31 |
|
| 32 |
def initialize |
| 33 |
csv = CSV.new(File.open(File.expand_path("../manifest.csv", __FILE__))) |
| 34 |
|
| 35 |
columns = [] |
| 36 |
csv.shift.each_with_index {|c, i| columns[i] = c.to_sym if c} |
| 37 |
|
| 38 |
@tests = csv.map do |line| |
| 39 |
entry = {} |
| 40 |
|
| 41 |
line.each_with_index {|v, i| entry[columns[i]] = v ? v.gsub("\r", "\n").gsub("\\", "\\\\") : nil} |
| 42 |
|
| 43 |
urgna2012 = "#{entry[:test]}-urgna2012.nq" if entry[:urgna2012] == "TRUE" |
| 44 |
urdna2015 = "#{entry[:test]}-urdna2015.nq" if entry[:urdna2015] == "TRUE" |
| 45 |
Test.new(entry[:test], entry[:name], entry[:comment], entry[:approval], |
| 46 |
"#{entry[:test]}-in.nq", urgna2012, urdna2015) |
| 47 |
end |
| 48 |
end |
| 49 |
|
| 50 |
|
| 51 |
def create_files |
| 52 |
tests.each do |test| |
| 53 |
files = [test.action, test.urgna2012, test.urdna2015].compact |
| 54 |
files.compact.select {|f| !File.exist?(f)}.each do |f| |
| 55 |
File.open(f, "w") {|io| io.puts( f.end_with?('.json') ? "{}" : "")} |
| 56 |
end |
| 57 |
end |
| 58 |
end |
| 59 |
|
| 60 |
def test_class(test, variant) |
| 61 |
case variant.to_sym |
| 62 |
when :urgna2012 then "rdfn:Urgna2012EvalTest" |
| 63 |
when :urdna2015 then "rdfn:Urdna2015EvalTest" |
| 64 |
end |
| 65 |
end |
| 66 |
|
| 67 |
def to_jsonld(variant) |
| 68 |
context = ::JSON.parse %({ |
| 69 |
"xsd": "http://www.w3.org/2001/XMLSchema#", |
| 70 |
"rdfs": "http://www.w3.org/2000/01/rdf-schema#", |
| 71 |
"mf": "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#", |
| 72 |
"mq": "http://www.w3.org/2001/sw/DataAccess/tests/test-query#", |
| 73 |
"rdfn": "http://json-ld.github.io/normalization/test-vocab#", |
| 74 |
"rdft": "http://www.w3.org/ns/rdftest#", |
| 75 |
"id": "@id", |
| 76 |
"type": "@type", |
| 77 |
"action": {"@id": "mf:action", "@type": "@id"}, |
| 78 |
"approval": {"@id": "rdft:approval", "@type": "@id"}, |
| 79 |
"comment": "rdfs:comment", |
| 80 |
"entries": {"@id": "mf:entries", "@type": "@id", "@container": "@list"}, |
| 81 |
"label": "rdfs:label", |
| 82 |
"name": "mf:name", |
| 83 |
"result": {"@id": "mf:result", "@type": "@id"} |
| 84 |
}) |
| 85 |
|
| 86 |
manifest = { |
| 87 |
"@context" => context, |
| 88 |
"id" => "manifest-#{variant}", |
| 89 |
"type" => "mf:Manifest", |
| 90 |
"label" => TITLE[variant], |
| 91 |
"comment" => DESCRIPTION[variant], |
| 92 |
"entries" => [] |
| 93 |
} |
| 94 |
|
| 95 |
tests.each do |test| |
| 96 |
next unless test.send(variant) |
| 97 |
|
| 98 |
manifest["entries"] << { |
| 99 |
"id" => "manifest-#{variant}##{test.id}", |
| 100 |
"type" => test_class(test, variant), |
| 101 |
"name" => test.name, |
| 102 |
"comment" => test.comment, |
| 103 |
"approval" => (test.approval ? "rdft:#{test.approval}" : "rdft:Proposed"), |
| 104 |
"action" => test.action, |
| 105 |
"result" => test.send(variant) |
| 106 |
} |
| 107 |
end |
| 108 |
|
| 109 |
manifest.to_json(JSON_STATE) |
| 110 |
end |
| 111 |
|
| 112 |
def to_html |
| 113 |
|
| 114 |
template = File.read("template.haml") |
| 115 |
manifests = TITLE.keys.inject({}) do |memo, v| |
| 116 |
memo["manifest-#{v}"] = ::JSON.load(File.read("manifest-#{v}.jsonld")) |
| 117 |
memo |
| 118 |
end |
| 119 |
|
| 120 |
Haml::Engine.new(template, :format => :html5).render(self, |
| 121 |
man: ::JSON.load(File.read("manifest.jsonld")), |
| 122 |
manifests: manifests |
| 123 |
) |
| 124 |
end |
| 125 |
|
| 126 |
def to_ttl(variant) |
| 127 |
output = [] |
| 128 |
output << %(## RDF Dataset Normalization tests |
| 129 |
## Distributed under both the W3C Test Suite License[1] and the W3C 3- |
| 130 |
## clause BSD License[2]. To contribute to a W3C Test Suite, see the |
| 131 |
## policies and contribution forms [3] |
| 132 |
## |
| 133 |
## 1. http://www.w3.org/Consortium/Legal/2008/04-testsuite-license |
| 134 |
## 2. http://www.w3.org/Consortium/Legal/2008/03-bsd-license |
| 135 |
## 3. http://www.w3.org/2004/10/27-testcases |
| 136 |
## |
| 137 |
## Test types |
| 138 |
## * rdfn:Urgna2012EvalTest - Normalization using URGNA2012 |
| 139 |
## * rdfn:Urdna2015EvalTest - Normalization using URDNA2015 |
| 140 |
|
| 141 |
@prefix : <manifest-#{variant}#> . |
| 142 |
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . |
| 143 |
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
| 144 |
@prefix mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#> . |
| 145 |
@prefix rdft: <http://www.w3.org/ns/rdftest#> . |
| 146 |
@prefix rdfn: <http://json-ld.github.io/normalization/test-vocab#> . |
| 147 |
|
| 148 |
<manifest-#{variant}> a mf:Manifest ; |
| 149 |
) |
| 150 |
output << %( rdfs:label "#{TITLE[variant]}";) |
| 151 |
output << %( rdfs:comment "#{DESCRIPTION[variant]}";) |
| 152 |
output << %( mf:entries \() |
| 153 |
|
| 154 |
tests.select {|t| t.send(variant)}.map {|t| ":#{t.id}"}.each_slice(10) do |entries| |
| 155 |
output << %( #{entries.join(' ')}) |
| 156 |
end |
| 157 |
output << %( \) .) |
| 158 |
|
| 159 |
tests.select {|t| t.send(variant)}.each do |test| |
| 160 |
output << "" |
| 161 |
output << ":#{test.id} a #{test_class(test, variant)};" |
| 162 |
output << %( mf:name "#{test.name}";) |
| 163 |
output << %( rdfs:comment "#{test.comment}";) if test.comment |
| 164 |
output << %( rdft:approval #{(test.approval ? "rdft:#{test.approval}" : "rdft:Proposed")};) |
| 165 |
output << %( mf:action <#{test.action}>;) |
| 166 |
output << %( mf:result <#{test.send(variant)}>;) |
| 167 |
output << %( .) |
| 168 |
end |
| 169 |
output.join("\n") |
| 170 |
end |
| 171 |
end |
| 172 |
|
| 173 |
options = { |
| 174 |
output: $stdout |
| 175 |
} |
| 176 |
|
| 177 |
OPT_ARGS = [ |
| 178 |
["--format", "-f", GetoptLong::REQUIRED_ARGUMENT,"Output format, default #{options[:format].inspect}"], |
| 179 |
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT,"Output to the specified file path"], |
| 180 |
["--quiet", GetoptLong::NO_ARGUMENT, "Supress most output other than progress indicators"], |
| 181 |
["--touch", GetoptLong::NO_ARGUMENT, "Create referenced files and directories if missing"], |
| 182 |
["--variant", GetoptLong::REQUIRED_ARGUMENT,"Test variant, 'rdf' or 'json'"], |
| 183 |
["--help", "-?", GetoptLong::NO_ARGUMENT, "This message"] |
| 184 |
] |
| 185 |
def usage |
| 186 |
STDERR.puts %{Usage: #{$0} [options] URL ...} |
| 187 |
width = OPT_ARGS.map do |o| |
| 188 |
l = o.first.length |
| 189 |
l += o[1].length + 2 if o[1].is_a?(String) |
| 190 |
l |
| 191 |
end.max |
| 192 |
OPT_ARGS.each do |o| |
| 193 |
s = " %-*s " % [width, (o[1].is_a?(String) ? "#{o[0,2].join(', ')}" : o[0])] |
| 194 |
s += o.last |
| 195 |
STDERR.puts s |
| 196 |
end |
| 197 |
exit(1) |
| 198 |
end |
| 199 |
|
| 200 |
opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]}) |
| 201 |
|
| 202 |
opts.each do |opt, arg| |
| 203 |
case opt |
| 204 |
when '--format' then options[:format] = arg.to_sym |
| 205 |
when '--output' then options[:output] = File.open(arg, "w") |
| 206 |
when '--quiet' then options[:quiet] = true |
| 207 |
when '--touch' then options[:touch] = true |
| 208 |
when '--variant' then options[:variant] = arg.to_sym |
| 209 |
when '--help' then usage |
| 210 |
end |
| 211 |
end |
| 212 |
|
| 213 |
vocab = Manifest.new |
| 214 |
vocab.create_files if options[:touch] |
| 215 |
if options[:format] || options[:variant] |
| 216 |
case options[:format] |
| 217 |
when :jsonld then options[:output].puts(vocab.to_jsonld(options[:variant])) |
| 218 |
when :ttl then options[:output].puts(vocab.to_ttl(options[:variant])) |
| 219 |
when :html then options[:output].puts(vocab.to_html) |
| 220 |
else STDERR.puts "Unknown format #{options[:format].inspect}" |
| 221 |
end |
| 222 |
else |
| 223 |
Manifest::TITLE.keys.each do |variant| |
| 224 |
%w(jsonld ttl).each do |format| |
| 225 |
File.open("manifest-#{variant}.#{format}", "w") do |output| |
| 226 |
output.puts(vocab.send("to_#{format}".to_sym, variant)) |
| 227 |
end |
| 228 |
end |
| 229 |
end |
| 230 |
File.open("index.html", "w") do |output| |
| 231 |
output.puts(vocab.to_html) |
| 232 |
end |
| 233 |
end |