
    4Ysh|              
       F   d Z ddlZddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZmZmZ ddlZddlZddlmZmZ ddlmZmZ ddlmZmZ ddlmZmZmZmZmZm Z  dd	l!m"Z" dd
l#m$Z$ ddl%m&Z& ddl'm(Z(m)Z) d Z*d Z+d Z,d Z-d Z.d Z/d Z0d Z1d Z2d Z3d Z4d Z5d Z6d Z7d Z8d Z9d Z:d Z;d Z<d  Z=d! Z>d" Z?d# Z@d$ ZAd% ZBd& ZCd' ZDd( ZEd) ZFd* ZGd+ ZHd, ZId- ZJd. ZKd/ ZLd0 ZMd1 ZNd2 ZOd3 ZPd4 ZQd5 ZRd6 ZSd7 ZTd8 ZUd9 ZVd: ZWd; ZXd< ZYd= ZZd> Z[d? Z\d@ Z]i Z^dadAZ_dadBZ`dC ZadD Zb e`ej                  dEF       e`ej                  dEF       e`ej                  dEF       e`ej                  dEF       e`ej                  dEF       e`ej                  dEF       e`ej                  dEF      dG                                                  ZidH ZjdI ZkdJ ZldK ZmdL ZndM ZodN ZpdO ZqdP ZrdQ Zs edRdS       ZtdT ZudU ZvdV ZdWej                  fdXZwdY ZcdZ Zxd[ Zyd\ Zzd] Z{d^ Z|d_ Z}d` Z~y)bz
This contains evaluation functions for expressions

They get bound as instances-methods to the CompValue objects from parserutils
using setEvalFn

    N)reduce)DecimalROUND_HALF_UPInvalidOperation)	CompValueExpr)XSD_DTstype_promotion)XSD_DateTime_DTsXSD_Duration_DTs)URIRefBNodeVariableLiteralXSDRDF)Node)quote)ParseResults)SPARQLErrorSPARQLTypeErrorc                     | j                   }t        |t              r|S t        |t              r$|j                  j                  t        |            S t        d      )z7
    http://www.w3.org/TR/sparql11-query/#func-iri
    z6IRI function only accepts URIRefs or Literals/Strings!)arg
isinstancer   r   prologue
absolutizer   exprctxas      /var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/sparql/operators.pyBuiltin_IRIr"   &   sK    
 	A!V!W||&&vay11
N
OO    c                 H    t        t        | j                  t                    S N)r   r   r   r   r   r   s     r!   Builtin_isBLANKr'   5   s    :dhh.//r#   c                 H    t        t        | j                  t                     S r%   )r   r   r   r&   s     r!   Builtin_isLITERALr)   9   s    :dhh011r#   c                 H    t        t        | j                  t                    S r%   )r   r   r   r   r&   s     r!   Builtin_isIRIr+   =   s    :dhh/00r#   c                 h    	 t        | j                         t        d      S #  t        d      cY S xY wNTF)numericr   r   r&   s     r!   Builtin_isNUMERICr/   A   s.    t}u~s   " 1c                     | j                   }|
t               S t        |t              r|j                  |   S t        d      )z9
    http://www.w3.org/TR/sparql11-query/#func-bnode
    z9BNode function only accepts no argument or literal/string)r   r   r   r   bnodesr   r   s      r!   Builtin_BNODEr2   I   s=    
 	Ayw!Wzz!}
Q
RRr#   c                 P    t        t        t        | j                                    S )z7
    http://www.w3.org/TR/sparql11-query/#func-abs
    )r   absr.   r   r&   s     r!   Builtin_ABSr5   Y   s    
 3wtxx()**r#   c                 \    t        | j                        r| j                  S | j                  S )z6
    http://www.w3.org/TR/sparql11-query/#func-if
    )EBVarg1arg2arg3r&   s     r!   
