
    4Ysh?                        d Z ddlZddlZddlZddlmZmZ ddlmZ ddl	Z
ddlmZ ddlmZmZmZmZmZ ddlmZ ddlmZ dd	lmZmZmZmZmZ dd
lmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% ddl&m'Z' ddlm(Z( d Z)d Z*d Z+d Z,d Z-d Z.d Z/d Z0d Z1d Z2d Z3d Z4d Z5	 d Z6d Z7d Z8d Z9d Z:d Z;d  Z<d! Z=d" Z>d# Z?d$ Z@d% ZAd'd&ZBy)(a  
These method recursively evaluate the SPARQL Algebra

evalQuery is the entry-point, it will setup context and
return the SPARQLResult object

evalPart is called on each level and will delegate to the right method

A rdflib.plugins.sparql.sparql.QueryContext is passed along, keeping
information needed for evaluation

A list of dicts (solution mappings) is returned, apart from GroupBy which may
also return a dict of list of dicts

    N)urlopenRequest)	urlencode)ParseException)VariableGraphBNodeURIRefLiteral)CUSTOM_EVALS)value)QueryContextAlreadyBoundFrozenBindingsBindingsSPARQLError)_filter_eval_join_diff_minus_fillTemplate_ebv_val)
Aggregator)parserc              #     K   |s| j                          y|d   \  }}}| |   }| |   }| |   }| j                  j                  |||f      D ]O  \  }}	}
d|||fv r| j                         }n| }||||<   	 ||	||<   	 ||
||<   t        ||dd       D ]  }|  Q y# t        $ r Y ^w xY w# t        $ r Y mw xY ww)z
    A basic graph pattern
    Nr      )solutiongraphtriplespushr   evalBGP)ctxbgpspo_s_p_ossspsocxs                ~/var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/sparql/evaluate.pyr#   r#   2   s	    
 lln!fGAq!	QB	QB	QBii''R5 
BBB<
AA:AaD	z!	z! CG$ 	AG	+  		  		sH   A4B?7B!?B0B?!	B-*B?,B--B?0	B<9B?;B<<B?c              #   .  K   t        | |j                        D ]e  }	 t        |j                  |j	                  | |j
                              }t        |t              r||j                  |j                  |i       g y # t        $ r | Y xw xY wwN)_except)
evalPartr'   r   exprforget_vars
isinstancer   mergevar)r$   extendr/   es       r1   
evalExtendr>   Z   s      c688$ 		fkk188C8#FGA![)''6::q/**	  	G	s)   BA"B?BBBBBc              #      K   t        | |j                        D ]A  }| j                  |      }t        ||j                        D ]  }|j	                  |        C yw)z
    A lazy join will push the variables bound
    in the first part to the second part,
    essentially doing the join implicitly
    hopefully evaluating much fewer triples
    N)r5   p1thawp2r:   )r$   joinar/   bs        r1   evalLazyJoinrF   i   sW      c477# HHQK!TWW% 	A''!*	s   AAc                     |j                   rt        | |      S t        | |j                        }t	        t        | |j
                              }t        ||      S N)lazyrF   r5   r@   setrB   r   )r$   rC   rD   rE   s       r1   evalJoinrK   v   sI    
 yyC&&S$''"dgg&'Q{    c                     g }t        | |j                        D ]  }|j                  |        t        | |j                        D ]  }|j                  |        |S rH   )r5   r@   appendrB   )r$   unionbranch1_branch2r0   s       r1   	evalUnionrQ      s]    Oc588$ "q!"c588$ "q!"rL   c                     t        | |j                        }t        t        | |j                              }t	        ||      S rH   )r5   r@   rJ   rB   r   )r$   minusrD   rE   s       r1   	evalMinusrT      s3    ehhAHS%((#$A!Q<rL   c              #     K   t        | j                        D ]  }d}| j                  |      }t        |j                        D ].  }t	        j
                  |j                  |             s)d}| 0 |r`j                  j                  }|Gt        fdt        | j                  |j                  |            j                        D              r|  y w)NFTc              3   J   K   | ]  }t        j                  |        y wrH   )r   r6   ).0rE   rC   s     r1   	<genexpr>zevalLeftJoin.<locals>.<genexpr>   s$      * TYY"*s    #)
r5   r@   rA   rB   r   r6   r7   r8   anyremember)r$   rC   rD   okr/   rE   p1_varss    `     r1   evalLeftJoinr]      s     c477# HHQK!TWW% 	ADIIqxx}-	  ggmmGc *!#((1::g+>"?I* '
 's   A+C /
