Skip to main content
Version: latest

SHACL export

to_shacl(schema) writes SHACL shapes as Turtle text for one schema. Use it to check extraction results in a graph tool that reads SHACL, or to share the schema with a triple store or validation pipeline that speaks Turtle.

from agrag.schema import load_schema
from agrag.schema.formats import to_shacl

schema = load_schema(["Person", "Company", "Person-WORKS_FOR->Company"])
print(to_shacl(schema))

The result is a string, not a dict. Each entity type becomes a sh:NodeShape with a sh:targetClass, and each declared property becomes a sh:property shape on that node shape.

Mapping​

Schema elementSHACL
Entity typeA sh:NodeShape with sh:targetClass naming the type
Entity descriptionrdfs:comment on the node shape
Property type string, integer, float, booleansh:datatype of xsd:string, xsd:integer, xsd:double, xsd:boolean
Property type datesh:datatype of xsd:date
Property requiredsh:minCount 1 on the property shape
Property enumsh:in with the allowed values
Property descriptionrdfs:comment on the property shape
Entity parentsrdfs:subClassOf pointing at each parent class
Relation typeAn rdf:Property plus a sh:property shape on each endpoint's node shape naming the allowed target class
Relation descriptionrdfs:comment on the relation property
Relation patternsx-agrag:pattern annotations with the declared pairs; endpoint shapes cover every pair GraphSchema.allows accepts

Relation limits appear twice: as sh:maxCount on the endpoint shapes, which validators enforce, and as x-agrag: annotations for pipelines. Patterns and symmetry appear only as annotations that validators ignore.

Schema elementAnnotation
Relation max_per_sourcex-agrag:maxPerSource
Relation max_per_targetx-agrag:maxPerTarget
Relation symmetricx-agrag:symmetric

What the export does not carry​

SHACL checks one graph. Cardinality limits are enforced per node through sh:maxCount on the endpoint shapes, with the same values repeated as annotations for pipelines. A symmetric relation is written once; the reverse direction is an annotation plus expanded endpoint shapes, not a second property. Endpoint shapes already include descendants and symmetric reversals, so validators should target exact types rather than re-applying parent shapes through subclass reasoning.

This page covers export only. You cannot load a schema from SHACL.