Builtin_IFr;   a   s!    
 DII4995DII5r#   c                 <    t        t        j                               S )z9
    http://www.w3.org/TR/sparql11-query/#idp2133952
    )r   randomr&   s     r!   Builtin_RANDr>   i   s    
 6==?##r#   c                 P    t        t        j                         j                        S 9
    http://www.w3.org/TR/sparql11-query/#func-strdt
    )r   uuiduuid4urnr&   s     r!   Builtin_UUIDrE   q   s    
 $**,""##r#   c                 N    t        t        t        j                                     S r@   )r   strrB   rC   r&   s     r!   Builtin_STRUUIDrH   y   s    
 3tzz|$%%r#   c                     t        | j                        j                  d      }t        t	        j
                  |      j                               S Nzutf-8)stringr   encoder   hashlibmd5	hexdigestr   r   ss      r!   Builtin_MD5rR      s7    txx(A7;;q>++-..r#   c                     t        | j                        j                  d      }t        t	        j
                  |      j                               S rJ   )rK   r   rL   r   rM   sha1rO   rP   s      r!   Builtin_SHA1rU      s7    txx(A7<<?,,.//r#   c                     t        | j                        j                  d      }t        t	        j
                  |      j                               S rJ   )rK   r   rL   r   rM   sha256rO   rP   s      r!   Builtin_SHA256rX      8    txx(A7>>!$..011r#   c                     t        | j                        j                  d      }t        t	        j
                  |      j                               S rJ   )rK   r   rL   r   rM   sha384rO   rP   s      r!   Builtin_SHA384r\      rY   r#   c                     t        | j                        j                  d      }t        t	        j
                  |      j                               S rJ   )rK   r   rL   r   rM   sha512rO   rP   s      r!   Builtin_SHA512r_      rY   r#   c                     | j                  dd      D ]  }|t        |t        t        f      r|c S  t        d      )z<
    http://www.w3.org/TR/sparql11-query/#func-coalesce
    r   T	variablesz;COALESCE got no arguments that did not evaluate to an error)getr   r   r   )r   r   xs      r!   Builtin_COALESCEre      sF     XXetX, =AX/F!GH S
TTr#   c           	          | j                   }t        t        t        j                  t        |                  |j                        S )z8
    http://www.w3.org/TR/sparql11-query/#func-ceil
    datatype)r   r   intmathceilr.   rh   r   r   l_s      r!   Builtin_CEILrn      s0    
 
B3tyy-.EEr#   c           	          | j                   }t        t        t        j                  t        |                  |j                        S )z9
    http://www.w3.org/TR/sparql11-query/#func-floor
    rg   )r   r   ri   rj   floorr.   rh   rl   s      r!   Builtin_FLOORrq      s0     
B3tzz'"+./"++FFr#   c                     | j                   }t        |      }t        t        |      j	                  dt
                    }t        ||j                        S )z9
    http://www.w3.org/TR/sparql11-query/#func-round
       rg   )r   r.   ri   r   quantizer   r   rh   )r   r   rm   vs       r!   Builtin_ROUNDrv      sB     
BAGAJ=12A1r{{++r#   c           
         t        | j                        }t        | j                        }| j                  }d}|rvt	        dt
        j                  fdt
        j                  fdt
        j                  fg      }t        t        j                  |D cg c]  }|j                  |d       c}      }t        t        t        j                  t!        |      ||                  S c c}w )a*  
    http://www.w3.org/TR/sparql11-query/#func-regex
    Invokes the XPath fn:matches function to match text against a regular
    expression pattern.
    The regular expression language is defined in XQuery 1.0 and XPath 2.0
    Functions and Operators section 7.6.1 Regular Expression Syntax
    r   irQ   m)rK   textpatternflagsdictre
IGNORECASEDOTALL	MULTILINEr   pyopor_rc   r   boolsearchrG   )r   r   rz   r{   r|   cFlagflagMapfs           r!   Builtin_REGEXr      s     $))DT\\"GJJEE bmm,sBII.>bll@STUtxxU!C'++a"3!CD4		#g,e<=>> "Ds   C#
c           
        	
 t        | j                        }t        | j                        	t        | j                        
| j                  }t        j                  dd
      
	
fd}d}|rvt        dt
        j                  fdt
        j                  fdt
        j                  fg      }t        t        j                  |D cg c]  }|j                  |d       c}      }t        j                   dd	 d
k\  rt#        
      n|}t%        t        j                  t#        	      |||      |j&                  |j(                        S c c}w ):
    http://www.w3.org/TR/sparql11-query/#func-substr
    z
