rdf-n3 0.3.4.1 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/{History.md → History.markdown} +0 -0
- data/{README → README.markdown} +13 -8
- data/VERSION +1 -1
- data/lib/rdf/n3.rb +1 -1
- data/lib/rdf/n3/format.rb +0 -35
- data/lib/rdf/n3/patches/list.rb +36 -0
- data/lib/rdf/n3/reader.rb +11 -26
- data/lib/rdf/n3/writer.rb +21 -40
- metadata +51 -71
- data/.yardopts +0 -12
- data/README.md +0 -149
- data/Rakefile +0 -45
- data/example-files/arnau-registered-vocab.rb +0 -21
- data/example-files/arnau-stack-overflow.ttl +0 -12
- data/example-files/best-buy.nt +0 -4
- data/example-files/dwbutler-mj.n3 +0 -10
- data/example-files/dwbutler-mj.ttl +0 -8
- data/example-files/lee-reilly-list.rb +0 -40
- data/example-files/recipe.ttl +0 -33
- data/example-files/sp2b.n3 +0 -50177
- data/example.rb +0 -45
- data/lib/rdf/n3/patches/graph_properties.rb +0 -34
- data/lib/rdf/n3/reader/bnf-rules.n3 +0 -134
- data/lib/rdf/n3/reader/n3-selectors.n3 +0 -0
- data/lib/rdf/n3/reader/n3.n3 +0 -261
- data/rdf-n3.gemspec +0 -111
- data/script/build_meta +0 -249
- data/script/console +0 -10
- data/script/parse +0 -103
- data/script/tc +0 -53
- data/script/yard-to-rubyforge +0 -2
- data/spec/.gitignore +0 -1
- data/spec/cwm_spec.rb +0 -44
- data/spec/cwm_test.rb +0 -76
- data/spec/format_spec.rb +0 -41
- data/spec/matchers.rb +0 -130
- data/spec/reader_spec.rb +0 -1243
- data/spec/spec.opts +0 -1
- data/spec/spec_helper.rb +0 -86
- data/spec/swap_spec.rb +0 -78
- data/spec/swap_test.rb +0 -79
- data/spec/turtle_spec.rb +0 -66
- data/spec/turtle_test.rb +0 -98
- data/spec/writer_spec.rb +0 -399
data/spec/spec.opts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--colour
|
data/spec/spec_helper.rb
DELETED
@@ -1,86 +0,0 @@
|
|
1
|
-
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
-
$:.unshift File.dirname(__FILE__)
|
3
|
-
|
4
|
-
require 'rubygems'
|
5
|
-
require 'rspec'
|
6
|
-
require 'matchers'
|
7
|
-
require 'bigdecimal' # XXX Remove Me
|
8
|
-
require 'rdf/n3'
|
9
|
-
require 'rdf/ntriples'
|
10
|
-
require 'rdf/spec'
|
11
|
-
require 'rdf/spec/matchers'
|
12
|
-
require 'rdf/isomorphic'
|
13
|
-
require 'yaml' # XXX should be in open-uri/cached
|
14
|
-
require 'open-uri/cached'
|
15
|
-
|
16
|
-
include Matchers
|
17
|
-
|
18
|
-
# Create and maintain a cache of downloaded URIs
|
19
|
-
URI_CACHE = File.expand_path(File.join(File.dirname(__FILE__), "uri-cache"))
|
20
|
-
Dir.mkdir(URI_CACHE) unless File.directory?(URI_CACHE)
|
21
|
-
OpenURI::Cache.class_eval { @cache_path = URI_CACHE }
|
22
|
-
|
23
|
-
module RDF
|
24
|
-
module Isomorphic
|
25
|
-
alias_method :==, :isomorphic_with?
|
26
|
-
end
|
27
|
-
class Graph
|
28
|
-
def to_ntriples
|
29
|
-
RDF::Writer.for(:ntriples).buffer do |writer|
|
30
|
-
self.each_statement do |statement|
|
31
|
-
writer << statement
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
def dump
|
36
|
-
b = []
|
37
|
-
self.each_statement do |statement|
|
38
|
-
b << statement.to_triple.inspect
|
39
|
-
end
|
40
|
-
b.join("\n")
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
::RSpec.configure do |c|
|
46
|
-
c.filter_run :focus => true
|
47
|
-
c.run_all_when_everything_filtered = true
|
48
|
-
c.exclusion_filter = {
|
49
|
-
:ruby => lambda { |version| !(RUBY_VERSION.to_s =~ /^#{version.to_s}/) },
|
50
|
-
}
|
51
|
-
c.include(Matchers)
|
52
|
-
c.include(RDF::Spec::Matchers)
|
53
|
-
end
|
54
|
-
|
55
|
-
# Serialize graph and replace bnodes with predictable versions, return as sorted array
|
56
|
-
def normalize_bnodes(graph, anon = "a")
|
57
|
-
anon_ctx = {}
|
58
|
-
# Find and replace all BNodes within graph string
|
59
|
-
g_str = graph.to_ntriples
|
60
|
-
anon_entries = g_str.scan(/_:g\d+/).sort.uniq
|
61
|
-
anon_entries.each do |a|
|
62
|
-
anon_ctx[a] = "_:#{anon}"
|
63
|
-
anon = anon.succ
|
64
|
-
end
|
65
|
-
|
66
|
-
g_str.gsub(/_:g\d+/) { |bn| anon_ctx[bn] }.split("\n").sort
|
67
|
-
end
|
68
|
-
|
69
|
-
# Heuristically detect the input stream
|
70
|
-
def detect_format(stream)
|
71
|
-
# Got to look into the file to see
|
72
|
-
if stream.is_a?(IO) || stream.is_a?(StringIO)
|
73
|
-
stream.rewind
|
74
|
-
string = stream.read(1000)
|
75
|
-
stream.rewind
|
76
|
-
else
|
77
|
-
string = stream.to_s
|
78
|
-
end
|
79
|
-
case string
|
80
|
-
when /<\w+:RDF/ then :rdfxml
|
81
|
-
when /<RDF/ then :rdfxml
|
82
|
-
when /<html/i then :rdfa
|
83
|
-
when /@prefix/i then :n3
|
84
|
-
else :n3
|
85
|
-
end
|
86
|
-
end
|
data/spec/swap_spec.rb
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
$:.unshift "."
|
2
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
3
|
-
|
4
|
-
describe RDF::N3::Reader do
|
5
|
-
# W3C N3 Test suite from http://www.w3.org/2000/10/swap/test/n3parser.tests
|
6
|
-
describe "w3c swap tests" do
|
7
|
-
require 'swap_test'
|
8
|
-
|
9
|
-
# Negative parser tests should raise errors.
|
10
|
-
describe "positive parser tests" do
|
11
|
-
Fixtures::SWAPTest::PositiveParserTest.each do |t|
|
12
|
-
#next unless t.subject.to_s =~ /rdfms-rdf-names-use/
|
13
|
-
#next unless t.name =~ /11/
|
14
|
-
#puts t.inspect
|
15
|
-
specify "#{t.name}: #{t.inputDocument} against #{t.outputDocument}" do
|
16
|
-
begin
|
17
|
-
t.run_test do
|
18
|
-
t.name.should_not == "n3_10012" # Too many bnodes makes graph compare unfeasable
|
19
|
-
t.debug = []
|
20
|
-
g = RDF::Graph.new
|
21
|
-
RDF::N3::Reader.new(t.input,
|
22
|
-
:base_uri => t.inputDocument,
|
23
|
-
:strict => true,
|
24
|
-
:debug => t.debug).each do |statement|
|
25
|
-
g << statement
|
26
|
-
end
|
27
|
-
g
|
28
|
-
end
|
29
|
-
rescue RSpec::Expectations::ExpectationNotMetError => e
|
30
|
-
if %w(n3_10012).include?(t.name)
|
31
|
-
pending("check visually, graph compare times too long")
|
32
|
-
elsif %w(n3_10010).include?(t.name)
|
33
|
-
pending("Not supported in Ruby 1.8")
|
34
|
-
elsif %w(n3_10008 n3_10013).include?(t.name)
|
35
|
-
pending("Isomorphic compare issue")
|
36
|
-
elsif %w(n3_10004 n3_10007 n3_10014 n3_10015 n3_10017).include?(t.name)
|
37
|
-
pending("Formulae inferrence not supported")
|
38
|
-
elsif %w(n3_10006 n3_10009).include?(t.name)
|
39
|
-
pending("Verified test results are incorrect")
|
40
|
-
else
|
41
|
-
raise
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
describe "negative parser tests" do
|
49
|
-
Fixtures::SWAPTest::NegativeParserTest.each do |t|
|
50
|
-
#next unless t.subject.uri.to_s =~ /rdfms-empty-property-elements/
|
51
|
-
#next unless t.name =~ /1/
|
52
|
-
#puts t.inspect
|
53
|
-
specify "#{t.name}: #{t.inputDocument}#{t.outputDocument ? (' against ' + t.outputDocument) : ''}" do
|
54
|
-
begin
|
55
|
-
t.run_test do
|
56
|
-
lambda do
|
57
|
-
t.debug = []
|
58
|
-
g = RDF::Graph.new
|
59
|
-
RDF::N3::Reader.new(t.input,
|
60
|
-
:base_uri => t.inputDocument,
|
61
|
-
:strict => true,
|
62
|
-
:debug => t.debug).each do |statement|
|
63
|
-
g << statement
|
64
|
-
end
|
65
|
-
end.should raise_error(RDF::ReaderError)
|
66
|
-
end
|
67
|
-
rescue RSpec::Expectations::ExpectationNotMetError => e
|
68
|
-
if %w(n3_10019 n3_10020).include?(t.name)
|
69
|
-
pending("This is still supported")
|
70
|
-
else
|
71
|
-
raise
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
data/spec/swap_test.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
# Spira class for manipulating test-manifest style test suites.
|
2
|
-
# Used for SWAP tests
|
3
|
-
require 'spira'
|
4
|
-
require 'rdf/n3'
|
5
|
-
require 'open-uri'
|
6
|
-
|
7
|
-
module Fixtures
|
8
|
-
module SWAPTest
|
9
|
-
class N3T < RDF::Vocabulary("http://www.w3.org/2004/11/n3test#"); end
|
10
|
-
|
11
|
-
class Entry
|
12
|
-
attr_accessor :debug
|
13
|
-
attr_accessor :compare
|
14
|
-
include Spira::Resource
|
15
|
-
|
16
|
-
property :description, :predicate => N3T.description, :type => XSD.string
|
17
|
-
property :inputDocument, :predicate => N3T.inputDocument
|
18
|
-
property :outputDocument, :predicate => N3T.outputDocument
|
19
|
-
|
20
|
-
def name
|
21
|
-
subject.to_s.split("#").last
|
22
|
-
end
|
23
|
-
|
24
|
-
def input
|
25
|
-
Kernel.open(self.inputDocument)
|
26
|
-
end
|
27
|
-
|
28
|
-
def output
|
29
|
-
self.outputDocument ? Kernel.open(self.outputDocument) : ""
|
30
|
-
end
|
31
|
-
|
32
|
-
def information; self.description; end
|
33
|
-
|
34
|
-
def inspect
|
35
|
-
"[#{self.class.to_s} " + %w(
|
36
|
-
subject
|
37
|
-
description
|
38
|
-
inputDocument
|
39
|
-
outputDocument
|
40
|
-
).map {|a| v = self.send(a); "#{a}='#{v}'" if v}.compact.join(", ") +
|
41
|
-
"]"
|
42
|
-
end
|
43
|
-
|
44
|
-
# Run test case, yields input for parser to create triples
|
45
|
-
def run_test(options = {})
|
46
|
-
# Run
|
47
|
-
graph = yield
|
48
|
-
|
49
|
-
return unless self.outputDocument
|
50
|
-
|
51
|
-
case self.compare
|
52
|
-
when :none
|
53
|
-
# Don't check output, just parse to graph
|
54
|
-
when :array
|
55
|
-
@parser.graph.should be_equivalent_graph(self.output, self)
|
56
|
-
else
|
57
|
-
#puts "parse #{self.outputDocument} as #{RDF::Reader.for(self.outputDocument)}"
|
58
|
-
format = detect_format(self.output)
|
59
|
-
output_graph = RDF::Graph.load(self.outputDocument, :format => format, :base_uri => self.inputDocument)
|
60
|
-
puts "result: #{CGI.escapeHTML(graph.to_ntriples)}" if ::RDF::N3::debug?
|
61
|
-
graph.should Matchers::be_equivalent_graph(output_graph, self)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
class PositiveParserTest < Entry
|
67
|
-
default_source :entries
|
68
|
-
type N3T.PositiveParserTest
|
69
|
-
end
|
70
|
-
|
71
|
-
class NegativeParserTest < Entry
|
72
|
-
default_source :entries
|
73
|
-
type N3T.NegativeParserTest
|
74
|
-
end
|
75
|
-
|
76
|
-
repo = RDF::Repository.load("http://www.w3.org/2000/10/swap/test/n3parser.tests", :format => :n3)
|
77
|
-
Spira.add_repository! :entries, repo
|
78
|
-
end
|
79
|
-
end
|
data/spec/turtle_spec.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
$:.unshift "."
|
2
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
3
|
-
|
4
|
-
describe RDF::N3::Reader do
|
5
|
-
# W3C Turtle Test suite from http://www.w3.org/2000/10/swap/test/regression.n3
|
6
|
-
describe "w3c turtle tests" do
|
7
|
-
require 'turtle_test'
|
8
|
-
|
9
|
-
describe "positive parser tests" do
|
10
|
-
Fixtures::TurtleTest::Good.each do |t|
|
11
|
-
next unless t.comment
|
12
|
-
#puts t.inspect
|
13
|
-
|
14
|
-
# modified test-10 results to be canonical
|
15
|
-
# modified test-21&22 results exponent to be E not e
|
16
|
-
# modified test-28 2.30 => 2.3 representation
|
17
|
-
specify "#{t.name}: #{t.comment}" do
|
18
|
-
#puts t.inspect
|
19
|
-
# Skip tests for very long files, too long
|
20
|
-
if !defined?(::Encoding) && %w(test-18).include?(t.name)
|
21
|
-
pending("Not supported in Ruby 1.8")
|
22
|
-
else
|
23
|
-
t.run_test do
|
24
|
-
t.debug = []
|
25
|
-
g = RDF::Graph.new
|
26
|
-
RDF::N3::Reader.new(t.input,
|
27
|
-
:base_uri => t.inputDocument,
|
28
|
-
:strict => true,
|
29
|
-
:canonicalize => true,
|
30
|
-
:debug => t.debug).each do |statement|
|
31
|
-
g << statement
|
32
|
-
end
|
33
|
-
g
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
describe "negative parser tests" do
|
41
|
-
Fixtures::TurtleTest::Bad.each do |t|
|
42
|
-
next unless t.comment
|
43
|
-
#puts t.inspect
|
44
|
-
specify "#{t.name}: #{t.comment}" do
|
45
|
-
begin
|
46
|
-
t.run_test do
|
47
|
-
lambda do
|
48
|
-
t.debug = []
|
49
|
-
g = RDF::Graph.new
|
50
|
-
RDF::N3::Reader.new(t.input,
|
51
|
-
:base_uri => t.inputDocument,
|
52
|
-
:strict => true,
|
53
|
-
:debug => t.debug).each do |statement|
|
54
|
-
g << statement
|
55
|
-
end
|
56
|
-
end.should raise_error(RDF::ReaderError)
|
57
|
-
end
|
58
|
-
rescue RSpec::Expectations::ExpectationNotMetError => e
|
59
|
-
pending() { raise }
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
data/spec/turtle_test.rb
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
# Spira class for manipulating test-manifest style test suites.
|
2
|
-
# Used for Turtle tests
|
3
|
-
require 'spira'
|
4
|
-
require 'rdf/n3'
|
5
|
-
require 'open-uri'
|
6
|
-
|
7
|
-
module Fixtures
|
8
|
-
module TurtleTest
|
9
|
-
class MF < RDF::Vocabulary("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#"); end
|
10
|
-
|
11
|
-
class Entry
|
12
|
-
attr_accessor :debug
|
13
|
-
attr_accessor :compare
|
14
|
-
include Spira::Resource
|
15
|
-
type MF["Entry"]
|
16
|
-
|
17
|
-
property :name, :predicate => MF["name"], :type => XSD.string
|
18
|
-
property :comment, :predicate => RDF::RDFS.comment, :type => XSD.string
|
19
|
-
property :result, :predicate => MF.result
|
20
|
-
has_many :action, :predicate => MF["action"]
|
21
|
-
|
22
|
-
def name
|
23
|
-
inputDocument.to_s.split("/").last
|
24
|
-
end
|
25
|
-
|
26
|
-
def input
|
27
|
-
Kernel.open(self.inputDocument)
|
28
|
-
end
|
29
|
-
|
30
|
-
def output
|
31
|
-
self.result ? Kernel.open(self.result) : ""
|
32
|
-
end
|
33
|
-
|
34
|
-
def inputDocument
|
35
|
-
self.class.repository.first_object(:subject => self.action.first)
|
36
|
-
end
|
37
|
-
|
38
|
-
def inspect
|
39
|
-
"[#{self.class.to_s} " + %w(
|
40
|
-
subject
|
41
|
-
name
|
42
|
-
comment
|
43
|
-
result
|
44
|
-
inputDocument
|
45
|
-
).map {|a| v = self.send(a); "#{a}='#{v}'" if v}.compact.join(", ") +
|
46
|
-
"]"
|
47
|
-
end
|
48
|
-
|
49
|
-
# Run test case, yields input for parser to create triples
|
50
|
-
def run_test(options = {})
|
51
|
-
# Run
|
52
|
-
graph = yield
|
53
|
-
|
54
|
-
return unless self.result
|
55
|
-
|
56
|
-
case self.compare
|
57
|
-
when :none
|
58
|
-
# Don't check output, just parse to graph
|
59
|
-
when :array
|
60
|
-
@parser.graph.should be_equivalent_graph(self.output, self)
|
61
|
-
else
|
62
|
-
#puts "parse #{self.outputDocument} as #{RDF::Reader.for(self.outputDocument)}"
|
63
|
-
format = detect_format(self.output)
|
64
|
-
output_graph = RDF::Graph.load(self.result, :format => format, :base_uri => self.inputDocument)
|
65
|
-
puts "result: #{CGI.escapeHTML(graph.to_ntriples)}" if ::RDF::N3::debug?
|
66
|
-
graph.should Matchers::be_equivalent_graph(output_graph, self)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
class Good < Entry
|
72
|
-
default_source :turtle
|
73
|
-
end
|
74
|
-
|
75
|
-
class Bad < Entry
|
76
|
-
default_source :turtle_bad
|
77
|
-
end
|
78
|
-
|
79
|
-
turtle = RDF::Repository.load("http://www.w3.org/2001/sw/DataAccess/df1/tests/manifest.ttl")
|
80
|
-
|
81
|
-
# Add types to entries
|
82
|
-
turtle.subjects(:predicate => MF["name"]).each do |s|
|
83
|
-
turtle << RDF::Statement.new(s, RDF.type, MF["Entry"])
|
84
|
-
end
|
85
|
-
|
86
|
-
Spira.add_repository! :turtle, turtle
|
87
|
-
|
88
|
-
turtle_bad = RDF::Repository.load("http://www.w3.org/2001/sw/DataAccess/df1/tests/manifest-bad.ttl")
|
89
|
-
|
90
|
-
# Add types to entries
|
91
|
-
turtle_bad.subjects(:predicate => MF["name"]).each do |s|
|
92
|
-
turtle_bad << RDF::Statement.new(s, RDF.type, MF["Entry"])
|
93
|
-
end
|
94
|
-
|
95
|
-
Spira.add_repository! :turtle_bad, turtle_bad
|
96
|
-
|
97
|
-
end
|
98
|
-
end
|
data/spec/writer_spec.rb
DELETED
@@ -1,399 +0,0 @@
|
|
1
|
-
$:.unshift "."
|
2
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
3
|
-
|
4
|
-
describe RDF::N3::Writer do
|
5
|
-
describe "simple tests" do
|
6
|
-
it "should use full URIs without base" do
|
7
|
-
input = %(<http://a/b> <http://a/c> <http://a/d> .)
|
8
|
-
serialize(input, nil, [%r(^<http://a/b> <http://a/c> <http://a/d> \.$)])
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should use relative URIs with base" do
|
12
|
-
input = %(<http://a/b> <http://a/c> <http://a/d> .)
|
13
|
-
serialize(input, "http://a/",
|
14
|
-
[ %r(^@base <http://a/> \.$),
|
15
|
-
%r(^<b> <c> <d> \.$)]
|
16
|
-
)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "should use qname URIs with prefix" do
|
20
|
-
input = %(<http://xmlns.com/foaf/0.1/b> <http://xmlns.com/foaf/0.1/c> <http://xmlns.com/foaf/0.1/d> .)
|
21
|
-
serialize(input, nil,
|
22
|
-
[%r(^@prefix foaf: <http://xmlns.com/foaf/0.1/> \.$),
|
23
|
-
%r(^foaf:b foaf:c foaf:d \.$)],
|
24
|
-
:prefixes => { :foaf => RDF::FOAF}
|
25
|
-
)
|
26
|
-
end
|
27
|
-
|
28
|
-
it "should use qname URIs with empty prefix" do
|
29
|
-
input = %(<http://xmlns.com/foaf/0.1/b> <http://xmlns.com/foaf/0.1/c> <http://xmlns.com/foaf/0.1/d> .)
|
30
|
-
serialize(input, nil,
|
31
|
-
[%r(^@prefix : <http://xmlns.com/foaf/0.1/> \.$),
|
32
|
-
%r(^:b :c :d \.$)],
|
33
|
-
:prefixes => { "" => RDF::FOAF}
|
34
|
-
)
|
35
|
-
end
|
36
|
-
|
37
|
-
# see example-files/arnau-registered-vocab.rb
|
38
|
-
it "should use qname URIs with empty suffix" do
|
39
|
-
input = %(<http://xmlns.com/foaf/0.1/> <http://xmlns.com/foaf/0.1/> <http://xmlns.com/foaf/0.1/> .)
|
40
|
-
serialize(input, nil,
|
41
|
-
[%r(^@prefix foaf: <http://xmlns.com/foaf/0.1/> \.$),
|
42
|
-
%r(^foaf: foaf: foaf: \.$)],
|
43
|
-
:prefixes => { "foaf" => RDF::FOAF}
|
44
|
-
)
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should not use qname with illegal local part" do
|
48
|
-
input = %(
|
49
|
-
@prefix db: <http://dbpedia.org/resource/> .
|
50
|
-
@prefix dbo: <http://dbpedia.org/ontology/> .
|
51
|
-
db:Michael_Jackson dbo:artistOf <http://dbpedia.org/resource/%28I_Can%27t_Make_It%29_Another_Day> .
|
52
|
-
)
|
53
|
-
|
54
|
-
serialize(input, nil,
|
55
|
-
[%r(^@prefix db: <http://dbpedia.org/resource/> \.$),
|
56
|
-
%r(^db:Michael_Jackson dbo:artistOf <http://dbpedia.org/resource/%28I_Can%27t_Make_It%29_Another_Day> \.$)],
|
57
|
-
:prefixes => {
|
58
|
-
"db" => RDF::URI("http://dbpedia.org/resource/"),
|
59
|
-
"dbo" => RDF::URI("http://dbpedia.org/ontology/")}
|
60
|
-
)
|
61
|
-
end
|
62
|
-
|
63
|
-
it "should order properties" do
|
64
|
-
input = %(
|
65
|
-
@prefix : <http://xmlns.com/foaf/0.1/> .
|
66
|
-
@prefix dc: <http://purl.org/dc/elements/1.1/> .
|
67
|
-
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
|
68
|
-
:b :c :d .
|
69
|
-
:b dc:title "title" .
|
70
|
-
:b a :class .
|
71
|
-
:b rdfs:label "label" .
|
72
|
-
)
|
73
|
-
serialize(input, nil,
|
74
|
-
[%r(^\s+a :class;$),
|
75
|
-
%r(^\s+rdfs:label "label"),
|
76
|
-
%r(^:b dc:title \"title\"),
|
77
|
-
%r(^\s+:c :d)],
|
78
|
-
:prefixes => { "" => RDF::FOAF, :dc => "http://purl.org/dc/elements/1.1/", :rdfs => RDF::RDFS}
|
79
|
-
)
|
80
|
-
end
|
81
|
-
|
82
|
-
it "should generate object list" do
|
83
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . :b :c :d, :e .)
|
84
|
-
serialize(input, nil,
|
85
|
-
[%r(^@prefix : <http://xmlns.com/foaf/0.1/> \.$),
|
86
|
-
%r(^:b :c :d,$),
|
87
|
-
%r(^\s+:e \.$)],
|
88
|
-
:prefixes => { "" => RDF::FOAF}
|
89
|
-
)
|
90
|
-
end
|
91
|
-
|
92
|
-
it "should generate property list" do
|
93
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . :b :c :d; :e :f .)
|
94
|
-
serialize(input, nil,
|
95
|
-
[%r(^@prefix : <http://xmlns.com/foaf/0.1/> \.$),
|
96
|
-
%r(^:b :c :d;$),
|
97
|
-
%r(^\s+:e :f \.$)],
|
98
|
-
:prefixes => { "" => RDF::FOAF}
|
99
|
-
)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
describe "anons" do
|
104
|
-
it "should generate bare anon" do
|
105
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . [:a :b] .)
|
106
|
-
serialize(input, nil,
|
107
|
-
[%r(^\s*\[ :a :b\] \.$)],
|
108
|
-
:prefixes => { "" => RDF::FOAF}
|
109
|
-
)
|
110
|
-
end
|
111
|
-
|
112
|
-
it "should generate anon as subject" do
|
113
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . [:a :b] :c :d .)
|
114
|
-
serialize(input, nil,
|
115
|
-
[%r(^\s*\[ :a :b;$),
|
116
|
-
%r(^\s+:c :d\] \.$)],
|
117
|
-
:prefixes => { "" => RDF::FOAF}
|
118
|
-
)
|
119
|
-
end
|
120
|
-
|
121
|
-
it "should generate anon as object" do
|
122
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . :a :b [:c :d] .)
|
123
|
-
serialize(input, nil,
|
124
|
-
[%r(^\s*\:a :b \[ :c :d\] \.$)],
|
125
|
-
:prefixes => { "" => RDF::FOAF}
|
126
|
-
)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
describe "lists" do
|
131
|
-
it "should generate bare list" do
|
132
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . (:a :b) .)
|
133
|
-
serialize(input, nil,
|
134
|
-
[%r(^\(:a :b\) \.$)],
|
135
|
-
:prefixes => { "" => RDF::FOAF}
|
136
|
-
)
|
137
|
-
end
|
138
|
-
|
139
|
-
it "should generate literal list" do
|
140
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . :a :b ( "apple" "banana" ) .)
|
141
|
-
serialize(input, nil,
|
142
|
-
[%r(^:a :b \("apple" "banana"\) \.$)],
|
143
|
-
:prefixes => { "" => RDF::FOAF}
|
144
|
-
)
|
145
|
-
end
|
146
|
-
|
147
|
-
it "should generate empty list" do
|
148
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . :a :b () .)
|
149
|
-
serialize(input, nil,
|
150
|
-
[%r(^:a :b \(\) \.$)],
|
151
|
-
:prefixes => { "" => RDF::FOAF}
|
152
|
-
)
|
153
|
-
end
|
154
|
-
|
155
|
-
it "should generate empty list(2)" do
|
156
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . :emptyList = () .)
|
157
|
-
serialize(input, nil,
|
158
|
-
[%r(^:emptyList (<.*sameAs>|owl:sameAs) \(\) \.$)],
|
159
|
-
:prefixes => { "" => RDF::FOAF}
|
160
|
-
)
|
161
|
-
end
|
162
|
-
|
163
|
-
it "should generate empty list as subject" do
|
164
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . () :a :b .)
|
165
|
-
serialize(input, nil,
|
166
|
-
[%r(^\(\) :a :b \.$)],
|
167
|
-
:prefixes => { "" => RDF::FOAF}
|
168
|
-
)
|
169
|
-
end
|
170
|
-
|
171
|
-
it "should generate list as subject" do
|
172
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . (:a) :b :c .)
|
173
|
-
serialize(input, nil,
|
174
|
-
[%r(^\(:a\) :b :c \.$)],
|
175
|
-
:prefixes => { "" => RDF::FOAF}
|
176
|
-
)
|
177
|
-
end
|
178
|
-
|
179
|
-
it "should generate list of empties" do
|
180
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . :listOf2Empties = (() ()) .)
|
181
|
-
serialize(input, nil,
|
182
|
-
[%r(^:listOf2Empties (<.*sameAs>|owl:sameAs) \(\(\) \(\)\) \.$)],
|
183
|
-
:prefixes => { "" => RDF::FOAF}
|
184
|
-
)
|
185
|
-
end
|
186
|
-
|
187
|
-
it "should generate list anon" do
|
188
|
-
input = %(@prefix : <http://xmlns.com/foaf/0.1/> . :twoAnons = ([a :mother] [a :father]) .)
|
189
|
-
serialize(input, nil,
|
190
|
-
[%r(^:twoAnons (<.*sameAs>|owl:sameAs) \(\[\s*a :mother\] \[\s*a :father\]\) \.$)],
|
191
|
-
:prefixes => { "" => RDF::FOAF}
|
192
|
-
)
|
193
|
-
end
|
194
|
-
|
195
|
-
it "should generate owl:unionOf list" do
|
196
|
-
input = %(
|
197
|
-
@prefix : <http://xmlns.com/foaf/0.1/> .
|
198
|
-
@prefix owl: <http://www.w3.org/2002/07/owl#> .
|
199
|
-
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
|
200
|
-
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
|
201
|
-
:a rdfs:domain [
|
202
|
-
a owl:Class;
|
203
|
-
owl:unionOf [
|
204
|
-
a owl:Class;
|
205
|
-
rdf:first :b;
|
206
|
-
rdf:rest [
|
207
|
-
a owl:Class;
|
208
|
-
rdf:first :c;
|
209
|
-
rdf:rest rdf:nil
|
210
|
-
]
|
211
|
-
]
|
212
|
-
] .
|
213
|
-
)
|
214
|
-
#$verbose = true
|
215
|
-
serialize(input, nil,
|
216
|
-
[
|
217
|
-
%r(:a rdfs:domain \[\s*a owl:Class;\s+owl:unionOf\s+\(:b\s+:c\)\]\s*\.$)m,
|
218
|
-
%r(@prefix : <http://xmlns.com/foaf/0.1/> \.),
|
219
|
-
%r(@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \.),
|
220
|
-
],
|
221
|
-
:prefixes => { "" => RDF::FOAF, :rdfs => RDF::RDFS, :owl => RDF::OWL, :rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#"}
|
222
|
-
)
|
223
|
-
#$verbose = false
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
describe "literals" do
|
228
|
-
describe "plain" do
|
229
|
-
it "encodes embedded \"\"\"" do
|
230
|
-
n3 = %(:a :b """testing string parsing in N3.
|
231
|
-
""" .)
|
232
|
-
serialize(n3, nil, [/testing string parsing in N3.\n/])
|
233
|
-
end
|
234
|
-
|
235
|
-
it "encodes embedded \"" do
|
236
|
-
n3 = %(:a :b """string with " escaped quote marks""" .)
|
237
|
-
serialize(n3, nil, [/string with \\" escaped quote mark/])
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
describe "with language" do
|
242
|
-
it "specifies language for literal with language" do
|
243
|
-
ttl = %q(:a :b "string"@en .)
|
244
|
-
serialize(ttl, nil, [%r("string"@en)])
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
describe "xsd:anyURI" do
|
249
|
-
it "uses xsd namespace for datatype" do
|
250
|
-
ttl = %q(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b "http://foo/"^^xsd:anyURI .)
|
251
|
-
serialize(ttl, nil, [
|
252
|
-
%r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
|
253
|
-
%r("http://foo/"\^\^xsd:anyURI \.),
|
254
|
-
])
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
describe "xsd:boolean" do
|
259
|
-
[
|
260
|
-
[%q("true"^^xsd:boolean), /true ./],
|
261
|
-
[%q("TrUe"^^xsd:boolean), /true ./],
|
262
|
-
[%q("1"^^xsd:boolean), /true ./],
|
263
|
-
[%q(true), /true ./],
|
264
|
-
[%q("false"^^xsd:boolean), /false ./],
|
265
|
-
[%q("FaLsE"^^xsd:boolean), /false ./],
|
266
|
-
[%q("0"^^xsd:boolean), /false ./],
|
267
|
-
[%q(false), /false ./],
|
268
|
-
].each do |(l,r)|
|
269
|
-
it "uses token for #{l.inspect}" do
|
270
|
-
ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b #{l} .)
|
271
|
-
serialize(ttl, nil, [
|
272
|
-
%r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
|
273
|
-
r,
|
274
|
-
], :canonicalize => true)
|
275
|
-
end
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
describe "xsd:integer" do
|
280
|
-
[
|
281
|
-
[%q("1"^^xsd:integer), /1 ./],
|
282
|
-
[%q(1), /1 ./],
|
283
|
-
[%q("0"^^xsd:integer), /0 ./],
|
284
|
-
[%q(0), /0 ./],
|
285
|
-
[%q("10"^^xsd:integer), /10 ./],
|
286
|
-
[%q(10), /10 ./],
|
287
|
-
].each do |(l,r)|
|
288
|
-
it "uses token for #{l.inspect}" do
|
289
|
-
ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b #{l} .)
|
290
|
-
serialize(ttl, nil, [
|
291
|
-
%r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
|
292
|
-
r,
|
293
|
-
], :canonicalize => true)
|
294
|
-
end
|
295
|
-
end
|
296
|
-
end
|
297
|
-
|
298
|
-
describe "xsd:int" do
|
299
|
-
[
|
300
|
-
[%q("1"^^xsd:int), /"1"\^\^xsd:int ./],
|
301
|
-
[%q("0"^^xsd:int), /"0"\^\^xsd:int ./],
|
302
|
-
[%q("10"^^xsd:int), /"10"\^\^xsd:int ./],
|
303
|
-
].each do |(l,r)|
|
304
|
-
it "uses token for #{l.inspect}" do
|
305
|
-
ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b #{l} .)
|
306
|
-
serialize(ttl, nil, [
|
307
|
-
%r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
|
308
|
-
r,
|
309
|
-
], :canonicalize => true)
|
310
|
-
end
|
311
|
-
end
|
312
|
-
end
|
313
|
-
|
314
|
-
describe "xsd:decimal" do
|
315
|
-
[
|
316
|
-
[%q("1.0"^^xsd:decimal), /1.0 ./],
|
317
|
-
[%q(1.0), /1.0 ./],
|
318
|
-
[%q("0.1"^^xsd:decimal), /0.1 ./],
|
319
|
-
[%q(0.1), /0.1 ./],
|
320
|
-
[%q("10.02"^^xsd:decimal), /10.02 ./],
|
321
|
-
[%q(10.02), /10.02 ./],
|
322
|
-
].each do |(l,r)|
|
323
|
-
it "uses token for #{l.inspect}" do
|
324
|
-
ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b #{l} .)
|
325
|
-
serialize(ttl, nil, [
|
326
|
-
%r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
|
327
|
-
r,
|
328
|
-
], :canonicalize => true)
|
329
|
-
end
|
330
|
-
end
|
331
|
-
end
|
332
|
-
|
333
|
-
describe "xsd:double" do
|
334
|
-
[
|
335
|
-
[%q("1.0e1"^^xsd:double), /1.0e1 ./],
|
336
|
-
[%q(1.0e1), /1.0e1 ./],
|
337
|
-
[%q("0.1e1"^^xsd:double), /1.0e0 ./],
|
338
|
-
[%q(0.1e1), /1.0e0 ./],
|
339
|
-
[%q("10.02e1"^^xsd:double), /1.002e2 ./],
|
340
|
-
[%q(10.02e1), /1.002e2 ./],
|
341
|
-
].each do |(l,r)|
|
342
|
-
it "uses token for #{l.inspect}" do
|
343
|
-
ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b #{l} .)
|
344
|
-
serialize(ttl, nil, [
|
345
|
-
%r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
|
346
|
-
r,
|
347
|
-
], :canonicalize => true)
|
348
|
-
end
|
349
|
-
end
|
350
|
-
end
|
351
|
-
end
|
352
|
-
|
353
|
-
# W3C Turtle Test suite from http://www.w3.org/2000/10/swap/test/regression.n3
|
354
|
-
describe "w3c turtle tests" do
|
355
|
-
require 'turtle_test'
|
356
|
-
|
357
|
-
Fixtures::TurtleTest::Good.each do |t|
|
358
|
-
next unless t.comment
|
359
|
-
#puts t.inspect
|
360
|
-
#next unless t.name == "test-04"
|
361
|
-
next if t.name == "test-29" # FIXME
|
362
|
-
|
363
|
-
specify "#{t.name}: #{t.comment}" do
|
364
|
-
@graph = parse(t.output, :base_uri => t.result, :format => :ntriples)
|
365
|
-
n3 = serialize(t.output, t.result, [], :format => :n3)
|
366
|
-
g2 = parse(n3, :base_uri => t.result)
|
367
|
-
g2.should be_equivalent_graph(@graph, :trace => @debug.join("\n"))
|
368
|
-
end
|
369
|
-
end
|
370
|
-
end
|
371
|
-
|
372
|
-
def parse(input, options = {})
|
373
|
-
graph = RDF::Graph.new
|
374
|
-
RDF::N3::Reader.new(input, options).each do |statement|
|
375
|
-
graph << statement
|
376
|
-
end
|
377
|
-
graph
|
378
|
-
end
|
379
|
-
|
380
|
-
# Serialize ntstr to a string and compare against regexps
|
381
|
-
def serialize(ntstr, base = nil, regexps = [], options = {})
|
382
|
-
prefixes = options[:prefixes] || {}
|
383
|
-
g = parse(ntstr, :base_uri => base, :prefixes => prefixes)
|
384
|
-
@debug = []
|
385
|
-
result = RDF::N3::Writer.buffer(options.merge(:debug => @debug, :base_uri => base, :prefixes => prefixes)) do |writer|
|
386
|
-
writer << g
|
387
|
-
end
|
388
|
-
if $verbose
|
389
|
-
require 'cgi'
|
390
|
-
#puts CGI.escapeHTML(result)
|
391
|
-
end
|
392
|
-
|
393
|
-
regexps.each do |re|
|
394
|
-
result.should match_re(re, :about => base, :trace => @debug, :inputDocument => ntstr)
|
395
|
-
end
|
396
|
-
|
397
|
-
result
|
398
|
-
end
|
399
|
-
end
|