Module: ShEx

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

Overview

A ShEx runtime for RDF.rb.

Defined Under Namespace

Modules: Algebra, Meta, Terminals Classes: Error, NotSatisfied, ParseError, Parser, StructureError

Class Method Summary collapse

Class Method Details

.execute(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)
  • shape (RDF::Resource)
  • format ('shexc', 'shexj', 'sse') (defaults to: 'shexc')

    (‘shexc’)

  • options (Hash{Symbol => Object})

Returns:

  • (Boolean)

    ‘true` if satisfied, `false` if it does not apply

Raises:



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

def self.execute(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

.open(filename, format: 'shexc', **options) {|ShEx::Algebra::Schema| ... } ⇒ 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)
  • format ('shexc', 'shexj', 'sse') (defaults to: 'shexc')

    (‘shexc’)

  • options (Hash{Symbol => Object})

    any additional options (see ‘RDF::Reader#initialize` and `RDF::Format.for`)

Yields:

Yield Parameters:

  • reader (RDF::Reader)

Yield Returns:

  • (void)

    ignored

Returns:

Raises:



53
54
55
56
57
# File 'lib/shex.rb', line 53

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', 'sse') (defaults to: 'shexc')

    (‘shexc’)

  • options (Hash{Symbol => Object})

Returns:

Raises:



28
29
30
31
32
33
34
35
# File 'lib/shex.rb', line 28

def self.parse(expression, format: 'shexc', **options)
  case format
  when 'shexc' then Parser.new(expression, options).parse
  when 'shexj'
  when 'sse'
  else raise "Unknown expression format: #{format.inspect}"
  end
end