o
    mi)                     @  s  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 dZ(G dd de(Z)ed-ddZ*ed.ddZ*d/ddZ*G dd de(Z+G dd de(Z,G dd  d e(Z-ed!e'e#e&e%Z.G d"d# d#e-Z/G d$d% d%e-Z0G d&d' d'e(Z1G d(d) d)e(Z2G d*d+ d+Z3d,S )0z
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                   @  s8   e Zd ZdZdddZdd
dZdddZdddZdS )Accumulatorz7abstract base class for different aggregation functionsaggregationr   c                 C  sD   |  |  |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     r-   Z/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/plugins/sparql/aggregates.py__init__$   s   
zAccumulator.__init__rowr   returnboolc                 C  s   dS )zskips distinct testTr-   r,   r0   r-   r-   r.   r'   1      zAccumulator.dont_carec                 C  s   t | j|| jvS )ztests distinct with set)r   r%   r*   r3   r-   r-   r.   r(   5   s   zAccumulator.use_rowbindings$MutableMapping[Variable, Identifier]Nonec                 C  s   |   || j< dS )zsets final value in bindingsN)	get_valuer#   r,   r5   r-   r-   r.   	set_value9   s   zAccumulator.set_valueNr    r   r0   r   r1   r2   r5   r6   r1   r7   )__name__
__module____qualname____doc__r/   r'   r(   r:   r-   r-   r-   r.   r   !   s    


r   c                      sP   e Zd Zd fddZdddZdddZdddZdddZdddZ  Z	S )Counterr    r   c                   s0   t t| | d| _| jdkr| j| _d S d S )Nr   *)superrB   r/   valuer%   eval_full_roweval_rowr+   	__class__r-   r.   r/   @   s
   
zCounter.__init__r0   r   
aggregator
Aggregatorr1   r7   c                 C  sL   z|  |}W n
 ty   Y d S w |  jd7  _| jr$| j| d S d S N   )rG   r   rE   r&   r*   add)r,   r0   rJ   valr-   r-   r.   updateH   s   zCounter.updater   c                 C  s
   t | jS N)r   rE   r,   r-   r-   r.   r8   R   s   
zCounter.get_valuer   c                 C  s   t | j|S rQ   )r   r%   r3   r-   r-   r.   rG   U   s   zCounter.eval_rowc                 C  s   |S rQ   r-   r3   r-   r-   r.   rF   X      zCounter.eval_full_rowr2   c                 C  s(   z	|  || jvW S  ty   Y dS w r!   )rG   r*   r   r3   r-   r-   r.   r(   [   s
   zCounter.use_rowr;   r0   r   rJ   rK   r1   r7   r1   r   )r0   r   r1   r   )r0   r   r1   r   r<   )
