rdf 0.0.6 → 0.0.7

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.
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
File without changes
File without changes
@@ -66,6 +66,18 @@ module RDF
66
66
  end
67
67
  end
68
68
 
69
+ ##
70
+ # @return [Boolean]
71
+ def invalid?
72
+ !valid?
73
+ end
74
+
75
+ ##
76
+ # @return [Boolean]
77
+ def valid?
78
+ has_subject? && has_predicate? && has_object?
79
+ end
80
+
69
81
  ##
70
82
  # @return [Boolean]
71
83
  def asserted?
@@ -21,11 +21,19 @@ module RDF
21
21
  end
22
22
 
23
23
  ##
24
- # @param [URI, String, #to_s] uri
25
- def initialize(uri)
26
- @uri = case uri
27
- when Addressable::URI then uri
28
- else Addressable::URI.parse(uri.to_s)
24
+ # @overload uri(uri)
25
+ # @param [URI, String, #to_s] uri
26
+ #
27
+ # @overload uri(options = {})
28
+ # @param [Hash{Symbol => Object} options
29
+ def initialize(uri_or_options)
30
+ case uri_or_options
31
+ when Hash
32
+ @uri = Addressable::URI.new(uri_or_options)
33
+ when Addressable::URI
34
+ @uri = uri_or_options
35
+ else
36
+ @uri = Addressable::URI.parse(uri_or_options.to_s)
29
37
  end
30
38
  end
31
39
 
@@ -56,13 +64,18 @@ module RDF
56
64
  # @param [Object] other
57
65
  # @return [Boolean]
58
66
  def ==(other)
59
- other.respond_to?(:to_uri) && @uri == other.to_uri
67
+ case other
68
+ when Addressable::URI
69
+ to_s == other.to_s
70
+ else
71
+ other.respond_to?(:to_uri) && to_s == other.to_uri.to_s
72
+ end
60
73
  end
61
74
 
62
75
  ##
63
76
  # @return [URI]
64
77
  def to_uri
65
- @uri
78
+ self
66
79
  end
67
80
 
68
81
  ##
