Class: RDF::Literal

Inherits:
Object show all
Includes:
Term
Defined in:
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-xsd-276267e926a9/lib/rdf/xsd/date.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-xsd-276267e926a9/lib/rdf/xsd/double.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-xsd-276267e926a9/lib/rdf/xsd/integer.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-xsd-276267e926a9/lib/rdf/xsd/xml.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-xsd-276267e926a9/lib/rdf/xsd/any_uri.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-xsd-276267e926a9/lib/rdf/xsd/duration.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-xsd-276267e926a9/lib/rdf/xsd/binary.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal/date.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal/double.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal/datetime.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal/numeric.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal/integer.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal/decimal.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal/boolean.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal/token.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal/time.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-spec-fc9710e70bc6/lib/rdf/spec/inspects.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/json-ld-94ee7791d19f/lib/json/ld/extensions.rb

Overview

override several inspect functions to improve output for what we're doing

Defined Under Namespace

Classes: AnyURI, Base64Binary, Boolean, Byte, Date, DateTime, DateTimeStamp, Day, DayTimeDuration, Decimal, Double, Duration, Float, HTML, HexBinary, Int, Integer, Long, Month, MonthDay, NegativeInteger, NonNegativeInteger, NonPositiveInteger, Numeric, PositiveInteger, Short, Time, Token, UnsignedByte, UnsignedInt, UnsignedLong, UnsignedShort, XML, Year, YearMonth, YearMonthDuration

Constant Summary collapse

TRUE =
RDF::Literal.new(true)
FALSE =
RDF::Literal.new(false)
ZERO =
RDF::Literal.new(0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Term

#<=>, #aggregate?, #evaluate, #ndvars, #term?, #to_base, #to_term, #vars

Methods included from SPARQL::Algebra::Expression

cast, #constant?, #evaluate, extension, extensions, for, #invalid?, #node?, open, #optimize, parse, register_extension, #to_sxp_bin, #variable?

Methods included from Value

#anonymous?, #canonicalize, #constant?, #graph?, #inspect!, #invalid?, #iri?, #list?, #node?, #resource?, #start_with?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #variable?

Constructor Details

#initialize(value, language: nil, datatype: nil, lexical: nil, validate: false, canonicalize: false, **options) ⇒ Literal

Literals without a datatype are given either xsd:string or rdf:langString depending on if there is language

Parameters:

  • value (Object)
  • language (Symbol)

    (nil) Language is downcased to ensure proper matching

  • lexical (String)

    (nil) Supplied lexical representation of this literal, otherwise it comes from transforming value to a string form..

  • datatype (URI)

    (nil)

  • validate (Boolean)

    (false)

  • canonicalize (Boolean)

    (false)

Raises:

  • (ArgumentError)

    if there is a language and datatype is no rdf:langString or datatype is rdf:langString and there is no language

See Also:



164
165
166
167
168
169
170
171
172
173
174
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 164

def initialize(value, language: nil, datatype: nil, lexical: nil, validate: false, canonicalize: false, **options)
  @object   = value.freeze
  @string   = lexical if 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 = language.to_s.downcase.to_sym if language
  @datatype = RDF::URI(datatype).freeze if datatype
  @datatype ||= self.class.const_get(:DATATYPE) if self.class.const_defined?(:DATATYPE)
  @datatype ||= @language ? RDF.langString : RDF::URI("http://www.w3.org/2001/XMLSchema#string")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#String (protected)

This method is implemented when the datatype is xsd:string or rdf:langString

Returns:



488
489
490
491
492
493
494
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 488

def method_missing(name, *args)
  case name
  when :to_str
    return to_s if @datatype == RDF.langString || @datatype == RDF::URI("http://www.w3.org/2001/XMLSchema#string")
  end
  super
end

Instance Attribute Details

#datatypeURI

Returns The XML Schema datatype URI (optional).

Returns:

  • (URI)

    The XML Schema datatype URI (optional).



143
144
145
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 143

def datatype
  @datatype
end

#languageSymbol

Returns The language tag (optional).

Returns:

  • (Symbol)

    The language tag (optional).



140
141
142
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 140

def language
  @language
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: ===

Returns true if this literal is equivalent to other (with type check).

Examples:

RDF::Literal(1) == RDF::Literal(1.0)     #=> true

Parameters:

Returns:

See Also:



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 285

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

#canonicalize!RDF::Literal

Converts this literal into its canonical lexical representation.

Subclasses should override this as needed and appropriate.

Returns:

Since:

  • 0.3.0



408
409
410
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 408

def canonicalize!
  self
end

#compatible?(other) ⇒ Boolean

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

Examples:

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

Returns:

See Also:

Since:

  • 2.0



221
222
223
224
225
226
227
228
229
230
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 221

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::URI("http://www.w3.org/2001/XMLSchema#string")) :
    other.datatype == RDF::URI("http://www.w3.org/2001/XMLSchema#string")
