tommorris-rena 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +13 -9
- data/lib/rena/bnode.rb +65 -59
- data/lib/rena/graph.rb +173 -135
- data/lib/rena/literal.rb +198 -80
- data/lib/rena/n3_grammar.treetop +129 -0
- data/lib/rena/n3parser.rb +145 -0
- data/lib/rena/namespace.rb +68 -66
- data/lib/rena/rdfxmlparser.rb +156 -150
- data/lib/rena/rexml_hacks.rb +36 -9
- data/lib/rena/triple.rb +80 -63
- data/lib/rena/uriref.rb +41 -27
- data/rena.gemspec +8 -7
- data/{test/spec/bnode.spec.rb → spec/bnode_spec.rb} +5 -1
- data/{test/spec/graph.spec.rb → spec/graph_spec.rb} +26 -7
- data/spec/literal_spec.rb +136 -0
- data/{test/spec/namespaces.spec.rb → spec/namespaces_spec.rb} +0 -0
- data/{test/spec/parser.spec.rb → spec/parser_spec.rb} +27 -1
- data/{test/spec/rexml_hacks.spec.rb → spec/rexml_hacks_spec.rb} +3 -4
- data/spec/triple_spec.rb +100 -0
- data/{test/spec/uriref.spec.rb → spec/uriref_spec.rb} +14 -1
- metadata +24 -12
- data/test/spec/literal.spec.rb +0 -112
- data/test/spec/triple.spec.rb +0 -32
File without changes
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'lib/rena'
|
2
|
+
include Rena
|
2
3
|
|
3
4
|
# w3c test suite: http://www.w3.org/TR/rdf-testcases/
|
4
5
|
|
@@ -161,7 +162,7 @@ describe "RDF/XML Parser" do
|
|
161
162
|
EOF
|
162
163
|
|
163
164
|
graph = RdfXmlParser.new(sampledoc, "http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-xml-literal-namespaces/test001.rdf")
|
164
|
-
graph.graph.to_ntriples.should == "<http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-xml-literal-namespaces/test001.rdf#John_Smith> <http://my.example.org/Name> \"<html:h1>\n <b>John</b>\n </html:h1>\"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
|
165
|
+
graph.graph.to_ntriples.should == "<http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-xml-literal-namespaces/test001.rdf#John_Smith> <http://my.example.org/Name> \"<html:h1>\n <b>John</b>\n </html:h1>\"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral> ."
|
165
166
|
end
|
166
167
|
|
167
168
|
it "should pass rdfms-syntax-incomplete-test001" do
|
@@ -285,4 +286,29 @@ describe "RDF/XML Parser" do
|
|
285
286
|
graph = RdfXmlParser.new(sampledoc)
|
286
287
|
end.should raise_error
|
287
288
|
end
|
289
|
+
|
290
|
+
describe "parsing rdf files" do
|
291
|
+
def test_file(filepath, uri = nil)
|
292
|
+
n3_string = File.read(filepath)
|
293
|
+
parser = RdfXmlParser.new(n3_string, uri)
|
294
|
+
ntriples = parser.graph.to_ntriples
|
295
|
+
ntriples.gsub!(/_:bn\d+/, '_:node1')
|
296
|
+
ntriples = ntriples.split("\n").sort
|
297
|
+
|
298
|
+
nt_string = File.read(filepath.sub('.rdf', '.nt'))
|
299
|
+
nt_string = nt_string.split("\n").sort
|
300
|
+
|
301
|
+
ntriples.should == nt_string
|
302
|
+
end
|
303
|
+
|
304
|
+
before(:all) do
|
305
|
+
@rdf_dir = File.join(File.dirname(__FILE__), '..', 'test', 'rdf_tests')
|
306
|
+
end
|
307
|
+
|
308
|
+
# it "should parse Coldplay's BBC Music profile" do
|
309
|
+
# gid = 'cc197bad-dc9c-440d-a5b5-d52ba2e14234'
|
310
|
+
# file = File.join(@rdf_dir, "#{gid}.rdf")
|
311
|
+
# test_file(file, "http://www.bbc.co.uk/music/artists/#{gid}")
|
312
|
+
# end
|
313
|
+
end
|
288
314
|
end
|
@@ -68,9 +68,8 @@ describe "REXML" do
|
|
68
68
|
</rdf:RDF>
|
69
69
|
EOF
|
70
70
|
doc2 = REXML::Document.new(sampledoc)
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
# pending
|
71
|
+
expectedoutput_str = "<html:h1 xmlns:html=\'http://NoHTML.example.org\' xmlns:my=\'http://my.example.org/\' xmlns:rdf=\'http://www.w3.org/1999/02/22-rdf-syntax-ns#\'>\n <b xmlns=\'http://www.w3.org/1999/xhtml\'>John</b>\n </html:h1>"
|
72
|
+
expectedout = REXML::Document.new(expectedoutput_str)
|
73
|
+
doc2.root.elements[1].elements[1].elements[1] == expectedout.root
|
75
74
|
end
|
76
75
|
end
|
data/spec/triple_spec.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'lib/rena'
|
2
|
+
|
3
|
+
describe "Triples" do
|
4
|
+
it "should have a subject" do
|
5
|
+
f = Triple.new(BNode.new, URIRef.new('http://xmlns.com/foaf/0.1/knows'), BNode.new)
|
6
|
+
f.subject.class.should == BNode
|
7
|
+
# puts f.to_ntriples
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should require that the subject is a URIRef or BNode" do
|
11
|
+
lambda do
|
12
|
+
Triple.new(Literal.new("foo"), URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new)
|
13
|
+
end.should raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should require that the predicate is a URIRef" do
|
17
|
+
lambda do
|
18
|
+
Triple.new(BNode.new, BNode.new, BNode.new)
|
19
|
+
end.should raise_error
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should require that the object is a URIRef, BNode, Literal or Typed Literal" do
|
23
|
+
lambda do
|
24
|
+
Triple.new(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), [])
|
25
|
+
end.should raise_error
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should emit an NTriple" do
|
29
|
+
s = URIRef.new("http://tommorris.org/foaf#me")
|
30
|
+
p = URIRef.new("http://xmlns.com/foaf/0.1/name")
|
31
|
+
o = Literal.untyped("Tom Morris")
|
32
|
+
f = Triple.new(s,p,o)
|
33
|
+
|
34
|
+
f.to_ntriples.should == "<http://tommorris.org/foaf#me> <http://xmlns.com/foaf/0.1/name> \"Tom Morris\" ."
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#coerce_subject" do
|
38
|
+
it "should accept a URIRef" do
|
39
|
+
ref = URIRef.new('http://localhost/')
|
40
|
+
Triple.coerce_subject(ref).should == ref
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should accept a BNode" do
|
44
|
+
node = BNode.new('a')
|
45
|
+
Triple.coerce_subject(node).should == node
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should accept a uri string and make URIRef" do
|
49
|
+
Triple.coerce_subject('http://localhost/').should == URIRef.new('http://localhost/')
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should turn an other string into a BNode" do
|
53
|
+
Triple.coerce_subject('foo').should == BNode.new('foo')
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should raise an InvalidSubject exception with any other class argument" do
|
57
|
+
lambda do
|
58
|
+
Triple.coerce_subject(Object.new)
|
59
|
+
end.should raise_error(Rena::Triple::InvalidSubject)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#coerce_predicate" do
|
64
|
+
it "should make a string into a URI ref" do
|
65
|
+
Triple.coerce_predicate("http://localhost/").should == URIRef.new('http://localhost')
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should leave a URIRef alone" do
|
69
|
+
ref = URIRef.new('http://localhost/')
|
70
|
+
Triple.coerce_predicate(ref).should == ref
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should barf on an illegal uri string" do
|
74
|
+
lambda do
|
75
|
+
Triple.coerce_predicate("I'm just a soul whose intention is good")
|
76
|
+
end.should raise_error(Rena::Triple::InvalidPredicate)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "#coerce_object" do
|
81
|
+
it "should leave URIRefs alone" do
|
82
|
+
ref = URIRef.new("http://localhost/")
|
83
|
+
Triple.coerce_object(ref).should == ref
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should leave BNodes alone" do
|
87
|
+
ref = BNode.new()
|
88
|
+
Triple.coerce_object(ref).should == ref
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should leave Literals alone" do
|
92
|
+
ref = Literal.untyped('foo')
|
93
|
+
Triple.coerce_object(ref).should == ref
|
94
|
+
|
95
|
+
typedref = Literal.build_from('foo')
|
96
|
+
Triple.coerce_object(ref).should == ref
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'webrick'
|
2
|
+
include WEBrick
|
1
3
|
require 'lib/rena'
|
2
4
|
#require 'lib/uriref'
|
3
5
|
|
@@ -25,6 +27,17 @@ describe "URI References" do
|
|
25
27
|
# end.should_not raise_error
|
26
28
|
# end
|
27
29
|
|
30
|
+
it "should return the 'last fragment' name" do
|
31
|
+
fragment = URIRef.new("http://example.org/foo#bar")
|
32
|
+
fragment.short_name.should == "bar"
|
33
|
+
|
34
|
+
path = URIRef.new("http://example.org/foo/bar")
|
35
|
+
path.short_name.should == "bar"
|
36
|
+
|
37
|
+
nonetest = URIRef.new("http://example.org/")
|
38
|
+
nonetest.short_name.should == false
|
39
|
+
end
|
40
|
+
|
28
41
|
it "produce a valid URI character sequence (per RFC 2396 §2.1) representing an absolute URI with optional fragment identifier" do
|
29
42
|
pending "TODO: figure out a series of tests for RFC 2396 §2.1 adherence"
|
30
43
|
end
|
@@ -44,4 +57,4 @@ describe "URI References" do
|
|
44
57
|
it "should discourage use of %-escaped characters" do
|
45
58
|
pending "TODO: figure out a way to discourage %-escaped character usage"
|
46
59
|
end
|
47
|
-
end
|
60
|
+
end
|
metadata
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tommorris-rena
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Morris
|
8
8
|
- Pius Uzamere
|
9
|
+
- Patrick Sinclair
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
13
|
|
13
|
-
date: 2008-
|
14
|
+
date: 2008-10-05 00:00:00 -07:00
|
14
15
|
default_executable:
|
15
16
|
dependencies:
|
16
17
|
- !ruby/object:Gem::Dependency
|
@@ -18,9 +19,18 @@ dependencies:
|
|
18
19
|
version_requirement:
|
19
20
|
version_requirements: !ruby/object:Gem::Requirement
|
20
21
|
requirements:
|
21
|
-
- - "
|
22
|
+
- - ">="
|
22
23
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
24
|
+
version: 1.0.4
|
25
|
+
version:
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: treetop
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.4
|
24
34
|
version:
|
25
35
|
description: Rena is a Ruby library for manipulating RDF files.
|
26
36
|
email: tom@tommorris.org
|
@@ -38,6 +48,8 @@ files:
|
|
38
48
|
- lib/rena/bnode.rb
|
39
49
|
- lib/rena/graph.rb
|
40
50
|
- lib/rena/literal.rb
|
51
|
+
- lib/rena/n3parser.rb
|
52
|
+
- lib/rena/n3_grammar.treetop
|
41
53
|
- lib/rena/namespace.rb
|
42
54
|
- lib/rena/rdfxmlparser.rb
|
43
55
|
- lib/rena/rexml_hacks.rb
|
@@ -74,11 +86,11 @@ summary: Ruby RDF library.
|
|
74
86
|
test_files:
|
75
87
|
- test/test_uris.rb
|
76
88
|
- test/xml.rdf
|
77
|
-
-
|
78
|
-
-
|
79
|
-
-
|
80
|
-
-
|
81
|
-
-
|
82
|
-
-
|
83
|
-
-
|
84
|
-
-
|
89
|
+
- spec/bnode_spec.rb
|
90
|
+
- spec/graph_spec.rb
|
91
|
+
- spec/literal_spec.rb
|
92
|
+
- spec/namespaces_spec.rb
|
93
|
+
- spec/parser_spec.rb
|
94
|
+
- spec/rexml_hacks_spec.rb
|
95
|
+
- spec/triple_spec.rb
|
96
|
+
- spec/uriref_spec.rb
|
data/test/spec/literal.spec.rb
DELETED
@@ -1,112 +0,0 @@
|
|
1
|
-
require 'lib/rena'
|
2
|
-
|
3
|
-
describe "Literals" do
|
4
|
-
it "accept a language tag" do
|
5
|
-
f = Literal.new("tom", "en")
|
6
|
-
f.lang.should == "en"
|
7
|
-
end
|
8
|
-
|
9
|
-
it "accepts an encoding" do
|
10
|
-
f = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#string")
|
11
|
-
f.encoding.should == "http://www.w3.org/2001/XMLSchema#string"
|
12
|
-
end
|
13
|
-
|
14
|
-
it "should be equal if they have the same contents" do
|
15
|
-
f = Literal.new("tom")
|
16
|
-
g = Literal.new("tom")
|
17
|
-
f.should == g
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should not be equal if they do not have the same contents" do
|
21
|
-
f = Literal.new("tom")
|
22
|
-
g = Literal.new("tim")
|
23
|
-
f.should_not == g
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should be equal if they have the same contents and language" do
|
27
|
-
f = Literal.new("tom", "en")
|
28
|
-
g = Literal.new("tom", "en")
|
29
|
-
f.should == g
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should not be equal if they do not have the same contents and language" do
|
33
|
-
f = Literal.new("tom", "en")
|
34
|
-
g = Literal.new("tim", "en")
|
35
|
-
f.should_not == g
|
36
|
-
|
37
|
-
lf = Literal.new("tom", "en")
|
38
|
-
lg = Literal.new("tom", "fr")
|
39
|
-
lf.should_not == lg
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should be equal if they have the same contents and datatype" do
|
43
|
-
f = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#string")
|
44
|
-
g = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#string")
|
45
|
-
f.should == g
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should not be equal if they do not have the same contents and datatype" do
|
49
|
-
f = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#string")
|
50
|
-
g = TypedLiteral.new("tim", "http://www.w3.org/2001/XMLSchema#string")
|
51
|
-
f.should_not == g
|
52
|
-
|
53
|
-
dtf = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#string")
|
54
|
-
dtg = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#token")
|
55
|
-
dtf.should_not == dtg
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should return valid N3/NTriples format strings" do
|
59
|
-
f = Literal.new("tom")
|
60
|
-
f.to_n3.should == "\"tom\""
|
61
|
-
f.to_ntriples.should == f.to_n3
|
62
|
-
|
63
|
-
g = Literal.new("tom", "en")
|
64
|
-
g.to_n3.should == "\"tom\"@en"
|
65
|
-
f.to_ntriples.should == f.to_n3
|
66
|
-
|
67
|
-
typed_int = TypedLiteral.new(5, "http://www.w3.org/2001/XMLSchema#int")
|
68
|
-
typed_int.to_n3.should == "5^^<http://www.w3.org/2001/XMLSchema#int>"
|
69
|
-
typed_int.to_ntriples.should == typed_int.to_n3
|
70
|
-
|
71
|
-
typed_string = TypedLiteral.new("foo", "http://www.w3.org/2001/XMLSchema#string")
|
72
|
-
typed_string.to_n3.should == "\"foo\"^^<http://www.w3.org/2001/XMLSchema#string>"
|
73
|
-
typed_string.to_ntriples.should == typed_string.to_n3
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should normalize language tags to lower case" do
|
77
|
-
f = Literal.new("tom", "EN")
|
78
|
-
f.lang.should == "en"
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should support TriX encoding" do
|
82
|
-
e = Literal.new("tom")
|
83
|
-
e.to_trix.should == "<plainLiteral>tom</plainLiteral>"
|
84
|
-
|
85
|
-
f = Literal.new("tom", "en")
|
86
|
-
f.to_trix.should == "<plainLiteral xml:lang=\"en\">tom</plainLiteral>"
|
87
|
-
|
88
|
-
g = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#string")
|
89
|
-
g.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#string\">tom</typedLiteral>"
|
90
|
-
end
|
91
|
-
|
92
|
-
it "should handle XML litearls" do
|
93
|
-
# first we should detect XML literals and mark them as such in the class
|
94
|
-
f = TypedLiteral.new("foo <sup>bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
|
95
|
-
f.xmlliteral?.should == true
|
96
|
-
# pending "TODO: the thought of XML literals makes me want to wretch"
|
97
|
-
end
|
98
|
-
|
99
|
-
it "should be able to infer!" do
|
100
|
-
int = TypedLiteral.new(15, nil)
|
101
|
-
int.infer!
|
102
|
-
int.encoding.should == "http://www.w3.org/2001/XMLSchema#int"
|
103
|
-
|
104
|
-
float = TypedLiteral.new(15.4, nil)
|
105
|
-
float.infer!
|
106
|
-
float.encoding.should == "http://www.w3.org/2001/XMLSchema#float"
|
107
|
-
|
108
|
-
other = TypedLiteral.new("foo", nil)
|
109
|
-
other.infer!
|
110
|
-
other.encoding.should == "http://www.w3.org/2001/XMLSchema#string"
|
111
|
-
end
|
112
|
-
end
|
data/test/spec/triple.spec.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'lib/rena'
|
2
|
-
|
3
|
-
describe "Triples" do
|
4
|
-
it "should have a subject" do
|
5
|
-
f = Triple.new(BNode.new, URIRef.new('http://xmlns.com/foaf/0.1/knows'), BNode.new)
|
6
|
-
f.subject.class.should == BNode
|
7
|
-
# puts f.to_ntriples
|
8
|
-
end
|
9
|
-
|
10
|
-
it "should require that the subject is a URIRef or BNode" do
|
11
|
-
lambda do
|
12
|
-
Triple.new(Literal.new("foo"), URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new)
|
13
|
-
end.should raise_error
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should require that the predicate is a URIRef" do
|
17
|
-
lambda do
|
18
|
-
Triple.new(BNode.new, BNode.new, BNode.new)
|
19
|
-
end.should raise_error
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should require that the object is a URIRef, BNode, Literal or Typed Literal" do
|
23
|
-
lambda do
|
24
|
-
Triple.new(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), [])
|
25
|
-
end.should raise_error
|
26
|
-
end
|
27
|
-
|
28
|
-
it "should emit an NTriple" do
|
29
|
-
f = Triple.new(URIRef.new("http://tommorris.org/foaf#me"), URIRef.new("http://xmlns.com/foaf/0.1/name"), Literal.new("Tom Morris"))
|
30
|
-
f.to_ntriples.should == "<http://tommorris.org/foaf#me> <http://xmlns.com/foaf/0.1/name> \"Tom Morris\" ."
|
31
|
-
end
|
32
|
-
end
|