Class: SPARQL::Algebra::Operator::LangMatches

Inherits:
Binary show all
Includes:
Evaluatable
Defined in:
lib/sparql/algebra/operator/lang_matches.rb

Overview

The SPARQL langMatches operator.

Examples:

(prefix ((: <http://example.org/#>))
  (filter (langMatches (lang ?v) "en-GB")
    (bgp (triple :x ?p ?v))))

See Also:

Constant Summary

NAME =
:langMatches

Constants inherited from Binary

Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY

Instance Attribute Summary

Attributes inherited from SPARQL::Algebra::Operator

#operands, #options

Instance Method Summary collapse

Methods included from Evaluatable

#evaluate, #memoize

Methods inherited from Binary

#initialize

Methods inherited from SPARQL::Algebra::Operator

arity, base_uri, #base_uri, base_uri=, #boolean, #constant?, #eql?, #evaluatable?, evaluate, #executable?, for, #initialize, #inspect, #operand, #optimize, prefixes, #prefixes, prefixes=, #to_sse, #to_sxp, #variable?

Methods included from Expression

cast, #constant?, #evaluate, for, new, open, #optimize, parse, #to_sse, #variable?

Constructor Details

This class inherits a constructor from SPARQL::Algebra::Operator::Binary

Instance Method Details

#apply(language_tag, language_range) ⇒ RDF::Literal::Boolean

Returns true if the language tag (the first operand) matches the language range (the second operand).

Parameters:

  • language_tag (RDF::Literal)

    a simple literal containing a language tag

  • language_range (RDF::Literal)

    a simple literal containing a language range, per RFC 4647 section 2.1

Returns:

  • (RDF::Literal::Boolean)

    true or false

Raises:

  • (TypeError)

    if either operand is unbound

  • (TypeError)

    if either operand is not a simple literal



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sparql/algebra/operator/lang_matches.rb', line 30

def apply(language_tag, language_range)
  raise TypeError, "expected a plain RDF::Literal for language_tag, but got #{language_tag.inspect}" unless language_tag.is_a?(RDF::Literal) && language_tag.plain?
  language_tag = language_tag.to_s.downcase

  raise TypeError, "expected a plain RDF::Literal for language_range, but got #{language_range.inspect}" unless language_range.is_a?(RDF::Literal) && language_range.plain?
  language_range = language_range.to_s.downcase

  case
    # A language range of "*" matches any non-empty language tag.
    when language_range.eql?('*')
      RDF::Literal(!(language_tag.empty?))
    # A language range matches a particular language tag if, in a
    # case-insensitive comparison, it exactly equals the tag, ...
    when language_tag.eql?(language_range)
      RDF::Literal::TRUE
    # ... or if it exactly equals a prefix of the tag such that the
    # first character following the prefix is "-".
    else
      RDF::Literal(language_tag.start_with?(language_range + '-'))
  end
end