
    4YshA                        d Z ddlmZmZ ddlmZmZ dZdZdZ	 G d de
      Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Zd Zd Zd Zd Zd Zedk(  rddlZ ej6                          yee_        ee_        ee_        ee_        ee_         ee_        ee_        ee_        ee_        ee_         y)a  

This module implements the SPARQL 1.1 Property path operators, as
defined in:

http://www.w3.org/TR/sparql11-query/#propertypaths

In SPARQL the syntax is as follows:

+--------------------+-------------------------------------------------+
|Syntax              | Matches                                         |
+====================+=================================================+
|iri                 | An IRI. A path of length one.                   |
+--------------------+-------------------------------------------------+
|^elt                | Inverse path (object to subject).               |
+--------------------+-------------------------------------------------+
|elt1 / elt2         | A sequence path of elt1 followed by elt2.       |
+--------------------+-------------------------------------------------+
|elt1 | elt2         | A alternative path of elt1 or elt2              |
|                    | (all possibilities are tried).                  |
+--------------------+-------------------------------------------------+
|elt*                | A path that connects the subject and object     |
|                    | of the path by zero or more matches of elt.     |
+--------------------+-------------------------------------------------+
|elt+                | A path that connects the subject and object     |
|                    | of the path by one or more matches of elt.      |
+--------------------+-------------------------------------------------+
|elt?                | A path that connects the subject and object     |
|                    | of the path by zero or one matches of elt.      |
+--------------------+-------------------------------------------------+
|!iri or             | Negated property set. An IRI which is not one of|
|!(iri\ :sub:`1`\ \| | iri\ :sub:`1`...iri\ :sub:`n`.                  |
|... \|iri\ :sub:`n`)| !iri is short for !(iri).                       |
+--------------------+-------------------------------------------------+
|!^iri or            | Negated property set where the excluded matches |
|!(^iri\ :sub:`1`\ \|| are based on reversed path. That is, not one of |
|...\|^iri\ :sub:`n`)| iri\ :sub:`1`...iri\ :sub:`n` as reverse paths. |
|                    | !^iri is short for !(^iri).                     |
+--------------------+-------------------------------------------------+
|!(iri\ :sub:`1`\ \| | A combination of forward and reverse            |
|...\|iri\ :sub:`j`\ | properties in a negated property set.           |
|\|^iri\ :sub:`j+1`\ |                                                 |
|\|... \|^iri\       |                                                 |
|:sub:`n`)|          |                                                 |
+--------------------+-------------------------------------------------+
|(elt)               | A group path elt, brackets control precedence.  |
+--------------------+-------------------------------------------------+

This module is used internally by the SPARQL engine, but the property paths
can also be used to query RDFLib Graphs directly.

Where possible the SPARQL syntax is mapped to Python operators, and property
path objects can be constructed from existing URIRefs.

>>> from rdflib import Graph, Namespace
>>> from rdflib.namespace import FOAF

