
    4YshG                     <   d dl mZmZ d dl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 	  G d de      Z G d	 d
e      Zd Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)    )LiteralXSD)_evalNotBoundError_val)numeric)type_promotion)SPARQLTypeError)Decimalc                   (    e Zd ZdZd Zd Zd Zd Zy)Accumulatorz7abstract base class for different aggregation functionsc                     |j                   | _        |j                  | _        |j                  s| j
                  | _        d| _        y |j                  | _        t               | _        y )NF)	resvarvarsexprdistinct	dont_careuse_rowsetseen)selfaggregations     /var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/sparql/aggregates.py__init__zAccumulator.__init__   sK    ??$$	##>>DL!DM'00DMDI    c                      y)zskips distinct testT r   rows     r   r   zAccumulator.dont_care       r   c                 F    t        | j                  |      | j                  vS )ztests distinct with set)r   r   r   r   s     r   r   zAccumulator.use_row    s    TYY$DII55r   c                 >    | j                         || j                  <   y)zsets final value in bindingsN)	get_valuer   r   bindingss     r   	set_valuezAccumulator.set_value$   s    !^^-r   N)__name__
__module____qualname____doc__r   r   r   r'   r   r   r   r   r      s    A6.r   r   c                   <     e Zd Z fdZd Zd Zd Zd Zd Z xZ	S )Counterc                 |    t         t        |   |       d| _        | j                  dk(  r| j
                  | _        y y )Nr   *)superr-   r   valuer   eval_full_roweval_rowr   r   	__class__s     r   r   zCounter.__init__*   s7    gt%k2
99 ..DM r   c                     	 | j                  |      }| xj                  dz  c_        | j                  r| j                  j                  |       y y # t        $ r Y y w xY wN   )r3   r   r1   r   r   add)r   r    
aggregatorvals       r   updatezCounter.update1   sT    	--$C 	

a
==IIMM# 	  		s   A 	AAc                 ,    t        | j                        S N)r   r1   r   s    r   r$   zCounter.get_value;   s    tzz""r   c                 .    t        | j                  |      S r>   )r   r   r   s     r   r3   zCounter.eval_row>   s    TYY$$r   c                     |S r>   r   r   s     r   r2   zCounter.eval_full_rowA   s    
r   c                 <    | j                  |      | j                  vS r>   )r3   r   r   s     r   r   zCounter.use_rowD   s    }}S!22r   )
r(   r)   r*   r   r<   r$   r3   r2   r   __classcell__r5   s   @r   r-   r-   )   s!    /#%3r   r-   c                  n    t        d | D              r"t        d | D              rt        t        |       S | S )Nc              3   <   K   | ]  }t        |t                y wr>   )
isinstancefloat.0args     r   	<genexpr>z$type_safe_numbers.<locals>.<genexpr>I   s     
2c:c5!
2   c              3   <   K   | ]  }t        |t                y wr>   )rG   r   rI   s     r   rL   z$type_safe_numbers.<locals>.<genexpr>I   s      ;%(
3 ;rM   )anymaprH   )argss    r   type_safe_numbersrR   H   s8    

2T
22s ;,0; 8 5$Kr   c                   *     e Zd Z fdZd Zd Z xZS )Sumc                 H    t         t        |   |       d| _        d | _        y Nr   )r0   rT   r   r1   datatyper4   s     r   r   zSum.__init__Q   s     c4!+.
r   c                 j   	 t        | j                  |      }| j                  }||j                  }nt        ||j                        }|| _        t	        t        | j                  t        |                  | _        | j                  r| j                  j                  |       y y # t        $ r Y y w xY wr>   )r   r   rW   r	   sumrR   r1   r   r   r   r9   r   r   r    r:   r1   dts        r   r<   z
Sum.updateV   s    	$))S)EBz^^#B7DM.tzz75>JKDJ}}		e$  		s   B"B& &	B21B2c                 D    t        | j                  | j                        S )N)rW   )r   r1   rW   r?   s    r   r$   zSum.get_valuef   s    tzzDMM::r   r(   r)   r*   r   r<   r$   rC   rD   s   @r   rT   rT   P   s    
 ;r   rT   c                   *     e Zd Z fdZd Zd Z xZS )Averagec                 V    t         t        |   |       d| _        d| _        d | _        y rV   )r0   r_   r   counterrY   rW   r4   s     r   r   zAverage.__init__k   s'    gt%k2r   c                    	 t        | j                  |      }| j                  }t        t	        | j                  t        |                  | _        ||j                  }nt        ||j                        }|| _        | j                  r| j                  j                  |       | xj                  dz  c_
        y # t        $ r Y y t        $ r Y y w xY wr7   )r   r   rW   rY   rR   r   r	   r   r   r9   ra   r   r
   rZ   s        r   r<   zAverage.updateq   s    	$))S)EB,TXXwu~FGDHz^^#B7DM}}		e$LLAL 	 		s   B7B: :	CCCc                 :   | j                   dk(  rt        d      S | j                  t        j                  t        j
                  fv r"t        | j                  | j                   z        S t        t        | j                        t        | j                         z        S rV   )ra   r   rW   r   rH   doublerY   r   r?   s    r   r$   zAverage.get_value   sk    <<11:==SYY

