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

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

Overview

A SPARQL substr operator.

Constant Summary

NAME =
:substr

Constants inherited from Ternary

Ternary::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 SPARQL::Algebra::Operator

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

Methods included from Expression

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

Constructor Details

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

Initializes a new operator instance.

Parameters:

Raises:

  • (TypeError)

    if any operand is invalid



25
26
27
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/substr.rb', line 25

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

Instance Method Details

#apply(source, startingLoc, length) ⇒ 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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/substr.rb', line 52

def apply(source, startingLoc, length)
  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_sxp_binArray

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

Remove the optional argument.

Returns:

See Also:



75
76
77
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/substr.rb', line 75

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