
    Pi                        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j        ej        ej        ej        ej        ej        iZddZ ddZ!	 d d!dZ"dS )"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dS )SHACLPathErrorN)__name__
__module____qualname__     g/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/rdflib/extras/shacl.pyr   r      s        Dr   r   shapes_graphr   path_identifierr   returnUnion[URIRef, Path]c                :    d}t          |t                    rt          d          t          |t                    r#|t          j        k    rt          d          |}n;                     |t          j                  `t           
                    |                    }t          |          dk     rt          d          t          j         fd|D              }n                     |t          j                  x}r$t          j        t#           |                    }nu                     |t          j                  x}r_t           
                    |                    }t          |          dk     rt          d          t          j         fd|D              }n                     |t          j                  x}r$t          j        t#           |          d	          }n                     |t          j                  x}r$t          j        t#           |          d
          }nh                     |t          j                  x}	r$t          j        t#           |	          d          }n"| t          dt1          |           d          |S )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  8   K   | ]}t          |          V  d S Nparse_shacl_path).0pathr   s     r   	<genexpr>z#parse_shacl_path.<locals>.<genexpr>G   s.      HHt|T22HHHHHHr   @List of SHACL alternate paths must have at least two path items.c              3  8   K   | ]}t          |          V  d S r"   r#   )r%   alternativer   s     r   r'   z#parse_shacl_path.<locals>.<genexpr>V   sA         !{;;     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   r   r&   sequenceinverse_pathalternative_pathalternativeszero_or_more_pathone_or_more_pathzero_or_one_paths
   `         r   r$   r$   !   s     +/D /7++ 6W>??? 
OV	,	, 2Wcg%% M    
		OSY	7	7	C**?;;<<x==1 V   !HHHHxHHH

 &++OR^LL	L W}-lLIIJJ *//ASTT	T	 WL../?@@AA|q   R   $   #/  
 +00"BSTT	T	 W}-l<MNNPSTT *//AQRR	R	 	W}-l<LMMsSS *//AQRR	R	 W}-l<LMMsSS 
UT/-B-BUUUVVVKr   graphpath_componentURIRef | Pathr   c                d    t          |t          t          f          s#t          dt	          |           ddz             t          |t                    r|S t                      }t          |t          j                  rKt          |j	                  dk     rt          d          t           | fd|j	        D                        net          |t          j                  r7                     |t          j        t!           |j                  f           nt          |t          j                  r}t          |j	                  dk     rt          d          t           t                       fd|j	        D                       }                     |t          j        |j        f           n}t          |t          j                  rct,                              |j                  }|t          d
|j                                        ||t!           |j                  f           |S )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    c                0    g | ]}t          |          S r   _build_path_componentr%   argrG   s     r   
<listcomp>z)_build_path_component.<locals>.<listcomp>   $    NNN3"5#..NNNr   r(   c                0    g | ]}t          |          S r   rL   rN   s     r   rP   z)_build_path_component.<locals>.<listcomp>   rQ   r   NzUnknown path modifier )r.   r
   r   r/   typer   r   r6   r5   argsr   r   r8   addr   r7   rM   rO   r:   r9   urir<   _PATH_MOD_TO_PREDgetmodr&   )rG   rH   bnodecollpreds   `    r   rM   rM   o   sM   " nvtn55 DtN33DDD+,
 
 	
 
NF	+	+ GGE .%"455 'T~"##a'' V   	NNNN.:MNNN	
 	
 	
 	
 
NEM	2	2 T		BN$9%AS$T$TU	
 	
 	
 	

 
NE$9	:	: T ~"##a'' R   GGNNNN.:MNNN
 

 			5",dh78888 
NEM	2	2 T $$^%788< !N.:L!N!NOOO		5$ 5e^=P Q QRSSS Lr   Nr&   target_graphGraph | None#tuple[IdentifiedNode, Graph | None]c                x    t          | t                    r| dfS |t                      }t          ||           |fS )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)r.   r
   r   rM   )r&   r]   s     r   build_shacl_pathra      sH    6 $ Tz ww !t44lBBr   )r   r   r   r   r   r   )rG   r   rH   rI   r   r   r"   )r&   rI   r]   r^   r   r_   )#__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>   rW   r$   rM   ra   r   r   r   <module>rn      s    # " " " " " 1 1 1 1 1 1 1 1 1 1 7 7 7 7 7 7 7 7 7 7 7 7 7 7 ( ( ( ( ( ( $ $ $ $ $ $ $ $             +******	 	 	 	 	Y 	 	 	 
b'	OR%	OR% K K K K\H H H HX 7;#C #C #C #C #C #C #Cr   