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 cann 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#
Include metadata in output
- 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
- --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: ~typing.Union[str, ~typing.TextIO, ~linkml_runtime.linkml_model.meta.SchemaDefinition, ~linkml.utils.generator.Generator], schemaview: ~typing.Optional[~linkml_runtime.utils.schemaview.SchemaView] = None, format: ~typing.Optional[str] = None, metadata: bool = <factory>, useuris: ~typing.Optional[bool] = None, log_level: int = 30, mergeimports: ~typing.Optional[bool] = <factory>, source_file_date: ~typing.Optional[str] = None, source_file_size: ~typing.Optional[int] = None, logger: ~typing.Optional[~logging.Logger] = None, verbose: ~typing.Optional[bool] = None, output: ~typing.Optional[str] = None, namespaces: ~typing.Optional[~linkml_runtime.utils.namespaces.Namespaces] = None, directory_output: bool = False, base_dir: ~typing.Optional[str] = None, metamodel_name_map: ~typing.Optional[~typing.Dict[str, str]] = None, importmap: ~typing.Optional[~typing.Union[str, ~typing.Mapping[str, str]]] = None, emit_prefixes: ~typing.Set[str] = <factory>, metamodel: ~typing.Optional[~linkml.utils.schemaloader.SchemaLoader] = None, stacktrace: bool = False, named_graphs: ~typing.Optional[~typing.List[str]] = None, limit: ~typing.Optional[int] = None, sparql: ~typing.Optional[str] = None, **_kwargs)[source]#
Generates SPARQL queries that can be used for delayed validation