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

Inherits:
Binary show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-bb59900f675b/lib/sparql/algebra/operator/strafter.rb

Overview

A SPARQL strafter operator.

Constant Summary

NAME =
:strafter

Constants inherited from Binary

Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

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=, #boolean, #constant?, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #parent, #parent=, prefixes, #prefixes, prefixes=, #rewrite, #to_binary, #to_sxp, #to_sxp_bin, #validate!, #variable?, #vars

Methods included from Expression

cast, #constant?, #evaluate, extension, extensions, for, #invalid?, new, #node?, open, #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) ⇒ 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:

Returns:

Raises:

  • (TypeError)

    if operands are not compatible



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-bb59900f675b/lib/sparql/algebra/operator/strafter.rb', line 40

def apply(left, right)
  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