3
dw                 @   s^   d Z ddlmZ ddlmZ ddlmZ ddlmZ G dd deZdd	 Z	e
d
krZe	  dS )ze
A commandline tool for testing if RDF graphs are isomorpic, i.e. equal
if BNode labels are ignored.
    )absolute_import)Graph)BNode)combinationsc                   sZ   e Zd ZdZ fddZdd Zdd Zdd	d
Zdd Zdd Z	dd Z
dd Z  ZS )IsomorphicTestableGraphz
    Ported from:
    http://www.w3.org/2001/sw/DataAccess/proto-tests/tools/rdfdiff.py
    (Sean B Palmer's RDF Graph Isomorphism Tester)
    c                s   t t| jf | d | _d S )N)superr   __init__hash)selfZkargs)	__class__ ?/tmp/pip-build-7vycvbft/rdflib/rdflib/tools/graphisomorphism.pyr      s    z IsomorphicTestableGraph.__init__c             C   s   t tt| j S )z
        This is defined instead of __hash__ to avoid a circular recursion
        scenario with the Memory store for rdflib which requires a hash
        lookup in order to return a generator of triples
        )r	   tuplesortedhashtriples)r
   r   r   r   internal_hash   s    z%IsomorphicTestableGraph.internal_hashc             #   s2   x, D ]$} fdd|D }t t|V  qW d S )Nc             3   s&   | ]}t |tr j|p|V  qd S )N)
isinstancer   vhash).0t)r
   r   r   	<genexpr>"   s    z6IsomorphicTestableGraph.hashtriples.<locals>.<genexpr>)r	   r   )r
   triplegr   )r
   r   r       s    
z#IsomorphicTestableGraph.hashtriplesFc             C   s   t t| j||S )N)r   r   vhashtriples)r
   termdoner   r   r   r   %   s    zIsomorphicTestableGraph.vhashc             c   s.   x(| D ] }||krt | j|||V  qW d S )N)r   vhashtriple)r
   r   r   r   r   r   r   r   (   s    
z$IsomorphicTestableGraph.vhashtriplesc             c   s\   xVt dD ]J}t|| ts(|| V  q
|s8|| |kr@|V  q
| j|| ddV  q
W d S )N   T)r   )ranger   r   r   )r
   r   r   r   pr   r   r   r   -   s    z#IsomorphicTestableGraph.vhashtriplec             C   sJ   t |tsdS t| t|kr"dS tjt| t|r:dS | j |j kS )zGraph isomorphism testing.FT)r   r   lenlist__eq__r   )r
   Gr   r   r   r"   6   s    
zIsomorphicTestableGraph.__eq__c             C   s   | j | S )z#Negative graph isomorphism testing.)r"   )r
   r#   r   r   r   __ne__@   s    zIsomorphicTestableGraph.__ne__)F)__name__
__module____qualname____doc__r   r   r   r   r   r   r"   r$   __classcell__r   r   )r   r   r      s   
	
r   c           
   C   s"  dd l } ddlm} d}||d}|jddddd	d
 |jdddddddddgdd |j \}}g }i }|jrt j| j|jd}|j	| d||< x0|D ](}	t j|	|jd}|j	| |	||< qW t
 }
xNt|dD ]@\}}||f|
kr||f|
kr||kstd|| || f qW d S )Nr   )OptionParserz,usage: %prog [options] file1 file2 ... fileN)usagez-sz--stdin
store_trueFzLoad from STDIN as well)actiondefaulthelpz--formatxmlinputFormatZ
RDF_FORMATZtrixZn3ntZrdfaz,The format of the RDF document(s) to comparez One of 'xml','n3','trix', 'nt', z#or 'rdfa'.  The default is %default)r.   destmetavarchoicesr/   )formatz(STDIN)   z%s != %szLThe format of the RDF document(s) to compareOne of 'xml','n3','trix', 'nt', zoThe format of the RDF document(s) to compareOne of 'xml','n3','trix', 'nt', or 'rdfa'.  The default is %default)sysoptparser*   
add_option
parse_argsstdinr   parser1   appendsetr   AssertionError)r8   r*   r+   opoptionsargsZgraphsZgraph2FNamegraphfncheckedZgraph1Zgraph2r   r   r   mainE   s>    
 


rG   __main__N)r(   
__future__r   Zrdflibr   r   	itertoolsr   r   rG   r%   r   r   r   r   <module>   s   8%