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.

[121] BuiltInCall ::= … | ‘LANGMATCHES’ ‘(’ Expression ‘,’ Expression ‘)’

Examples:

SPARQL Grammar

PREFIX : <http://example.org/#>

SELECT *
{ :x ?p ?v . FILTER langMatches(lang(?v), "en-GB") . }

SSE

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

See Also:

Constant Summary collapse

NAME =
:langMatches

Constants inherited from Binary

Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Constants included from Expression

Expression::PATTERN_PARENTS

Instance Attribute Summary

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Evaluatable

#evaluate, #memoize, #replace_aggregate!, #replace_vars!

Methods inherited from Binary

#initialize

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, #base_uri, base_uri, base_uri=, #bind, #boolean, #constant?, #deep_dup, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, to_sparql, #to_sxp, #to_sxp_bin, #validate!, #variable?, #variables, #vars

Methods included from Expression

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

Constructor Details

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

Instance Method Details

#apply(language_tag, language_range, **options) ⇒ 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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sparql/algebra/operator/lang_matches.rb', line 38

def apply(language_tag, language_range, **options)
  raise TypeError, "expected a plain RDF::Literal for language_tag, but got #{language_tag.inspect}" unless language_tag.is_a?(RDF::Literal) && language_tag.simple?
  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.simple?
  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

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:

  • (String)


65
66
67
68
69
70
71
# File 'lib/sparql/algebra/operator/lang_matches.rb', line 65

def to_sparql(**options)
  "langMatches(" +
    operands.first.to_sparql(**options) +
    ", " +
    operands.last.to_sparql(**options) +
    ")"
end