rdf 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -18,7 +18,7 @@ Examples
18
18
 
19
19
  ### Creating an RDF statement
20
20
 
21
- s = RDF::URI.parse("http://gemcutter.org/gems/rdf")
21
+ s = RDF::URI.parse("http://rubygems.org/gems/rdf")
22
22
  p = RDF::DC.creator
23
23
  o = RDF::URI.parse("http://ar.to/#self")
24
24
 
@@ -55,7 +55,7 @@ Documentation
55
55
 
56
56
  * <http://rdf.rubyforge.org/>
57
57
 
58
- ### RDF Objects
58
+ ### RDF Object Model
59
59
 
60
60
  * {RDF::Graph}
61
61
  * {RDF::Literal}
@@ -74,17 +74,20 @@ Documentation
74
74
  ### RDF Serialization Formats
75
75
 
76
76
  * {RDF::NTriples}
77
+ * [RDF::JSON](http://rdf.rubyforge.org/json/) (plugin)
78
+ * [RDF::Trix](http://rdf.rubyforge.org/trix/) (plugin)
77
79
 
78
80
  ### RDF Storage
79
81
 
80
82
  * {RDF::Repository}
83
+ * [RDF::Sesame](http://rdf.rubyforge.org/sesame/) (plugin)
81
84
 
82
85
  ### RDF Querying
83
86
 
84
87
  * {RDF::Query}
85
- * {RDF::Query::Pattern}
86
- * {RDF::Query::Solution}
87
- * {RDF::Query::Variable}
88
+ * {RDF::Query::Pattern}
89
+ * {RDF::Query::Solution}
90
+ * {RDF::Query::Variable}
88
91
 
89
92
  ### RDF Vocabularies
90
93
 
@@ -112,7 +115,7 @@ Installation
112
115
  ------------
113
116
 
114
117
  The recommended installation method is via RubyGems. To install the latest
115
- official release from Gemcutter, do:
118
+ official release from [RubyGems](http://rubygems.org/), do:
116
119
 
117
120
  % [sudo] gem install rdf
118
121
 
@@ -133,7 +136,7 @@ Resources
133
136
 
134
137
  * <http://rdf.rubyforge.org/>
135
138
  * <http://github.com/bendiken/rdf>
136
- * <http://gemcutter.org/gems/rdf>
139
+ * <http://rubygems.org/gems/rdf>
137
140
  * <http://rubyforge.org/projects/rdf/>
138
141
  * <http://raa.ruby-lang.org/project/rdf/>
139
142
  * <http://www.ohloh.net/p/rdf>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.9
1
+ 0.1.0
data/etc/doap.nt CHANGED
@@ -1,24 +1,24 @@
1
1
  <http://ar.to/#self> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
2
- <http://ar.to/#self> <http://xmlns.com/foaf/0.1/made> <http://gemcutter.org/gems/rdf> .
2
+ <http://ar.to/#self> <http://xmlns.com/foaf/0.1/made> <http://rubygems.org/gems/rdf> .
3
3
  <http://ar.to/#self> <http://xmlns.com/foaf/0.1/mbox> <mailto:arto.bendiken@gmail.com> .
4
4
  <http://ar.to/#self> <http://xmlns.com/foaf/0.1/name> "Arto Bendiken" .
5
5
  <http://bhuga.net/#ben> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
6
6
  <http://bhuga.net/#ben> <http://xmlns.com/foaf/0.1/mbox> <mailto:blavender@gmail.com> .
7
7
  <http://bhuga.net/#ben> <http://xmlns.com/foaf/0.1/name> "Ben Lavender" .
8
- <http://gemcutter.org/gems/rdf> <http://purl.org/dc/terms/creator> <http://ar.to/#self> .
9
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#blog> <http://ar.to/> .
10
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#bug-database> <http://github.com/bendiken/rdf/issues> .
11
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#created> "2007-10-23" .
12
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#description> "RDF.rb is a pure-Ruby library for working with Resource Description Framework (RDF) data."@en .
13
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#developer> <http://ar.to/#self> .
14
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#developer> <http://bhuga.net/#ben> .
15
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#documenter> <http://ar.to/#self> .
16
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#download-page> <http://rubyforge.org/projects/rdf/> .
17
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#homepage> <http://rdf.rubyforge.org/> .
18
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#license> <http://creativecommons.org/licenses/publicdomain/> .
19
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#maintainer> <http://ar.to/#self> .
20
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#name> "RDF.rb" .
21
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#platform> "Ruby" .
22
- <http://gemcutter.org/gems/rdf> <http://usefulinc.com/ns/doap#shortdesc> "A Ruby library for working with Resource Description Framework (RDF) data."@en .
23
- <http://gemcutter.org/gems/rdf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://usefulinc.com/ns/doap#Project> .
24
- <http://gemcutter.org/gems/rdf> <http://xmlns.com/foaf/0.1/maker> <http://ar.to/#self> .
8
+ <http://rubygems.org/gems/rdf> <http://purl.org/dc/terms/creator> <http://ar.to/#self> .
9
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#blog> <http://ar.to/> .
10
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#bug-database> <http://github.com/bendiken/rdf/issues> .
11
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#created> "2007-10-23" .
12
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#description> "RDF.rb is a pure-Ruby library for working with Resource Description Framework (RDF) data."@en .
13
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#developer> <http://ar.to/#self> .
14
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#developer> <http://bhuga.net/#ben> .
15
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#documenter> <http://ar.to/#self> .
16
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#download-page> <http://rubyforge.org/projects/rdf/> .
17
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#homepage> <http://rdf.rubyforge.org/> .
18
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#license> <http://creativecommons.org/licenses/publicdomain/> .
19
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#maintainer> <http://ar.to/#self> .
20
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#name> "RDF.rb" .
21
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#platform> "Ruby" .
22
+ <http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#shortdesc> "A Ruby library for working with Resource Description Framework (RDF) data."@en .
23
+ <http://rubygems.org/gems/rdf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://usefulinc.com/ns/doap#Project> .
24
+ <http://rubygems.org/gems/rdf> <http://xmlns.com/foaf/0.1/maker> <http://ar.to/#self> .
data/lib/rdf.rb CHANGED
@@ -39,6 +39,7 @@ module RDF
39
39
  autoload :Vocabulary, 'rdf/vocab'
40
40
  autoload :CC, 'rdf/vocab/cc'
41
41
  autoload :DC, 'rdf/vocab/dc'
42
+ autoload :DC11, 'rdf/vocab/dc11'
42
43
  autoload :DOAP, 'rdf/vocab/doap'
43
44
  autoload :EXIF, 'rdf/vocab/exif'
44
45
  autoload :FOAF, 'rdf/vocab/foaf'
@@ -1,7 +1,7 @@
1
1
  module RDF
2
2
  ##
3
3
  # Classes that include this module must implement the methods
4
- # {#insert_statement}, {#delete_statement} and {#each_statement}.
4
+ # `#insert_statement`, `#delete_statement` and `#each_statement`.
5
5
  #
6
6
  # @see RDF::Graph
7
7
  # @see RDF::Repository
@@ -3,21 +3,31 @@ module RDF
3
3
  # An RDF graph.
4
4
  class Graph < Resource
5
5
  include RDF::Enumerable
6
+ include RDF::Mutable
6
7
  include RDF::Queryable
7
8
 
8
- # @return [URI]
9
- attr_accessor :uri
9
+ ##
10
+ # @return [Resource]
11
+ attr_accessor :context
10
12
 
13
+ ##
11
14
  # @return [Array<Statement>]
12
15
  attr_accessor :data
13
16
 
14
17
  ##
15
- # @param [URI] uri
18
+ # @param [Resource] context
19
+ # @param [Hash{Symbol => Object}] options
16
20
  # @yield [graph]
17
21
  # @yieldparam [Graph]
18
- def initialize(uri = nil, options = {}, &block)
19
- @uri, @options = uri, options
20
- @data = []
22
+ def initialize(context = nil, options = {}, &block)
23
+ @context = case context
24
+ when nil then nil
25
+ when RDF::Resource then context
26
+ else RDF::URI.new(context)
27
+ end
28
+
29
+ @data = options.delete(:data) || []
30
+ @options = options
21
31
 
22
32
  if block_given?
23
33
  case block.arity
@@ -28,7 +38,7 @@ module RDF
28
38
  end
29
39
 
30
40
  ##
31
- # Returns `true`.
41
+ # Returns `true` to indicate that this is a graph.
32
42
  #
33
43
  # @return [Boolean]
34
44
  def graph?
@@ -36,57 +46,121 @@ module RDF
36
46
  end
37
47
 
38
48
  ##
49
+ # Returns `true` if this is a named graph.
50
+ #
39
51
  # @return [Boolean]
40
- def named?() !unnamed? end
52
+ def named?
53
+ !unnamed?
54
+ end
41
55
 
42
56
  ##
57
+ # Returns `true` if this is a unnamed graph.
58
+ #
43
59
  # @return [Boolean]
44
- def unnamed?() uri.nil? end
60
+ def unnamed?
61
+ context.nil?
62
+ end
45
63
 
46
64
  ##
47
- # @return [Integer]
48
- def size() @data.size end
65
+ # Returns all unique RDF contexts for this graph.
66
+ #
67
+ # @return [Array<Resource>]
68
+ def contexts
69
+ named? ? [context] : []
70
+ end
49
71
 
50
72
  ##
51
- # @yield [statement]
52
- # @yieldparam [Array<Statement>]
53
- # @return [Graph]
54
- def each(&block)
55
- @data.each(&block)
73
+ # Returns the URI representation of this graph.
74
+ #
75
+ # @return [URI]
76
+ def to_uri
77
+ context
56
78
  end
57
79
 
58
80
  ##
59
- # @return [Resource]
60
- def context
61
- uri
81
+ # Returns a string representation of this graph.
82
+ #
83
+ # @return [String]
84
+ def to_s
85
+ named? ? context.to_s : "<>"
62
86
  end
63
87
 
64
88
  ##
65
- # @return [Array<Resource>]
66
- def contexts
67
- named? ? [uri] : []
89
+ # Returns `true` if this graph contains no RDF statements.
90
+ #
91
+ # @return [Boolean]
92
+ # @see RDF::Enumerable#empty?
93
+ def empty?
94
+ @data.empty?
68
95
  end
69
96
 
70
97
  ##
71
- # @param [Statement, Array(Value)]
72
- # @return [Graph]
73
- def <<(statement)
74
- @data << case statement
75
- when Array then Statement.new(*statement)
76
- when Statement then statement
77
- else statement
78
- end
79
- self
98
+ # Returns the number of RDF statements in this graph.
99
+ #
100
+ # @return [Integer]
101
+ # @see RDF::Enumerable#count
102
+ def count
103
+ @data.size
80
104
  end
81
105
 
82
106
  ##
83
- # @return [URI]
84
- def to_uri() uri end
107
+ # Returns `true` if this graph contains the given RDF statement.
108
+ #
109
+ # @param [Statement] statement
110
+ # @return [Boolean]
111
+ # @see RDF::Enumerable#has_statement?
112
+ def has_statement?(statement)
113
+ statement = statement.dup
114
+ statement.context = context
115
+ @data.include?(statement)
116
+ end
85
117
 
86
118
  ##
87
- # @return [String]
88
- def to_s
89
- named? ? uri.to_s : "<>"
119
+ # Enumerates each RDF statement in this graph.
120
+ #
121
+ # @yield [statement]
122
+ # @yieldparam [Statement] statement
123
+ # @return [Enumerator]
124
+ # @see RDF::Enumerable#each_statement
125
+ def each(&block)
126
+ @data.each(&block)
90
127
  end
128
+
129
+ ##
130
+ # Inserts the given RDF statement into the graph.
131
+ #
132
+ # @param [RDF::Statement] statement
133
+ # @return [void]
134
+ # @see RDF::Mutable#insert
135
+ def insert_statement(statement)
136
+ statement = statement.dup
137
+ statement.context = context
138
+ @data.push(statement.dup) unless @data.include?(statement)
139
+ end
140
+
141
+ ##
142
+ # Deletes the given RDF statement from the graph.
143
+ #
144
+ # @param [RDF::Statement] statement
145
+ # @return [void]
146
+ # @see RDF::Mutable#delete
147
+ def delete_statement(statement)
148
+ statement = statement.dup
149
+ statement.context = context
150
+ @data.delete(statement)
151
+ end
152
+
153
+ ##
154
+ # Deletes all RDF statements from this graph.
155
+ #
156
+ # @return [void]
157
+ # @see RDF::Mutable#clear
158
+ def clear_statements
159
+ @data.clear
160
+ end
161
+
162
+ protected :insert_statement
163
+ protected :delete_statement
164
+ protected :clear_statements
91
165
  end
92
166
  end
@@ -8,7 +8,7 @@ module RDF
8
8
  #
9
9
  # @example Creating a language-tagged literal (1)
10
10
  # value = RDF::Literal.new("Hello!", :language => :en)
11
- # value.language? #=> true
11
+ # value.has_language? #=> true
12
12
  # value.language #=> :en
13
13
  #
14
14
  # @example Creating a language-tagged literal (2)
@@ -18,12 +18,12 @@ module RDF
18
18
  #
19
19
  # @example Creating an explicitly datatyped literal
20
20
  # value = RDF::Literal.new("2009-12-31", :datatype => RDF::XSD.date)
21
- # value.datatype? #=> true
21
+ # value.has_datatype? #=> true
22
22
  # value.datatype #=> RDF::XSD.date
23
23
  #
24
24
  # @example Creating an implicitly datatyped literal
25
25
  # value = RDF::Literal.new(Date.today)
26
- # value.datatype? #=> true
26
+ # value.has_datatype? #=> true
27
27
  # value.datatype #=> RDF::XSD.date
28
28
  #
29
29
  # @example Creating implicitly datatyped literals
@@ -173,21 +173,24 @@ module RDF
173
173
  #
174
174
  # @return [Boolean]
175
175
  # @see http://www.w3.org/TR/rdf-concepts/#dfn-plain-literal
176
- def language?
176
+ def has_language?
177
177
  !language.nil?
178
178
  end
179
179
 
180
+ alias_method :language?, :has_language?
181
+
180
182
  ##
181
183
  # Returns `true` if this is a datatyped literal.
182
184
  #
183
185
  # @return [Boolean]
184
186
  # @see http://www.w3.org/TR/rdf-concepts/#dfn-typed-literal
185
- def datatype?
187
+ def has_datatype?
186
188
  !datatype.nil?
187
189
  end
188
190
 
189
- alias_method :typed?, :datatype?
190
- alias_method :datatyped?, :datatype?
191
+ alias_method :datatype?, :has_datatype?
192
+ alias_method :typed?, :has_datatype?
193
+ alias_method :datatyped?, :has_datatype?
191
194
 
192
195
  ##
193
196
  # Returns a string representation of this literal.
@@ -1,21 +1,35 @@
1
1
  module RDF
2
2
  ##
3
- # An RDF blank node, also known as an unlabeled node.
3
+ # An RDF blank node, also known as an anonymous or unlabeled node.
4
4
  #
5
5
  # @example Creating a blank node with an implicit identifier
6
6
  # bnode = RDF::Node.new
7
7
  #
8
- # @example Creating a blank node with an UUID identifier (1)
9
- # require 'uuid'
10
- # bnode = RDF::Node.new(UUID.generate)
8
+ # @example Creating a blank node with an UUID identifier
9
+ # bnode = RDF::Node.uuid
10
+ # bnode.to_s #=> "_:504c0a30-0d11-012d-3f50-001b63cac539"
11
11
  #
12
- # @example Creating a blank node with an UUID identifier (2)
13
- # require 'uuidtools'
14
- # bnode = RDF::Node.new(UUIDTools::UUID.random_create)
15
- #
16
- # @see http://github.com/assaf/uuid
17
- # @see http://uuidtools.rubyforge.org/
12
+ # @see http://rubygems.org/gems/uuid
13
+ # @see http://rubygems.org/gems/uuidtools
18
14
  class Node < Resource
15
+ ##
16
+ # Returns a blank node with a random UUID-based identifier.
17
+ #
18
+ # @return [RDF::Node]
19
+ def self.uuid
20
+ begin
21
+ require 'uuid'
22
+ self.new(UUID.generate)
23
+ rescue LoadError => e
24
+ begin
25
+ require 'uuidtools'
26
+ self.new(UUIDTools::UUID.random_create)
27
+ rescue LoadError => e
28
+ raise LoadError.new("no such file to load -- uuid or uuidtools")
29
+ end
30
+ end
31
+ end
32
+
19
33
  # @return [String]
20
34
  attr_accessor :id
21
35
 
@@ -3,7 +3,7 @@ module RDF
3
3
  # An RDF statement.
4
4
  #
5
5
  # @example Creating an RDF statement
6
- # s = RDF::URI.new("http://gemcutter.org/gems/rdf")
6
+ # s = RDF::URI.new("http://rubygems.org/gems/rdf")
7
7
  # p = RDF::DC.creator
8
8
  # o = RDF::URI.new("http://ar.to/#self")
9
9
  # RDF::Statement.new(s, p, o)
@@ -13,7 +13,7 @@ module RDF
13
13
  #
14
14
  # @example Creating an RDF statement from a `Hash`
15
15
  # RDF::Statement.new({
16
- # :subject => RDF::URI.new("http://gemcutter.org/gems/rdf"),
16
+ # :subject => RDF::URI.new("http://rubygems.org/gems/rdf"),
17
17
  # :predicate => RDF::DC.creator,
18
18
  # :object => RDF::URI.new("http://ar.to/#self"),
19
19
  # })
@@ -62,7 +62,11 @@ module RDF
62
62
  @options = options
63
63
  @subject = subject
64
64
  @predicate = predicate
65
- @object = object
65
+ @object = case object
66
+ when nil then nil
67
+ when RDF::Value then object
68
+ else RDF::Literal.new(object)
69
+ end
66
70
  end
67
71
  end
68
72
 
@@ -206,13 +210,25 @@ module RDF
206
210
  end
207
211
 
208
212
  ##
213
+ # Returns a string representation of this statement.
214
+ #
209
215
  # @return [String]
210
216
  def to_s
211
217
  require 'stringio' unless defined?(StringIO)
212
218
  StringIO.open do |buffer|
213
- buffer << "<#{subject}> "
214
- buffer << "<#{predicate}> "
215
- buffer << "<#{object}> ."
219
+ buffer << case subject
220
+ when RDF::Node then subject.to_s
221
+ when RDF::URI then "<#{subject}>"
222
+ else subject.inspect
223
+ end
224
+ buffer << " <#{predicate}> "
225
+ buffer << case object
226
+ when RDF::Literal then object.to_s
227
+ when RDF::Node then object.to_s
228
+ when RDF::URI then "<#{object}>"
229
+ else object.inspect
230
+ end
231
+ buffer << " ."
216
232
  buffer.string
217
233
  end
218
234
  end