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
- -f, --format <format>
Output format (default=sparql)
- Options
sparql
- --metadata, --no-metadata
Include metadata in output (default=–metadata)
- --useuris, --metauris
Include metadata in output (default=–useuris)
- -im, --importmap <importmap>
Import mapping file
- --log_level <log_level>
Logging level (default=WARNING)
- Options
CRITICAL | ERROR | WARNING | INFO | DEBUG
- --mergeimports, --no-mergeimports
Merge imports into source file (default=mergeimports)
Arguments
- YAMLFILE
Required argument
Code
- class linkml.generators.sparqlgen.SparqlGenerator(schema: Union[str, TextIO, linkml_runtime.linkml_model.meta.SchemaDefinition], format: str = 'sparql', named_graphs: Optional[List[str]] = None, limit: Optional[int] = None, **kwargs)[source]
Generates SPARQL queries that can be used for delayed validation