Class: SPARQL::Algebra::Operator::StrAfter

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

Overview

A SPARQL strafter operator.

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

Examples:

SPARQL Grammar

PREFIX : <http://example.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?s (STRAFTER(?str,"e") AS ?suffix) WHERE {
  ?s :str ?str
}

SSE

(prefix
 ((: <http://example.org/>)
  (xsd: <http://www.w3.org/2001/XMLSchema#>))
 (project (?s ?suffix)
  (extend ((?suffix (strafter ?str "e")))
   (bgp (triple ?s :str ?str)))))

See Also:

Constant Summary collapse

NAME =
:strafter

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(left, right, **options) ⇒ RDF::Literal

The STRAFTER function corresponds to the XPath fn:substring-after function. The arguments must be argument compatible otherwise an error is raised.

For compatible arguments, if the lexical part of the second argument occurs as a substring of the lexical part of the first argument, the function returns a literal of the same kind as the first argument arg1 (simple literal, plain literal same language tag, xsd:string). The lexical form of the result is the substring of the lexcial form of arg1 that follows the first occurrence of the lexical form of arg2. If the lexical form of arg2 is the empty string, this is considered to be a match and the lexical form of the result is the lexical form of arg1.

If there is no such occurrence, an empty simple literal is returned.

Examples:

strafter("abc","b") #=> "c"
strafter("abc"@en,"ab") #=> "c"@en
strafter("abc"@en,"b"@cy) #=> error
strafter("abc"^^xsd:string,"") #=> "abc"^^xsd:string
strafter("abc","xyz") #=> ""
strafter("abc"@en, "z"@en) #=> ""
strafter("abc"@en, "z") #=> ""
strafter("abc"@en, ""@en) #=> "abc"@en
strafter("abc"@en, "") #=> "abc"@en

Parameters:

  • left (RDF::Literal)

    a literal

  • right (RDF::Literal)

    a literal

Returns:

  • (RDF::Literal)

Raises:

  • (TypeError)

    if operands are not compatible



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sparql/algebra/operator/strafter.rb', line 54

def apply(left, right, **options)
  case
  when !left.compatible?(right)
    raise TypeError, "expected two RDF::Literal operands, but got #{left.inspect} and #{right.inspect}"
  when right.to_s.empty?
    # If the lexical form of arg2 is the empty string, this is considered to be a match and the lexical form of the result is the lexical form of arg1. 
    left
  when !left.to_s.include?(right.to_s)
    # If the lexical form of arg2 is the empty string, this is considered to be a match and the lexical form of the result is is the empty string. 
    RDF::Literal("")
  else
    parts = left.to_s.split(right.to_s)
    RDF::Literal(parts.last, datatype: left.datatype, language: left.language)
  end
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:

  • (String)


75
76
77
# File 'lib/sparql/algebra/operator/strafter.rb', line 75

def to_sparql(**options)
  "STRAFTER(" + operands.to_sparql(delimiter: ', ', **options) + ")"
end