Class: SPARQL::Algebra::Operator::SubStr

Inherits:
Ternary show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/substr.rb

Overview

A SPARQL substr operator.

[123] SubstringExpression ::= 'SUBSTR' '(' Expression ',' Expression ( ',' Expression )? ')'

Examples:

SPARQL Grammar

PREFIX : <http://example.org/>
SELECT ?s ?str (SUBSTR(?str,1,1) AS ?substr)
WHERE {
  ?s :str ?str
}

SSE

(prefix ((: <http://example.org/>))
 (project (?s ?str ?substr)
  (extend ((?substr (substr ?str 1 1)))
   (bgp (triple ?s :str ?str)))))

See Also:

Constant Summary collapse

NAME =
:substr

Constants inherited from Ternary

Ternary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Constants included from Expression

Expression::PATTERN_PARENTS

Constants included from RDF::Util::Logger

RDF::Util::Logger::IOWrapper

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 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, #formulae, #inspect, #mergable?, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, to_sparql, #to_sxp, #validate!, #variable?, #variables, #vars

Methods included from Expression

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

Methods included from RDF::Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Constructor Details

#initialize(source, startingLoc, length = RDF::Literal(""), **options) ⇒ SubStr

Initializes a new operator instance.

Parameters:

Raises:

  • (TypeError)

    if any operand is invalid



37
38
39
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/substr.rb', line 37

def initialize(source, startingLoc, length = RDF::Literal(""), **options)
  super
end

Instance Method Details

#apply(source, startingLoc, length, **options) ⇒ RDF::Literal

The substr function corresponds to the XPath fn:substring function and returns a literal of the same kind (simple literal, literal with language tag, xsd:string typed literal) as the source input parameter but with a lexical form formed from the substring of the lexcial form of the source.

The arguments startingLoc and length may be derived types of xsd:integer.

The index of the first character in a strings is 1.

Examples:

substr("foobar", 4) #=> "bar"
substr("foobar"@en, 4) #=> "bar"@en
substr("foobar"^^xsd:string, 4) #=> "bar"^^xsd:string
substr("foobar", 4, 1) #=> "b"
substr("foobar"@en, 4, 1) #=> "b"@en
substr("foobar"^^xsd:string, 4, 1) #=> "b"^^xsd:string

Parameters:

Returns:

Raises:

  • (TypeError)

    if operands are not compatible



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/substr.rb', line 64

def apply(source, startingLoc, length, **options)
  raise TypeError, "expected a plain RDF::Literal, but got #{source.inspect}" unless source.literal? && source.plain?
  text = text.to_s

  raise TypeError, "expected an integer, but got #{startingLoc.inspect}" unless startingLoc.is_a?(RDF::Literal::Integer)
  startingLoc = startingLoc.to_i

  if length == RDF::Literal("")
    RDF::Literal(source.to_s[(startingLoc-1)..-1], datatype: source.datatype, language: source.language)
  else
    raise TypeError, "expected an integer, but got #{length.inspect}" unless length.is_a?(RDF::Literal::Integer)
    length = length.to_i
    RDF::Literal(source.to_s[(startingLoc-1), length], datatype: source.datatype, language: source.language)
  end
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:



96
97
98
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/substr.rb', line 96

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

#to_sxp_binArray

Returns the SPARQL S-Expression (SSE) representation of this expression.

Remove the optional argument.

Returns:

See Also:



87
88
89
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/substr.rb', line 87

def to_sxp_bin
  [NAME] + operands.reject {|o| o.to_s == ""}
end