\$([0-9]*)z\\\1c                 X      G  fdd      }t        j                   |             S )Nc                       e Zd Zd Z fdZy)'Builtin_REPLACE.<locals>._r.<locals>._mc                 4    || _         |j                  | _        y r%   )ry   rK   )selfry   s     r!   __init__z0Builtin_REPLACE.<locals>._r.<locals>._m.__init__   s    hhr#   c                 .    j                  |      xs dS )N )group)r   nry   s     r!   r   z-Builtin_REPLACE.<locals>._r.<locals>._m.group   s    wwqz'R'r#   N)__name__
__module____qualname__r   r   )ry   s   r!   _mr      s    '(r#   r   )r~   _expand)ry   r   r{   replacements   ` r!   _rzBuiltin_REPLACE.<locals>._r   s&    	( 	( zz'2a5+66r#   r   rx   rQ   ry   N   )      rh   lang)rK   r   r{   r   r|   r~   subr}   r   r   r   r   r   r   rc   sysversion_inforG   r   rh   language)r   r   rz   r|   r   r   r   r   compat_rr{   r   s            @@r!   Builtin_REPLACEr      s
    $((DT\\"G))*KJJE &&=K7, E bmm,sBII.>bll@STUtxxU!C'++a"3!CD $'#3#3BQ#76#As;rH
s7|XtU3]]  "Ds   E	
c                 V    t        t        | j                        | j                        S )rA   rg   )r   rG   r8   r9   r&   s     r!   Builtin_STRDTr     s    
 3tyy>DII66r#   c                     t        | j                        }|j                  s|j                  rt	        d      t        t        |      t        | j                        j                               S )z;
    http://www.w3.org/TR/sparql11-query/#func-strlang
    z STRLANG expects a simple literal)r   )	rK   r8   r   rh   r   r   rG   r9   lowerrP   s      r!   Builtin_STRLANGr     sO    
 	tyyAzzQZZ<== 3q6DII 4 4 677r#   c                 N   t        d | j                  D              }t        |      dk(  r|j                         nd}t        d | j                  D              }t        |      dk(  r|j                         nd}t	        dj                  d | j                  D              ||      S )z:
    http://www.w3.org/TR/sparql11-query/#func-concat
    c              3   4   K   | ]  }|j                     y wr%   rg   .0rd   s     r!   	<genexpr>z!Builtin_CONCAT.<locals>.<genexpr>.  s     *AQZZ*   rs   Nc              3   4   K   | ]  }|j                     y wr%   )r   r   s     r!   r   z!Builtin_CONCAT.<locals>.<genexpr>1  s     ,aqzz,r   r   c              3   2   K   | ]  }t        |        y wr%   )rK   r   s     r!   r   z!Builtin_CONCAT.<locals>.<genexpr>4  s     76!97   r   )setr   lenpopr   join)r   r   dtr   s       r!   Builtin_CONCATr   '  s|     
**	*BW\tB,488,,DTa488:TD2777dhh77"4PPr#   c                     t        |        t        |       |j                  r%| j                  |j                  k7  rt        d      y y )Nz'incompatible arguments to str functions)rK   r   r   )r    bs     r!   _compatibleStringsr   7  s9    
1I
1IzzajjAJJ.CDD /zr#   c                 ~    | j                   }| j                  }t        ||       t        |j	                  |            S )z=
    http://www.w3.org/TR/sparql11-query/#func-strstarts
    )r8   r9   r   r   
startswithr   r   r    r   s       r!   Builtin_STRSTARTSr   ?  s3    
 			A		Aq!1<<?##r#   c                 ~    | j                   }| j                  }t        ||       t        |j	                  |            S )z;
    http://www.w3.org/TR/sparql11-query/#func-strends
    )r8   r9   r   r   endswithr   s       r!   Builtin_STRENDSr   K  s3     			A		Aq!1::a=!!r#   c                     | j                   }| j                  }t        ||       |j                  |      }|dk(  rt	        d      S t	        |d| |j
                  |j                        S )z=
    http://www.w3.org/TR/sparql11-query/#func-strbefore
    r   Nr   rh   )r8   r9   r   findr   r   rh   r   r   r    r   rx   s        r!   Builtin_STRBEFOREr   W  s[    
 			A		Aq!	q	ABwr{q!u1::

CCr#   c                     | j                   }| j                  }t        ||       |j                  |      }|dk(  rt	        d      S t	        ||t        |      z   d |j                  |j                        S )z<
    http://www.w3.org/TR/sparql11-query/#func-strafter
    r   r   Nr   )r8   r9   r   r   r   r   r   rh   r   s        r!   Builtin_STRAFTERr   g  sc    
 			A		Aq!	q	ABwr{qSVQZZ!**MMr#   c                 d    | j                   }| j                  }t        ||       t        ||v       S )z?
    http://www.w3.org/TR/sparql11-query/#func-strcontains
    )r8   r9   r   r   r   s       r!   Builtin_CONTAINSr   w  s.    
 			A		Aq!16?r#   c                 n    t        t        t        | j                        j	                  d                  S rJ   )r   r   rK   r   rL   r&   s     r!   Builtin_ENCODE_FOR_URIr     s&    5)009:;;r#   c                     t        | j                        }t        | j                        dz
  }| j                  }|t        |      |z   }t        ||| |j                  |j                        S )r   rs   r   )rK   r   r.   startlengthr   r   rh   )r   r   r    r   r   s        r!   Builtin_SUBSTRr     s_    
 	txxADJJ!#E[[F5(1U6?ajjIIr#   c                 T    t        | j                        }t        t        |            S r%   )rK   r   r   r   er   rm   s      r!   Builtin_STRLENr     s    	B3r7r#   c                 f    | j                   }t        |t              r|t        t	        |            S r%   )r   r   r   r   rG   )r   r   r   s      r!   Builtin_STRr     s)    
