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

// -*-Mode: Java-*- 
// $Id: WSentence.java,v 1.14 2003/03/06 00:40:48 moltmans Exp $
// WSentence.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Thu Feb  3 19:03:17 2000> 
// Time-stamp: <2001-02-25 22:33:59 moltmans> 
package rec.annotate.widgets;

import java.util.*;
import rec.sys.*;
import rec.util.*;
import rec.annotate.facts.*;
import rec.annotate.rules.*;
import rec.speech.Tag;
import rec.annotate.lisp.Lisp;

/**
 * Sentence widget for further processing by individual recognizers.
 **/
public class WSentence extends Widget implements WAnnotation
{
  private WUtterance m_utterance;
  private Sentence m_sentenceFact;

  public WSentence( Surface s, WUtterance utterance )
  {
    m_utterance = utterance;
  }

  public void makeDefaultSuitcase( Surface s )
  {
    Assert.check( m_sentenceFact == null,
		  "Remaking SentenceFact" );
    m_sentenceFact = new Sentence( s, m_utterance.getTags(),
				   this );

    DoTransition dt = new DoTransition(s) {
      public void doDo( )
      {
	Notebook book = m_Surface.getNotebook();
	DebugWindow.addLine("Tags", m_utterance.getTags());
	
	assumeSentence(book);
//  	book.assume( "(sentence " + book.getSymbol(m_sentenceFact) + ")",
//  		     ":REC-SYSTEM" );
	book.updateNotes();
      }
      public void doUndo( )
      {
	Notebook book = m_Surface.getNotebook();
	book.retract("(sentence " + book.getSymbol(m_sentenceFact) + ")",
		     ":REC-SYSTEM" );
	book.updateNotes();
      }
      public String getAction( )
      {
	return "WSentence: " + m_sentenceFact;
      }
    };
    addToSuitcase( dt );
  }

  public Annotation getAnnotationFact() 
  {
    return m_sentenceFact;
  }

  private void assumeSentence(Notebook book) 
  {
    String symbol = book.getSymbol(this);
    Tag tags = m_utterance.getTags();
    List roots = new LinkedList();
    roots.add(symbol);
    assumeTags(book, tags, roots);
    book.assumeTimeStamp(this);
    book.assume(new String[] {"sentence", symbol,
			      Lisp.list(getTagSymbols(book))},
		":REC-SYSTEM");
		
  }

  private Set getTagSymbols(Notebook book) 
  {
    HashSet set = new HashSet();
    LinkedList list = new LinkedList();
    list.add(m_utterance.getTags());

    while (!list.isEmpty()) {
      Tag tag = (Tag)list.removeFirst();
      set.add(book.getSymbol(tag));
      list.addAll(tag.getSubTags());
    }
    return set;
  }
  
  /**
   * Assertions are of the form:
   *   (tag-name tag-text list-of-parent-tag-ids tag-id)
   **/
  private void assumeTags(Notebook book, Tag tags, List roots)
  {
    String symbol = book.getSymbol(tags);
    Collection tagList = tags.getTags();
    Iterator it = tagList.iterator();
    while (it.hasNext()) {
      String tag = (String)it.next();
      book.assume(new String[] {"u-" + tag, Lisp.quotes(tags.getText()),
				Lisp.list(roots), symbol },
		  ":REC-SYSTEM");
    }

    Iterator subs = tags.getSubTags().iterator();

    while ( subs.hasNext() ) {
      Tag sub = (Tag)subs.next();
      List newRoots = new LinkedList(roots);
      newRoots.add(symbol);
      assumeTags(book, sub, newRoots);
    }
  }
}

