@statewalker/fsm-charts 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/LICENSE +21 -0
  3. package/README.md +1 -0
  4. package/dist/StateChart-DNGQd1XG.d.ts +53 -0
  5. package/dist/chunk-3GGM3ZWL.js +2685 -0
  6. package/dist/chunk-NXWOFQMO.js +10 -0
  7. package/dist/chunk-TPNWHU7Y.js +437 -0
  8. package/dist/index-layout.d.ts +96 -0
  9. package/dist/index-layout.js +2 -0
  10. package/dist/index-svg.d.ts +25 -0
  11. package/dist/index-svg.js +2 -0
  12. package/dist/index.d.ts +3 -0
  13. package/dist/index.js +3 -0
  14. package/package.json +60 -0
  15. package/src/FsmStateConfig.ts +5 -0
  16. package/src/dagre/acyclic.d.ts +2 -0
  17. package/src/dagre/acyclic.js +60 -0
  18. package/src/dagre/add-border-segments.d.ts +1 -0
  19. package/src/dagre/add-border-segments.js +36 -0
  20. package/src/dagre/coordinate-system.d.ts +2 -0
  21. package/src/dagre/coordinate-system.js +75 -0
  22. package/src/dagre/data/list.d.ts +12 -0
  23. package/src/dagre/data/list.js +55 -0
  24. package/src/dagre/debug.d.ts +2 -0
  25. package/src/dagre/debug.js +32 -0
  26. package/src/dagre/greedy-fas.d.ts +1 -0
  27. package/src/dagre/greedy-fas.js +126 -0
  28. package/src/dagre/index.d.ts +5 -0
  29. package/src/dagre/index.js +6 -0
  30. package/src/dagre/layout.d.ts +1 -0
  31. package/src/dagre/layout.js +466 -0
  32. package/src/dagre/nesting-graph.d.ts +2 -0
  33. package/src/dagre/nesting-graph.js +137 -0
  34. package/src/dagre/normalize.d.ts +2 -0
  35. package/src/dagre/normalize.js +91 -0
  36. package/src/dagre/order/add-subgraph-constraints.d.ts +1 -0
  37. package/src/dagre/order/add-subgraph-constraints.js +53 -0
  38. package/src/dagre/order/barycenter.d.ts +15 -0
  39. package/src/dagre/order/barycenter.js +31 -0
  40. package/src/dagre/order/build-layer-graph.d.ts +2 -0
  41. package/src/dagre/order/build-layer-graph.js +76 -0
  42. package/src/dagre/order/cross-count.d.ts +1 -0
  43. package/src/dagre/order/cross-count.js +82 -0
  44. package/src/dagre/order/index.d.ts +1 -0
  45. package/src/dagre/order/index.js +77 -0
  46. package/src/dagre/order/init-order.d.ts +1 -0
  47. package/src/dagre/order/init-order.js +44 -0
  48. package/src/dagre/order/resolve-conflicts.d.ts +1 -0
  49. package/src/dagre/order/resolve-conflicts.js +128 -0
  50. package/src/dagre/order/sort-subgraph.d.ts +8 -0
  51. package/src/dagre/order/sort-subgraph.js +78 -0
  52. package/src/dagre/order/sort.d.ts +6 -0
  53. package/src/dagre/order/sort.js +59 -0
  54. package/src/dagre/parent-dummy-chains.d.ts +1 -0
  55. package/src/dagre/parent-dummy-chains.js +86 -0
  56. package/src/dagre/position/bk.d.ts +31 -0
  57. package/src/dagre/position/bk.js +423 -0
  58. package/src/dagre/position/index.d.ts +1 -0
  59. package/src/dagre/position/index.js +31 -0
  60. package/src/dagre/rank/feasible-tree.d.ts +2 -0
  61. package/src/dagre/rank/feasible-tree.js +87 -0
  62. package/src/dagre/rank/index.d.ts +1 -0
  63. package/src/dagre/rank/index.js +52 -0
  64. package/src/dagre/rank/network-simplex.d.ts +16 -0
  65. package/src/dagre/rank/network-simplex.js +236 -0
  66. package/src/dagre/rank/util.d.ts +2 -0
  67. package/src/dagre/rank/util.js +63 -0
  68. package/src/dagre/util.d.ts +27 -0
  69. package/src/dagre/util.js +250 -0
  70. package/src/graphlib/alg/components.d.ts +1 -0
  71. package/src/graphlib/alg/components.js +27 -0
  72. package/src/graphlib/alg/dfs.d.ts +1 -0
  73. package/src/graphlib/alg/dfs.js +46 -0
  74. package/src/graphlib/alg/dijkstra-all.d.ts +1 -0
  75. package/src/graphlib/alg/dijkstra-all.js +14 -0
  76. package/src/graphlib/alg/dijkstra.d.ts +1 -0
  77. package/src/graphlib/alg/dijkstra.js +65 -0
  78. package/src/graphlib/alg/find-cycles.d.ts +1 -0
  79. package/src/graphlib/alg/find-cycles.js +10 -0
  80. package/src/graphlib/alg/floyd-warshall.d.ts +1 -0
  81. package/src/graphlib/alg/floyd-warshall.js +55 -0
  82. package/src/graphlib/alg/index.d.ts +24 -0
  83. package/src/graphlib/alg/index.js +25 -0
  84. package/src/graphlib/alg/is-acyclic.d.ts +1 -0
  85. package/src/graphlib/alg/is-acyclic.js +15 -0
  86. package/src/graphlib/alg/postorder.d.ts +1 -0
  87. package/src/graphlib/alg/postorder.js +7 -0
  88. package/src/graphlib/alg/preorder.d.ts +1 -0
  89. package/src/graphlib/alg/preorder.js +7 -0
  90. package/src/graphlib/alg/prim.d.ts +2 -0
  91. package/src/graphlib/alg/prim.js +52 -0
  92. package/src/graphlib/alg/tarjan.d.ts +1 -0
  93. package/src/graphlib/alg/tarjan.js +47 -0
  94. package/src/graphlib/alg/topsort.d.ts +5 -0
  95. package/src/graphlib/alg/topsort.js +36 -0
  96. package/src/graphlib/data/priority-queue.d.ts +59 -0
  97. package/src/graphlib/data/priority-queue.js +152 -0
  98. package/src/graphlib/graph.d.ts +55 -0
  99. package/src/graphlib/graph.js +510 -0
  100. package/src/graphlib/index.d.ts +3 -0
  101. package/src/graphlib/index.js +7 -0
  102. package/src/graphlib/json.d.ts +16 -0
  103. package/src/graphlib/json.js +63 -0
  104. package/src/index-layout.ts +1 -0
  105. package/src/index-svg.ts +1 -0
  106. package/src/index.ts +2 -0
  107. package/src/layout/Dimensions.ts +8 -0
  108. package/src/layout/GraphParamsProvider.ts +17 -0
  109. package/src/layout/Padding.ts +10 -0
  110. package/src/layout/Position.ts +4 -0
  111. package/src/layout/StateChart.ts +8 -0
  112. package/src/layout/StateGraphEdge.ts +14 -0
  113. package/src/layout/StateGraphNode.ts +12 -0
  114. package/src/layout/Transition.ts +5 -0
  115. package/src/layout/TransitionsGraph.ts +13 -0
  116. package/src/layout/buildCharts.ts +74 -0
  117. package/src/layout/buildFlatCharts.ts +142 -0
  118. package/src/layout/getGraphParamsProvider.ts +63 -0
  119. package/src/layout/getLabelDimensions.ts +59 -0
  120. package/src/layout/index.ts +15 -0
  121. package/src/lodash-es/_deleteme.js +291 -0
  122. package/src/lodash-es/index.ts +9 -0
  123. package/src/lodash-es/types.ts +35 -0
  124. package/src/svg/buildStatechartCss.ts +120 -0
  125. package/src/svg/buildStatechartSvg.ts +256 -0
  126. package/src/svg/buildStatechartsPanel.ts +38 -0
  127. package/src/svg/index.ts +3 -0
  128. package/src/utils/BasisCurve.ts +89 -0
  129. package/src/utils/getPadding.ts +13 -0
  130. package/src/utils/serializeAttrs.ts +28 -0
  131. package/src/utils/serializeStyle.ts +11 -0
  132. package/src/utils/toKebabCase.ts +18 -0