@@ -0,0 +1,135 @@
1
+ module RDF
2
+ ##
3
+ # An RDF value.
4
+ #
5
+ # This is the base class for the RDF.rb class hierarchy. The class of
6
+ # every object that can be a component of {RDF::Statement statements} is a
7
+ # subclass of this class.
8
+ #
9
+ # @example Checking if a value is a resource (blank node or URI reference)
10
+ # value.resource?
11
+ #
12
+ # @example Checking if a value is a blank node
13
+ # value.node?
14
+ #
15
+ # @example Checking if a value is a URI reference
16
+ # value.uri?
17
+ # value.iri?
18
+ #
19
+ # @example Checking if a value is a literal
20
+ # value.literal?
21
+ #
22
+ # @abstract
23
+ # @see RDF::Graph
24
+ # @see RDF::Literal
25
+ # @see RDF::Node
26
+ # @see RDF::Resource
27
+ # @see RDF::Statement
28
+ # @see RDF::URI
29
+ class Value
30
+ include Comparable
31
+
32
+ # Prevent the instantiation of this class.
33
+ private_class_method :new
34
+
35
+ ##
36
+ # Returns `true` if this value is a graph.
37
+ #
38
+ # @return [Boolean]
39
+ def graph?
40
+ false
41
+ end
42
+
43
+ ##
44
+ # Returns `true` if this value is a literal.
45
+ #
46
+ # @return [Boolean]
47
+ def literal?
48
+ false
49
+ end
50
+
51
+ ##
52
+ # Returns `true` if this value is a blank node.
53
+ #
54
+ # @return [Boolean]
55
+ def node?
56
+ false
57
+ end
58
+
59
+ ##
60
+ # Returns `true` if this value is a resource.
61
+ #
62
+ # @return [Boolean]
63
+ def resource?
64
+ false
65
+ end
66
+
67
+ ##
68
+ # Returns `true` if this value is a statement.
69
+ #
70
+ # @return [Boolean]
71
+ def statement?
72
+ false
73
+ end
74
+
75
+ ##
76
+ # Returns `true` if this value is a URI reference.
77
+ #
78
+ # @return [Boolean]
79
+ def uri?
80
+ false
81
+ end
82
+
83
+ alias_method :iri?, :uri?
84
+
85
+ ##
86
+ # Compares this value to `other` for sorting purposes.
87
+ #
88
+ # Subclasses should override this to provide a more meaningful
89
+ # implementation than the default which simply performs a string
90
+ # comparison based on `#to_s`.
91
+ #
92
+ # @abstract
93
+ # @param [Object] other
94
+ # @return [Integer] -1, 0, 1
95
+ def <=>(other)
96
+ self.to_s <=> other.to_s
97
+ end
98
+
99
+ ##
100
+ # Returns an RDF::Value representation of this object.
101
+ #
102
+ # @return [Value]
103
+ def to_rdf
104
+ self
105
+ end
106
+
107
+ ##
108
+ # Returns a developer-friendly representation of this value.
109
+ #
110
+ # The result will be of the format `#<RDF::Value::0x12345678(...)>`,
111
+ # where `...` is the string returned by `#to_s`.
112
+ #
113
+ # @return [String]
114
+ def inspect
115
+ sprintf("#<%s:%#0x(%s)>", self.class.name, object_id, to_s)
116
+ end
117
+
118
+ ##
119
+ # Outputs a developer-friendly representation of this value to `stderr`.
120
+ #
121
+ # @return [void]
122
+ def inspect!
123
+ warn(inspect)
124
+ end
125
+
126
+ private
127
+
128
+ def self.inherited(child) #:nodoc:
129
+ # Enable the instantiation of any subclasses.
130
+ child.send(:public_class_method, :new)
131
+ super
132
+ end
133
+
134
+ end
135
+ end
data/lib/rdf/ntriples.rb CHANGED
@@ -1,11 +1,44 @@
1
1
  module RDF
2
2
  ##
3
- # N-Triples serialization format.
3
+ # **`RDF::NTriples`** provides support for the N-Triples serialization
4
+ # format.
5
+ #
6
+ # N-Triples is a line-based plain-text format for encoding an RDF graph.
7
+ # It is a very restricted, explicit and well-defined subset of both
8
+ # [Turtle](http://www.w3.org/TeamSubmission/turtle/) and
9
+ # [Notation3](http://www.w3.org/TeamSubmission/n3/) (N3).
10
+ #
11
+ # The MIME content type for N-Triples files is `text/plain` and the
12
+ # recommended file extension is `.nt`.
13
+ #
14
+ # An example of an RDF statement in N-Triples format:
15
+ #
16
+ # <http://rubyforge.org/> <http://purl.org/dc/terms/title> "RubyForge" .
17
+ #
18
+ # Installation
19
+ # ------------
20
+ #
21
+ # This is the only RDF serialization format that is directly supported by
22
+ # RDF.rb. Support for other formats is available in the form of add-on
23
+ # gems, e.g. 'rdf-xml' or 'rdf-json'.
24
+ #
25
+ # Documentation
26
+ # -------------
27
+ #
28
+ # * {RDF::NTriples::Format}
29
+ # * {RDF::NTriples::Reader}
30
+ # * {RDF::NTriples::Writer}
31
+ #
32
+ # @example Requiring the `RDF::NTriples` module explicitly
33
+ # require 'rdf/ntriples'
4
34
  #
5
35
  # @see http://www.w3.org/TR/rdf-testcases/#ntriples
6
36
  # @see http://en.wikipedia.org/wiki/N-Triples
37
+ # @see http://librdf.org/ntriples/
38
+ #
39
+ # @author [Arto Bendiken](http://ar.to/)
7
40
  module NTriples
