Is it possible to add edges to a graph using a loop? I am parsing a string to determine the corresponding edges and labels. For some reason, it will only add an edge for the first round of the while loop used to iterate over String. For everyone else, the following message appears ...
Warning: the edge was deleted because the main graph JGraphT refused to create it. This situation can occur when the restriction of the base chart is violated, for example, an attempt to add a parallel edge or a custom loop to the chart that prohibits them. To avoid this message, be sure to use the appropriate JGraphT graph.
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import org.jgraph.*;
import org.jgraph.graph.*;
import org.jgrapht.*;
import org.jgrapht.ext.*;
import org.jgrapht.graph.*;
import org.jgrapht.graph.DefaultEdge;
public class JGraphAdapterDemo
extends JApplet
{
private static final long serialVersionUID = 3256444702936019250L;
private static final Color DEFAULT_BG_COLOR = Color.decode("#FAFBFF");
private static final Dimension DEFAULT_SIZE = new Dimension(530, 320);
static ListenableGraph<String, String> g =
new ListenableDirectedMultigraph<String, String>(String.class);
static int [] finalStates = new int[10];
static int startState = 0;
static char tran = ' ';
static int endState = 0;
private JGraphModelAdapter<String,String> jgAdapter;
public static void main(String [] args)
{
JGraphAdapterDemo applet = new JGraphAdapterDemo();
applet.init();
JFrame frame = new JFrame();
frame.getContentPane().add(applet);
frame.setTitle("JGraphT Adapter to JGraph Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void init()
{
ListenableGraph<String, String> g =
new ListenableDirectedMultigraph<String, String>(String.class);
jgAdapter = new JGraphModelAdapter<String, String>(g);
JGraph jgraph = new JGraph(jgAdapter);
adjustDisplaySettings(jgraph);
getContentPane().add(jgraph);
resize(DEFAULT_SIZE);
int numStates = 4;
int numSymbols;
int currentState;
int i = 0;
String input = "4 2 0 2 -1 0 a 1 1 b 3 2 c 2 3 c 3 -1";
int readInt = 0;
int j = 0;
String str = "";
int place = 0;
String fState;
i=0;
i = input.indexOf(" ",0);
str = input.substring(0,i);
numStates = Integer.parseInt(str);
for(int k = 0; k< numStates; k++){
g.addVertex("q"+k);
}
i++;
j = i;
i=input.indexOf(" ",j);
str = input.substring(j,i);
numSymbols = Integer.parseInt(str);
i++;
j = i;
i=input.indexOf(" ",j);
str = input.substring(j,i);
currentState = Integer.parseInt(str);
i++;
j = i;
while(readInt!=-1){
i=input.indexOf(" ",j);
fState = input.substring(j,i);
readInt = Integer.parseInt(fState);
if(readInt!=-1){
finalStates[place] = readInt;
i++;
j = i;
place++;
}
}
i++;
j = i;
String sState;
String eState;
while(startState!=-1&& j<(input.length()-2)){
i=input.indexOf(" ",j);
sState = input.substring(j,i);
startState = Integer.parseInt(sState);
if(startState!=-1){
i++;
j = i;
String cStr = "";
tran = input.charAt(i);
cStr = cStr + tran;
i = i+2;
j=i;
i=input.indexOf(" ",j);
eState = input.substring(j,i);
endState = Integer.parseInt(eState);
i++;
j=i;
String one = "q"+startState;
String two = "q"+endState;
System.out.println(one+ two +" "+cStr);
g.addEdge(one, two, cStr);
}
}
}
public static void drawEdge(String v, String v1, String label){
System.out.println(v +" "+v1+ " "+label);
g.addEdge(v,v1,label);
}
private void adjustDisplaySettings(JGraph jg)
{
jg.setPreferredSize(DEFAULT_SIZE);
Color c = DEFAULT_BG_COLOR;
String colorStr = null;
try {
colorStr = getParameter("bgcolor");
} catch (Exception e) {
}
if (colorStr != null) {
c = Color.decode(colorStr);
}
jg.setBackground(c);
}
@SuppressWarnings("unchecked")
private void positionVertexAt(Object vertex, int x, int y)
{
DefaultGraphCell cell = jgAdapter.getVertexCell(vertex);
AttributeMap attr = cell.getAttributes();
Rectangle2D bounds = GraphConstants.getBounds(attr);
Rectangle2D newBounds =
new Rectangle2D.Double(
x,
y,
bounds.getWidth(),
bounds.getHeight());
GraphConstants.setBounds(attr, newBounds);
AttributeMap cellAttr = new AttributeMap();
cellAttr.put(cell, attr);
jgAdapter.edit(cellAttr, null, null, null);
}
private static class ListenableDirectedMultigraph<V, E>
extends DefaultListenableGraph<V, E>
implements DirectedGraph<V, E>
{
private static final long serialVersionUID = 1L;
ListenableDirectedMultigraph(Class<E> edgeClass)
{
super(new DirectedMultigraph<V, E>(edgeClass));
}
}
}