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

// -*-Mode: Java-*- 
// $Id: MovementUtteranceRecognizer.java,v 1.2 2003/03/06 00:40:43 moltmans Exp $
// MovementUtteranceRecognizer.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Thu Feb 10 23:20:06 2000> 
// Time-stamp: <2000-04-18 16:02:09 moltmans> 
package rec.annotate.recognizers;

import java.util.*;
import rec.util.*;
import rec.sys.*;
import rec.simulate.widgets.*;
import rec.annotate.*;
import rec.annotate.widgets.*;

/**
 * This class recognizes movement utterances and matches up the
 * subject of the utterance by finding a movement gesture.
 **/
public class MovementUtteranceRecognizer extends Recognizer
{

  public boolean recognize( Surface s, Widget w )
  {
    WSentence sentence = (WSentence)w;

//      if ( utterance.hasTag( "rotates" ) ) {
//        MovementAnnotation annotation = new RotationAnnotation( s, utterance );
//        WMovementAnnotation widget =
//  	new WMovementAnnotation( annotation, s.getAnnotationNotebook() );
      
//        s.replaceWidget( w, widget );
//        return true;
//      } 

    String mover = null;
    String movee = null;
    String path = null;
    
    if ( sentence.hasTag( "propels" ) ) {
      if ( sentence.hasDirectObject() ) {
	movee = sentence.getDirectObject();
	mover = sentence.getSubject();
      }
      // check for: "pushes against the lever"
      else if ( sentence.hasPrepositionalPhrase() ) {
	mover = sentence.getObjectOfPreposition();
	movee = sentence.getSubject();
      }
      else {
	DebugWindow.addLine( "Problems",
			     "Unhandled propels in MovementAnnotation: "
			     + this + "\n" + sentence.getText() );
      }
    }
    else if ( sentence.hasTag( "moves" ) ) {
      movee = sentence.getSubject();
      if ( sentence.hasPrepositionalPhrase() ) {
	path = sentence.getPrepositionalPhrase();
      }
    }
    else {
      DebugWindow.addLine( "Problems",
			   "Unhandled MovementAnnotation: "
			   + sentence.getText() );
    }

    if( movee == null )
      return false;

    // the primary body is moving
    AnnotationNotebook book =  s.getAnnotationNotebook();
    MovementAnnotation a1 = new MovementAnnotation( s, sentence,
						    movee, path );
    WMovementAnnotation w1 = new WMovementAnnotation( a1, book );

    // check if it is propelling any other bodies
    MovementAnnotation a2 = null;
    WMovementAnnotation w2 = null;

    if( mover != null ) {
      a2 = new MovementAnnotation( s, sentence, mover, path );
      w2 = new WMovementAnnotation( a2, book );

      a2.setCause( a1 );
      a1.setEffect( a2 );
      
      HashSet set = new HashSet();
      set.add( w1 );
      set.add( w2 );
      s.replaceWidget( w, set, true );
      return true;
    }
    else {
      s.replaceWidget( w, w1 );
      return true;
    }
  }

  public Iterator getInputTypes()
  {
    Vector v = new Vector();
    v.addElement( "rec.annotate.widgets.WSentence" );
    return( v.iterator() );
  }

  public Iterator getOutputTypes()
  {
    Vector v = new Vector();
    v.addElement( "rec.annotate.widgets.WMovementAnnotation" );
    return( v.iterator() );
  }
}

//    public boolean recognize( Surface s, Widget w )
//    {
//      WSentence utterance = (WSentence)w;
//      if ( utterance.hasTag( "moves" ) ) {
//        MovementAnnotation annotation = new MovementAnnotation( s, utterance );

//        WAnnotation widget = new WAnnotation( annotation ) {
//  	public void performAction( Surface surface ) {
//  	  WSquiggleArrow path = ((MovementAnnotation)m_annotation).getPath();
//  	  if ( path != null ) {
//  	    surface.addWidgetToSurface( path );
//  	  }
//  	}
//  	public void undoAction( Surface surface ) {
//  	  WSquiggleArrow path = ((MovementAnnotation)m_annotation).getPath();
//  	  if ( path != null ) {
//  	    surface.removeWidgetFromSurface( path );
//  	  }
//  	}
//        }
//        s.replaceWidget( w, widget );
//        return true;
//      }
//      return false;
//    }


//    public Iterator getInputTypes()
//    {
//      Vector v = new Vector();
//      v.addElement( "rec.annotate.widgets.WSentence" );
//      return( v.iterator() );
//    }

//    public Iterator getOutputTypes()
//    {
//      Vector v = new Vector();
//      v.addElement( "rec.annotate.widgets.WMovementAnnotation" );
//      return( v.iterator() );
//    }
//  }

