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 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 g2
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 --pattern test/spec/*.spec.rb"
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 --pattern test/spec/*.spec.rb --format html:rena_new_spec.html"
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 --pattern test/spec/*.spec.rb --format html:rena_new_spec.html"
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['test/spec/**/*.rb']
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
@@ -1,63 +1,69 @@
1
- class BNode
2
- attr_accessor :identifier
3
- def initialize(identifier = nil)
4
- if identifier != nil && self.valid_id?(identifier) != false
5
- @identifier = identifier
6
- else
7
- @identifier = "bn" + self.hash.to_s
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
- end
10
-
11
- def eql? (eql)
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
- end
18
-
19
- ##
20
- # Exports the BNode in N-Triples form.
21
- #
22
- # ==== Example
23
- # b = BNode.new; b.to_n3 # => returns a string of the BNode in n3 form
24
- #
25
- # ==== Returns
26
- # @return [String] The BNode in n3.
27
- #
28
- # @author Tom Morris
29
-
30
- def to_n3
31
- "_:" + @identifier
32
- end
33
-
34
-
35
- ##
36
- # Exports the BNode in N-Triples form.
37
- #
38
- # ==== Example
39
- # b = BNode.new; b.to_ntriples # => returns a string of the BNode in N-Triples form
40
- #
41
- # ==== Returns
42
- # @return [String] The BNode in N-Triples.
43
- #
44
- # @author Tom Morris
45
-
46
- def to_ntriples
47
- self.to_n3
48
- end
49
-
50
- def to_s
51
- @identifier
52
- end
53
-
54
- # TODO: add valid bnode name exceptions?
55
- protected
56
- def valid_id? name
57
- if name =~ /^[a-zA-Z_][a-zA-Z0-9]*$/
58
- true
59
- else
60
- false
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
@@ -4,147 +4,185 @@ require 'rena/uriref'
4
4
  require 'rena/literal'
5
5
  require 'rena/triple'
6
6
 
7
- class Graph
8
- attr_accessor :triples, :nsbinding
9
-
10
- def initialize
11
- @triples = []
12
- @nsbinding = {}
13
- end
14
-
15
- def size
16
- @triples.size
17
- end
18
-
19
- def each
20
- @triples.each { |value| yield value }
21
- end
22
-
23
- def each_with_subject(subject)
24
- @triples.each {|value|
25
- if value.subject == subject
26
- yield value
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
- return str
87
- end
88
-
89
- ##
90
- # Creates a new namespace given a URI and the short name and binds it to the graph.
91
- #
92
- # ==== Example
93
- # g = Graph.new; g.namespace("http://xmlns.com/foaf/0.1/", "foaf") # => binds the Foaf namespace to g
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
- end
116
-
117
- def has_bnode_identifier?(bnodeid)
118
- temp_bnode = BNode.new(bnodeid)
119
- returnval = false
120
- @triples.each { |triple|
121
- if triple.subject.eql?(temp_bnode)
122
- returnval = true
123
- break
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
- if triple.object.eql?(temp_bnode)
126
- returnval = true
127
- break
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
- return returnval
131
- end
132
-
133
- def get_bnode_by_identifier(bnodeid)
134
- temp_bnode = BNode.new(bnodeid)
135
- returnval = false
136
- @triples.each { |triple|
137
- if triple.subject.eql?(temp_bnode)
138
- returnval = triple.subject
139
- break
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
- if triple.object.eql?(temp_bnode)
142
- returnval = triple.object
143
- break
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
- return returnval
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