vscode-debug-visualizer icon indicating copy to clipboard operation
vscode-debug-visualizer copied to clipboard

[Java] Not able to visualize Graph in Java

Open Mayank0896 opened this issue 3 years ago • 4 comments

Hi Hediet, First of all thanks for making such cool extension and contributing it to the community. I am trying to visualize a Graph. But I am facing some issues with it. When I am passing Graph as a json as per format shared by you. It is showing json as an object only not a graph.

`JSONObject mainJsonObject = new JSONObject();

    JSONObject typeGraph = new JSONObject();
    typeGraph.put("graph", true);
    mainJsonObject.put("kind", typeGraph);

    int[][] edges = new int[][] { { 0, 1 }, { 0, 4 }, { 1, 4 }, { 1, 3 }, { 4, 3 }, { 1, 2 }, { 3, 2 } };

    JSONArray arrNodes = new JSONArray();
    ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        adj.add(new ArrayList<>());
        JSONObject nodeU = new JSONObject();
        nodeU.put("id", String.valueOf(i));
        nodeU.put("label", String.valueOf(i));
        arrNodes.add(nodeU);
    }

    mainJsonObject.put("nodes", arrNodes);

    for (int i = 0; i < edges.length; ++i) {
        int u = edges[i][0];
        int v = edges[i][1];
        adj.get(u).add(v);
    }

    JSONArray arrEdges = new JSONArray();
    for (int u = 0; u < adj.size(); u++) {
        for (int v : adj.get(u)) {
            // u->v is edge
            JSONObject edgeUV = new JSONObject();
            edgeUV.put("from", String.valueOf(u));
            edgeUV.put("to", String.valueOf(v));
            arrEdges.add(edgeUV);
        }
    }

    mainJsonObject.put("edges", arrEdges);

    // const example1 = {
    // "kind": { "graph": true },
    // "nodes": [
    // { "id": "1", "label": "1" },
    // { "id": "2", "label": "2", "color": "orange" },
    // { "id": "3", "label": "3" }
    // ],
    // "edges": [
    // { "from": "1", "to": "2", "color": "red" },
    // { "from": "1", "to": "3" }
    // ]
    // };
    System.out.println(mainJsonObject);`

mainJsonObject is the json of Graph created from Nested arraylist. But when I am trying to visualize then it is showing it as an object only. image Please refer the screenshot. It looks cool though but it is of no use.

I am trying to pass mainJsonObject directly to expression bar.

Json passed by my code is completely valid ( checked on jsonlint) - { "nodes": [ { "id": "0", "label": "0" }, { "id": "1", "label": "1" }, { "id": "2", "label": "2" }, { "id": "3", "label": "3" }, { "id": "4", "label": "4" } ], "kind": { "graph": true }, "edges": [ { "from": "0", "to": "1" }, { "from": "0", "to": "4" }, { "from": "1", "to": "4" }, { "from": "1", "to": "3" }, { "from": "1", "to": "2" }, { "from": "3", "to": "2" }, { "from": "4", "to": "3" } ] }

Mayank0896 avatar Apr 29 '22 11:04 Mayank0896

Try mainJsonObject.toString(). The value to visualize must be a (JSON) string!

hediet avatar Apr 29 '22 16:04 hediet

I tried mainJsonObject.toString() but visualization is this - image

Mayank0896 avatar Apr 29 '22 16:04 Mayank0896

Then something changed in the implementation of the Java Debug adapter :(

It should be very easy to fix though. Can you look into it? The source code of this extension has instructions how to build it.

hediet avatar May 02 '22 07:05 hediet

Hi @hediet , I suppose this extension is build in TypeScript. I have no idea about typescript or javascript. Just a beginner to Java. So, I can't promise any fix from my side.

Mayank0896 avatar May 02 '22 13:05 Mayank0896