Convert SHACL model file to code bindings
Project description
Convert SHACL Model to code bindings
This tool can be used to convert a SHACL model into various code bindings
Installation
shacl2code
can be installed using pip:
python3 -m pip install shacl2code
Usage
shacl2code
can generate bindings from either a local file:
shacl2code generate -i model.jsonld python -o out.py
Or from a URL:
shacl2code generate -i https://example.com/rdf/model.jsonld python -o out.py
Or from stdin:
cat model.jsonld | shacl2code generate -i - python -o - > out.py
For more information, run:
shacl2code --help
The available language bindings can be viewed by running:
shacl2code list
Developing
Developing on shacl2code
is best done using a virtual environment. You can
configure one and install shacl2code in editable mode with all necessary
development dependencies by running:
python3 -m venv .venv
. .venv/bin/activate
pip install -e ".[dev]"
Testing
shacl2code
has a test suite written in pytest. To run it, setup a
virtual environment as shown above, then run:
pytest
In addition to the test results, a test coverage report will also be generated using pytest-cov
Custom Annotations
shacl2code
supports a number of custom annotations that can be specified in a
SHACL model to give hints about the generated code. All of these annotations
live in the https://jpewdev.github.io/shacl2code/schema#
namespace, and
commonly are given the sh-to-code
prefix to make it easier to reference them.
For example, in Turtle one would add the prefix mapping:
@prefix sh-to-code: <https://jpewdev.github.io/shacl2code/schema#> .
ID Property Name
The idPropertyName
annotation allows a class to specify what the name of the
"property" that specifies the RDF subject of an object is for serializations
that support it. For example, in JSON-LD, the @id
property indicates the
subject in RDF. If you wanted to alias the @id
property to another name, the
idPropertyName
annotation will let you do this. For example, the following
turtle will use MyId
instead of @id
when writing JSON-LD bindings:
<MyClass> a owl:Class, sh:NodeShape ;
sh-to-code:idPropertyName "MyId"
.
When doing this, the class would then look like this in JSON-LD:
{
"@type": "MyClass",
"MyId": "http://example.com/id"
}
The idProperyName
annotation is inherited by derived classes, so for example
any class that derived from MyClass
would also use MyId
as the subject
property.
Note: This only specifies what the name of the field should be in generated
bindings and has no bearing on how an RDF parser would interpret the property.
In order to still be parsed by RDF, you would also need context file that maps
MyId
to @id
, for example:
{
"@context": {
"MyId": "@id"
}
}
shacl2code
doesn't do this for you, nor does it validate that you have done
it.
Extensible Classes
Most bindings generated from shacl2code
are "closed" in that they do not
allow extra properties to be added to object outside of what is specified in
model. This ensures that field name typos and other unintended properties are
not added to an object. However, in some cases a class may be specifically
intended to be extended such that arbitrary fields can be added to it, which
can be done using the isExtensible
property. This is a boolean property that
indicates if a class can be extended, and defaults to false
. For example, the
following turtle will declare a class as extensible:
<MyClass> a owl:Class, sh:NodeShape ;
sh-to-code:isExtensible true
.
The isExtensible
property is not inherited by derived classes, meaning it
is possible to have a class derived from MyClass
which is itself not
extensible.
The mechanism for dealing with extensible classes will vary between the different bindings, but in general it means that they will not be very picky about object types and properties in any location where an extensible class is allowed.
Note: You may want to be careful about where and how many extensible
classes are allowed in your model. If there are too many and they are allowed
anywhere, it may mean that typos in object types (e.g. @type
in JSON-LD) are
not caught by validation as they will have to be assumed to be a derived class
from an extensible type.
Abstract Classes
By default, classes generated by shacl2code
are all instantiable (i.e. they
can be created). In some instances, it may be desirable to declare a class as
abstract (meaning that it cannot be instantiated, but non-abstract derived
classes can). There are several ways of marking a class as abstract listed
below, in order of preference (with the most preferred being first).
SHACL Validated Abstract Classes
A class can be prevented from being directly instantiated using SHACL by adding a constraint on the shape that it cannot be of its own type. This can be done with the following turtle:
<MyClass> a owl:Class, sh:NodeShape ;
# SHACL to prevent a class from being instansiated as this exact type
sh:not [
sh:class <MyClass>
] .
shacl2code
will detect this pattern and generate abstract bindings for
MyClass
.
This method is most preferred, since it is enforced by SHACL and not just
shacl2code
bindings
shacl2code Annotation
shacl2code
has a custom annotation that can be used to mark a class as
abstract. This can be done with the boolean isAbstract
property. For
example, the following turtle will declare a class as abstract:
<MyClass> a owl:Class, sh:NodeShape ;
sh-to-code:isAbstract true
.
The isAbstract
property is not inherited by derived classes, so any derived
classes are automatically concrete unless they indicate otherwise.
SPDX Abstract Class Parent
It is also possible to define a class as abstract by declaring it to be of
type: http://spdx.invalid./AbstractClass
, but this is not preferred.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file shacl2code-0.0.15.tar.gz
.
File metadata
- Download URL: shacl2code-0.0.15.tar.gz
- Upload date:
- Size: 715.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f3a78550724fa2edc2dba57fc26b023c8380e5e61be2dd0662cacc780f9f6f8 |
|
MD5 | 94a1a8160d87b0b62eb756b5dca54a50 |
|
BLAKE2b-256 | 077aec1616ec85c1bea74bbb30616d067c8234bb4c26ba665c7623ef3191bfa2 |
Provenance
The following attestation bundles were made for shacl2code-0.0.15.tar.gz
:
Publisher:
publish.yaml
on JPEWdev/shacl2code
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
shacl2code-0.0.15.tar.gz
- Subject digest:
9f3a78550724fa2edc2dba57fc26b023c8380e5e61be2dd0662cacc780f9f6f8
- Sigstore transparency entry: 145176645
- Sigstore integration time:
- Predicate type:
File details
Details for the file shacl2code-0.0.15-py3-none-any.whl
.
File metadata
- Download URL: shacl2code-0.0.15-py3-none-any.whl
- Upload date:
- Size: 266.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3aaca95b8c385f91fcd7c17300bec4b5d36bfd20f36a3a22b8aa57d074416e3d |
|
MD5 | 2f3b07834e9cd834343ed1f48911cf3e |
|
BLAKE2b-256 | da3977e6273b257dffa9b59f5d8c4c2a93b25d0345efefa8b2b2bb0e883c65f6 |
Provenance
The following attestation bundles were made for shacl2code-0.0.15-py3-none-any.whl
:
Publisher:
publish.yaml
on JPEWdev/shacl2code
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
shacl2code-0.0.15-py3-none-any.whl
- Subject digest:
3aaca95b8c385f91fcd7c17300bec4b5d36bfd20f36a3a22b8aa57d074416e3d
- Sigstore transparency entry: 145176647
- Sigstore integration time:
- Predicate type: