3
d@                 @   s>  d dl mZ dZd dlmZmZ dZdZ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G dd deZdd Zdd Zdd Zdd Zdd Zdd  Zed!krd d"lZej  nTee_ee_ee_ee_ee_esee_ ee
_ee
_ee
_ee
_ee
_es:ee
_ d"S )#    )PY3a  

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 be the SPARQL engine, but they 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

>>> foaf=Namespace('http://xmlns.com/foaf/0.1/')

>>> ~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(u'ex:j'), rdflib.term.URIRef(u'ex:g'),
    rdflib.term.URIRef(u'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(u'ex:q'), rdflib.term.URIRef(u'ex:q'))]

>>> list(evalPath(g, (None, e.p1|e.p2, e.c)))
[(rdflib.term.URIRef(u'ex:a'), rdflib.term.URIRef(u'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(u'ex:c'), rdflib.term.URIRef(u'ex:c')),
 (rdflib.term.URIRef(u'ex:a'), rdflib.term.URIRef(u'ex:c'))]

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

>>> list(evalPath(g, (None, e.p3*ZeroOrMore, e.a))) # doctest: +NORMALIZE_WHITESPACE
[(rdflib.term.URIRef(u'ex:a'), rdflib.term.URIRef(u'ex:a')),
 (rdflib.term.URIRef(u'ex:h'), rdflib.term.URIRef(u'ex:a')),
 (rdflib.term.URIRef(u'ex:g'), rdflib.term.URIRef(u'ex:a')),
 (rdflib.term.URIRef(u'ex:c'), rdflib.term.URIRef(u'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(u'ex:q'), rdflib.term.URIRef(u'ex:q'))]

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

No vars specified:

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

.. versionadded:: 4.0

)URIRefNode*+?c               @   sN   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dS )PathNc             C   s
   t  d S )N)NotImplementedError)selfgraphsubjobj r   ./tmp/pip-build-7vycvbft/rdflib/rdflib/paths.pyeval   s    z	Path.evalc             C   s   t t| S )N)hashrepr)r
   r   r   r   __hash__   s    zPath.__hash__c             C   s   t | t |kS )N)r   )r
   otherr   r   r   __eq__   s    zPath.__eq__c             C   s6   t |ttfs&tdt| t|f t| t|k S )Nzunorderable types: %s() < %s())
isinstancer   r   	TypeErrorr   )r
   r   r   r   r   __lt__   s    zPath.__lt__c             C   s6   t |ttfs&tdt| t|f t| t|kS )Nzunorderable types: %s() < %s())r   r   r   r   r   )r
   r   r   r   r   __le__   s    zPath.__le__c             C   s
   | |k S )Nr   )r
   r   r   r   r   __ne__   s    zPath.__ne__c             C   s
   | |k S )Nr   )r
   r   r   r   r   __gt__   s    zPath.__gt__c             C   s
   | |k  S )Nr   )r
   r   r   r   r   __ge__   s    zPath.__ge__)NN)__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r   r   r   r      s   
r   c               @   s.   e Zd Zdd Zd
ddZdd Zdd	 ZdS )InvPathc             C   s
   || _ d S )N)arg)r
   r!   r   r   r   __init__   s    zInvPath.__init__Nc             c   s.   x(t ||| j|fD ]\}}||fV  qW d S )N)evalPathr!   )r
   r   r   r   sor   r   r   r      s    zInvPath.evalc             C   s   d| j f S )Nz	Path(~%s))r!   )r
   r   r   r   __repr__   s    zInvPath.__repr__c             C   s   d| j j  S )Nz^%s)r!   n3)r
   r   r   r   r'      s    z
InvPath.n3)NN)r   r   r   r"   r   r&   r'   r   r   r   r   r       s   
r    c               @   s.   e Zd Zdd Zd
ddZdd Zdd	 ZdS )SequencePathc             G   s@   g | _ x4|D ],}t|tr,|  j |j 7  _ q| j j| qW d S )N)argsr   r(   append)r
   r)   ar   r   r   r"      s
    

zSequencePath.__init__Nc                sR    fdd  fdd}|r. | j ||S |r@|| j ||S  | j ||S d S )Nc             3   s   | dd  r\xzt || d d fD ]4\}}x* | dd  ||D ]}||d fV  q@W q"W n,x*t || d |fD ]\}}||fV  qrW d S )N   r   )r#   )pathsr   r   r$   r%   r)	_eval_seqr   r   r   r/     s    z$SequencePath.eval.<locals>._eval_seqc             3   s   | d d r\xzt d | d |fD ]4\}}x* | d d ||D ]}|d |fV  q@W q"W n,x*t || d |fD ]\}}||fV  qrW d S )Nr,   r   r0   r0   )r#   )r-   r   r   r$   r%   r.   )r/   r   r   r   _eval_seq_bw  s    z'SequencePath.eval.<locals>._eval_seq_bw)r)   )r
   r   r   r   r1   r   )r/   r   r   r      s    

zSequencePath.evalc             C   s   ddj dd | jD  S )NzPath(%s)z / c             s   s   | ]}t |V  qd S )N)str).0xr   r   r   	<genexpr>  s    z(SequencePath.__repr__.<locals>.<genexpr>)joinr)   )r
   r   r   r   r&     s    zSequencePath.__repr__c             C   s   dj dd | jD S )N/c             s   s   | ]}|j  V  qd S )N)r'   )r3   r+   r   r   r   r5      s    z"SequencePath.n3.<locals>.<genexpr>)r6   r)   )r
   r   r   r   r'     s    zSequencePath.n3)NN)r   r   r   r"   r   r&   r'   r   r   r   r   r(      s   
r(   c               @   s.   e Zd Zdd Zd
ddZdd Zdd	 ZdS )AlternativePathc             G   s@   g | _ x4|D ],}t|tr,|  j |j 7  _ q| j j| qW d S )N)r)   r   r8   r*   )r
   r)   r+   r   r   r   r"   $  s
    

zAlternativePath.__init__Nc             c   s4   x.| j D ]$}xt||||fD ]
}|V  qW qW d S )N)r)   r#   )r
   r   r   r   r4   yr   r   r   r   ,  s    zAlternativePath.evalc             C   s   ddj dd | jD  S )NzPath(%s)z | c             s   s   | ]}t |V  qd S )N)r2   )r3   r4   r   r   r   r5   2  s    z+AlternativePath.__repr__.<locals>.<genexpr>)r6   r)   )r
   r   r   r   r&   1  s    zAlternativePath.__repr__c             C   s   dj dd | jD S )N|c             s   s   | ]}|j  V  qd S )N)r'   )r3   r+   r   r   r   r5   5  s    z%AlternativePath.n3.<locals>.<genexpr>)r6   r)   )r
   r   r   r   r'   4  s    zAlternativePath.n3)NN)r   r   r   r"   r   r&   r'   r   r   r   r   r8   #  s   
r8   c               @   s.   e Zd Zdd ZdddZdd Zd	d
 ZdS )MulPathc             C   s^   || _ || _|tkr"d| _d| _n8|tkr8d| _d| _n"|tkrNd| _d| _ntd| d S )NTFzUnknown modifier %s)pathmod	ZeroOrOnezeromore
ZeroOrMore	OneOrMore	Exception)r
   r<   r=   r   r   r   r"   9  s    zMulPath.__init__NTc             #   s  j rD|rD|r&|r&||krD||fV  n|r6||fV  n|rD||fV  dfdd	d fdd	 fdd}t }|rx||t D ]}||kr|j| |V  qW n^|rxX ||t D ]}||kr|j| |V  qW n(x&| D ]}||kr|j| |V  qW d S )	Nc             3   sz   |j |  xjt| jd fD ]T\}}| s4||kr>||fV  jr||krNqx" |||D ]\}}||fV  q\W qW d S )N)addr#   r<   r@   )r   r   seenr$   r%   s2o2)_fwdr   r
   r   r   rH   S  s    

zMulPath.eval.<locals>._fwdc             3   sz   |j | xjtd j|fD ]T\}}|  s4| |kr>||fV  jr||krNqx" d ||D ]\}}||fV  q\W qW d S )N)rD   r#   r<   r@   )r   r   rE   r$   r%   rF   rG   )_bwdr   r
   r   r   rI   _  s    

