connecting vertices in jung by edges results in another extra vertex being created

210 views Asked by At

I am implementing an interface for taking commands for creating , connecting and coloring vertices in JUNG when I want to connect two already existing vertices JUNG connects to vertices and creates an extra vertex , why?

Here is my code for connect method:

public class Connect extends Command {
private CommandMaster cm;
private BehGraphUndirected behGraph;
private static int edgenumber=0;
@Override
public Object run(BehGraphUndirected behGraph, VisualizationImageServer panel, InterpretMaster interpretMaster, String... args) {

    System.out.print("connect Runs\n");
    this.cm = new CommandMaster();
    this.behGraph = behGraph;

    if(cm.exists(args[0]))
    {
        //got to another command
    }else
    {
        switch (args[0]) {
            case "edge":
                this.createEdge(args[1] , args[2]);
                break;
        }
    }
    interpretMaster.refreshAndRepaint();
    return null;

}
public void createEdge(String nodeName1 , String nodeName2)
{
    this.behGraph.addEdge(edgenumber++,nodeName1, nodeName2);
    System.out.println(this.behGraph.getVertexCount());
    System.out.println("edge between: "+nodeName1+" and "+ nodeName2+" added");
}

And it's the create method just in case you want to know the way I implemented the code:

package interpreter.command;

import GraphHandling.BehGraphUndirected;
import edu.uci.ics.jung.visualization.VisualizationImageServer;
import interpreter.Command;
import interpreter.CommandMaster;
import interpreter.InterpretMaster;


/**
*
* @author Administrator
*/
public class Create extends Command{
private CommandMaster cm;
private BehGraphUndirected behGraph;

@Override
public Object run(BehGraphUndirected behGraph, VisualizationImageServer panel, InterpretMaster interpretMaster, String... args) {
    System.out.print("create Runs \n");

    this.cm = new CommandMaster();
    this.behGraph = behGraph;

    if(cm.exists(args[0]))
    {
        //got to another command
    }else
    {
        switch (args[0]) {
            case "node":
                this.createNode(args[1]);
                break;
            case "label":
                this.createLabel(args[1]);
                break;
        }
    }
    interpretMaster.refreshAndRepaint();
    return null;
}
public void createNode(String nodeName)
{
    this.behGraph.addVertex(nodeName);
    System.out.print("vertex: "+nodeName+" added");
}

private void createLabel(String string) {

}
class str
{
    int i;
    long j;
}

}

Graph images before and after connecting two nodes:

enter image description here

enter image description here

and Here is my BehGraphUndirected class:

package GraphHandling;

import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import java.util.LinkedList;

/**
*
* @author Administrator
*/
public class BehGraphUndirected extends UndirectedSparseGraph{
private final LinkedList<Node> nodeList;

public BehGraphUndirected()
{ 
    this.nodeList = new LinkedList<>();
}
public void addNode(Node newNode)
{
   this.nodeList.add(newNode);
}

}
2

There are 2 answers

3
Joshua O'Madadhain On BEST ANSWER

You should look at what BehGraphUndirected is doing; it's not a JUNG class or interface.

What is the name of the vertex that's being created, and how does that relate to what's being passed to the create method?

0
AudioBubble On

I have compiled and tested your code , The Jung library seems working right and It extinguishes the different nodes by the different object that was given to it It seems you have some other problem , Like a problem in processing the input strings that are used as objects that create nodes.