o
    iT                     @  s  d dl mZ dZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZ d dlmZmZ erLd dlmZ d dlmZmZmZmZ d d	lmZ d
ZdZdZ	d>d?ddZ eG dd dZ!G dd de!Z"G dd de!Z#G dd de!Z$G dd de!Z%G dd  d e!Z&G d!d" d"e'Z(d@d&d'Z)d@d(d)Z*dAd/d0Z+dAd1d2Z,dBd6d7Z-dCd8d9Z.dDd;d<Z/e0d=krdS e)e_1e-e_2e.e_3e/e_4e*e_5e.e!_3e/e!_4e-e!_2e)e!_1e*e!_5dS )E    )annotationsa  

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(eval_path(g, (None, e.p1/e.p2, None)))==[(e.a, e.e)]
True
>>> list(eval_path(g, (e.a, e.p1|e.p2, None)))==[(e.a,e.c), (e.a,e.f)]
True
>>> list(eval_path(g, (e.c, ~e.p1, None))) == [ (e.c, e.a) ]
True
>>> list(eval_path(g, (e.a, e.p1*ZeroOrOne, None))) == [(e.a, e.a), (e.a, e.c)]
True
>>> list(eval_path(g, (e.c, e.p3*OneOrMore, None))) == [
...     (e.c, e.g), (e.c, e.h), (e.c, e.a)]
True
>>> list(eval_path(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(eval_path(g, (e.a, -e.p1, None))) == [(e.a, e.f)]
True
>>> list(eval_path(g, (e.a, -(e.p1|e.p2), None))) == []
True
>>> list(eval_path(g, (e.g, -~e.p2, None))) == [(e.g, e.j)]
True
>>> list(eval_path(g, (e.e, ~(e.p1/e.p2), None))) == [(e.e, e.a)]
True
>>> list(eval_path(g, (e.a, e.p1/e.p3/e.p3, None))) == [(e.a, e.h)]
True

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

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

>>> list(eval_path(g, (None, ~e.p1, e.a))) == [ (e.c, e.a) ]
True
>>> list(eval_path(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(eval_path(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(eval_path(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(eval_path(g, (None, -e.p1, e.f))) == [(e.a, e.f)]
True
>>> list(eval_path(g, (None, -(e.p1|e.p2), e.c))) == []
True
>>> list(eval_path(g, (None, -~e.p2, e.j))) == [(e.g, e.j)]
True
>>> list(eval_path(g, (None, ~(e.p1/e.p2), e.a))) == [(e.e, e.a)]
True
>>> list(eval_path(g, (None, e.p1/e.p3/e.p3, e.h))) == [(e.a, e.h)]
True

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

>>> list(eval_path(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(eval_path(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'))]

N)total_ordering)
TYPE_CHECKINGAnyCallable	GeneratorIteratorListOptionalSetTupleUnion)NodeURIRef)_MulPathMod)Graph_ObjectType_PredicateType_SubjectType)NamespaceManager*+?argUnion['URIRef', 'Path']namespace_managerOptional['NamespaceManager']returnstrc                 C  s4   t | ttfrt| jdkrd| | S | |S )N   z(%s))
isinstanceSequencePathAlternativePathlenargsn3)r   r    r&   G/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/paths.py_n3   s   
r(   c                   @  s`   e Zd ZU ded< ded< ded< ded< d	ed
< 		dd ddZdd Zdd Zd!ddZdS )"Pathz>Callable[['Path', Union['URIRef', 'Path']], 'AlternativePath']__or__zCallable[['Path'], 'InvPath']
__invert__z!Callable[['Path'], 'NegatedPath']__neg__z;Callable[['Path', Union['URIRef', 'Path']], 'SequencePath']__truediv__z"Callable[['Path', str], 'MulPath']__mul__Ngraph'Graph'subjOptional['_SubjectType']objOptional['_ObjectType']r   .Iterator[Tuple['_SubjectType', '_ObjectType']]c                 C  s   t  N)NotImplementedError)selfr/   r1   r3   r&   r&   r'   eval   s   z	Path.evalc                 C  s   t t| S r6   )hashreprr8   r&   r&   r'   __hash__      zPath.__hash__c                 C  s   t | t |kS r6   )r;   r8   otherr&   r&   r'   __eq__      zPath.__eq__r@   r   boolc                 C  s6   t |ttfstdt| t|f t| t|k S )Nzunorderable types: %s() < %s())r    r)   r   	TypeErrorr;   r?   r&   r&   r'   __lt__   s
   zPath.__lt__NN)r/   r0   r1   r2   r3   r4   r   r5   )r@   r   r   rC   )__name__
__module____qualname____annotations__r9   r=   rA   rE   r&   r&   r&   r'   r)      s   
 r)   c                   @  <   e Zd ZdddZ		ddddZdddZddddZdS )InvPathr   Union[Path, URIRef]c                 C  s
   || _ d S r6   r   r8   r   r&   r&   r'   __init__   s   
zInvPath.__init__Nr/   r0   r1   r2   r3   r4   r   7Generator[Tuple[_ObjectType, _SubjectType], None, None]c                 c  s,    t ||| j|fD ]	\}}||fV  q
d S r6   )	eval_pathr   )r8   r/   r1   r3   sor&   r&   r'   r9     s   zInvPath.evalr   c                 C  s   d| j f S )Nz	Path(~%s)rN   r<   r&   r&   r'   __repr__  r>   zInvPath.__repr__r   r   c                 C  s   dt | j| S )Nz^%s)r(   r   r8   r   r&   r&   r'   r%     rB   z
InvPath.n3)r   rM   rF   )r/   r0   r1   r2   r3   r4   r   rQ   r   r   r6   r   r   r   r   rG   rH   rI   rP   r9   rU   r%   r&   r&   r&   r'   rL      s    

	rL   c                   @  rK   )r!   r$   rM   c                 G  <   g | _ |D ]}t|tr|  j |j 7  _ q| j | qd S r6   )r$   r    r!   appendr8   r$   ar&   r&   r'   rP        
zSequencePath.__init__Nr/   r0   r1   r2   r3   r4   r   7Generator[Tuple[_SubjectType, _ObjectType], None, None]c                   sR   d fd	d
 d fdd}|r | j ||S |r"|| j ||S  | j ||S )NpathsList[Union[Path, URIRef]]r1   Optional[_SubjectType]r3   Optional[_ObjectType]r   r_   c                 3  s    | dd  r,t || d d fD ]\}} | dd  ||D ]	}||d fV  qqd S t || d |fD ]	\}}||fV  q6d S )Nr   r   rR   r`   r1   r3   rS   rT   r	_eval_seqr/   r&   r'   rh   "     z$SequencePath.eval.<locals>._eval_seqr   c                 3  s    | d d r,t d | d |fD ]\}} | d d ||D ]	}|d |fV  qqd S t || d |fD ]	\}}||fV  q6d S )Nr   rd   re   rg   r&   r'   _eval_seq_bw0  ri   z'SequencePath.eval.<locals>._eval_seq_bw)r`   ra   r1   rb   r3   rc   r   r_   )r`   ra   r1   rb   r3   r   r   r_   )r$   )r8   r/   r1   r3   rk   r&   rg   r'   r9     s   zSequencePath.evalr   c                 C     dd dd | jD  S )NPath(%s)z / c                 s      | ]}t |V  qd S r6   r   .0xr&   r&   r'   	<genexpr>F      z(SequencePath.__repr__.<locals>.<genexpr>joinr$   r<   r&   r&   r'   rU   E     zSequencePath.__repr__r   r   c                      d  fdd| jD S )N/c                 3      | ]}t | V  qd S r6   r(   rq   r]   r   r&   r'   rs   I      z"SequencePath.n3.<locals>.<genexpr>ru   rV   r&   r}   r'   r%   H  rw   zSequencePath.n3r$   rM   rF   r/   r0   r1   r2   r3   r4   r   r_   rW   r6   rX   rY   r&   r&   r&   r'   r!     s    

)r!   c                   @  rK   )r"   r$   rM   c                 G  rZ   r6   )r$   r    r"   r[   r\   r&   r&   r'   rP   M  r^   zAlternativePath.__init__Nr/   r0   r1   r2   r3   r4   r   r_   c                 c  s.    | j D ]}t||||fD ]}|V  qqd S r6   )r$   rR   )r8   r/   r1   r3   rr   yr&   r&   r'   r9   U  s   
zAlternativePath.evalr   c                 C  rl   )Nrm   z | c                 s  rn   r6   ro   rp   r&   r&   r'   rs   `  rt   z+AlternativePath.__repr__.<locals>.<genexpr>ru   r<   r&   r&   r'   rU   _  rw   zAlternativePath.__repr__r   r   c                   rx   )N|c                 3  rz   r6   r{   r|   r}   r&   r'   rs   c  r~   z%AlternativePath.n3.<locals>.<genexpr>ru   rV   r&   r}   r'   r%   b  rw   zAlternativePath.n3r   rF   r   rW   r6   rX   rY   r&   r&   r&   r'   r"   L  s    


