Commit d1e1dc06 authored by Antoine's avatar Antoine

Added a test for the LinLog layout.

The LinLog layout is in core but needs components in this package.
parent d68658c1
......@@ -32,24 +32,37 @@
package org.graphstream.ui.layout.test;
import org.graphstream.algorithm.generator.BarabasiAlbertGenerator;
import org.graphstream.algorithm.generator.Generator;
import org.graphstream.algorithm.generator.GridGenerator;
import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.MultiGraph;
public class TestLayout {
public static void main(String args[]) {
System.getProperties().put("gs.ui.layout", "org.graphstream.ui.layout.springbox.implementations.LinLog");
(new TestLayout()).test();
}
public void test() {
Graph graph = new MultiGraph("test");
graph.addAttribute("layout.force", 0.8);
graph.display();
GridGenerator gen = new GridGenerator();
// int steps = 50;
// Generator gen = new BarabasiAlbertGenerator(2);
int steps = 6;
Generator gen = new GridGenerator();
gen.addSink(graph);
gen.begin();
for(int i=0; i<30; i++) gen.nextEvents();
for(int i=0; i<steps; i++) {
gen.nextEvents();
sleep(10);
}
gen.end();
// try { graph.write("grid7x7.dgs"); } catch(Exception e) { }
graph.display();
}
public static void sleep(long ms) {
try { Thread.sleep(ms); } catch(Exception e) {}
}
}
\ No newline at end of file
/*
* Copyright 2006 - 2012
* Stefan Balev <stefan.balev@graphstream-project.org>
* Julien Baudry <julien.baudry@graphstream-project.org>
* Antoine Dutot <antoine.dutot@graphstream-project.org>
* Yoann Pigné <yoann.pigne@graphstream-project.org>
* Guilhelm Savin <guilhelm.savin@graphstream-project.org>
*
* GraphStream is a library whose purpose is to handle static or dynamic
* graph, create them from scratch, file or any source and display them.
*
* This program is free software distributed under the terms of two licenses, the
* CeCILL-C license that fits European law, and the GNU Lesser General Public
* License. You can use, modify and/ or redistribute the software under the terms
* of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
* URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
*/
package org.graphstream.ui.layout.test;
import java.io.IOException;
import org.graphstream.algorithm.ConnectedComponents;
import org.graphstream.graph.Edge;
import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.MultiGraph;
import org.graphstream.stream.ProxyPipe;
import org.graphstream.stream.file.FileSource;
import org.graphstream.stream.file.FileSourceDGS;
import org.graphstream.stream.file.FileSourceGML;
import org.graphstream.stream.thread.ThreadProxyPipe;
import org.graphstream.ui.geom.Point3;
import org.graphstream.ui.graphicGraph.GraphPosLengthUtils;
import org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.Units;
import org.graphstream.ui.layout.springbox.implementations.LinLog;
import org.graphstream.ui.spriteManager.Sprite;
import org.graphstream.ui.spriteManager.SpriteManager;
import org.graphstream.ui.swingViewer.Viewer;
/**
* This test creates a layout (instead of using the default layout of
* the viewer) and creates a loop :
*
* <pre>
* Graph ------> Layout --+
* ^ |
* | |
* +--------------------+
* </pre>
*/
public class TestLinLogLayout {
// public static final String GRAPH = "data/dorogovtsev_mendes6000.dgs"; public static final double a= 0; public static final double r=-1.3; public static double force = 3;
public static final String GRAPH = "data/karate.gml"; public static double a= 0; public static double r=-1.3; public static double force = 3;
// public static final String GRAPH = "data/dolphins.gml"; public static double a= 0; public static double r=-1.2; public static double force = 8;
// public static final String GRAPH = "data/polbooks.gml"; public static double a= 0; public static double r=-2; public static double force = 3;
// public static final String GRAPH = "data/triangles.dgs"; public static double a= 1; public static double r=-1; public static double force = 0.5;
// public static final String GRAPH = "data/FourClusters.dgs"; public static double a= 0; public static double r=-1; public static double force = 3;
// public static final String GRAPH = "data/grid7x7.dgs"; public static double a= 0; public static double r=-1; public static double force = 100;
// public static final String GRAPH = "data/imdb.dgs";
protected Graph graph;
protected Viewer viewer;
protected LinLog layout;
protected Sprite CC;
protected ConnectedComponents cc;
public static void main(String args[]) {
new TestLinLogLayout();
}
public TestLinLogLayout() {
boolean loop = true;
graph = new MultiGraph("test");
viewer = new Viewer(new ThreadProxyPipe(graph));
ProxyPipe fromViewer = viewer.newThreadProxyOnGraphicGraph();
layout = new LinLog(false);
SpriteManager sm = new SpriteManager(graph);
CC = sm.addSprite("CC");
cc = new ConnectedComponents(graph);
cc.setCutAttribute("cut");
CC.setPosition(Units.PX, 20, 20, 0);
layout.configure(a, r, true, force);
graph.addAttribute("ui.antialias");
graph.addAttribute("ui.stylesheet", styleSheet);
fromViewer.addSink(graph);
viewer.addDefaultView(true);
graph.addSink(layout);
layout.addAttributeSink(graph);
FileSource dgs = GRAPH.endsWith(".gml") ? new FileSourceGML() : new FileSourceDGS();
dgs.addSink(graph);
try {
dgs.begin(getClass().getResourceAsStream(GRAPH));
for (int i = 0; i < 5000 && dgs.nextEvents(); i++) {
// fromViewer.pump();
// layout.compute();
// sleep(100);
}
dgs.end();
} catch (IOException e1) {
e1.printStackTrace();
System.exit(1);
}
System.out.println("Finished creating the graph.");
while (loop) {
fromViewer.pump();
if (graph.hasAttribute("ui.viewClosed")) {
loop = false;
} else {
//sleep(1000);
layout.compute();
findCommunities(1.3);
updateCC();
}
}
System.exit(0);
}
protected void findCommunities(double threshold) {
int nedges = graph.getEdgeCount();
double avgDist = 0;
double edgesDists[] = new double[nedges];
for(int i=0; i<nedges; i++) {
Edge edge = graph.getEdge(i);
Point3 posFrom = GraphPosLengthUtils.nodePointPosition(edge.getNode0());
Point3 posTo = GraphPosLengthUtils.nodePointPosition(edge.getNode1());
edgesDists[i] = posFrom.distance(posTo);
avgDist += edgesDists[i];
}
avgDist /= nedges;
// Nothing happened to the graph so the order remains.
for(int i=0; i<nedges; i++) {
Edge edge = graph.getEdge(i);
if(edgesDists[i] > avgDist*threshold) {
edge.addAttribute("ui.class", "cut");
edge.addAttribute("cut");
} else {
edge.removeAttribute("ui.class");
edge.removeAttribute("cut");
}
}
}
protected void updateCC() {
CC.setAttribute("ui.label", String.format("CC %d", cc.getConnectedComponentsCount()));
}
protected static void sleep(long ms) {
try { Thread.sleep(ms); } catch(Exception e) {}
}
protected static String styleSheet =
"node { size: 7px; fill-color: rgb(150,150,150); }" +
"edge { fill-color: rgb(255,50,50); size: 2px; }" +
"edge.cut { fill-color: rgba(200,200,200,128); }" +
"sprite#CC { size: 0px; text-color: rgb(100,100,100); text-size: 20; }";
}
\ No newline at end of file
DGS004
null 0 0
an "0"
an "1"
an "2"
an "3"
an "10"
an "11"
an "12"
an "13"
an "20"
an "21"
an "22"
an "23"
an "30"
an "31"
an "32"
an "33"
ae "0-1" "0" "1"
ae "0-2" "0" "2"
ae "0-3" "0" "3"
ae "1-2" "1" "2"
ae "1-3" "1" "3"
ae "2-3" "2" "3"
ae "10-11" "10" "11"
ae "10-12" "10" "12"
ae "10-13" "10" "13"
ae "11-12" "11" "12"
ae "11-13" "11" "13"
ae "12-13" "12" "13"
ae "20-11" "20" "21"
ae "20-12" "20" "22"
ae "20-13" "20" "23"
ae "21-12" "21" "22"
ae "21-13" "21" "23"
ae "22-13" "22" "23"
ae "30-31" "30" "31"
ae "30-32" "30" "32"
ae "30-33" "30" "33"
ae "31-32" "31" "32"
ae "31-33" "31" "33"
ae "32-33" "32" "33"
ae "inter1" "0" "10"
ae "inter2" "10" "20"
ae "inter3" "20" "30"
ae "inter4" "30" "0"
Creator "Mark Newman on Wed Jul 26 15:04:20 2006"
graph
[
directed 0
node
[
id 0
label "Beak"
]
node
[
id 1
label "Beescratch"
]
node
[
id 2
label "Bumper"
]
node
[
id 3
label "CCL"
]
node
[
id 4
label "Cross"
]
node
[
id 5
label "DN16"
]
node
[
id 6
label "DN21"
]
node
[
id 7
label "DN63"
]
node
[
id 8
label "Double"
]
node
[
id 9
label "Feather"
]
node
[
id 10
label "Fish"
]
node
[
id 11
label "Five"
]
node
[
id 12
label "Fork"
]
node
[
id 13
label "Gallatin"
]
node
[
id 14
label "Grin"
]
node
[
id 15
label "Haecksel"
]
node
[
id 16
label "Hook"
]
node
[
id 17
label "Jet"
]
node
[
id 18
label "Jonah"
]
node
[
id 19
label "Knit"
]
node
[
id 20
label "Kringel"
]
node
[
id 21
label "MN105"
]
node
[
id 22
label "MN23"
]
node
[
id 23
label "MN60"
]
node
[
id 24
label "MN83"
]
node
[
id 25
label "Mus"
]
node
[
id 26
label "Notch"
]
node
[
id 27
label "Number1"
]
node
[
id 28
label "Oscar"
]
node
[
id 29
label "Patchback"
]
node
[
id 30
label "PL"
]
node
[
id 31
label "Quasi"
]
node
[
id 32
label "Ripplefluke"
]
node
[
id 33
label "Scabs"
]
node
[
id 34
label "Shmuddel"
]
node
[
id 35
label "SMN5"
]
node
[
id 36
label "SN100"
]
node
[
id 37
label "SN4"
]
node
[
id 38
label "SN63"
]
node
[
id 39
label "SN89"
]
node
[
id 40
label "SN9"
]
node
[
id 41
label "SN90"
]
node
[
id 42
label "SN96"
]
node
[
id 43
label "Stripes"
]
node
[
id 44
label "Thumper"
]
node
[
id 45
label "Topless"
]
node
[
id 46
label "TR120"
]
node
[
id 47
label "TR77"
]
node
[
id 48
label "TR82"
]
node
[
id 49
label "TR88"
]
node
[
id 50
label "TR99"
]
node
[
id 51
label "Trigger"
]
node
[
id 52
label "TSN103"
]
node
[
id 53
label "TSN83"
]
node
[
id 54
label "Upbang"
]
node
[
id 55
label "Vau"
]
node
[
id 56
label "Wave"
]
node
[
id 57
label "Web"
]
node
[
id 58
label "Whitetip"
]
node
[
id 59
label "Zap"
]
node
[
id 60
label "Zig"
]
node
[
id 61
label "Zipfel"
]
edge
[
source 8
target 3
]
edge
[
source 9
target 5
]
edge
[
source 9
target 6
]
edge
[
source 10
target 0
]
edge
[
source 10
target 2
]
edge
[
source 13
target 5
]
edge
[
source 13
target 6
]
edge
[
source 13
target 9
]
edge
[
source 14
target 0
]
edge
[
source 14
target 3
]
edge