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

Inherits:
Quaternary show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-683a29666c28/lib/sparql/algebra/operator/replace.rb

Overview

The SPARQL replace operator.

Examples:

(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

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(text, pattern, replacement, flags = RDF::Literal(''), options = {}) ⇒ Replace

Initializes a new operator instance.

Parameters:

Raises:

  • (TypeError)

    if any operand is invalid



30
31
32
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-683a29666c28/lib/sparql/algebra/operator/replace.rb', line 30

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

Instance Method Details

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

Matches text against a regular expression pattern.

Parameters:

Returns:

Raises:

  • (TypeError)

    if any operand is unbound

  • (TypeError)

    if any operand is not a plain literal



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-683a29666c28/lib/sparql/algebra/operator/replace.rb', line 45

def apply(text, pattern, replacement, flags = RDF::Literal(''))
  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_sxp_binArray

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

Remove the optional argument.

Returns:

See Also:



76
77
78
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-683a29666c28/lib/sparql/algebra/operator/replace.rb', line 76

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