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

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/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.

Constant Summary collapse

NAME =
:concat

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

#apply, #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, #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?, 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

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:

  • bindings (RDF::Query::Solution)

    a query solution containing zero or more variable bindings

  • options (Hash{Symbol => Object}) (defaults to: {})

    ({}) options passed from query

Returns:

Raises:

  • (TypeError)

    if any operand is not a literal



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/lib/sparql/algebra/operator/concat.rb', line 35

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

  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