Search code examples
javagraphstream

setAttribute in GraphStream


I have a project in GraphStream in which I need to alter a graph's nodes coordinates.

These coordinates are stored in a variable called "xy"

The functions that do this job are supposed to be either setAttribute() or addAttribute(), but when I use them, nothing happens, or sometimes it gives me a NaN message.

This is an example of a code where it makes no change at all :

public class TestClass {

public static void main(String[] args) {
    Graph graph = new MultiGraph("Dorogovtsev mendes");
    Generator gen = new DMCoordGen();       // Coordinates Generator
    gen.addSink(graph);
    gen.begin();
    for (int j = 0; j < 5; j++) {           // Graph Nodes
        gen.nextEvents();
    }
    gen.end();
    System.out.println("-----------------");
    System.out.println("First Coordinates");
    System.out.println("-----------------");
    for(int i=0; i<graph.getNodeCount(); i++) {             // First Reading
        Double[] attributes = (Double[]) graph.getNode(i).getAttribute("xy");
        Double x = attributes[0];
        Double y = attributes[1];
        System.out.println(x + " , " + y);
    }
    System.out.println("---------------");
    System.out.println("New Coordinates");
    System.out.println("---------------");
    for(int i=0; i<graph.getNodeCount(); i++) {             // Modification
        Double[] attributes = (Double[]) graph.getNode(i).getAttribute("xy");
        Double x = attributes[0] * 100;
        Double y = attributes[1] * 100;
        graph.getNode(i).setAttribute("xy", (Object[]) attributes);
    }
    for(int i=0; i<graph.getNodeCount(); i++) {             // Second Reading
        Double[] attributes = (Double[]) graph.getNode(i).getAttribute("xy");
        Double x = attributes[0];
        Double y = attributes[1];
        System.out.println(x + " , " + y);
    }
}

}

The result returned by this code is :

-----------------
First Coordinates
-----------------
0.27463410536937105 , 0.908142618579691
0.5945324304252239 , 0.011861230502362319
0.7645069243611142 , 0.8994092027470882
0.23856199477010953 , 0.6174255664753833
0.9215549969974312 , 0.46748048612026005
0.5283548936726747 , 0.3995089175747245
0.14035732608566487 , 0.32181008971710223
0.8782155705197804 , 0.8271792979519879
---------------
New Coordinates
---------------
0.27463410536937105 , 0.908142618579691
0.5945324304252239 , 0.011861230502362319
0.7645069243611142 , 0.8994092027470882
0.23856199477010953 , 0.6174255664753833
0.9215549969974312 , 0.46748048612026005
0.5283548936726747 , 0.3995089175747245
0.14035732608566487 , 0.32181008971710223
0.8782155705197804 , 0.8271792979519879

As you can see, nothing has been modified either by using setAttribute() or addAttribute() .

Please refer to the line graph.getNode(i).setAttribute("xy", (Object[]) attributes); .

What did I do wrong ? And how can I fix it ?

Thanks !


Solution

  • In your example, did you mean to do:

    attributes[0] *= 100;
    

    instead of :

    Double x = attributes[0] * 100;
    

    In your case the attributes array never gets modified. That's probably why the 'setAttribute' call has (apparently) no effect.