rdf 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/AUTHORS +1 -1
  2. data/VERSION +1 -1
  3. data/lib/rdf.rb +25 -22
  4. data/lib/rdf/enumerable.rb +554 -0
  5. data/lib/rdf/format.rb +239 -41
  6. data/lib/rdf/model/graph.rb +82 -0
  7. data/lib/rdf/{literal.rb → model/literal.rb} +47 -4
  8. data/lib/rdf/{node.rb → model/node.rb} +0 -0
  9. data/lib/rdf/{resource.rb → model/resource.rb} +0 -0
  10. data/lib/rdf/{statement.rb → model/statement.rb} +12 -0
  11. data/lib/rdf/{uri.rb → model/uri.rb} +20 -7
  12. data/lib/rdf/model/value.rb +135 -0
  13. data/lib/rdf/ntriples.rb +35 -2
  14. data/lib/rdf/ntriples/format.rb +4 -4
  15. data/lib/rdf/ntriples/reader.rb +2 -2
  16. data/lib/rdf/ntriples/writer.rb +26 -19
  17. data/lib/rdf/query.rb +4 -4
  18. data/lib/rdf/query/pattern.rb +3 -3
  19. data/lib/rdf/query/solution.rb +2 -2
  20. data/lib/rdf/query/variable.rb +3 -3
  21. data/lib/rdf/reader.rb +85 -16
  22. data/lib/rdf/repository.rb +104 -12
  23. data/lib/rdf/version.rb +1 -1
  24. data/lib/rdf/vocab.rb +171 -0
  25. data/lib/rdf/vocab/cc.rb +18 -0
  26. data/lib/rdf/vocab/dc.rb +63 -0
  27. data/lib/rdf/vocab/doap.rb +45 -0
  28. data/lib/rdf/vocab/exif.rb +168 -0
  29. data/lib/rdf/vocab/foaf.rb +69 -0
  30. data/lib/rdf/vocab/http.rb +26 -0
  31. data/lib/rdf/vocab/owl.rb +59 -0
  32. data/lib/rdf/{vocabulary → vocab}/rdf.rb +7 -1
  33. data/lib/rdf/vocab/rdfs.rb +17 -0
  34. data/lib/rdf/{vocabulary → vocab}/rss.rb +6 -1
  35. data/lib/rdf/vocab/sioc.rb +93 -0
  36. data/lib/rdf/vocab/skos.rb +36 -0
  37. data/lib/rdf/vocab/wot.rb +21 -0
  38. data/lib/rdf/vocab/xhtml.rb +9 -0
  39. data/lib/rdf/vocab/xsd.rb +58 -0
  40. data/lib/rdf/writer.rb +123 -16
  41. metadata +26 -27
  42. data/lib/rdf/graph.rb +0 -197
  43. data/lib/rdf/reader/ntriples.rb +0 -5
  44. data/lib/rdf/value.rb +0 -76
  45. data/lib/rdf/vocabulary.rb +0 -133
  46. data/lib/rdf/vocabulary/cc.rb +0 -9
  47. data/lib/rdf/vocabulary/dc.rb +0 -9
  48. data/lib/rdf/vocabulary/doap.rb +0 -9
  49. data/lib/rdf/vocabulary/exif.rb +0 -9
  50. data/lib/rdf/vocabulary/foaf.rb +0 -9
  51. data/lib/rdf/vocabulary/http.rb +0 -9
  52. data/lib/rdf/vocabulary/owl.rb +0 -9
  53. data/lib/rdf/vocabulary/rdfs.rb +0 -9
  54. data/lib/rdf/vocabulary/sioc.rb +0 -9
  55. data/lib/rdf/vocabulary/skos.rb +0 -9
  56. data/lib/rdf/vocabulary/wot.rb +0 -9
  57. data/lib/rdf/vocabulary/xhtml.rb +0 -9
  58. data/lib/rdf/vocabulary/xsd.rb +0 -9
  59. data/lib/rdf/writer/ntriples.rb +0 -5
@@ -1,8 +1,47 @@
1
1
  module RDF
2
2
  ##
3
3
  # An RDF repository.
