pySHACL | A Python validator for SHACL | Validation library
kandi X-RAY | pySHACL Summary
kandi X-RAY | pySHACL Summary
A Python validator for SHACL. This is a pure Python module which allows for the validation of RDF graphs against Shapes Constraint Language (SHACL) graphs. This module uses the rdflib Python library for working with RDF and is dependent on the OWL-RL Python module for OWL2 RL Profile based expansion of data graphs.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Load data from source
- Get a graph from the web
- Clone a source graph
- Clone a dataset
- Validate the current graph
- Focus the target nodes
- Find custom constraints
- Path of the object
- Validate a graph
- Evaluate the target graph
- Evaluate the constraints
- Evaluate the constraint on the target graph
- Validate against the given values
- Evaluate the graph
- Evaluate the context on the target graph
- Evaluate a string rule
- Applies the filter to the given graph
- Execute the rule
- Validate the constraint on the target graph
- Applies the rule
- Make a validator for a shape
- Validate function
- Return the order of the SHACL shape
- Execute an expression from a SPARQL expression
- Binds parameters to the constraint
- Evaluate the objective function
pySHACL Key Features
pySHACL Examples and Code Snippets
ttl = """
PREFIX sh:
sh:pattern "^\s|\d{VALUE}\D" .
"""
g = Graph()
g.add((
URIRef("a:"),
URIRef("http://www.w3.org/ns/shacl#pattern"),
Literal("^\s|\d{VALUE}\D")
))
g2 = Graph().parse(da
hr:EmployeeShape
a sh:NodeShape ;
sh:targetClass hr:Employee ;
sh:property hr:nameShape ;
sh:property hr:jobGradeShape .
Community Discussions
Trending Discussions on pySHACL
QUESTION
I have a NodeShape with a sh:SPARQLTarget . I tried to run the Target SPARQL query in an ontology editor and it delivered results, but when I'm executing the same query in my custom target node shape in sh:select, it won't validate the target nodes returned by the SPARQL query. I am using pySHACL. Did I do something wrong? I'm out of ideas. Here is my Nodeshape and data graph:
I have used “” for sh:select instead of “”” “””, since I am defining the shapes_graph as a variable in my python code and it is already encoded in """ """. I have also enabled meta_shacl=True in pyShacl to ensure that my shapes_graph is valid. Also the nodeShape (snomed:dob363698007Shape) works well when provided with a normal TargetClass or TargetNode. What am I missing?
I have already referred SPARQLRule not constructing
...ANSWER
Answered 2022-Mar-12 at 10:21I've put your shacl shapes file and data graph into PySHACL to isolate the issue you are seeing.
There are two problems with your given setup that I have found.
Firstly, SPARQL-Based Targets is a feature from the SHACL Advanced Specification. PySHACL does not enable the Advanced-Spec features by default. You can enable "advanced mode" by passing advanced=True
to the validation module, or -a
or --advanced
on the commandline tool.
That is the main reason your SPARQL target was not selecting the nodes you expected.
Next, after enabling advanced mode, you will see that PySHACL fails when loading your SHACL Shape Graph. That is because your prefix namespace is not declared correctly.
See the examples in the SPARQL-Prefixes section of the Spec document. The specification states
"The values of sh:namespace are literals of datatype xsd:anyURI."
Your sh:namespace
is a URIRef, not a Literal. Changing the namespace declaration to the following, fixes the error.
QUESTION
I want to validate the existing rdfs:domain and rdfs:range statements of an existing ontology and knowledge base with SHACL. However it seems like it is extremely verbose to do that with SHACL.
Existing Definition ...ANSWER
Answered 2022-Jan-22 at 02:23Let me start by saying that rdfs:domain and rdfs:range are not constraints and don't mean what you imply. They are merely producing inferences. Having said this, many people have in the past used them to "mean" constraints simply because there was no other modeling language. For background, see
https://www.topquadrant.com/owl-blog/
If you don't want to duplicate the RDFS triples as individual SHACL constraints, you can write a single generic SHACL shape that has sh:targetSubjectsOf rdfs:domain (and range) and then uses a SPARQL constraint to check that there is no instance of a class other than the domain class that has values for the given property. The end result would be that all rdfs:domain statements would be checked at once.
But arguably RDFS should be left alone. If you want to use closed-world semantics you should use a language that has been designed for that purpose, i.e. SHACL.
(BTW there is a Discord group in case you want a higher bandwidth to discuss such things https://twitter.com/HolgerKnublauch/status/1461590465304662019)
QUESTION
I assumed that not having any datatype is implicitly the same as having xsd:string
as datatype, however SHACL gives me a validation error. Is my assumption wrong or is that just not covered by SHACL? In the latter case, how do I validate that the datatype is either empty or xsd:string?
ANSWER
Answered 2022-Jan-12 at 00:05In your example the data contains an rdf:langString literal, which is not the same datatype as xsd:string - you see the @en language tag. You probably refer to the fact that the following would be equivalent: "Hello World" and "Hello World"^^xsd:string. So the pyshacl result above looks correct to me.
QUESTION
I have the following Data Graph.
...ANSWER
Answered 2020-Apr-22 at 18:09It is important to understand how property paths work. A path is used to reach values. When using sh:path [sh:zeroOrMorePath rdf:path]
and considering the node hr:Longer
, it will reach three values -- (0) hr:Longer
, (1) hr:Employee
, and (2) rdfs:Class
.
With this concept firmly in mind, what is going on in (B) and why it does not work can be fully explained.
Both (A) and (B) have the same target definition and will return the same focus nodes. These are:
QUESTION
I have the following Data & Shape Graph.
...ANSWER
Answered 2020-Apr-16 at 16:33What follows results in the expected validation errors, however, there are still several things I do not understand.
The
sh:prefixes hr: ;
is not needed. It is designed to supply prefixes for the SPARQL target SELECT statement itself and nothing more.Inference needed to be disabled. It was inserting triples and trying to validate them. In this use case, that is not what is desired. What should be validated is what is in the schema and nothing else.
I was also thinking that it would not be an issue to put everything into a single graph based on what apparently was a misunderstanding of https://github.com/RDFLib/pySHACL/issues/46.
QUESTION
I have a gist with all of the relevant files at: https://gist.github.com/James-Hudson3010/2588d9b17dd33e15922122b8b5cf1bd7
If I execute:
...ANSWER
Answered 2020-Apr-01 at 12:59When you use the individual files, pySHACL has no way of knowing what to associate your Shape file's hr:Employee
NodeShape with. It seems to know when it's in that single file (perhaps it runs against all classes in the file??).
So:
- rename the Employee shape to not overload the
hr:Employee
class name:hr:EmployeeShape
- add back in the
sh:targetClass
directive:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install pySHACL
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page