Class: SPARQL::Algebra::Operator::Concat

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/concat.rb

Overview

A SPARQL concat operator.

The CONCAT function corresponds to the XPath fn:concat function. The function accepts string literals as arguments.

[121] BuiltInCall ::= ... 'CONCAT' ExpressionList

Examples:

SPARQL Grammar

PREFIX : <http://example.org/>
SELECT (CONCAT(?str1,?str2) AS ?str) WHERE {
  :s6 :str ?str1 .
  :s7 :str ?str2 .
}

SSE

(prefix
 ((: <http://example.org/>))
 (project (?str)
  (extend ((?str (concat ?str1 ?str2)))
   (bgp
    (triple :s6 :str ?str1)
    (triple :s7 :str ?str2)))))

See Also:

Constant Summary collapse

NAME =
:concat

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

#apply, #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, #initialize, #inspect, #mergable?, #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?, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, parse, register_extension, #to_sxp_bin, #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

This class inherits a constructor from SPARQL::Algebra::Operator

Instance Method Details

#evaluate(bindings, **options) ⇒ RDF::Term

The lexical form of the returned literal is obtained by concatenating the lexical forms of its inputs. If all input literals are typed literals of type xsd:string, then the returned literal is also of type xsd:string, if all input literals are plain literals with identical language tag, then the returned literal is a plain literal with the same language tag, in all other cases, the returned literal is a simple literal.

Examples:

concat("foo", "bar")                         #=> "foobar"
concat("foo"@en, "bar"@en)                   #=> "foobar"@en
concat("foo"^^xsd:string, "bar"^^xsd:string) #=> "foobar"^^xsd:string
concat("foo", "bar"^^xsd:string)             #=> "foobar"
concat("foo"@en, "bar")                      #=> "foobar"
concat("foo"@en, "bar"^^xsd:string)          #=> "foobar"

Parameters:

Returns:

Raises:

  • (TypeError)

    if any operand is not a literal



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/concat.rb', line 50

def evaluate(bindings, **options)
  ops = operands.map {|op| op.evaluate(bindings, **options.merge(depth: options[:depth].to_i + 1))}

  # rdf:nil is like empty string
  if ops == [RDF.nil]
    return RDF::Literal.new("")
  end

  raise TypeError, "expected all plain literal operands" unless ops.all? {|op| op.literal? && op.plain?}

  ops.inject do |memo, op|
    case
    when memo.datatype == RDF::XSD.string && op.datatype == RDF::XSD.string
      RDF::Literal.new("#{memo}#{op}", datatype: RDF::XSD.string)
    when memo.has_language? && memo.language == op.language
      RDF::Literal.new("#{memo}#{op}", language: memo.language)
    else
      RDF::Literal.new("#{memo}#{op}")
    end
  end
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:



77
78
79
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/concat.rb', line 77

def to_sparql(**options)
  "CONCAT(#{operands.to_sparql(delimiter: ', ', **options)})"
end