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

// -*-Mode: Java-*- 
// $Id: LeverRule.java,v 1.6 2005/02/17 21:40:59 moltmans Exp $
// LeverRule.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Tue May 16 15:17:42 2000> 
// Time-stamp: <2005-02-16 14:03:39 moltmans> 
package rec.annotate.rules;

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

/**
 * Recognize a long thing body with a pinjoint as a lever.
 **/
public class LeverRule extends AnnotateRule
{

  public LinkedList getTriggers() 
  {
    LinkedList triggers = new LinkedList();
    triggers.add( Type.BODY );
    triggers.add( Type.PIN_JOINT );
    return triggers;
  }

  public boolean match( LinkedList params ) 
  {
    Body body = (Body)params.get(0);
    WSimBody wbody = body.getBody();

    if( wbody instanceof WSimQuadrilateral ) {
      // make sure it is oblong
      WSimQuadrilateral quad = (WSimQuadrilateral)wbody;
      if( !quad.isOblong() ) {
	return false;
      }
    }
    else if( wbody instanceof WSimRectangle ) {
      // make sure it is oblong
      WSimRectangle rect = (WSimRectangle)wbody;
      if( !rect.isOblong() ) {
	return false;
      }
    }
    else {
      return false;
    }
    
    if( params.size() < 2 ) {
      return true;
    }

    PinJoint pin = (PinJoint)params.get(1);
    return pin.isAttachedTo( body );
  }

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

    Lever lever = new Lever( pin, body, body.getName() );
    book.myassert( lever, "Lever Rule", params );
  }
}

