javajung

Jung API - How to add new Edge between two existing nodes


I try to make a lattice with Jung like this :

my target

Until now, I made the link between 2 stages, but I don't know how to make the link between 2 existing Vertex.

Here the link between the stage 1 et 2 :

my target

Here the link between the stage 2 et 3 :

my target

Here the link between the stage 3 et 4 :

my target

The problem is that I cannot make all the stages together because I can't add a Edge with an existing vertex. It will make this error :

Exception in thread "main" java.lang.IllegalArgumentException: Tree must not already contain child µ1234
    at edu.uci.ics.jung.graph.DelegateTree.addChild(DelegateTree.java:182)
    at edu.uci.ics.jung.graph.DelegateTree.addEdge(DelegateTree.java:102)
    at edu.uci.ics.jung.graph.DelegateTree.addEdge(DelegateTree.java:346)
    at edu.uci.ics.jung.graph.util.TreeUtils.growSubTree(TreeUtils.java:76)
    at edu.uci.ics.jung.graph.util.TreeUtils.growSubTree(TreeUtils.java:80)
    at edu.uci.ics.jung.graph.DelegateForest.getTrees(DelegateForest.java:295)
    at edu.uci.ics.jung.graph.util.TreeUtils.getRoots(TreeUtils.java:34)
    at edu.uci.ics.jung.algorithms.layout.TreeLayout.buildTree(TreeLayout.java:102)
    at edu.uci.ics.jung.algorithms.layout.TreeLayout.<init>(TreeLayout.java:97)
    at edu.uci.ics.jung.algorithms.layout.TreeLayout.<init>(TreeLayout.java:75)
    at code.Gui_Arbre.<init>(Gui_Arbre.java:48)
    at code.Gui_Arbre.main(Gui_Arbre.java:171)

Here is my code :

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
import javax.swing.JFrame;
import org.apache.commons.collections15.Factory;
import org.apache.commons.collections15.functors.ConstantTransformer;
import edu.uci.ics.jung.algorithms.layout.TreeLayout;
import edu.uci.ics.jung.graph.DelegateForest;
import edu.uci.ics.jung.graph.Forest;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;

@SuppressWarnings({ "serial", "deprecation" })
public class Gui_Arbre extends JApplet {
    private Fuzzy_Mesure fm;
    /**
     * le graph
     */
    Forest<U, Integer> graph;
    // mod?le de lien entre noeud
    Factory<Integer> edgeFactory = new Factory<Integer>() {
        int i = 0;

        public Integer create() {
            return i++;
        }
    };
    /**
     * l'?l?ment visuel
     */
    VisualizationViewer<U, Integer> vv;
    TreeLayout<U, Integer> layout;

    @SuppressWarnings("unchecked")
    public Gui_Arbre(Fuzzy_Mesure fm) {
        // create a simple graph for the demo
        graph = new DelegateForest<U, Integer>();
        this.fm = fm;
        createTree();
        layout = new TreeLayout<U, Integer>(graph);
        vv = new VisualizationViewer<U, Integer>(layout, new Dimension(600, 600));
        vv.setBackground(Color.white);
        // personnalisation des fleches
        vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line<U, Integer>());
        vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray));
        // affiche les labels
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<U>());

        Container content = getContentPane();
        final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
        content.add(panel);
        final DefaultModalGraphMouse<U, Integer> graphMouse = new DefaultModalGraphMouse<>();
        vv.setGraphMouse(graphMouse);
    }

    /**
     * cr?ation de l'arbre
     */

    private void createTree() {
        for (int i = 0; i < this.fm.getLevel(); i++) {// parcourir etage
            for (int y = 0; y < this.fm.getMap().get(i).size(); y++) {// parcourir list de l'etage

                // create a link between stage 1 and 2
                if (i == this.fm.getLevel() - 2) {

                    for (int o = 0; o < this.fm.getMap().get(i).get(y).getEnfant().size(); o++) {
                        graph.addEdge(edgeFactory.create(), this.fm.getMap().get(i).get(y),
                                this.fm.getMap().get(i).get(y).getEnfant().get(o));

                    }

                }
                if (i == this.fm.getLevel() - 3) {// create a link between stage 2 and 3, but as stage 2 already exists
                                                    // with existing Vertex, there is an error

                    for (int o = 0; o < this.fm.getMap().get(i).get(y).getEnfant().size(); o++) {
                        graph.addEdge(edgeFactory.create(), this.fm.getMap().get(i).get(y),
                                this.fm.getMap().get(i).get(y).getEnfant().get(o));

                    }

                }

            }

        }

    }

    /**
     * Tests
     */
    public static void main(String[] args) {

        JFrame frame = new JFrame();
        Container content = frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        List<U> set = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            set.add(new U(1));
        }
        Fuzzy_Mesure fm = new Fuzzy_Mesure(set);
        content.add(new Gui_Arbre(fm));

        frame.pack();
        frame.setVisible(true);
    }
}

Solution

  • JUNG defines a tree as a graph from which there is exactly one path from the (designated) root to any vertex.

    A lattice is not a tree, nor is it a forest, so you can't construct the graph you want as a tree/forest; you need to use the general Graph type.

    This will also mean that you can't (directly) use TreeLayout on your graph. You can construct a spanning tree of your graph and use TreeLayout on that, but it probably won't look like your diagram above.