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

Inherits:
Ternary show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-bb59900f675b/lib/sparql/algebra/operator/regex.rb

Overview

The SPARQL regex operator.

Examples:

(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

NAME =
:regex

Constants inherited from Ternary

Ternary::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, #to_sxp_bin, #validate!, #variable?, #vars

Methods included from Expression

cast, #constant?, #evaluate, extension, extensions, for, #invalid?, new, #node?, open, #optimize, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Constructor Details

#initialize(text, pattern, flags = RDF::Literal(''), options = {}) ⇒ Regex

Initializes a new operator instance.

Parameters:

Raises:

  • (TypeError)

    if any operand is invalid



29
30
31
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-bb59900f675b/lib/sparql/algebra/operator/regex.rb', line 29

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

Instance Method Details

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



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-bb59900f675b/lib/sparql/algebra/operator/regex.rb', line 45

def apply(text, pattern, flags = RDF::Literal(''))
  # @see http://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 http://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 http://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