
    i)                       d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZm Z m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z'  G d d      Z( G d de(      Z)ed%d       Z*e	 	 	 	 d&d       Z*d'dZ* G d de(      Z+ G d de(      Z, G d de(      Z- ede'e#e&e%      Z. G d de-      Z/ G d de-      Z0 G d de(      Z1 G d  d!e(      Z2 G d" d#      Z3y$)(z
Aggregation functions
    )annotations)Decimal)AnyCallableDictIterableListMappingMutableMappingOptionalSetTupleTypeVarUnionoverload)XSD)type_promotion)_eval_val)numeric)	CompValue)FrozenBindingsNotBoundErrorSPARQLTypeError)BNode
IdentifierLiteralURIRefVariablec                  0    e Zd ZdZddZddZddZd	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     OC:\Projects\mas-dev\.venv\Lib\site-packages\rdflib/plugins/sparql/aggregates.py__init__zAccumulator.__init__$   sQ    ??$$	##>>DL!DM'00DM"%%DI    c                     y)zskips distinct testT r-   rows     r/   r)   zAccumulator.dont_care1       r1   c                F    t        | j                  |      | j                  vS )ztests distinct with set)r   r'   r,   r4   s     r/   r*   zAccumulator.use_row5   s    TYY$DII55r1   c                >    | j                         || j                  <   y)zsets final value in bindingsN)	get_valuer%   r-   bindingss     r/   	set_valuezAccumulator.set_value9   s     "^^-r1   Nr.   r   r5   r   returnboolr;   z$MutableMapping[Variable, Identifier]r?   None)__name__
__module____qualname____doc__r0   r)   r*   r<   r3   r1   r/   r!   r!   !   s    A(6.r1   r!   c                  H     e Zd Zd fdZddZd	dZd
dZddZddZ xZ	S )Counterc                |    t         t        |   |       d| _        | j                  dk(  r| j
                  | _        y y )Nr   *)superrH   r0   valuer'   eval_full_roweval_rowr-   r.   	__class__s     r/   r0   zCounter.__init__@   s9    gt%k2
99 !..DM r1   c                    	 | j                  |      }| xj                  dz  c_        | j                  r| j                  j                  |       y y # t        $ r Y y w xY wN   )rN   r   rL   r(   r,   add)r-   r5   
aggregatorvals       r/   updatezCounter.updateH   sT    	--$C 	

a
==IIMM# 	  		s   A 	AAc                ,    t        | j                        S N)r   rL   r-   s    r/   r9   zCounter.get_valueR   s    tzz""r1   c                .    t        | j                  |      S rY   )r   r'   r4   s     r/   rN   zCounter.eval_rowU   s    TYY$$r1   c                    |S rY   r3   r4   s     r/   rM   zCounter.eval_full_rowX   s    
r1   c                \    	 | j                  |      | j                  vS # t        $ r Y yw xY wr#   )rN   r,   r   r4   s     r/   r*   zCounter.use_row[   s1    	==%TYY66 		s    	++r=   r5   r   rU   
Aggregatorr?   rB   r?   r   )r5   r   r?   r   )r5   r   r?   r   r>   )
rC   rD   rE   r0   rW   r9   rN   rM   r*   __classcell__rP   s   @r/   rH   rH   ?   s!    /#%r1   rH   c                      y rY   r3   argss    r/   type_safe_numbersrf   c   s    14r1   c                      y rY   r3   rd   s    r/   rf   rf   g   s      #r1   c                 n    t        d | D              r"t        d | D              rt        t        |       S | S )Nc              3  <   K   | ]  }t        |t                y wrY   )
isinstancefloat.0args     r/   	<genexpr>z$type_safe_numbers.<locals>.<genexpr>n   s     
2Tc:c5!T   c              3  <   K   | ]  }t        |t                y wrY   )rj   r   rl   s     r/   ro   z$type_safe_numbers.<locals>.<genexpr>n   s      ;,0S
3 Drp   )anymaprk   rd   s    r/   rf   rf   m   s:    

