1

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);
}

}
Freelancer
  • 836
  • 1
  • 14
  • 47

2 Answers2

1

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?

Joshua O'Madadhain
  • 2,704
  • 1
  • 14
  • 18
  • I have added the `BehGraphUndirected` class that extends the `UndirectedSparseGraph` class of `JUNG`. I have to say I have already found the answer to my question.But thank you for your attention. – Freelancer Dec 21 '14 at 05:48
  • The node list seems redundant. The fact that the list allows duplicates is also a potential sources of errors. Anyway, what was the actual problem? – Joshua O'Madadhain Dec 21 '14 at 05:53
  • when parsing the input arguments as the `nodeName1` and `nodeName2` there was a `newline` character being left in the second argument that caused `JUNG` to create it as a new vertex.but my idea was that `JUNG` doesn't compare the new nodes by the char sequence and just adds them as new objects of nodes it took me sometime to figure out the problem was with that newline character – Freelancer Dec 21 '14 at 06:13
1

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.