r"   c                   @  s>   e Zd ZdddZ			ddddZdddZd d!ddZdS )"MulPathpathrM   modr   c                 C  s`   || _ || _|tkrd| _d| _d S |tkrd| _d| _d S |tkr*d| _d| _d S td| )NTFzUnknown modifier %s)r   r   	ZeroOrOnezeromore
ZeroOrMore	OneOrMore	Exception)r8   r   r   r&   r&   r'   rP   g  s   


zMulPath.__init__NTr/   r0   r1   r2   r3   r4   firstrC   r   r_   c                 #  s"   j r#|r#|r|r||kr||fV  n|r||fV  n|r#||fV  	 	 	 ddfd	d
	 	 	 dd fdd dfdd}t }|rc||t D ]}||vr`|| |V  qRd S |r} ||t D ]}||vrz|| |V  qld S | D ]}||vr|| |V  qd S )Nr1   rb   r3   rc   seenOptional[Set[_SubjectType]]r   r_   c                 3  sr    | |  t| jd fD ]'\}}|r||kr||fV  jr6||v r&q |||D ]	\}}||fV  q,qd S r6   addrR   r   r   r1   r3   r   rS   rT   s2o2_fwdr/   r8   r&   r'   r     s   

zMulPath.eval.<locals>._fwdOptional[Set[_ObjectType]]c                 3  sr    | | td j|fD ]'\}}| r| |kr||fV  jr6||v r&q d ||D ]	\}}||fV  q,qd S r6   r   r   )_bwdr/   r8   r&   r'   r     s   

