/
literal.rb
481 lines (442 loc) · 16.7 KB
/
literal.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# -*- encoding: utf-8 -*-
module RDF
##
# An RDF literal.
#
# Subclasses of {RDF::Literal} should define DATATYPE and GRAMMAR constants, which are used for identifying the appropriate class to use for a datatype URI and to perform lexical matching on the value.
#
# Literal comparison with other {RDF::Value} instances call {RDF::Value#type_error}, which, returns false. Implementations wishing to have {RDF::TypeError} raised should mix-in {RDF::TypeCheck}. This is required for strict SPARQL conformance.
#
# Specific typed literals may have behavior different from the default implementation. See the following defined sub-classes for specific documentation. Additional sub-classes may be defined, and will interoperate by defining `DATATYPE` and `GRAMMAR` constants, in addition other required overrides of RDF::Literal behavior.
#
# In RDF 1.1, all literals are typed, including plain literals and language tagged literals. Internally, plain literals are given the `xsd:string` datatype and language tagged literals are given the `rdf:langString` datatype. Creating a plain literal, without a datatype or language, will automatically provide the `xsd:string` datatype; similar for language tagged literals. Note that most serialization formats will remove this datatype. Code which depends on a literal having the `xsd:string` datatype being different from a plain literal (formally, without a datatype) may break. However note that the `#has\_datatype?` will continue to return `false` for plain or language-tagged literals.
#
# * {RDF::Literal::Boolean}
# * {RDF::Literal::Date}
# * {RDF::Literal::DateTime}
# * {RDF::Literal::Decimal}
# * {RDF::Literal::Double}
# * {RDF::Literal::Integer}
# * {RDF::Literal::Time}
#
# @example Creating a plain literal
# value = RDF::Literal.new("Hello, world!")
# value.plain? #=> true`
#
# @example Creating a language-tagged literal (1)
# value = RDF::Literal.new("Hello!", language: :en)
# value.has_language? #=> true
# value.language #=> :en
#
# @example Creating a language-tagged literal (2)
# RDF::Literal.new("Wazup?", language: :"en-US")
# RDF::Literal.new("Hej!", language: :sv)
# RDF::Literal.new("¡Hola!", language: :es)
#
# @example Creating an explicitly datatyped literal
# value = RDF::Literal.new("2009-12-31", datatype: RDF::XSD.date)
# value.has_datatype? #=> true
# value.datatype #=> RDF::XSD.date
#
# @example Creating an implicitly datatyped literal
# value = RDF::Literal.new(Date.today)
# value.has_datatype? #=> true
# value.datatype #=> RDF::XSD.date
#
# @example Creating implicitly datatyped literals
# RDF::Literal.new(false).datatype #=> XSD.boolean
# RDF::Literal.new(true).datatype #=> XSD.boolean
# RDF::Literal.new(123).datatype #=> XSD.integer
# RDF::Literal.new(9223372036854775807).datatype #=> XSD.integer
# RDF::Literal.new(3.1415).datatype #=> XSD.double
# RDF::Literal.new(Time.now).datatype #=> XSD.time
# RDF::Literal.new(Date.new(2010)).datatype #=> XSD.date
# RDF::Literal.new(DateTime.new(2010)).datatype #=> XSD.dateTime
#
# @see http://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal
# @see http://www.w3.org/TR/rdf11-concepts/#section-Datatypes
class Literal
private
@@subclasses = [] # @private
@@datatype_map = nil # @private
##
# @private
# @return [void]
def self.inherited(child)
@@subclasses << child
@@datatype_map = nil
super
end
public
require 'rdf/model/literal/numeric'
require 'rdf/model/literal/boolean'
require 'rdf/model/literal/decimal'
require 'rdf/model/literal/integer'
require 'rdf/model/literal/double'
require 'rdf/model/literal/date'
require 'rdf/model/literal/datetime'
require 'rdf/model/literal/time'
require 'rdf/model/literal/token'
include RDF::Term
##
# @private
# Return Hash mapping from datatype URI to class
def self.datatype_map
@@datatype_map ||= Hash[
@@subclasses
.select {|klass| klass.const_defined?(:DATATYPE)}
.map {|klass| [klass.const_get(:DATATYPE).to_s, klass]}
]
end
##
# @private
# Return datatype class for uri, or nil if none is found
def self.datatyped_class(uri)
datatype_map[uri]
end
##
# @private
def self.new(value, options = {})
raise ArgumentError, "datatype with language must be rdf:langString" if options[:language] && (options[:datatype] || RDF.langString).to_s != RDF.langString.to_s
klass = case
when !self.equal?(RDF::Literal)
self # subclasses can be directly constructed without type dispatch
when typed_literal = datatyped_class(options[:datatype].to_s)
typed_literal
else case value
when ::TrueClass then RDF::Literal::Boolean
when ::FalseClass then RDF::Literal::Boolean
when ::Integer then RDF::Literal::Integer
when ::Float then RDF::Literal::Double
when ::BigDecimal then RDF::Literal::Decimal
when ::DateTime then RDF::Literal::DateTime
when ::Date then RDF::Literal::Date
when ::Time then RDF::Literal::Time # FIXME: Ruby's Time class can represent datetimes as well
when ::Symbol then RDF::Literal::Token
else self
end
end
literal = klass.allocate
literal.send(:initialize, value, options)
literal.validate! if options[:validate]
literal.canonicalize! if options[:canonicalize]
literal
end
TRUE = RDF::Literal.new(true)
FALSE = RDF::Literal.new(false)
ZERO = RDF::Literal.new(0)
# @return [Symbol] The language tag (optional).
attr_accessor :language
# @return [URI] The XML Schema datatype URI (optional).
attr_accessor :datatype
##
# Literals without a datatype are given either xsd:string or rdf:langString
# depending on if there is language
#
# @param [Object] value
# @option options [Symbol] :language (nil)
# Language is downcased to ensure proper matching
# @option options [String] :lexical (nil)
# Supplied lexical representation of this literal,
# otherwise it comes from transforming `value` to a string form
# See {#to_s}.
# @option options [URI] :datatype (nil)
# @option options [Boolean] :validate (false)
# @option options [Boolean] :canonicalize (false)
# @raise [ArgumentError]
# if there is a language and datatype is no rdf:langString
# or datatype is rdf:langString and there is no language
# @see http://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal
# @see http://www.w3.org/TR/rdf11-concepts/#section-Datatypes
def initialize(value, options = {})
@object = value.freeze
@string = options[:lexical] if options[:lexical]
@string = value if !defined?(@string) && value.is_a?(String)
@string = @string.encode(Encoding::UTF_8).freeze if @string
@object = @string if @string && @object.is_a?(String)
@language = options[:language].to_s.downcase.to_sym if options[:language]
@datatype = RDF::URI(options[:datatype]).freeze if options[:datatype]
@datatype ||= self.class.const_get(:DATATYPE) if self.class.const_defined?(:DATATYPE)
@datatype ||= @language ? RDF.langString : RDF::XSD.string
raise ArgumentError, "datatype of rdf:langString requires a language" if !@language && @datatype == RDF::langString
end
##
# Returns the value as a string.
#
# @return [String]
def value
@string || to_s
end
##
# @return [Object]
def object
defined?(@object) ? @object : value
end
##
# Returns `true`.
#
# @return [Boolean] `true` or `false`
def literal?
true
end
##
# Term compatibility according to SPARQL
#
# Compatibility of two arguments is defined as:
# * The arguments are simple literals or literals typed as xsd:string
# * The arguments are plain literals with identical language tags
# * The first argument is a plain literal with language tag and the second argument is a simple literal or literal typed as xsd:string
#
# @example
# compatible?("abc" "b") #=> true
# compatible?("abc" "b"^^xsd:string) #=> true
# compatible?("abc"^^xsd:string "b") #=> true
# compatible?("abc"^^xsd:string "b"^^xsd:string) #=> true
# compatible?("abc"@en "b") #=> true
# compatible?("abc"@en "b"^^xsd:string) #=> true
# compatible?("abc"@en "b"@en) #=> true
# compatible?("abc"@fr "b"@ja) #=> false
# compatible?("abc" "b"@ja) #=> false
# compatible?("abc" "b"@en) #=> false
# compatible?("abc"^^xsd:string "b"@en) #=> false
#
# @see http://www.w3.org/TR/sparql11-query/#func-arg-compatibility
# @since 2.0
def compatible?(other)
return false unless other.literal? && plain? && other.plain?
# * The arguments are simple literals or literals typed as xsd:string
# * The arguments are plain literals with identical language tags
# * The first argument is a plain literal with language tag and the second argument is a simple literal or literal typed as xsd:string
has_language? ?
(language == other.language || other.datatype == RDF::XSD.string) :
other.datatype == RDF::XSD.string
end
##
# Returns a hash code for this literal.
#
# @return [Fixnum]
def hash
@hash ||= [to_s, datatype, language].hash
end
##
# Returns a hash code for the value.
#
# @return [Fixnum]
def value_hash
@value_hash ||= value.hash
end
##
# @private
def freeze
hash.freeze
value_hash.freeze
super
end
##
# Determins if `self` is the same term as `other`.
#
# @example
# RDF::Literal(1).eql?(RDF::Literal(1.0)) #=> false
#
# @param [Object] other
# @return [Boolean] `true` or `false`
def eql?(other)
self.equal?(other) ||
(self.class.eql?(other.class) &&
self.value_hash == other.value_hash &&
self.value.eql?(other.value) &&
self.language.to_s.eql?(other.language.to_s) &&
self.datatype.eql?(other.datatype))
end
##
# Returns `true` if this literal is equivalent to `other` (with type check).
#
# @example
# RDF::Literal(1) == RDF::Literal(1.0) #=> true
#
# @param [Object] other
# @return [Boolean] `true` or `false`
#
# @see http://www.w3.org/TR/rdf-sparql-query/#func-RDFterm-equal
# @see http://www.w3.org/TR/rdf-concepts/#section-Literal-Equality
def ==(other)
case other
when Literal
case
when self.eql?(other)
true
when self.has_language? && self.language.to_s == other.language.to_s
# Literals with languages can compare if languages are identical
self.value_hash == other.value_hash && self.value == other.value
when self.simple? && other.simple?
self.value_hash == other.value_hash && self.value == other.value
when other.comperable_datatype?(self) || self.comperable_datatype?(other)
# Comoparing plain with undefined datatypes does not generate an error, but returns false
# From data-r2/expr-equal/eq-2-2.
false
else
type_error("unable to determine whether #{self.inspect} and #{other.inspect} are equivalent")
end
when String
self.simple? && self.value.eql?(other)
else false
end
end
alias_method :===, :==
##
# Returns `true` if this is a plain literal. A plain literal
# may have a language, but may not have a datatype. For
# all practical purposes, this includes xsd:string literals
# too.
#
# @return [Boolean] `true` or `false`
# @see http://www.w3.org/TR/rdf-concepts/#dfn-plain-literal
def plain?
[RDF.langString, RDF::XSD.string].include?(datatype)
end
##
# Returns `true` if this is a simple literal.
# A simple literal has no datatype or language.
#
# @return [Boolean] `true` or `false`
# @see http://www.w3.org/TR/sparql11-query/#simple_literal
def simple?
datatype == RDF::XSD.string
end
##
# Returns `true` if this is a language-tagged literal.
#
# @return [Boolean] `true` or `false`
# @see http://www.w3.org/TR/rdf-concepts/#dfn-plain-literal
def has_language?
datatype == RDF.langString
end
alias_method :language?, :has_language?
##
# Returns `true` if this is a datatyped literal.
#
# For historical reasons, this excludes xsd:string and rdf:langString
#
# @return [Boolean] `true` or `false`
# @see http://www.w3.org/TR/rdf-concepts/#dfn-typed-literal
def has_datatype?
!plain? && !language?
end
alias_method :datatype?, :has_datatype?
alias_method :typed?, :has_datatype?
alias_method :datatyped?, :has_datatype?
##
# Returns `true` if the value adheres to the defined grammar of the
# datatype.
#
# @return [Boolean] `true` or `false`
# @since 0.2.1
def valid?
grammar = self.class.const_get(:GRAMMAR) rescue nil
grammar.nil? || !!(value =~ grammar)
end
##
# Validates the value using {RDF::Value#valid?}, raising an error if the value is
# invalid.
#
# @return [RDF::Literal] `self`
# @raise [ArgumentError] if the value is invalid
# @since 0.2.1
def validate!
raise ArgumentError, "#{to_s.inspect} is not a valid <#{datatype.to_s}> literal" if invalid?
self
end
##
# Returns `true` if the literal has a datatype and the comparison should
# return false instead of raise a type error.
#
# This behavior is intuited from SPARQL data-r2/expr-equal/eq-2-2
# @return [Boolean]
def comperable_datatype?(other)
return false unless self.plain? || self.has_language?
case other
when RDF::Literal::Numeric, RDF::Literal::Boolean,
RDF::Literal::Date, RDF::Literal::Time, RDF::Literal::DateTime
# Invald types can be compared without raising a TypeError if literal has a language (open-eq-08)
!other.valid? && self.has_language?
else
# An unknown datatype may not be used for comparison, unless it has a language? (open-eq-8)
self.has_language?
end
end
##
# Converts this literal into its canonical lexical representation.
#
# Subclasses should override this as needed and appropriate.
#
# @return [RDF::Literal] `self`
# @since 0.3.0
def canonicalize!
self
end
##
# Returns the literal, first removing all whitespace on both ends of the value, and then changing remaining consecutive whitespace groups into one space each.
#
# Note that it handles both ASCII and Unicode whitespace.
#
# @see [String#squish](http://apidock.com/rails/String/squish)
# @return [RDF::Literal] a new literal based on `self`.
def squish(*other_string)
self.dup.squish!
end
##
# Performs a destructive {#squish}.
#
# @see [String#squish!](http://apidock.com/rails/String/squish%21)
# @return self
def squish!
@string = value.
gsub(/\A[[:space:]]+/, '').
gsub(/[[:space:]]+\z/, '').
gsub(/[[:space:]]+/, ' ')
self
end
##
# Escape a literal using ECHAR escapes.
#
# ECHAR ::= '\' [tbnrf"'\]
#
# @note N-Triples only requires '\"\n\r' to be escaped.
#
# @param [String] string
# @return [String]
# @see {RDF::Term#escape}
def escape(string)
string.gsub('\\', '\\\\').
gsub("\t", '\\t').
gsub("\b", '\\b').
gsub("\n", '\\n').
gsub("\r", '\\r').
gsub("\f", '\\f').
gsub('"', '\\"').
freeze
end
##
# Returns the value as a string.
#
# @return [String]
def to_s
@object.to_s.freeze
end
##
# Returns a human-readable value for the literal
#
# @return [String]
# @since 1.1.6
def humanize(lang = :en)
to_s.freeze
end
##
# Returns a developer-friendly representation of `self`.
#
# @return [String]
def inspect
sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, RDF::NTriples.serialize(self))
end
end # Literal
end # RDF