pySHACL

repository·master·Indexed 43 days ago

https://github.com/rdflib/pyshacl

A pure Python module for validating RDF graphs against SHACL (Shapes Constraint Language) graphs. Version 0.40.1 relies on rdflib for RDF processing and OWL-RL for OWL2 RL profile expansion. It provides a Python API via the validate function, a command-line tool, and an optional OpenAPI 3.0-compatible REST service. Supports advanced SHACL features, SHACL-JS, and Oxigraph store backends.

Tokens
22.2K
Snippets
58
Records
93
Agent score
52%

What's inside pySHACL

  1. Use Focus Node Filtering and SHACL Shape Selection

    master

    PySHACL (v0.27.0+) allows highly targeted validation using two features:

    1. Focus Node Filtering: Pass a list of specific focus nodes to validate only those nodes. The shapes must still target these nodes, but the validator will filter the shapes' targets to match your list.
    2. SHACL Shape Selection: Pass a list of specific SHACL Shapes to use for validation. This is useful for testing specific shapes within a larger graph.
    3. Combined Mode: If you provide both a list of Shapes and a list of focus nodes, the validator operates in a highly-targeted mode, feeding the focus nodes directly into the selected shapes. In this mode, the selected shapes do not need to specify their own focus-targeting mechanisms.
  2. Validate multiple data graphs

    master

    Depending on your needs, you can validate multiple graphs in two ways:

    1. Combine Mode (Default): Validates all graphs as a single combined dataset.
    2. Independent Mode: Validates each graph separately using validate_each.
    # Combine mode (default)
    from pyshacl import validate
    
    data_graphs = ["data1.ttl", "data2.ttl", "data3.ttl"]
    conforms, results_graph, results_text = validate(data_graphs, shacl_graph="shapes.ttl")
    
    # Independent mode
    from pyshacl import validate_each
    
    data_graphs = ["data1.ttl", "data2.ttl", "data3.ttl"]
    results = validate_each(data_graphs, shacl_graph="shapes.ttl")
    for graph_id, (conforms, results_graph, results_text) in results.items():
        print(graph_id, conforms)
  3. Install PySHACL with SHACL-JS support

    master

    SHACL-JS features (Javascript-based constraints, functions, rules, and targets) are implemented as a Python "extras" feature. To use these advanced features, you must install PySHACL using PIP with the js extra included.

    pip install pyshacl[js]
  4. Check pySHACL compatibility requirements

    master

    pySHACL is a Python 3 library. Ensure your environment meets the following requirements:

    • Python Version: Python v3.8 or greater is recommended. Python 3.7 and below are not supported. Python 2.7.x and below are not supported.
    • pip Version: If installing from PyPI via pip, use pip v20.0.2 or above for best compatibility. On older systems like Ubuntu 18.04, you may need to upgrade pip manually.
    # Upgrade pip if on an older system (e.g., Ubuntu 18.04)
    sudo pip3 install --upgrade pip
  5. Run the pySHACL HTTP REST Service

    master

    PySHACL includes an OpenAPI 3.0-compatible REST service. This is an optional feature requiring the http extra.

    Installation:

    $ pip3 install -U pyshacl[http]

    Starting the server:

    $ pyshacl --server
    # or
    $ pyshacl_server
    # or
    $ python3 -m pyshacl server
    # or via Docker:
    $ docker run --rm -e PYSHACL_SERVER=TRUE -i -t docker.io/ashleysommer/pyshacl:latest

    Default Access:

    • Base URL: http://127.0.0.1:8099
    • SwaggerUI: http://127.0.0.1:8099/docs/swagger
    • ReDoc: http://127.0.0.1:8099/docs/redoc
    • OpenAPI Schema: http://127.0.0.1:8099/docs/openapi.json

    Configuration via Environment Variables:

    • PYSHACL_SERVER=TRUE: Force HTTP Server mode (useful for containers).
    • PYSHACL_SERVER_LISTEN: Set listen IP/hostname (e.g., 1.2.3.4).
    • PYSHACL_SERVER_PORT: Set TCP port (e.g., 8080).
    • PYSHACL_SERVER_HOSTNAME: Set the externally facing hostname (e.g., example.org).
    $ pip3 install -U pyshacl[http]
    $ pyshacl --server
  6. Run pySHACL using Docker

    master

    You can run pySHACL inside a container by pulling the official image from Dockerhub or building it from the repository. When running the container, you must mount the local directory containing your data to a destination inside the container (e.g., /data) to access your RDF files.

    To pull the image: docker pull docker.io/ashleysommer/pyshacl:latest

    To build the image locally: docker build . -t pyshacl

    # Pull the official image
    docker pull docker.io/ashleysommer/pyshacl:latest
    
    # Build the image locally
    docker build . -t pyshacl
    
    # Run validation by mounting the current directory to /data
    docker run --rm -i -t --mount type=bind,src=`pwd`,dst=/data pyshacl -s /data/shacl.ttl /data/graph.ttl
  7. Install pySHACL

    master

    Install pySHACL using pip3. It is recommended to use a Python virtual environment.

    To install the base package:

    pip3 install pyshacl

    To enable Oxigraph compatibility (which allows pySHACL to run validation and SHACL Rules against an Oxigraph store backend), install the oxigraph extra:

    pip3 install pyshacl[oxigraph]
  8. How SHACL target types are discovered and applied

    master

    In pySHACL, target types (which define how to select nodes for validation) are discovered by scanning the ShapesGraph for subjects that are subclasses of sh:Target.

    To use custom or built-in target types, you can use gather_target_types to find all valid SHACLTargetType or SPARQLTargetType instances in your shapes graph, and then apply_target_types to register them.

    Note: If sh:JSTargetType is found but JavaScript mode is not enabled in the ShapesGraph, those targets are silently ignored. Currently, SPARQLTargetType is the primary implemented target type.

    from pyshacl.target import gather_target_types, apply_target_types
    
    # Assuming shacl_graph is your ShapesGraph instance
    target_types = gather_target_types(shacl_graph)
    apply_target_types(target_types)
  9. Configure SPARQL Remote Graph Mode

    master

    When sparql_mode=True is passed to validate(), pySHACL treats the data_graph string as a SPARQL endpoint URL. It will query the endpoint to retrieve the data for validation.

    Authentication can be configured via environment variables:

    • PYSHACL_SPARQL_USERNAME: Username for SPARQL endpoint authentication.
    • PYSHACL_SPARQL_PASSWORD: Password for SPARQL endpoint authentication.
    • PYSHACL_SPARQL_METHOD: HTTP method to use (e.g., GET or POST). Defaults to GET.

    Limitations of SPARQL Mode:

    • Cannot use shacl-js.
    • Cannot use inplace=True.
    • Cannot use ont_graph (extra ontology inoculation).
    • Does not support multiple data graphs.
  10. How DataGraph handles different backend implementations

    master

    PySHACL uses DataGraph to abstract away the differences between rdflib and pyoxigraph.

    • RdfLib backend: Used when the provided store is an rdflib_Store. It supports locked_context (allowing operations to be scoped to a specific graph within a dataset) and provides standard RDFLib functionality.
    • Oxigraph backend: Used when pyoxigraph is installed and an ox_Store is provided. It is optimized for performance but has specific constraints: it does not support Literals or Blank Nodes in the predicate position, and it is not formula-aware (the quoted argument in add is not supported).
  11. How ShapesGraph manages shapes and constraints

    master

    The ShapesGraph acts as a specialized wrapper around an rdflib graph that automates the discovery and caching of SHACL shapes.

    Shape Discovery

    Shapes are identified in several ways:

    1. Explicitly: Nodes defined with rdf:type sh:NodeShape or rdf:type sh:PropertyShape.
    2. Implicitly via Targets: Nodes targeted by sh:targetClass, sh:targetNode, sh:targetObjectsOf, or sh:targetSubjectsOf.
    3. Implicitly via Constraints: Nodes that are subjects of sh:property or sh:node, or values of shape-expecting parameters like sh:not or sh:qualifiedValueShape.
    4. Implicitly via Logical Constraints: Members of SHACL lists used in sh:and, sh:or, or sh:xone.

    Custom Constraints

    The custom_constraints property automatically discovers and instantiates CustomConstraintComponentFactory objects for any component in the graph that is a subclass of sh:ConstraintComponent but is not a built-in PySHACL component (those starting with the sh: prefix).

  12. How SHACL-AF (Advanced Features) works

    master

    When options['advanced'] is set to True, the Validator activates SHACL-AF features, including SHACL Rules and SHACL Functions.

    1. Target Types: The validator gathers target types from the shacl_graph and applies them.
    2. Functions & Rules: It gathers functions and rules defined in the shapes graph.
    3. Execution: During the .run() process, functions are applied to the graph, and rules are executed (if not in sparql_mode).
    4. Cleanup: After validation, any applied functions are unapplied to restore the graph state (if not in inplace mode).