Ricardo Sousa - 2012-04-23

Hello,

I'm trying to create the best way to draw the graph of the network, but as yet have little experience using the jung. I make much errors. I'm follow the manual to help me solve the problem.
The goal is to use the tool to create graphs of network from reading a file,
The first network graph that I want to create is based on the number of hits, how higher this value more closer the source node is the target node. The second graph network is based on the average. The smaller the value, the node are near each other

With the help of the manual was creating the code below, but that is not working properly, it certainly my fault.

The file is
//"v_source","v_destine","Hit","sum","minimum","maximum","average"
1,2,1,415,415,415,415
1,3,2,604,0,604,302
1,4,4813,50006,0,1921,10.3897776854353
1,5,386,5637,0,1005,14.6036269430052
1,8,3,1492,148,744,497.333333333333
1,9,270,6290,0,1074,23.2962962962963
1,11,37,1225,0,394,33.1081081081081
1,13,1,1394,1394,1394,1394
1,14,1,64,64,64,64
1,15,395,7122,0,1736,18.0303797468354

package viewnetwork;
import java.awt.*;
import java.lang.*;
import java.math.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.io.*;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.algorithms.generators.random.MixedRandomGraphGenerator;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
import edu.uci.ics.jung.algorithms.layout.KKLayout;

import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.graph.util.TestGraphs;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ScalingControl;
import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.layout.LayoutTransition;
import edu.uci.ics.jung.visualization.util.Animator;
import edu.uci.ics.jung.graph.UndirectedGraph;
import edu.uci.ics.jung.graph.UndirectedSparseMultigraph;
import edu.uci.ics.jung.algorithms.shortestpath.Distance;
import edu.uci.ics.jung.algorithms.shortestpath.UnweightedShortestPath;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
import edu.uci.ics.jung.algorithms.flows.EdmondsKarpMaxFlow;
import javax.xml.bind.ParseConversionEvent;
import java.lang.*;
import java.util.*;

public class ViewNetwork
{
Graph g;
Graph<String, Number> graph = new SparseMultigraph<String, Number>();

class MyLink {
double Hit;
double sum;
double min;
double max;
double avg;
String id;

  public MyLink(double hit,String id) {
this.id = id;
this.Hit = hit;
}

public MyLink(String id,double Hit,double sum,double min,double max,double avg) {
this.id = id;
this.Hit = Hit;
this.sum=sum;
this.min=min;
this.max=max;
this.avg=avg;
}

public String toString()
{
return "E"+id;
}
}

public ViewNetwork() {

//Read the file
File file = new File("/home/ricardosousa/Desktop/Results/test2.csv");
BufferedReader br = null;
DirectedSparseMultigraph g = new DirectedSparseMultigraph<MyLink, String>();
try
{
br = new BufferedReader(new FileReader(file));
String line = null;
//int hit =0;
while ((line = br.readLine()) != null)
{
String values = line.split(",");
String source=values.toString();
String target=values.toString();
double hit= Double.parseDouble(values);
double sum= Double.parseDouble(values);
double min= Double.parseDouble(values);
double max= Double.parseDouble(values);
double avg= Double.parseDouble(values);
String link= source +"->" + target;

MyLink m = new MyLink(link,hit,sum,min,max,avg);
g.addEdge(m,source,target,EdgeType.DIRECTED);
//graph.addEdge(hit, source,target);

System.out.println();

}

}

catch (FileNotFoundException ex)

{

ex.printStackTrace();

}

catch (IOException ex)

{

ex.printStackTrace();

}

finally

{

try {

if (br != null)

br.close();

}

catch (IOException ex)

{

ex.printStackTrace();

}

}

}

public static void main(String args) {

ViewNetwork graph= new ViewNetwork();

Layout<MyLink, String> layout = new KKLayout(graph.g);

DijkstraShortestPath alg = new DijkstraShortestPath(graph.graph);

layout.setSize(new Dimension(1024,1024));

VisualizationViewer<MyLink, String> vv = new VisualizationViewer<MyLink, String>(layout);

vv.setPreferredSize(new Dimension(1024,1024));

vv.setBackground(Color.WHITE);

vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());

vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());

JFrame frmPrincipal = new JFrame("View Network Porto");

frmPrincipal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel lab= new JLabel("View Network Porto");

frmPrincipal.getContentPane().add(vv);

frmPrincipal.setExtendedState(frmPrincipal.getExtendedState()|JFrame.MAXIMIZED_BOTH);

frmPrincipal.pack();

frmPrincipal.setVisible(true);

}

}

My first problem arises when the number of items have equal values​​, it gives me the following error.
at edu.uci.ics.jung.graph.AbstractGraph.getValidatedEndpoints(AbstractGraph.java:93)
at edu.uci.ics.jung.graph.SparseMultigraph.addEdge(SparseMultigraph.java:123)
at edu.uci.ics.jung.graph.AbstractGraph.addEdge(AbstractGraph.java:60)
at edu.uci.ics.jung.graph.AbstractGraph.addEdge(AbstractGraph.java:55)
at viewnetwork.ViewNetwork.<init>(ViewNetwork.java:125)

The second question is how can I view the graph g, which is built from the structure MyLink. The ultimate goal of the work is to generate a graph of proximity between the nodes, creating clusters among nearby points, but the way I'm generating the graphics, I'm not achieve the objectives, because I do not know how it generates the positions and distances.
If possible, what better way to do.
Thanks