use of org.opennms.features.topology.api.topo.Edge in project opennms by OpenNMS.
the class GraphUtils method renderGraphToFile.
public static void renderGraphToFile(Graph<VertexRef, Edge> jungGraph, File file) {
final edu.uci.ics.jung.algorithms.layout.Layout<VertexRef, Edge> jungLayout = new KKLayout<>(jungGraph);
// Size of the layout
jungLayout.setSize(new Dimension(1800, 1800));
final Set<VertexRef> roots = jungGraph.getVertices().stream().filter(v -> jungGraph.getInEdges(v).isEmpty()).collect(Collectors.toSet());
VisualizationImageServer<VertexRef, Edge> vv = new VisualizationImageServer<>(jungLayout, jungLayout.getSize());
// Viewing area size
vv.setPreferredSize(new Dimension(2000, 2000));
vv.getRenderContext().setVertexLabelTransformer(VertexRef::getLabel);
vv.getRenderContext().setEdgeLabelTransformer(Edge::getLabel);
vv.getRenderContext().setVertexFillPaintTransformer(vertexRef -> {
if (roots.contains(vertexRef)) {
return Color.RED;
}
return Color.BLUE;
});
// Draw vertices according to in/out edge count. The more edges, the bigger the vertex
vv.getRenderContext().setVertexShapeTransformer(vertexRef -> {
Collection<Edge> inEdges = jungGraph.getInEdges(vertexRef);
Collection<Edge> outEdges = jungGraph.getOutEdges(vertexRef);
int edgeCount = inEdges.size() + outEdges.size();
int widthHeight = (edgeCount / 4 + 1) * 20;
return new Ellipse2D.Float(-1 * widthHeight / 2, -1 * widthHeight / 2, widthHeight, widthHeight);
});
// Create the buffered image
BufferedImage image = (BufferedImage) vv.getImage(new Point2D.Double(vv.getGraphLayout().getSize().getWidth() / 2, vv.getGraphLayout().getSize().getHeight() / 2), new Dimension(vv.getGraphLayout().getSize()));
// Render
try {
ImageIO.write(image, "png", file);
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
use of org.opennms.features.topology.api.topo.Edge in project opennms by OpenNMS.
the class VertexHopGraphProviderTest method testCollapseEdges.
@Test
public void testCollapseEdges() {
CollapsibleCriteria collapseMe = new CollapsibleCriteria() {
@Override
public void setCollapsed(boolean collapsed) {
}
@Override
public boolean isCollapsed() {
return true;
}
@Override
public Set<VertexRef> getVertices() {
Set<VertexRef> retval = new HashSet<>();
retval.add(new DefaultVertexRef("nodes", "g2"));
return retval;
}
@Override
public String getNamespace() {
return "nodes";
}
@Override
public String getLabel() {
return "Test Criteria";
}
@Override
public String getId() {
return "Test Criteria";
}
@Override
public Vertex getCollapsedRepresentation() {
return new AbstractVertex("category", "c");
}
};
Set<Edge> edges = VertexHopGraphProvider.collapseEdges(new HashSet<Edge>(m_provider.getEdges()), new CollapsibleCriteria[] { collapseMe });
for (Edge edge : edges) {
assertEquals("nodes", edge.getNamespace());
/*
Here's the original list of edges
.edge("e1", "g0", "g1").eLabel("edge1").eStyleName("edge")
.edge("e2", "g0", "g2").eLabel("edge2").eStyleName("edge")
.edge("e3", "g1", "v1").eLabel("edge3").eStyleName("edge")
.edge("e4", "g1", "v2").eLabel("edge4").eStyleName("edge")
.edge("e5", "g2", "v3").eLabel("edge5").eStyleName("edge")
.edge("e6", "g2", "v4").eLabel("edge6").eStyleName("edge")
*/
if (edge.getId().equals("e1")) {
assertEquals("nodes", edge.getSource().getVertex().getNamespace());
assertEquals("g0", edge.getSource().getVertex().getId());
assertEquals("nodes", edge.getTarget().getVertex().getNamespace());
assertEquals("g1", edge.getTarget().getVertex().getId());
} else if (edge.getId().equals("collapsedTarget-e2")) {
assertEquals("nodes", edge.getSource().getVertex().getNamespace());
assertEquals("g0", edge.getSource().getVertex().getId());
assertEquals("category", edge.getTarget().getVertex().getNamespace());
assertEquals("c", edge.getTarget().getVertex().getId());
} else if (edge.getId().equals("e3")) {
assertEquals("nodes", edge.getSource().getVertex().getNamespace());
assertEquals("g1", edge.getSource().getVertex().getId());
assertEquals("nodes", edge.getTarget().getVertex().getNamespace());
assertEquals("v1", edge.getTarget().getVertex().getId());
} else if (edge.getId().equals("e4")) {
assertEquals("nodes", edge.getSource().getVertex().getNamespace());
assertEquals("g1", edge.getSource().getVertex().getId());
assertEquals("nodes", edge.getTarget().getVertex().getNamespace());
assertEquals("v2", edge.getTarget().getVertex().getId());
} else if (edge.getId().equals("collapsedSource-e5")) {
assertEquals("category", edge.getSource().getVertex().getNamespace());
assertEquals("c", edge.getSource().getVertex().getId());
assertEquals("nodes", edge.getTarget().getVertex().getNamespace());
assertEquals("v3", edge.getTarget().getVertex().getId());
} else if (edge.getId().equals("collapsedSource-e6")) {
assertEquals("category", edge.getSource().getVertex().getNamespace());
assertEquals("c", edge.getSource().getVertex().getId());
assertEquals("nodes", edge.getTarget().getVertex().getNamespace());
assertEquals("v4", edge.getTarget().getVertex().getId());
} else {
fail("Unexpected edge found: " + edge.toString());
}
}
}
use of org.opennms.features.topology.api.topo.Edge in project opennms by OpenNMS.
the class SFreeTopologyProvider method createERRandomTopology.
private void createERRandomTopology(int numberOfNodes, double averageNumberofNeighboors) {
Map<Integer, SimpleLeafVertex> nodes = new HashMap<Integer, SimpleLeafVertex>();
List<AbstractEdge> edges = new ArrayList<>();
for (Integer i = 0; i < numberOfNodes; i++) {
SimpleLeafVertex vertex = new SimpleLeafVertex(TOPOLOGY_NAMESPACE_SFREE, Integer.toString(i), 0, 0);
vertex.setIconKey("sfree.system");
vertex.setLabel("ErdosReniyNode" + i);
nodes.put(i, vertex);
}
Double z = 0.5 * (numberOfNodes - 1);
// Double p = averageNumberofNeighboors/z;
Random r = new Random((new Date()).getTime());
for (Integer start = 0; start < numberOfNodes; start++) {
for (Integer end = start + 1; end < numberOfNodes; end++) {
if (z * r.nextDouble() < averageNumberofNeighboors) {
String edgeId = "link:" + start + "-" + end;
SimpleConnector source = new SimpleConnector(TOPOLOGY_NAMESPACE_SFREE, nodes.get(start).getId() + "-" + edgeId + "-connector", nodes.get(start));
SimpleConnector target = new SimpleConnector(TOPOLOGY_NAMESPACE_SFREE, nodes.get(end).getId() + "-" + edgeId + "-connector", nodes.get(end));
edges.add(new AbstractEdge(TOPOLOGY_NAMESPACE_SFREE, edgeId, source, target));
}
}
}
addVertices(nodes.values().toArray(new Vertex[] {}));
addEdges(edges.toArray(new Edge[] {}));
}
use of org.opennms.features.topology.api.topo.Edge in project opennms by OpenNMS.
the class SFreeTopologyProvider method createBARandomTopology.
private void createBARandomTopology(int numberOfNodes, double averageNumberofNeighboors) {
Map<Integer, SimpleLeafVertex> nodes = new HashMap<Integer, SimpleLeafVertex>();
List<AbstractEdge> edges = new ArrayList<>();
for (int i = 0; i < 2 * averageNumberofNeighboors; i++) {
LOG.debug("Creating First Cluster from: {}", i);
int j = (i + 1) % ((int) Math.round(2 * averageNumberofNeighboors));
SimpleLeafVertex vertexi = new SimpleLeafVertex(TOPOLOGY_NAMESPACE_SFREE, Integer.toString(i), 0, 0);
vertexi.setIconKey("sfree.system");
vertexi.setLabel("BarabasiAlbertNode" + i);
if (!nodes.containsKey(i)) {
nodes.put(i, vertexi);
LOG.debug("Added Node: {}", vertexi.getId());
}
SimpleLeafVertex vertexj = new SimpleLeafVertex(TOPOLOGY_NAMESPACE_SFREE, Integer.toString(j), 0, 0);
vertexj.setIconKey("sfree.system");
vertexj.setLabel("BarabasiAlbertNode" + j);
if (!nodes.containsKey(j)) {
nodes.put(j, vertexj);
LOG.debug("Added Node: {}", vertexj.getId());
}
String edgeId = "link:" + i + "-" + j;
SimpleConnector source = new SimpleConnector(TOPOLOGY_NAMESPACE_SFREE, nodes.get(i).getId() + "-" + edgeId + "-connector", nodes.get(i));
SimpleConnector target = new SimpleConnector(TOPOLOGY_NAMESPACE_SFREE, nodes.get(j).getId() + "-" + edgeId + "-connector", nodes.get(j));
edges.add(new AbstractEdge(TOPOLOGY_NAMESPACE_SFREE, edgeId, source, target));
LOG.debug("Added Link: {}", edgeId);
}
Random r = new Random((new Date()).getTime());
for (int i = ((int) Math.floor(2 * averageNumberofNeighboors)); i < numberOfNodes; i++) {
SimpleLeafVertex vertexi = new SimpleLeafVertex(TOPOLOGY_NAMESPACE_SFREE, Integer.toString(i), 0, 0);
vertexi.setIconKey("sfree.system");
vertexi.setLabel("BarabasiAlbertNode" + i);
nodes.put(i, vertexi);
LOG.debug("Adding Node: {}", i);
for (int times = 0; times < averageNumberofNeighboors; times++) {
AbstractEdge edge;
// choose node to attach to
double d = r.nextDouble() * nodes.size();
LOG.debug("Generated random position: {}", d);
Long j = (long) d;
LOG.debug("Try Adding edge: {}--->{}", j, i);
String edgeId = "link:" + i + "-" + j.intValue();
SimpleConnector source = new SimpleConnector(TOPOLOGY_NAMESPACE_SFREE, nodes.get(i).getId() + "-" + edgeId + "-connector", nodes.get(i));
SimpleConnector target = new SimpleConnector(TOPOLOGY_NAMESPACE_SFREE, nodes.get(j.intValue()).getId() + "-" + edgeId + "-connector", nodes.get(j.intValue()));
edge = new AbstractEdge(TOPOLOGY_NAMESPACE_SFREE, edgeId, source, target);
if (i == j.intValue())
continue;
edges.add(edge);
}
// m links added
}
addVertices(nodes.values().toArray(new Vertex[] {}));
addEdges(edges.toArray(new Edge[] {}));
}
use of org.opennms.features.topology.api.topo.Edge in project opennms by OpenNMS.
the class TopoFRLayoutAlgorithm method updateLayout.
@Override
public void updateLayout(final Graph graph) {
final Layout graphLayout = graph.getLayout();
SparseGraph<VertexRef, EdgeRef> jungGraph = new SparseGraph<VertexRef, EdgeRef>();
Collection<Vertex> vertices = graph.getDisplayVertices();
for (Vertex v : vertices) {
jungGraph.addVertex(v);
}
Collection<Edge> edges = graph.getDisplayEdges();
for (Edge e : edges) {
jungGraph.addEdge(e, e.getSource().getVertex(), e.getTarget().getVertex());
}
TopoFRLayout<VertexRef, EdgeRef> layout = new TopoFRLayout<VertexRef, EdgeRef>(jungGraph);
// Initialize the vertex positions to the last known positions from the layout
Dimension size = selectLayoutSize(graph);
layout.setInitializer(initializer(graphLayout, (int) size.getWidth() / 2, (int) size.getHeight() / 2));
// Resize the graph to accommodate the number of vertices
layout.setSize(size);
while (!layout.done()) {
layout.step();
}
// Store the new positions in the layout
for (Vertex v : vertices) {
graphLayout.setLocation(v, new Point(layout.getX(v) - (size.getWidth() / 2), (int) layout.getY(v) - (size.getHeight() / 2)));
}
}
Aggregations