Module: ShEx

Defined in:
lib/shex.rb,
lib/shex/parser.rb,
lib/shex/algebra.rb,
lib/shex/version.rb,
lib/shex/terminals.rb

Overview

A ShEx runtime for RDF.rb.

Defined Under Namespace

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

Constant Summary collapse

CONTEXT =

Location of the ShEx JSON-LD context

"https://shexspec.github.io/context.jsonld"
EXTENSIONS =

Extensions defined in this gem

%w{test}

Class Method Summary collapse

Class Method Details

.execute(expression, queryable, focus, shape, format: 'shexc', **options) ⇒ Operand

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, "http://rubygems.org/gems/shex", "")

Parameters:

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

    (ShExC or ShExJ)

  • focus (RDF::Term)
  • graph (RDF::Queryable)
  • map (Hash{RDF::Resource => RDF::Resource})
  • shapeExterns (Array<Schema, String>)

    ([]) One or more schemas, or paths to ShEx schema resources used for finding external shapes.

  • options (Hash{Symbol => Object})

Returns:

  • (Operand)

    Returns operand graph annotated with satisfied and unsatisfied operations.

Raises:



74
75
76
77
78
79
# File 'lib/shex.rb', line 74

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

  shex.execute(focus, queryable, {focus => shape}, options)
end

.Extension(uri) ⇒ Class

Alias for ShEx::Extension.create.

Returns:

  • (Class)


104
105
106
# File 'lib/shex.rb', line 104

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:



57
58
59
60
61
# File 'lib/shex.rb', line 57

def self.open(filename, format: 'shexc', **options, &block)
  RDF::Util::File.open_file(filename, options) do |file|
    self.parse(file, options.merge(format: format))
  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:



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/shex.rb', line 33

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)
  when 'sxp'
    expression = expression.read if expression.respond_to?(:read)
    Algebra.from_sxp(JSON.parse expression)
  else raise "Unknown expression format: #{format.inspect}"
  end
end

.satisfies?(expression, queryable, focus, shape, 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, "http://rubygems.org/gems/shex", "")

Parameters:

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

    (ShExC or ShExJ)

  • focus (RDF::Resource)
  • graph (RDF::Queryable)
  • map (Hash{RDF::Resource => RDF::Resource})
  • shapeExterns (Array<Schema, String>)

    ([]) One or more schemas, or paths to ShEx schema resources used for finding external shapes.

  • options (Hash{Symbol => Object})

Returns:

  • (Boolean)
  • (Boolean)


92
93
94
95
96
97
# File 'lib/shex.rb', line 92

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

  shex.satisfies?(focus, queryable, {focus => shape}, options)
end