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

// -*-Mode: JDE-*- 
// $Id: DistanceRule.java,v 1.4 2005/02/17 21:41:00 moltmans Exp $
// DistanceRule.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Sat Jun 10 12:42:31 2000> 
// Time-stamp: <2005-02-16 14:03:41 moltmans> 
package rec.annotate.rules;

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

/**
 * Recognize two points and an utterance as a distance indication
 **/
public class DistanceRule extends AnnotateRule
{
  public LinkedList getTriggers() 
  {
    LinkedList triggers = new LinkedList();
    triggers.add( Type.SENTENCE );
    triggers.add( Type.POINT );
    triggers.add( Type.POINT );
    return triggers;
  }

  /**
   * The rule matches when it is of the form "from here to here" and
   * two point locations that are temporally close by.
   **/
  public boolean match( LinkedList params ) 
  {
    Sentence sentence = (Sentence)params.get(0);
    if( params.size() == 1 ) {
      return sentence.isDistanceStatement();
    }

    Point p1 = (Point)params.get(1);

    if( params.size() == 2 ) {
      int num = getNotebook().numAnnotations( sentence, p1 );
      return Math.abs( num ) < 2;
    }

    Point p2 = (Point)params.get(2);
    if( p1.equals( p2 ) ) {
      return false;
    }
    
    int dist = getNotebook().numAnnotations( p1, p2 );

    return( (dist > 0) && (dist < 3) );
  }

  public void run( Surface s, Notebook book, LinkedList params )
  {
    Sentence sentence = (Sentence)params.get(0);
    Point p1 = (Point)params.get(1);
    Point p2 = (Point)params.get(2);
    Measurement m = new Measurement( p1, p2 );
    book.myassert( m, "DistanceRule", params );
  }
}

