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

package rec.fsm;

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


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

public class EdgeDupRecognizer extends Aligner
{

  public EdgeDupRecognizer()
  {
  }

  /////////////////////////////////////////////////////////////////////////////
  
  /**
   * Each new stroke generated gets put into the pipe here.
   *     It will remove and add objects to the 'out' target
   *     as it deems fit.
   *
   * @return a boolean if changes were made.
   */
  public boolean recognize( Surface s, Widget w )
  {
    Edge ed = (Edge)w;

    Console.indent();

    if ( ed.full() )
      {
	Vector v = ed.getNode(true).getEdgesTo( ed.getNode(false) );
	if ( v.size() > 1 )
	  {
	    int size = v.size();
	    //Console.writeln( "EdgeDup online: " + size + " edges." );

	    for ( int i = 0; i < size; i++ )
	      {
		Edge te = (Edge)v.elementAt(i);
		if ( ! (te instanceof rec.fsm.EdgeDup ) )
		  {
		    Edge ted = new EdgeDup( te );
		    s.replaceWidget( te, ted );

		    v.setElementAt( ted, i );
		  }
	      }
	    
	    EdgeDup an_edge = (EdgeDup)v.elementAt(0);
	    an_edge.setIndex();

	    Console.outdent();
	    return true;
	  }
      }

    Console.outdent();
    return false;
  }

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

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

    return( v.iterator() );
  }

  public Iterator getOutputTypes()
  {
    Vector v = new Vector();
    v.addElement( "rec.fsm.EdgeDup" );

    return v.iterator();
  }
}

////////////////////////////////////////////////////////////////////////////////