8
- autoload :Format, 'rdf/ntriples/format'
41
+ require 'rdf/ntriples/format'
9
42
  autoload :Reader, 'rdf/ntriples/reader'
10
43
  autoload :Writer, 'rdf/ntriples/writer'
11
44
  end
@@ -1,4 +1,4 @@
1
- module RDF module NTriples
1
+ module RDF::NTriples
2
2
  ##
3
3
  # N-Triples format specification.
4
4
  #
@@ -7,7 +7,7 @@ module RDF module NTriples
7
7
  content_type 'text/plain', :extension => :nt
8
8
  content_encoding 'ascii'
9
9
 
10
- reader RDF::NTriples::Reader
11
- writer RDF::NTriples::Format
10
+ reader { RDF::NTriples::Reader }
11
+ writer { RDF::NTriples::Writer }
12
12
  end
13
- end end
13
+ end
@@ -1,4 +1,4 @@
1
- module RDF module NTriples
1
+ module RDF::NTriples
2
2
  ##
3
3
  # N-Triples parser.
4
4
  #
@@ -84,4 +84,4 @@ module RDF module NTriples
84
84
  string
85
85
  end
86
86
  end
87
- end end
87
+ end
@@ -1,8 +1,8 @@
1
- module RDF module NTriples
1
+ module RDF::NTriples
2
2
  ##
3
3
  # N-Triples serializer.
4
4
  #
5
- # @see <http://www.w3.org/TR/rdf-testcases/#ntriples>
5
+ # @see http://www.w3.org/TR/rdf-testcases/#ntriples
6
6
  class Writer < RDF::Writer
7
7
  format RDF::NTriples::Format
8
8
 
@@ -19,32 +19,39 @@ module RDF module NTriples
19
19
  # @param [Value] object
20
20
  # @return [void]
21
21
  def write_triple(subject, predicate, object)
22
- s = format_uri(subject)
23
- p = format_uri(predicate)
24
- o = object.kind_of?(RDF::URI) ? format_uri(object) : format_literal(object)
25
- puts "%s %s %s ." % [s, p, o]
22
+ puts "%s %s %s ." % [subject, predicate, object].map { |value| format_value(value) }
26
23
  end
27
24
 
28
25
  ##
29
- # @param [node] Resource
30
- # @return [void]
31
- def format_uri(node)
32
- "<%s>" % uri_for(node)
26
+ # @param [URI] value
27
+ # @param [Hash{Symbol => Object}] options
28
+ # @return [String]
29
+ def format_uri(value, options = {})
30
+ "<%s>" % uri_for(value)
33
31
  end
34
32
 
35
33
  ##
36
- # @param [String, Literal] literal
37
- # @return [void]
38
- def format_literal(literal)
39
- case literal
34
+ # @param [Node] value
35
+ # @param [Hash{Symbol => Object}] options
36
+ # @return [String]
37
+ def format_node(value, options = {})
38
+ "_:%s" % node.id
39
+ end
40
+
41
+ ##
42
+ # @param [Literal, String, #to_s] value
43
+ # @param [Hash{Symbol => Object}] options
44
+ # @return [String]
45
+ def format_literal(value, options = {})
46
+ case value
40
47
  when RDF::Literal
41
- text = quoted(escaped(literal.value))
42
- text << "@#{literal.language}" if literal.language
43
- text << "^^<#{uri_for(literal.datatype)}>" if literal.datatype
48
+ text = quoted(escaped(value.value))
49
+ text << "@#{value.language}" if value.language
50
+ text << "^^<#{uri_for(value.datatype)}>" if value.datatype
44
51
  text
45
52
  else
46
- quoted(escaped(literal.to_s))
53
+ quoted(escaped(value.to_s))
47
54
  end
48
55
  end
49
56
  end
50
- end end
57
+ end
data/lib/rdf/query.rb CHANGED
@@ -41,7 +41,7 @@ module RDF
41
41
  autoload :Solution, 'rdf/query/solution'
