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59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
() => {
    var script = document.createElement("script");
    script.src = "https://visjs.github.io/vis-network/standalone/umd/vis-network.min.js";
    document.head.appendChild(script);

    // var current_options;
    // var node_pass_prob, prelen, max_path, node_tokens, node_probs, links, tarlen;
    // var visible_nodes, map_max_path;
    // var visible_edges, sorted_links_out, sorted_links_in;
    // var nodes, edges, network;
    // var disable_edge_select;
    showing_node = undefined;
    showing_edge = undefined;
    
    function obtain_node_object(node_id) {
        var title = "Node " + node_id.toString() + " (Pass Prob = " + node_pass_prob[node_id].toFixed(2) + ")\n";
        title += "==========\n"
        for(var j=0;j<5;j++){
            if(node_probs[node_id][j]> 0.01){
                title += node_tokens[node_id][j] + "\t" + node_probs[node_id][j].toFixed(2) + "\n";
            }
        }

        var currentNode = {"id": node_id, "label": node_tokens[node_id][0], "value":node_pass_prob[node_id], "title": title};
        if (current_options.show_node_detail){
            currentNode.label = title;
        }
        if(map_max_path[node_id] !== undefined){
            currentNode.x = map_max_path[node_id].x;
            currentNode.y = map_max_path[node_id].y;
            currentNode.fixed = true;
            currentNode.mass = 5;
            currentNode.color = {border: "red", background: "orange", highlight: {border: "red", background: "#ffcc66"}};
        }
        return currentNode;
    }

    function set_node_visibility(node_id, flag){
        if(visible_nodes[node_id] == flag) return;
        visible_nodes[node_id] = flag;
        if(flag){
            nodes.add(obtain_node_object(node_id));
        }else{
            nodes.remove(node_id);
        }
    }

    function update_visible_nodes(clear_state=false){
        if(typeof visible_nodes === "undefined" || clear_state){
            visible_nodes = [...Array(prelen)].map((_, __) => false); // size: [prelen]
            map_max_path = {}; // {node_id -> position on max_path}
            var accumulated_x = 0;
            for (var i=0;i<tarlen;i++){
                accumulated_x += node_tokens[max_path[i]][0].length * 5
                var y = Math.floor(Math.random() * 3) * 100 - 100;
                map_max_path[max_path[i]] = {position: i, x: accumulated_x, y:y};
                accumulated_x += node_tokens[max_path[i]][0].length * 5 + 100;
            }
            nodes = new vis.DataSet();
        }

        for (var i=0;i<prelen;i++){
            if(node_pass_prob[i] >= current_options.minimum_node_pass_prob || map_max_path[i] !== undefined){
                set_node_visibility(i, true);
            }else{
                set_node_visibility(i, false);
            }
        }
    }

    
    function update_node_details(){
        for(var i=0;i<prelen;i++) if (visible_nodes[i]){
            currentNode = obtain_node_object(i);
            nodes.updateOnly(currentNode);
        }
    }

    function obtain_edge_object(i, j){
        var edge_id = i.toString() + "-" + j.toString();
        var label = links[i][j].toFixed(2);
        var pass_label = (node_pass_prob[i] * links[i][j]).toFixed(2);
        var title = "From Node " + i.toString() + " to Node " + j.toString() + "\n" + "Transition Probability:" + label + "\nPassing Probability:" + pass_label;
        var currentEdge = {id: edge_id,
            from: i, to: j, value: links[i][j] * node_pass_prob[i], title: title};
        if (map_max_path[i] !== undefined && map_max_path[j] !== undefined && map_max_path[i].position + 1 == map_max_path[j].position){
            currentEdge.color = "red";
        }
        if(current_options.show_edge_label){
            currentEdge.label = label;
        }else{
            currentEdge.label = " ";
        }

        return currentEdge;
    }

    function set_edge_visibility(i, j, flag){
        if(visible_edges[i][j] == flag) return;
        visible_edges[i][j] = flag;
        if(flag){
            edges.add(obtain_edge_object(i, j));
        }else{
            var edge_id = i.toString() + "-" + j.toString();
            edges.remove(edge_id);
        }
    }

    function update_visible_edges(clear_state=false){
        if(typeof visible_edges === "undefined" || clear_state){
            visible_edges = [...Array(prelen)].map((_, __) => {return [...Array(prelen)].map((_, __) => false);}); // size: [prelen, prelen]

            sorted_links_out = []; // size: [prelen, prelen]
            for (var i=0;i<prelen;i++){
                // sort the out edge according to their prob
                sorted_links_out.push(links[i].map((val, idx) => {return {'idx': idx, 'val': val};}).
                    sort((v1, v2) => v2.val - v1.val)
                );
            }

            sorted_links_in = []; // size: [prelen, prelen], element {idx, val}, from big to small
            for (var i=0;i<prelen;i++){
                links_in = []
                for(var j=0;j<prelen;j++) links_in.push({idx: j, val: links[j][i] * node_pass_prob[j]})
                sorted_links_in.push(links_in.sort((v1, v2) => v2.val - v1.val));
            }
            edges = new vis.DataSet();
        }

        var next_visible_edges = [...Array(prelen)].map((_, __) => {return [...Array(prelen)].map((_, __) => false);}); // size: [prelen, prelen]
        var links_in_num = [...Array(prelen)].map((_, __) => 0);

        for (var i=0;i<prelen - 1;i++){
            if(!visible_nodes[i]) continue;

            // select visible out edge of node i
            var left_visible_edge_num = current_options.max_out_edge_num;
            var left_visible_edge_prob = current_options.max_out_edge_prob;
            for(var j=0; j<prelen;j++){
                var idx = sorted_links_out[i][j]['idx'];
                if (sorted_links_out[i][j]['val'] < current_options.minimum_edge_prob) break;
                if (visible_nodes[idx]){
                    links_in_num[idx]++;
                    next_visible_edges[i][idx] = true;
                    left_visible_edge_num--;
                    left_visible_edge_prob -= sorted_links_out[i][j]['val'];
                    if (left_visible_edge_num==0 || left_visible_edge_prob < 0){
                        break;
                    }
                }
            }
        }

        if(current_options.force_in_edge){
            // add at least one in edge per node
            for (var i=0;i<prelen;i++){
                if(i == 0 || !visible_nodes[i] || links_in_num[i] > 0) continue;
                for(var j=0; j<prelen;j++){
                    var idx = sorted_links_in[i][j]['idx'];
                    if (visible_nodes[idx]){
                        next_visible_edges[idx][i] = true;
                        break;
                    }
                }
            }
        }
        
        for(var i=0;i<prelen;i++){
            for(var j=i+1;j<prelen;j++){
                set_edge_visibility(i, j, next_visible_edges[i][j]);
            }
        }
    }

    function update_edge_label(){
        for(var i=0;i<prelen;i++){
            for(var j=i+1;j<prelen;j++) if (visible_edges[i][j]){
                currentEdge = obtain_edge_object(i, j);
                edges.updateOnly(currentEdge);
            }
        }
    }

    function customScalingFunction(min,max,total,value) {
        min = 0;
        var scale = 1 / (max - min);
        return Math.max(0,(value - min)*scale);
    }

    function escapeHtml(unsafe)
    {
        return unsafe
            .replace(/&/g, "&amp;")
            .replace(/</g, "&lt;")
            .replace(/>/g, "&gt;")
            .replace(/"/g, "&quot;")
            .replace(/'/g, "&#039;");
    }
    function get_jumpable_node(idx){
        if(visible_nodes[idx]){
            return "<a href=\"javascript:network.selectNodes(["+ idx.toString() +"]);show_hint_node(" + idx.toString() + ");\">" + idx.toString() + "</a>";
        }else{
            return "<a class=\"invisible\" href=\"javascript:show_hint_node(["+ idx.toString() +"]);network.unselectAll();\">" + idx.toString() + "</a>";
        }
    }
    function get_jumpable_edge(i, j, label){
        var edge_id = i.toString() + "-" + j.toString();
        if(visible_edges[i][j]){
            return "<a href=\"javascript:network.selectEdges(['"+ edge_id +"']);show_hint_edge('" + edge_id + "');\">" + label + "</a>";
        }else{
            return "<a class=\"invisible\" href=\"javascript:show_hint_edge('" + edge_id + "');network.unselectAll();\">" + label + "</a>";
        }
    }

    show_hint_node = function(node_id){
        showing_node = node_id;
        showing_edge = undefined;
        var title = "<p>You selected <b>Node " + node_id.toString() + "</b> ";
        if (visible_nodes[node_id]){
            title += "(<a href=\"javascript:network.fit({nodes:[" + node_id.toString() + "],animation:true});\">Find Me!</a>). "
        }else{
            title += "(Not shown). "
        }
        title += "Passing Probability: <b>" + node_pass_prob[node_id].toFixed(2) + "</b>. You can click the links below to jump to other edges or nodes.</p>";
        document.getElementById("hintsupper").innerHTML = title;

        title = "<table><thead><tr><th>Rank</th><th>Candidate</th><th>Probability</th></tr></thead><tbody>";
        for (var j=0;j<5;j++){
            title += "<tr><td>#" + (j+1).toString() + "</td><td>" + escapeHtml(node_tokens[node_id][j]) + "</td><td>" + node_probs[node_id][j].toFixed(2) + "</td></tr>";
        }
        title += "</tbody>"
        title += "<p>Top-5 Token Candidates: </p>";
        document.getElementById("hintsleft").innerHTML = title;

        title = "<table><thead><tr><th>Rank</th><th>To</th><th>Transition Prob.</th><th>Passing Prob.</th></tr></thead><tbody>";
        for (var j=0;j<prelen;j++){
            var idx = sorted_links_out[node_id][j].idx;
            if(j < 5 || visible_edges[node_id][idx]){
                title += "<tr><td>" + get_jumpable_edge(node_id, idx, "#" + (j+1).toString()) + "</td><td>" + get_jumpable_node(idx) + "</td><td>" + links[node_id][idx].toFixed(2) + "</td><td>" +
                    (node_pass_prob[node_id] * links[node_id][idx]).toFixed(2) + "</td></tr>";
            }
        }
        title += "</tbody>"
        title += "<p>Top Outgoing Edges: </p>"
        document.getElementById("hintscenter").innerHTML = title;

        title = "<table><thead><tr><th>Rank</th><th>From</th><th>Transition Prob.</th><th>Passing Prob.</th></tr></thead><tbody>";
        for (var j=0;j<prelen;j++){
            var idx = sorted_links_in[node_id][j].idx;
            if(j < 5 || visible_edges[idx][node_id]){
                title += "<tr><td>" + get_jumpable_edge(idx, node_id, "#" + (j+1).toString()) + "</td><td>" + get_jumpable_node(idx) + "</td><td>" + links[idx][node_id].toFixed(2) + "</td><td>" +
                    (node_pass_prob[idx] * links[idx][node_id]).toFixed(2) + "</td></tr>";
            }
        }
        title += "</tbody>"
        title += "<p>Top Incoming Edges: </p>"
        document.getElementById("hintsright").innerHTML = title;

        document.getElementById("hintsbottom").innerHTML =   
          "<br>"+ 
          "Passing probability of a node V represents how likely the node will be choosen in a random path, i.e., P(V \\in A). <br>" + 
          "Passing probability of an edge from U to V represents how likely the node V follows the node U in a random path, i.e., P(a_i = U && a_{i+1} = V). <br>" + 
          "Token probability represents how likely a token is predicted on the given node, i.e., P(y_i| v_{a_{i}}). <br>" + 
          "Transition probability represents how likely a specific node is following the given node, i.e. P(a_{i+1} | a_{i}).<br>"
    }

    show_hint_edge = function(edge_id){
        showing_edge = edge_id;
        showing_node = undefined;
        var i = parseInt(edge_id.split("-")[0]);
        var j = parseInt(edge_id.split("-")[1]);
        var label = links[i][j].toFixed(2);
        var passing_label = (links[i][j] * node_pass_prob[i]).toFixed(2);
        var title = "You selected an edge from <b>Node " + get_jumpable_node(i) + " to Node " + get_jumpable_node(j) + "</b>."
        if (visible_edges[i][j]){
            title += "(<a href=\"javascript:network.fit({nodes:[" + i.toString() + "," + j.toString() + "],animation:true});\">Find Me!</a>). "
        }else{
            title += "(Not shown). "
        }
        title += "<br> You can click the links above to jump to the nodes. <br><br>"
        title += "Transition Probability:<b>" + label + "</b><br>";
        title += "Passing Probability:<b>" + passing_label + "</b><br>";
        title += "<br>" + 
            "Transition probability represents how likely a specific node is following the given node, i.e. P(a_{i+1} | a_{i}).<br>" + 
            "Passing probability of an edge from U to V represents how likely the node V follows the node U in a random path, i.e., P(a_i = U && a_{i+1} = V). <br>"
        document.getElementById("hintsupper").innerHTML = title;
        document.getElementById("hintsleft").innerHTML = "";
        document.getElementById("hintsright").innerHTML = "";
        document.getElementById("hintscenter").innerHTML = "";
        document.getElementById("hintsbottom").innerHTML = "";
    }

    function clear_hint(){
        showing_node = undefined;
        showing_edge = undefined;
        document.getElementById("hintsupper").innerHTML = "Use scroll to zoom in or out. Select or Hover over nodes and edges for more information ... (Try dragging nodes to replace them.)";
        document.getElementById("hintsleft").innerHTML = "";
        document.getElementById("hintsright").innerHTML = "";
        document.getElementById("hintscenter").innerHTML = "";
        document.getElementById("hintsbottom").innerHTML = "";
    }

    startNetwork = function(graph_info, options) {
        current_options = options;

        global_graph_info = graph_info;
        node_pass_prob = graph_info['node_pass_prob'][0] // size: [prelen]
        prelen = node_pass_prob.length
        max_path = graph_info['max_paths'][0] // size: [tarlen]
        tarlen = max_path.length
        node_tokens = graph_info['node_tokens'][0] // size: [prelen, 5]
        node_probs = graph_info['node_probs'][0] // size: [prelen, 5]
        links = graph_info['links'][0] // size: [prelen, prelen]

        update_visible_nodes(true);
        update_visible_edges(true);

        // create a network
        var container = document.getElementById("daggraph");
        var data = {
            nodes: nodes,
            edges: edges,
        };
        network_options = {
            nodes: {
                shape: "ellipse",
                scaling: {
                    label: {
                      min: 8,
                      max: 20,
                    },
                    customScalingFunction: customScalingFunction,
                },
            },
            edges: {
                arrowStrikethrough: false,
                arrows: "to",
                smooth: {
                    type: "continuous"
                },
                color: "#2B7CE9",
                font: { align: "bottom" },
                length: 120,
                scaling: {
                    min: 0.5,
                    max: 3,
                    label: {
                        min: 8,
                        max: 15,
                    },
                    customScalingFunction: customScalingFunction,
                }
            }
        };
        network = new vis.Network(container, data, network_options);

        network.off("dragStart");
        network.on("dragStart", function (params) {
            var idx = this.getNodeAt(params.pointer.DOM);
            if (idx !== undefined) {
                // console.log("dragstart " + idx.toString());
                if (map_max_path[idx] !== undefined){
                    data.nodes.update({id: idx, fixed: false});
                }
            }
        });
        network.off("dragEnd");
        network.on("dragEnd", function (params) {
            var idx = this.getNodeAt(params.pointer.DOM);
            if (idx !== undefined){
                // console.log("dragend " + idx.toString());
                if (map_max_path[idx] !== undefined){
                    data.nodes.update({id: idx, fixed: true});
                    map_max_path[idx].x = params.pointer.canvas.x;
                    map_max_path[idx].y = params.pointer.canvas.y;
                }
            }
        });

        disable_edge_select = false;
        network.off("selectNode");
        network.on("selectNode", function (params) {
            var node_id = params.nodes[0];
            show_hint_node(node_id);
            disable_edge_select = true;
            setTimeout(() => {disable_edge_select=false;}, 200);
        });

        network.off("selectEdge");
        network.on("selectEdge", function (params) {
            if(disable_edge_select) return;
            var edge_id = params.edges[0];
            show_hint_edge(edge_id);
        });

        network.off("deselectNode");
        network.on("deselectNode", function (params) {
            clear_hint();
            showing_node = undefined;
            showing_edge = undefined;
        });
        network.off("deselectEdge");
        network.on("deselectEdge", function (params) {
            clear_hint();
        });
    }

    updateNetwork = function(options) {
        if(typeof node_pass_prob === "undefined") return;
        old_options = current_options;
        current_options = options;
        if(options.minimum_node_pass_prob != old_options.minimum_node_pass_prob){
            update_visible_nodes();
        }
        if(options.minimum_node_pass_prob != old_options.minimum_node_pass_prob ||
            options.minimum_edge_prob != old_options.minimum_edge_prob ||
            options.max_out_edge_num != old_options.max_out_edge_num ||
            options.max_out_edge_prob != old_options.max_out_edge_prob ||
            options.force_in_edge != old_options.force_in_edge){
            update_visible_edges();
        }
        if(options.show_node_detail != old_options.show_node_detail){
            if(options.show_node_detail) {
                network_options.nodes.shape = "dot";
                network_options.nodes.scaling.label.min=10;
                network_options.nodes.scaling.label.max=10;
            }else{
                network_options.nodes.shape = "ellipse";
                network_options.nodes.scaling.label.min=8;
                network_options.nodes.scaling.label.max=20;
            }
            network.setOptions(network_options);
            update_node_details();
        }
        if(options.show_edge_label != old_options.show_edge_label){
            update_edge_label();
        }

        if(showing_node != undefined){
            show_hint_node(showing_node);
        }
        if(showing_edge != undefined){
            show_hint_edge(showing_edge);
        }
    }
}