%%C#{#	3s8r#   c                     t        | j                        }t        |j                         |j                  |j
                        S Nr   )rK   r   r   r   rh   r   r   s      r!   Builtin_LCASEr     ,    	B288:"++FFr#   c                     t        | j                        }t        | j                        }t        |      dk(  rt	        d      S t	        t        ||            S )zA
    http://www.w3.org/TR/sparql11-query/#func-langMatches


    r   F)rK   r8   r9   rG   r   _lang_range_check)r   r   langTag	langRanges       r!   Builtin_LANGMATCHESr     sG     QVVnGqvvI
7|ru~$Y899r#   c                 ,    t        |j                        S )z7
    http://www.w3.org/TR/sparql11-query/#func-now
    )r   now)r   r   s     r!   Builtin_NOWr     s     377r#   c                 V    t        | j                        }t        |j                        S r%   )dater   r   yearr   r   ds      r!   Builtin_YEARr     s    QUUA166?r#   c                 V    t        | j                        }t        |j                        S r%   )r   r   r   monthr   s      r!   Builtin_MONTHr     s    QUUA177r#   c                 V    t        | j                        }t        |j                        S r%   )r   r   r   dayr   s      r!   Builtin_DAYr     s    QUUA155>r#   c                 V    t        | j                        }t        |j                        S r%   )datetimer   r   hourr   s      r!   Builtin_HOURSr     s    A166?r#   c                 V    t        | j                        }t        |j                        S r%   )r   r   r   minuter   s      r!   Builtin_MINUTESr     s    A188r#   c                 v    t        | j                        }t        |j                  t        j
                        S )z;
    http://www.w3.org/TR/sparql11-query/#func-seconds
    rg   )r   r   r   secondr   decimalr   s      r!   Builtin_SECONDSr     s&     	A188ckk22r#   c                    t        | j                        }|j                  st        d|z        |j	                         }|j
                  }|j                  }d}|dk  rd|z  |z
  }d}d}|dz  }||dz  dz  z
  dz  }||dz  dz  z
  |dz  z
  }|d|rd	|z  ndd
|rd|z  nd|rd|z  nd|s	|s|sd|z  nd}	t        |	t        j                        S )z
    http://www.w3.org/TR/sparql11-query/#func-timezone

    :returns: the timezone part of arg as an xsd:dayTimeDuration.
    :raises: an error if there is no timezone.
    zdatatime has no timezone: %rr   r   i-i  <   Pz%dDTz%dHz%dMz%dSrg   )
r   r   tzinfor   	utcoffsetdayssecondsr   r   dayTimeDuration)
r   r   r   deltar   rQ   neghry   tzdeltas
             r!   Builtin_TIMEZONEr    s    
!%%B9982=>>LLNE

AA
C1uA!	WA	
QVb[BA	AFRK!b& A 		B	B	B1Q	B6G 7S%8%899r#   c                     t        | j                        }|j                  st        d      S |j                  j	                  |      }|dk(  rd}t        |      S )Nr   UTCZ)r   r   r  r   tzname)r   r   r   r   s       r!   
Builtin_TZr    sH    A88r{	AEz1:r#   c                     t        | j                        }t        |j                         |j                  |j
                        S r   )rK   r   r   upperrh   r   r   s      r!   Builtin_UCASEr    r   r#   c                 ^    t        | j                        }t        |j                  xs d      S )z
    http://www.w3.org/TR/sparql11-query/#func-lang

    Returns the language tag of ltrl, if it has one. It returns "" if ltrl has
    no language tag. Note that the RDF data model does not include literals
    with an empty language tag.
    r   )literalr   r   r   r   s      r!   Builtin_LANGr    s%     
B2;;$"%%r#   c                     | j                   }t        |t              st        d|z        |j                  rt
        j                  S |j                  s|j                  st        j                  S |j                  S )Nz$Can only get datatype of literal: %r)
r   r   r   r   r   r   
langStringrh   r   rK   r   s      r!   Builtin_DATATYPEr  #  sX    	
Bb'"@2EFF	{{~~;;r{{zz;;r#   c                 N    | j                   }| j                  }t        ||k(        S r%   )r8   r9   r   )r   r   r    r   s       r!   Builtin_sameTermr  .  s"    	A	A16?r#   c                 \    | j                  dd      }t        t        |t                     S )z9
    http://www.w3.org/TR/sparql11-query/#func-bound
    r   Tra   )rc   r   r   r   )r   r   r   s      r!   Builtin_BOUNDr  4  s+     	
