
    01i                     v   S r SSKJr  SSKJrJrJr  SSKJrJ	r	J
r
JrJr  SSKJr  SSKJrJr  SSKJr  SSKJr  \(       a  SS	KJr   " S
 S\5      r\R2                  \R4                  \R6                  \R8                  \R:                  \R<                  0r      SS jr       SS jr! S     SS jjr"g)zA
Utilities for interacting with SHACL Shapes Graphs more easily.
    )annotations)TYPE_CHECKINGOptionalUnion)BNodeGraphLiteralURIRefpaths)
Collection)RDFSH)Path)Node)IdentifiedNodec                      \ rS rSrSrg)SHACLPathError    N)__name__
__module____qualname____firstlineno____static_attributes__r       M/home/james-whalen/.local/lib/python3.13/site-packages/rdflib/extras/shacl.pyr   r      s    r   r   c                  ^  Sn[        U[        5      (       a  [        S5      e[        U[        5      (       a#  U[        R
                  :X  a  [        S5      eUnU$ T R                  U[        R                  5      bS  [        T R                  U5      5      n[        U5      S:  a  [        S5      e[        R                  " U 4S jU 5       6 nU$ T R                  U[        R                  5      =n(       a"  [        R                   " [#        T U5      5      nU$ T R                  U[        R$                  5      =n(       aS  [        T R                  U5      5      n[        U5      S:  a  [        S5      e[        R&                  " U 4S jU 5       6 nU$ T R                  U[        R(                  5      =n(       a#  [        R*                  " [#        T U5      S	5      nU$ T R                  U[        R,                  5      =n(       a#  [        R*                  " [#        T U5      S
5      nU$ T R                  U[        R.                  5      =n	(       a#  [        R*                  " [#        T U	5      S5      nU$ Uc  [        S[1        U5       S35      eU$ )a  
Parse a valid SHACL path (e.g. the object of a triple with predicate sh:path)
from a [`Graph`][rdflib.graph.Graph] as a [`URIRef`][rdflib.term.URIRef] if the path
is simply a predicate or a [`Path`][rdflib.paths.Path] otherwise.

Args:
    shapes_graph: A [`Graph`][rdflib.graph.Graph] containing the path to be parsed
    path_identifier: A [`Node`][rdflib.term.Node] of the path

Returns:
    A [`URIRef`][rdflib.term.URIRef] or a [`Path`][rdflib.paths.Path]
Nz$Literals are not a valid SHACL path.z;A list of SHACL Paths must contain at least two path items.   DA list of SHACL Sequence Paths must contain at least two path items.c              3  <   >#    U  H  n[        TU5      v   M     g 7fNparse_shacl_path).0pathshapes_graphs     r   	<genexpr>#parse_shacl_path.<locals>.<genexpr>G   s     Hxt|T22x   @List of SHACL alternate paths must have at least two path items.c              3  <   >#    U  H  n[        TU5      v   M     g 7fr!   r"   )r$   alternativer&   s     r   r'   r(   V   s"      #/K !{;;#/r)   *+?zCannot parse z as a SHACL Path.)
isinstancer	   	TypeErrorr
   r   nilr   valuefirstlistitemslenr   SequencePathr   inversePathInvPathr#   alternativePathAlternativePathzeroOrMorePathMulPathoneOrMorePathzeroOrOnePathrepr)
r&   path_identifierr%   sequenceinverse_pathalternative_pathalternativeszero_or_more_pathone_or_more_pathzero_or_one_paths
   `         r   r#   r#   !   sn     +/D /7++>?? 
OV	,	,cgg% M  ^ KY 
		OSYY	7	C**?;<x=1 V  !!HxH
L KC &++OR^^L	L	L}}-lLIJ@ K; *//ASAST	T		TL../?@A|q  R  $$#/
. K +00"BSBST	T		T}}-l<MNPST K *//AQAQR	R		R}}-l<LMsS K *//AQAQR	R		R}}-l<LMsS K 
}T/-B,CCTUVVKr   c                   [        U[        [        45      (       d  [        S[	        U5       S3S-   5      e[        U[        5      (       a  U$ [        5       n[        U[        R                  5      (       aW  [        UR                  5      S:  a  [        S5      e[        U UUR                   Vs/ s H  n[        X5      PM     sn5        U$ [        U[        R                  5      (       a7  U R                  U[        R                   [        XR"                  5      45        U$ [        U[        R$                  5      (       a  [        UR                  5      S:  a  [        S5      e[        U [        5       UR                   Vs/ s H  n[        X5      PM     sn5      nU R                  U[        R&                  UR(                  45        U$ [        U[        R*                  5      (       a`  [,        R/                  UR0                  5      nUc  [        SUR0                   35      eU R                  X%[        XR2                  5      45        U$ s  snf s  snf )a  
Helper method that implements the recursive component of SHACL path
triple construction.

Args:
    graph: A [`Graph`][rdflib.graph.Graph] into which to insert triples
    graph_component: A [`URIRef`][rdflib.term.URIRef] or
        [`Path`][rdflib.paths.Path] that is part of a path expression

Returns:
    The [`IdentifiedNode`][rdflib.term.IdentifiedNode] of the resource in the
        graph that corresponds to the provided path_component
zObjects of type z are not valid zcomponents of a SHACL path.r   r   r*   zUnknown path modifier )r0   r
   r   r1   typer   r   r8   r7   argsr   r   _build_path_componentr:   addr   r9   argr<   r;   urir>   _PATH_MOD_TO_PREDgetmodr%   )graphpath_componentbnoderO   collpreds         r   rM   rM   o   s   " nvtn55tN34OD+,
 	
 
NF	+	+GE .%"4"455~""#a' V  	:H:M:MN:M3"5.:MN	
H L; 
NEMM	2	2		BNN$9%ASAS$TU	
8 L/ 
NE$9$9	:	: ~""#a' R  G:H:M:MN:M3"5.:MN

 			5",,dhh78 L 
NEMM	2	2 $$^%7%78< #9.:L:L9M!NOO		5 5e=P=P QRS LC O( Os   0IINc                h    [        U [        5      (       a  U S4$ Uc
  [        5       n[        X5      U4$ )a  
Build the SHACL Path triples for a path given by a [`URIRef`][rdflib.term.URIRef] for
simple paths or a [`Path`][rdflib.paths.Path] for complex paths.

Returns an [`IdentifiedNode`][rdflib.term.IdentifiedNode] for the path (which should be
the object of a triple with predicate `sh:path`) and the graph into which any
new triples were added.

Args:
    path: A [`URIRef`][rdflib.term.URIRef] or a [`Path`][rdflib.paths.Path]
    target_graph: Optionally, a [`Graph`][rdflib.graph.Graph] into which to put
        constructed triples. If not provided, a new graph will be created

Returns:
    A (path_identifier, graph) tuple where:
        - path_identifier: If path is a [`URIRef`][rdflib.term.URIRef], this is simply
        the provided path. If path is a [`Path`][rdflib.paths.Path], this is
        the [`BNode`][rdflib.term.BNode] corresponding to the root of the SHACL
        path expression added to the graph.
        - graph: None if path is a [`URIRef`][rdflib.term.URIRef] (as no new triples
        are constructed). If path is a [`Path`][rdflib.paths.Path], this is either the
        target_graph provided or a new graph into which the path triples were added.
N)r0   r
   r   rM   )r%   target_graphs     r   build_shacl_pathr[      s<    6 $Tz w !4lBBr   )r&   r   rB   r   returnzUnion[URIRef, Path])rT   r   rU   URIRef | Pathr\   r   r!   )r%   r]   rZ   zGraph | Noner\   z#tuple[IdentifiedNode, Graph | None])#__doc__
__future__r   typingr   r   r   rdflibr   r   r	   r
   r   rdflib.collectionr   rdflib.namespacer   r   rdflib.pathsr   rdflib.termr   r   	Exceptionr   
ZeroOrMorer=   	OneOrMorer?   	ZeroOrOner@   rQ   r#   rM   r[   r   r   r   <module>rj      s    # 1 1 7 7 ( $  *	Y 	 
b''	OOR%%	OOR%% KKK K\HH"/HHX 7;#C
#C'3#C(#Cr   