Debian Codegraph @

lib/normalize.js

raw symbols SCIP
1 "use strict";
2
3 var _ = require("./lodash");
4 var util = require("./util");
5
6 module.exports = {
7 run: run,
8 undo: undo
9 };
10
11 /*
12 * Breaks any long edges in the graph into short segments that span 1 layer
13 * each. This operation is undoable with the denormalize function.
14 *
15 * Pre-conditions:
16 *
17 * 1. The input graph is a DAG.
18 * 2. Each node in the graph has a "rank" property.
19 *
20 * Post-condition:
21 *
22 * 1. All edges in the graph have a length of 1.
23 * 2. Dummy nodes are added where edges have been split into segments.
24 * 3. The graph is augmented with a "dummyChains" attribute which contains
25 * the first dummy in each chain of dummy nodes produced.
26 */
27 function run(g) {
28 g.graph().dummyChains = [];
29 _.forEach(g.edges(), function(edge) { normalizeEdge(g, edge); });
30 }
31
32 function normalizeEdge(g, e) {
33 var v = e.v;
34 var vRank = g.node(v).rank;
35 var w = e.w;
36 var wRank = g.node(w).rank;
37 var name = e.name;
38 var edgeLabel = g.edge(e);
39 var labelRank = edgeLabel.labelRank;
40
41 if (wRank === vRank + 1) return;
42
43 g.removeEdge(e);
44
45 var dummy, attrs, i;
46 for (i = 0, ++vRank; vRank < wRank; ++i, ++vRank) {
47 edgeLabel.points = [];
48 attrs = {
49 width: 0, height: 0,
50 edgeLabel: edgeLabel, edgeObj: e,
51 rank: vRank
52 };
53 dummy = util.addDummyNode(g, "edge", attrs, "_d");
54 if (vRank === labelRank) {
55 attrs.width = edgeLabel.width;
56 attrs.height = edgeLabel.height;
57 attrs.dummy = "edge-label";
58 attrs.labelpos = edgeLabel.labelpos;
59 }
60 g.setEdge(v, dummy, { weight: edgeLabel.weight }, name);
61 if (i === 0) {
62 g.graph().dummyChains.push(dummy);
63 }
64 v = dummy;
65 }
66
67 g.setEdge(v, w, { weight: edgeLabel.weight }, name);
68 }
69
70 function undo(g) {
71 _.forEach(g.graph().dummyChains, function(v) {
72 var node = g.node(v);
73 var origLabel = node.edgeLabel;
74 var w;
75 g.setEdge(node.edgeObj, origLabel);
76 while (node.dummy) {
77 w = g.successors(v)[0];
78 g.removeNode(v);
79 origLabel.points.push({ x: node.x, y: node.y });
80 if (node.dummy === "edge-label") {
81 origLabel.x = node.x;
82 origLabel.y = node.y;
83 origLabel.width = node.width;
84 origLabel.height = node.height;
85 }
86 v = w;
87 node = g.node(v);
88 }
89 });
90 }