et$Az!X..//r#   c                     ddl m} | j                  dk(  }|j                  j	                  |      } ||| j
                        D ]  }t        |      c S  t        |       S )Nr   )evalPartBuiltin_EXISTS)rdflib.plugins.sparql.evaluater!  namer   thawgraphr   )r   r   r!  existsrd   s        r!   r"  r"  =  sW    7VV''F
'',,s
Cc177# vv:r#   c                 f    |s$| t         v rt        d| j                         z        ||ft         | <   y)a  
    Register a custom SPARQL function.

    By default, the function will be passed the RDF terms in the argument list.
    If raw is True, the function will be passed an Expression and a Context.

    The function must return an RDF term, or raise a SparqlError.
    z&A function is already registered as %sN)_CUSTOM_FUNCTIONS
ValueErrorn3)urifuncoverrideraws       r!   register_custom_functionr0  L  s5     00ACFFHLMM"C[cr#   c                       fd}|S )z@
    Decorator version of :func:`register_custom_function`.
    c                 &    t        |        | S )N)r.  r/  )r0  )r-  r.  r/  r,  s    r!   	decoratorz"custom_function.<locals>.decorator_  s     dX3Gr#    )r,  r.  r/  r3  s   ``` r!   custom_functionr5  Z  s    
 r#   c                     t         j                  | d      d   |k7  rt        d| j                         z        t         | = y )N)NNr   z%This function is not registered as %s)r)  rc   r*  r+  )r,  r-  s     r!   unregister_custom_functionr7  f  s;    S,/2d:@3668KLL#r#   c                     t         j                  | j                        }|t        d| j                  z        |\  }}|r	 || |      S 	  || j                   S # t
        $ r}t        |j                   d}~ww xY w)z$
    Custom functions and casts
    NzUnknown function %r)r)  rc   irir   r   	TypeErrorargs)r   r   pairr-  r/  exs         r!   Functionr>  l  s|       'D|/!%%788ID#
As|	(=  	(rww''	(s   A 	A:#A55A:T)r/  c                    | j                   st        d      t        | j                         dkD  rt        d      | j                   d   }| j                  t        j
                  k(  rKt        |t        t        f      rt        |t        j
                        S t        d|dt        |            t        |t              st        d      |j                  r*|j                  t        vrt        d	|j                  z        | j                  t        j                  k(  r{|j                  rD|j                  t        j                  t        j
                  fvrt        d
|j                  z        	 t        t        j                  |      | j                        S |j                  t        j                  k(  rt        d| j                  z        | j                  t        j                  t        j                   fv r!	 t        t        |      | j                        S | j                  t        j"                  k(  r7d|v sd|v rt        d|z        	 t        t%        |      | j                        S | j                  t        j&                  k(  r%	 t        t)        |      t        j&                        S | j                  t        j*                  k(  rH|j-                         dv rt        d      S |j-                         dv rt        d      S t        d|z        y #  t        d|z        xY w#  t        d|z        xY w#  t        d|z        xY w#  t        d|z        xY w)NzNothing given to cast.rs   z Cannot cast more than one thing!r   rg   zCannot cast term z	 of type z/Can only cast Literals to non-string data-typesz-Cannot cast literal with unknown datatype: %rzCannot cast %r to XSD:dateTimez!Cannot interpret '%r' as datetimezCannot cast XSD.dateTime to %rzCannot interpret '%r' as floatr   Ez Cannot interpret '%r' as decimalzCannot interpret '%r' as int)1trueT)0falseFzCannot interpret '%r' as bool)r   r   r   r9  r   rK   r   r   r   typerh   r	   dateTimeisodateparse_datetimefloatdoubler   r   integerri   booleanr   )r   r   rd   s      r!   default_castrM    s    66233
166{Q<==	q	Auu

a&'*+1szz22!T!WMNNa!KLLzz!**/IAJJVWWuu::!**S\\3::,FF>KLL	G711!4quuEE 	zzS\\!:QUUBCCuuCJJ''	D58aee44 
#++	!8sax@1DEE	F71:66 
#++		B3q6CKK88 
#++	
 779%4= 779&5>!9A=>> 
5	GAAEFF	D>BCC	F@1DEE
	B<q@AAs0   1)L =L, L? #M L),L<?MM"c                 @    t        t        | j                               S r%   )r   r7   r   r&   s     r!   UnaryNotrO    s    s499~%&&r#   c                 @    t        t        | j                               S r%   r   r.   r   r&   s     r!   
UnaryMinusrR        GDII&&''r#   c                 @    t        t        | j                              S r%   rQ  r&   s     r!   	UnaryPlusrU    rS  r#   c                 d   | j                   }| j                  }||S 	 t        t        |            }t	        | j
                  |      D ]=  \  }}t        |      }t        |      t        k(  rt        |      }|dk(  r||z  }9||z  }? 	 t        |      S # t        t        f$ r t        d      w xY w)N*zdivide by 0)r   otherr   r.   zipoprE  rI  r   ZeroDivisionErrorr   r   )r   r   r   rX  resrZ  r   s          r!   MultiplicativeExpressionr]    s    66DGGE })gdm$u% 		EB
AAw%CjSyqq		 3< /0 )-(()s   A*B B/c                 ~   | j                   }| j                  }||S t        |d      rL|j                  t        v s|j                  t
        v r't        |      }|j                  }t        | j                  |      D ]  \  }}|t        v rU||j                  k(  rF|dk(  rAt        |      dkD  rd}t        ||j                        t        |      }	t        ||	      }|c S |t        v r:|j                  t
        v r(t        |      }	t        |||	|j                  |      }|c S |t
        v r@|j                  t        v r.|dk(  st        |      }	t        |||	|j                  |      }|c S t        d       y t        |      }|j                  }t        | j                  |      D ]  \  }}t        |      }	t        |	t              rt        |t               rt!        |	      }	t        |	t               rt        |t              rt!        |      }t#        ||j                        }|dk(  r||	z  }||	z  } t%        ||      S )Nrh   r   rs   z$Can't evaluate multiple %r arguments+zInvalid DateTime Operationsrg   )r   rX  hasattrrh   r   r   dateTimeObjectsrY  rZ  r   r   calculateDurationcalculateFinalDateTimer.   r   r   rI  r
   r   )
