Module: ShEx

Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/format.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/parser.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/algebra.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/version.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/terminals.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/extensions/test.rb

Overview

A ShEx runtime for RDF.rb.

Defined Under Namespace

Modules: Algebra, Meta, Terminals, VERSION Classes: Error, Extension, Format, NotMatched, NotSatisfied, ParseError, Parser, StructureError

Constant Summary collapse

CONTEXT =

Location of the ShEx JSON-LD context

"http://www.w3.org/ns/shex.jsonld"
EXTENSIONS =

Extensions defined in this gem

%w{test}
Test =
Class.new(ShEx::Extension("http://shex.io/extensions/Test/")) do
  # (see ShEx::Extension#visit)
  def visit(code: nil, matched: nil, depth: 0, **options)
    str = if md = /^ *(fail|print) *\( *(?:(\"(?:[^\\"]|\\")*\")|([spo])) *\) *$/.match(code.to_s)
      md[2] || case md[3]
      when 's' then matched.subject
      when 'p' then matched.predicate
      when 'o' then matched.object
      else          matched.to_sxp
      end.to_s
    else
      matched ? matched.to_sxp : 'no statement'
    end

    $stdout.puts str
    return !md || md[1] == 'print'
  end
end

Class Method Summary collapse

Class Method Details

.execute(expression, queryable, map, format: 'shexc', **options) ⇒ Hash{RDF::Term => Array<ShapeResult>}

Parse and validate the given ShEx expression string against queriable.

Examples:

executing a ShExC schema

graph = RDF::Graph.load("etc/doap.ttl")
ShEx.execute('etc/doap.shex', graph, "https://rubygems.org/gems/shex", "")

Parameters:

Returns:

  • (Hash{RDF::Term => Array<ShapeResult>})

    Returns ShapeResults, a hash of graph nodes to the results of their associated shapes

Raises:



78
79
80
81
82
83
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex.rb', line 78

def self.execute(expression, queryable, map, format: 'shexc', **options)
  shex = self.parse(expression, format: format, **options)
  queryable = queryable || RDF::Graph.new

  shex.execute(queryable, map, **options)
end

.Extension(uri) ⇒ Class

Alias for ShEx::Extension.create.

Returns:

  • (Class)


108
109
110
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex.rb', line 108

def self.Extension(uri)
  Extension.send(:create, uri)
end

.open(filename, format: 'shexc', **options, &block) ⇒ ShEx::Algebra::Schema

Parses input from the given file name or URL.

Examples:

parsing a ShExC schema

schema = ShEx.parse('foo.shex').parse

Parameters:

  • filename (String, #to_s)
  • expression (IO, StringIO, String, #to_s)

    (ShExC or ShExJ)

  • format ('shexc', 'shexj', 'sxp') (defaults to: 'shexc')

    ('shexc')

  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :prefixes (Hash) — default: Hash.new

    the prefix mappings to use (for acessing intermediate parser productions)

  • :base_uri (#to_s) — default: nil

    the base URI to use when resolving relative URIs (for acessing intermediate parser productions)

  • :anon_base (#to_s) — default: "b0"

    Basis for generating anonymous Nodes

  • :validate (Boolean) — default: false

    whether to validate the parsed statements and values

  • :progress (Boolean)

    Show progress of parser productions

  • :debug (Boolean)

    Detailed debug output

Returns:

Raises:



61
62
63
64
65
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex.rb', line 61

def self.open(filename, format: 'shexc', **options, &block)
  RDF::Util::File.open_file(filename, **options) do |file|
    self.parse(file, format: format, **options)
  end
end

.parse(expression, format: 'shexc', **options) ⇒ ShEx::Algebra::Schema

Parse the given ShEx query string.

Examples:

parsing a ShExC schema

schema = ShEx.parse(%(
  PREFIX ex: <http://schema.example/> ex:IssueShape {ex:state IRI}
).parse

Parameters:

  • expression (IO, StringIO, String, #to_s)

    (ShExC or ShExJ)

  • format ('shexc', 'shexj', 'sxp') (defaults to: 'shexc')

    ('shexc')

  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :prefixes (Hash) — default: Hash.new

    the prefix mappings to use (for acessing intermediate parser productions)

  • :base_uri (#to_s) — default: nil

    the base URI to use when resolving relative URIs (for acessing intermediate parser productions)

  • :anon_base (#to_s) — default: "b0"

    Basis for generating anonymous Nodes

  • :validate (Boolean) — default: false

    whether to validate the parsed statements and values

  • :progress (Boolean)

    Show progress of parser productions

  • :debug (Boolean)

    Detailed debug output

Returns:

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex.rb', line 37

def self.parse(expression, format: 'shexc', **options)
  case format.to_s
  when 'shexc' then Parser.new(expression, **options).parse
  when 'shexj'
    expression = expression.read if expression.respond_to?(:read)
    Algebra.from_shexj(JSON.parse(expression), **options)
  when 'sxp'
    expression = expression.read if expression.respond_to?(:read)
    Algebra.from_sxp(expression, **options)
  else raise "Unknown expression format: #{format.inspect}"
  end
end

.satisfies?(expression, queryable, map, format: 'shexc', **options) ⇒ Boolean

Parse and validate the given ShEx expression string against queriable.

Examples:

executing a ShExC schema

graph = RDF::Graph.load("etc/doap.ttl")
ShEx.execute('etc/doap.shex', graph, "https://rubygems.org/gems/shex", "")

Parameters:

Returns:

  • (Boolean)
  • (Boolean)


96
97
98
99
100
101
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex.rb', line 96

def self.satisfies?(expression, queryable, map, format: 'shexc', **options)
  shex = self.parse(expression, format: format, **options)
  queryable = queryable || RDF::Graph.new

  shex.satisfies?(queryable, map, **options)
end