tommorris-rena 0.0.1 → 0.0.2
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/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
data/Rakefile
CHANGED
@@ -7,11 +7,11 @@ task :default => [:spec]
|
|
7
7
|
desc "Install dependencies"
|
8
8
|
task :dependencies do
|
9
9
|
require ''
|
10
|
-
gems = ['addressable/uri']
|
10
|
+
gems = ['addressable/uri', 'treetop']
|
11
11
|
gems.each do |g|
|
12
12
|
g2 = g.split('/')[0]
|
13
13
|
begin
|
14
|
-
require
|
14
|
+
require g
|
15
15
|
rescue
|
16
16
|
sh "sudo gem install " + g2
|
17
17
|
end
|
@@ -24,27 +24,31 @@ task :push do
|
|
24
24
|
sh "growlnotify -m \"Updates pushed\" \"Git\""
|
25
25
|
end
|
26
26
|
|
27
|
-
desc "Runs specs"
|
28
27
|
task :spec do
|
29
|
-
sh "spec --colour
|
28
|
+
sh "spec --colour spec"
|
30
29
|
end
|
31
30
|
|
32
31
|
desc "Turns spec results into HTML and publish to web (Tom only!)"
|
33
32
|
task :spec_html do
|
34
|
-
sh "spec --
|
33
|
+
sh "spec --format html:rena_new_spec.html spec"
|
35
34
|
sh "scp rena_new_spec.html bbcityco@bbcity.co.uk:www/tom/files/rena_new_spec.html"
|
36
35
|
sh "rm rena_new_spec.html"
|
37
36
|
end
|
38
37
|
|
39
38
|
desc "Turns spec results into local HTML"
|
40
39
|
task :spec_local do
|
41
|
-
sh "spec --
|
40
|
+
sh "spec --format html:rena_new_spec.html spec/"
|
42
41
|
# sh "open rena_new_spec.html"
|
43
42
|
end
|
44
43
|
|
45
44
|
desc "Run specs through RCov"
|
46
45
|
Spec::Rake::SpecTask.new('coverage') do |t|
|
47
|
-
t.spec_files = FileList['
|
46
|
+
t.spec_files = FileList['spec']
|
48
47
|
t.rcov = true
|
49
|
-
t.rcov_opts = ['--exclude', 'test,\/Library\/Ruby\/Gems\/1.8\/gems']
|
50
|
-
end
|
48
|
+
t.rcov_opts = ['--exclude', 'spec,test,\/Library\/Ruby\/Gems\/1.8\/gems']
|
49
|
+
end
|
50
|
+
|
51
|
+
desc "Runs specs on JRuby"
|
52
|
+
task :jspec do
|
53
|
+
sh "jruby -S `whereis spec` --colour spec"
|
54
|
+
end
|
data/lib/rena/bnode.rb
CHANGED
@@ -1,63 +1,69 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module Rena
|
2
|
+
class BNode
|
3
|
+
attr_accessor :identifier
|
4
|
+
def initialize(identifier = nil)
|
5
|
+
if identifier != nil && self.valid_id?(identifier) != false
|
6
|
+
@identifier = identifier
|
7
|
+
else
|
8
|
+
@identifier = "bn" + self.hash.to_s
|
9
|
+
end
|
8
10
|
end
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
if self.identifier == eql.identifier
|
13
|
-
true
|
14
|
-
else
|
15
|
-
false
|
11
|
+
|
12
|
+
def eql? (other)
|
13
|
+
other.is_a?(self.class) && other.identifier == self.identifier
|
16
14
|
end
|
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
|
-
|
15
|
+
|
16
|
+
alias_method :==, :eql?
|
17
|
+
|
18
|
+
##
|
19
|
+
# Exports the BNode in N-Triples form.
|
20
|
+
#
|
21
|
+
# ==== Example
|
22
|
+
# b = BNode.new; b.to_n3 # => returns a string of the BNode in n3 form
|
23
|
+
#
|
24
|
+
# ==== Returns
|
25
|
+
# @return [String] The BNode in n3.
|
26
|
+
#
|
27
|
+
# @author Tom Morris
|
28
|
+
|
29
|
+
def to_n3
|
30
|
+
"_:" + @identifier
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
##
|
35
|
+
# Exports the BNode in N-Triples form.
|
36
|
+
#
|
37
|
+
# ==== Example
|
38
|
+
# b = BNode.new; b.to_ntriples # => returns a string of the BNode in N-Triples form
|
39
|
+
#
|
40
|
+
# ==== Returns
|
41
|
+
# @return [String] The BNode in N-Triples.
|
42
|
+
#
|
43
|
+
# @author Tom Morris
|
44
|
+
|
45
|
+
def to_ntriples
|
46
|
+
self.to_n3
|
47
|
+
end
|
48
|
+
|
49
|
+
##
|
50
|
+
# Returns the identifier as a string.
|
51
|
+
#
|
52
|
+
# === Returns
|
53
|
+
# @return [String] Blank node identifier.
|
54
|
+
#
|
55
|
+
# @author Tom Morris
|
56
|
+
def to_s
|
57
|
+
@identifier
|
58
|
+
end
|
59
|
+
|
60
|
+
protected
|
61
|
+
def valid_id? name
|
62
|
+
if name =~ /^[a-zA-Z_][a-zA-Z0-9]*$/
|
63
|
+
true
|
64
|
+
else
|
65
|
+
false
|
66
|
+
end
|
61
67
|
end
|
62
68
|
end
|
63
|
-
end
|
69
|
+
end
|
data/lib/rena/graph.rb
CHANGED
@@ -4,147 +4,185 @@ require 'rena/uriref'
|
|
4
4
|
require 'rena/literal'
|
5
5
|
require 'rena/triple'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
7
|
+
module Rena
|
8
|
+
class Graph
|
9
|
+
attr_accessor :triples, :nsbinding
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@triples = []
|
13
|
+
@nsbinding = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def size
|
17
|
+
@triples.size
|
18
|
+
end
|
19
|
+
|
20
|
+
def each
|
21
|
+
@triples.each { |value| yield value }
|
22
|
+
end
|
23
|
+
|
24
|
+
def [] (item)
|
25
|
+
@triples[item]
|
26
|
+
end
|
27
|
+
|
28
|
+
def each_with_subject(subject)
|
29
|
+
@triples.each do |value|
|
30
|
+
yield value if value.subject == subject
|
27
31
|
end
|
28
|
-
}
|
29
|
-
end
|
30
|
-
|
31
|
-
##
|
32
|
-
# Adds a triple to a graph directly from the intended subject, predicate, and object.
|
33
|
-
#
|
34
|
-
# ==== Example
|
35
|
-
# g = Graph.new; g.add_triple(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new) # => results in the triple being added to g; returns an array of g's triples
|
36
|
-
#
|
37
|
-
# @param [URIRef, BNode] s the subject of the triple
|
38
|
-
# @param [URIRef] p the predicate of the triple
|
39
|
-
# @param [URIRef, BNode, Literal, TypedLiteral] o the object of the triple
|
40
|
-
#
|
41
|
-
# ==== Returns
|
42
|
-
# @return [Array] An array of the triples (leaky abstraction? consider returning the graph instead)
|
43
|
-
#
|
44
|
-
# @raise [Error] Checks parameter types and raises if they are incorrect.
|
45
|
-
# @author Tom Morris
|
46
|
-
|
47
|
-
def add_triple(s, p, o)
|
48
|
-
@triples += [ Triple.new(s, p, o) ]
|
49
|
-
end
|
50
|
-
|
51
|
-
##
|
52
|
-
# Adds an extant triple to a graph
|
53
|
-
#
|
54
|
-
# ==== Example
|
55
|
-
# g = Graph.new; t = Triple.new(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new); g << t) # => results in the triple being added to g; returns an array of g's triples
|
56
|
-
#
|
57
|
-
# @param [Triple] t the triple to be added to the graph
|
58
|
-
#
|
59
|
-
# ==== Returns
|
60
|
-
# @return [Array] An array of the triples (leaky abstraction? consider returning the graph instead)
|
61
|
-
#
|
62
|
-
# @author Tom Morris
|
63
|
-
|
64
|
-
|
65
|
-
def << (triple)
|
66
|
-
# self.add_triple(s, p, o)
|
67
|
-
@triples += [ triple ]
|
68
|
-
end
|
69
|
-
|
70
|
-
##
|
71
|
-
# Exports the graph to RDF in N-Triples form.
|
72
|
-
#
|
73
|
-
# ==== Example
|
74
|
-
# g = Graph.new; g.add_triple(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new); g.to_ntriples # => returns a string of the graph in N-Triples form
|
75
|
-
#
|
76
|
-
# ==== Returns
|
77
|
-
# @return [String] The graph in N-Triples.
|
78
|
-
#
|
79
|
-
# @author Tom Morris
|
80
|
-
|
81
|
-
def to_ntriples
|
82
|
-
str = ""
|
83
|
-
@triples.each do |t|
|
84
|
-
str << t.to_ntriples + "\n"
|
85
32
|
end
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
# @param [String] uri the URI of the namespace
|
96
|
-
# @param [String] short the short name of the namespace
|
97
|
-
#
|
98
|
-
# ==== Returns
|
99
|
-
# @return [Namespace] The newly created namespace.
|
100
|
-
#
|
101
|
-
# @raise [Error] Checks validity of the desired shortname and raises if it is incorrect.
|
102
|
-
# @raise [Error] Checks that the newly created Namespace is of type Namespace and raises if it is incorrect.
|
103
|
-
# @author Tom Morris
|
104
|
-
|
105
|
-
def namespace(uri, short)
|
106
|
-
self.bind Namespace.new(uri, short)
|
107
|
-
end
|
108
|
-
|
109
|
-
def bind(namespace)
|
110
|
-
if namespace.class == Namespace
|
111
|
-
@nsbinding["#{namespace.short}"] = namespace
|
112
|
-
else
|
113
|
-
raise
|
33
|
+
|
34
|
+
def get_resource(subject)
|
35
|
+
temp = []
|
36
|
+
each_with_subject(subject) do |value|
|
37
|
+
temp << subject
|
38
|
+
end
|
39
|
+
if temp.any?
|
40
|
+
Resource.new(temp)
|
41
|
+
end
|
114
42
|
end
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
43
|
+
|
44
|
+
##
|
45
|
+
# Adds a triple to a graph directly from the intended subject, predicate, and object.
|
46
|
+
#
|
47
|
+
# ==== Example
|
48
|
+
# g = Graph.new; g.add_triple(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new) # => results in the triple being added to g; returns an array of g's triples
|
49
|
+
#
|
50
|
+
# @param [URIRef, BNode] s the subject of the triple
|
51
|
+
# @param [URIRef] p the predicate of the triple
|
52
|
+
# @param [URIRef, BNode, Literal, TypedLiteral] o the object of the triple
|
53
|
+
#
|
54
|
+
# ==== Returns
|
55
|
+
# @return [Array] An array of the triples (leaky abstraction? consider returning the graph instead)
|
56
|
+
#
|
57
|
+
# @raise [Error] Checks parameter types and raises if they are incorrect.
|
58
|
+
# @author Tom Morris
|
59
|
+
|
60
|
+
def add_triple(s, p, o)
|
61
|
+
@triples += [ Triple.new(s, p, o) ]
|
62
|
+
end
|
63
|
+
|
64
|
+
##
|
65
|
+
# Adds an extant triple to a graph
|
66
|
+
#
|
67
|
+
# ==== Example
|
68
|
+
# g = Graph.new; t = Triple.new(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new); g << t) # => results in the triple being added to g; returns an array of g's triples
|
69
|
+
#
|
70
|
+
# @param [Triple] t the triple to be added to the graph
|
71
|
+
#
|
72
|
+
# ==== Returns
|
73
|
+
# @return [Array] An array of the triples (leaky abstraction? consider returning the graph instead)
|
74
|
+
#
|
75
|
+
# @author Tom Morris
|
76
|
+
|
77
|
+
|
78
|
+
def << (triple)
|
79
|
+
# self.add_triple(s, p, o)
|
80
|
+
@triples += [ triple ]
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Exports the graph to RDF in N-Triples form.
|
85
|
+
#
|
86
|
+
# ==== Example
|
87
|
+
# g = Graph.new; g.add_triple(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new); g.to_ntriples # => returns a string of the graph in N-Triples form
|
88
|
+
#
|
89
|
+
# ==== Returns
|
90
|
+
# @return [String] The graph in N-Triples.
|
91
|
+
#
|
92
|
+
# @author Tom Morris
|
93
|
+
|
94
|
+
def to_ntriples
|
95
|
+
@triples.collect do |t|
|
96
|
+
t.to_ntriples
|
97
|
+
end * "\n"
|
98
|
+
end
|
99
|
+
|
100
|
+
##
|
101
|
+
# Creates a new namespace given a URI and the short name and binds it to the graph.
|
102
|
+
#
|
103
|
+
# ==== Example
|
104
|
+
# g = Graph.new; g.namespace("http://xmlns.com/foaf/0.1/", "foaf") # => binds the Foaf namespace to g
|
105
|
+
#
|
106
|
+
# @param [String] uri the URI of the namespace
|
107
|
+
# @param [String] short the short name of the namespace
|
108
|
+
#
|
109
|
+
# ==== Returns
|
110
|
+
# @return [Namespace] The newly created namespace.
|
111
|
+
#
|
112
|
+
# @raise [Error] Checks validity of the desired shortname and raises if it is incorrect.
|
113
|
+
# @raise [Error] Checks that the newly created Namespace is of type Namespace and raises if it is incorrect.
|
114
|
+
# @author Tom Morris
|
115
|
+
|
116
|
+
def namespace(uri, short)
|
117
|
+
self.bind Namespace.new(uri, short)
|
118
|
+
end
|
119
|
+
|
120
|
+
def bind(namespace)
|
121
|
+
if namespace.class == Namespace
|
122
|
+
@nsbinding["#{namespace.short}"] = namespace
|
123
|
+
else
|
124
|
+
raise
|
124
125
|
end
|
125
|
-
|
126
|
-
|
127
|
-
|
126
|
+
end
|
127
|
+
|
128
|
+
def has_bnode_identifier?(bnodeid)
|
129
|
+
temp_bnode = BNode.new(bnodeid)
|
130
|
+
returnval = false
|
131
|
+
@triples.each { |triple|
|
132
|
+
if triple.subject.eql?(temp_bnode)
|
133
|
+
returnval = true
|
134
|
+
break
|
135
|
+
end
|
136
|
+
if triple.object.eql?(temp_bnode)
|
137
|
+
returnval = true
|
138
|
+
break
|
139
|
+
end
|
140
|
+
}
|
141
|
+
return returnval
|
142
|
+
end
|
143
|
+
|
144
|
+
def get_bnode_by_identifier(bnodeid)
|
145
|
+
temp_bnode = BNode.new(bnodeid)
|
146
|
+
each do |triple|
|
147
|
+
if triple.subject == temp_bnode
|
148
|
+
return triple.subject
|
149
|
+
end
|
150
|
+
if triple.object == temp_bnode
|
151
|
+
return triple.object
|
152
|
+
end
|
128
153
|
end
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
154
|
+
return false
|
155
|
+
end
|
156
|
+
|
157
|
+
def get_by_type(object)
|
158
|
+
out = []
|
159
|
+
each do |t|
|
160
|
+
next unless t.is_type?
|
161
|
+
next unless case object
|
162
|
+
when String
|
163
|
+
object == t.object.to_s
|
164
|
+
when Regexp
|
165
|
+
object.match(t.object.to_s)
|
166
|
+
else
|
167
|
+
object == t.object
|
168
|
+
end
|
169
|
+
out << t.subject
|
140
170
|
end
|
141
|
-
|
142
|
-
|
143
|
-
|
171
|
+
return out
|
172
|
+
end
|
173
|
+
|
174
|
+
def join(graph)
|
175
|
+
if graph.class == Graph
|
176
|
+
graph.each { |t|
|
177
|
+
self << t
|
178
|
+
}
|
179
|
+
else
|
180
|
+
raise "join requires you provide a graph object"
|
144
181
|
end
|
145
|
-
|
146
|
-
|
182
|
+
end
|
183
|
+
# alias :add, :add_triple
|
184
|
+
# alias (=+, add_triple)
|
185
|
+
private
|
186
|
+
|
147
187
|
end
|
148
|
-
# alias :add, :add_triple
|
149
|
-
# alias (=+, add_triple)
|
150
188
|
end
|