r   r   r   rX  r\  r   rZ  termerror_messager   s
             r!   AdditiveExpressionrf    s   66DGGE } tZ ))T]]>N-N d#]]ADD%( 	AHB %%"*=") u:>$JM%mR[[AA'-A+C3CJ ''DMM=M,M#D),S"aK
 ''DMM=M,M9'-A0b!T]]BOCJ ""?@@?	AF dm]]ADD%( 	HBA!W%*S%*@!H!U#
3(@CjDMM2BSyqq	 sR((r#   c                     | j                   }| j                  }| j                  }||S t        dd fdd fdd fdd fd	d
 fdd fdt        j
                  fdd fg      }|dv rK|dk(  }d}|t        j                  k(  rg }|D ]  }	 ||k(  rt        d|z        c S  |st        d|z        S ||dvr<t        |t              st        d|z        t        |t              sJt        d|z        t        |t              st        d|z        t        |t              st        d|z        t        |t              r[t        |t              rK|j                  ?|j                  t        vr-|j                  !|j                  t        vr|dvrt        d      	  ||   ||      }	|	t        k(  rt        d      	 t        |	      S # t        $ r} | }Y d } ~ Rd } ~ ww xY w# t        $ r}
t        |
j                    d }
~
ww xY w)N>c                 $    | j                  |      S r%   )__gt__rd   ys     r!   <lambda>z&RelationalExpression.<locals>.<lambda>G      qxx{ r#   <c                 $    | j                  |      S r%   )__lt__rk  s     r!   rm  z&RelationalExpression.<locals>.<lambda>H  rn  r#   =c                 $    | j                  |      S r%   )eqrk  s     r!   rm  z&RelationalExpression.<locals>.<lambda>I  s    qttAw r#   !=c                 $    | j                  |      S r%   )neqrk  s     r!   rm  z&RelationalExpression.<locals>.<lambda>J  s    a r#   z>=c                 $    | j                  |      S r%   )__ge__rk  s     r!   rm  z&RelationalExpression.<locals>.<lambda>K       r#   z<=c                 $    | j                  |      S r%   )__le__rk  s     r!   rm  z&RelationalExpression.<locals>.<lambda>L  rz  r#   INNOT INc                 0    t        j                  | |       S r%   )r   containsrk  s     r!   rm  z&RelationalExpression.<locals>.<lambda>N  s    a(;$; r#   )r}  r~  FT)rr  ru  r}  r~  z8Compare other than =, != of non-literals is an error: %rz"I cannot compare this non-node: %r)rr  ru  z0Can only do =,!= comparisons of non-XSD LiteralszError when comparing)r   rX  rZ  r}   r   r  r   nilr   r   r   r   rh   r	   NotImplementedr:  r;  )r   r   r   rX  rZ  opsr\  errorrd   rtes              r!   RelationalExpressionr  :  sB   66DGGE	
B }
*+*+&'()+,+,4==!;<		
C 
HnCGGE 	A9"4#:.. 	 53;''K	,,$(JTQ  %)JUR  $%BTIJJ%&BUJKK$ Zw%? MM%W,*g- $!"TUU$CGD% 455  1:Q  L  $277##$s0   G G 	GGG	G=&G88G=c                 x    | j                   }| j                  }||S t        t        d |g|z   D                    S )Nc              3   2   K   | ]  }t        |        y wr%   )r7   r   s     r!   r   z+ConditionalAndExpression.<locals>.<genexpr>  s     6!s1v6r   )r   rX  r   all)r   r   r   rX  s       r!   ConditionalAndExpressionr    s>     66DGGE }36v~6677r#   c                     | j                   }| j                  }||S d }|g|z   D ]  }	 t        |      rt        d      c S  |r|t        d      S # t        $ r} | }Y d } ~ =d } ~ ww xY wr-   )r   rX  r7   r   r   )r   r   r   rX  r  rd   s         r!   ConditionalOrExpressionr    s     66DGGE } EVe^ 	1vt}$  5>	  	E	s   A	A'A""A'c                 &    t        dt        |       S )NrO  r   )r   rO  )r   s    r!   not_r    s    
H3//r#   c            
      l    t        |       dk(  r| d   S t        dt        | d   t        | dd              S )Nrs   r   r  )r   rX  )r   r   r  list)r;  s    r!   and_r    s=    
