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

// -*-Mode: JDE-*- 
// $Id: ContinuedWhenRule.java,v 1.6 2005/02/17 21:41:00 moltmans Exp $
// ContinuedWhenRule.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Tue Jun 06 14:52:24 2000> 
// Time-stamp: <2005-02-16 14:03:42 moltmans> 
package rec.annotate.rules;

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

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

  public boolean match( LinkedList params ) 
  {
    Sentence when = (Sentence)params.get(0);

    // must be "when" and should not have a second clause    
    if( params.size() == 1 ){
      return ( when.isWhenStatement() &&
	       when.getClause2() == null );
    }
  
    Sentence then = (Sentence)params.get(1);

    if( params.size() == 2 ) {
      int dist = getNotebook().numSentences( when, then );
      // there should be no sentences between these 2
      return dist == 0;
    }
    
    Event e1 = (Event)params.get(2);
    if( params.size() == 3 ) {
      return e1.isBasedOn( when.getClause1() );
    }
    
    Event e2 = (Event)params.get(3);
    return e2.isBasedOn( when.getClause2() );
  }

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

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

