Class: SPARQL::Algebra::Operator::Regex

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/regex.rb

Overview

The SPARQL regex operator.

[122] RegexExpression ::= 'REGEX' '(' Expression ',' Expression ( ',' Expression )? ')'

Examples:

SPARQL Grammar

PREFIX  ex: <http://example.com/#>
PREFIX  rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?val
WHERE {
  ex:foo rdf:value ?val .
  FILTER regex(?val, "GHI")
}

SSE

(prefix ((ex: <http://example.com/#>)
         (rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>))
 (project (?val)
  (filter (regex ?val "GHI")
   (bgp (triple ex:foo rdf:value ?val)))))

See Also:

Constant Summary collapse

NAME =
:regex

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Constants included from Expression

Expression::PATTERN_PARENTS

Constants included from RDF::Util::Logger

RDF::Util::Logger::IOWrapper

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, #formulae, #initialize, #inspect, #mergable?, #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?, #evaluate, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Methods included from RDF::Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Constructor Details

This class inherits a constructor from SPARQL::Algebra::Operator

Instance Method Details

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

Matches text against a regular expression pattern.

Parameters:

  • text (RDF::Literal)

    a simple literal

  • pattern (RDF::Literal)

    a simple literal

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

    a simple literal (defaults to an empty string)

Returns:

Raises:

  • (TypeError)

    if any operand is unbound

  • (TypeError)

    if any operand is not a simple literal



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/regex.rb', line 43

def apply(text, pattern, flags = RDF::Literal(''), **options)
  # @see https://www.w3.org/TR/xpath-functions/#regex-syntax
  raise TypeError, "expected a plain RDF::Literal, but got #{text.inspect}" unless text.is_a?(RDF::Literal) && text.plain?
  text = text.to_s
  # TODO: validate text syntax

  # @see https://www.w3.org/TR/xpath-functions/#regex-syntax
  raise TypeError, "expected a plain RDF::Literal, but got #{pattern.inspect}" unless pattern.is_a?(RDF::Literal) && pattern.plain?
  pattern = pattern.to_s
  # TODO: validate pattern syntax

  # @see https://www.w3.org/TR/xpath-functions/#flags
  raise TypeError, "expected a plain RDF::Literal, but got #{flags.inspect}" unless flags.is_a?(RDF::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(Regexp.new(pattern, options) === text)
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:



72
73
74
75
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/regex.rb', line 72

def to_sparql(**options)
  ops = operands.last.to_s.empty? ? operands[0..-2] : operands
  "regex(" + ops.to_sparql(delimiter: ', ', **options) + ")"
end