Class: SPARQL::Algebra::Operator::Replace

Inherits:
Quaternary show all
Includes:
Evaluatable
Defined in:
lib/sparql/algebra/operator/replace.rb

Overview

The SPARQL replace operator.

[124] StrReplaceExpression ::= ‘REPLACE’ ‘(’ Expression ‘,’ Expression ‘,’ Expression ( ‘,’ Expression )? ‘)’

Examples:

SPARQL Grammar

PREFIX : <http://example.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?s (REPLACE(?str,"[^a-z0-9]", "-") AS ?new)
WHERE {
  ?s :str ?str
}

SSE

(prefix ((: <http://example.org/>)
         (xsd: <http://www.w3.org/2001/XMLSchema#>))
  (project (?s ?new)
    (extend ((?new (replace ?str "[^a-z0-9]" "-")))
      (bgp (triple ?s :str ?str)))))

See Also:

Constant Summary collapse

NAME =
:replace

Constants inherited from Quaternary

Quaternary::ARITY

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

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

Constructor Details

#initialize(text, pattern, replacement, flags = RDF::Literal(''), **options) ⇒ Replace

Initializes a new operator instance.

Parameters:

Raises:

  • (TypeError)

    if any operand is invalid



40
41
42
# File 'lib/sparql/algebra/operator/replace.rb', line 40

def initialize(text, pattern, replacement, flags = RDF::Literal(''), **options)
  super
end

Instance Method Details

#apply(text, pattern, replacement, flags = RDF::Literal(''), **options) ⇒ RDF::Literal

Matches text against a regular expression pattern.

Parameters:

  • text (RDF::Literal)

    a simple literal

  • pattern (RDF::Literal)

    a simple literal

  • replacement (RDF::Literal)
  • flags (RDF::Literal) (defaults to: RDF::Literal(''))

    a simple literal (defaults to an empty string)

Returns:

  • (RDF::Literal)

Raises:

  • (TypeError)

    if any operand is unbound

  • (TypeError)

    if any operand is not a plain literal



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sparql/algebra/operator/replace.rb', line 55

def apply(text, pattern, replacement, flags = RDF::Literal(''), **options)
  raise TypeError, "expected a plain RDF::Literal, but got #{text.inspect}" unless text.literal? && text.plain?
  # TODO: validate text syntax

  raise TypeError, "expected a plain RDF::Literal, but got #{pattern.inspect}" unless pattern.literal? && pattern.plain?
  pattern = pattern.to_s
  # TODO: validate pattern syntax

  raise TypeError, "expected a plain RDF::Literal, but got #{replacement.inspect}" unless replacement.literal? && replacement.plain?
  replacement = replacement.to_s.gsub('$', '\\')  # Replace references
  # TODO: validate flag syntax

  raise TypeError, "expected a plain RDF::Literal, but got #{flags.inspect}" unless flags.literal? && flags.plain?
  flags = flags.to_s
  # TODO: validate flag syntax

  options = 0
  raise NotImplementedError, "unsupported regular expression flag: /s" if flags.include?(?s) # FIXME
  options |= Regexp::MULTILINE  if flags.include?(?m)
  options |= Regexp::IGNORECASE if flags.include?(?i)
  options |= Regexp::EXTENDED   if flags.include?(?x)
  RDF::Literal(text.to_s.gsub(Regexp.new(pattern, options), replacement), datatype: text.datatype, language: text.language)
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:

  • (String)


95
96
97
98
# File 'lib/sparql/algebra/operator/replace.rb', line 95

def to_sparql(**options)
  ops = operands.last.to_s.empty? ? operands[0..-2] : operands
  "REPLACE(" + ops.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:



86
87
88
# File 'lib/sparql/algebra/operator/replace.rb', line 86

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