2T
22s ;,0; 8 5$ Kr1   c                  0     e Zd Zd fdZddZddZ xZS )Sumc                H    t         t        |   |       d| _        d | _        y Nr   )rK   ru   r0   rL   datatyperO   s     r/   r0   zSum.__init__x   s     c4!+.
'+r1   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rY   )r   r'   rx   r   sumrf   rL   r   r(   r,   rT   r   r-   r5   rU   rL   dts        r/   rW   z
Sum.update}   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)rx   )r   rL   rx   rZ   s    r/   r9   zSum.get_value   s    tzzDMM::r1   r=   r^   r`   rC   rD   rE   r0   rW   r9   ra   rb   s   @r/   ru   ru   w   s    ,
";r1   ru   c                  0     e Zd Zd fdZddZddZ xZS )Averagec                V    t         t        |   |       d| _        d| _        d | _        y rw   )rK   r   r0   counterrz   rx   rO   s     r/   r0   zAverage.__init__   s'    gt%k2'+r1   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rR   )r   r'   rx   rz   rf   r   r   r(   r,   rT   r   r   r   r{   s        r/   rW   zAverage.update   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 rw   )r   r   rx   r   rk   doublerz   r   rZ   s    r/   r9   zAverage.get_value   sk    <<11:==SYY

33488dll2337488,wt||/DDEEr1   r=   r^   r`   r~   rb   s   @r/   r   r      s    ,(Fr1   r   c                  4     e Zd ZdZd fdZddZddZ xZS )Extremumz+abstract base class for Minimum and Maximumc                `    |  t         t        |   |       d | _        | j                  | _        y rY   )rK   r   r0   rL   r)   r*   rO   s     r/   r0   zExtremum.__init__   s)    h&{3
 ~~r1   c                b    | j                   #t        | j                         || j                  <   y y rY   )rL   r   r%   r:   s     r/   r<   zExtremum.set_value   s(    ::!!(!4HTXX "r1   c                    	 | j                   t        | j                  |      | _         y | j                  | j                   t        | j                  |            | _         y # t        $ r Y y t
        $ r Y y w xY wrY   )rL   r   r'   comparer   r   r-   r5   rU   s      r/   rW   zExtremum.update   sc    
	zz!"499c2
 "\\$**eDIIs6KL
 	 		s   'A  5A   	A6+A65A6r=   rA   r^   )rC   rD   rE   rF   r0   r<   rW   ra   rb   s   @r/   r   r      s    5&5
r1   r   _ValueTc                      e Zd ZddZy)Minimumc                &    t        ||t              S N)key)minr   r-   val1val2s      r/   r   zMinimum.compare       44((r1   Nr   r   r   r   r?   r   rC   rD   rE   r   r3   r1   r/   r   r          )r1   r   c                      e Zd ZddZy)Maximumc                &    t        ||t              S r   )maxr   r   s      r/   r   zMaximum.compare   r   r1   Nr   r   r3   r1   r/   r   r      r   r1   r   c                  2     e Zd ZdZ fdZddZddZ xZS )Sampleztakes the first eligible valuec                N    t         t        |   |       | j                  | _        y rY   )rK   r   r0   r)   r*   rO   s     r/   r0   zSample.__init__   s    fd$[1 ~~r1   c                    	 t        | j                  |      |j                  | j                  <   |j                  | j                  = y # t
        $ r Y y w xY wrY   )r   r'   r;   r%   accumulatorsr   r   s      r/   rW   zSample.update   sJ    	,1$))S,AJ)''1 		s   AA 	AAc                     y rY   r3   rZ   s    r/   r9   zSample.get_value   r6   r1   r^   )r?   rB   )rC   rD   rE   rF   r0   rW   r9   ra   rb   s   @r/   r   r      s    (&r1   r   c                  <     e Zd ZU ded<   d fdZddZddZ xZS )	GroupConcatzList[Literal]rL   c                    t         t        |   |       g | _        |j                  d| _        y |j                  | _        y )N )rK   r   r0   rL   	separatorrO   s     r/   r0   zGroupConcat.__init__   s9    k4)+6
  ( DN(22DNr1   c                    	 t        | j                  |      }t        |t              ry | j                  j                  |       | j                  r| j                  j                  |       y y # t        $ r Y y w xY wrY   )	r   r'   rj   r   rL   appendr(   r,   rT   )r-   r5   rU   rL   s       r/   rW   zGroupConcat.update  sf    	$))S)E%/JJe$}}		e$   		s   &A- AA- -	A98A9c                l    t        | j                  j                  d | j                  D                    S )Nc              3  2   K   | ]  }t        |        y wrY   )str)rm   vs     r/   ro   z(GroupConcat.get_value.<locals>.<genexpr>  s     *F:a3q6:s   )r   r   joinrL   rZ   s    r/   r9   zGroupConcat.get_value  s&    t~~***F4::*FFGGr1   r=   r^   r`   )rC   rD   rE   __annotations__r0   rW   r9   ra   rb   s   @r/   r   r      s    3"Hr1   r   c                  <    e Zd ZdZeeeeee	e
dZddZddZd	dZy)
r_   z&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/   r0   zAggregator.__init__&  sj    4646A $ 8 8 < <QVV D ( = FGG'8';Daee$	 r1   c                    t        | j                  j                               D ]&  }|j                  |      s|j	                  ||        ( y)zupdate all own accumulatorsN)listr   valuesr*   rW   )r-   r5   accs      r/   rW   zAggregator.update/  s<    
 ))0023C{{3

3% 4r1   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_bindings8  s5    $$++-CMM$--( .}}r1   N)r   zList[CompValue])r5   r   r?   rB   )r?   zMapping[Variable, Identifier])rC   rD   rE   rF   rH   r   ru   r   r   r   r   r   r0   rW   r   r3   r1   r/   r_   r_     s3    0 #"   !,<&r1   r_   N)re   intr?   z
Tuple[int])re   Union[Decimal, float, int]r?   zTuple[Union[float, int]])re   r   r?   zIterable[Union[float, int]])4rF   
__future__r   decimalr   typingr   r   r   r   r	   r
   r   r   r   r   r   r   r   rdflib.namespacer   rdflib.plugins.sparql.datatypesr   rdflib.plugins.sparql.evalutilsr   r   rdflib.plugins.sparql.operatorsr   !rdflib.plugins.sparql.parserutilsr   rdflib.plugins.sparql.sparqlr   r   r   rdflib.termr   r   r   r   r   r!   rH   rf   ru   r   r   r   r   r   r   r   r_   r3   r1   r/   <module>r      s   #      ! : 7 3 7 W W D D. .<!k !H 
 4 
 4 
#%## 
#
;+ ;6!Fk !FH{ < )Xufg
>)h )
)h )
[ .H+ HB# #r1   