zMulPath.eval.<locals>._bwdc                  3  s    j r-t } d D ] \}}|| vr| | ||fV  || vr,| | ||fV  qt }td jd fD ]1\}}jsF||fV  q9||vrj|| t |d t }|D ]\}}||ksdJ ||fV  qZq9d S r6   )r   setsubject_objectsr   rR   r   r   list)seen1rS   rT   r   fs1o1r   r&   r'   _all_fwd_paths  s.   




z$MulPath.eval.<locals>._all_fwd_paths)NNN)r1   rb   r3   rc   r   r   r   r_   )r1   rb   r3   rc   r   r   r   r_   )r   r_   )r   r   r   )r8   r/   r1   r3   r   r   donerr   r&   )r   r   r/   r8   r'   r9   w  sP   






zMulPath.evalr   c                 C  s   d| j | jf S )Nz
Path(%s%s))r   r   r<   r&   r&   r'   rU     rB   zMulPath.__repr__r   r   c                 C  s   dt | j|| jf S )Nz%s%s)r(   r   r   rV   r&   r&   r'   r%     s   z
MulPath.n3)r   rM   r   r   )NNT)
r/   r0   r1   r2   r3   r4   r   rC   r   r_   rW   r6   rX   rY   r&   r&   r&   r'   r   f  s    

