forked from odpi/happi-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
happi-graph-algorithms.js
192 lines (153 loc) · 4.55 KB
/
happi-graph-algorithms.js
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
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
/*
let nodes = [
{ id: 4, value: "Node 4" },
{ id: 2, value: "Node 2" },
{ id: 1, value: "Node 1" },
{ id: 3, value: "Node 3" },
{ id: 5, value: "Node 5" },
{ id: 6, value: "Node 6" },
{ id: 7, value: "Node 7" },
{ id: 8, value: "Node 8" }
];
let edges = [
{ from: 4, to: 5 },
{ from: 3, to: 4 },
{ from: 1, to: 4 },
{ from: 2, to: 4 },
{ from: 5, to: 6 },
{ from: 5, to: 7 },
{ from: 5, to: 8 },
];
// */
/*
* Current implementation example flow on the parsing algorithm.
*
-> anotherNode(-1, 1) -> anotherAnotherNode(-1, 2)
/ ^
secondPart(0, -1) -> selectedNode(0, 0) -> firstPart (0, 1) /
^ \ \ /
\ \ -> oneMoreHere (1, 1) /
\ \ \
\ ---------------------> twoOne (2, 1) |
\ /
\ ------------------------------------------- /
*/
function mapped(_data) {
let result = {};
_data.nodes.forEach(n => {
result[n.id] = {
...n,
linksTo: [
..._data.links.filter(e => e.from === n.id).map(e => {
return e.to
})
],
linksFrom: [
..._data.links.filter(e => e.to === n.id).map(e => {
return e.from
})
],
visited: false
}
});
return result;
}
function initializeCoords(currentNode, orientation, linksCount, side) {
let coords = {};
if(orientation === 'HORIZONTAL') {
coords = {
ww: side > 0 ? currentNode.w - 1 : currentNode.w + 1,
hh: currentNode.h - parseInt(linksCount / 2)
}
} else if(orientation === 'VERTICAL') {
coords = {
ww: currentNode.w - parseInt(linksCount / 2),
hh: side > 0 ? currentNode.h + 1 : currentNode.h - 1
}
}
return coords;
}
function incrementCoords(coords, orientation, linksCount) {
if(linksCount === 2) {
if(orientation === 'HORIZONTAL') {
coords.hh++;
coords.hh++;
} else if(orientation === 'VERTICAL') {
coords.ww++;
coords.ww++;
}
} else {
if(orientation === 'HORIZONTAL') {
coords.hh++;
} else if(orientation === 'VERTICAL') {
coords.ww++;
}
}
return coords;
}
function calc(_startNode, _data, orientation) {
_startNode = { ..._startNode, visited: true, w: 0, h: 0 };
_data = {
..._data,
[_startNode.id]: {
..._startNode
}
}
// linksFrom
let linksFromExplore = [_startNode];
while (linksFromExplore.length > 0) {
let currentNode = linksFromExplore.shift();
let linksCount = currentNode.linksFrom.length;
if (linksCount > 0) {
let coords = initializeCoords(currentNode, orientation, linksCount, 1);
currentNode.linksFrom.forEach(linkedNode => {
_data = {
..._data,
[linkedNode]: { ..._data[linkedNode], visited: true, w: coords.ww, h: coords.hh }
};
coords = incrementCoords(coords, orientation, linksCount);
// if (!_data[linkedNode].visited) {
linksFromExplore.push(_data[linkedNode]);
// }
});
}
// linksTo
let linksToExplore = [_startNode];
while (linksToExplore.length > 0) {
let currentNode = linksToExplore.shift();
let linksCount = currentNode.linksTo.length;
if (linksCount > 0) {
let coords = initializeCoords(currentNode, orientation, linksCount, -1);
currentNode.linksTo.forEach(linkedNode => {
_data = {
..._data,
[linkedNode]: { ..._data[linkedNode], visited: true, w: coords.ww, h: coords.hh }
};
coords = incrementCoords(coords, orientation, linksCount);
// if (!_data[linkedNode].visited) {
linksToExplore.push(_data[linkedNode]);
// }
});
}
}
}
return _data;
}
export const compute = (_startNodeId, _nodes, _links, orientation) => {
let mappedData = mapped({nodes: _nodes, links: _links});
let startNode = mappedData[_startNodeId];
let computed = calc(startNode, mappedData, orientation);
let result = [];
Object.keys(computed).forEach(k => {
result.push(computed[k]);
});
result = result.map(n => {
let result = {
...n,
x: n.w ? n.w * 400 : 0, // TODO: calculate these coordinates so that
y: n.h ? n.h * 400 : 0, // all nodes are centered
};
return result;
})
return result;
};