end

#comperable_datatype?(other) ⇒ Boolean

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

Returns:



387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 387

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

#eql?(other) ⇒ Boolean

Determins if self is the same term as other.

Examples:

RDF::Literal(1).eql?(RDF::Literal(1.0))  #=> false

Parameters:

Returns:



265
266
267
268
269
270
271
272
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 265

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

#escape(string) ⇒ String

Note:

N-Triples only requires '\"\n\r' to be escaped.

Escape a literal using ECHAR escapes.

ECHAR ::= '\' [tbnrf"']

Parameters:

Returns:

See Also:



446
447
448
449
450
451
452
453
454
455
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 446

def escape(string)
  string.gsub('\\', '\\\\').
         gsub("\t", '\\t').
         gsub("\b", '\\b').
         gsub("\n", '\\n').
         gsub("\r", '\\r').
         gsub("\f", '\\f').
         gsub('"', '\\"').
         freeze
end

#has_datatype?Boolean Also known as: datatype?, typed?, datatyped?

Returns true if this is a datatyped literal.

For historical reasons, this excludes xsd:string and rdf:langString

Returns:

See Also:



349
350
351
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 349

def has_datatype?
  !plain? && !language?
end

#has_language?Boolean Also known as: language?

Returns true if this is a language-tagged literal.

Returns:

See Also:



337
338
339
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 337

def has_language?
  datatype == RDF.langString
end

#hashInteger

Returns a hash code for this literal.

Returns:



236
237
238
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 236

def hash
  @hash ||= [to_s, datatype, language].hash
end

#humanize(lang = :en) ⇒ String

Returns a human-readable value for the literal

Returns:

Since:

  • 1.1.6



470
471
472
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 470

def humanize(lang = :en)
  to_s.freeze
end

#inspectString

Returns a developer-friendly representation of self.

Returns:



478
479
480
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 478

def inspect
  sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, RDF::NTriples.serialize(self))
end

#literal?Boolean

Returns true.

Returns:



194
195
196
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 194

def literal?
  true
end

#objectObject

Returns:



186
187
188
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 186

def object
  defined?(@object) ? @object : value
end

#plain?Boolean

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.

Returns:

See Also:



318
319
320
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 318

def plain?
  [RDF.langString, RDF::URI("http://www.w3.org/2001/XMLSchema#string")].include?(datatype)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean (protected)

Returns:



496
497
498
499
500
501
502
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 496

def respond_to_missing?(name, include_private = false)
  case name
  when :to_str
    return true if @datatype == RDF.langString || @datatype == RDF::URI("http://www.w3.org/2001/XMLSchema#string")
  end
  super
end

#simple?Boolean

Returns true if this is a simple literal. A simple literal has no datatype or language.

Returns:

See Also:



328
329
330
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 328

def simple?
  datatype == RDF::URI("http://www.w3.org/2001/XMLSchema#string")
end

#squish(*other_string) ⇒ RDF::Literal

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.

Returns:

See Also:



419
420
421
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 419

def squish(*other_string)
  self.dup.squish!
end

#squish!Object

Performs a destructive #squish.



428
429
430
431
432
433
434
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 428

def squish!
  @string = value.
    gsub(/\A[[:space:]]+/, '').
    gsub(/[[:space:]]+\z/, '').
    gsub(/[[:space:]]+/, ' ')
  self
end

#to_sString

Returns the value as a string.

Returns:



461
462
463
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 461

def to_s
  @object.to_s.freeze
end

#valid?Boolean

Returns true if the value adheres to the defined grammar of the datatype.

Returns:

Since:

  • 0.2.1



362
363
364
365
366
367
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 362

def valid?
  return false if language? && language.to_s !~ /^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/
  return false if datatype? && datatype.invalid?
  grammar = self.class.const_get(:GRAMMAR) rescue nil
  grammar.nil? || value.match?(grammar)
end

#valid_extended?Boolean

Validate extended RDF

Returns:



37
38
39
40
41
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/json-ld-94ee7791d19f/lib/json/ld/extensions.rb', line 37

def valid_extended?
  return false if language? && language.to_s !~ /^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/
  return false if datatype? && datatype.invalid?
  value.is_a?(String)
end

#validate!RDF::Literal

Validates the value using Value#valid?, raising an error if the value is invalid.

Returns:

Raises:

  • (ArgumentError)

    if the value is invalid

Since:

  • 0.2.1



376
377
378
379
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 376

def validate!
  raise ArgumentError, "#{to_s.inspect} is not a valid <#{datatype.to_s}> literal" if invalid?
  self
end

#valueString

Returns the value as a string.

Returns:



180
181
182
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 180

def value
  @string || to_s
end

#value_hashInteger

Returns a hash code for the value.

Returns:



245
246
247
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/literal.rb', line 245

def value_hash
  @value_hash ||= value.hash
end