-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.js
More file actions
163 lines (142 loc) · 3.31 KB
/
Graph.js
File metadata and controls
163 lines (142 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
class Graph {
constructor(def) {
this.nodesByLabel = new Map();
this.nodesByPageRank = new Map();
this.nodesBySubgraph = new Map();
this.groups = [];
this.links = [];
this.nodes = [];
this.tags = [];
this.label = null;
this.processList(def.editor);
this.removeSoftNodes();
this.resolveLinks();
this.resolveGroups();
this.resolveTags();
this.flattenNodes();
this.setPageRank();
this.setSubgraph();
this.bucketNodes();
}
processList(list, soft=false) {
for (let item of list) {
this.processItem(item, soft);
}
}
processItem(item, soft=false) {
switch (item.type) {
case 'group':
this.processGroup(item);
break;
case 'label':
this.processLabel(item);
break;
case 'link':
this.processLink(item);
break;
case 'node':
this.processNode(item, soft);
break;
case 'tag':
this.processTag(item);
break;
}
}
processGroup(item) {
let group = GraphGroup.process(item);
if (!group) {
return;
}
this.groups.push(group);
this.processList(item.members, true);
}
processLabel(item) {
if (item.label == '') {
return;
}
this.label = item.label;
this.labelId = item.id;
}
processLink(item) {
let link = GraphLink.process(item);
if (!link) {
return;
}
this.links.push(link);
this.processItem(item.from, true);
this.processItem(item.to, true);
}
processNode(item, soft=false) {
let node = GraphNode.process(item, soft);
if (!node) {
return;
}
getOrSet(this.nodesByLabel, node.label, []).push(node);
}
processTag(item) {
let tag = GraphTag.process(item);
if (!tag) {
return;
}
this.tags.push(tag);
this.processList(item.members, true);
}
removeSoftNodes() {
for (let nodes of this.nodesByLabel.values()) {
for (let i = nodes.length - 1; i >= 0 && nodes.length > 1; --i) {
if (nodes[i].soft) {
nodes.splice(i, 1);
}
}
}
}
resolveLinks() {
for (let link of this.links) {
link.resolve(this.nodesByLabel);
}
}
resolveGroups() {
for (let group of this.groups) {
group.resolve(this.nodesByLabel);
}
}
resolveTags() {
for (let tag of this.tags) {
tag.resolve(this.nodesByLabel);
}
}
flattenNodes() {
for (let nodes of this.nodesByLabel.values()) {
this.nodes.push(...nodes);
}
}
setPageRank() {
for (let link of this.links) {
link.setPageRank();
}
for (let group of this.groups) {
group.setPageRank();
}
}
setSubgraph() {
let nodes = new Set(this.nodes);
for (let subgraph = 0; nodes.size; ++subgraph) {
let node = nodes.values().next().value;
node.setSubgraph(subgraph, nodes);
}
}
bucketNodes() {
for (let node of this.nodes) {
getOrSet(this.nodesByPageRank, node.pageRank, []).push(node);
getOrSet(this.nodesBySubgraph, node.subgraph, []).push(node);
}
for (let nodes of this.nodesByPageRank.values()) {
// Ensure deterministic sort order
nodes.sort();
}
}
}
<!--# include file="GraphGroup.js" -->
<!--# include file="GraphLink.js" -->
<!--# include file="GraphNode.js" -->
<!--# include file="GraphTag.js" -->