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

// -*-Mode: JDE-*- 
// $Id: RotationUtteranceRule.java,v 1.4 2005/02/17 21:40:57 moltmans Exp $
// RotationUtteranceRule.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Mon Jun 05 21:55:47 2000> 
// Time-stamp: <2005-02-16 14:03:36 moltmans> 
package rec.annotate.rules;

import rec.annotate.facts.*;
import java.util.*;
import rec.util.*;
import rec.tms.*;
import rec.sys.*;
import rec.annotate.widgets.*;

/**
 * @author Michael Oltmans
 * @version $Id: RotationUtteranceRule.java,v 1.4 2005/02/17 21:40:57 moltmans Exp $
 */
public class RotationUtteranceRule extends AnnotateRule
{
					      
  public LinkedList getTriggers() 
  {
    LinkedList triggers = new LinkedList();
    triggers.add( Type.SENTENCE );
    triggers.add( Type.BODY );
    triggers.add( new String[] {Type.GROUNDED,
				Type.ATTACHMENT} );
    triggers.add( Type.PIN_JOINT );
    return triggers;
  }

  /**
   * The rule matches when it describes a rotation event
   **/
  public boolean match( LinkedList params ) 
  {
    Sentence sent = (Sentence)params.get(0);
    if( !sent.isRotatesStatement() ) {
      return false;
    }
    if( params.size() == 1 ) {
      return true;
    }

    Body body = (Body)params.get(1);
    if( !sent.getSubject().equals( body.getName() ) )
      return false;
    if( params.size() == 2 ) {
      return true;
    }

    if( params.get(2) instanceof Grounded ) {
      Grounded attach = (Grounded)params.get(2);
      if( !attach.getBody().equals( body ) )
	return false;
      if( params.size() == 3 )
	return true;
    
      PinJoint joint = (PinJoint)params.get(3);
      return attach.getPart().equals( joint );
    }
    else {
      Attachment attach = (Attachment)params.get(2);
      if( !attach.getBodies().contains( body ) )
	return false;
      if( params.size() == 3 )
	return true;
    
      PinJoint joint = (PinJoint)params.get(3);
      return attach.getPart().equals( joint );
    }
  }

  public void run( Surface s, Notebook book, LinkedList params )
  {
    Sentence sent = (Sentence)params.get(0);
    Body body = (Body)params.get(1);
    PinJoint joint = (PinJoint)params.get(3);

    MotionEvent motion = new MotionEvent( s, body, sent );
    RotationEvent rotation = new RotationEvent( s, motion, body, joint );
    SameEvent same = new SameEvent( motion, rotation );
    
    book.myassert( motion, "RotationUtteranceRule", params );
    params.add( motion );
    book.myassert( rotation, "RotationUtteranceRule", params );
    params.add( rotation );
    book.myassert( same, "RotationRule", params );
  }
}