42
42
  autoload :Variable, 'rdf/query/variable'
43
43
 
44
- include Enumerable
44
+ include ::Enumerable
45
45
 
46
46
  # @return [Hash{Symbol => Variable}]
47
47
  attr_reader :variables
@@ -75,7 +75,7 @@ module RDF
75
75
  #
76
76
  # @yield [solution]
77
77
  # @yieldparam [Solution]
78
- # @return [Enumerable]
78
+ # @return [Enumerator]
79
79
  def each_solution(&block)
80
80
  solutions.each do |bindings|
81
81
  block.call(Solution.new(bindings))
@@ -123,7 +123,7 @@ module RDF
123
123
  ##
124
124
  # Reorders the solution sequence based on `variables`.
125
125
  #
126
- # @param [Enumerable<Symbol>] variables
126
+ # @param [Array<Symbol>] variables
127
127
  # @return [Query]
128
128
  def order(*variables)
129
129
  if variables.empty?
@@ -145,7 +145,7 @@ module RDF
145
145
  ##
146
146
  # Restricts the the solution sequence to the given `variables` only.
147
147
  #
148
- # @param [Enumerable<Symbol>] variables
148
+ # @param [Array<Symbol>] variables
149
149
  # @return [Query]
150
150
  def project(*variables)
151
151
  unless variables.empty?
@@ -1,7 +1,7 @@
1
- module RDF class Query
1
+ class RDF::Query
2
2
  ##
3
3
  # An RDF query pattern.
4
- class Pattern < Statement
4
+ class Pattern < RDF::Statement
5
5
  # @return [Hash{Symbol => Object}]
6
6
  attr_reader :options
7
7
 
@@ -149,4 +149,4 @@ module RDF class Query
149
149
  end
150
150
  end
151
151
  end
152
- end end
152
+ end
@@ -1,4 +1,4 @@
1
- module RDF class Query
1
+ class RDF::Query
2
2
  ##
3
3
  # An RDF query solution.
4
4
  #
@@ -136,4 +136,4 @@ module RDF class Query
136
136
  end
137
137
 
138
138
  end
139
- end end
139
+ end
@@ -1,4 +1,4 @@
1
- module RDF class Query
1
+ class RDF::Query
2
2
  ##
3
3
  # An RDF query variable.
4
4
  #
@@ -38,7 +38,7 @@ module RDF class Query
38
38
  # var = RDF::Query::Variable.new(:y, 123)
39
39
  # var.to_s #=> "?y=123"
40
40
  #
41
- class Variable < Value
41
+ class Variable < RDF::Value
42
42
  # @return [Symbol] The variable's name.
43
43
  attr_accessor :name
44
44
 
@@ -142,4 +142,4 @@ module RDF class Query
142
142
  unbound? ? "?#{name}" : "?#{name}=#{value}"
143
143
  end
144
144
  end
145
- end end
145
+ end
data/lib/rdf/reader.rb CHANGED
@@ -2,41 +2,114 @@ module RDF
2
2
  ##
3
3
  # An RDF parser.
4
4
  #
5
+ # @example Iterating over known RDF reader classes
6
+ # RDF::Reader.each { |klass| puts klass.name }
7
+ #
8
+ # @example Obtaining an RDF reader class
9
+ # RDF::Reader.for(:ntriples) #=> RDF::NTriples::Reader
10
+ # RDF::Reader.for("spec/data/test.nt")
11
+ # RDF::Reader.for(:file_name => "spec/data/test.nt")
12
+ # RDF::Reader.for(:file_extension => "nt")
13
+ # RDF::Reader.for(:content_type => "text/plain")
14
+ #
15
+ # @example Instantiating an RDF reader class
16
+ # RDF::Reader.for(:ntriples).new($stdin) { |reader| ... }
17
+ #
18
+ # @example Parsing RDF statements from a file
19
+ # RDF::Reader.open("spec/data/test.nt") do |reader|
20
+ # reader.each_statement do |statement|
21
+ # puts statement.inspect
22
+ # end
23
+ # end
24
+ #
25
+ # @example Parsing RDF statements from a string
26
+ # data = StringIO.new(File.read("spec/data/test.nt"))
27
+ # RDF::Reader.for(:ntriples).new(data) do |reader|
28
+ # reader.each_statement do |statement|
29
+ # puts statement.inspect
30
+ # end
31
+ # end
32
+ #
5
33
  # @abstract