C :AC C c              #      K   t        | |j                        D ]F  }t        |j                  |j                  s|j                  | |j                        n|      sC| H y wr3   )r5   r'   r   r6   no_isolated_scoper7   r8   )r$   partr/   s      r1   
evalFilterra      sU     c466" II595K5KAHHS$**H-QR
 Gs   AA#A#c              #     K   | j                   t        d      | j                         } | |j                     }| j                  }|| j                   j                         D ]  }|| j                   j                  k(  r| j                  |      }|j                         }|j                  |j                  ig}t        t        ||j                        |      D ]  }||j                  _        |   y | j                  | j                   j                  |            }t        ||j                        D ]  }||j                  _        |  y w)NzKNon-conjunctive-graph doesn't know about graphs. Try a query without GRAPH.)dataset	Exceptionclonetermr    contextsdefault_context	pushGraphr"   
identifierr   r5   r'   r$   get_context)r$   r`   r    
prev_graphr/   graphSolutionr0   s          r1   	evalGraphrn      s'    
{{3
 	

 ))+C		NEJ}[[))+ 	E 333e$AA"ii)9)9:;M8Atvv.> (	 MM#++11%89!TVV$ 	A$AEEKG	s   EEc              #      K   |j                   j                  D ]I  }| j                         }	 |j                         D ]  \  }}|dk7  s|||<    	 |j                          K y # t        $ r Y Xw xY ww)NUNDEF)r'   resr"   itemsr   r   )r$   r`   rr/   kvs         r1   
evalValuesrv      sw     VVZZ 	HHJ		 1<AaD jjl	  		s.   *A5A&	A&A5&	A2/A51A22A5c                 x    |j                   j                  dk(  rt        | |      S t        | |j                         S )Nvalues)r'   namerv   r5   )r$   r`   s     r1   evalMultisetrz      s0    vv{{h#t$$C  rL   c                 B    t        j                         D ]  \  }}	  | |      c S  |j                  dk(  r&t	        |j
                   fd      }t         |      S |j                  dk(  rt         |      S |j                  dk(  rt         |      S |j                  dk(  rt         |      S |j                  dk(  rt         |      S |j                  dk(  rt         |      S |j                  d	k(  rt         |      S |j                  d
k(  rt         |      S |j                  dk(  rt         |      S |j                  dk(  rt         |      S |j                  dk(  rt!         |      S |j                  dk(  rt#         |      S |j                  dk(  rt%         |      S |j                  dk(  rt'         |      S |j                  dk(  rt)         |      S |j                  dk(  rt+         |      S |j                  dk(  rt-         |      S |j                  dk(  rt/         |      S |j                  dk(  rt1         |      S |j                  dk(  rt3         |      S |j                  dk(  rt5        d      t5        d|j                  z        # t        $ r Y w xY w)NBGPc                 L    t        | D cg c]
  }|   	| c}      S c c}w rH   )len)tnr$   s     r1   <lambda>zevalPart.<locals>.<lambda>   s"    ,L1SV^Q,L(M ,Ls   
!!)keyFilterJoinLeftJoinr   Union
ToMultiSetExtendMinusProjectSliceDistinctReducedOrderByGroupAggregateJoinSelectQueryAskQueryConstructQueryServiceGraphPatternDescribeQueryzDESCRIBE not implementedzI dont know: %s)r   rr   NotImplementedErrorry   sortedr!   r#   ra   rK   r]   rn   rQ   rz   r>   rT   evalProject	evalSliceevalDistinctevalReducedevalOrderBy	evalGroupevalAggregateJoinevalSelectQueryevalAskQueryevalConstructQueryevalServiceQueryrd   )r$   r`   ry   r/   r!   s   `    r1   r5   r5      s     %%' a	S$< yyE LLM
 sG$$	h	#t$$	f	T""	j	 C&&	g	d##	g	d##	l	"C&&	h	#t$$	g	d##	i	3%%	g	d##	j	 C&&	i	3%%	i	3%%	g	d##	o	% d++	m	#sD))	j	 C&&	&	&!#t,,	+	+T** 
o	%233 )DII566w # 		s   J	JJc              #     K   i }t        j                  d|j                  dd      t         j                  t         j                  z        }|r|j                  d      }t        | |      }|dd}ddd	}t        |      d
k  r&t        t        |dz   t        |      z   |            }n.t        t        |t        |      j                         |            }|j                  dk(  ret        j                  |j                               }	|	d   d   x}
|d<   |	d   d   }t        |      dkD  r|D ]  }t!        | ||
      D ]  }|   y y t#        d||j                        y w)Nz ^service <(.*)>[ 
]*{(.*)}[ 
]*$service_string r   json)queryoutputzapplication/sparql-results+jsonrdflibForAnUser)acceptz
user-agentiX  ?)headers)datar      headvarsvars_resultsbindingsr   z#Service: %s responded with code: %s)rematchgetDOTALLIgroup_buildQueryStringForServiceCallr~   r   r   r   encodestatusjloadsread#_yieldBindingsFromServiceCallResultrd   )r$   r`   rq   r   service_urlservice_queryquery_settingsr   responser   	variablesrs   bounds                r1   r   r   .  sp    
CHH,!2&
		BDDE kk!n7UC#0FC7+

 }#c)In,EEwWH ">299;#H ??c!778==?+D'+F|F';;IGy/*-C3x!| $A!DS!Y!W $#$$ 
 5{HOO A s   E!E#c                    |j                  d      }	 t        j                  |       | j                         }t        |      dkD  rzdj                  |D cg c]  }|j                          c}      }dj                  |D cg c]!  }| j                  |      j                         # c}      }|dz   |z   dz   |z   dz   }|S # t        $ r d|z   dz   }| j                  j
                  j                  j                         D ]'  }d|d   z   dz   |d   j                         z   dz   |z   }) | j                  j                  }|t        |      dkD  rd	|z   d
