o
    iz)                     @  s  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,Z/G d%d& d&e'Z0G d'd( d(e'Z1G d)d* d*Z2d+S )/    )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-   [/var/www/edux/Edux_v2/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'Aggregator'r1   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   s   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-   rV   r-   r-   r.   type_safe_numbersc   r4   r[   Union[Decimal, float, int]Tuple[Union[float, int]]c                  G  rY   rQ   r-   rZ   r-   r-   r.   r[   h   r4   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   )r`   r   rb   r-   r-   r.   re   n   s    

)anymapra   rZ   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   ri   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%   rk   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)rk   )r   rE   rk   rR   r-   r-   r.   r8      s   zSum.get_valuer;   rS   rT   r>   r?   r@   r/   rP   r8   rU   r-   r-   rH   r.   ri   w   s    
ri   c                      rh   )Averager    r   c                   s&   t t| | d| _d| _d | _d S rj   )rD   rp   r/   counterrl   rk   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%   rk   rl   r[   r   r   r&   r*   rN   rq   r   r   rm   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 rj   )rq   r   rk   r   ra   doublerl   r   rR   r-   r-   r.   r8      s
   
zAverage.get_valuer;   rS   rT   ro   r-   r-   rH   r.   rp      s    
rp   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   rs   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=   rS   )r>   r?   r@   rA   r/   r:   rP   rU   r-   r-   rH   r.   rs      s
    
rs   _ValueTc                   @     e Zd ZdddZdS )	Minimumval1rv   val2r1   c                 C     t ||tdS N)key)minr   r,   ry   rz   r-   r-   r.   rt         zMinimum.compareNry   rv   rz   rv   r1   rv   r>   r?   r@   rt   r-   r-   r-   r.   rx          rx   c                   @  rw   )	Maximumry   rv   rz   r1   c                 C  r{   r|   )maxr   r   r-   r-   r.   rt      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   ru   r-   r-   r.   rP      s   zSample.updatec                 C  rY   rQ   r-   rR   r-   r-   r.   r8      r4   zSample.get_valuerS   )r1   r7   )r>   r?   r@   rA   r/   rP   r8   rU   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%   r`   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)rc   vr-   r-   r.   re     s    z(GroupConcat.get_value.<locals>.<genexpr>)r   r   joinrE   rR   r-   r-   r.   r8     s   zGroupConcat.get_valuer;   rS   rT   )r>   r?   r@   __annotations__r/   rP   r8   rU   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 )
Aggregatorz&combines different Accumulator objects)Aggregate_CountAggregate_SampleAggregate_SumAggregate_AvgAggregate_MinAggregate_Max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   a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_bindings7  s   zAggregator.get_bindingsN)r   r   )r0   r   r1   r7   )r1   r   )r>   r?   r@   rA   rB   r   ri   rp   rx   r   r   r   r/   rP   r   r-   r-   r-   r.   r     s    


		r   N)rV   rW   r1   rX   )rV   r\   r1   r]   )rV   r\   r1   r^   )3
__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   rB   r[   ri   rp   rs   rv   rx   r   r   r   r   r-   r-   r-   r.   <module>   s6    <$

$!