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

// $Id: TransitiveBeforeRule.java,v 1.3 2005/02/17 21:40:57 moltmans Exp $
// TransitiveBeforeRule.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Fri Jul 21 00:46:35 2000> 
// Time-stamp: <2005-02-16 14:03:28 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.*;
import rec.core.widgets.*;

/**
 * Propagate the before relation tranisitively
 **/
public class TransitiveBeforeRule extends AnnotateRule
{

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

  public boolean match( LinkedList params ) 
  {
    if( params.size() == 1 ) {
      return true;
    }

    Before b1 = (Before)params.get(0);
    Before b2 = (Before)params.get(1);

    if( params.size() == 2 ) {
      return b1.getEvent2().sameFact( b2.getEvent1() );
    }
    
    Before b3 = (Before)params.get(2);
    return b2.getEvent2().sameFact( b3.getEvent1() );    
  }

  public void run( Surface s, Notebook book, LinkedList params )
  {
    Before b1 = (Before)params.get(0);
    Before b2 = (Before)params.get(1);
    Before b3 = (Before)params.get(2);

    Before b = new Before( b1.getEvent1(), b3.getEvent2() );
    book.myassert( b, "TranisitiveBeforeRule", params );
  }
}

