Search in sources :

Example 1 with BreadthFirstIterator

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);
}
Also used : BreadthFirstIterator(org.jgrapht.traverse.BreadthFirstIterator) Test(org.junit.Test)

Example 2 with 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;
}
Also used : DirectedMultigraph(org.jgrapht.graph.DirectedMultigraph) ArrayList(java.util.ArrayList) DirectedGraph(org.jgrapht.DirectedGraph) Rectangle2D(java.awt.geom.Rectangle2D) DefaultConfiguration(org.abego.treelayout.util.DefaultConfiguration) DefaultTreeForTreeLayout(org.abego.treelayout.util.DefaultTreeForTreeLayout) VertexVis(automenta.vivisect.graph.VertexVis) BreadthFirstIterator(org.jgrapht.traverse.BreadthFirstIterator) DefaultTreeForTreeLayout(org.abego.treelayout.util.DefaultTreeForTreeLayout) TreeLayout(org.abego.treelayout.TreeLayout) KruskalMinimumSpanningTree(org.jgrapht.alg.KruskalMinimumSpanningTree) Map(java.util.Map)

Example 3 with BreadthFirstIterator

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;
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) BreadthFirstIterator(org.jgrapht.traverse.BreadthFirstIterator) TreeSet(java.util.TreeSet) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge)

Example 4 with BreadthFirstIterator

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());
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) BreadthFirstIterator(org.jgrapht.traverse.BreadthFirstIterator) Return(org.candle.decompiler.intermediate.expression.Return) GoToIntermediate(org.candle.decompiler.intermediate.code.GoToIntermediate) Throw(org.candle.decompiler.intermediate.expression.Throw) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge)

Aggregations

BreadthFirstIterator (org.jgrapht.traverse.BreadthFirstIterator)4 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)2 IntermediateEdge (org.candle.decompiler.intermediate.graph.edge.IntermediateEdge)2 VertexVis (automenta.vivisect.graph.VertexVis)1 Rectangle2D (java.awt.geom.Rectangle2D)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 TreeLayout (org.abego.treelayout.TreeLayout)1 DefaultConfiguration (org.abego.treelayout.util.DefaultConfiguration)1 DefaultTreeForTreeLayout (org.abego.treelayout.util.DefaultTreeForTreeLayout)1 GoToIntermediate (org.candle.decompiler.intermediate.code.GoToIntermediate)1 StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)1 Return (org.candle.decompiler.intermediate.expression.Return)1 Throw (org.candle.decompiler.intermediate.expression.Throw)1 DirectedGraph (org.jgrapht.DirectedGraph)1 KruskalMinimumSpanningTree (org.jgrapht.alg.KruskalMinimumSpanningTree)1 DirectedMultigraph (org.jgrapht.graph.DirectedMultigraph)1 Test (org.junit.Test)1