Class: SPARQL::Algebra::Operator::StrBefore

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

Overview

A SPARQL strbefore operator.

Constant Summary collapse

NAME =
:strbefore

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 STRBEFORE function corresponds to the XPath fn:substring-before 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 lexical form of arg1 that precedes 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 empty string.

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

Examples:

strbefore("abc","b") #=> "a"
strbefore("abc"@en,"bc") #=> "a"@en
strbefore("abc"@en,"b"@cy) #=> error
strbefore("abc"^^xsd:string,"") #=> ""^^xsd:string
strbefore("abc","xyz") #=> ""
strbefore("abc"@en, "z"@en) #=> ""
strbefore("abc"@en, "z") #=> ""
strbefore("abc"@en, ""@en) #=> ""@en
strbefore("abc"@en, "") #=> ""@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
55
56
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/strbefore.rb', line 40

def apply(left, right)
  case
  when !left.plain? || !right.plain?
    raise TypeError, "expected two RDF::Literal operands, but got #{left.inspect} and #{right.inspect}"
  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 is the empty string. 
    RDF::Literal("", language: left.language, datatype: left.datatype)
  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.first, language: left.language, datatype: left.datatype)
  end
end