4yA~Aw" !W48n	 r#   
TrueFilterc                     t        d      S )NT)r   )_1_2s     r!   rm  rm    s
    wt} r#   c                    t        | t              rt        |       dk(  rt        | d         S t        | t        t        f      rt	        t        t        |             S t        | t              s| S | j                  j                  d      r!| j                  t        | j                        S | j                         D ]  }t        | |         | |<    | S )Nrs   r   
Expression)r   r   r   simplifyr  mapr   r$  r   rX  r   keys)r   ks     r!   r  r    s    $%#d)q.Q  $|,-C$'((dI&yy,'::DII&&YY[ $47#Q$
 Kr#   c                 B    t        | t              st        d| z        | S )Nz Non-literal passed as string: %r)r   r   r   rQ   s    r!   r  r    s"    a!<q@AAHr#   c                     t        | t              st        d| z        | j                  t        j
                  k(  st        d| z        | j                         S )Nz"Non-literal passed as datetime: %rz2Literal with wrong datatype passed as datetime: %r)r   r   r   rh   r   rF  toPython)r   s    r!   r   r     sK    a!>BCC::%NQRRSS::<r#   returnc                 *   t        | t              st        d| z        | j                  t        j
                  t        j                  fvrt        d| z        | j                         }t        |t        j                        r|j                         S |S )NzNon-literal passed as date: %rz.Literal with wrong datatype passed as date: %r)
r   r   r   rh   r   r   rF  r  py_datetimer   )r   results     r!   r   r     ss    a!:Q>??zz#((CLL11JQNOOZZ\F&+../{{}Mr#   c                     t        | t              st        d| z        | j                  r+| j                  t        j
                  k7  rt        d| z        | S )zz
    Make sure the passed thing is a string literal
    i.e. plain literal, xsd:string literal or lang-tagged literal
    z Non-literal passes as string: %rz0Non-string datatype-literal passes as string: %r)r   r   r   rh   r   rK   r  s    r!   rK   rK     sK    
 a!<q@AAzzajjCJJ.LqPQQHr#   c                 v   t        | t              st        d| z        | j                  t        j
                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                   t        j"                  t        j$                  t        j&                  t        j(                  fvrt        d| z        | j+                         S )zf
    return a number from a literal
    http://www.w3.org/TR/xpath20/#promotion

    or TypeError
    z%r is not a literal!z$%r does not have a numeric datatype!)r   r   r   rh   r   rI  rJ  r   rK  nonPositiveIntegernegativeIntegernonNegativeIntegerpositiveIntegerunsignedLongunsignedIntunsignedShortunsignedBytelongri   shortbyter  r  s    r!   r.   r.     s     dG$4t;<<}}		

		! $ DtKLL==?r#   c                 "    | j                         S )zo
    return a dataTime/date/time/duration/dayTimeDuration/yearMonthDuration python objects from a literal

    )r  r  s    r!   ra  ra  !  s    
 ==?r#   c                    |t         j                  k(  rI|t         j                  k(  ry|t         j                  k(  s|t         j                  k(  rdt        |      v ryy|t         j                  k(  rM|t         j                  k(  ry|t         j                  k(  s|t         j                  k(  rdt        |      d   k(  ryy|t         j                  k(  ryy)zq
    Returns a boolean indicating if first object is compatible
    with operation(+/-) over second object.

    Tr  Frs   N)r   r   yearMonthDurationr  DurationrG   timerF  )obj1dt1obj2dt2s       r!   isCompatibleDateTimeDatatyper  )  s     chh#'''C'''3#,,+> c$i
