ó
ù`]c           @  s2  d  Z  d d l m 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 d e e f d	 „  ƒ  YZ d
 e e f d „  ƒ  YZ d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d g f d d d g f g Z d „  Z e d k r.e ƒ  n  d S(   sA   
A model builder that makes use of the external 'Mace4' package.
iÿÿÿÿ(   t   print_functionN(   t	   is_indvar(   t	   Valuationt
   Expression(   t   ModelBuildert   BaseModelBuilderCommand(   t   Prover9CommandParentt   Prover9Parentt   MaceCommandc           B  s   e  Z d  Z d Z d d d d d „ Z e d „  ƒ Z d „  Z e	 d „  ƒ Z
 e	 d „  ƒ Z e	 d „  ƒ Z d „  Z d	 „  Z g  e d
 „ Z RS(   s¸   
    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.
    iô  c         C  sJ   | d k	 r$ t | t ƒ s0 t ‚ n t | ƒ } t j |  | | | ƒ d S(   s•  
        :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(   t   Nonet
   isinstancet   Macet   AssertionErrorR   t   __init__(   t   selft   goalt   assumptionst
   max_modelst   model_builder(    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyR   !   s    c         C  s   |  j  d ƒ S(   Nt	   valuation(   t   model(   t   mbc(    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyR   3   s    c         C  s[  |  j  | d ƒ } g  } x6| j t ƒ D]%} | j ƒ  } | j d ƒ r{ t | | j d ƒ d | j d ƒ !j ƒ  ƒ } q( | j d ƒ r1| j d ƒ d k r1| | j d ƒ d | j d ƒ !j ƒ  } t | ƒ rã | j	 ƒ  } n  t | | j d	 ƒ d | j d
 ƒ !j ƒ  ƒ } | j
 | t j | ƒ f ƒ q( | j d ƒ r( | | j d ƒ d } d | k ré| | j d ƒ  j ƒ  } g  | | j d	 ƒ d | j d
 ƒ !j d ƒ D] }	 t |	 j ƒ  ƒ ^ q©}
 | j
 | t j | |
 ƒ f ƒ qM| | j d ƒ  j ƒ  } t | | j d	 ƒ d | j d
 ƒ !j ƒ  ƒ } | j
 | | d k f ƒ q( q( Wt | ƒ S(   s¦   
        Transform the output file into an NLTK-style Valuation.

        :return: A model if one is generated; None otherwise.
        :rtype: sem.Valuation
        t   standardt   interpretationt   (i   t   ,t   functiont   _iÿÿÿÿt   [t   ]t   relation(   t   _transform_outputt
   splitlinest   Falset   stript
   startswitht   intt   indext   findR   t   uppert   appendR   t   _make_model_vart   splitt   _make_relation_setR   (   R   t   valuation_strt   valuation_standard_formatt   valt   linet   lt   num_entitiest   namet   valuet   vt   values(    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyt   _convert2val7   s0    2$)/H/ c         C  sk   t  ƒ  } x[ g  t | ƒ D] \ } } | d k r | ^ q D]( } | j t t j | | |  ƒ ƒ ƒ q; W| S(   s]  
        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
        i   (   t   sett	   enumeratet   addt   tupleR   t   _make_relation_tuple(   R1   R5   t   rt   posR4   t   position(    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyR+   d   s
    
	8 c         C  s€   t  | ƒ d k r g  St  | ƒ | } |  | } t |  | ƒ } | | | | d | !} t j | ƒ g t j | | | ƒ Sd  S(   Ni   (   t   lenR$   R   R)   R;   (   R>   R5   R1   t   sublist_sizet   sublist_startt   sublist_positiont   sublist(    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyR;   u   s    
c         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 r| | t  | ƒ S| S(   s³   
        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
        t   at   bt   ct   dt   et   ft   gt   ht   it   jt   kR0   t   mt   nt   ot   pt   qR<   t   st   tt   uR4   t   wt   xt   yt   zi   i    (   t   str(   R3   t   lettert   num(    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyR)   ‡   s:    	
c         C  s7   | s
 | S| d k r# |  j  | ƒ S|  j | | ƒ Sd S(   s^  
        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(   R6   R   (   R   R,   t   format(    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyt   _decorate_model®   s
    
c      	   C  s3   | d k r# |  j  | | g ƒ d	 St d
 ƒ ‚ d S(   sª   
        Transform the output file into any Mace4 ``interpformat`` format.

        :param format: Output format for displaying models.
        :type format: str
        R   t	   standard2t   portablet   tabulart   rawt   cookedt   xmlt   texi    s#   The specified format does not existN(   R   R`   Ra   Rb   Rc   Rd   Re   Rf   (   t   _call_interpformatt   LookupError(   R   R,   R^   (    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyR   ¿   s           	c         C  sF   |  j  d k r* |  j j d | ƒ |  _  n  |  j j | |  j  | | ƒ S(   s  
        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``
        t   interpformatN(   t   _interpformat_binR	   t   _modelbuildert   _find_binaryt   _call(   R   t	   input_strt   argst   verbose(    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyRg   Ô   s
    			N(   t   __name__t
   __module__t   __doc__R	   Rj   R   t   propertyR   R6   t   staticmethodR+   R;   R)   R_   R   R!   Rg   (    (    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyR      s   	-'		R   c           B  s;   e  Z d Z d  d „ Z d d e d „ Z g  e d „ Z RS(   iô  c         C  s   | |  _  d  S(   N(   t	   _end_size(   R   t   end_size(    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyR   ê   s    	c         C  sF   | s g  } n  |  j  |  j | | ƒ d | ƒ\ } } | d k | f S(   s    
        Use Mace4 to build a first order model.

        :return: ``True`` if a model was found (i.e. Mace returns value of 0),
        else ``False``
        Rp   i    (   t   _call_mace4t   prover9_input(   R   R   R   Rp   t   stdoutt
   returncode(    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyt   _build_modelï   s
    	!c         C  ss   |  j  d k r' |  j d | ƒ |  _  n  d } |  j d k rP | d |  j 7} n  | | 7} |  j | |  j  | | ƒ S(   s  
        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``
        t   mace4t    i    s   assign(end_size, %d).

N(   t
   _mace4_binR	   Rl   Rv   Rm   (   R   Rn   Ro   Rp   t   updated_input_str(    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyRx   þ   s    	
N(   Rq   Rr   R	   R   R   R!   R|   Rx   (    (    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyR   ç   s   i   c         C  s   t  d |  ƒ d  S(   Nt   -(   t   print(   R]   (    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyt   spacer  s    c         C  s   i d t  6d t 6d d 6|  S(   sq   
    Decode the result of model_found()

    :param found: The output of model_found()
    :type found: bool
    s   Countermodel founds   No countermodel foundR	   N(   t   TrueR!   R	   (   t   found(    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyt   decode_result  s    c         C  s©   x¢ |  D]š \ } } t  j | ƒ } g  | D] } t j | ƒ ^ q) } t | d | d d ƒ} | j ƒ  } x | D] } t d | ƒ qo Wt d | t | ƒ f ƒ q Wd S(   s2   
    Try some proofs and exhibit the results.
    R   R   i2   s      %ss
   |- %s: %s
N(   R   t
   fromstringt   lpt   parseR   t   build_modelR‚   R†   (   t	   argumentsR   R   RJ   RD   t   alistRO   R…   (    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyt   test_model_found"  s    "c         C  sâ   t  j d ƒ } g  d d d d d d g D] } t  j | ƒ ^ q( } t | d | ƒ} | j ƒ  t ƒ  t d	 ƒ t ƒ  x | D] } t d
 | ƒ q~ Wt d | t | j ƒ  ƒ f ƒ t ƒ  t d ƒ t ƒ  t | j d ƒ d S(   s0   
    Try to build a ``nltk.sem.Valuation``.
    s   all x.man(x)s	   man(John)s   man(Socrates)s	   man(Bill)s,   some x.(-(x = John) & man(x) & sees(John,x))s   some x.(-(x = Bill) & man(x))s,   all x.some y.(man(x) -> gives(Socrates,x,y))R   s   Assumptions and Goals      %ss
   |- %s: %s
R   s   
N(   R   R‡   R   RŠ   Rƒ   R‚   R†   R   (   R‹   RJ   RD   RŒ   RO   (    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyt   test_build_model0  s(    "

 
c         C  sá   t  j |  d ƒ } g  |  d D] } t j | ƒ ^ q } t | d | ƒ} | j ƒ  x | D] } t d | ƒ q\ Wt d | | j ƒ  f ƒ xL d d d d	 g D]8 } t ƒ  t d
 | ƒ t ƒ  t | j d | ƒ ƒ q¡ Wd S(   sJ   
    Transform the model into various Mace4 ``interpformat`` formats.
    i    i   R   s      %ss
   |- %s: %s
R   Ra   Re   Rd   s   Using '%s' formatR^   N(	   R   R‡   Rˆ   R‰   R   RŠ   R‚   Rƒ   R   (   t   argument_pairRJ   RD   RŒ   RO   R^   (    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyt   test_transform_outputQ  s    &
c           C  sÇ   t  t j d d d d d d g ƒ t d
 d g ƒ k ƒ t  t j d d d d d d d d d d d d g	 ƒ t d g ƒ k ƒ t  t j d d d d d d d d d d d g ƒ t d d g ƒ k ƒ d  S(   NR1   i   R5   i   i    RF   RD   i   RE   (   RF   (   RD   (   RF   RD   (   RD   RE   RD   (   RE   RE   RD   (   R‚   R   R+   R7   (    (    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyt   test_make_relation_setc  s    	'-s   mortal(Socrates)s   all x.(man(x) -> mortal(x))s   man(Socrates)s   (not mortal(Socrates))c           C  s&   t  t ƒ t t ƒ t t d ƒ d  S(   Ni   (   R   R‹   RŽ   R   (    (    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyt   demoz  s    

t   __main__(   Rs   t
   __future__R    t   ost   tempfilet   nltk.sem.logicR   t   nltk.semR   R   t   nltk.inference.apiR   R   t   nltk.inference.prover9R   R   R   R   Rƒ   R†   R   RŽ   R   R‘   R‹   R’   Rq   (    (    (    s2   lib/python2.7/site-packages/nltk/inference/mace.pyt   <module>   s(   Ï+			!			