34
+ # @see RDF::Format
35
+ # @see RDF::Writer
6
36
  class Reader
7
- autoload :NTriples, 'rdf/reader/ntriples' # @deprecated
8
-
9
- include Enumerable
37
+ extend ::Enumerable
38
+ include ::Enumerable
10
39
 
11
40
  ##
12
41
  # Enumerates known RDF reader classes.
13
42
  #
14
43
  # @yield [klass]
15
44
  # @yieldparam [Class]
45
+ # @return [Enumerator]
16
46
  def self.each(&block)
17
- !block_given? ? @@subclasses : @@subclasses.each { |klass| yield klass }
47
+ @@subclasses.each(&block)
48
+ end
49
+
50
+ ##
51
+ # Finds an RDF reader class based on the given criteria.
52
+ #
53
+ # @overload for(format)
54
+ # Finds an RDF reader class based on a symbolic name.
55
+ #
56
+ # @param [Symbol] format
57
+ # @return [Class]
58
+ #
59
+ # @overload for(filename)
60
+ # Finds an RDF reader class based on a file name.
61
+ #
62
+ # @param [String] filename
63
+ # @return [Class]
64
+ #
65
+ # @overload for(options = {})
66
+ # Finds an RDF reader class based on various options.
67
+ #
68
+ # @param [Hash{Symbol => Object}] options
69
+ # @option options [String, #to_s] :file_name (nil)
70
+ # @option options [Symbol, #to_sym] :file_extension (nil)
71
+ # @option options [String, #to_s] :content_type (nil)
72
+ # @return [Class]
73
+ #
74
+ # @return [Class]
75
+ def self.for(options = {})
76
+ if format = Format.for(options)
77
+ format.reader
78
+ end
18
79
  end
19
80
 
20
81
  ##
21
- # @param [Symbol] format
82
+ # Retrieves the RDF serialization format class for this writer class.
83
+ #
22
84
  # @return [Class]
23
- def self.for(format)
24
- klass = case format.to_s.downcase.to_sym
25
- when :ntriples then RDF::NTriples::Reader
26
- else nil # FIXME
85
+ def self.format(klass = nil)
86
+ if klass.nil?
87
+ Format.each do |format|
88
+ if format.reader == self
89
+ return format
90
+ end
91
+ end
92
+ nil # not found
27
93
  end
28
94
  end
29
95
 
96
+ class << self
97
+ alias_method :format_class, :format
98
+ end
99
+
30
100
  ##
31
101
  # @param [String] filename
32
102
  # @option options [Symbol] :format (:ntriples)
33
103
  # @yield [reader]
34
104
  # @yieldparam [Reader]
105
+ # @raise [FormatError] if no reader available for the specified format
35
106
  def self.open(filename, options = {}, &block)
36
- options[:format] ||= :ntriples # FIXME
37
-
38
107
  File.open(filename, 'rb') do |file|
39
- self.for(options[:format]).new(file, options, &block)
108
+ if reader = self.for(options[:format] || filename)
109
+ reader.new(file, options, &block)
110
+ else
111
+ raise FormatError.new("unknown RDF format: #{options[:format] || filename}")
112
+ end
40
113
  end
41
114
  end
42
115
 
@@ -118,10 +191,6 @@ module RDF
118
191
  super
119
192
  end
120
193
 
121
- def self.format(klass)
122
- # TODO
123
- end
124
-
125
194
  def lineno
126
195
  @input.lineno
127
196
  end