// (c) MIT 2003.  All rights reserved.

// $Id: Answer.java,v 1.4 2003/03/06 00:40:49 moltmans Exp $
// Answer.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Thu Jul 13 22:39:52 2000> 
// Time-stamp: <2001-02-28 16:03:38 moltmans> 
package rec.annotate.answers;

import java.util.*;
import rec.sys.*;
import rec.util.*;
import rec.speech.*;
import rec.annotate.widgets.*;
import rec.annotate.facts.*;
import rec.annotate.rules.*;
import rec.annotate.lisp.LispTMS;
import rec.annotate.lisp.LispObj;

/**
 **/
public class Answer
{
  private Notebook m_notes;

  public Answer( Notebook notes )
  {
    m_notes = notes;
  }
  
  public void answer( WUtterance utterance )
  {
    String subjectName = Sentence.getNoun( utterance.getTag( "subject" ) );
    
    LispTMS tms = m_notes.getTMS();
    LispObj obj = tms.readEval("(talk-about-causality \"" + subjectName +
			       "\")");
    String result = obj.getStringValue();
    if (result != null) {
      Say.say( result );
    }
    else {
       Say.say( "I'm sorry I don't understand constraints like " +
		subjectName +
		".  I only talk about bodies." );
    }
  }    
// old version of answer.  this is now handled in lisp...
//      if( body == null ) {
//        Say.say( "I'm sorry I don't understand constraints like " +
//  	       subjectName +
//  	       ".  I only talk about bodies." );
//        return;
//      }
//      HashSet relevent = new HashSet();
//      CausalStructure causal = m_notes.getCausalStructure();
//      Set events = m_notes.getEvents();
//      Iterator it = events.iterator();
//      while( it.hasNext() ) {
//        Event event = (Event)it.next();
//        if( event.getSubject().equals( body ) ) {
//  	relevent.add( causal.getCanonical( event ) );
//        }
//      }

//      if( relevent.isEmpty() ) {
//        Say.say( "I do not think that " + subjectName +
//  	       " is an important factor in the devices behavior" );
//        return;
//      }

//      if( utterance.hasTag( "what_role" ) ) {
//        TalkAboutCausality tac = new TalkAboutCausality( m_notes );
//        tac.answer( relevent );

//  //        Iterator related = relevent.iterator();
//  //        while( related.hasNext() ) {
//  //  	CanonicalEvent event = (CanonicalEvent)related.next();
//  //  	tac.answer( event );
//  //        }
//        return;
//      }

//      Iterator related = relevent.iterator();
//      while( related.hasNext() ) {
//        CanonicalEvent event = (CanonicalEvent)related.next();
//        Say.say( event.toEnglish() );
//        if( related.hasNext() ) {
//  	Say.say( "and" );	
//        }
//      }
//    }
}
