Class: ShEx::Extension

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/shex/extensions/extension.rb

Overview

Abstract class of ShEx [Extension](shex.io/shex-semantics/#semantic-actions) extensions.

Extensions are registered automatically when they are required by subclassing this class.

Implementations may provide an initializer which is called once for a given semantic action. Additionally, ‘enter` and `exit` methods are invoked when beginning any Triple Expression containing this Semantic Action. The `visit` method is invoked once for each matched triple within that Triple Expression.

Subclasses must define at least ‘visit`.

Examples:

Test extension

class Test < ShEx::Extension("http://shex.io/extensions/Test/")

  # Called to initialize module before evaluating shape
  def initialize(schema: nil, depth: 0, logger: nil, **options)
  end

  # Called on entry to containing Triple Expression
  def enter(code: nil, arcs_in: nil, arcs_out: nil, depth: 0, **options)
  end

  # Called once for each matched statement
  def visit(code: nil, matched: nil, depth: 0, **options)
  end

  # Called on entry to containing Triple Expression
  def exit(code: nil, matched: [], unmatched: [], depth: 0, **options)
  end

  # Called after shape completes on success or failure
  def close(schema: nil, depth: 0, **options)
  end

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema: nil, logger: nil, depth: 0, **options) ⇒ self

Initializer for a given instance. Implementations may define this for instance and/or class

Parameters:

  • schema (ShEx::Algebra::Schema) (defaults to: nil)

    top level of the shape expression

  • logger (RDF::Util::Logger) (defaults to: nil)
  • depth (Integer) (defaults to: 0)

    for logging

  • options (Hash{Symbol => Object})

    from shape initialization



101
102
103
104
105
# File 'lib/shex/extensions/extension.rb', line 101

def initialize(schema: nil, logger: nil, depth: 0, **options)
  @logger = logger
  @options = options
  self
end

Class Method Details

.each {|klass| ... } ⇒ Enumerator

Enumerates known Semantic Action classes.

Yields:

  • (klass)

Yield Parameters:

  • klass (Class)

Returns:

  • (Enumerator)


54
55
56
57
58
59
60
61
62
63
# File 'lib/shex/extensions/extension.rb', line 54

def each(&block)
  if self.equal?(ShEx::Extension)
    # This is needed since all Semantic Action classes are defined using
    # Ruby's autoloading facility, meaning that `@@subclasses` will be
    # empty until each subclass has been touched or require'd.
    @@subclasses.values.each(&block)
  else
    block.call(self)
  end
end

.find(name) ⇒ SemanticAction

Return the SemanticAction associated with a URI.

Parameters:

  • name (#to_s)

Returns:

  • (SemanticAction)


70
71
72
# File 'lib/shex/extensions/extension.rb', line 70

def find(name)
  @@subclasses.fetch(name.to_s, nil)
end

.nameString

The “name” of this class is a URI used to uniquely identify it.

Returns:

  • (String)


44
45
46
# File 'lib/shex/extensions/extension.rb', line 44

def name
  @@subclasses.invert[self]
end

Instance Method Details

#close(schema: nil, depth: 0, **options) ⇒ self

Called after shape completes on success or failure

Parameters:

  • schema (ShEx::Algebra::Schema) (defaults to: nil)

    top level of the shape expression

  • depth (Integer) (defaults to: 0)

    for logging

  • options (Hash{Symbol => Object})

    Other, operand-specific options

Returns:

  • (self)


157
158
159
# File 'lib/shex/extensions/extension.rb', line 157

def close(schema: nil, depth: 0, **options)
  self
end

#enter(code: nil, arcs_in: nil, arcs_out: nil, expression: nil, depth: 0, **options) ⇒ Boolean

Called on entry to containing TripleExpression

Parameters:

  • code (String) (defaults to: nil)
  • arcs_in (Array<RDF::Statement>) (defaults to: nil)

    available statements to be matched

  • arcs_out (Array<RDF::Statement>) (defaults to: nil)

    available statements to be matched

  • expression (ShEx::Algebra::TripleExpression) (defaults to: nil)

    containing this semantic act

  • depth (Integer) (defaults to: 0)

    for logging

  • options (Hash{Symbol => Object})

    Other, operand-specific options

Returns:

  • (Boolean)

    Returning ‘false` results in NotSatisfied exception



118
119
120
# File 'lib/shex/extensions/extension.rb', line 118

def enter(code: nil, arcs_in: nil, arcs_out: nil, expression: nil, depth: 0, **options)
  true
end

#exit(code: nil, matched: [], unmatched: [], expression: nil, depth: 0, **options) ⇒ self

Called on exit from containing TripleExpression

Parameters:

  • code (String) (defaults to: nil)
  • matched (Array<RDF::Statement>) (defaults to: [])

    statements matched by this expression

  • unmatched (Array<RDF::Statement>) (defaults to: [])

    statements considered, but not matched by this expression

  • expression (ShEx::Algebra::TripleExpression) (defaults to: nil)

    containing this semantic act

  • depth (Integer) (defaults to: 0)

    for logging

  • options (Hash{Symbol => Object})

    Other, operand-specific options

Returns:

  • (self)


147
148
149
# File 'lib/shex/extensions/extension.rb', line 147

def exit(code: nil, matched: [], unmatched: [], expression: nil, depth: 0, **options)
  self
end

#visit(code: nil, matched: nil, expression: nil, depth: 0, **options) ⇒ Boolean

Called after a TripleExpression has matched zero or more statements

Parameters:

  • code (String) (defaults to: nil)
  • matched (RDF::Statement) (defaults to: nil)

    statement

  • depth (Integer) (defaults to: 0)

    for logging

  • expression (ShEx::Algebra::TripleExpression) (defaults to: nil)

    containing this semantic act

  • options (Hash{Symbol => Object})

    Other, operand-specific options

Returns:

Raises:

  • (NotImplementedError)


132
133
134
# File 'lib/shex/extensions/extension.rb', line 132

def visit(code: nil, matched: nil, expression: nil, depth: 0, **options)
  raise NotImplementedError
end