如何正确修改JGraphT中的顶点?

时间:2019-09-08 21:04:01

标签: java oop jgrapht

我用一些顶点建立了一个图。顶点属于我自己的类,包含一些信息,但不包含更多信息。 然后,我遍历该图,如果满足某些条件,则更改这些信息。之后,我尝试打印带有已修改信息的新图形,但似乎只有一些顶点被修改。

我已经检查了我是否真的在未修改的节点上工作,是的。因此,我的修改代码有效,但有时会打印未经修改的顶点版本。

首先,我创建图形。 g是整个类的全局变量。

package test;

import com.mxgraph.layout.*;
import com.mxgraph.swing.*;

import org.bouncycastle.util.Arrays;
import org.jgrapht.*;
import org.jgrapht.alg.DijkstraShortestPath;
import org.jgrapht.ext.*;
import org.jgrapht.graph.*;
import org.jgrapht.traverse.BreadthFirstIterator;
import org.jgrapht.traverse.DepthFirstIterator;
import org.jgrapht.traverse.GraphIterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JsonArray;
import org.json.simple.JsonObject;
import java.util.concurrent.ThreadLocalRandom;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.Queue;

/**
 * A demo applet that shows how to use JGraphX to visualize JGraphT graphs.
 * Applet based on JGraphAdapterDemo.
 *
 */
public class JGraphXAdapterDemo extends JApplet {
    static ListenableDirectedGraph<MyVertex, DefaultEdge> g;

    public static void assignLevel() {

        MyVertex node = (MyVertex) g.vertexSet().toArray()[0];
        System.out.println("Start: " + node);
        Stack<MyVertex> q = new Stack<MyVertex>();
        Stack<MyVertex> utilq = new Stack<MyVertex>();

        q.add(node);
        while (!q.isEmpty()) {
            MyVertex current = q.pop();
            utilq.add(current);

            if (!current.visited) {

                current.visited = true;
            }
            if (!g.outgoingEdgesOf(current).isEmpty()) {
                for (DefaultEdge a : g.outgoingEdgesOf(current)) {
                    MyVertex neighbor = g.getEdgeTarget(a);
                    if (!neighbor.visited) {

                        q.push(neighbor);
                    }
                }

            } else {
                int count = 0;
                System.out.println("UtilQ");
                while (utilq.size() > 0) {
                    MyVertex todo = utilq.pop();
                    System.out.println("Then:" + todo);
                    if (g.outgoingEdgesOf(todo).isEmpty()) {
                        todo.level = 0;

                    } else {
                        for (DefaultEdge a : g.outgoingEdgesOf(todo)) {
                            todo.setLevel(g.getEdgeTarget(a).level + 1);
                        }
                    }
                    System.out.println("Now: " + todo);
                }
                utilq = new Stack<MyVertex>();
            }

        }

    }

    public static class MyVertex {
        String name;
        boolean visited = false;
        double prio = 0.0;
        int level = 0;

        public double getPrio() {
            return prio;
        }

        public void setPrio(double prio) {
            this.prio = prio;
        }

        public int getEta() {
            return eta;
        }

        public void setEta(int eta) {
            this.eta = eta;
        }

        int eta;

        public MyVertex(String name) {
            this.name = name;
        }

        public int getLevel() {
            return level;
        }

        public void setLevel(int level) {
            this.level = level;
        }

        public MyVertex(String name, int eta) {
            this.name = name;
            this.eta = eta;
        }

        @Override
        public String toString() {
            return "name: " + name.replace("donatello_", "") + " level:" + level;
        }

        @Override
        public int hashCode() {
            return name.hashCode();
        }

        @Override
        public boolean equals(Object e) {
            if (e == null) {
                return false;
            } else if (!(e instanceof MyVertex)) {
                return false;
            }
            if (e instanceof MyVertex && ((MyVertex) e).name.equals(this.name)) {
                return true;
            } else {
                return false;
            }
        }
    }

    public JGraphXAdapterDemo(JSONObject json) throws HeadlessException, InterruptedException {

        // create a JGraphT graph
        g = new ListenableDirectedGraph<MyVertex, DefaultEdge>(DefaultEdge.class);
        // create a visualization using JGraph, via an adapter

        JSONArray nodes = (JSONArray) json.get("nodes");
        MyVertex v = new MyVertex(null);

        for (Object s : nodes) {
            String w = new String(s.toString());
            // System.out.println(w.toString());
            int eta = ThreadLocalRandom.current().nextInt(1, 10 + 1);

            v = new MyVertex(w, 100);
            g.addVertex(v);
        }

        // System.out.println(g.containsVertex("donatello_head"));

        JSONObject edges = (JSONObject) json.get("edges");

        for (Object o : edges.keySet()) {
            String p = o.toString();
            String s = edges.get(o).toString();
            // System.out.println("s -----> " + s);
            s = s.replace("[", "").replace("]", "");
            String[] q = s.split(", ");
            // System.out.println("q ----> " + java.util.Arrays.asList(q));
            for (String x : java.util.Arrays.asList(q)) {

                String[] a = x.split(",");
                for (String r : a) {
                    v = new MyVertex(p);
                    MyVertex w = new MyVertex(r.replace("\"", ""));
                    DefaultEdge f = g.addEdge(v, w);
                    int randomNum = ThreadLocalRandom.current().nextInt(1, 10 + 1);

                    v.setEta(randomNum);

                }

            }
        }

        assignLevel();
        GraphIterator<MyVertex, DefaultEdge> iterator2 = new BreadthFirstIterator<MyVertex, DefaultEdge>(g);
        System.out.println("Final versions of vertices");

        while (iterator2.hasNext()) {
            System.out.println(iterator2.next());
        }

        // visualize(g);

    }

    public static void visualize(ListenableDirectedGraph<MyVertex, DefaultEdge> g2) {
        JFrame frame = new JFrame("DemoGraph");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ListenableGraph<MyVertex, DefaultEdge> a = g2;

        JGraphXAdapter<MyVertex, DefaultEdge> graphAdapter = new JGraphXAdapter<MyVertex, DefaultEdge>(a);

        mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
        layout.execute(graphAdapter.getDefaultParent());

        frame.add(new mxGraphComponent(graphAdapter));

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

    private static final long serialVersionUID = 2202072534703043194L;

}

作为参数的JSON字符串应为: “ {\” nodes \“:[\” donatello_plate \“,\” donatello_r_leg \“,\” donatello_l_leg \“,\” donatello_lower_torso \“,\” donatello_upper_torso \“,\\” donatello_l_arm \“,\” donatello_skin_shell \“ ,\“ donatello_r_arm \”,\“ donatello_head \”],“ startNode \”:\“ donatello_plate \”,\“ edges \”:{\“ donatello_skin_shell \”:[\“ donatello_head \”],\“ donatello_upper_torso \“:[\” donatello_l_arm \“,\” donatello_skin_shell \“,\” donatello_r_arm \“],\” donatello_plate \“:[\” donatello_r_leg \“,\” donatello_l_leg \“],\” donatello_r_arm \“:[ \“ donatello_head \”],\“ donatello_lower_torso \”],\“ donatello_upper_torso \”],\“ donatello_l_arm \”:[\“ donatello_head \”],\“ donatello_r_leg \”:[\“ donatello_lower_torso \”],\ “ donatello_l_leg \”:[\“ donatello_lower_torso \”]}}“

只需将其转换为Json并将其作为参数传递即可。

因此,如您在assignLevel()的打印中看到的那样,“ lower_torso” .level设置为3,但是当我打印最终结果时,它又是0。“ r_leg”也是如此。但是其他人都是正确的

0 个答案:

没有答案