z   |z   }Y 7w xY wc c}w c c}w )N   zSELECT REDUCED * WHERE {}zPREFIX r   :r    zBASE <z> zVALUES (z) {(z)})r   r   
parseQueryr   prologuenamespace_managerstore
namespacesn3baser~   r   rC   r   )	r$   r   r   r'   r   solru   r   variables_bounds	            r1   r   r   c  s_   KKNM
C-( ,,.C
3x!|HHc2addf23	((S#ACGGAJMMO#ABJ&2V;oMPTT 	 !  C2]BSH//55@@B 	UA%!,s2QqTWWY>D}TM	U ||  D	A$tOd2]BMC 3#As   C E#&E(BE E c              #     K   i }|D ]  }||v s||   s||   d   dk(  rt        ||   d         |t        |      <   7||   d   dk(  rt        ||   d         |t        |      <   `||   d   dk(  r-d||   v r&t        ||   d   ||   d         |t        |      <   ||   d   dk(  sd||   v st        ||   d   ||   d   	      |t        |      <    t	        | |       y w)
Ntypeurir   bnodeliteraldatatype)r   zxml:lang)lang)r
   r   r	   r   r   )r$   rs   r   res_dictr;   s        r1   r   r   {  s    H !8#vf~&*03*A#'37**/#w*@#'39,qv1E*1cF7OafZ.@+#' 39,qv1E*1cF7O!C&*<+#' h
''s   C)C)BC)+C)36C)c                 .    t        | |j                        S )z<
    http://www.w3.org/TR/sparql11-query/#defn_algGroup
    r5   r'   )r$   r   s     r1   r   r     s    
 C!!rL   c              #     K   t        | j                        }j                  j                  }t        j                  fd      }||d   }|D ]  |j                          n/|D ]*  t        fd|D              }||   j                         , |j                         D ]  }t        | |j                                  t        |      dk(  rt        |        y y w)Nc                  0    t         j                        S )N)aggregations)r   A)aggs   r1   r   z#evalAggregateJoin.<locals>.<lambda>  s    *#%%*H rL   Tc              3   8   K   | ]  }t        |d         yw)FN)r   )rW   r=   rows     r1   rX   z$evalAggregateJoin.<locals>.<genexpr>  s     ?qeAsE*?s   r   )r5   r'   r6   collectionsdefaultdictupdatetuplerx   r   get_bindingsr~   )r$   r   r'   
