
    iy                     d   d Z ddlmZ ddlmZmZmZ ddlmZm	Z	m
Z
mZmZ ddlmZ ddlmZmZ ddlmZ ddlmZ erdd	lmZ  G d
 de      Zej2                  ej4                  ej6                  ej8                  ej:                  ej<                  iZ	 	 	 	 	 	 ddZ 	 	 	 	 	 	 ddZ!	 d	 	 	 	 	 ddZ"y)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                      e Zd Zy)SHACLPathErrorN)__name__
__module____qualname__     BC:\Projects\mas-dev\.venv\Lib\site-packages\rdflib/extras/shacl.pyr   r      s    r   r   c                    d}t        |t              rt        d      t        |t              r"|t        j
                  k(  rt        d      |}|S  j                  |t        j                        Pt         j                  |            }t        |      dk  rt        d      t        j                   fd|D         }|S  j                  |t        j                        x}r!t        j                   t#         |            }|S  j                  |t        j$                        x}rPt         j                  |            }t        |      dk  rt        d      t        j&                   fd|D         }|S  j                  |t        j(                        x}r"t        j*                  t#         |      d	      }|S  j                  |t        j,                        x}r"t        j*                  t#         |      d
      }|S  j                  |t        j.                        x}	r"t        j*                  t#         |	      d      }|S |t        dt1        |       d      |S )a  
    Parse a valid SHACL path (e.g. the object of a triple with predicate sh:path)
    from a :class:`~rdflib.graph.Graph` as a :class:`~rdflib.term.URIRef` if the path
    is simply a predicate or a :class:`~rdflib.paths.Path` otherwise.

    :param shapes_graph: A :class:`~rdflib.graph.Graph` containing the path to be parsed
    :param path_identifier: A :class:`~rdflib.term.Node` of the path
    :return: A :class:`~rdflib.term.URIRef` or a :class:`~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  6   K   | ]  }t        |        y wNparse_shacl_path).0pathshapes_graphs     r   	<genexpr>z#parse_shacl_path.<locals>.<genexpr>D   s     Hxt|T2x   @List of SHACL alternate paths must have at least two path items.c              3  6   K   | ]  }t        |        y wr   r   )r!   alternativer#   s     r   r$   z#parse_shacl_path.<locals>.<genexpr>S   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    !   sh    +/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                n   t        |t        t        f      st        dt	        |       ddz         t        |t              r|S t               }t        |t        j                        rTt        |j                        dk  rt        d      t        | ||j                  D cg c]  }t        | |       c}       |S t        |t        j                        r8| j                  |t        j                   t        | |j"                        f       |S t        |t        j$                        rt        |j                        dk  rt        d      t        | t               |j                  D cg c]  }t        | |       c}      }| j                  |t        j&                  |j(                  f       |S t        |t        j*                        rat,        j/                  |j0                        }|t        d|j0                         | j                  ||t        | |j2                        f       |S c c}w c c}w )a  
    Helper method that implements the recursive component of SHACL path
    triple construction.

    :param graph: A :class:`~rdflib.graph.Graph` into which to insert triples
    :param graph_component: A :class:`~rdflib.term.URIRef` or
        :class:`~rdflib.paths.Path` that is part of a path expression
    :return: The :class:`~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 )r,   r
   r   r-   typer   r   r4   r3   argsr   r   _build_path_componentr6   addr   r5   argr8   r7   urir:   _PATH_MOD_TO_PREDgetmodr"   )graphpath_componentbnoderK   collpreds         r   rI   rI   l   s    nvtn5tN34OD+,
 	
 
NF	+GE .%"4"45~""#a' V  	:H:M:MN:M3"5#.:MN	
H L; 
NEMM	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 $$^%7%78< #9.:L:L9M!NOO		5$ 5e^=P=P QRS LC O( Os    H-(H2Nc                ^    t        | t              r| dfS |
t               }t        ||       |fS )a  
    Build the SHACL Path triples for a path given by a :class:`~rdflib.term.URIRef` for
    simple paths or a :class:`~rdflib.paths.Path` for complex paths.

    Returns an :class:`~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.

    :param path: A :class:`~rdflib.term.URIRef` or a :class:`~rdflib.paths.Path`
    :param target_graph: Optionally, a :class:`~rdflib.graph.Graph` into which to put
        constructed triples. If not provided, a new graph will be created
    :return: A (path_identifier, graph) tuple where:
        - path_identifier: If path is a :class:`~rdflib.term.URIRef`, this is simply
        the provided path. If path is a :class:`~rdflib.paths.Path`, this is
        the :class:`~rdflib.term.BNode` corresponding to the root of the SHACL
        path expression added to the graph.
        - graph: None if path is a :class:`~rdflib.term.URIRef` (as no new triples
        are constructed). If path is a :class:`~rdflib.paths.Path`, this is either the
        target_graph provided or a new graph into which the path triples were added.
    N)r,   r
   r   rI   )r"   target_graphs     r   build_shacl_pathrW      s;    0 $Tz w !t4lBBr   )r#   r   r>   r   returnzUnion[URIRef, Path])rP   r   rQ   URIRef | PathrX   r   r   )r"   rY   rV   zGraph | NonerX   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   
ZeroOrMorer9   	OneOrMorer;   	ZeroOrOner<   rM   r    rI   rW   r   r   r   <module>rf      s    # 1 1 7 7 ( $  *	Y 	 
b''	OOR%%	OOR%% HHH HVEE"/EER 7; C
 C'3 C( Cr   