chh#'''C'''3#,,+> c$il"
cll r#   c                 J    | }|}||z
  }t        |t        j                        S )z<
    returns the duration Literal between two datetime

    rg   )r   r   duration)r  r  date1date2
differences        r!   rb  rb  K  s(    
 EEJ:55r#   c                     t        | |||      r)|dk(  r| |z
  }t        ||      S | |z   }t        ||      S t        d      )z
    Calculates the final dateTime/date/time resultant after addition/
    subtraction of duration/dayTimeDuration/yearMonthDuration
    r   rg   z.Incompatible Data types to DateTime Operations)r  r   r   )r  r  r  r  	operationanss         r!   rc  rc  V  sT     $D#tS9+C3--+C3-- JKKr#   c                 x   t        | t              r| j                  t        j                  k(  r| j                         S | j                  t        j                  k(  s| j                  t        |       dkD  S | j                         }t        |t              rt        d| z        t        |      S t        d| z        )a  
    Effective Boolean Value (EBV)

    * If the argument is a typed literal with a datatype of xsd:boolean,
      the EBV is the value of that argument.
    * If the argument is a plain literal or a typed literal with a
      datatype of xsd:string, the EBV is false if the operand value
      has zero length; otherwise the EBV is true.
    * If the argument is a numeric type or a typed literal with a datatype
      derived from a numeric type, the EBV is false if the operand value is
      NaN or is numerically equal to zero; otherwise the EBV is true.
    * All other arguments, including unbound arguments, produce a type error.

    r   zjhttp://www.w3.org/TR/rdf-sparql-query/#ebv - ' +                     'Could not determine the EBV for : %rzchttp://www.w3.org/TR/rdf-sparql-query/#ebv - ' +             'Only literals have Boolean values! %r)
r   r   rh   r   rL  r  rK   r   r   r   )rtpyRTs     r!   r7   r7   j  s      "g;;#++%;;= [[CJJ&"++*=r7Q; ;;=D$(%;  Dz! 4
 	
r#   c                 J   d | j                         j                         j                  d      }|j                         j                         j                  d      } |d   |d         syt        |      t        |      kD  ryt	        fdt        ||      D              S )a(  
    Implementation of the extended filtering algorithm, as defined in point
    3.3.2, of U{RFC 4647<http://www.rfc-editor.org/rfc/rfc4647.txt>}, on
    matching language ranges and language tags.
    Needed to handle the C{rdf:PlainLiteral} datatype.
    @param range: language range
    @param lang: language tag
    @rtype: boolean

        @author: U{Ivan Herman<a href="http://www.w3.org/People/Ivan/">}

        Taken from `RDFClosure/RestrictedDatatype.py`__

    .. __:http://dev.w3.org/2004/PythonLib-IH/RDFClosure/RestrictedDatatype.py

    c                     | dk(  xs | |k(  S )z
        Matching of a range and language item: either range is a wildcard
        or the two are equal
        @param r: language range item
        @param l_: language tag item
        @rtype: boolean
        rW  r4  )r  rm   s     r!   _matchz!_lang_range_check.<locals>._match  s     Cx"17"r#   r   r   Fc              3   (   K   | ]	  } |   y wr%   r4  )r   rd   r  s     r!   r   z$_lang_range_check.<locals>.<genexpr>  s     <avqz<s   )stripr   splitr   r  rY  )ranger   	rangeListlangListr  s       @r!   r   r     s    $# ##%++C0Izz|!!#))#.H)A,,
9~H%<3y(#;<<<r#   )FF)__doc__r   r~   rj   r=   rB   rM   r   r  	functoolsr   r   r   r   r   operatorr   rG  !rdflib.plugins.sparql.parserutilsr   r   rdflib.plugins.sparql.datatypesr	   r
   r   r   rdflibr   r   r   r   r   r   rdflib.termr   urllib.parser   	pyparsingr   rdflib.plugins.sparql.sparqlr   r   r"   r'   r)   r+   r/   r2   r5   r;   r>   rE   rH   rR   rU   rX   r\   r_   re   rn   rq   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r"  r)  r0  r5  r7  r>  rK   rF  rI  rJ  rK  rL  rM  rO  rR  rU  r]  rf  r  r  r  r  r  r  r  r  r   r.   ra  r  rb  rc  r7   r   r4  r#   r!   <module>r     s    	       < <   = C N = =   " EP021S +6$$&/
0
2
2
2
UFG,?.3l78Q E	$	"D N 	<J G:




3":JG
&0	  )	(* &4(%&$'$'$'=? ( ( ( ' & ) '=?@'((6H)VM`840	 , <=
({ 	DD6L(*
Z#=r#   