Class: SPARQL::Algebra::Operator::Replace
- Inherits:
-
Quaternary
- Object
- SPARQL::Algebra::Operator
- Quaternary
- SPARQL::Algebra::Operator::Replace
- Includes:
- Evaluatable
- Defined in:
- lib/sparql/algebra/operator/replace.rb
Overview
The SPARQL replace
operator.
[124] StrReplaceExpression ::= ‘REPLACE’ ‘(’ Expression ‘,’ Expression ‘,’ Expression ( ‘,’ Expression )? ‘)’
Constant Summary collapse
- NAME =
:replace
Constants inherited from Quaternary
Constants inherited from SPARQL::Algebra::Operator
Constants included from Expression
Instance Attribute Summary
Attributes inherited from SPARQL::Algebra::Operator
Instance Method Summary collapse
-
#apply(text, pattern, replacement, flags = RDF::Literal(''), **options) ⇒ RDF::Literal
Matches
text
against a regular expressionpattern
. -
#initialize(text, pattern, replacement, flags = RDF::Literal(''), **options) ⇒ Replace
constructor
Initializes a new operator instance.
-
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
-
#to_sxp_bin ⇒ Array
Returns the SPARQL S-Expression (SSE) representation of this expression.
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.
40 41 42 |
# File 'lib/sparql/algebra/operator/replace.rb', line 40 def initialize(text, pattern, replacement, flags = RDF::Literal(''), **) super end |
Instance Method Details
#apply(text, pattern, replacement, flags = RDF::Literal(''), **options) ⇒ RDF::Literal
Matches text
against a regular expression pattern
.
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(''), **) 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 = 0 raise NotImplementedError, "unsupported regular expression flag: /s" if flags.include?(?s) # FIXME |= Regexp::MULTILINE if flags.include?(?m) |= Regexp::IGNORECASE if flags.include?(?i) |= Regexp::EXTENDED if flags.include?(?x) RDF::Literal(text.to_s.gsub(Regexp.new(pattern, ), replacement), datatype: text.datatype, language: text.language) end |
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
95 96 97 98 |
# File 'lib/sparql/algebra/operator/replace.rb', line 95 def to_sparql(**) ops = operands.last.to_s.empty? ? operands[0..-2] : operands "REPLACE(" + ops.to_sparql(delimiter: ', ', **) + ")" end |