>>> ~FOAF.knows
Path(~http://xmlns.com/foaf/0.1/knows)

>>> FOAF.knows/FOAF.name
Path(http://xmlns.com/foaf/0.1/knows / http://xmlns.com/foaf/0.1/name)

>>> FOAF.name|FOAF.givenName
Path(http://xmlns.com/foaf/0.1/name | http://xmlns.com/foaf/0.1/givenName)

Modifiers (?, *, +) are done using * (the multiplication operator) and
the strings '*', '?', '+', also defined as constants in this file.

>>> FOAF.knows*OneOrMore
Path(http://xmlns.com/foaf/0.1/knows+)

The path objects can also be used with the normal graph methods.

First some example data:

>>> g=Graph()

>>> g=g.parse(data='''
... @prefix : <ex:> .
...
... :a :p1 :c ; :p2 :f .
... :c :p2 :e ; :p3 :g .
... :g :p3 :h ; :p2 :j .
... :h :p3 :a ; :p2 :g .
...
... :q :px :q .
...
... ''', format='n3') # doctest: +ELLIPSIS

>>> e = Namespace('ex:')

Graph contains:

>>> (e.a, e.p1/e.p2, e.e) in g
True

Graph generator functions, triples, subjects, objects, etc. :

>>> list(g.objects(e.c, (e.p3*OneOrMore)/e.p2)) # doctest: +NORMALIZE_WHITESPACE
[rdflib.term.URIRef('ex:j'), rdflib.term.URIRef('ex:g'),
    rdflib.term.URIRef('ex:f')]

A more complete set of tests:

>>> list(evalPath(g, (None, e.p1/e.p2, None)))==[(e.a, e.e)]
True
>>> list(evalPath(g, (e.a, e.p1|e.p2, None)))==[(e.a,e.c), (e.a,e.f)]
True
>>> list(evalPath(g, (e.c, ~e.p1, None))) == [ (e.c, e.a) ]
True
>>> list(evalPath(g, (e.a, e.p1*ZeroOrOne, None))) == [(e.a, e.a), (e.a, e.c)]
True
>>> list(evalPath(g, (e.c, e.p3*OneOrMore, None))) == [
...     (e.c, e.g), (e.c, e.h), (e.c, e.a)]
True
>>> list(evalPath(g, (e.c, e.p3*ZeroOrMore, None))) == [(e.c, e.c),
...     (e.c, e.g), (e.c, e.h), (e.c, e.a)]
True
>>> list(evalPath(g, (e.a, -e.p1, None))) == [(e.a, e.f)]
True
>>> list(evalPath(g, (e.a, -(e.p1|e.p2), None))) == []
True
>>> list(evalPath(g, (e.g, -~e.p2, None))) == [(e.g, e.j)]
True
>>> list(evalPath(g, (e.e, ~(e.p1/e.p2), None))) == [(e.e, e.a)]
True
>>> list(evalPath(g, (e.a, e.p1/e.p3/e.p3, None))) == [(e.a, e.h)]
True

>>> list(evalPath(g, (e.q, e.px*OneOrMore, None)))
[(rdflib.term.URIRef('ex:q'), rdflib.term.URIRef('ex:q'))]

>>> list(evalPath(g, (None, e.p1|e.p2, e.c)))
[(rdflib.term.URIRef('ex:a'), rdflib.term.URIRef('ex:c'))]

>>> list(evalPath(g, (None, ~e.p1, e.a))) == [ (e.c, e.a) ]
True
>>> list(evalPath(g, (None, e.p1*ZeroOrOne, e.c))) # doctest: +NORMALIZE_WHITESPACE
[(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:c')),
 (rdflib.term.URIRef('ex:a'), rdflib.term.URIRef('ex:c'))]

>>> list(evalPath(g, (None, e.p3*OneOrMore, e.a))) # doctest: +NORMALIZE_WHITESPACE
[(rdflib.term.URIRef('ex:h'), rdflib.term.URIRef('ex:a')),
 (rdflib.term.URIRef('ex:g'), rdflib.term.URIRef('ex:a')),
 (rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:a'))]

>>> list(evalPath(g, (None, e.p3*ZeroOrMore, e.a))) # doctest: +NORMALIZE_WHITESPACE
[(rdflib.term.URIRef('ex:a'), rdflib.term.URIRef('ex:a')),
 (rdflib.term.URIRef('ex:h'), rdflib.term.URIRef('ex:a')),
 (rdflib.term.URIRef('ex:g'), rdflib.term.URIRef('ex:a')),
 (rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:a'))]

>>> list(evalPath(g, (None, -e.p1, e.f))) == [(e.a, e.f)]
True
>>> list(evalPath(g, (None, -(e.p1|e.p2), e.c))) == []
True
>>> list(evalPath(g, (None, -~e.p2, e.j))) == [(e.g, e.j)]
True
>>> list(evalPath(g, (None, ~(e.p1/e.p2), e.a))) == [(e.e, e.a)]
True
>>> list(evalPath(g, (None, e.p1/e.p3/e.p3, e.h))) == [(e.a, e.h)]
True

>>> list(evalPath(g, (e.q, e.px*OneOrMore, None)))
[(rdflib.term.URIRef('ex:q'), rdflib.term.URIRef('ex:q'))]

>>> list(evalPath(g, (e.c, (e.p2|e.p3)*ZeroOrMore, e.j)))
[(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:j'))]

No vars specified:

>>> sorted(list(evalPath(g, (None, e.p3*OneOrMore, None)))) #doctest: +NORMALIZE_WHITESPACE
[(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:a')),
 (rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:g')),
 (rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:h')),
 (rdflib.term.URIRef('ex:g'), rdflib.term.URIRef('ex:a')),
 (rdflib.term.URIRef('ex:g'), rdflib.term.URIRef('ex:h')),
 (rdflib.term.URIRef('ex:h'), rdflib.term.URIRef('ex:a'))]

    )URIRefNode)UnionCallable*+?c                       e Zd ZU ed ed   gdf   ed<   ed gdf   ed<   ed gdf   ed<   ed ed   gdf   ed	<   ed egd
f   ed<   ddZd Zd Z	d Z
d Zd Zd Zd Zy)Path)r   r   AlternativePath__or__InvPath
__invert__NegatedPath__neg__SequencePath__truediv__MulPath__mul__Nc                     t               N)NotImplementedError)selfgraphsubjobjs       l/var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/paths.pyevalz	Path.eval   s    !##    c                 *    t        t        |             S r   )hashreprr   s    r   __hash__zPath.__hash__   s    DJr   c                 0    t        |       t        |      k(  S r   )r"   r   others     r   __eq__zPath.__eq__   s    DzT%[((r   c                     t        |t        t        f      s$t        dt	        |       dt	        |      d      t	        |       t	        |      k  S Nzunorderable types: z() < z()
isinstancer   r   	TypeErrorr"   r&   s     r   __lt__zPath.__lt__   sB    %$.48JUL  DzDK''r   c                     t        |t        t        f      s$t        dt	        |       dt	        |      d      t	        |       t	        |      k  S r*   r+   r&   s     r   __le__zPath.__le__   sB    %$.48JUL  DzT%[((r   c                     | |k(   S r    r&   s     r   __ne__zPath.__ne__       5=  r   c                     | |k   S r   r2   r&   s     r   __gt__zPath.__gt__   r4   r   c                     | |k   S r   r2   r&   s     r   __ge__zPath.__ge__   s    %<r   NN)__name__
__module____qualname__r   r   __annotations__strr   r$   r(   r.   r0   r3   r6   r8   r2   r   r   r   r      s    fe$4568IIJJ&9,--vh-..65)9#:;^KLLvsmY.//$ )()!! r   r   c                   &    e Zd Zd ZddZd Zd Zy)r   c                     || _         y r   argr   rB   s     r   __init__zInvPath.__init__   s	    r   Nc              #   Z   K   t        ||| j                  |f      D ]  \  }}||f  y wr   )evalPathrB   )r   r   r   r   sos         r   r   zInvPath.eval   s4     US$((D$9: 	DAqQ$J	s   )+c                 "    d| j                   dS )NzPath(~)rA   r#   s    r   __repr__zInvPath.__repr__   s    "hh((r   c                 <    d| j                   j                         z  S )Nz^%s)rB   n3r#   s    r   rM   z
InvPath.n3   s    txx{{}$$r   r9   r:   r;   r<   rD   r   rK   rM   r2   r   r   r   r      s    )%r   r   c                   &    e Zd Zd ZddZd Zd Zy)r   c                     g | _         |D ]M  }t        |t              r | xj                   |j                   z  c_         3| j                   j                  |       O y r   )argsr,   r   appendr   rQ   as      r   rD   zSequencePath.__init__   sF    	 	$A!\*		QVV#			  #		$r   Nc                     fdfd}|r | j                   ||      S |r || j                   ||      S  | j                   ||      S )Nc              3      K   | dd  r6t        || d   d f      D ]   \  }} | dd  ||      D ]  }||d   f  " y t        || d   |f      D ]  \  }}||f  y w)N   r   rF   pathsr   r   rG   rH   r	_eval_seqr   s         r   r\   z$SequencePath.eval.<locals>._eval_seq  s     QRy$UT58T,BC &DAq&uQRy!S9 &1g&&
 %UT58S,AB DAqQ$J   AA c              3      K   | d d r6t        d | d   |f      D ]   \  }} | d d ||      D ]  }|d   |f  " y t        || d   |f      D ]  \  }}||f  y w)Nr   rX   rY   s         r   _eval_seq_bwz'SequencePath.eval.<locals>._eval_seq_bw  s     Sbz$UT59c,BC &DAq&uSbz4; &dAg&&
 %UT58S,AB DAqQ$Jr]   )rQ   )r   r   r   r   r`   r\   s    `   @r   r   zSequencePath.eval  sN    		 TYYc22		455TYYc22r   c                 L    ddj                  d | j                  D              z  S )NPath(%s)z / c              3   2   K   | ]  }t        |        y wr   r>   .0xs     r   	<genexpr>z(SequencePath.__repr__.<locals>.<genexpr>!       &A!s1v&A   joinrQ   r#   s    r   rK   zSequencePath.__repr__        EJJ&Atyy&AAAAr   c                 F    dj                  d | j                  D              S )N/c              3   <   K   | ]  }|j                           y wr   rM   rf   rT   s     r   rh   z"SequencePath.n3.<locals>.<genexpr>$       212   rk   r#   s    r   rM   zSequencePath.n3#      xx2		222r   r9   rN   r2   r   r   r   r      s    $38B3r   r   c                   &    e Zd Zd ZddZd Zd Zy)r   c                     g | _         |D ]M  }t        |t              r | xj                   |j                   z  c_         3| j                   j                  |       O y r   )rQ   r,   r   rR   rS   s      r   rD   zAlternativePath.__init__(  sF    	 	$A!_-		QVV#			  #		$r   Nc              #   ^   K   | j                   D ]  }t        ||||f      D ]  }|   y wr   )rQ   rF   )r   r   r   r   rg   ys         r   r   zAlternativePath.eval0  s:      	AedAs^4 	s   +-c                 L    ddj                  d | j                  D              z  S )Nrb   z | c              3   2   K   | ]  }t        |        y wr   rd   re   s     r   rh   z+AlternativePath.__repr__.<locals>.<genexpr>6  ri   rj   rk   r#   s    r   rK   zAlternativePath.__repr__5  rm   r   c                 F    dj                  d | j                  D              S )N|c              3   <   K   | ]  }|j                           y wr   rq   rr   s     r   rh   z%AlternativePath.n3.<locals>.<genexpr>9  rs   rt   rk   r#   s    r   rM   zAlternativePath.n38  ru   r   r9   rN   r2   r   r   r   r   '  s    $
B3r   r   c                   &    e Zd Zd ZddZd Zd Zy)r   c                     || _         || _        |t        k(  rd| _        d| _        y |t
        k(  rd| _        d| _        y |t        k(  rd| _        d| _        y t        d|z        )NTFzUnknown modifier %s)pathmod	ZeroOrOnezeromore
ZeroOrMore	OneOrMore	Exception)r   r   r   s      r   rD   zMulPath.__init__=  sc    	)DIDIJDIDIIDIDI1C788r   Nc              #      	K    j                   r#|r!|r|r||k(  r||f n|r||f n|r||f d	 fd		d fd		 fd}t               }|r2 	||t                     D ]  }||vs|j                  |       |  y |r2 ||t                     D ]  }||vs|j                  |       |  y  |       D ]  }||vs|j                  |       |  y w)Nc              3      K   |j                  |        t        | 	j                  d f      D ]<  \  }}|r||k(  r||f 	j                  s ||v r% |||      D ]  \  }}||f  > y wr   addrF   r   r   )
r   r   seenrG   rH   s2o2_fwdr   r   s
          r   r   zMulPath.eval.<locals>._fwdW  s     HHTN tyy$(?@ $1a3hQ$J99Dy "&q#t"4 $Be$$   A
A. A.c              3      K   |j                  |       t        d 	j                  |f      D ]<  \  }}| r| |k(  r||f 	j                  s ||v r% d ||      D ]  \  }}||f  > y wr   r   )
r   r   r   rG   rH   r   r   _bwdr   r   s
          r   r   zMulPath.eval.<locals>._bwdc  s     HHSM tyy#(>? $1tqyQ$J99Dy "&tQ"5 $B !e$$r   c            	   3     K   	j                   rZt               } j                  d       D ]<  \  }}|| vr| j                  |       ||f || vs&| j                  |       ||f > t               }t	        d 	j
                  d f      D ]`  \  }}	j                  s||f ||vs|j                  |       t         |d t                           }|D ]  \  }}||k(  sJ ||f  b y wr   )r   setsubject_objectsr   rF   r   r   list)
seen1rG   rH   r   fs1o1r   r   r   s
          r   _all_fwd_pathsz$MulPath.eval.<locals>._all_fwd_pathsp  s     yy "11$7 #DAq~		!d
~		!d
# 5D tyy$(?@ 	)1yyQ$J} asu!56&' )FB#%7N7"$b&L)	)s   AC1AC1+AC1)NNN)r   r   r   )
r   r   r   r   firstr   donerg   r   r   s
   ``      @@r   r   zMulPath.evalM  s     993;)ODj 3h
	$	$	)8 u$SU+ D=HHQKG $SU+ D=HHQKG
 $% D=HHQKGs   A,C&33C&'&C&C&c                 :    d| j                   | j                  dS )NzPath(rJ   )r   r   r#   s    r   rK   zMulPath.__repr__  s    #yy$((33r   c                 R    | j                   j                         | j                  S r   )r   rM   r   r#   s    r   rM   z
MulPath.n3  s    22r   )NNTrN   r2   r   r   r   r   <  s    9 N`43r   r   c                   &    e Zd Zd ZddZd Zd Zy)r   c                     t        |t        t        f      r	|g| _        y t        |t              r|j                  | _        y t        dd|z         )Nz%Can only negate URIRefs, InvPaths or zAlternativePaths, not: )r,   r   r   rQ   r   r   rC   s     r   rD   zNegatedPath.__init__  sJ    cFG,-DI_-DI71467 r   Nc              #     K   |j                  |d |f      D ]f  \  }}}| j                  D ]K  }t        |t              r||k(  s .t        |t              r||j
                  |f|v s= Rt        d|z         ||f h y w)NzInvalid path in NegatedPath: %s)triplesrQ   r,   r   r   rB   r   )r   r   r   r   rG   prH   rT   s           r   r   zNegatedPath.eval  s     }}dD#%67 	GAq!YY 
