Skip to content

Classes:

Functions:

ConstraintViolation

ConstraintViolation(domain, message)

Bases: Exception

Exception for domain-specific constraint violation

Attributes:

  • domain (str) –

    the violation's domain

Source code in src/rdf_utils/constraints.py
14
15
16
def __init__(self, domain: str, message: str):
    super().__init__(f"{domain} constraint violated: {message}")
    self.domain = domain

SHACLViolation

SHACLViolation(violation_str)

Bases: ConstraintViolation

Specialized exception for SHACL violations

Source code in src/rdf_utils/constraints.py
21
22
def __init__(self, violation_str: str):
    super().__init__("SHACL", violation_str)

check_shacl_constraints

check_shacl_constraints(graph, shacl_dict, quiet=False)

Check a graph against a collection of SHACL constraints

Parameters:

  • graph (Graph) –

    rdflib.Graph to be checked

  • shacl_dict (dict[str, str]) –

    mapping from SHACL path to graph format, e.g. URL -> "turtle"

  • quiet (bool, default: False ) –

    if true will not throw an exception

Source code in src/rdf_utils/constraints.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def check_shacl_constraints(graph: Graph, shacl_dict: dict[str, str], quiet:bool = False) -> bool:
    """Check a graph against a collection of SHACL constraints

    Parameters:
        graph: rdflib.Graph to be checked
        shacl_dict: mapping from SHACL path to graph format, e.g. URL -> "turtle"
        quiet: if true will not throw an exception
    """
    shacl_g = Dataset()
    for mm_url, fmt in shacl_dict.items():
        shacl_g.parse(mm_url, format=fmt)

    conforms, _, report_text = pyshacl.validate(graph, shacl_graph=shacl_g, inference="rdfs")

    if not conforms and not quiet:
        raise SHACLViolation(report_text)

    return conforms