@@ -0,0 +1,52 @@
1
+ import { feasibleTree } from './feasible-tree.js';
2
+ import { networkSimplex } from './network-simplex.js';
3
+ import { longestPath } from './util.js';
4
+
5
+ export { rank };
6
+
7
+ /*
8
+ * Assigns a rank to each node in the input graph that respects the "minlen"
9
+ * constraint specified on edges between nodes.
10
+ *
11
+ * This basic structure is derived from Gansner, et al., "A Technique for
12
+ * Drawing Directed Graphs."
13
+ *
14
+ * Pre-conditions:
15
+ *
16
+ * 1. Graph must be a connected DAG
17
+ * 2. Graph nodes must be objects
18
+ * 3. Graph edges must have "weight" and "minlen" attributes
19
+ *
20
+ * Post-conditions:
21
+ *
22
+ * 1. Graph nodes will have a "rank" attribute based on the results of the
23
+ * algorithm. Ranks can start at any index (including negative), we'll
24
+ * fix them up later.
25
+ */
26
+ function rank(g) {
27
+ switch (g.graph().ranker) {
28
+ case 'network-simplex':
29
+ networkSimplexRanker(g);
30
+ break;
31
+ case 'tight-tree':
32
+ tightTreeRanker(g);
33
+ break;
34
+ case 'longest-path':
35
+ longestPathRanker(g);
36
+ break;
37
+ default:
38
+ networkSimplexRanker(g);
39
+ }
40
+ }
41
+
42
+ // A fast and simple ranker, but results are far from optimal.
43
+ var longestPathRanker = longestPath;
44
+
45
+ function tightTreeRanker(g) {
46
+ longestPath(g);
47
+ feasibleTree(g);
48
+ }
49
+
50
+ function networkSimplexRanker(g) {
51
+ networkSimplex(g);
52
+ }
@@ -0,0 +1,16 @@
1
+ export function networkSimplex(g: any): void;
2
+ export namespace networkSimplex {
3
+ export { initLowLimValues };
4
+ export { initCutValues };
5
+ export { calcCutValue };
6
+ export { leaveEdge };
7
+ export { enterEdge };
8
+ export { exchangeEdges };
9
+ }
10
+ declare function initLowLimValues(tree: any, root: any, ...args: any[]): void;
11
+ declare function initCutValues(t: any, g: any): void;
12
+ declare function calcCutValue(t: any, g: any, child: any): number;
13
+ declare function leaveEdge(tree: any): any;
14
+ declare function enterEdge(t: any, g: any, edge: any): any;
15
+ declare function exchangeEdges(t: any, g: any, e: any, f: any): void;
16
+ export {};
@@ -0,0 +1,236 @@
1
+ import { _ } from '../../lodash-es/index.ts';
2
+ import * as alg from '../../graphlib/alg/index.js';
3
+ import { simplify } from '../util.js';
4
+ import { feasibleTree } from './feasible-tree.js';
5
+ import { longestPath, slack } from './util.js';
6
+
7
+ export { networkSimplex };
8
+
9
+ // Expose some internals for testing purposes
10
+ networkSimplex.initLowLimValues = initLowLimValues;
11
+ networkSimplex.initCutValues = initCutValues;
12
+ networkSimplex.calcCutValue = calcCutValue;
13
+ networkSimplex.leaveEdge = leaveEdge;
14
+ networkSimplex.enterEdge = enterEdge;
15
+ networkSimplex.exchangeEdges = exchangeEdges;
16
+
17
+ /*
18
+ * The network simplex algorithm assigns ranks to each node in the input graph
19
+ * and iteratively improves the ranking to reduce the length of edges.
20
+ *
21
+ * Preconditions:
22
+ *
23
+ * 1. The input graph must be a DAG.
24
+ * 2. All nodes in the graph must have an object value.
25
+ * 3. All edges in the graph must have "minlen" and "weight" attributes.
26
+ *
27
+ * Postconditions:
28
+ *
29
+ * 1. All nodes in the graph will have an assigned "rank" attribute that has
30
+ * been optimized by the network simplex algorithm. Ranks start at 0.
31
+ *
32
+ *
33
+ * A rough sketch of the algorithm is as follows:
34
+ *
35
+ * 1. Assign initial ranks to each node. We use the longest path algorithm,
36
+ * which assigns ranks to the lowest position possible. In general this
37
+ * leads to very wide bottom ranks and unnecessarily long edges.
38
+ * 2. Construct a feasible tight tree. A tight tree is one such that all
39
+ * edges in the tree have no slack (difference between length of edge
40
+ * and minlen for the edge). This by itself greatly improves the assigned
41
+ * rankings by shorting edges.
42
+ * 3. Iteratively find edges that have negative cut values. Generally a
43
+ * negative cut value indicates that the edge could be removed and a new
44
+ * tree edge could be added to produce a more compact graph.
45
+ *
46
+ * Much of the algorithms here are derived from Gansner, et al., "A Technique
47
+ * for Drawing Directed Graphs." The structure of the file roughly follows the
48
+ * structure of the overall algorithm.
49
+ */
50
+ function networkSimplex(g) {
51
+ g = simplify(g);
52
+ longestPath(g);
53
+ var t = feasibleTree(g);
54
+ initLowLimValues(t);
55
+ initCutValues(t, g);
56
+
57
+ var e, f;
58
+ while ((e = leaveEdge(t))) {
59
+ f = enterEdge(t, g, e);
60
+ exchangeEdges(t, g, e, f);
61
+ }
62
+ }
63
+
64
+ /*
65
+ * Initializes cut values for all edges in the tree.
66
+ */
67
+ function initCutValues(t, g) {
68
+ var vs = alg.postorder(t, t.nodes());
69
+ vs = vs.slice(0, vs.length - 1);
70
+ _.forEach(vs, function (v) {
71
+ assignCutValue(t, g, v);
72
+ });
73
+ }
74
+
75
+ function assignCutValue(t, g, child) {
76
+ var childLab = t.node(child);
77
+ var parent = childLab.parent;
78
+ t.edge(child, parent).cutvalue = calcCutValue(t, g, child);
79
+ }
80
+
81
+ /*
82
+ * Given the tight tree, its graph, and a child in the graph calculate and
83
+ * return the cut value for the edge between the child and its parent.
84
+ */
85
+ function calcCutValue(t, g, child) {
86
+ var childLab = t.node(child);
87
+ var parent = childLab.parent;
88
+ // True if the child is on the tail end of the edge in the directed graph
89
+ var childIsTail = true;
90
+ // The graph's view of the tree edge we're inspecting
91
+ var graphEdge = g.edge(child, parent);
92
+ // The accumulated cut value for the edge between this node and its parent
93
+ var cutValue = 0;
94
+
95
+ if (!graphEdge) {
96
+ childIsTail = false;
97
+ graphEdge = g.edge(parent, child);
98
+ }
99
+
100
+ cutValue = graphEdge.weight;
101
+
102
+ _.forEach(g.nodeEdges(child), function (e) {
103
+ var isOutEdge = e.v === child,
104
+ other = isOutEdge ? e.w : e.v;
105
+
106
+ if (other !== parent) {
107
+ var pointsToHead = isOutEdge === childIsTail,
108
+ otherWeight = g.edge(e).weight;
109
+
110
+ cutValue += pointsToHead ? otherWeight : -otherWeight;
111
+ if (isTreeEdge(t, child, other)) {
112
+ var otherCutValue = t.edge(child, other).cutvalue;
113
+ cutValue += pointsToHead ? -otherCutValue : otherCutValue;
114
+ }
115
+ }
116
+ });
117
+
118
+ return cutValue;
119
+ }
120
+
121
+ function initLowLimValues(tree, root) {
122
+ if (arguments.length < 2) {
123
+ root = tree.nodes()[0];
124
+ }
125
+ dfsAssignLowLim(tree, {}, 1, root);
126
+ }
127
+
128
+ function dfsAssignLowLim(tree, visited, nextLim, v, parent) {
129
+ var low = nextLim;
130
+ var label = tree.node(v);
131
+
132
+ visited[v] = true;
133
+ _.forEach(tree.neighbors(v), function (w) {
134
+ if (!_.has(visited, w)) {
135
+ nextLim = dfsAssignLowLim(tree, visited, nextLim, w, v);
136
+ }
137
+ });
138
+
139
+ label.low = low;
140
+ label.lim = nextLim++;
141
+ if (parent) {
142
+ label.parent = parent;
143
+ } else {
144
+ // TODO should be able to remove this when we incrementally update low lim
145
+ delete label.parent;
146
+ }
147
+
148
+ return nextLim;
149
+ }
150
+
151
+ function leaveEdge(tree) {
152
+ return _.find(tree.edges(), function (e) {
153
+ return tree.edge(e).cutvalue < 0;
154
+ });
155
+ }
156
+
157
+ function enterEdge(t, g, edge) {
158
+ var v = edge.v;
159
+ var w = edge.w;
160
+
161
+ // For the rest of this function we assume that v is the tail and w is the
162
+ // head, so if we don't have this edge in the graph we should flip it to
163
+ // match the correct orientation.
164
+ if (!g.hasEdge(v, w)) {
165
+ v = edge.w;
166
+ w = edge.v;
167
+ }
168
+
169
+ var vLabel = t.node(v);
170
+ var wLabel = t.node(w);
171
+ var tailLabel = vLabel;
172
+ var flip = false;
173
+
174
+ // If the root is in the tail of the edge then we need to flip the logic that
175
+ // checks for the head and tail nodes in the candidates function below.
176
+ if (vLabel.lim > wLabel.lim) {
177
+ tailLabel = wLabel;
178
+ flip = true;
179
+ }
180
+
181
+ var candidates = _.filter(g.edges(), function (edge) {
182
+ return (
183
+ flip === isDescendant(t, t.node(edge.v), tailLabel) &&
184
+ flip !== isDescendant(t, t.node(edge.w), tailLabel)
185
+ );
186
+ });
187
+
188
+ return _.minBy(candidates, function (edge) {
189
+ return slack(g, edge);
190
+ });
191
+ }
192
+
193
+ function exchangeEdges(t, g, e, f) {
194
+ var v = e.v;
195
+ var w = e.w;
196
+ t.removeEdge(v, w);
197
+ t.setEdge(f.v, f.w, {});
198
+ initLowLimValues(t);
199
+ initCutValues(t, g);
200
+ updateRanks(t, g);
201
+ }
202
+
203
+ function updateRanks(t, g) {
204
+ var root = _.find(t.nodes(), function (v) {
205
+ return !g.node(v).parent;
206
+ });
207
+ var vs = alg.preorder(t, root);
208
+ vs = vs.slice(1);
209
+ _.forEach(vs, function (v) {
210
+ var parent = t.node(v).parent,
211
+ edge = g.edge(v, parent),
212
+ flipped = false;
213
+
214
+ if (!edge) {
215
+ edge = g.edge(parent, v);
216
+ flipped = true;
217
+ }
218
+
219
+ g.node(v).rank = g.node(parent).rank + (flipped ? edge.minlen : -edge.minlen);
220
+ });
221
+ }
222
+
223
+ /*
224
+ * Returns true if the edge is in the tree.
225
+ */
226
+ function isTreeEdge(tree, u, v) {
227
+ return tree.hasEdge(u, v);
228
+ }
229
+
230
+ /*
231
+ * Returns true if the specified node is descendant of the root node per the
232
+ * assigned low and lim attributes in the tree.
233
+ */
234
+ function isDescendant(tree, vLabel, rootLabel) {
235
+ return rootLabel.low <= vLabel.lim && vLabel.lim <= rootLabel.lim;
236
+ }
@@ -0,0 +1,2 @@
1
+ export function longestPath(g: any): void;
2
+ export function slack(g: any, e: any): number;
@@ -0,0 +1,63 @@
1
+ import { _ } from '../../lodash-es/index.ts';
2
+
3
+ export { longestPath, slack };
4
+
5
+ /*
6
+ * Initializes ranks for the input graph using the longest path algorithm. This
7
+ * algorithm scales well and is fast in practice, it yields rather poor
8
+ * solutions. Nodes are pushed to the lowest layer possible, leaving the bottom
9
+ * ranks wide and leaving edges longer than necessary. However, due to its
10
+ * speed, this algorithm is good for getting an initial ranking that can be fed
11
+ * into other algorithms.
12
+ *
13
+ * This algorithm does not normalize layers because it will be used by other
14
+ * algorithms in most cases. If using this algorithm directly, be sure to
15
+ * run normalize at the end.
16
+ *
17
+ * Pre-conditions:
18
+ *
19
+ * 1. Input graph is a DAG.
20
+ * 2. Input graph node labels can be assigned properties.
21
+ *
22
+ * Post-conditions:
23
+ *
24
+ * 1. Each node will be assign an (unnormalized) "rank" property.
25
+ */
26
+ function longestPath(g) {
27
+ var visited = {};
28
+
29
+ function dfs(v) {
30
+ var label = g.node(v);
31
+ if (_.has(visited, v)) {
32
+ return label.rank;
33
+ }
34
+ visited[v] = true;
35
+
36
+ var rank = _.min(
37
+ _.map(g.outEdges(v), function (e) {
38
+ return dfs(e.w) - g.edge(e).minlen;
39
+ })
40
+ );
41
+
42
+ if (
43
+ rank === Number.POSITIVE_INFINITY || // return value of _.map([]) for Lodash 3
44
+ rank === undefined || // return value of _.map([]) for Lodash 4
45
+ rank === null
46
+ ) {
47
+ // return value of _.map([null])
48
+ rank = 0;
49
+ }
50
+
51
+ return (label.rank = rank);
52
+ }
53
+
54
+ _.forEach(g.sources(), dfs);
55
+ }
56
+
57
+ /*
58
+ * Returns the amount of slack for the given edge. The slack is defined as the
59
+ * difference between the length of the edge and its minimum length.
60
+ */
61
+ function slack(g, e) {
62
+ return g.node(e.w).rank - g.node(e.v).rank - g.edge(e).minlen;
63
+ }
@@ -0,0 +1,27 @@
1
+ export function addDummyNode(g: any, type: any, attrs: any, name: any): string;
2
+ export function simplify(g: any): Graph;
3
+ export function asNonCompoundGraph(g: any): Graph;
4
+ export function successorWeights(g: any): import('lodash').Dictionary<{}>;
5
+ export function predecessorWeights(g: any): import('lodash').Dictionary<{}>;
6
+ export function intersectRect(
7
+ rect: any,
8
+ point: any
9
+ ): {
10
+ x: any;
11
+ y: any;
12
+ };
13
+ export function buildLayerMatrix(g: any): any[][];
14
+ export function normalizeRanks(g: any): void;
15
+ export function removeEmptyRanks(g: any): void;
16
+ export function addBorderNode(g: any, prefix: any, rank: any, order: any, ...args: any[]): string;
17
+ export function maxRank(g: any): any;
18
+ export function partition(
19
+ collection: any,
20
+ fn: any
21
+ ): {
22
+ lhs: any[];
23
+ rhs: any[];
24
+ };
25
+ export function time(name: any, fn: any): any;
26
+ export function notime(name: any, fn: any): any;
27
+ import { Graph } from '../graphlib/index.js';
@@ -0,0 +1,250 @@
1
+ import { _ } from '../lodash-es/index.ts';
2
+ import { Graph } from '../graphlib/index.js';
3
+
4
+ export {
5
+ addDummyNode,
6
+ simplify,
7
+ asNonCompoundGraph,
8
+ successorWeights,
9
+ predecessorWeights,
10
+ intersectRect,
11
+ buildLayerMatrix,
12
+ normalizeRanks,
13
+ removeEmptyRanks,
14
+ addBorderNode,
15
+ maxRank,
16
+ partition,
17
+ time,
18
+ notime,
19
+ };
20
+
21
+ /*
22
+ * Adds a dummy node to the graph and return v.
23
+ */
24
+ function addDummyNode(g, type, attrs, name) {
25
+ var v;
26
+ do {
27
+ v = _.uniqueId(name);
28
+ } while (g.hasNode(v));
29
+
30
+ attrs.dummy = type;
31
+ g.setNode(v, attrs);
32
+ return v;
33
+ }
34
+
35
+ /*
36
+ * Returns a new graph with only simple edges. Handles aggregation of data
37
+ * associated with multi-edges.
38
+ */
39
+ function simplify(g) {
40
+ var simplified = new Graph().setGraph(g.graph());
41
+ _.forEach(g.nodes(), function (v) {
42
+ simplified.setNode(v, g.node(v));
43
+ });
44
+ _.forEach(g.edges(), function (e) {
45
+ var simpleLabel = simplified.edge(e.v, e.w) || { weight: 0, minlen: 1 };
46
+ var label = g.edge(e);
47
+ simplified.setEdge(e.v, e.w, {
48
+ weight: simpleLabel.weight + label.weight,
49
+ minlen: Math.max(simpleLabel.minlen, label.minlen),
50
+ });
51
+ });
52
+ return simplified;
53
+ }
54
+
55
+ function asNonCompoundGraph(g) {
56
+ var simplified = new Graph({ multigraph: g.isMultigraph() }).setGraph(g.graph());
57
+ _.forEach(g.nodes(), function (v) {
58
+ if (!g.children(v).length) {
59
+ simplified.setNode(v, g.node(v));
60
+ }
61
+ });
62
+ _.forEach(g.edges(), function (e) {
63
+ simplified.setEdge(e, g.edge(e));
64
+ });
65
+ return simplified;
66
+ }
67
+
68
+ function successorWeights(g) {
69
+ var weightMap = _.map(g.nodes(), function (v) {
70
+ var sucs = {};
71
+ _.forEach(g.outEdges(v), function (e) {
72
+ sucs[e.w] = (sucs[e.w] || 0) + g.edge(e).weight;
73
+ });
74
+ return sucs;
75
+ });
76
+ return _.zipObject(g.nodes(), weightMap);
77
+ }
78
+
79
+ function predecessorWeights(g) {
80
+ var weightMap = _.map(g.nodes(), function (v) {
81
+ var preds = {};
82
+ _.forEach(g.inEdges(v), function (e) {
83
+ preds[e.v] = (preds[e.v] || 0) + g.edge(e).weight;
84
+ });
85
+ return preds;
86
+ });
87
+ return _.zipObject(g.nodes(), weightMap);
88
+ }
89
+
90
+ /*
91
+ * Finds where a line starting at point ({x, y}) would intersect a rectangle
92
+ * ({x, y, width, height}) if it were pointing at the rectangle's center.
93
+ */
94
+ function intersectRect(rect, point) {
95
+ var x = rect.x;
96
+ var y = rect.y;
97
+
98
+ // Rectangle intersection algorithm from:
99
+ // http://math.stackexchange.com/questions/108113/find-edge-between-two-boxes
100
+ var dx = point.x - x;
101
+ var dy = point.y - y;
102
+ var w = rect.width / 2;
103
+ var h = rect.height / 2;
104
+
105
+ if (!dx && !dy) {
106
+ throw new Error('Not possible to find intersection inside of the rectangle');
107
+ }
108
+
109
+ var sx, sy;
110
+ if (Math.abs(dy) * w > Math.abs(dx) * h) {
111
+ // Intersection is top or bottom of rect.
112
+ if (dy < 0) {
113
+ h = -h;
114
+ }
115
+ sx = (h * dx) / dy;
116
+ sy = h;
117
+ } else {
118
+ // Intersection is left or right of rect.
119
+ if (dx < 0) {
120
+ w = -w;
121
+ }
122
+ sx = w;
123
+ sy = (w * dy) / dx;
124
+ }
125
+
126
+ return { x: x + sx, y: y + sy };
127
+ }
128
+
129
+ /*
130
+ * Given a DAG with each node assigned "rank" and "order" properties, this
131
+ * function will produce a matrix with the ids of each node.
132
+ */
133
+ function buildLayerMatrix(g) {
134
+ var layering = _.map(_.range(maxRank(g) + 1), function () {
135
+ return [];
136
+ });
137
+ _.forEach(g.nodes(), function (v) {
138
+ var node = g.node(v);
139
+ var rank = node.rank;
140
+ if (!_.isUndefined(rank)) {
141
+ layering[rank][node.order] = v;
142
+ }
143
+ });
144
+ return layering;
145
+ }
146
+
147
+ /*
148
+ * Adjusts the ranks for all nodes in the graph such that all nodes v have
149
+ * rank(v) >= 0 and at least one node w has rank(w) = 0.
150
+ */
151
+ function normalizeRanks(g) {
152
+ var min = _.min(
153
+ _.map(g.nodes(), function (v) {
154
+ return g.node(v).rank;
155
+ })
156
+ );
157
+ _.forEach(g.nodes(), function (v) {
158
+ var node = g.node(v);
159
+ if (_.has(node, 'rank')) {
160
+ node.rank -= min;
161
+ }
162
+ });
163
+ }
164
+
165
+ function removeEmptyRanks(g) {
166
+ // Ranks may not start at 0, so we need to offset them
167
+ var offset = _.min(
168
+ _.map(g.nodes(), function (v) {
169
+ return g.node(v).rank;
170
+ })
171
+ );
172
+
173
+ var layers = [];
174
+ _.forEach(g.nodes(), function (v) {
175
+ var rank = g.node(v).rank - offset;
176
+ if (!layers[rank]) {
177
+ layers[rank] = [];
178
+ }
179
+ layers[rank].push(v);
180
+ });
181
+
182
+ var delta = 0;
183
+ var nodeRankFactor = g.graph().nodeRankFactor;
184
+ _.forEach(layers, function (vs, i) {
185
+ if (_.isUndefined(vs) && i % nodeRankFactor !== 0) {
186
+ --delta;
187
+ } else if (delta) {
188
+ _.forEach(vs, function (v) {
189
+ g.node(v).rank += delta;
190
+ });
191
+ }
192
+ });
193
+ }
194
+
195
+ function addBorderNode(g, prefix, rank, order) {
196
+ var node = {
197
+ width: 0,
198
+ height: 0,
199
+ };
200
+ if (arguments.length >= 4) {
201
+ node.rank = rank;
202
+ node.order = order;
203
+ }
204
+ return addDummyNode(g, 'border', node, prefix);
205
+ }
206
+
207
+ function maxRank(g) {
208
+ return _.max(
209
+ _.map(g.nodes(), function (v) {
210
+ var rank = g.node(v).rank;
211
+ if (!_.isUndefined(rank)) {
212
+ return rank;
213
+ }
214
+ })
215
+ );
216
+ }
217
+
218
+ /*
219
+ * Partition a collection into two groups: `lhs` and `rhs`. If the supplied
220
+ * function returns true for an entry it goes into `lhs`. Otherwise it goes
221
+ * into `rhs.
222
+ */
223
+ function partition(collection, fn) {
224
+ var result = { lhs: [], rhs: [] };
225
+ _.forEach(collection, function (value) {
226
+ if (fn(value)) {
227
+ result.lhs.push(value);
228
+ } else {
229
+ result.rhs.push(value);
230
+ }
231
+ });
232
+ return result;
233
+ }
234
+
235
+ /*
236
+ * Returns a new function that wraps `fn` with a timer. The wrapper logs the
237
+ * time it takes to execute the function.
238
+ */
239
+ function time(name, fn) {
240
+ var start = _.now();
241
+ try {
242
+ return fn();
243
+ } finally {
244
+ console.log(name + ' time: ' + (_.now() - start) + 'ms');
245
+ }
246
+ }
247
+
248
+ function notime(name, fn) {
249
+ return fn();
250
+ }
@@ -0,0 +1 @@
1
+ export function components(g: any): any[];
@@ -0,0 +1,27 @@
1
+ import { _ } from '../../lodash-es/index.ts';
2
+
3
+ export { components };
4
+
5
+ function components(g) {
6
+ var visited = {};
7
+ var cmpts = [];
8
+ var cmpt;
9
+
10
+ function dfs(v) {
11
+ if (_.has(visited, v)) return;
12
+ visited[v] = true;
13
+ cmpt.push(v);
14
+ _.each(g.successors(v), dfs);
15
+ _.each(g.predecessors(v), dfs);
16
+ }
17
+
18
+ _.each(g.nodes(), function (v) {
19
+ cmpt = [];
20
+ dfs(v);
21
+ if (cmpt.length) {
22
+ cmpts.push(cmpt);
23
+ }
24
+ });
25
+
26
+ return cmpts;
27
+ }
@@ -0,0 +1 @@
1
+ export function dfs(g: any, vs: any, order: any): any[];