³ò
4ÒÇIc        
   @   s  d  d k  Z  d  d k Z d  d k Td  d k l Z d  d k l Z l Z d  d k Td 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 e d j o& e e ƒ e e ƒ e e d ƒ n d S(   iÿÿÿÿN(   t   *(   t	   Valuation(   t   ModelBuildert   BaseModelBuilderCommandt   MaceCommandc           B   s   e  Z d  Z e Z e e d e 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 L{MaceCommand} specific to the L{Mace} model builder.  It contains
    the a print_assumptions() method that is used to print the list
    of assumptions in multiple formats.
    i<   c         C   sN   | d j	 o t | t ƒ p t ‚ n t | ƒ } t i |  | | | ƒ d S(   su  
        @param goal: Input expression to prove
        @type goal: L{logic.Expression}
        @param assumptions: Input expressions to use as assumptions in
            the proof.
        @type assumptions: C{list} of L{logic.Expression}
        @param timeout: number of seconds before timeout; set to 0 for
            no timeout.
        @type timeout: C{int}
        N(   t   Nonet
   isinstancet   Macet   AssertionErrorR   t   __init__(   t   selft   goalt   assumptionst   timeoutt   model_builder(    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyR	      s    c         C   s   |  i  d  ƒ S(   t	   valuation(   t   model(   t   mbc(    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyt   <lambda>/   s    c         C   sn  |  i  | d ƒ } g  } xI| i t ƒ D]8} | i ƒ  } | i d ƒ o3 t | | i d ƒ d | i d ƒ !i ƒ  ƒ } q( | i d ƒ o« | i d ƒ d j o• | | i d ƒ d | i d ƒ !i ƒ  } t | ƒ o | i	 ƒ  } n t | | i d	 ƒ d | i d
 ƒ !i ƒ  ƒ } | i
 | t i | ƒ f ƒ q( | i d ƒ o| | i d ƒ d } d | j o | | i d ƒ  i ƒ  } g  }	 | | i d	 ƒ d | i d
 ƒ !i d ƒ D] }
 |	 t |
 i ƒ  ƒ q¶~	 } | i
 | t i | | ƒ f ƒ q`| | i d ƒ  i ƒ  } t | | i d	 ƒ d | i d
 ƒ !i ƒ  ƒ } | i
 | | d j 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: L{nltk.sem.Valuation} 
        t   standardt   interpretationt   (i   t   ,t   functiont   _iÿÿÿÿt   [t   ]t   relation(   t   _transform_outputt
   splitlinest   Falset   stript
   startswitht   intt   indext   findt	   is_indvart   uppert   appendR   t   _make_model_vart   splitt   _make_relation_setR   (   R
   t   valuation_strt   valuation_standard_formatt   valt   linet   lt   num_entitiest   namet   valuet   _[1]t   vt   values(    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyt   _convert2val1   s.     3&)/ S#/!c      	   C   sx   t  ƒ  } xh g  } t | ƒ D]$ \ } } | d j o | | q q ~ D]( } | i t t i | | |  ƒ ƒ ƒ qH W| S(   sv  
        Convert a Mace4-style relation table into a dictionary.
        
        @parameter num_entities: the number of entities in the model; determines the row length in the table.
        @type num_entities: C{int}
        @parameter values: a list of 1's and 0's that represent whether a relation holds in a Mace4 model.
        @type values: C{list} of C{int}
        i   (   t   sett	   enumeratet   addt   tupleR   t   _make_relation_tuple(   R/   R4   t   rR2   t   posR3   t   position(    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyR)   Y   s
    
	? &c         C   s   t  | ƒ d j o g  Sna t  | ƒ | } |  | } |  | } | | | | d | !} t i | ƒ g t i | | | ƒ Sd  S(   Ni   (   t   lenR   R'   R:   (   R=   R4   R/   t   sublist_sizet   sublist_startt   sublist_positiont   sublist(    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyR:   h   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 |  } t  |  ƒ d } | d j o | t | ƒ Sn | Sd S(   sÂ   
        Pick an alphabetic character as identifier for an entity in the model.
        
        @parameter value: where to index into the list of characters
        @type value: C{int}
        t   at   bt   ct   dt   et   ft   gt   ht   it   jt   kR.   t   mt   nt   ot   pt   qR;   t   st   tt   uR3   t   wt   xt   yt   zi   i    N(   R!   t   str(   R1   t   lettert   num(    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyR'   w   s    *.c         C   sA   | p | Sn/ | d j o |  i  | ƒ Sn |  i | | ƒ Sd S(   ss  
        Print out a Mace4 model using any Mace4 C{interpformat} format. 
        See U{http://www.cs.unm.edu/~mccune/mace4/manual/} for details.
        
        @param valuation_str: C{str} with the model builder's output 
        @param format: C{str} indicating the format for displaying
        models. Defaults to 'standard' format.
        @return: C{str}
        R   N(   R5   R   (   R
   R*   t   format(    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyt   _decorate_model‡   s
    
c      	   C   s8   | d j o |  i  | | g ƒ d	 Sn t d
 ƒ ‚ d S(   sº   
        Transform the output file into any Mace4 C{interpformat} format. 
        
        @parameter format: Output format for displaying models. 
        @type format: C{str}
        R   t	   standard2t   portablet   tabulart   rawt   cookedt   xmlt   texi    s#   The specified format does not existN(   s   standardR_   R`   Ra   s   rawRc   s   xmlRe   (   t   _call_interpformatt   LookupError(   R
   R*   R]   (    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyR   ˜   s    
c         C   sH   |  i  t j o |  i i d | ƒ |  _  n |  i i | |  i  | | ƒ S(   s  
        Call the C{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: L{config_prover9}
        t   interpformat(   t   _interpformat_binR   t   _modelbuildert   _find_binaryt   _call(   R
   t	   input_strt   argst   verbose(    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyRf   ¥   s
    		(   t   __name__t
   __module__t   __doc__R   Ri   R	   t   propertyR   R5   t   staticmethodR)   R:   R'   R^   R   R   Rf   (    (    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyR      s   	(		R   c           B   s/   e  Z e Z e e e d  „ Z g  e d „ Z RS(   c         C   sH   | p
 g  } n |  i  |  i | | ƒ d | ƒ\ } } | d j | f S(   s®   
        Use Mace4 to build a first order model.
        
        @return: C{True} if a model was found (i.e. Mace returns value of 0),
        else C{False}        
        Ro   i    (   t   _call_mace4t   prover9_input(   R
   R   R   Ro   t   stdoutt
   returncode(    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyt   _build_model¹   s
    
c         C   sB   |  i  t j o |  i d | ƒ |  _  n |  i | |  i  | | ƒ S(   s  
        Call the C{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: L{config_prover9}
        t   mace4(   t
   _mace4_binR   Rk   Rl   (   R
   Rm   Rn   Ro   (    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyRu   Ç   s    	(   Rp   Rq   R   R{   R   Ry   Ru   (    (    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyR   ¶   s   i   c         C   s   d |  GHd  S(   Nt   -(    (   R\   (    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyt   spacerÖ   s    c         C   s#   h  d t  <d t <d t <|  S(   s   
    Decode the result of model_found() 
    
    @parameter found: The output of model_found() 
    @type found: C{boolean}
    s   Countermodel founds   No countermodel foundR   (   t   TrueR   R   (   t   found(    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyt   decode_resultÙ   s    c   
   	   C   s°   t  ƒ  } x  |  D]˜ \ } } | i | ƒ } g  } | D] } | | i | ƒ q6 ~ } t | d | d d ƒ} | i ƒ  }	 x | D] } d | GHq€ Wd | t |	 ƒ f GHq Wd S(   s2   
    Try some proofs and exhibit the results.
    R   R   i   s      %ss
   |- %s: %s
N(   t   LogicParsert   parseR   t   build_modelR€   (
   t	   argumentst   lpR   R   RI   R2   RC   t   alistRN   R   (    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyt   test_model_foundâ   s    	 * c         C   sÛ   t  ƒ  } | i d ƒ } g  } d d d d d d g D] } | | i | ƒ q5 ~ } t | d | ƒ} | i ƒ  t ƒ  d	 GHt ƒ  x | D] } d
 | GHqŠ Wd | t | i ƒ  ƒ f GHt ƒ  d GHt ƒ  | i Gd GHd S(   s/   
    Try to build a L{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…   RI   R2   RC   R†   RN   (    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyt   test_build_modelñ   s*    	
&
 c         C   sÞ   t  ƒ  } | i |  d ƒ } g  } |  d D] } | | i | ƒ q+ ~ } t | d | ƒ} | i ƒ  x | D] } d | GHqm Wd | | i ƒ  f GHxB d d d d	 g D]. } t ƒ  d
 | GHt ƒ  | i d | ƒ GHq¨ Wd S(   sI   
    Transform the model into various Mace4 C{interpformat} formats.
    i    i   R   s      %ss
   |- %s: %s
R   R`   Rd   Rc   s   Using '%s' formatR]   N(   R   R‚   R   Rƒ   R}   R   (   t   argument_pairR…   RI   R2   RC   R†   RN   R]   (    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyt   test_transform_output  s    	.
  	c           C   s¸   t  i d d d d d d g ƒ t d
 d g ƒ j GHt  i d d d d d d d d d d d d g	 ƒ t d g ƒ j GHt  i d d d d d d d d d d d g ƒ t d d g ƒ j GHd  S(   NR/   i   R4   i   i    RE   RC   i   RD   (   RE   (   RC   (   RE   RC   (   RC   RD   RC   (   RD   RD   RC   (   R   R)   R6   (    (    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pyt   test_make_relation_set  s    2As   mortal(Socrates)s   all x.(man(x) -> mortal(x))s   man(Socrates)s   (not mortal(Socrates))t   __main__i   (   t   ost   tempfilet   nltk.sem.logict   nltk.semR   t   apiR   R   t   prover9t   Prover9CommandParentR   t   Prover9ParentR   R}   R€   R‡   Rˆ   RŠ   R‹   R„   Rp   (    (    (    s)   /p/zhu/06/nlp/nltk/nltk/inference/mace.pys   <module>	   s&   

¡ 						

