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

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Evaluatable
Defined in:
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

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, #initialize, #inspect, #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?

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})

    ({}) options passed from query

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 '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:

  • (String)


77
78
79
# File 'lib/sparql/algebra/operator/concat.rb', line 77

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