4
+ #
5
+ # @example Creating a transient in-memory repository
6
+ # repository = RDF::Repository.new
7
+ #
8
+ # @example Checking whether a repository is readable/writable
9
+ # repository.readable?
10
+ # repository.writable?
11
+ #
12
+ # @example Checking whether a repository is persistent or transient
13
+ # repository.persistent?
14
+ # repository.transient?
15
+ #
16
+ # @example Checking whether a repository is empty
17
+ # repository.empty?
18
+ #
19
+ # @example Checking how many statements a repository contains
20
+ # repository.count
21
+ #
22
+ # @example Checking whether a repository contains a specific statement
23
+ # repository.has_statement?(statement)
24
+ #
25
+ # @example Enumerating statements in a repository
26
+ # repository.each_statement { |statement| statement.inspect! }
27
+ #
28
+ # @example Inserting statements into a repository
29
+ # repository.insert(*statements)
30
+ # repository.insert(statement)
31
+ # repository.insert([subject, predicate, object])
32
+ # repository << statement
33
+ # repository << [subject, predicate, object]
34
+ #
35
+ # @example Deleting statements from a repository
36
+ # repository.delete(*statements)
37
+ # repository.delete(statement)
38
+ # repository.delete([subject, predicate, object])
39
+ #
40
+ # @example Deleting all statements from a repository
41
+ # repository.clear!
42
+ #
4
43
  class Repository
5
- include Enumerable
44
+ include RDF::Enumerable
6
45
 
7
46
  # @return [URI]
8
47
  attr_reader :uri
@@ -116,7 +155,7 @@ module RDF
116
155
  ##
117
156
  # Returns the number of RDF statements in the repository.
118
157
  #
119
- # @return [Integer]
158
+ # @return [Integer]
120
159
  def size
121
160
  @data.size
122
161
  end
@@ -124,7 +163,7 @@ module RDF
124
163
  alias_method :count, :size
125
164
 
126
165
  ##
127
- # Returns `true` if the repository contains the given RDF statement.
166
+ # Returns `true` if this repository contains the given RDF `statement`.
128
167
  #
129
168
  # @param [Statement] statement
130
169
  # @return [Boolean]
@@ -137,16 +176,13 @@ module RDF
137
176
  ##
138
177
  # Enumerates each RDF statement in the repository.
139
178
  #
140
- # @yield [statement]
179
+ # @yield [statement]
141
180
  # @yieldparam [Statement]
142
- # @return [Repository]
143
- def each_statement(&block)
181
+ # @return [Enumerator]
182
+ def each(&block)
144
183
  @data.each(&block)
145
- self
146
184
  end
147
185
 
148
- alias_method :each, :each_statement
149
-
150
186
  ##
151
187
  # Queries the repository for RDF statements matching the given pattern.
152
188
  #
@@ -189,7 +225,7 @@ module RDF
189
225
  end
190
226
 
191
227
  ##
192
- # Inserts an RDF statement to the repository.
228
+ # Inserts an RDF statement into the repository.
193
229
  #
194
230
  # @param [Statement, Array(Value), #to_a] statement
195
231
  # @return [Repository]
@@ -202,6 +238,22 @@ module RDF
202
238
  self
203
239
  end
204
240
 
241
+ ##
242
+ # Updates RDF statements in the repository.
243
+ #
244
+ # @param [Array<Statement>] statements
245
+ # @raise [TypeError] if the repository is immutable
246
+ # @return [Repository]
247
+ def update(*statements)
248
+ raise TypeError.new("repository is immutable") if immutable?
249
+ statements.each do |statement|
250
+ if (statement = create_statement(statement))
251
+ delete([statement.subject, statement.predicate, nil])
252
+ insert(statement) if statement.has_object?
253
+ end
254
+ end
255
+ end
256
+
205
257
  ##
206
258
  # Inserts RDF statements into the repository.
207
259
  #
@@ -211,7 +263,11 @@ module RDF
211
263
  def insert(*statements)
212
264
  raise TypeError.new("repository is immutable") if immutable?
213
265
  statements.each do |statement|
214
- insert_statement(statement)
266
+ if (statement = create_statement(statement)).valid?
267
+ insert_statement(statement)
268
+ else
269
+ raise ArgumentError.new # FIXME
270
+ end
215
271
  end
216
272
  self
217
273
  end
@@ -225,11 +281,38 @@ module RDF
225
281
  def delete(*statements)
226
282
  raise TypeError.new("repository is immutable") if immutable?
227
283
  statements.each do |statement|
228
- delete_statement(statement)
284
+ if (statement = create_statement(statement)).valid?
285
+ delete_statement(statement)
286
+ else
287
+ query(statement).each do |statement|
288
+ delete_statement(statement)
289
+ end
290
+ end
229
291
  end
230
292
  self
231
293
  end
232
294
 
295
+ ##
296
+ # Deletes all RDF statements from this repository.
297
+ #
298
+ # @return [Repository]
299
+ def clear
300
+ @data.clear
301
+ self
302
+ end
303
+
304
+ alias_method :clear!, :clear
305
+
306
+ ##
307
+ # Outputs a developer-friendly representation of this repository to
308
+ # `stderr`.
309
+ #
310
+ # @return [void]
311
+ def inspect!
312
+ each_statement { |statement| statement.inspect! }
313
+ nil
314
+ end
315
+
233
316
  protected
234
317
 
235
318
  def insert_statement(statement)
@@ -240,5 +323,14 @@ module RDF
240
323
  @data.delete(statement)