r>   r?   r@   r/   rP   r8   rG   rF   r(   __classcell__r-   r-   rH   r.   rB   ?   s    




rB   argsintr1   
Tuple[int]c                  G     d S rQ   r-   rW   r-   r-   r.   type_safe_numbersc   rS   r\   Union[Decimal, float, int]Tuple[Union[float, int]]c                  G  rZ   rQ   r-   r[   r-   r-   r.   r\   g   s   Iterable[Union[float, int]]c                  G  s2   t dd | D rt dd | D rtt| S | S )Nc                 s      | ]}t |tV  qd S rQ   )
isinstancefloat.0argr-   r-   r.   	<genexpr>n   s    z$type_safe_numbers.<locals>.<genexpr>c                 s  r`   rQ   )ra   r   rc   r-   r-   r.   rf   n   s    

)anymaprb   r[   r-   r-   r.   r\   m   s
   
c                      2   e Zd Zd fddZdddZdddZ  ZS )Sumr    r   c                   s    t t| | d| _d | _d S Nr   )rD   rj   r/   rE   datatyper+   rH   r-   r.   r/   x   s   
zSum.__init__r0   r   rJ   rK   r1   r7   c                 C  s~   z4t | j|}| j}|d u r|j}nt||j}|| _tt| jt|| _| jr2| j	
| W d S W d S  ty>   Y d S w rQ   )r   r%   rl   r   sumr\   rE   r   r&   r*   rN   r   r,   r0   rJ   rE   dtr-   r-   r.   rP   }   s   z
Sum.updater   c                 C  s   t | j| jdS )N)rl   )r   rE   rl   rR   r-   r-   r.   r8      s   zSum.get_valuer;   rT   rU   r>   r?   r@   r/   rP   r8   rV   r-   r-   rH   r.   rj   w   s    
rj   c                      ri   )Averager    r   c                   s&   t t| | d| _d| _d | _d S rk   )rD   rq   r/   counterrm   rl   r+   rH   r-   r.   r/      s   
zAverage.__init__r0   r   rJ   rK   r1   r7   c                 C  s   z8t | j|}| j}tt| jt|| _|d u r|j}nt||j}|| _| jr/| j	| |  j
d7  _
W d S  tyB   Y d S  tyK   Y d S w rL   )r   r%   rl   rm   r\   r   r   r&   r*   rN   rr   r   r   rn   r-   r-   r.   rP      s    zAverage.updater   c                 C  sL   | j dkr	tdS | jtjtjfv rt| j| j  S tt| jt| j  S rk   )rr   r   rl   r   rb   doublerm   r   rR   r-   r-   r.   r8      s
   
zAverage.get_valuer;   rT   rU   rp   r-   r-   rH   r.   rq      s    
rq   c                      s6   e Zd ZdZd fddZdd
dZdddZ  ZS )Extremumz+abstract base class for Minimum and Maximumr    r   c                   s&   |  t t| | d | _| j| _d S rQ   )rD   rt   r/   rE   r'   r(   r+   rH   r-   r.   r/      s   zExtremum.__init__r5   r6   r1   r7   c                 C  s"   | j d urt| j || j< d S d S rQ   )rE   r   r#   r9   r-   r-   r.   r:      s   
zExtremum.set_valuer0   r   rJ   rK   c                 C  sd   z| j d u rt| j|| _ W d S | | j t| j|| _ W d S  ty(   Y d S  ty1   Y d S w rQ   )rE   r   r%   comparer   r   r,   r0   rJ   r-   r-   r.   rP      s   
zExtremum.updater;   r=   rT   )r>   r?   r@   rA   r/   r:   rP   rV   r-   r-   rH   r.   rt      s
    
rt   _ValueTc                   @     e Zd ZdddZdS )	Minimumval1rw   val2r1   c                 C     t ||tdS N)key)minr   r,   rz   r{   r-   r-   r.   ru         zMinimum.compareNrz   rw   r{   rw   r1   rw   r>   r?   r@   ru   r-   r-   r-   r.   ry          ry   c                   @  rx   )	Maximumrz   rw   r{   r1   c                 C  r|   r}   )maxr   r   r-   r-   r.   ru      r   zMaximum.compareNr   r   r-   r-   r-   r.   r      r   r   c                      s4   e Zd ZdZ fddZdd
dZdddZ  ZS )Sampleztakes the first eligible valuec                   s   t t| | | j| _d S rQ   )rD   r   r/   r'   r(   r+   rH   r-   r.   r/      s   zSample.__init__r0   r   rJ   rK   r1   r7   c                 C  s:   zt | j||j| j< |j| j= W d S  ty   Y d S w rQ   )r   r%   r5   r#   accumulatorsr   rv   r-   r-   r.   rP      s   zSample.updatec                 C  rZ   rQ   r-   rR   r-   r-   r.   r8      r4   zSample.get_valuerT   )r1   r7   )r>   r?   r@   rA   r/   rP   r8   rV   r-   r-   rH   r.   r      s
    
	r   c                      s<   e Zd ZU ded< d fddZdddZdddZ  ZS )GroupConcatzList[Literal]rE   r    r   c                   s6   t t| | g | _|jd u rd| _d S |j| _d S )N )rD   r   r/   rE   	separatorr+   rH   r-   r.   r/      s
   

zGroupConcat.__init__r0   r   rJ   rK   r1   r7   c                 C  s\   z#t | j|}t|trW d S | j| | jr!| j| W d S W d S  ty-   Y d S w rQ   )	r   r%   ra   r   rE   appendr&   r*   rN   )r,   r0   rJ   rE   r-   r-   r.   rP     s   
zGroupConcat.updater   c                 C  s   t | jdd | jD S )Nc                 s  s    | ]}t |V  qd S rQ   )str)rd   vr-   r-   r.   rf     s    z(GroupConcat.get_value.<locals>.<genexpr>)r   r   joinrE   rR   r-   r-   r.   r8     s   zGroupConcat.get_valuer;   rT   rU   )r>   r?   r@   __annotations__r/   rP   r8   rV   r-   r-   rH   r.   r      s
   
 
	r   c                   @  sB   e Zd ZdZeeeeee	e
dZdddZdddZdddZdS )rK   z&combines different Accumulator objects)ZAggregate_CountZAggregate_SampleZAggregate_SumZAggregate_AvgZAggregate_MinZAggregate_MaxZAggregate_GroupConcataggregationsList[CompValue]c                 C  sN   i | _ i | _|D ]}| j|j}|d u rtd|j ||| j|j< qd S )NzUnknown aggregate function )r5   r   accumulator_classesgetname	Exceptionr"   )r,   r   aZaccumulator_classr-   r-   r.   r/   &  s   zAggregator.__init__r0   r   r1   r7   c                 C  s.   t | j D ]}||r|||  qdS )zupdate all own accumulatorsN)listr   valuesr(   rP   )r,   r0   accr-   r-   r.   rP   /  s
   
zAggregator.updateMapping[Variable, Identifier]c                 C  s"   | j  D ]}|| j q| jS )zcalculate and set last values)r   r   r:   r5   )r,   r   r-   r-   r.   get_bindings8  s   zAggregator.get_bindingsN)r   r   )r0   r   r1   r7   )r1   r   )r>   r?   r@   rA   rB   r   rj   rq   ry   r   r   r   r/   rP   r   r-   r-   r-   r.   rK     s    


		rK   N)rW   rX   r1   rY   )rW   r]   r1   r^   )rW   r]   r1   r_   )4rA   
__future__r   decimalr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   Zrdflib.namespacer   Zrdflib.plugins.sparql.datatypesr   Zrdflib.plugins.sparql.evalutilsr   r   Zrdflib.plugins.sparql.operatorsr   Z!rdflib.plugins.sparql.parserutilsr   Zrdflib.plugins.sparql.sparqlr   r   r   Zrdflib.termr   r   r   r   r   r   rB   r\   rj   rq   rt   rw   ry   r   r   r   rK   r-   r-   r-   r.   <module>   s6    <$

$!