dr   c                   @  s6   e Zd ZdddZdddZdd
dZddddZdS )NegatedPathr   'Union[AlternativePath, InvPath, URIRef]c                 C  sF   |  t |ttfr|g| _d S t |tr|j| _d S tdd|f  )Nz%Can only negate URIRefs, InvPaths or zAlternativePaths, not: %s)r    r   rL   r$   r"   r   rO   r&   r&   r'   rP     s   
zNegatedPath.__init__Nc                 c  sz    | |d |fD ]1\}}}| jD ]#}t|tr||kr nqt|tr/||j|f|v r. nqtd| ||fV  q	d S )NzInvalid path in NegatedPath: %s)triplesr$   r    r   rL   r   r   )r8   r/   r1   r3   rS   prT   r]   r&   r&   r'   r9     s   



zNegatedPath.evalr   r   c                 C  rl   )Nz
Path(! %s),c                 s  rn   r6   ro   rp   r&   r&   r'   rs     rt   z'NegatedPath.__repr__.<locals>.<genexpr>ru   r<   r&   r&   r'   rU     rw   zNegatedPath.__repr__r   r   c                   s   dd  fdd| jD  S )Nz!(%s)r   c                 3  rz   r6   r{   )rq   r   r}   r&   r'   rs     r~   z!NegatedPath.n3.<locals>.<genexpr>ru   rV   r&   r}   r'   r%      s   zNegatedPath.n3)r   r   rF   rW   r6   rX   rY   r&   r&   r&   r'   r     s
    


r   c                   @  s   e Zd ZdS )PathListN)rG   rH   rI   r&   r&   r&   r'   r     s    r   r8   Union[URIRef, Path]r@   c                 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?   r&   r&   r'   path_alternative     
r   c                 C  r   )z
    sequence path
    r   )r    r   r)   r   r!   r?   r&   r&   r'   path_sequence  r   r   r/   r   t[Tuple[Optional['_SubjectType'], Union[None, Path, _PredicateType], Optional['_ObjectType']]*Iterator[Tuple[_SubjectType, _ObjectType]]c                 C  s   t td t| |S )Nzrdflib.path.evalPath() is deprecated, use the (snake-cased) eval_path(). The mixed-case evalPath() function name is incompatible with PEP8 recommendations and will be replaced by eval_path() in rdflib 7.0.0.)warningswarnDeprecationWarningrR   r/   r   r&   r&   r'   evalPath  s   
r   c                 C  s   dd |  |D S )Nc                 s  s    | ]
\}}}||fV  qd S r6   r&   )rq   rS   r   rT   r&   r&   r'   rs   4  s    zeval_path.<locals>.<genexpr>)r   r   r&   r&   r'   rR   ,  s   rR   r   mulr   c                 C  s
   t | |S )z
    cardinality path
    )r   )r   r   r&   r&   r'   mul_path7  s   
r   c                 C     t | S )z
    inverse path
    )rL   r   r&   r&   r'   inv_path>     r   'Union[URIRef, AlternativePath, InvPath]c                 C  r   )z
    negated path
    )r   r   r&   r&   r'   neg_pathE  r   r   __main__r6   )r   r   r   r   r   r   )r8   r   r@   r   )r/   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   rL   )r   r   r   r   )6
__future__r   __doc__r   	functoolsr   typingr   r   r   r   r   r	   r
   r   r   r   rdflib.termr   r   rdflib._type_checkingr   rdflib.graphr   r   r   r   rdflib.namespacer   r   r   r   r(   r)   rL   r!   r"   r   r   r   r   r   r   r   rR   r   r   r   rG   r*   r.   r+   r,   r-   r&   r&   r&   r'   <module>   sV     80	9|"

	
	