241
324
  end
242
325
 
326
+ def create_statement(statement)
327
+ case statement
328
+ when Statement then statement
329
+ when Hash then Statement.new(statement)
330
+ when Array then Statement.new(*statement)
331
+ else raise ArgumentError.new # FIXME
332
+ end
333
+ end
334
+
243
335
  end
244
336
  end
data/lib/rdf/version.rb CHANGED
@@ -2,7 +2,7 @@ module RDF
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 6
5
+ TINY = 7
6
6
  EXTRA = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
data/lib/rdf/vocab.rb ADDED
@@ -0,0 +1,171 @@
1
+ module RDF
2
+ ##
3
+ # A {Vocabulary} represents an RDFS or OWL vocabulary.
4
+ #
5
+ # ### Vocabularies:
6
+ #
7
+ # The following vocabularies are pre-defined for your convenience:
8
+ #
9
+ # * {RDF::CC} - Creative Commons (CC)
10
+ # * {RDF::DC} - Dublin Core (DC)
11
+ # * {RDF::DOAP} - Description of a Project (DOAP)
12
+ # * {RDF::EXIF} - Exchangeable Image File Format (EXIF)
13
+ # * {RDF::FOAF} - Friend of a Friend (FOAF)
14
+ # * {RDF::HTTP} - Hypertext Transfer Protocol (HTTP)
15
+ # * {RDF::OWL} - Web Ontology Language (OWL)
16
+ # * {RDF::RDFS} - RDF Schema (RDFS)
17
+ # * {RDF::RSS} - RDF Site Summary (RSS)
18
+ # * {RDF::SIOC} - Semantically-Interlinked Online Communities (SIOC)
19
+ # * {RDF::SKOS} - Simple Knowledge Organization System (SKOS)
20
+ # * {RDF::WOT} - Web of Trust (WOT)
21
+ # * {RDF::XHTML} - Extensible HyperText Markup Language (XHTML)
22
+ # * {RDF::XSD} - XML Schema (XSD)
23
+ #
24
+ # @example Using pre-defined RDF vocabularies
25
+ # include RDF
26
+ # DC.title #=> RDF::URI("http://purl.org/dc/terms/title")
27
+ # FOAF.knows #=> RDF::URI("http://xmlns.com/foaf/0.1/knows")
28
+ # RDFS.seeAlso #=> RDF::URI("http://www.w3.org/2000/01/rdf-schema#seeAlso")
29
+ # RSS.title #=> RDF::URI("http://purl.org/rss/1.0/title")
30
+ # OWL.sameAs #=> RDF::URI("http://www.w3.org/2002/07/owl#sameAs")
31
+ # XSD.dateTime #=> RDF::URI("http://www.w3.org/2001/XMLSchema#dateTime")
32
+ #
33
+ # @example Using ad-hoc RDF vocabularies
34
+ # foaf = RDF::Vocabulary.new("http://xmlns.com/foaf/0.1/")
35
+ # foaf.knows #=> RDF::URI("http://xmlns.com/foaf/0.1/knows")
36
+ # foaf[:name] #=> RDF::URI("http://xmlns.com/foaf/0.1/name")
37
+ # foaf['mbox'] #=> RDF::URI("http://xmlns.com/foaf/0.1/mbox")
38
+ #
39
+ # @see http://www.w3.org/TR/curie/
40
+ # @see http://en.wikipedia.org/wiki/QName
41
+ class Vocabulary
42
+ ##
43
+ # Defines a vocabulary term called `property`.
44
+ #
45
+ # @param [Symbol]
46
+ # @return [void]
47
+ def self.property(property)
48
+ metaclass = class << self; self; end
49
+ metaclass.send(:define_method, property) { self[property] } # class method
50
+ end
51
+
52
+ ##
53
+ # Returns the URI for the term `property` in this vocabulary.
54
+ #
55
+ # @param [#to_s] property
56
+ # @return [URI]
57
+ def self.[](property)
58
+ RDF::URI.new([to_s, property.to_s].join(''))
59
+ end
60
+
61
+ ##
62
+ # Returns the base URI for this vocabulary class.
63
+ #
64
+ # @return [URI]
65
+ def self.to_uri
66
+ RDF::URI.new(to_s)
67
+ end
68
+
69
+ ##
70
+ # Returns a string representation of this vocabulary class.
71
+ #
72
+ # @return [String]
73
+ def self.to_s
74
+ @@uris.has_key?(self) ? @@uris[self].to_s : super
75
+ end
76
+
77
+ ##
78
+ # Returns a developer-friendly representation of this vocabulary class.
79
+ #
80
+ # @return [String]
81
+ def self.inspect
82
+ if self == Vocabulary
83
+ self.to_s
84
+ else
85
+ sprintf("%s(%s)", superclass.to_s, to_s)
86
+ end
87
+ end
88
+
89
+ # Undefine all superfluous instance methods:
90
+ undef_method *(instance_methods - %w(__id__ __send__ __class__ __eval__ instance_eval inspect class))
91
+
92
+ ##
93
+ # @param [URI, String, #to_s]
94
+ def initialize(uri)
95
+ @uri = case uri
96
+ when RDF::URI then uri.to_s
97
+ else RDF::URI.parse(uri.to_s) ? uri.to_s : nil
98
+ end
99
+ end
100
+
101
+ ##
102
+ # Returns the URI for the term `property` in this vocabulary.
103
+ #
104
+ # @param [#to_s] property
105
+ # @return [URI]
106
+ def [](property)
107
+ RDF::URI.new([to_s, property.to_s].join(''))
108
+ end
109
+
110
+ ##
111
+ # Returns the base URI for this vocabulary.
112
+ #
113
+ # @return [URI]
114
+ def to_uri
115
+ RDF::URI.new(to_s)
116
+ end
117
+
118
+ ##
119
+ # Returns a string representation of this vocabulary.
120
+ #
121
+ # @return [String]
122
+ def to_s
123
+ @uri.to_s
124
+ end
125
+
126
+ ##
127
+ # Returns a developer-friendly representation of this vocabulary.
128
+ #
129
+ # @return [String]
130
+ def inspect
131
+ sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, to_s)
132
+ end
133
+
134
+ protected
135
+
136
+ def self.create(uri) # @private
137
+ @@uri = uri
138
+ self
139
+ end
140
+
141
+ def self.inherited(subclass) # @private
142
+ unless @@uri.nil?
143
+ subclass.send(:private_class_method, :new)
144
+ @@uris[subclass] = @@uri
145
+ @@uri = nil
146
+ end
147
+ end
148
+
149
+ def self.method_missing(property, *args, &block)
150
+ if args.empty? && @@uris.has_key?(self)
151
+ self[property]
152
+ else
153
+ super
154
+ end
155
+ end
156
+
157
+ def method_missing(property, *args, &block)
158
+ if args.empty?
159
+ self[property]
160
+ else
161
+ raise ArgumentError.new("wrong number of arguments (#{args.size} for 0)")
162
+ end
163
+ end
164
+
165
+ private
166
+
167
+ @@uris = {} # @private
168
+ @@uri = nil # @private
169
+
170
+ end
171
+ end
@@ -0,0 +1,18 @@
1
+ module RDF
2
+ ##
3
+ # Creative Commons (CC) vocabulary.
4
+ #
5
+ # @see http://creativecommons.org/ns
6
+ class CC < Vocabulary("http://creativecommons.org/ns#")
7
+ property :attributionName
8
+ property :attributionURL
9
+ property :deprecatedOn
10
+ property :jurisdiction
11
+ property :legalcode
12
+ property :license
13
+ property :morePermissions
14
+ property :permits
15
+ property :prohibits
16
+ property :requires
17
+ end
18
+ end
@@ -0,0 +1,63 @@
1
+ module RDF
2
+ ##
3
+ # Dublin Core (DC) vocabulary.
4
+ #
5
+ # @see http://dublincore.org/schemas/rdfs/
6
+ class DC < Vocabulary("http://purl.org/dc/terms/")
7
+ property :abstract
8
+ property :accessRights
9
+ property :accrualMethod
10
+ property :accrualPeriodicity
11
+ property :accrualPolicy
12
+ property :alternative
13
+ property :audience
14
+ property :available
15
+ property :bibliographicCitation
16
+ property :conformsTo
17
+ property :contributor
18
+ property :coverage
19
+ property :created
20
+ property :creator
21
+ property :date
22
+ property :dateAccepted
23
+ property :dateCopyrighted
24
+ property :dateSubmitted
25
+ property :description
26
+ property :educationLevel
27
+ property :extent
28
+ property :format
29
+ property :hasFormat
30
+ property :hasPart
31
+ property :hasVersion
32
+ property :identifier
33
+ property :instructionalMethod
34
+ property :isFormatOf
35
+ property :isPartOf
36
+ property :isReferencedBy
37
+ property :isReplacedBy
38
+ property :isRequiredBy
39
+ property :isVersionOf
40
+ property :issued
41
+ property :language
42
+ property :license
43
+ property :mediator
44
+ property :medium
45
+ property :modified
46
+ property :provenance
47
+ property :publisher
48
+ property :references
49
+ property :relation
50
+ property :replaces
51
+ property :requires
52
+ property :rights
53
+ property :rightsHolder
54
+ property :source
55
+ property :spatial
56
+ property :subject
57
+ property :tableOfContents
58
+ property :temporal
59
+ property :title
60
+ property :type
61
+ property :valid
62
+ end
63
+ end