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

package rec.fsm;

///////////////////////////////////////////////////////////////////////////////
//
// FILE:	EdgeRecombiner.java
//
// AUTHOR:      Luke Weisman
//              luke@ai.mit.edu
//
// DATE:	June, 1998
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
//
///////////////////////////////////////////////////////////////////////////////


import java.util.*;
import rec.util.*;
import rec.sys.*;
import rec.core.*;

public class EdgeRecombiner extends Aligner
{

  public EdgeRecombiner()
  {
  }


  public boolean recognize( Surface s, Widget w )
  {
    Edge e = (Edge)w;

    if ( e.full() )
      {
	// get all edges going from 2 points of this edge
	// somehow get the node to remove
	Vector es = e.getNode(true).getEdgesTo( e.getNode( false ) );
	
	// add name of this edge to one of the other edges.
	int size = es.size();
	if ( size > 1 )
	  {
	    Edge tmp = (Edge)es.elementAt( 0 );
	    int i = 0;
	    while( tmp.equals( e ) )
	      tmp = (Edge)es.elementAt( ++i );

	    tmp.setName( fixName( tmp.getName(), e.getName() ) );

	    s.removeWidget( e );

	    // done
	    return true;
	  }
      }

    return false;
  }


  public String fixName( String p1, String p2 )
  {
    if ( p1.indexOf( ' ' ) != -1 )
      p1 = "(" + p1 + ")";
    if ( p2.indexOf( ' ' ) != -1 )
      p2 = "(" + p2 + ")";

    return p1 + " | " + p2;
  }

  



  public Iterator getInputTypes()
  {
    Vector v = new Vector();

    v.addElement( "rec.fsm.Edge" );
    v.addElement( "rec.fsm.EdgeDup" );

    return( v.iterator() );
  }

  public Iterator getOutputTypes()
  {
    return getInputTypes();
  }
  
}


