rdf 1.0.10.2 → 1.1.0.p0
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.
- checksums.yaml +8 -8
- data/CREDITS +0 -2
- data/README +67 -20
- data/VERSION +1 -1
- data/etc/doap.nt +75 -75
- data/lib/rdf.rb +15 -64
- data/lib/rdf/format.rb +11 -20
- data/lib/rdf/mixin/countable.rb +4 -11
- data/lib/rdf/mixin/enumerable.rb +4 -8
- data/lib/rdf/mixin/mutable.rb +1 -4
- data/lib/rdf/mixin/queryable.rb +13 -60
- data/lib/rdf/model/graph.rb +46 -22
- data/lib/rdf/model/list.rb +27 -102
- data/lib/rdf/model/literal.rb +48 -100
- data/lib/rdf/model/literal/date.rb +1 -1
- data/lib/rdf/model/literal/datetime.rb +1 -35
- data/lib/rdf/model/literal/decimal.rb +0 -30
- data/lib/rdf/model/literal/double.rb +6 -14
- data/lib/rdf/model/literal/integer.rb +3 -11
- data/lib/rdf/model/literal/numeric.rb +0 -40
- data/lib/rdf/model/literal/time.rb +4 -3
- data/lib/rdf/model/node.rb +1 -3
- data/lib/rdf/model/statement.rb +7 -47
- data/lib/rdf/model/term.rb +9 -0
- data/lib/rdf/model/uri.rb +28 -68
- data/lib/rdf/model/value.rb +1 -31
- data/lib/rdf/nquads.rb +4 -7
- data/lib/rdf/ntriples.rb +2 -2
- data/lib/rdf/ntriples/format.rb +1 -2
- data/lib/rdf/ntriples/reader.rb +15 -68
- data/lib/rdf/ntriples/writer.rb +13 -53
- data/lib/rdf/query.rb +1 -8
- data/lib/rdf/query/pattern.rb +25 -7
- data/lib/rdf/query/solution.rb +3 -19
- data/lib/rdf/query/solutions.rb +4 -22
- data/lib/rdf/query/variable.rb +1 -3
- data/lib/rdf/reader.rb +8 -22
- data/lib/rdf/repository.rb +23 -5
- data/lib/rdf/transaction.rb +19 -8
- data/lib/rdf/util/aliasing.rb +3 -13
- data/lib/rdf/util/cache.rb +2 -3
- data/lib/rdf/util/file.rb +5 -15
- data/lib/rdf/vocab.rb +19 -20
- data/lib/rdf/writer.rb +5 -44
- metadata +12 -27
- data/lib/rdf/vocab/schema.rb +0 -652
data/lib/rdf/nquads.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
1
|
module RDF
|
3
2
|
##
|
4
3
|
# **`RDF::NQuads`** provides support for the N-Quads serialization format.
|
@@ -18,7 +17,7 @@ module RDF
|
|
18
17
|
# RDF::Format.for(:content_type => "application/n-quads")
|
19
18
|
# RDF::Format.for(:content_type => "text/x-nquads")
|
20
19
|
#
|
21
|
-
# @see http://
|
20
|
+
# @see http://sw.deri.org/2008/07/n-quads/#mediatype
|
22
21
|
# @since 0.4.0
|
23
22
|
class Format < RDF::Format
|
24
23
|
content_type 'application/n-quads', :extension => :nq, :alias => ['text/x-nquads']
|
@@ -39,14 +38,12 @@ module RDF
|
|
39
38
|
def self.detect(sample)
|
40
39
|
!!sample.match(%r(
|
41
40
|
(?:\s*(?:<[^>]*>) | (?:_:\w+)) # Subject
|
42
|
-
\s*
|
43
41
|
(?:\s*<[^>]*>) # Predicate
|
44
42
|
\s*
|
45
43
|
(?:(?:<[^>]*>) | (?:_:\w+) | (?:"[^"\n]*"(?:^^|@\S+)?)) # Object
|
46
|
-
|
47
|
-
(?:\s*(?:<[^>]*>) | (?:_:\w+)) # Context
|
44
|
+
(?:\s*<[^>]*>) # Context
|
48
45
|
\s*\.
|
49
|
-
)
|
46
|
+
)mx) && !(
|
50
47
|
sample.match(%r(@(base|prefix|keywords)|\{)) || # Not Turtle/N3/TriG
|
51
48
|
sample.match(%r(<(html|rdf))i) # Not HTML or XML
|
52
49
|
)
|
@@ -81,7 +78,7 @@ module RDF
|
|
81
78
|
subject = read_uriref || read_node || fail_subject
|
82
79
|
predicate = read_uriref(:intern => true) || fail_predicate
|
83
80
|
object = read_uriref || read_node || read_literal || fail_object
|
84
|
-
context = read_uriref || read_node
|
81
|
+
context = read_uriref || read_node || read_literal
|
85
82
|
if validate? && !read_eos
|
86
83
|
raise RDF::ReaderError, "expected end of statement in line #{lineno}: #{current_line.inspect}"
|
87
84
|
end
|
data/lib/rdf/ntriples.rb
CHANGED
@@ -32,12 +32,12 @@ module RDF
|
|
32
32
|
# @example Requiring the `RDF::NTriples` module explicitly
|
33
33
|
# require 'rdf/ntriples'
|
34
34
|
#
|
35
|
-
# @see http://www.w3.org/TR/
|
35
|
+
# @see http://www.w3.org/TR/rdf-testcases/#ntriples
|
36
36
|
# @see http://en.wikipedia.org/wiki/N-Triples
|
37
|
+
# @see http://librdf.org/ntriples/
|
37
38
|
#
|
38
39
|
# @author [Arto Bendiken](http://ar.to/)
|
39
40
|
module NTriples
|
40
|
-
require 'iconv' unless "".respond_to?(:encode )# needed on Ruby 1.8.x
|
41
41
|
require 'rdf/ntriples/format'
|
42
42
|
autoload :Reader, 'rdf/ntriples/reader'
|
43
43
|
autoload :Writer, 'rdf/ntriples/writer'
|
data/lib/rdf/ntriples/format.rb
CHANGED
@@ -15,7 +15,6 @@ module RDF::NTriples
|
|
15
15
|
# RDF::Format.for(:content_type => "text/plain")
|
16
16
|
#
|
17
17
|
# @see http://www.w3.org/TR/rdf-testcases/#ntriples
|
18
|
-
# @see http://www.w3.org/TR/n-triples/
|
19
18
|
class Format < RDF::Format
|
20
19
|
content_type 'application/n-triples', :extension => :nt, :alias => ['text/plain']
|
21
20
|
content_encoding 'utf-8'
|
@@ -40,7 +39,7 @@ module RDF::NTriples
|
|
40
39
|
\s*
|
41
40
|
(?:(?:<[^>]*>) | (?:_:\w+) | (?:"[^"\n]*"(?:^^|@\S+)?)) # Object
|
42
41
|
\s*\.
|
43
|
-
)
|
42
|
+
)mx) && !(
|
44
43
|
sample.match(%r(@(base|prefix|keywords)|\{)) || # Not Turtle/N3/TriG
|
45
44
|
sample.match(%r(<(html|rdf))i) # Not HTML or XML
|
46
45
|
) && !RDF::NQuads::Format.detect(sample)
|
data/lib/rdf/ntriples/reader.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
1
|
module RDF::NTriples
|
3
2
|
##
|
4
3
|
# N-Triples parser.
|
@@ -26,7 +25,6 @@ module RDF::NTriples
|
|
26
25
|
# end
|
27
26
|
#
|
28
27
|
# @see http://www.w3.org/TR/rdf-testcases/#ntriples
|
29
|
-
# @see http://www.w3.org/TR/n-triples/
|
30
28
|
class Reader < RDF::Reader
|
31
29
|
format RDF::NTriples::Format
|
32
30
|
|
@@ -44,60 +42,16 @@ module RDF::NTriples
|
|
44
42
|
#
|
45
43
|
# @see http://www.w3.org/TR/n-triples/
|
46
44
|
# @see http://www.w3.org/TR/turtle/
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
IRI_RANGE = Regexp.compile("[[^<>\"{}|^`\\\\]&&[^\\x00-\\x20]]").freeze
|
58
|
-
else
|
59
|
-
##
|
60
|
-
# UTF-8 regular expressions for Ruby 1.8.x.
|
61
|
-
U_CHARS1 = Regexp.compile(<<-EOS.gsub(/\s+/, ''))
|
62
|
-
\\xC3[\\x80-\\x96]| (?# [\\u00C0-\\u00D6]|)
|
63
|
-
\\xC3[\\x98-\\xB6]| (?# [\\u00D8-\\u00F6]|)
|
64
|
-
\\xC3[\\xB8-\\xBF]|[\\xC4-\\xCB][\\x80-\\xBF]| (?# [\\u00F8-\\u02FF]|)
|
65
|
-
\\xCD[\\xB0-\\xBD]| (?# [\\u0370-\\u037D]|)
|
66
|
-
\\xCD\\xBF|[\\xCE-\\xDF][\\x80-\\xBF]| (?# [\\u037F-\\u1FFF]|)
|
67
|
-
\\xE0[\\xA0-\\xBF][\\x80-\\xBF]| (?# ...)
|
68
|
-
\\xE1[\\x80-\\xBF][\\x80-\\xBF]| (?# ...)
|
69
|
-
\\xE2\\x80[\\x8C-\\x8D]| (?# [\\u200C-\\u200D]|)
|
70
|
-
\\xE2\\x81[\\xB0-\\xBF]| (?# [\\u2070-\\u218F]|)
|
71
|
-
\\xE2[\\x82-\\x85][\\x80-\\xBF]| (?# ...)
|
72
|
-
\\xE2\\x86[\\x80-\\x8F]| (?# ...)
|
73
|
-
\\xE2[\\xB0-\\xBE][\\x80-\\xBF]| (?# [\\u2C00-\\u2FEF]|)
|
74
|
-
\\xE2\\xBF[\\x80-\\xAF]| (?# ...)
|
75
|
-
\\xE3\\x80[\\x81-\\xBF]| (?# [\\u3001-\\uD7FF]|)
|
76
|
-
\\xE3[\\x81-\\xBF][\\x80-\\xBF]| (?# ...)
|
77
|
-
[\\xE4-\\xEC][\\x80-\\xBF][\\x80-\\xBF]| (?# ...)
|
78
|
-
\\xED[\\x80-\\x9F][\\x80-\\xBF]| (?# ...)
|
79
|
-
\\xEF[\\xA4-\\xB6][\\x80-\\xBF]| (?# [\\uF900-\\uFDCF]|)
|
80
|
-
\\xEF\\xB7[\\x80-\\x8F]| (?# ...)
|
81
|
-
\\xEF\\xB7[\\xB0-\\xBF]| (?# [\\uFDF0-\\uFFFD]|)
|
82
|
-
\\xEF[\\xB8-\\xBE][\\x80-\\xBF]| (?# ...)
|
83
|
-
\\xEF\\xBF[\\x80-\\xBD]| (?# ...)
|
84
|
-
\\xF0[\\x90-\\xBF][\\x80-\\xBF][\\x80-\\xBF]| (?# [\\u{10000}-\\u{EFFFF}])
|
85
|
-
[\\xF1-\\xF2][\\x80-\\xBF][\\x80-\\xBF][\\x80-\\xBF]|
|
86
|
-
\\xF3[\\x80-\\xAF][\\x80-\\xBF][\\x80-\\xBF] (?# ...)
|
87
|
-
EOS
|
88
|
-
U_CHARS2 = Regexp.compile(<<-EOS.gsub(/\s+/, ''))
|
89
|
-
\\xC2\\xB7| (?# \\u00B7|)
|
90
|
-
\\xCC[\\x80-\\xBF]|\\xCD[\\x80-\\xAF]| (?# [\\u0300-\\u036F]|)
|
91
|
-
\\xE2\\x80\\xBF|\\xE2\\x81\\x80 (?# [\\u203F-\\u2040])
|
92
|
-
EOS
|
93
|
-
IRI_RANGE = Regexp.compile(<<-EOS.gsub(/\s+/, ''))
|
94
|
-
\\x21| (?# ")
|
95
|
-
[\\x23-\\x3b]|\\x3d| (?# < & >)
|
96
|
-
[\\x3f-\\x5b]|\\x5d|\\x5f| (?# \ ^ `)
|
97
|
-
[\\x61-\\x7a]| (?# { } |)
|
98
|
-
[\\x7e-\\xff]
|
99
|
-
EOS
|
100
|
-
end
|
45
|
+
##
|
46
|
+
# Unicode regular expressions.
|
47
|
+
U_CHARS1 = Regexp.compile(<<-EOS.gsub(/\s+/, ''))
|
48
|
+
[\\u00C0-\\u00D6]|[\\u00D8-\\u00F6]|[\\u00F8-\\u02FF]|
|
49
|
+
[\\u0370-\\u037D]|[\\u037F-\\u1FFF]|[\\u200C-\\u200D]|
|
50
|
+
[\\u2070-\\u218F]|[\\u2C00-\\u2FEF]|[\\u3001-\\uD7FF]|
|
51
|
+
[\\uF900-\\uFDCF]|[\\uFDF0-\\uFFFD]|[\\u{10000}-\\u{EFFFF}]
|
52
|
+
EOS
|
53
|
+
U_CHARS2 = Regexp.compile("\\u00B7|[\\u0300-\\u036F]|[\\u203F-\\u2040]").freeze
|
54
|
+
IRI_RANGE = Regexp.compile("[[^<>\"{}|^`\\\\]&&[^\\x00-\\x20]]").freeze
|
101
55
|
|
102
56
|
# 163s
|
103
57
|
PN_CHARS_BASE = /[A-Z]|[a-z]|#{U_CHARS1}/.freeze
|
@@ -203,8 +157,7 @@ module RDF::NTriples
|
|
203
157
|
# @see http://blog.grayproductions.net/articles/understanding_m17n
|
204
158
|
# @see http://yehudakatz.com/2010/05/17/encodings-unabridged/
|
205
159
|
def self.unescape(string)
|
206
|
-
string = string.to_s
|
207
|
-
string = string.dup.force_encoding(Encoding::ASCII_8BIT) if string.respond_to?(:force_encoding)
|
160
|
+
string = string.to_s.force_encoding(Encoding::ASCII_8BIT)
|
208
161
|
|
209
162
|
# Decode \t|\n|\r|\"|\\ character escapes:
|
210
163
|
ESCAPE_CHARS.each { |escape| string.gsub!(escape.inspect[1...-1], escape) }
|
@@ -214,25 +167,21 @@ module RDF::NTriples
|
|
214
167
|
(string.sub!(ESCAPE_SURROGATE) do
|
215
168
|
if ESCAPE_SURROGATE1.include?($1.hex) && ESCAPE_SURROGATE2.include?($2.hex)
|
216
169
|
s = [$1, $2].pack('H*H*')
|
217
|
-
s = s.
|
218
|
-
# for Ruby 1.9+
|
219
|
-
s.force_encoding(Encoding::UTF_16BE).encode!(Encoding::UTF_8) :
|
220
|
-
# for Ruby 1.8.x
|
221
|
-
Iconv.conv('UTF-8', 'UTF-16BE', s)
|
170
|
+
s = s.force_encoding(Encoding::UTF_16BE).encode!(Encoding::UTF_8)
|
222
171
|
else
|
223
172
|
s = [$1.hex].pack('U*') << '\u' << $2
|
224
173
|
end
|
225
|
-
s.
|
174
|
+
s.force_encoding(Encoding::ASCII_8BIT)
|
226
175
|
end)
|
227
176
|
end
|
228
177
|
|
229
178
|
# Decode \uXXXX and \UXXXXXXXX code points:
|
230
179
|
string.gsub!(ESCAPE_CHAR) do
|
231
180
|
s = [($1 || $2).hex].pack('U*')
|
232
|
-
s.
|
181
|
+
s.force_encoding(Encoding::ASCII_8BIT)
|
233
182
|
end
|
234
183
|
|
235
|
-
string.force_encoding(Encoding::UTF_8)
|
184
|
+
string.force_encoding(Encoding::UTF_8)
|
236
185
|
string
|
237
186
|
end
|
238
187
|
|
@@ -290,8 +239,6 @@ module RDF::NTriples
|
|
290
239
|
uri.canonicalize! if canonicalize?
|
291
240
|
uri
|
292
241
|
end
|
293
|
-
rescue ArgumentError => e
|
294
|
-
raise RDF::ReaderError, "invalid URI"
|
295
242
|
end
|
296
243
|
|
297
244
|
##
|
data/lib/rdf/ntriples/writer.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
1
|
module RDF::NTriples
|
3
2
|
##
|
4
3
|
# N-Triples serializer.
|
@@ -36,7 +35,6 @@ module RDF::NTriples
|
|
36
35
|
# end
|
37
36
|
#
|
38
37
|
# @see http://www.w3.org/TR/rdf-testcases/#ntriples
|
39
|
-
# @see http://www.w3.org/TR/n-triples/
|
40
38
|
class Writer < RDF::Writer
|
41
39
|
format RDF::NTriples::Format
|
42
40
|
|
@@ -45,67 +43,53 @@ module RDF::NTriples
|
|
45
43
|
ESCAPE_ASCII = /\A[\x00-\x7F]*\z/m.freeze
|
46
44
|
|
47
45
|
##
|
48
|
-
# Escape Literal and URI content. If encoding is ASCII, all unicode
|
49
|
-
# is escaped, otherwise only ASCII characters that must be escaped are
|
50
|
-
# escaped.
|
51
|
-
#
|
52
46
|
# @param [String] string
|
53
|
-
# @param [Encoding] encoding
|
47
|
+
# @param [Encoding] encoding
|
54
48
|
# @return [String]
|
55
49
|
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
|
56
50
|
def self.escape(string, encoding = nil)
|
57
51
|
ret = case
|
58
52
|
when string =~ ESCAPE_PLAIN # a shortcut for the simple case
|
59
53
|
string
|
60
|
-
when string.
|
54
|
+
when string.ascii_only?
|
61
55
|
StringIO.open do |buffer|
|
62
|
-
|
63
|
-
string.each_byte { |u| buffer << escape_ascii(u, encoding) }
|
56
|
+
string.each_byte { |u| buffer << escape_ascii(u) }
|
64
57
|
buffer.string
|
65
58
|
end
|
66
|
-
when
|
59
|
+
when encoding && encoding != Encoding::ASCII
|
67
60
|
# Not encoding UTF-8 characters
|
68
61
|
StringIO.open do |buffer|
|
69
|
-
buffer.set_encoding(encoding) if buffer.respond_to?(:set_encoding)
|
70
62
|
string.each_char do |u|
|
71
63
|
buffer << case u.ord
|
72
64
|
when (0x00..0x7F)
|
73
|
-
escape_ascii(u
|
65
|
+
escape_ascii(u)
|
74
66
|
else
|
75
67
|
u
|
76
68
|
end
|
77
69
|
end
|
78
70
|
buffer.string
|
79
71
|
end
|
80
|
-
|
81
|
-
# Encode ASCII && UTF-8 characters
|
72
|
+
else
|
82
73
|
StringIO.open do |buffer|
|
83
|
-
buffer.set_encoding(Encoding::ASCII) if buffer.respond_to?(:set_encoding)
|
84
74
|
string.each_codepoint { |u| buffer << escape_unicode(u, encoding) }
|
85
75
|
buffer.string
|
86
76
|
end
|
87
|
-
else # works in Ruby 1.8.x, too
|
88
|
-
# Encode ASCII && UTF-8 characters
|
89
|
-
StringIO.open do |buffer|
|
90
|
-
buffer.set_encoding(Encoding::ASCII) if buffer.respond_to?(:set_encoding)
|
91
|
-
string.scan(/./mu) { |c| buffer << escape_unicode(u = c.unpack('U*').first, encoding) }
|
92
|
-
buffer.string
|
93
|
-
end
|
94
77
|
end
|
95
|
-
|
78
|
+
encoding ? ret.dup.force_encoding(encoding) : ret
|
96
79
|
end
|
97
80
|
|
98
81
|
##
|
99
82
|
# Escape ascii and unicode characters.
|
83
|
+
# If encoding is UTF_8, only ascii characters are escaped.
|
100
84
|
#
|
101
85
|
# @param [Integer, #ord] u
|
102
|
-
# @param [Encoding] encoding
|
86
|
+
# @param [Encoding] encoding
|
103
87
|
# @return [String]
|
104
88
|
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
|
105
89
|
def self.escape_unicode(u, encoding)
|
106
90
|
case (u = u.ord)
|
107
91
|
when (0x00..0x7F) # ASCII 7-bit
|
108
|
-
escape_ascii(u
|
92
|
+
escape_ascii(u)
|
109
93
|
when (0x80..0xFFFF) # Unicode BMP
|
110
94
|
escape_utf16(u)
|
111
95
|
when (0x10000..0x10FFFF) # Unicode
|
@@ -116,22 +100,15 @@ module RDF::NTriples
|
|
116
100
|
end
|
117
101
|
|
118
102
|
##
|
119
|
-
# Standard ASCII escape sequences. If encoding is ASCII, use Test-Cases
|
120
|
-
# sequences, otherwise, assume the test-cases escape sequences. Otherwise,
|
121
|
-
# the N-Triples recommendation includes `\b` and `\f` escape sequences.
|
122
|
-
#
|
123
103
|
# @param [Integer, #ord] u
|
124
104
|
# @return [String]
|
125
105
|
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
|
126
|
-
|
127
|
-
def self.escape_ascii(u, encoding)
|
106
|
+
def self.escape_ascii(u)
|
128
107
|
case (u = u.ord)
|
129
|
-
when (0x00..
|
130
|
-
when (0x08) then (encoding.nil? || encoding == Encoding::ASCII ? escape_utf16(u) : "\\b")
|
108
|
+
when (0x00..0x08) then escape_utf16(u)
|
131
109
|
when (0x09) then "\\t"
|
132
110
|
when (0x0A) then "\\n"
|
133
|
-
when (0x0B)
|
134
|
-
when (0x0C) then (encoding.nil? || encoding == Encoding::ASCII ? escape_utf16(u) : "\\f")
|
111
|
+
when (0x0B..0x0C) then escape_utf16(u)
|
135
112
|
when (0x0D) then "\\r"
|
136
113
|
when (0x0E..0x1F) then escape_utf16(u)
|
137
114
|
when (0x22) then "\\\""
|
@@ -180,23 +157,6 @@ module RDF::NTriples
|
|
180
157
|
end
|
181
158
|
end
|
182
159
|
|
183
|
-
##
|
184
|
-
# Initializes the writer.
|
185
|
-
#
|
186
|
-
# @param [IO, File] output
|
187
|
-
# the output stream
|
188
|
-
# @param [Hash{Symbol => Object}] options
|
189
|
-
# any additional options. See {RDF::Writer#initialize}
|
190
|
-
# @option options [Boolean] :validate (true)
|
191
|
-
# whether to validate terms when serializing
|
192
|
-
# @yield [writer] `self`
|
193
|
-
# @yieldparam [RDF::Writer] writer
|
194
|
-
# @yieldreturn [void]
|
195
|
-
def initialize(output = $stdout, options = {}, &block)
|
196
|
-
options = {:validate => true}.merge(options)
|
197
|
-
super
|
198
|
-
end
|
199
|
-
|
200
160
|
##
|
201
161
|
# Outputs an N-Triples comment line.
|
202
162
|
#
|
data/lib/rdf/query.rb
CHANGED
@@ -89,7 +89,7 @@ module RDF
|
|
89
89
|
# @return [RDF::Query::Solutions]
|
90
90
|
# the resulting solution sequence
|
91
91
|
# @see RDF::Query#execute
|
92
|
-
def self.execute(queryable, patterns =
|
92
|
+
def self.execute(queryable, patterns = nil, options = {}, &block)
|
93
93
|
self.new(patterns, options, &block).execute(queryable, options)
|
94
94
|
end
|
95
95
|
|
@@ -243,9 +243,6 @@ module RDF
|
|
243
243
|
# or named contexts.
|
244
244
|
# The name of `false` will only match against the default context.
|
245
245
|
#
|
246
|
-
# If the query nas no patterns, it returns a single empty solution as
|
247
|
-
# per SPARQL 1.1 _Empty Group Pattern_.
|
248
|
-
#
|
249
246
|
# @param [RDF::Queryable] queryable
|
250
247
|
# the graph or repository to query
|
251
248
|
# @param [Hash{Symbol => Object}] options
|
@@ -262,7 +259,6 @@ module RDF
|
|
262
259
|
# @return [RDF::Query::Solutions]
|
263
260
|
# the resulting solution sequence
|
264
261
|
# @see http://www.holygoat.co.uk/blog/entry/2005-10-25-1
|
265
|
-
# @see http://www.w3.org/TR/sparql11-query/#emptyGroupPattern
|
266
262
|
def execute(queryable, options = {})
|
267
263
|
validate!
|
268
264
|
options = options.dup
|
@@ -275,9 +271,6 @@ module RDF
|
|
275
271
|
# the first pattern
|
276
272
|
@solutions = options[:solutions] || (Solutions.new << RDF::Query::Solution.new({}))
|
277
273
|
|
278
|
-
# If there are no patterns, just return the empty solution
|
279
|
-
return @solutions if empty?
|
280
|
-
|
281
274
|
patterns = @patterns
|
282
275
|
context = options.fetch(:context, options.fetch(:name, self.context))
|
283
276
|
|
data/lib/rdf/query/pattern.rb
CHANGED
@@ -67,6 +67,29 @@ module RDF; class Query
|
|
67
67
|
subject.nil? && predicate.nil? && object.nil? && context.nil?
|
68
68
|
end
|
69
69
|
|
70
|
+
##
|
71
|
+
# Returns `true` if this is a constant pattern, with all terms being
|
72
|
+
# either URIs, blank nodes, or literals.
|
73
|
+
#
|
74
|
+
# A constant pattern is structurally and functionally equivalent to an
|
75
|
+
# RDF statement.
|
76
|
+
#
|
77
|
+
# @return [Boolean] `true` or `false`
|
78
|
+
# @since 0.3.0
|
79
|
+
def constant?
|
80
|
+
!(variable?)
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Returns `true` if this is a variable pattern, with any term being
|
85
|
+
# `nil` or a variable.
|
86
|
+
#
|
87
|
+
# @return [Boolean] `true` or `false`
|
88
|
+
# @since 0.3.0
|
89
|
+
def variable?
|
90
|
+
subject.nil? || predicate.nil? || object.nil? || context.nil? || has_variables?
|
91
|
+
end
|
92
|
+
|
70
93
|
##
|
71
94
|
# Returns `true` if this pattern contains any variables.
|
72
95
|
#
|
@@ -104,7 +127,7 @@ module RDF; class Query
|
|
104
127
|
# To match triples only in the default context, set context to `false`.
|
105
128
|
#
|
106
129
|
# @example
|
107
|
-
# Pattern.new(:s, :p, :o).execute(RDF::Repository.load('
|
130
|
+
# Pattern.new(:s, :p, :o).execute(RDF::Repository.load('data.nt'))
|
108
131
|
#
|
109
132
|
# @param [RDF::Queryable] queryable
|
110
133
|
# the graph or repository to query
|
@@ -156,12 +179,7 @@ module RDF; class Query
|
|
156
179
|
# pattern with the corresponding terms in the given `statement`.
|
157
180
|
#
|
158
181
|
# @example
|
159
|
-
# pattern
|
160
|
-
# solution = pattern.solution(statement)
|
161
|
-
#
|
162
|
-
# pattern[:s] #=> statement.subject
|
163
|
-
# pattern[:p] #=> statement.predicate
|
164
|
-
# pattern[:o] #=> statement.object
|
182
|
+
# pattern.solution(statement)
|
165
183
|
#
|
166
184
|
# @param [RDF::Statement] statement
|
167
185
|
# an RDF statement to bind terms from
|
data/lib/rdf/query/solution.rb
CHANGED
@@ -25,7 +25,7 @@ class RDF::Query
|
|
25
25
|
undef_method(*instance_methods.
|
26
26
|
map(&:to_s).
|
27
27
|
select {|m| m =~ /^\w+$/}.
|
28
|
-
reject {|m| %w(object_id dup instance_eval inspect to_s class
|
28
|
+
reject {|m| %w(object_id dup instance_eval inspect to_s class).include?(m) || m[0,2] == '__'}.
|
29
29
|
map(&:to_sym))
|
30
30
|
|
31
31
|
include Enumerable
|
@@ -145,7 +145,7 @@ class RDF::Query
|
|
145
145
|
# @return [RDF::Term]
|
146
146
|
# @since 0.3.0
|
147
147
|
def []=(name, value)
|
148
|
-
@bindings[name.to_sym] = value
|
148
|
+
@bindings[name.to_sym] = value
|
149
149
|
end
|
150
150
|
|
151
151
|
##
|
@@ -182,33 +182,17 @@ class RDF::Query
|
|
182
182
|
|
183
183
|
##
|
184
184
|
# Compatible Mappings
|
185
|
-
#
|
186
185
|
# Two solution mappings u1 and u2 are compatible if, for every variable v in dom(u1) and in dom(u2), u1(v) = u2(v).
|
187
186
|
#
|
188
187
|
# @param [RDF::Query::Solution, #to_hash] other
|
189
188
|
# another query solution or hash bindings
|
190
189
|
# @return [Boolean]
|
191
|
-
# @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algCompatibleMapping
|
192
190
|
def compatible?(other)
|
193
191
|
@bindings.all? do |k, v|
|
194
192
|
!other.to_hash.has_key?(k) || other[k].eql?(v)
|
195
193
|
end
|
196
194
|
end
|
197
|
-
|
198
|
-
##
|
199
|
-
# Disjoint mapping
|
200
|
-
#
|
201
|
-
# A solution is disjoint with another solution if it shares no common variables in their domains.
|
202
|
-
#
|
203
|
-
# @param [RDF::Query::Solution] other
|
204
|
-
# @return [Boolean]
|
205
|
-
# @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algMinus
|
206
|
-
def disjoint?(other)
|
207
|
-
@bindings.none? do |k, v|
|
208
|
-
v && other.to_hash.has_key?(k) && other[k].eql?(v)
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
195
|
+
|
212
196
|
##
|
213
197
|
# Isomorphic Mappings
|
214
198
|
# Two solution mappings u1 and u2 are isomorphic if,
|