33488dll2337488,wt||/DDEEr   r]   rD   s   @r   r_   r_   j   s    &Fr   r_   c                   .     e Zd ZdZ fdZd Zd Z xZS )Extremumz+abstract base class for Minimum and Maximumc                 \    t         t        |   |       d | _        | j                  | _        y r>   )r0   rf   r   r1   r   r   r4   s     r   r   zExtremum.__init__   s$    h&{3
~~r   c                 b    | j                   #t        | j                         || j                  <   y y r>   )r1   r   r   r%   s     r   r'   zExtremum.set_value   s(    ::!!(!4HTXX "r   c                     	 | j                   t        | j                  |      | _         y | j                  | j                   t        | j                  |            | _         y # t        $ r Y y t
        $ r Y y w xY wr>   )r1   r   r   comparer   r
   r   r    r:   s      r   r<   zExtremum.update   sc    
	zz!"499c2
 "\\$**eDIIs6KL
 	 		s   'A  5A   	A6+A65A6)r(   r)   r*   r+   r   r'   r<   rC   rD   s   @r   rf   rf      s    5&5
r   rf   c                       e Zd Zd Zy)Minimumc                 &    t        ||t              S N)key)minr   r   val1val2s      r   rj   zMinimum.compare       44((r   Nr(   r)   r*   rj   r   r   r   rm   rm          )r   rm   c                       e Zd Zd Zy)Maximumc                 &    t        ||t              S ro   )maxr   rr   s      r   rj   zMaximum.compare   ru   r   Nrv   r   r   r   ry   ry      rw   r   ry   c                   .     e Zd ZdZ fdZd Zd Z xZS )Sampleztakes the first eligible valuec                 N    t         t        |   |       | j                  | _        y r>   )r0   r}   r   r   r   r4   s     r   r   zSample.__init__   s    fd$[1~~r   c                     	 t        | j                  |      |j                  | j                  <   |j                  | j                  = y # t
        $ r Y y w xY wr>   )r   r   r&   r   accumulatorsr   rk   s      r   r<   zSample.update   sJ    	,1$))S,AJ)''1 		s   AA 	AAc                      y r>   r   r?   s    r   r$   zSample.get_value   r!   r   )r(   r)   r*   r+   r   r<   r$   rC   rD   s   @r   r}   r}      s    (&
r   r}   c                   *     e Zd Z fdZd Zd Z xZS )GroupConcatc                 d    t         t        |   |       g | _        |j                  xs d| _        y )N )r0   r   r   r1   	separatorr4   s     r   r   zGroupConcat.__init__   s+    k4)+6
$..5#r   c                     	 t        | j                  |      }| j                  j                  |       | j                  r| j
                  j                  |       y y # t        $ r Y y w xY wr>   )r   r   r1   appendr   r   r9   r   )r   r    r:   r1   s       r   r<   zGroupConcat.update   sY    	$))S)EJJe$}}		e$   		s   AA 	A('A(c                 l    t        | j                  j                  d | j                  D                    S )Nc              3   2   K   | ]  }t        |        y wr>   )str)rJ   vs     r   rL   z(GroupConcat.get_value.<locals>.<genexpr>   s     *Fa3q6*Fs   )r   r   joinr1   r?   s    r   r$   zGroupConcat.get_value   s&    t~~***F4::*FFGGr   r]   rD   s   @r   r   r      s    6Hr   r   c                   6    e Zd ZdZeeeeee	e
dZd Zd Zd Zy)
Aggregatorz&combines different Accumulator objects)Aggregate_CountAggregate_SampleAggregate_SumAggregate_AvgAggregate_MinAggregate_MaxAggregate_GroupConcatc                     i | _         i | _        |D ]`  }| j                  j                  |j                        }|t        d|j                  z          ||      | j                  |j                  <   b y )NzUnknown aggregate function )r&   r   accumulator_classesgetname	Exceptionr   )r   aggregationsaaccumulator_classs       r   r   zAggregator.__init__   so     	<A $ 8 8 < <QVV D ( = FGG'8';Daee$		<r   c                     t        | j                  j                               D ]&  }|j                  |      s|j	                  ||        ( y)zupdate all own accumulatorsN)listr   valuesr   r<   )r   r    accs      r   r<   zAggregator.update   sA    
 ))0023 	&C{{3

3%	&r   c                     | j                   j                         D ]  }|j                  | j                          | j                  S )zcalculate and set last values)r   r   r'   r&   )r   r   s     r   get_bindingszAggregator.get_bindings   s:    $$++- 	)CMM$--(	)}}r   N)r(   r)   r*   r+   r-   r}   rT   r_   rm   ry   r   r   r   r<   r   r   r   r   r   r      s3    0 #"   !,<&r   r   N)rdflibr   r   rdflib.plugins.sparql.evalutilsr   r   r   rdflib.plugins.sparql.operatorsr   rdflib.plugins.sparql.datatypesr	   rdflib.plugins.sparql.sparqlr
   decimalr   objectr   r-   rR   rT   r_   rf   rm   ry   r}   r   r   r   r   r   <module>r      s     F F 3 : 8 
.& .43k 3>;+ ;4 Fk  FF{ 8)h )
)h )
[ ,H+ H*# #r   