
    Pi)                    8   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*          Z3d+S )/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                  2    e Zd ZdZddZdd	Zdd
ZddZdS )Accumulatorz7abstract base class for different aggregation functionsaggregationr   c                    |  |  |j         | _        |j        | _        |j        s| j        | _        d| _        d S |j        | _        t                      | _        d S NF)	resvarvarsexprdistinct	dont_careuse_rowsetseen)selfr"   s     t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/rdflib/plugins/sparql/aggregates.py__init__zAccumulator.__init__$   sY    7A?$	# 	(>DL!DMMM'0DM"%%%DIII    rowr   returnboolc                    dS )zskips distinct testT r.   r2   s     r/   r*   zAccumulator.dont_care1       tr1   c                :    t          | j        |          | j        vS )ztests distinct with set)r   r(   r-   r7   s     r/   r+   zAccumulator.use_row5   s    TY$$DI55r1   bindings$MutableMapping[Variable, Identifier]Nonec                >    |                                  || j        <   dS )zsets final value in bindingsN)	get_valuer&   r.   r:   s     r/   	set_valuezAccumulator.set_value9   s      "^^--r1   Nr"   r   r2   r   r3   r4   r:   r;   r3   r<   )__name__
__module____qualname____doc__r0   r*   r+   r@   r6   r1   r/   r!   r!   !   sj        AA( ( ( (   6 6 6 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 )Counterr"   r   c                    t          t          |                               |           d| _        | j        dk    r| j        | _        d S d S )Nr   *)superrI   r0   valuer(   eval_full_roweval_rowr.   r"   	__class__s     r/   r0   zCounter.__init__@   sM    gt%%k222
9 !.DMMM r1   r2   r   
aggregator
Aggregatorr3   r<   c                    	 |                      |          }n# t          $ r Y d S w xY w| xj        dz  c_        | j        r| j                            |           d S d S N   )rO   r   rM   r)   r-   add)r.   r2   rR   vals       r/   updatezCounter.updateH   s{    	--$$CC 	 	 	FF	 	

a

= 	IMM#	 	s    
&&r   c                *    t          | j                  S N)r   rM   r.   s    r/   r>   zCounter.get_valueR   s    tz"""r1   r   c                ,    t          | j        |          S r[   )r   r(   r7   s     r/   rO   zCounter.eval_rowU   s    TY$$$r1   c                    |S r[   r6   r7   s     r/   rN   zCounter.eval_full_rowX   s    
r1   r4   c                ^    	 |                      |          | j        vS # t          $ r Y dS w xY wr$   )rO   r-   r   r7   s     r/   r+   zCounter.use_row[   sB    	==%%TY66 	 	 	55	s    
,,rA   r2   r   rR   rS   r3   r<   r3   r   )r2   r   r3   r   )r2   r   r3   r   rB   )
rD   rE   rF   r0   rY   r>   rO   rN   r+   __classcell__rQ   s   @r/   rI   rI   ?   s        / / / / / /   # # # #% % % %          r1   rI   argsintr3   
Tuple[int]c                     d S r[   r6   rd   s    r/   type_safe_numbersri   c   s    14r1   Union[Decimal, float, int]Tuple[Union[float, int]]c                     d S r[   r6   rh   s    r/   ri   ri   g   s	      #sr1   Iterable[Union[float, int]]c                     t          d | D                       r.t          d | D                       rt          t          |           S | S )Nc              3  @   K   | ]}t          |t                    V  d S r[   )
isinstancefloat.0args     r/   	<genexpr>z$type_safe_numbers.<locals>.<genexpr>n   s,      
2
2c:c5!!
2
2
2
2
2
2r1   c              3  @   K   | ]}t          |t                    V  d S r[   )rp   r   rr   s     r/   ru   z$type_safe_numbers.<locals>.<genexpr>n   s=       ; ;%(
3  ; ; ; ; ; ;r1   )anymaprq   rh   s    r/   ri   ri   m   sg    

2
2T
2
2
222  s ; ;,0; ; ; 8 8   5$ Kr1   c                  0     e Zd Zd fdZdd
ZddZ xZS )Sumr"   r   c                t    t          t          |                               |           d| _        d | _        d S Nr   )rL   rz   r0   rM   datatyperP   s     r/   r0   zSum.__init__x   s3    c4!!+...
'+r1   r2   r   rR   rS   r3   r<   c                \   	 t          | j        |          }| j        }||j        }nt          ||j                  }|| _        t	          t          | j        t          |                              | _        | j        r| j	        
                    |           d S d S # t          $ r Y d S w xY wr[   )r   r(   r}   r   sumri   rM   r   r)   r-   rW   r   r.   r2   rR   rM   dts        r/   rY   z
Sum.update}   s    	$)S))EBz^ $B77DM.tz75>>JJKKDJ} %	e$$$$$% % 	 	 	DD	s   BB 
B+*B+r   c                8    t          | j        | j                  S )N)r}   )r   rM   r}   r\   s    r/   r>   zSum.get_value   s    tzDM::::r1   rA   r`   ra   rD   rE   rF   r0   rY   r>   rb   rc   s   @r/   rz   rz   w   se        , , , , , ,
   "; ; ; ; ; ; ; ;r1   rz   c                  0     e Zd Zd fdZdd
ZddZ xZS )Averager"   r   c                    t          t          |                               |           d| _        d| _        d | _        d S r|   )rL   r   r0   counterr   r}   rP   s     r/   r0   zAverage.__init__   s:    gt%%k222'+r1   r2   r   rR   rS   r3   r<   c                   	 t          | j        |          }| j        }t          t	          | j        t          |                              | _        ||j        }nt          ||j                  }|| _        | j        r| j        	                    |           | xj
        dz  c_
        d S # t          $ r Y d S t          $ r Y d S w xY wrU   )r   r(   r}   r   ri   r   r   r)   r-   rW   r   r   r   r   s        r/   rY   zAverage.update   s    	$)S))EB,TXwu~~FFGGDHz^ $B77DM} %	e$$$LLALLLL 	 	 	DD 	 	 	DD	s   B'B+ +
C8	CCr   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 r|   )r   r   r}   r   rq   doubler   r   r\   s    r/   r>   zAverage.get_value   so    <11::=SY
33348dl2333748,,wt|/D/DDEEEr1   rA   r`   ra   r   rc   s   @r/   r   r      sm        , , , , , ,   (F F F F F F F 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 Maximumr"   r   c                    |  t          t          |                               |           d | _        | j        | _        d S r[   )rL   r   r0   rM   r*   r+   rP   s     r/   r0   zExtremum.__init__   s=    /h&&{333
 ~r1   r:   r;   r3   r<   c                P    | j         t          | j                   || j        <   d S d S r[   )rM   r   r&   r?   s     r/   r@   zExtremum.set_value   s.    :!!(!4!4HTX "!r1   r2   r   rR   rS   c                    	 | j         t          | j        |          | _         d S |                     | j         t          | j        |                    | _         d S # t          $ r Y d S t
          $ r Y d S w xY wr[   )rM   r   r(   comparer   r   r.   r2   rR   s      r/   rY   zExtremum.update   s    
	z!"49c22


 "\\$*eDIs6K6KLL


 	 	 	DD 	 	 	DD	s   !A 3A 
A4'	A43A4rA   rC   r`   )rD   rE   rF   rG   r0   r@   rY   rb   rc   s   @r/   r   r      sk        55& & & & & &5 5 5 5
       r1   r   _ValueTc                      e Zd ZddZdS )Minimumval1r   val2r3   c                0    t          ||t                    S N)key)minr   r.   r   r   s      r/   r   zMinimum.compare       44((((r1   Nr   r   r   r   r3   r   rD   rE   rF   r   r6   r1   r/   r   r      (        ) ) ) ) ) )r1   r   c                      e Zd ZddZdS )Maximumr   r   r   r3   c                0    t          ||t                    S r   )maxr   r   s      r/   r   zMaximum.compare   r   r1   Nr   r   r6   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                p    t          t          |                               |           | j        | _        d S r[   )rL   r   r0   r*   r+   rP   s     r/   r0   zSample.__init__   s0    fd$$[111 ~r1   r2   r   rR   rS   r3   r<   c                    	 t          | j        |          |j        | j        <   |j        | j        = d S # t
          $ r Y d S w xY wr[   )r   r(   r:   r&   accumulatorsr   r   s      r/   rY   zSample.update   sW    	,1$)S,A,AJ)'111 	 	 	DD	s   /3 
A Ac                    d S r[   r6   r\   s    r/   r>   zSample.get_value   r8   r1   r`   )r3   r<   )rD   rE   rF   rG   r0   rY   r>   rb   rc   s   @r/   r   r      sf        ((& & & & &          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]rM   r"   r   c                    t          t          |                               |           g | _        |j        	d| _        d S |j        | _        d S )N )rL   r   r0   rM   	separatorrP   s     r/   r0   zGroupConcat.__init__   sJ    k4  ))+666
 ( DNNN(2DNNNr1   r2   r   rR   rS   r3   r<   c                    	 t          | j        |          }t          |t                    rd S | j                            |           | j        r| j                            |           d S d S # t          $ r Y d S w xY wr[   )	r   r(   rp   r   rM   appendr)   r-   rW   )r.   r2   rR   rM   s       r/   rY   zGroupConcat.update  s    	$)S))E%// Je$$$} %	e$$$$$% %  	 	 	DD	s   *A- ;A- -
A;:A;r   c                n    t          | j                            d | j        D                                 S )Nc              3  4   K   | ]}t          |          V  d S r[   )str)rs   vs     r/   ru   z(GroupConcat.get_value.<locals>.<genexpr>  s(      *F*Fa3q66*F*F*F*F*F*Fr1   )r   r   joinrM   r\   s    r/   r>   zGroupConcat.get_value  s1    t~***F*F4:*F*F*FFFGGGr1   rA   r`   ra   )rD   rE   rF   __annotations__r0   rY   r>   rb   rc   s   @r/   r   r      s{         3 3 3 3 3 3   "H H H H H H H 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dS )rS   z&combines different Accumulator objects)Aggregate_CountAggregate_SampleAggregate_SumAggregate_AvgAggregate_MinAggregate_MaxAggregate_GroupConcataggregationsList[CompValue]c                    i | _         i | _        |D ]R}| j                            |j                  }|t          d|j        z              ||          | j        |j        <   Sd S )NzUnknown aggregate function )r:   r   accumulator_classesgetname	Exceptionr%   )r.   r   aaccumulator_classs       r/   r0   zAggregator.__init__&  s|    4646 	< 	<A $ 8 < <QV D D ( = FGGG'8'8';';Dae$$		< 	<r1   r2   r   r3   r<   c                    t          | j                                                  D ]-}|                    |          r|                    ||            .dS )zupdate all own accumulatorsN)listr   valuesr+   rY   )r.   r2   accs      r/   rY   zAggregator.update/  s]    
 )002233 	& 	&C{{3 &

3%%%	& 	&r1   Mapping[Variable, Identifier]c                |    | j                                         D ]}|                    | j                   | j        S )zcalculate and set last values)r   r   r@   r:   )r.   r   s     r/   get_bindingszAggregator.get_bindings8  s?    $++-- 	) 	)CMM$-((((}r1   N)r   r   )r2   r   r3   r<   )r3   r   )rD   rE   rF   rG   rI   r   rz   r   r   r   r   r   r0   rY   r   r6   r1   r/   rS   rS     sz        00 #"   !, < < < <& & & &     r1   rS   N)rd   re   r3   rf   )rd   rj   r3   rk   )rd   rj   r3   rm   )4rG   
__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!   rI   ri   rz   r   r   r   r   r   r   r   rS   r6   r1   r/   <module>r      s    # " " " " "                                     !           : : : : : : 7 7 7 7 7 7 7 7 3 3 3 3 3 3 7 7 7 7 7 7 W W W W W W W W W W D D D D D D D D D D D D D D. . . . . . . .<! ! ! ! !k ! ! !H 
 4 4 4 
 4 
# # # 
#
   ; ; ; ; ;+ ; ; ;6!F !F !F !F !Fk !F !F !FH    {   < ')Xufg
>
>) ) ) ) )h ) ) )
) ) ) ) )h ) ) )
    [   .H H H H H+ H H HB# # # # # # # # # #r1   