group_exprrq   
aggregatorrt   r   s    `     @r1   r   r     s     ceeA J

!
!"H
IC Y
 	#Cc"	#  	C?J??AFMM#	 jjl =
S*"9"9";<<= 3x1}S!! s   C"C&c                     t        | |j                        }t        |j                        D ]9  t	        j
                  xr j
                  dk(        }t        |fd|      }; |S )NDESCc                 F    t        t        | j                  d            S )NT)r   )r   r   r6   )r0   r=   s    r1   r   zevalOrderBy.<locals>.<lambda>  s    tE!QVVt$DE rL   )r   reverse)r5   r'   reversedr6   boolorderr   )r$   r`   rq   r   r=   s       @r1   r   r     s`    
3
Cdii  
qww4177f#45Ew

 JrL   c                     t        | |j                        }t        j                  ||j                  |j
                  |j                  |j
                  z         S d       S rH   )r5   r'   	itertoolsislicestartlength)r$   slicerq   s      r1   r   r     sY    
3
 C&+ll&>ell"  EI rL   c              #   T  K   d}t               }t        j                         }t        | |j                        D ]k  }||v r|j                  |       nB| |j                  |       t        |      |kD  r|j                  |j                                |j                  |       m yw)zapply REDUCED to result

    REDUCED is not as strict as DISTINCT, but if the incoming rows were sorted
    it should produce the same result with limited extra memory and time per
    incoming row.
    r   N)
rJ   r   dequer5   r'   removeaddr~   pop
appendleft)r$   r`   MAXmru_set	mru_queuer   s         r1   r   r     s      C eG!!#ITVV$ "'>S! IKK7|c!y}}/S!"s   B&B(c              #      K   t        | |j                        }t               }|D ]  }||vs| |j                  |        y wrH   )r5   r'   rJ   r   )r$   r`   rq   doner0   s        r1   r   r     sC     
3
C5D D=GHHQKs
   *AAc                 F    t        | j                        }fd|D        S )Nc              3   T   K   | ]  }|j                  j                         ! y wrH   )projectPV)rW   r   r  s     r1   rX   zevalProject.<locals>.<genexpr>  s     3CKK

#3s   %(r   )r$   r  rq   s    ` r1   r   r     s    
3		
"C3s33rL   c                 d    i }d|d<   t        | |j                        |d<   |j                  |d<   |S )NSELECTtype_r   r   )r5   r'   r  )r$   r   rq   s      r1   r   r     s6    
CCLsEGG,C
O88CLJrL   c                 b    i }d|d<   d|d<   t        | |j                        D ]	  }d|d<    |S  |S )NASKr  F	askAnswerTr   )r$   r   rq   r0   s       r1   r   r   
  sM    
CCLCc577# KJ	 JrL   c                     |j                   }|s |j                  j                  j                  }t               }t	        | |j                        D ]  }|t        ||      z  } i }d|d<   ||d<   |S )N	CONSTRUCTr  r    )templater'   r!   r   r5   r   )r$   r   r  r    r/   rq   s         r1   r   r     sq    ~~H7799$$GEc577# ,x++, CCLCLJrL   c                 V   t        d |j                         D              }t        | |      }|j                  |_        |j                  }|j
                  r|j                  t        d      |j                         }d}|j
                  D ]  }|j                  rV|r6|j                  j                  t                     }|j                  |      }d}|j                  |j                  d       e|j                  sr|j                  }	|j                  |	d        t        ||      S )Nc              3   <   K   | ]  \  }}t        |      |f  y wrH   )r   )rW   rt   ru   s      r1   rX   zevalQuery.<locals>.<genexpr>*  s     JTQ!a(Js   )initBindingszRNon-conjunctive-graph doesn't know about graphs! Try a query without FROM (NAMED).FT)default)dictrr   r   r   algebradatasetClauserc   rd   re   r  rk   r	   ri   loadnamedr5   )
r    r   r  r   r$   mainfirstDefaultddggs
             r1   	evalQueryr   (  s    J\5G5G5IJJL
u<
8C>>CL==D;;> 
 iik## 	+Ayy009B--+C#'LD1GGE*	+ CrL   rH   )C__doc__r   r   r   urllib.requestr   r   urllib.parser   r   r   	pyparsingr   rdflibr   r   r	   r
   r   rdflib.plugins.sparqlr   !rdflib.plugins.sparql.parserutilsr   rdflib.plugins.sparql.sparqlr   r   r   r   r   rdflib.plugins.sparql.evalutilsr   r   r   r   r   r   r   r    rdflib.plugins.sparql.aggregatesr   r   r#   r>   rF   rK   rQ   rT   r]   ra   rn   rv   rz   r5   r   r   r   r   r   r   r   r   r   r   r   r   r   r    rL   r1   <module>r,     s       	 + "  $ : : . 3 	 	 	 8 (%P

0B
!A7H*Z0(&"":#"L4&"rL   