SHACL

Warning

Beta implementation, some features may change

Example Output

personinfo.shacl.ttl

Overview

SHACL (Shapes Constraint Language) is a language for validating RDF graphs against a set of conditions

To run:

gen-shacl personinfo.yaml > personinfo.shacl.ttl

Docs

Example Input:

NamedThing:
  slots:
    - id
    - name

HasAliases:
  mixin: true
  attributes:
    aliases:
      multivalued: true

Person:
  is_a: NamedThing
  mixins:
    - HasAliases
  slots:
    - birth_date
    - age_in_years
    - gender

Example Output:

<https://w3id.org/linkml/tests/kitchen_sink/Person> a shacl:NodeShape ;
    shacl:closed true ;
    shacl:ignoredProperties ( rdf:type ) ;
    shacl:property [ shacl:class <https://w3id.org/linkml/tests/kitchen_sink/BirthEvent> ;
            shacl:maxCount 1 ;
            shacl:nodeKind shacl:BlankNode ;
            shacl:path <https://w3id.org/linkml/tests/kitchen_sink/has_birth_event> ],
        [ shacl:maxCount 1 ;
            shacl:maxInclusive 999 ;
            shacl:minInclusive 0 ;
            shacl:path <https://w3id.org/linkml/tests/kitchen_sink/age_in_years> ],
        [ shacl:class <https://w3id.org/linkml/tests/kitchen_sink/FamilialRelationship> ;
            shacl:nodeKind shacl:BlankNode ;
            shacl:path <https://w3id.org/linkml/tests/kitchen_sink/has_familial_relationships> ],
        [ shacl:maxCount 1 ;
            shacl:path <https://w3id.org/linkml/tests/core/name> ;
            shacl:pattern "^\\S+ \\S+" ],
        [ shacl:class <https://w3id.org/linkml/tests/kitchen_sink/MedicalEvent> ;
            shacl:nodeKind shacl:BlankNode ;
            shacl:path <https://w3id.org/linkml/tests/kitchen_sink/has_medical_history> ],
        [ shacl:class <https://w3id.org/linkml/tests/kitchen_sink/Address> ;
            shacl:nodeKind shacl:BlankNode ;
            shacl:path <https://w3id.org/linkml/tests/kitchen_sink/addresses> ],
        [ shacl:maxCount 1 ;
            shacl:path <https://w3id.org/linkml/tests/core/id> ],
        [ shacl:path <https://w3id.org/linkml/tests/kitchen_sink/aliases> ],
        [ shacl:class <https://w3id.org/linkml/tests/kitchen_sink/EmploymentEvent> ;
            shacl:nodeKind shacl:BlankNode ;
            shacl:path <https://w3id.org/linkml/tests/kitchen_sink/has_employment_history> ] ;
    shacl:targetClass <https://w3id.org/linkml/tests/kitchen_sink/Person> .

Command Line

gen-shacl

Generate SHACL turtle from a LinkML model

gen-shacl [OPTIONS] YAMLFILE

Options

--closed, --non-closed

Use ‘–closed’ to generate closed SHACL shapes. Use ‘–non-closed’ to generate open SHACL shapes.

Default:

True

-s, --suffix <suffix>

Use –suffix to append given string to SHACL class name (e. g. –suffix Shape: Person becomes PersonShape).

--include-annotations, --exclude-annotations

Use –include-annotations to include annotations of slots, types, and classes in the generated SHACL shapes.

Default:

False

--exclude-imports, --include-imports

Use –exclude-imports to exclude imported elements from the generated SHACL shapes. This is useful when extending a substantial ontology to avoid large output files.

Default:

False

--use-class-uri-names, --use-native-names

If –use-class-uri-names (default), SHACL shape names are based on class_uri. If –use-native-names, SHACL shape names are based on LinkML class names from the schema file. Suffixes from the –suffix option can still be appended.

Default:

True

--expand-subproperty-of, --no-expand-subproperty-of

If –expand-subproperty-of (default), slots with subproperty_of will generate sh:in constraints containing all slot descendants. Use –no-expand-subproperty-of to disable this behavior.

Default:

True

-V, --version

Show the version and exit.

-f, --format <format>

Output format

Default:

'ttl'

Options:

ttl

--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.shaclgen.ShaclGenerator(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, metamodel_name_map: dict[str, str] = None, importmap: str | ~collections.abc.Mapping[str, str] | None = None, emit_prefixes: set[str] = <factory>, metamodel: ~linkml.utils.schemaloader.SchemaLoader = None, stacktrace: bool = False, include: str | ~pathlib.Path | ~linkml_runtime.linkml_model.meta.SchemaDefinition | None = None, closed: bool = True, suffix: str = None, include_annotations: bool = False, exclude_imports: bool = False, use_class_uri_names: bool = True, expand_subproperty_of: bool = True)[source]

Generate SHACL (Shapes Constraint Language) shapes from a LinkML schema.

SHACL shapes are used to validate RDF data. Each LinkML class is converted to a sh:NodeShape with property constraints derived from the class’s slots.

Shape Naming Modes

The generator supports two naming modes controlled by use_class_uri_names:

Default mode (use_class_uri_names=True):

Shapes are named using the class_uri. If multiple LinkML classes share the same class_uri, their properties are merged into a single shape. This is the traditional RDF-centric behavior.

Example: LinkML classes Entity and EvaluatedEntity both with class_uri prov:Entity produce a single shape <prov:Entity>.

Native names mode (use_class_uri_names=False):

Shapes are named using the native LinkML class name from the schema. Each LinkML class produces a distinct shape, even if they share a class_uri. The sh:targetClass still correctly points to the class_uri.

Example: The same two classes produce two shapes, each with sh:targetClass prov:Entity.

Use native names mode when multiple LinkML classes intentionally map to the same external ontology class and you need distinct validation shapes per class. See #3011 for background.

serialize(**args) str[source]

Generate output in the required format

Parameters:

kwargs – Generator specific parameters

Returns:

Generated output