zMulPath.eval.<locals>._bwdc              3   s   j r\t } xNjd D ]@\}}|| kr<| j| ||fV  || kr| j| ||fV  qW t }x|td jd fD ]f\}}js||fV  qv||krv|j| t |d t }x&|D ]\}}||kst||fV  qW qvW d S )N)	r?   setZsubject_objectsrD   r#   r<   r@   listAssertionError)Zseen1r$   r%   rE   fs1Zo1)rH   r   r
   r   r   _all_fwd_pathsl  s&    



z$MulPath.eval.<locals>._all_fwd_paths)NNN)NNN)r?   rJ   rD   )r
   r   r   r   firstrO   doner4   r   )rI   rH   r   r
   r   r   I  s4    




zMulPath.evalc             C   s   d| j | jf S )Nz
Path(%s%s))r<   r=   )r
   r   r   r   r&     s    zMulPath.__repr__c             C   s   d| j j | jf S )Nz%s%s)r<   r'   r=   )r
   r   r   r   r'     s    z
MulPath.n3)NNT)r   r   r   r"   r   r&   r'   r   r   r   r   r;   8  s   
Pr;   c               @   s.   e Zd Zdd Zd
ddZdd Zdd	 ZdS )NegatedPathc             C   sB   t |ttfr|g| _n&t |tr,|j| _ntdd|f  d S )Nz%Can only negate URIRefs, InvPaths or zAlternativePaths, not: %s)r   r   r    r)   r8   rC   )r
   r!   r   r   r   r"     s    


zNegatedPath.__init__Nc             c   s~   xx|j |d |fD ]d\}}}xX| jD ]D}t|tr>||krhP q$t|tr\||j|f|krhP q$td| q$W ||fV  qW d S )NzInvalid path in NegatedPath: %s)triplesr)   r   r   r    r!   rC   )r
   r   r   r   r$   pr%   r+   r   r   r   r     s    

zNegatedPath.evalc             C   s   ddj dd | jD  S )Nz
Path(! %s),c             s   s   | ]}t |V  qd S )N)r2   )r3   r4   r   r   r   r5     s    z'NegatedPath.__repr__.<locals>.<genexpr>)r6   r)   )r
   r   r   r   r&     s    zNegatedPath.__repr__c             C   s   ddj | j S )Nz!(%s)r:   )r6   r)   )r
   r   r   r   r'     s    zNegatedPath.n3)NN)r   r   r   r"   r   r&   r'   r   r   r   r   rR     s   

rR   c               @   s   e Zd ZdS )PathListN)r   r   r   r   r   r   r   rV     s   rV   c             C   s    t |ttfstdt| |S )z
    alternative path
    z&Only URIRefs or Paths can be in paths!)r   r   r   rC   r8   )r
   r   r   r   r   path_alternative  s    rW   c             C   s    t |ttfstdt| |S )z
    sequence path
    z&Only URIRefs or Paths can be in paths!)r   r   r   rC   r(   )r
   r   r   r   r   path_sequence  s    rX   c             C   s   dd | j |D S )Nc             s   s   | ]\}}}||fV  qd S )Nr   )r3   r$   rT   r%   r   r   r   r5     s    zevalPath.<locals>.<genexpr>)rS   )r   tr   r   r   r#     s    r#   c             C   s
   t | |S )z
    cardinality path
    )r;   )rT   mulr   r   r   mul_path  s    r[   c             C   s   t | S )z
    inverse path
    )r    )rT   r   r   r   inv_path  s    r\   c             C   s   t | S )z
    negated path
    )rR   )rT   r   r   r   neg_path  s    r]   __main__N)!sixr   __doc__Zrdflib.termr   r   rA   rB   r>   objectr   r    r(   r8   r;   rR   rK   rV   rW   rX   r#   r[   r\   r]   r   doctesttestmod__or____mul__
__invert____neg____truediv__Z__div__r   r   r   r   <module>   sH    : ,h 		
