/**
* Graph.java class file lets you open and save a graph file.
* Part of the mtrL1.zip file.
*
* @author Michael Rivers
* @version January 26th, 2007
**/

import java.io.IOException;
public class Graph extends BasicGraph
{
	GraphFile graph;
	
	/**
	* Default Constructor
	*/
	public Graph()
	{
		super();
	}
	
	/**
	* Second Constructor
	*/
	public Graph( int nodes, int directed, int weighted )
	{
		super( nodes, directed, weighted );
	}
	
	/**
	* Main method
	*/
	public static void main( String[] args )
	{
	}
	
	/**
	* open method opens a file with the given path name and
	* instantiates a bunch of parameters in BasicGraph
	*/
	public void open( String fileName ) throws IOException
	{
		graph = new GraphFile( fileName );
		
		super.fileName = fileName; // sets the file name
		graph.openForReading();
		int dummy = graph.nextInt();
		if( dummy != 0 )
			setNumberOfNodes( dummy ); // set the number of nodes
		
		dummy = graph.nextInt();
		if( dummy != 0 )
			setDirected( DIRECTED == dummy );// set weather the graph is directed
		
		dummy = graph.nextInt();
		if( dummy != 0 )
			setWeighted( WEIGHTED == dummy );// set weather the graph is weighted

		int x = 0, y = 0;
		double weight;
		while( dummy != 0 && dummy < getNumberOfNodes() )
		{
			dummy = graph.nextInt();
			if( dummy != 0 )
				x = dummy + 1;
			dummy = graph.nextInt();
			if( dummy != 0 )
				y = dummy + 1;
			if( isWeighted() )
			{
				weight = graph.nextDouble();
				if( weight != 0 )
				{
					setEdge( x, y, weight );
				}
			} else // for unweighted values
				setEdge( x,y );
			dummy = graph.nextInt();
		} // fill the adjacency matrix
		
		graph.closeFromReading();
	}
	
	/**
	* saveAs method saves the file in a text file
	*/
	public void saveAs( String fileName ) throws IOException
	{
		int dummy;
		graph = new GraphFile( fileName );
		graph.openForWriting();
		graph.println( numberOfNodes() + " number of nodes" );
		if( isDirected() )
			dummy = 2;
		else
			dummy = 1;
		graph.println( dummy + " one means undirected, two means directed" );
		if( isWeighted() )
			dummy = 2;
		else
			dummy = 1;
		graph.println( dummy + " one means unweighted, two means weighted" );
		graph.println( "" );
		
		dummy = 0;
		int a = 1, b = 2;
		double weight = 0;
		if( isDirected() && isWeighted() )
		{
			while( a < numberOfNodes() )
			{
				while( b < numberOfNodes() + 1 )
				{
					if( isEdge( a, b ) )
						graph.println( a + " --> " + b + " weight " + getWeight( a,b ) );
					++b;
				}
				++a;
				b = 1;
			}
		} else if( isDirected() && !isWeighted() )
		{
			while( a < numberOfNodes() )
			{
				while( b < numberOfNodes() + 1 )
				{
					if( isEdge( a, b ) )
						graph.println( a + " --> " + b );
					++b;
				}
				++a;
				b = 1;
			}
		} else if( !isDirected() && isWeighted() )
		{	
					while( a < numberOfNodes() )
			{
				while( b < numberOfNodes() + 1 )
				{
					if( isEdge( a, b ) )
						graph.println( a + " <--> " + b + " weight " + getWeight( a,b ) );
					++b;
				}
				++a;
				b = a + 1;
			}
		} else if( !isDirected() && !isWeighted() )
		{
					while( a < numberOfNodes() )
			{
				while( b < numberOfNodes() + 1 )
				{
					if( isEdge( a, b ) )
						graph.println( a + " <--> " + b );
					++b;
				}
				++a;
				b = a + 1;
			}
		}
	}
}