a(Av7+155!}-#$E$IJJ
 d
	s   A B#B'Bc                 L    ddj                  d | j                  D              z  S )Nz
Path(! %s),c              3   2   K   | ]  }t        |        y wr   rd   re   s     r   rh   z'NegatedPath.__repr__.<locals>.<genexpr>  ri   rj   rk   r#   s    r   rK   zNegatedPath.__repr__  s     chh&Atyy&AAAAr   c                 >    ddj                  | j                        z  S )Nz!(%s)r}   rk   r#   s    r   rM   zNegatedPath.n3  s    #((499-..r   r9   rN   r2   r   r   r   r     s    	B/r   r   c                       e Zd Zy)PathListN)r:   r;   r<   r2   r   r   r   r     s    r   r   c                 \    t        |t        t        f      st        d      t	        | |      S )z
    alternative path
    &Only URIRefs or Paths can be in paths!)r,   r   r   r   r   r&   s     r   path_alternativer     s+     efd^,@AA4''r   c                 \    t        |t        t        f      st        d      t	        | |      S )z
    sequence path
    r   )r,   r   r   r   r   r&   s     r   path_sequencer     s+     efd^,@AAe$$r   c                 2    d | j                  |      D        S )Nc              3   ,   K   | ]  \  }}}||f  y wr   r2   )rf   rG   r   rH   s       r   rh   zevalPath.<locals>.<genexpr>  s     3wq!QQF3s   )r   )r   ts     r   rF   rF     s    3%--"233r   c                     t        | |      S )z
    cardinality path
    )r   )r   muls     r   mul_pathr     s     1c?r   c                     t        |       S )z
    inverse path
    )r   r   s    r   inv_pathr     s     1:r   c                     t        |       S )z
    negated path
    )r   r   s    r   neg_pathr     s     q>r   __main__N)!__doc__rdflib.termr   r   typingr   r   r   r   r   objectr   r   r   r   r   r   r   r   r   r   rF   r   r   r   r:   doctesttestmodr   r   r   r   r   r2   r   r   <module>r      s  tn % "
 
		& 6 & R%d %)34 )3X3d 3*e3d e3P/$ /B	t 	(%4 zGOO %FM FN FFN&FDODLDL"DK$Dr   