B
    ²ô`p.  ã               @   sØ   d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZmZ G dd„ dee
ƒZG d	d
„ d
ee	ƒZddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdddgfdddgfgZdd„ ZedkrÔeƒ  dS ) zA
A model builder that makes use of the external 'Mace4' package.
é    N)Ú	is_indvar)Ú	ValuationÚ
Expression)ÚModelBuilderÚBaseModelBuilderCommand)ÚProver9CommandParentÚProver9Parentc               @   st   e Zd ZdZdZddd„Zedd„ ƒZdd	„ Ze	d
d„ ƒZ
e	dd„ ƒZe	dd„ ƒZdd„ Zdd„ Zg dfdd„ZdS )ÚMaceCommandz¸
    A ``MaceCommand`` specific to the ``Mace`` model builder.  It contains
    a print_assumptions() method that is used to print the list
    of assumptions in multiple formats.
    Néô  c             C   s4   |dk	rt |tƒs t‚nt|ƒ}t | |||¡ dS )a•  
        :param goal: Input expression to prove
        :type goal: sem.Expression
        :param assumptions: Input expressions to use as assumptions in
            the proof.
        :type assumptions: list(sem.Expression)
        :param max_models: The maximum number of models that Mace will try before
            simply returning false. (Use 0 for no maximum.)
        :type max_models: int
        N)Ú
isinstanceÚMaceÚAssertionErrorr   Ú__init__)ÚselfÚgoalÚassumptionsÚ
max_modelsZmodel_builder© r   úB/home/dcms/DCMS/lib/python3.7/site-packages/nltk/inference/mace.pyr       s    zMaceCommand.__init__c             C   s
   |   d¡S )NÚ	valuation)Úmodel)Úmbcr   r   r   r   2   s    zMaceCommand.valuationc       
      C   sº  |   |d¡}g }xž| d¡D ]Ž}| ¡ }| d¡r\t|| d¡d | d¡…  ¡ ƒ}q| d¡rÞ| d¡d	krÞ|| d¡d | d¡…  ¡ }t|ƒr¤| ¡ }t|| d
¡d | d¡…  ¡ ƒ}| 	|t
 |¡f¡ q| d¡r|| d¡d d… }d|krb|d| d¡…  ¡ }dd„ || d
¡d | d¡…  d¡D ƒ}	| 	|t
 ||	¡f¡ q|d| d¡…  ¡ }t|| d
¡d | d¡…  ¡ ƒ}| 	||dkf¡ qW t|ƒS )z¦
        Transform the output file into an NLTK-style Valuation.

        :return: A model if one is generated; None otherwise.
        :rtype: sem.Valuation
        ÚstandardFZinterpretationú(é   ú,ÚfunctionÚ_éÿÿÿÿú[ú]ZrelationNc             S   s   g | ]}t | ¡ ƒ‘qS r   )ÚintÚstrip)Ú.0Úvr   r   r   ú
<listcomp>U   s   z,MaceCommand._convert2val.<locals>.<listcomp>)Ú_transform_outputÚ
splitlinesr"   Ú
startswithr!   ÚindexÚfindr   ÚupperÚappendr	   Ú_make_model_varÚsplitÚ_make_relation_setr   )
r   Úvaluation_strZvaluation_standard_formatÚvalÚlineÚlÚnum_entitiesÚnameÚvalueÚvaluesr   r   r   Ú_convert2val6   s0    
& $

&$zMaceCommand._convert2valc          	   C   s>   t ƒ }x2dd„ t|ƒD ƒD ]}| tt ||| ¡ƒ¡ qW |S )a]  
        Convert a Mace4-style relation table into a dictionary.

        :param num_entities: the number of entities in the model; determines the row length in the table.
        :type num_entities: int
        :param values: a list of 1's and 0's that represent whether a relation holds in a Mace4 model.
        :type values: list of int
        c             S   s   g | ]\}}|d kr|‘qS )r   r   )r#   Úposr$   r   r   r   r%   n   s    z2MaceCommand._make_relation_set.<locals>.<listcomp>)ÚsetÚ	enumerateÚaddÚtupler	   Ú_make_relation_tuple)r4   r7   ÚrÚpositionr   r   r   r/   c   s
    
zMaceCommand._make_relation_setc             C   sf   t |ƒdkrg S t |ƒ| }| | }t| | ƒ}||| |d | … }t |¡gt |||¡ S d S )Nr   )Úlenr!   r	   r-   r>   )r@   r7   r4   Zsublist_sizeZsublist_startZsublist_positionZsublistr   r   r   r>   t   s    
z MaceCommand._make_relation_tuplec             C   s\   ddddddddd	d
ddddddddddddddddg|  }| d }|dkrX|t |ƒ S |S )z³
        Pick an alphabetic character as identifier for an entity in the model.

        :param value: where to index into the list of characters
        :type value: int
        ÚaÚbÚcÚdÚeÚfÚgÚhÚiÚjÚkr3   ÚmÚnÚoÚpÚqr?   ÚsÚtÚur$   ÚwÚxÚyÚzé   r   )Ústr)r6   ÚletterÚnumr   r   r   r-   †   s:    	zMaceCommand._make_model_varc             C   s*   |s|S |dkr|   |¡S |  ||¡S dS )a^  
        Print out a Mace4 model using any Mace4 ``interpformat`` format.
        See http://www.cs.unm.edu/~mccune/mace4/manual/ for details.

        :param valuation_str: str with the model builder's output
        :param format: str indicating the format for displaying
        models. Defaults to 'standard' format.
        :return: str
        r   N)r8   r&   )r   r0   Úformatr   r   r   Ú_decorate_model­   s
    

zMaceCommand._decorate_modelc             C   s&   |dkr|   ||g¡d S tdƒ‚dS )zª
        Transform the output file into any Mace4 ``interpformat`` format.

        :param format: Output format for displaying models.
        :type format: str
        )r   Z	standard2ÚportableZtabularÚrawÚcookedÚxmlZtexr   z#The specified format does not existN)Ú_call_interpformatÚLookupError)r   r0   r]   r   r   r   r&   ¾   s    
zMaceCommand._transform_outputFc             C   s.   | j dkr| j d|¡| _ | j || j ||¡S )a  
        Call the ``interpformat`` binary with the given input.

        :param input_str: A string whose contents are used as stdin.
        :param args: A list of command-line arguments.
        :return: A tuple (stdout, returncode)
        :see: ``config_prover9``
        NZinterpformat)Ú_interpformat_binZ_modelbuilderÚ_find_binaryÚ_call)r   Ú	input_strÚargsÚverboser   r   r   rc   Ó   s
    	

zMaceCommand._call_interpformat)NNr
   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__re   r   Úpropertyr   r8   Ústaticmethodr/   r>   r-   r^   r&   rc   r   r   r   r   r	      s   
-'r	   c               @   s2   e Zd ZdZd
dd„Zddd„Zg dfdd	„ZdS )r   Néô  c             C   s
   || _ d S )N)Ú	_end_size)r   Zend_sizer   r   r   r   é   s    zMace.__init__Fc             C   s.   |sg }| j |  ||¡|d\}}|dk|fS )z 
        Use Mace4 to build a first order model.

        :return: ``True`` if a model was found (i.e. Mace returns value of 0),
        else ``False``
        )rj   r   )Ú_call_mace4Zprover9_input)r   r   r   rj   ÚstdoutÚ
returncoder   r   r   Ú_build_modelî   s
    zMace._build_modelc             C   sN   | j dkr|  d|¡| _ d}| jdkr4|d| j 7 }||7 }|  || j ||¡S )a  
        Call the ``mace4`` binary with the given input.

        :param input_str: A string whose contents are used as stdin.
        :param args: A list of command-line arguments.
        :return: A tuple (stdout, returncode)
        :see: ``config_prover9``
        NZmace4Ú r   zassign(end_size, %d).

)Ú
_mace4_binrf   rr   rg   )r   rh   ri   rj   Zupdated_input_strr   r   r   rs   ý   s    	

zMace._call_mace4)rq   )NNF)rk   rl   rm   rx   r   rv   rs   r   r   r   r   r   æ   s   

r   é   c             C   s   t d|  ƒ d S )Nú-)Úprint)r\   r   r   r   Úspacer  s    r|   c             C   s   ddddœ|  S )zq
    Decode the result of model_found()

    :param found: The output of model_found()
    :type found: bool
    zCountermodel foundzNo countermodel foundÚNone)TFNr   )Úfoundr   r   r   Údecode_result  s    
r   c             C   sr   xl| D ]d\}}t  |¡}dd„ |D ƒ}t||dd}| ¡ }x|D ]}td| ƒ qBW td|t|ƒf ƒ qW dS )z2
    Try some proofs and exhibit the results.
    c             S   s   g | ]}t  |¡‘qS r   )ÚlpÚparse)r#   rB   r   r   r   r%   '  s    z$test_model_found.<locals>.<listcomp>é2   )r   r   z   %sz
|- %s: %s
N)r   Ú
fromstringr	   Úbuild_modelr{   r   )Ú	argumentsr   r   rH   ÚalistrM   r~   rB   r   r   r   Útest_model_found!  s    

r‡   c             C   s–   t  d¡}dd„ dD ƒ}t||d}| ¡  tƒ  tdƒ tƒ  x|D ]}td| ƒ qFW td|t| ¡ ƒf ƒ tƒ  td	ƒ tƒ  t|jd
ƒ dS )z0
    Try to build a ``nltk.sem.Valuation``.
    zall x.man(x)c             S   s   g | ]}t  |¡‘qS r   )r   rƒ   )r#   rB   r   r   r   r%   5  s   z$test_build_model.<locals>.<listcomp>)z	man(John)zman(Socrates)z	man(Bill)z,some x.(-(x = John) & man(x) & sees(John,x))zsome x.(-(x = Bill) & man(x))z,all x.some y.(man(x) -> gives(Socrates,x,y)))r   zAssumptions and Goalz   %sz
|- %s: %s
r   Ú
N)r   rƒ   r	   r„   r|   r{   r   r   )r…   rH   r†   rM   rB   r   r   r   Útest_build_model/  s    


r‰   c             C   sœ   t  | d ¡}dd„ | d D ƒ}t||d}| ¡  x|D ]}td| ƒ q:W td|| ¡ f ƒ x4dD ],}tƒ  td	| ƒ tƒ  t|j|d
ƒ qhW dS )zJ
    Transform the model into various Mace4 ``interpformat`` formats.
    r   c             S   s   g | ]}t  |¡‘qS r   )r€   r   )r#   rB   r   r   r   r%   U  s    z)test_transform_output.<locals>.<listcomp>r   )r   z   %sz
|- %s: %s
)r   r_   rb   ra   zUsing '%s' format)r]   N)r   rƒ   r	   r„   r{   r|   r   )Zargument_pairrH   r†   rM   rB   r]   r   r   r   Útest_transform_outputP  s    

rŠ   c               C   s„   t tjddddgdtddgƒkƒ t tjddddddddddg	dtdgƒkƒ t tjdddddddddgdtd	d
gƒkƒ d S )Né   r   r   )r4   r7   )rD   )rB   )rD   rB   é   )rB   rC   rB   )rC   rC   rB   )r{   r	   r/   r:   r   r   r   r   Útest_make_relation_setb  s    r   zmortal(Socrates)zall x.(man(x) -> mortal(x))zman(Socrates)z(not mortal(Socrates))c               C   s    t tƒ ttƒ ttd ƒ d S )Nr   )r‡   r…   r‰   rŠ   r   r   r   r   Údemoy  s    rŽ   Ú__main__)ry   )rn   ÚosÚtempfileZnltk.sem.logicr   Znltk.semr   r   Znltk.inference.apir   r   Znltk.inference.prover9r   r   r	   r   r|   r   r‡   r‰   rŠ   r   r…   rŽ   rk   r   r   r   r   Ú<module>   s(    P+
!
