I have simple program that simulates distributed environment. I want to visualize processor behaviour by using GraphStream library. Each processor is a thread, they do some computations, but not all the time, only when I set the switch
variable
while(running){
if(switch)
computate();
}
I have written a class which take processorList and prepare graph visualization. There is a function for this task
public void adjustBFSGraph2(){
for(Edge e: this.g.getEdgeSet())
{
e.clearAttributes();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(Processor p : processorList)
{
p.getNode().setAttribute("ui.label", "Pid" +" " + p.getPID() +" "+"Dist: " + p.getFieldValue("bfs") + " " + "Parent" + " " + p.getFieldValue("parent"));
if(!p.getFieldValue("parent").equals("-1"))
{
Edge e = p.getNode().getEdgeBetween(p.getFieldValue("parent"));
if(e != null)
{
e.setAttribute("ui.color", p.getColor());
e.setAttribute("ui.label", "added" + p.getPID());
}
}
}
}
My problem is that i see colors on edges only for blink of eye and then color disapear. It looks like it adds attribute "ui.color"
and removes it in same loop round, but how is it possible?
@update
I have edited my code, now i can see edges for time specified in thread.sleep()
after first loop, well i dont get why after clearing all attributes i can actually see them.
here is how i'm calling my function
while(true)
{ i++;
//if any processor is not runing
boolean aux = false;
while(!aux) {
for (Processor proc : s.processorList) {
aux = aux || proc.isEnabled();
}
aux = !aux;
}
s.gp.adjustBFSGraph();
Thread.sleep(5000);
for(Processor proc: s.processorList)
{
proc.enable();
}
}
When Thread.sleep()
inside adjust function is set to less then 100 ms it starts to blinks again.
Because it may be a little unclear what i'm doing i have created smaller example
This is equivalent of my processor class
public class SomeObjectWithNode {
public Node n;
public Color c;
public SomeObjectWithNode(Node n)
{
Random rand = new Random();
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color randomColor = new Color(r, g, b);
c = randomColor;
this.n = n;
}
}
Here is a class witch change graph styling/ drawing it
public class TestDraw {
Vector<SomeObjectWithNode> n;
Graph g;
public TestDraw(Vector<SomeObjectWithNode> k, Graph g)
{
this.g= g;
this.n = k;
}
public void adjust()
{
Random rand = new Random();
for(Edge e: g.getEdgeSet())
{
e.clearAttributes();
}
for(SomeObjectWithNode k: n)
{
k.n.addAttribute("ui.color", k.c);
for(Edge e: k.n.getEdgeSet())
{
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color randomColor = new Color(r, g, b);
e.addAttribute("ui.color", randomColor);
}
}
}
}
and here is main class
public class TestGs {
public static void main(String[] args) {
Node lastNode;
TestDraw t;
Vector<SomeObjectWithNode> a = new Vector<SomeObjectWithNode>();
System.setProperty("gs.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
Graph g = new SingleGraph("test1");
g.display();
g.addAttribute("ui.stylesheet", "node { fill-mode: dyn-plain; size: 10px;} edge { fill-mode: dyn-plain; size: 2px;}");
a.add(new SomeObjectWithNode(g.addNode(Integer.toString(0))));
lastNode = g.getNode("0");
for(int i = 1; i < 10; i++)
{
a.add(new SomeObjectWithNode(g.addNode(Integer.toString(i))));
g.addEdge(Integer.toString(i-1).concat(Integer.toString(i)), a.get(i).n, lastNode);
lastNode = a.get(i).n;
}
t = new TestDraw(a,g);
while(true)
{
t.adjust();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
When i run example we can see graph has colours only for the blink of eye instead of displaying it for time in Thread.sleep()