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

// $Id: OpModeWhenMatcher.java,v 1.4 2005/02/17 21:40:57 moltmans Exp $
// OpModeWhenMatcher.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Wed Jul 19 14:28:00 2000> 
// Time-stamp: <2005-02-16 14:03:37 moltmans> 
package rec.annotate.rules;

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

public class OpModeWhenMatcher extends AnnotateRule 
{
  // "when <event1> then <event2>" and the 2 events
  public LinkedList getTriggers() 
  {
    LinkedList triggers = new LinkedList();
    triggers.add( Type.SENTENCE ); // when
    triggers.add( Type.EVENT );    // condition 1
    triggers.add( Type.SENTENCE ); // when 
    triggers.add( Type.EVENT );    // condition 2
    return triggers;
  }

  public boolean match( LinkedList params ) 
  {
    Sentence sent = (Sentence)params.get(0);
    if( !sent.isWhenStatement() ||
	sent.getClause2() == null )
    {
      return false;
    }
    if( params.size() < 2 )
      return true;
    
    Event e1 = (Event)params.get(1);
    if( !e1.isBasedOn( sent.getClause1() ) )
      return false;

    if( params.size() < 3 )
      return true;
    
    Event e2 = (Event)params.get(2);
    return e2.isBasedOn( sent.getClause2() );
  }

  public void run( Surface s, Notebook book, LinkedList params )
  {
    Event cause = (Event)params.get(1);
    Event effect = (Event)params.get(2);

    LeadsTo link = new LeadsTo( cause, effect );
    book.myassert( link, "WhenUtteranceRule", new LinkedList( params ) );
  }
}

