Module: ShEx

Defined in:
lib/shex.rb,
lib/shex/format.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, Format, NotMatched, NotSatisfied, ParseError, Parser, StructureError, Test

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}

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

Parameters:

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

    (ShExC or ShExJ)

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

    A set of (‘term`, `resource`) pairs where `term` is a node within `graph`, and `resource` identifies a shape

  • *focus (Array<RDF::Term>)

    One or more nodes within ‘graph` for which to run the start expression.

  • shapeExterns (Array<Schema, String>)

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

  • options (Hash{Symbol => Object})

Returns:

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

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

Raises:



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

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

  shex.execute(queryable, map, options)
end

.Extension(uri) ⇒ Class

Alias for ‘ShEx::Extension.create`.

Returns:

  • (Class)


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

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:



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

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:



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

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

Parameters:

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

    (ShExC or ShExJ)

  • options (Hash{Symbol => Object})
  • graph (RDF::Queryable)
  • map (Hash{RDF::Term => <RDF::Resource>}, Array<Array(RDF::Term, RDF::Resource)>)

    A set of (‘term`, `resource`) pairs where `term` is a node within `graph`, and `resource` identifies a shape

  • *focus (Array<RDF::Term>)

    One or more nodes within ‘graph` for which to run the start expression.

  • 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)


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

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

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