use of org.jgrapht.traverse.BreadthFirstIterator in project tutorials by eugenp.
the class DirectedGraphTests method givenDirectedGraph_whenCreateInstanceBreadthFirstIterator_thenGetIterator.
@Test
public void givenDirectedGraph_whenCreateInstanceBreadthFirstIterator_thenGetIterator() {
BreadthFirstIterator breadthFirstIterator = new BreadthFirstIterator<>(directedGraph);
assertNotNull(breadthFirstIterator);
}
use of org.jgrapht.traverse.BreadthFirstIterator in project opennars by opennars.
the class AbegoTreeLayout method postUpdate.
@Override
public boolean postUpdate(AbstractGraphVis<V, E> g) {
if (finished)
return true;
float px = 0, py = 0;
float horizontalMargin = 16f;
float levelGap = 32f;
float nodeGap = 8f;
DirectedGraph<V, E> graph = (DirectedGraph<V, E>) g.getGraph();
ArrayList<V> roots = new ArrayList<V>();
for (V v : graph.vertexSet()) if (graph.inDegreeOf(v) == 0)
roots.add(v);
KruskalMinimumSpanningTree<V, E> st = new KruskalMinimumSpanningTree<V, E>(graph);
for (V r : roots) {
DefaultTreeForTreeLayout<VertexVis<V, E>> tree = new DefaultTreeForTreeLayout(g.getVertexDisplay(r));
DirectedMultigraph<V, E> subgraph = new DirectedMultigraph(DefaultEdge.class);
subgraph.addVertex(r);
// add all other vertices which are not roots
for (V v : graph.vertexSet()) {
if (!roots.contains(v))
subgraph.addVertex(v);
}
for (E e : st.getMinimumSpanningTreeEdgeSet()) {
V from = graph.getEdgeSource(e);
V to = graph.getEdgeTarget(e);
// continue;
if (!subgraph.containsVertex(from) || !subgraph.containsVertex(to))
continue;
// processed.add(from);
// processed.add(to);
subgraph.addEdge(from, to, e);
}
BreadthFirstIterator<V, E> i = new BreadthFirstIterator<V, E>(subgraph, r) {
@Override
protected void encounterVertex(V parent, E edge) {
super.encounterVertex(parent, edge);
VertexVis p = g.getVertexDisplay(parent);
Set<E> children = graph.outgoingEdgesOf(parent);
VertexVis[] cv = new VertexVis[children.size()];
int j = 0;
for (E ec : children) {
V vc = graph.getEdgeTarget(ec);
cv[j++] = g.getVertexDisplay(vc);
}
try {
tree.addChildren(p, cv);
} catch (Exception e) {
System.err.println(e);
System.err.println("vertex -> " + parent + " " + Arrays.toString(cv));
}
}
};
while (i.hasNext()) i.next();
TreeLayout<VertexVis<V, E>> treeLayout = new TreeLayout(tree, new NodeExtentProvider<VertexVis<V, E>>() {
@Override
public double getWidth(VertexVis<V, E> tn) {
return tn.getRadius();
}
@Override
public double getHeight(VertexVis<V, E> tn) {
return tn.getRadius();
}
}, new DefaultConfiguration(levelGap, nodeGap));
Map<VertexVis<V, E>, Rectangle2D.Double> bounds = treeLayout.getNodeBounds();
for (Map.Entry<VertexVis<V, E>, Rectangle2D.Double> vv : bounds.entrySet()) {
VertexVis<V, E> v = vv.getKey();
Rectangle2D.Double b = vv.getValue();
v.setPosition(px + (float) b.getCenterX(), py + (float) b.getCenterY());
}
// System.out.println(bounds);
px += treeLayout.getBounds().getWidth() + horizontalMargin;
}
finished = true;
return true;
}
use of org.jgrapht.traverse.BreadthFirstIterator in project candle-decompiler by bradsdavis.
the class RetractDuplicateFinally method collectOffsets.
protected Set<Integer> collectOffsets(AbstractIntermediate line) {
// this is the finally template.
AbstractIntermediate first = igc.getSingleSuccessor(line);
// now, store the instruction position.
int position = first.getInstruction().getPosition();
BreadthFirstIterator<AbstractIntermediate, IntermediateEdge> bfi = new BreadthFirstIterator<AbstractIntermediate, IntermediateEdge>(igc.getGraph(), first);
Set<Integer> offsets = new TreeSet<Integer>();
while (bfi.hasNext()) {
AbstractIntermediate next = bfi.next();
int nextPosition = next.getInstruction().getPosition();
int offset = nextPosition - position;
LOG.debug("Offset: " + offset);
offsets.add(offset);
}
return offsets;
}
use of org.jgrapht.traverse.BreadthFirstIterator in project candle-decompiler by bradsdavis.
the class CatchUpperRangeVisitor method visitCatchIntermediate.
@Override
public void visitCatchIntermediate(CatchIntermediate line) {
// first, check if the line has an end already..
if (line.getBlockRange().getEnd() != null) {
return;
}
// processLastCatch(line);
BreadthFirstIterator<AbstractIntermediate, IntermediateEdge> bfi = new BreadthFirstIterator<AbstractIntermediate, IntermediateEdge>(igc.getGraph(), line);
AbstractIntermediate lastStatement = null;
while (bfi.hasNext()) {
AbstractIntermediate next = bfi.next();
if (next instanceof GoToIntermediate) {
// this would be a possible GOTO... find previous.
LOG.debug("Catch GOGO: " + next + " goto:" + next.getInstruction().getPosition());
lastStatement = igc.getSinglePredecessor(next);
break;
}
if (next instanceof StatementIntermediate) {
// determine what type of statement...
if (((StatementIntermediate) next).getExpression() instanceof Throw) {
lastStatement = next;
break;
}
if (((StatementIntermediate) next).getExpression() instanceof Return) {
lastStatement = next;
break;
}
}
}
if (lastStatement != null) {
line.getBlockRange().setEnd(lastStatement.getInstruction());
}
}
Aggregations