rdf 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +11 -3
- data/VERSION +1 -1
- data/bin/rdf +28 -0
- data/lib/rdf.rb +1 -0
- data/lib/rdf/reader.rb +128 -0
- data/lib/rdf/reader/ntriples.rb +78 -0
- data/lib/rdf/statement.rb +27 -7
- data/lib/rdf/version.rb +1 -1
- metadata +6 -4
data/README
CHANGED
@@ -40,10 +40,18 @@ Examples
|
|
40
40
|
foaf[:name] #=> RDF::URI("http://xmlns.com/foaf/0.1/name")
|
41
41
|
foaf['mbox'] #=> RDF::URI("http://xmlns.com/foaf/0.1/mbox")
|
42
42
|
|
43
|
+
### Reading N-Triples data
|
44
|
+
|
45
|
+
RDF::Reader::NTriples.open("spec/data/test.nt") do |reader|
|
46
|
+
reader.each_statement do |statement|
|
47
|
+
puts statement.inspect
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
43
51
|
Documentation
|
44
52
|
-------------
|
45
53
|
|
46
|
-
* <http://
|
54
|
+
* <http://rdf.rubyforge.org/>
|
47
55
|
|
48
56
|
Download
|
49
57
|
--------
|
@@ -73,10 +81,10 @@ official release from Gemcutter, do:
|
|
73
81
|
Resources
|
74
82
|
---------
|
75
83
|
|
76
|
-
* <http://
|
84
|
+
* <http://rdf.rubyforge.org/>
|
77
85
|
* <http://github.com/bendiken/rdf>
|
78
86
|
* <http://gemcutter.org/gems/rdf>
|
79
|
-
* <http://rubyforge.org/projects/
|
87
|
+
* <http://rubyforge.org/projects/rdf/>
|
80
88
|
* <http://raa.ruby-lang.org/project/rdf>
|
81
89
|
|
82
90
|
Author
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/bin/rdf
CHANGED
@@ -1,3 +1,31 @@
|
|
1
1
|
#!/usr/bin/env ruby -rubygems
|
2
2
|
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
|
3
3
|
require 'rdf'
|
4
|
+
|
5
|
+
abort "Usage: #{File.basename($0)} command [args...]" if ARGV.empty?
|
6
|
+
|
7
|
+
module RDF
|
8
|
+
class CLI
|
9
|
+
def count(*files)
|
10
|
+
count = 0
|
11
|
+
files.each do |file|
|
12
|
+
RDF::Reader::NTriples.open(file) do |reader|
|
13
|
+
reader.each do |statement|
|
14
|
+
count += 1
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
puts count
|
19
|
+
end
|
20
|
+
|
21
|
+
def query(*args)
|
22
|
+
# TODO
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_missing(command, *args, &block)
|
26
|
+
abort "#{File.basename($0)}: unknown command `#{command}'"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
RDF::CLI.new.send(ARGV.shift, *ARGV)
|
data/lib/rdf.rb
CHANGED
data/lib/rdf/reader.rb
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
module RDF
|
2
|
+
class ReaderError < IOError; end
|
3
|
+
|
4
|
+
class Reader
|
5
|
+
autoload :NTriples, 'rdf/reader/ntriples'
|
6
|
+
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
@@subclasses = []
|
10
|
+
@@file_extensions = {}
|
11
|
+
@@content_types = {}
|
12
|
+
@@content_encoding = {}
|
13
|
+
|
14
|
+
def self.each(&block)
|
15
|
+
!block_given? ? @@subclasses : @@subclasses.each { |klass| yield klass }
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.content_types
|
19
|
+
@@content_types
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.file_extensions
|
23
|
+
@@file_extensions
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.for(format)
|
27
|
+
klass = case format.to_s.downcase.to_sym
|
28
|
+
when :ntriples then RDF::Reader::NTriples
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.open(filename, options = {}, &block)
|
33
|
+
options[:format] ||= :ntriples # FIXME
|
34
|
+
|
35
|
+
File.open(filename, 'rb') do |file|
|
36
|
+
self.for(options[:format]).new(file, options, &block)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize(input = $stdin, options = {}, &block)
|
41
|
+
@options = options
|
42
|
+
@nodes = {}
|
43
|
+
@input = case input
|
44
|
+
when String then StringIO.new(input)
|
45
|
+
else input
|
46
|
+
end
|
47
|
+
block.call(self) if block_given?
|
48
|
+
end
|
49
|
+
|
50
|
+
def each(&block)
|
51
|
+
each_statement(&block)
|
52
|
+
end
|
53
|
+
|
54
|
+
def each_statement(&block)
|
55
|
+
each_triple { |*triple| block.call(Statement.new(*triple)) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def each_triple(&block)
|
59
|
+
begin
|
60
|
+
loop { block.call(*read_triple) }
|
61
|
+
rescue EOFError => e
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
protected
|
66
|
+
|
67
|
+
def read_triple
|
68
|
+
raise NotImplementedError
|
69
|
+
end
|
70
|
+
|
71
|
+
def fail_subject
|
72
|
+
raise RDF::ReaderError, "expected subject in #{@input.inspect} line #{lineno}"
|
73
|
+
end
|
74
|
+
|
75
|
+
def fail_predicate
|
76
|
+
raise RDF::ReaderError, "expected predicate in #{@input.inspect} line #{lineno}"
|
77
|
+
end
|
78
|
+
|
79
|
+
def fail_object
|
80
|
+
raise RDF::ReaderError, "expected object in #{@input.inspect} line #{lineno}"
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def self.inherited(child) #:nodoc:
|
86
|
+
@@subclasses << child
|
87
|
+
super
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.content_type(type, options = {})
|
91
|
+
@@content_types[type] ||= []
|
92
|
+
@@content_types[type] << self
|
93
|
+
|
94
|
+
if options[:extension]
|
95
|
+
extensions = [options[:extension]].flatten.map { |ext| ext.to_sym }
|
96
|
+
extensions.each { |ext| @@file_extensions[ext] = type }
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.content_encoding(encoding)
|
101
|
+
@@content_encoding[self] = encoding.to_sym
|
102
|
+
end
|
103
|
+
|
104
|
+
def lineno
|
105
|
+
@input.lineno
|
106
|
+
end
|
107
|
+
|
108
|
+
def readline
|
109
|
+
@line = @input.readline.chomp
|
110
|
+
end
|
111
|
+
|
112
|
+
def strip!
|
113
|
+
@line.strip!
|
114
|
+
end
|
115
|
+
|
116
|
+
def blank?
|
117
|
+
@line.nil? || @line.empty?
|
118
|
+
end
|
119
|
+
|
120
|
+
def match(pattern)
|
121
|
+
if @line =~ pattern
|
122
|
+
result, @line = $1, $'.lstrip
|
123
|
+
result || true
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module RDF class Reader
|
2
|
+
# See <http://www.w3.org/TR/rdf-testcases/#ntriples>
|
3
|
+
class NTriples < Reader
|
4
|
+
|
5
|
+
content_type 'text/plain', :extension => :nt
|
6
|
+
content_encoding 'ascii'
|
7
|
+
|
8
|
+
protected
|
9
|
+
|
10
|
+
def read_triple
|
11
|
+
# <http://www.w3.org/TR/rdf-testcases/#ntrip_grammar>
|
12
|
+
|
13
|
+
loop do
|
14
|
+
readline.strip! # EOFError thrown on end of input
|
15
|
+
|
16
|
+
unless blank? || read_comment
|
17
|
+
subject = read_uriref || read_bnode || fail_subject
|
18
|
+
predicate = read_uriref || fail_predicate
|
19
|
+
object = read_uriref || read_bnode || read_literal || fail_object
|
20
|
+
return [subject, predicate, object]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def read_comment
|
26
|
+
# <http://www.w3.org/TR/rdf-testcases/#ntrip_grammar> (comment)
|
27
|
+
|
28
|
+
match(/^#\s*(.*)$/)
|
29
|
+
end
|
30
|
+
|
31
|
+
def read_uriref
|
32
|
+
# <http://www.w3.org/TR/rdf-testcases/#ntrip_grammar> (uriref)
|
33
|
+
|
34
|
+
if uri = match(/^<([^>]+)>/)
|
35
|
+
RDF::URI.parse(uri)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def read_bnode
|
40
|
+
# <http://www.w3.org/TR/rdf-testcases/#ntrip_grammar> (nodeID)
|
41
|
+
|
42
|
+
if node_id = match(/^_:([A-Za-z][A-Za-z0-9]*)/)
|
43
|
+
@nodes[node_id] ||= Object.new # TODO: RDF::Node.new
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def read_literal
|
48
|
+
# <http://www.w3.org/TR/rdf-testcases/#ntrip_grammar> (literal)
|
49
|
+
|
50
|
+
if literal = match(/^"((?:\\"|[^"])*)"/)
|
51
|
+
literal = unescaped(literal)
|
52
|
+
|
53
|
+
if language = match(/^@([a-z]+[\-a-z0-9]*)/)
|
54
|
+
# TODO: RDF::Literal.new(literal, :language => language)
|
55
|
+
literal
|
56
|
+
elsif datatype = match(/^(\^\^)/)
|
57
|
+
# TODO: RDF::Literal.new(literal, :type => read_uriref || fail_object)
|
58
|
+
literal
|
59
|
+
else
|
60
|
+
literal # plain string literal
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def unescaped(string)
|
66
|
+
# <http://www.w3.org/TR/rdf-testcases/#ntrip_strings>
|
67
|
+
|
68
|
+
["\t", "\n", "\r", "\"", "\\"].each do |escape|
|
69
|
+
string.gsub!(escape.inspect[1...-1], escape)
|
70
|
+
end
|
71
|
+
string.gsub!(/\\u([0-9A-Fa-f]{4,4})/u) { [$1.hex].pack('U*') }
|
72
|
+
string.gsub!(/\\U([0-9A-Fa-f]{8,8})/u) { [$1.hex].pack('U*') }
|
73
|
+
|
74
|
+
string
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end end
|
data/lib/rdf/statement.rb
CHANGED
@@ -7,27 +7,47 @@ module RDF
|
|
7
7
|
attr_accessor :predicate
|
8
8
|
attr_accessor :object
|
9
9
|
|
10
|
-
def initialize(s, p, o, options = {})
|
10
|
+
def initialize(s = nil, p = nil, o = nil, options = {})
|
11
|
+
@context = options[:context] || options[:graph]
|
11
12
|
@subject, @predicate, @object = s, p, o
|
12
|
-
@context = options[:context] if options[:context]
|
13
13
|
end
|
14
14
|
|
15
|
-
def subject?() !!subject end
|
16
|
-
def predicate?() !!predicate end
|
17
|
-
def object?() !!object end
|
18
|
-
def context?() !!context end
|
19
15
|
def asserted?() !quoted? end
|
20
16
|
def quoted?() false end
|
21
17
|
|
18
|
+
def has_graph?() has_context? end
|
19
|
+
def has_context?() !!context end
|
20
|
+
def has_subject?() !!subject end
|
21
|
+
def has_predicate?() !!predicate end
|
22
|
+
def has_object?() !!object end
|
23
|
+
|
22
24
|
def ==(other)
|
23
25
|
to_a == other.to_a
|
24
26
|
end
|
25
27
|
|
28
|
+
def ===(other)
|
29
|
+
return false if has_subject? && subject != other.subject
|
30
|
+
return false if has_predicate? && predicate != other.predicate
|
31
|
+
return false if has_object? && object != other.object
|
32
|
+
return true
|
33
|
+
end
|
34
|
+
|
26
35
|
def [](index)
|
27
36
|
to_a[index]
|
28
37
|
end
|
29
38
|
|
30
|
-
def
|
39
|
+
def []=(index, value)
|
40
|
+
case index
|
41
|
+
when 0 then subject = value
|
42
|
+
when 1 then predicate = value
|
43
|
+
when 2 then object = value
|
44
|
+
when 3 then context = value
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_a() to_ary end
|
49
|
+
|
50
|
+
def to_ary
|
31
51
|
[subject, predicate, object]
|
32
52
|
end
|
33
53
|
|
data/lib/rdf/version.rb
CHANGED
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
|
+
version: 0.0.3
|
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-
|
12
|
+
date: 2009-12-22 00:00:00 +01:00
|
13
13
|
default_executable: rdf
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -47,6 +47,8 @@ files:
|
|
47
47
|
- UNLICENSE
|
48
48
|
- VERSION
|
49
49
|
- bin/rdf
|
50
|
+
- lib/rdf/reader/ntriples.rb
|
51
|
+
- lib/rdf/reader.rb
|
50
52
|
- lib/rdf/statement.rb
|
51
53
|
- lib/rdf/uri.rb
|
52
54
|
- lib/rdf/version.rb
|
@@ -68,7 +70,7 @@ files:
|
|
68
70
|
- lib/rdf/vocabulary.rb
|
69
71
|
- lib/rdf.rb
|
70
72
|
has_rdoc: false
|
71
|
-
homepage: http://
|
73
|
+
homepage: http://rdf.rubyforge.org/
|
72
74
|
licenses:
|
73
75
|
- Public Domain
|
74
76
|
post_install_message:
|
@@ -90,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
92
|
version:
|
91
93
|
requirements: []
|
92
94
|
|
93
|
-
rubyforge_project:
|
95
|
+
rubyforge_project: rdf
|
94
96
|
rubygems_version: 1.3.5
|
95
97
|
signing_key:
|
96
98
|
specification_version: 3
|