SPARQL#

This generator allows you to generate a bank of sparql queries from a schema. These queries can then be used for validation.

Note this approach is redundant with more complete RDF shape based strategies – however, it can be convenient to generate individual SPARQL queries as these can be applied on an ad-hoc basis.

Overview#

To run:

gen-sparql -d /path/to/sparql-queries/ personinfo.yaml

See also

Data Validation for other validation strategies.

See also

linkml-sparql for an alpha version of a SPARQL based ORM

For example, when running over the personinfo schema, one of the queries “CHECK_permitted_Person.rq” checks to see that only specifically permitted properties are used when the domain is Person.

SELECT ?g ?s ?p WHERE {
 GRAPH ?g {
  ?s rdf:type schema:Person ;
     ?p ?o .
  FILTER ( ?p NOT IN (
   schema:email,
   schema:birthDate,
   personinfo:age_in_years,
   schema:gender,
   personinfo:current_address,
   personinfo:has_employment_history,
   personinfo:has_familial_relationships,
   personinfo:has_medical_history,
   personinfo:aliases,
   schema:identifier,
   schema:name,
   schema:description,
   schema:image,
   rdf:type ))
 }

}

Docs#

Command Line#

gen-sparql#

Generate SPARQL queries for validation

This will generate a directory of queries that can be used for QC over a triplestore that is conformant to the same LinkML schema.

Each query in the directory will be of the form

CHECK_<ConstraintType>_<SchemaElement>.rq

Example:

gen-sparql -d ./sparql/ personinfo.yaml

gen-sparql [OPTIONS] YAMLFILE

Options

-d, --dir <dir>#

Directory in which queries will be deposited

-V, --version#

Show the version and exit.

-f, --format <format>#

Output format

Default:

sparql

Options:

sparql

--metadata, --no-metadata#

Include metadata in output

Default:

True

--useuris, --metauris#

Use class and slot URIs over model uris

Default:

True

-im, --importmap <importmap>#

Import mapping file

--log_level <log_level>#

Logging level

Default:

WARNING

Options:

CRITICAL | ERROR | WARNING | INFO | DEBUG

-v, --verbose#

Verbosity. Takes precedence over –log_level.

--mergeimports, --no-mergeimports#

Merge imports into source file (default=mergeimports)

--stacktrace, --no-stacktrace#

Print a stack trace when an error occurs

Default:

False

Arguments

YAMLFILE#

Required argument

Code#

class linkml.generators.sparqlgen.SparqlGenerator(schema: str | ~typing.TextIO | ~linkml_runtime.linkml_model.meta.SchemaDefinition | ~linkml.utils.generator.Generator | ~pathlib.Path, schemaview: ~linkml_runtime.utils.schemaview.SchemaView | None = None, format: str | None = None, metadata: bool = True, useuris: bool | None = None, log_level: int | None = 30, mergeimports: bool | None = True, source_file_date: str | None = None, source_file_size: int | None = None, logger: ~logging.Logger | None = None, verbose: bool | None = None, output: str | None = None, namespaces: ~linkml_runtime.utils.namespaces.Namespaces | None = None, directory_output: bool = False, base_dir: str | None = None, metamodel_name_map: ~typing.Dict[str, str] | None = None, importmap: str | ~typing.Mapping[str, str] | None = None, emit_prefixes: ~typing.Set[str] = <factory>, metamodel: ~linkml.utils.schemaloader.SchemaLoader | None = None, stacktrace: bool = False, named_graphs: ~typing.List[str] | None = None, limit: int | None = None, sparql: str | None = None, **_kwargs)[source]#

Generates SPARQL queries that can be used for delayed validation

serialize(directory=None) str[source]#

Generate output in the required format

Parameters:

kwargs – Generator specific parameters

Returns:

Generated output