rdf 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -97,7 +97,7 @@ as follows:
97
97
 
98
98
  % wget http://github.com/bendiken/rdf/tarball/master
99
99
 
100
- Requirements
100
+ Dependencies
101
101
  ------------
102
102
 
103
103
  * [Addressable](http://addressable.rubyforge.org/) (>= 2.1.1)
@@ -117,7 +117,14 @@ Resources
117
117
  * <http://github.com/bendiken/rdf>
118
118
  * <http://gemcutter.org/gems/rdf>
119
119
  * <http://rubyforge.org/projects/rdf/>
120
- * <http://raa.ruby-lang.org/project/rdf>
120
+ * <http://raa.ruby-lang.org/project/rdf/>
121
+
122
+ See also
123
+ --------
124
+
125
+ * [RDFize](http://rdfize.rubyforge.org/)
126
+ * [RDFbus](http://rdfbus.rubyforge.org/)
127
+ * [RDFcache](http://rdfcache.rubyforge.org/)
121
128
 
122
129
  Author
123
130
  ------
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
data/lib/rdf.rb CHANGED
@@ -14,6 +14,9 @@ module RDF
14
14
  autoload :Reader, 'rdf/reader'
15
15
  autoload :Writer, 'rdf/writer'
16
16
 
17
+ # RDF storage
18
+ autoload :Repository, 'rdf/repository'
19
+
17
20
  # RDF vocabularies
18
21
  autoload :Vocabulary, 'rdf/vocabulary'
19
22
  autoload :CC, 'rdf/vocabulary/cc'
@@ -38,6 +41,12 @@ module RDF
38
41
  Vocabulary.create(uri)
39
42
  end
40
43
 
44
+ ##
45
+ # @return [URI]
46
+ def self.type
47
+ self[:type]
48
+ end
49
+
41
50
  ##
42
51
  # @return [#to_s] property
43
52
  # @return [URI]
@@ -56,7 +56,7 @@ module RDF
56
56
 
57
57
  ##
58
58
  # @yield [triple]
59
- # @yieldparam [Array<Value>]
59
+ # @yieldparam [Array(Value)]
60
60
  # @return [Graph]
61
61
  def each_triple(&block)
62
62
  @data.each do |statement|
@@ -67,7 +67,7 @@ module RDF
67
67
 
68
68
  ##
69
69
  # @yield [quad]
70
- # @yieldparam [Array<Value>]
70
+ # @yieldparam [Array(Value)]
71
71
  # @return [Graph]
72
72
  def each_quad(&block)
73
73
  @data.each do |statement|
@@ -125,13 +125,13 @@ module RDF
125
125
  end
126
126
 
127
127
  ##
128
- # @return [Array<Array>]
128
+ # @return [Array<Array(Value)>]
129
129
  def triples(&block)
130
130
  block_given? ? each_triple(&block) : map { |statement| statement.to_triple }
131
131
  end
132
132
 
133
133
  ##
134
- # @return [Array<Array>]
134
+ # @return [Array<Array(Value)>]
135
135
  def quads(&block)
136
136
  block_given? ? each_quad(&block) : map { |statement| statement.to_quad }
137
137
  end
@@ -165,7 +165,7 @@ module RDF
165
165
  end
166
166
 
167
167
  ##
168
- # @param [Statement, Array]
168
+ # @param [Statement, Array(Value)]
169
169
  # @return [Graph]
170
170
  def <<(statement)
171
171
  @data << case statement
@@ -23,13 +23,13 @@ module RDF
23
23
  end
24
24
 
25
25
  ##
26
- # @return [{String => Symbol}]
26
+ # @return [Hash{String => Symbol}]
27
27
  def self.content_types
28
28
  @@content_types
29
29
  end
30
30
 
31
31
  ##
32
- # @return [{Symbol => String}]
32
+ # @return [Hash{Symbol => String}]
33
33
  def self.file_extensions
34
34
  @@file_extensions
35
35
  end
@@ -89,7 +89,7 @@ module RDF
89
89
 
90
90
  ##
91
91
  # @yield [triple]
92
- # @yieldparam [Array]
92
+ # @yieldparam [Array(Value)]
93
93
  # @return [Reader]
94
94
  def each_triple(&block)
95
95
  begin
@@ -0,0 +1,242 @@
1
+ module RDF
2
+ ##
3
+ # An RDF repository.
4
+ class Repository
5
+ include Enumerable
6
+
7
+ # @return [URI]
8
+ attr_reader :uri
9
+
10
+ # @return [String]
11
+ attr_reader :title
12
+
13
+ ##
14
+ # Loads an N-Triples file as a transient in-memory repository.
15
+ #
16
+ # @param [String] filename
17
+ # @yield [repository]
18
+ # @yieldparam [Repository]
19
+ # @return [void]
20
+ def self.load(filename, options = {}, &block)
21
+ self.new(options) do |repository|
22
+ repository.load(filename, options)
23
+
24
+ if block_given?
25
+ case block.arity
26
+ when 1 then block.call(repository)
27
+ else repository.instance_eval(&block)
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ ##
34
+ # @yield [repository]
35
+ # @yieldparam [Repository]
36
+ def initialize(options = {}, &block)
37
+ @uri = options.delete(:uri)
38
+ @title = options.delete(:title)
39
+ @data, @options = [], options
40
+
41
+ if block_given?
42
+ case block.arity
43
+ when 1 then block.call(self)
44
+ else instance_eval(&block)
45
+ end
46
+ end
47
+ end
48
+
49
+ ##
50
+ # Returns `true` if the repository is transient.
51
+ #
52
+ # @return [Boolean]
53
+ # @see #persistent?
54
+ def transient?
55
+ !persistent?
56
+ end
57
+
58
+ ##
59
+ # Returns `true` if the repository is persistent.
60
+ #
61
+ # @return [Boolean]
62
+ # @see #transient?
63
+ # @abstract
64
+ def persistent?
65
+ false # NOTE: override this in any persistent subclasses
66
+ end
67
+
68
+ ##
69
+ # Returns `true` if the repository is readable.
70
+ #
71
+ # @return [Boolean]
72
+ def readable?
73
+ true
74
+ end
75
+
76
+ ##
77
+ # Returns `true` if the repository is mutable.
78
+ #
79
+ # @return [Boolean]
80
+ # @see #immutable?
81
+ # @see #immutable!
82
+ def mutable?
83
+ !immutable?
84
+ end
85
+
86
+ alias_method :writable?, :mutable?
87
+
88
+ ##
89
+ # Returns `true` if the repository is immutable.
90
+ #
91
+ # @return [Boolean]
92
+ # @see #mutable?
93
+ # @see #immutable!
94
+ def immutable?
95
+ @options[:mutable] == false
96
+ end
97
+
98
+ ##
99
+ # Makes the repository contents immutable.
100
+ #
101
+ # @return [void]
102
+ # @see #mutable?
103
+ # @see #immutable?
104
+ def immutable!
105
+ @options[:mutable] = true
106
+ end
107
+
108
+ ##
109
+ # Returns `true` if the repository contains no RDF statements.
110
+ #
111
+ # @return [Boolean]
112
+ def empty?
113
+ @data.empty?
114
+ end
115
+
116
+ ##
117
+ # Returns the number of RDF statements in the repository.
118
+ #
119
+ # @return [Integer]
120
+ def size
121
+ @data.size
122
+ end
123
+
124
+ alias_method :count, :size
125
+
126
+ ##
127
+ # Returns `true` if the repository contains the given RDF statement.
128
+ #
129
+ # @param [Statement] statement
130
+ # @return [Boolean]
131
+ def has_statement?(statement)
132
+ @data.include?(statement)
133
+ end
134
+
135
+ alias_method :include?, :has_statement?
136
+
137
+ ##
138
+ # Enumerates each RDF statement in the repository.
139
+ #
140
+ # @yield [statement]
141
+ # @yieldparam [Statement]
142
+ # @return [Repository]
143
+ def each_statement(&block)
144
+ @data.each(&block)
145
+ self
146
+ end
147
+
148
+ alias_method :each, :each_statement
149
+
150
+ ##
151
+ # Queries the repository for RDF statements matching the given pattern.
152
+ #
153
+ # @param [Statement, Array(Value)] pattern
154
+ # @yield [statement]
155
+ # @yieldparam [Statement]
156
+ # @return [Array<Statement>, nil]
157
+ def query(pattern, &block)
158
+ raise TypeError.new("repository is not readable") unless readable?
159
+ case pattern
160
+ when Array
161
+ query(Statement.new(*pattern), &block)
162
+ when Statement
163
+ if block_given?
164
+ # TODO: yield any found statements
165
+ nil
166
+ else
167
+ find_all { |statement| pattern === statement }
168
+ end
169
+ end
170
+ end
171
+
172
+ ##
173
+ # Loads RDF statements from the given N-Triples file into the repository.
174
+ #
175
+ # @param [String] filename
176
+ # @return [Integer] the number of inserted RDF statements
177
+ def load(filename, options = {})
178
+ raise TypeError.new("repository is immutable") if immutable?
179
+ count = 0
180
+ Reader.open(filename, options) do |reader|
181
+ reader.each_statement do |statement|
182
+ insert_statement(statement)
183
+ count += 1
184
+ end
185
+ end
186
+ count
187
+ end
188
+
189
+ ##
190
+ # Inserts an RDF statement to the repository.
191
+ #
192
+ # @param [Statement, Array(Value), #to_a] statement
193
+ # @return [Repository]
194
+ def <<(statement)
195
+ raise TypeError.new("repository is immutable") if immutable?
196
+ case statement
197
+ when Statement then insert_statement(statement)
198
+ else insert_statement(Statement.new(*statement.to_a))
199
+ end
200
+ self
201
+ end
202
+
203
+ ##
204
+ # Inserts RDF statements into the repository.
205
+ #
206
+ # @param [Array<Statement>] statements
207
+ # @raise [TypeError] if the repository is immutable
208
+ # @return [Repository]
209
+ def insert(*statements)
210
+ raise TypeError.new("repository is immutable") if immutable?
211
+ statements.each do |statement|
212
+ insert_statement(statement)
213
+ end
214
+ self
215
+ end
216
+
217
+ ##
218
+ # Deletes RDF statements from the repository.
219
+ #
220
+ # @param [Array<Statement>] statements
221
+ # @raise [TypeError] if the repository is immutable
222
+ # @return [Repository]
223
+ def delete(*statements)
224
+ raise TypeError.new("repository is immutable") if immutable?
225
+ statements.each do |statement|
226
+ delete_statement(statement)
227
+ end
228
+ self
229
+ end
230
+
231
+ protected
232
+
233
+ def insert_statement(statement)
234
+ @data.push(statement) unless @data.include?(statement)
235
+ end
236
+
237
+ def delete_statement(statement)
238
+ @data.delete(statement)
239
+ end
240
+
241
+ end
242
+ end
@@ -23,37 +23,58 @@ module RDF
23
23
  # @param [Value] o
24
24
  # @option options [Resource] :context (nil)
25
25
  def initialize(s = nil, p = nil, o = nil, options = {})
26
+ @id = options[:id] if options.has_key?(:id)
26
27
  @context = options[:context] || options[:graph]
27
28
  @subject, @predicate, @object = s, p, o
28
29
  end
29
30
 
30
31
  ##
31
32
  # @return [Boolean]
32
- def asserted?() !quoted? end
33
+ def asserted?
34
+ !quoted?
35
+ end
36
+
37
+ ##
38
+ # @return [Boolean]
39
+ def quoted?
40
+ false
41
+ end
33
42
 
34
43
  ##
35
44
  # @return [Boolean]
36
- def quoted?() false end
45
+ def inferred?
46
+ false
47
+ end
37
48
 
38
49
  ##
39
50
  # @return [Boolean]
40
- def has_graph?() has_context? end
51
+ def has_graph?
52
+ has_context?
53
+ end
41
54
 
42
55
  ##
43
56
  # @return [Boolean]
44
- def has_context?() !!context end
57
+ def has_context?
58
+ !!context
59
+ end
45
60
 
46
61
  ##
47
62
  # @return [Boolean]
48
- def has_subject?() !!subject end
63
+ def has_subject?
64
+ !!subject
65
+ end
49
66
 
50
67
  ##
51
68
  # @return [Boolean]
52
- def has_predicate?() !!predicate end
69
+ def has_predicate?
70
+ !!predicate
71
+ end
53
72
 
54
73
  ##
55
74
  # @return [Boolean]
56
- def has_object?() !!object end
75
+ def has_object?
76
+ !!object
77
+ end
57
78
 
58
79
  ##
59
80
  # @param [Statement] other
@@ -100,24 +121,19 @@ module RDF
100
121
  end
101
122
 
102
123
  ##
103
- # @return [Array]
104
- def to_triple
105
- [subject, predicate, object]
106
- end
107
-
108
- ##
109
- # @return [Array]
124
+ # @return [Array(Value)]
110
125
  def to_quad
111
126
  [subject, predicate, object, context]
112
127
  end
113
128
 
114
129
  ##
115
- # @return [Array]
116
- def to_a() to_triple end
130
+ # @return [Array(Value)]
131
+ def to_triple
132
+ [subject, predicate, object]
133
+ end
117
134
 
118
- ##
119
- # @return [Array]
120
- def to_ary() to_triple end
135
+ alias_method :to_a, :to_triple
136
+ alias_method :to_ary, :to_triple
121
137
 
122
138
  ##
123
139
  # @return [Hash]
@@ -2,7 +2,7 @@ module RDF
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 4
5
+ TINY = 5
6
6
  EXTRA = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
@@ -132,7 +132,7 @@ module RDF
132
132
  end
133
133
 
134
134
  ##
135
- # @param [Array<Array>] triples
135
+ # @param [Array<Array(Value)>] triples
136
136
  def write_triples(*triples)
137
137
  triples.each { |triple| write_triple(*triple) }
138
138
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arto Bendiken
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-23 00:00:00 +01:00
12
+ date: 2009-12-27 00:00:00 +01:00
13
13
  default_executable: rdf
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -37,6 +37,10 @@ email: arto.bendiken@gmail.com
37
37
  executables:
38
38
  - rdf
39
39
  - rdf-count
40
+ - rdf-lengths
41
+ - rdf-objects
42
+ - rdf-predicates
43
+ - rdf-subjects
40
44
  extensions: []
41
45
 
42
46
  extra_rdoc_files: []
@@ -44,7 +48,6 @@ extra_rdoc_files: []
44
48
  files:
45
49
  - AUTHORS
46
50
  - README
47
- - Rakefile
48
51
  - UNLICENSE
49
52
  - VERSION
50
53
  - bin/rdf
@@ -60,6 +63,7 @@ files:
60
63
  - lib/rdf/node.rb
61
64
  - lib/rdf/reader/ntriples.rb
62
65
  - lib/rdf/reader.rb
66
+ - lib/rdf/repository.rb
63
67
  - lib/rdf/resource.rb
64
68
  - lib/rdf/statement.rb
65
69
  - lib/rdf/uri.rb
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env ruby
2
- $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'lib')))
3
- require 'rubygems'
4
- begin
5
- require 'rakefile' # http://github.com/bendiken/rakefile
6
- rescue LoadError => e
7
- end
8
- require 'rdf'