vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

759 lines
26 KiB

9 years ago
9 years ago
  1. let util = require("../../util");
  2. import NetworkUtil from '../NetworkUtil';
  3. import Cluster from './components/nodes/Cluster'
  4. class ClusterEngine {
  5. constructor(body) {
  6. this.body = body;
  7. this.clusteredNodes = {};
  8. this.clusteredEdges = {};
  9. this.options = {};
  10. this.defaultOptions = {};
  11. util.extend(this.options, this.defaultOptions);
  12. this.body.emitter.on('_resetData', () => {this.clusteredNodes = {}; this.clusteredEdges = {};})
  13. }
  14. setOptions(options) {
  15. if (options !== undefined) {
  16. }
  17. }
  18. /**
  19. *
  20. * @param hubsize
  21. * @param options
  22. */
  23. clusterByHubsize(hubsize, options) {
  24. if (hubsize === undefined) {
  25. hubsize = this._getHubSize();
  26. }
  27. else if (typeof(hubsize) === "object") {
  28. options = this._checkOptions(hubsize);
  29. hubsize = this._getHubSize();
  30. }
  31. let nodesToCluster = [];
  32. for (let i = 0; i < this.body.nodeIndices.length; i++) {
  33. let node = this.body.nodes[this.body.nodeIndices[i]];
  34. if (node.edges.length >= hubsize) {
  35. nodesToCluster.push(node.id);
  36. }
  37. }
  38. for (let i = 0; i < nodesToCluster.length; i++) {
  39. this.clusterByConnection(nodesToCluster[i],options,true);
  40. }
  41. this.body.emitter.emit('_dataChanged');
  42. }
  43. /**
  44. * loop over all nodes, check if they adhere to the condition and cluster if needed.
  45. * @param options
  46. * @param refreshData
  47. */
  48. cluster(options = {}, refreshData = true) {
  49. if (options.joinCondition === undefined) {throw new Error("Cannot call clusterByNodeData without a joinCondition function in the options.");}
  50. // check if the options object is fine, append if needed
  51. options = this._checkOptions(options);
  52. let childNodesObj = {};
  53. let childEdgesObj = {};
  54. // collect the nodes that will be in the cluster
  55. for (let i = 0; i < this.body.nodeIndices.length; i++) {
  56. let nodeId = this.body.nodeIndices[i];
  57. let node = this.body.nodes[nodeId];
  58. let clonedOptions = NetworkUtil.cloneOptions(node);
  59. if (options.joinCondition(clonedOptions) === true) {
  60. childNodesObj[nodeId] = this.body.nodes[nodeId];
  61. // collect the nodes that will be in the cluster
  62. for (let i = 0; i < node.edges.length; i++) {
  63. let edge = node.edges[i];
  64. if (this.clusteredEdges[edge.id] === undefined) {
  65. childEdgesObj[edge.id] = edge;
  66. }
  67. }
  68. }
  69. }
  70. this._cluster(childNodesObj, childEdgesObj, options, refreshData);
  71. }
  72. /**
  73. * Cluster all nodes in the network that have only X edges
  74. * @param edgeCount
  75. * @param options
  76. * @param refreshData
  77. */
  78. clusterByEdgeCount(edgeCount, options, refreshData = true) {
  79. options = this._checkOptions(options);
  80. let clusters = [];
  81. let usedNodes = {};
  82. let edge, edges, node, nodeId, relevantEdgeCount;
  83. // collect the nodes that will be in the cluster
  84. for (let i = 0; i < this.body.nodeIndices.length; i++) {
  85. let childNodesObj = {};
  86. let childEdgesObj = {};
  87. nodeId = this.body.nodeIndices[i];
  88. // if this node is already used in another cluster this session, we do not have to re-evaluate it.
  89. if (usedNodes[nodeId] === undefined) {
  90. relevantEdgeCount = 0;
  91. node = this.body.nodes[nodeId];
  92. edges = [];
  93. for (let j = 0; j < node.edges.length; j++) {
  94. edge = node.edges[j];
  95. if (this.clusteredEdges[edge.id] === undefined) {
  96. if (edge.toId !== edge.fromId) {
  97. relevantEdgeCount++;
  98. }
  99. edges.push(edge);
  100. }
  101. }
  102. // this node qualifies, we collect its neighbours to start the clustering process.
  103. if (relevantEdgeCount === edgeCount) {
  104. let gatheringSuccessful = true;
  105. for (let j = 0; j < edges.length; j++) {
  106. edge = edges[j];
  107. let childNodeId = this._getConnectedId(edge, nodeId);
  108. // add the nodes to the list by the join condition.
  109. if (options.joinCondition === undefined) {
  110. childEdgesObj[edge.id] = edge;
  111. childNodesObj[nodeId] = this.body.nodes[nodeId];
  112. childNodesObj[childNodeId] = this.body.nodes[childNodeId];
  113. usedNodes[nodeId] = true;
  114. }
  115. else {
  116. let clonedOptions = NetworkUtil.cloneOptions(this.body.nodes[nodeId]);
  117. if (options.joinCondition(clonedOptions) === true) {
  118. childEdgesObj[edge.id] = edge;
  119. childNodesObj[nodeId] = this.body.nodes[nodeId];
  120. usedNodes[nodeId] = true;
  121. }
  122. else {
  123. // this node does not qualify after all.
  124. gatheringSuccessful = false;
  125. break;
  126. }
  127. }
  128. }
  129. // add to the cluster queue
  130. if (Object.keys(childNodesObj).length > 0 && Object.keys(childEdgesObj).length > 0 && gatheringSuccessful === true) {
  131. clusters.push({nodes: childNodesObj, edges: childEdgesObj})
  132. }
  133. }
  134. }
  135. }
  136. for (let i = 0; i < clusters.length; i++) {
  137. this._cluster(clusters[i].nodes, clusters[i].edges, options, false)
  138. }
  139. if (refreshData === true) {
  140. this.body.emitter.emit('_dataChanged');
  141. }
  142. }
  143. /**
  144. * Cluster all nodes in the network that have only 1 edge
  145. * @param options
  146. * @param refreshData
  147. */
  148. clusterOutliers(options, refreshData = true) {
  149. this.clusterByEdgeCount(1,options,refreshData);
  150. }
  151. /**
  152. * Cluster all nodes in the network that have only 2 edge
  153. * @param options
  154. * @param refreshData
  155. */
  156. clusterBridges(options, refreshData = true) {
  157. this.clusterByEdgeCount(2,options,refreshData);
  158. }
  159. /**
  160. * suck all connected nodes of a node into the node.
  161. * @param nodeId
  162. * @param options
  163. * @param refreshData
  164. */
  165. clusterByConnection(nodeId, options, refreshData = true) {
  166. // kill conditions
  167. if (nodeId === undefined) {throw new Error("No nodeId supplied to clusterByConnection!");}
  168. if (this.body.nodes[nodeId] === undefined) {throw new Error("The nodeId given to clusterByConnection does not exist!");}
  169. let node = this.body.nodes[nodeId];
  170. options = this._checkOptions(options, node);
  171. if (options.clusterNodeProperties.x === undefined) {options.clusterNodeProperties.x = node.x;}
  172. if (options.clusterNodeProperties.y === undefined) {options.clusterNodeProperties.y = node.y;}
  173. if (options.clusterNodeProperties.fixed === undefined) {
  174. options.clusterNodeProperties.fixed = {};
  175. options.clusterNodeProperties.fixed.x = node.options.fixed.x;
  176. options.clusterNodeProperties.fixed.y = node.options.fixed.y;
  177. }
  178. let childNodesObj = {};
  179. let childEdgesObj = {};
  180. let parentNodeId = node.id;
  181. let parentClonedOptions = NetworkUtil.cloneOptions(node);
  182. childNodesObj[parentNodeId] = node;
  183. // collect the nodes that will be in the cluster
  184. for (let i = 0; i < node.edges.length; i++) {
  185. let edge = node.edges[i];
  186. if (this.clusteredEdges[edge.id] === undefined) {
  187. let childNodeId = this._getConnectedId(edge, parentNodeId);
  188. // if the child node is not in a cluster
  189. if (this.clusteredNodes[childNodeId] === undefined) {
  190. if (childNodeId !== parentNodeId) {
  191. if (options.joinCondition === undefined) {
  192. childEdgesObj[edge.id] = edge;
  193. childNodesObj[childNodeId] = this.body.nodes[childNodeId];
  194. }
  195. else {
  196. // clone the options and insert some additional parameters that could be interesting.
  197. let childClonedOptions = NetworkUtil.cloneOptions(this.body.nodes[childNodeId]);
  198. if (options.joinCondition(parentClonedOptions, childClonedOptions) === true) {
  199. childEdgesObj[edge.id] = edge;
  200. childNodesObj[childNodeId] = this.body.nodes[childNodeId];
  201. }
  202. }
  203. }
  204. else {
  205. // swallow the edge if it is self-referencing.
  206. childEdgesObj[edge.id] = edge;
  207. }
  208. }
  209. }
  210. }
  211. this._cluster(childNodesObj, childEdgesObj, options, refreshData);
  212. }
  213. /**
  214. * This function creates the edges that will be attached to the cluster
  215. * It looks for edges that are connected to the nodes from the "outside' of the cluster.
  216. *
  217. * @param childNodesObj
  218. * @param childEdgesObj
  219. * @param clusterNodeProperties
  220. * @param clusterEdgeProperties
  221. * @private
  222. */
  223. _createClusterEdges (childNodesObj, childEdgesObj, clusterNodeProperties, clusterEdgeProperties) {
  224. let edge, childNodeId, childNode, toId, fromId, otherNodeId;
  225. // loop over all child nodes and their edges to find edges going out of the cluster
  226. // these edges will be replaced by clusterEdges.
  227. let childKeys = Object.keys(childNodesObj);
  228. let createEdges = [];
  229. for (let i = 0; i < childKeys.length; i++) {
  230. childNodeId = childKeys[i];
  231. childNode = childNodesObj[childNodeId];
  232. // construct new edges from the cluster to others
  233. for (let j = 0; j < childNode.edges.length; j++) {
  234. edge = childNode.edges[j];
  235. // we only handle edges that are visible to the system, not the disabled ones from the clustering process.
  236. if (this.clusteredEdges[edge.id] === undefined) {
  237. // self-referencing edges will be added to the "hidden" list
  238. if (edge.toId == edge.fromId) {
  239. childEdgesObj[edge.id] = edge;
  240. }
  241. else {
  242. // set up the from and to.
  243. if (edge.toId == childNodeId) { // this is a double equals because ints and strings can be interchanged here.
  244. toId = clusterNodeProperties.id;
  245. fromId = edge.fromId;
  246. otherNodeId = fromId;
  247. }
  248. else {
  249. toId = edge.toId;
  250. fromId = clusterNodeProperties.id;
  251. otherNodeId = toId;
  252. }
  253. }
  254. // Only edges from the cluster outwards are being replaced.
  255. if (childNodesObj[otherNodeId] === undefined) {
  256. createEdges.push({edge: edge, fromId: fromId, toId: toId});
  257. }
  258. }
  259. }
  260. }
  261. // here we actually create the replacement edges. We could not do this in the loop above as the creation process
  262. // would add an edge to the edges array we are iterating over.
  263. for (let j = 0; j < createEdges.length; j++) {
  264. let edge = createEdges[j].edge;
  265. // copy the options of the edge we will replace
  266. let clonedOptions = NetworkUtil.cloneOptions(edge, 'edge');
  267. // make sure the properties of clusterEdges are superimposed on it
  268. util.deepExtend(clonedOptions, clusterEdgeProperties);
  269. // set up the edge
  270. clonedOptions.from = createEdges[j].fromId;
  271. clonedOptions.to = createEdges[j].toId;
  272. clonedOptions.id = 'clusterEdge:' + util.randomUUID();
  273. //clonedOptions.id = '(cf: ' + createEdges[j].fromId + " to: " + createEdges[j].toId + ")" + Math.random();
  274. // create the edge and give a reference to the one it replaced.
  275. let newEdge = this.body.functions.createEdge(clonedOptions);
  276. newEdge.clusteringEdgeReplacingId = edge.id;
  277. // connect the edge.
  278. this.body.edges[newEdge.id] = newEdge;
  279. newEdge.connect();
  280. // hide the replaced edge
  281. this._backupEdgeOptions(edge);
  282. edge.setOptions({physics:false, hidden:true});
  283. }
  284. }
  285. /**
  286. * This function checks the options that can be supplied to the different cluster functions
  287. * for certain fields and inserts defaults if needed
  288. * @param options
  289. * @returns {*}
  290. * @private
  291. */
  292. _checkOptions(options = {}) {
  293. if (options.clusterEdgeProperties === undefined) {options.clusterEdgeProperties = {};}
  294. if (options.clusterNodeProperties === undefined) {options.clusterNodeProperties = {};}
  295. return options;
  296. }
  297. /**
  298. *
  299. * @param {Object} childNodesObj | object with node objects, id as keys, same as childNodes except it also contains a source node
  300. * @param {Object} childEdgesObj | object with edge objects, id as keys
  301. * @param {Array} options | object with {clusterNodeProperties, clusterEdgeProperties, processProperties}
  302. * @param {Boolean} refreshData | when true, do not wrap up
  303. * @private
  304. */
  305. _cluster(childNodesObj, childEdgesObj, options, refreshData = true) {
  306. // kill condition: no children so can't cluster or only one node in the cluster, don't bother
  307. if (Object.keys(childNodesObj).length < 2) {return;}
  308. // check if this cluster call is not trying to cluster anything that is in another cluster.
  309. for (let nodeId in childNodesObj) {
  310. if (childNodesObj.hasOwnProperty(nodeId)) {
  311. if (this.clusteredNodes[nodeId] !== undefined) {
  312. return;
  313. }
  314. }
  315. }
  316. let clusterNodeProperties = util.deepExtend({},options.clusterNodeProperties);
  317. // construct the clusterNodeProperties
  318. if (options.processProperties !== undefined) {
  319. // get the childNode options
  320. let childNodesOptions = [];
  321. for (let nodeId in childNodesObj) {
  322. if (childNodesObj.hasOwnProperty(nodeId)) {
  323. let clonedOptions = NetworkUtil.cloneOptions(childNodesObj[nodeId]);
  324. childNodesOptions.push(clonedOptions);
  325. }
  326. }
  327. // get cluster properties based on childNodes
  328. let childEdgesOptions = [];
  329. for (let edgeId in childEdgesObj) {
  330. if (childEdgesObj.hasOwnProperty(edgeId)) {
  331. // these cluster edges will be removed on creation of the cluster.
  332. if (edgeId.substr(0, 12) !== "clusterEdge:") {
  333. let clonedOptions = NetworkUtil.cloneOptions(childEdgesObj[edgeId], 'edge');
  334. childEdgesOptions.push(clonedOptions);
  335. }
  336. }
  337. }
  338. clusterNodeProperties = options.processProperties(clusterNodeProperties, childNodesOptions, childEdgesOptions);
  339. if (!clusterNodeProperties) {
  340. throw new Error("The processProperties function does not return properties!");
  341. }
  342. }
  343. // check if we have an unique id;
  344. if (clusterNodeProperties.id === undefined) {clusterNodeProperties.id = 'cluster:' + util.randomUUID();}
  345. let clusterId = clusterNodeProperties.id;
  346. if (clusterNodeProperties.label === undefined) {
  347. clusterNodeProperties.label = 'cluster';
  348. }
  349. // give the clusterNode a position if it does not have one.
  350. let pos = undefined;
  351. if (clusterNodeProperties.x === undefined) {
  352. pos = this._getClusterPosition(childNodesObj);
  353. clusterNodeProperties.x = pos.x;
  354. }
  355. if (clusterNodeProperties.y === undefined) {
  356. if (pos === undefined) {pos = this._getClusterPosition(childNodesObj);}
  357. clusterNodeProperties.y = pos.y;
  358. }
  359. // force the ID to remain the same
  360. clusterNodeProperties.id = clusterId;
  361. // create the clusterNode
  362. let clusterNode = this.body.functions.createNode(clusterNodeProperties, Cluster);
  363. clusterNode.isCluster = true;
  364. clusterNode.containedNodes = childNodesObj;
  365. clusterNode.containedEdges = childEdgesObj;
  366. // cache a copy from the cluster edge properties if we have to reconnect others later on
  367. clusterNode.clusterEdgeProperties = options.clusterEdgeProperties;
  368. // finally put the cluster node into global
  369. this.body.nodes[clusterNodeProperties.id] = clusterNode;
  370. // create the new edges that will connect to the cluster, all self-referencing edges will be added to childEdgesObject here.
  371. this._createClusterEdges(childNodesObj, childEdgesObj, clusterNodeProperties, options.clusterEdgeProperties);
  372. // disable the childEdges
  373. for (let edgeId in childEdgesObj) {
  374. if (childEdgesObj.hasOwnProperty(edgeId)) {
  375. if (this.body.edges[edgeId] !== undefined) {
  376. let edge = this.body.edges[edgeId];
  377. // cache the options before changing
  378. this._backupEdgeOptions(edge);
  379. // disable physics and hide the edge
  380. edge.setOptions({physics:false, hidden:true});
  381. }
  382. }
  383. }
  384. // disable the childNodes
  385. for (let nodeId in childNodesObj) {
  386. if (childNodesObj.hasOwnProperty(nodeId)) {
  387. this.clusteredNodes[nodeId] = {clusterId:clusterNodeProperties.id, node: this.body.nodes[nodeId]};
  388. this.body.nodes[nodeId].setOptions({hidden:true, physics:false});
  389. }
  390. }
  391. // set ID to undefined so no duplicates arise
  392. clusterNodeProperties.id = undefined;
  393. // wrap up
  394. if (refreshData === true) {
  395. this.body.emitter.emit('_dataChanged');
  396. }
  397. }
  398. _backupEdgeOptions(edge) {
  399. if (this.clusteredEdges[edge.id] === undefined) {
  400. this.clusteredEdges[edge.id] = {physics: edge.options.physics, hidden: edge.options.hidden};
  401. }
  402. }
  403. _restoreEdge(edge) {
  404. let originalOptions = this.clusteredEdges[edge.id];
  405. if (originalOptions !== undefined) {
  406. edge.setOptions({physics: originalOptions.physics, hidden: originalOptions.hidden});
  407. delete this.clusteredEdges[edge.id];
  408. }
  409. }
  410. /**
  411. * Check if a node is a cluster.
  412. * @param nodeId
  413. * @returns {*}
  414. */
  415. isCluster(nodeId) {
  416. if (this.body.nodes[nodeId] !== undefined) {
  417. return this.body.nodes[nodeId].isCluster === true;
  418. }
  419. else {
  420. console.log("Node does not exist.");
  421. return false;
  422. }
  423. }
  424. /**
  425. * get the position of the cluster node based on what's inside
  426. * @param {object} childNodesObj | object with node objects, id as keys
  427. * @returns {{x: number, y: number}}
  428. * @private
  429. */
  430. _getClusterPosition(childNodesObj) {
  431. let childKeys = Object.keys(childNodesObj);
  432. let minX = childNodesObj[childKeys[0]].x;
  433. let maxX = childNodesObj[childKeys[0]].x;
  434. let minY = childNodesObj[childKeys[0]].y;
  435. let maxY = childNodesObj[childKeys[0]].y;
  436. let node;
  437. for (let i = 1; i < childKeys.length; i++) {
  438. node = childNodesObj[childKeys[i]];
  439. minX = node.x < minX ? node.x : minX;
  440. maxX = node.x > maxX ? node.x : maxX;
  441. minY = node.y < minY ? node.y : minY;
  442. maxY = node.y > maxY ? node.y : maxY;
  443. }
  444. return {x: 0.5*(minX + maxX), y: 0.5*(minY + maxY)};
  445. }
  446. /**
  447. * Open a cluster by calling this function.
  448. * @param {String} clusterNodeId | the ID of the cluster node
  449. * @param {Boolean} refreshData | wrap up afterwards if not true
  450. */
  451. openCluster(clusterNodeId, options, refreshData = true) {
  452. // kill conditions
  453. if (clusterNodeId === undefined) {throw new Error("No clusterNodeId supplied to openCluster.");}
  454. if (this.body.nodes[clusterNodeId] === undefined) {throw new Error("The clusterNodeId supplied to openCluster does not exist.");}
  455. if (this.body.nodes[clusterNodeId].containedNodes === undefined) {
  456. console.log("The node:" + clusterNodeId + " is not a cluster.");
  457. return
  458. }
  459. let clusterNode = this.body.nodes[clusterNodeId];
  460. let containedNodes = clusterNode.containedNodes;
  461. let containedEdges = clusterNode.containedEdges;
  462. // allow the user to position the nodes after release.
  463. if (options !== undefined && options.releaseFunction !== undefined && typeof options.releaseFunction === 'function') {
  464. let positions = {};
  465. let clusterPosition = {x:clusterNode.x, y:clusterNode.y};
  466. for (let nodeId in containedNodes) {
  467. if (containedNodes.hasOwnProperty(nodeId)) {
  468. let containedNode = this.body.nodes[nodeId];
  469. positions[nodeId] = {x: containedNode.x, y: containedNode.y};
  470. }
  471. }
  472. let newPositions = options.releaseFunction(clusterPosition, positions);
  473. for (let nodeId in containedNodes) {
  474. if (containedNodes.hasOwnProperty(nodeId)) {
  475. let containedNode = this.body.nodes[nodeId];
  476. if (newPositions[nodeId] !== undefined) {
  477. containedNode.x = (newPositions[nodeId].x === undefined ? clusterNode.x : newPositions[nodeId].x);
  478. containedNode.y = (newPositions[nodeId].y === undefined ? clusterNode.y : newPositions[nodeId].y);
  479. }
  480. }
  481. }
  482. }
  483. else {
  484. // copy the position from the cluster
  485. for (let nodeId in containedNodes) {
  486. if (containedNodes.hasOwnProperty(nodeId)) {
  487. let containedNode = this.body.nodes[nodeId];
  488. containedNode = containedNodes[nodeId];
  489. // inherit position
  490. if (containedNode.options.fixed.x === false) {containedNode.x = clusterNode.x;}
  491. if (containedNode.options.fixed.y === false) {containedNode.y = clusterNode.y;}
  492. }
  493. }
  494. }
  495. // release nodes
  496. for (let nodeId in containedNodes) {
  497. if (containedNodes.hasOwnProperty(nodeId)) {
  498. let containedNode = this.body.nodes[nodeId];
  499. // inherit speed
  500. containedNode.vx = clusterNode.vx;
  501. containedNode.vy = clusterNode.vy;
  502. // we use these methods to avoid re-instantiating the shape, which happens with setOptions.
  503. containedNode.setOptions({hidden:false, physics:true});
  504. delete this.clusteredNodes[nodeId];
  505. }
  506. }
  507. // copy the clusterNode edges because we cannot iterate over an object that we add or remove from.
  508. let edgesToBeDeleted = [];
  509. for (let i = 0; i < clusterNode.edges.length; i++) {
  510. edgesToBeDeleted.push(clusterNode.edges[i]);
  511. }
  512. // actually handling the deleting.
  513. for (let i = 0; i < edgesToBeDeleted.length; i++) {
  514. let edge = edgesToBeDeleted[i];
  515. let otherNodeId = this._getConnectedId(edge, clusterNodeId);
  516. // if the other node is in another cluster, we transfer ownership of this edge to the other cluster
  517. if (this.clusteredNodes[otherNodeId] !== undefined) {
  518. // transfer ownership:
  519. let otherCluster = this.body.nodes[this.clusteredNodes[otherNodeId].clusterId];
  520. let transferEdge = this.body.edges[edge.clusteringEdgeReplacingId];
  521. if (transferEdge !== undefined) {
  522. otherCluster.containedEdges[transferEdge.id] = transferEdge;
  523. // delete local reference
  524. delete containedEdges[transferEdge.id];
  525. // create new cluster edge from the otherCluster:
  526. // get to and from
  527. let fromId = transferEdge.fromId;
  528. let toId = transferEdge.toId;
  529. if (transferEdge.toId == otherNodeId) {
  530. toId = this.clusteredNodes[otherNodeId].clusterId;
  531. }
  532. else {
  533. fromId = this.clusteredNodes[otherNodeId].clusterId;
  534. }
  535. // clone the options and apply the cluster options to them
  536. let clonedOptions = NetworkUtil.cloneOptions(transferEdge, 'edge');
  537. util.deepExtend(clonedOptions, otherCluster.clusterEdgeProperties);
  538. // apply the edge specific options to it.
  539. let id = 'clusterEdge:' + util.randomUUID();
  540. util.deepExtend(clonedOptions, {from: fromId, to: toId, hidden: false, physics: true, id: id});
  541. // create it
  542. let newEdge = this.body.functions.createEdge(clonedOptions);
  543. newEdge.clusteringEdgeReplacingId = transferEdge.id;
  544. this.body.edges[id] = newEdge;
  545. this.body.edges[id].connect();
  546. }
  547. }
  548. else {
  549. let replacedEdge = this.body.edges[edge.clusteringEdgeReplacingId];
  550. if (replacedEdge !== undefined) {
  551. this._restoreEdge(replacedEdge);
  552. }
  553. }
  554. edge.cleanup();
  555. // this removes the edge from node.edges, which is why edgeIds is formed
  556. edge.disconnect();
  557. delete this.body.edges[edge.id];
  558. }
  559. // handle the releasing of the edges
  560. for (let edgeId in containedEdges) {
  561. if (containedEdges.hasOwnProperty(edgeId)) {
  562. this._restoreEdge(containedEdges[edgeId]);
  563. }
  564. }
  565. // remove clusterNode
  566. delete this.body.nodes[clusterNodeId];
  567. if (refreshData === true) {
  568. this.body.emitter.emit('_dataChanged');
  569. }
  570. }
  571. getNodesInCluster(clusterId) {
  572. let nodesArray = [];
  573. if (this.isCluster(clusterId) === true) {
  574. let containedNodes = this.body.nodes[clusterId].containedNodes;
  575. for (let nodeId in containedNodes) {
  576. if (containedNodes.hasOwnProperty(nodeId)) {
  577. nodesArray.push(this.body.nodes[nodeId].id)
  578. }
  579. }
  580. }
  581. return nodesArray;
  582. }
  583. /**
  584. * Get the stack clusterId's that a certain node resides in. cluster A -> cluster B -> cluster C -> node
  585. * @param nodeId
  586. * @returns {Array}
  587. */
  588. findNode(nodeId) {
  589. let stack = [];
  590. let max = 100;
  591. let counter = 0;
  592. while (this.clusteredNodes[nodeId] !== undefined && counter < max) {
  593. stack.push(this.body.nodes[nodeId].id);
  594. nodeId = this.clusteredNodes[nodeId].clusterId;
  595. counter++;
  596. }
  597. stack.push(this.body.nodes[nodeId].id);
  598. stack.reverse();
  599. return stack;
  600. }
  601. /**
  602. * Get the Id the node is connected to
  603. * @param edge
  604. * @param nodeId
  605. * @returns {*}
  606. * @private
  607. */
  608. _getConnectedId(edge, nodeId) {
  609. if (edge.toId != nodeId) {
  610. return edge.toId;
  611. }
  612. else if (edge.fromId != nodeId) {
  613. return edge.fromId;
  614. }
  615. else {
  616. return edge.fromId;
  617. }
  618. }
  619. /**
  620. * We determine how many connections denote an important hub.
  621. * We take the mean + 2*std as the important hub size. (Assuming a normal distribution of data, ~2.2%)
  622. *
  623. * @private
  624. */
  625. _getHubSize() {
  626. let average = 0;
  627. let averageSquared = 0;
  628. let hubCounter = 0;
  629. let largestHub = 0;
  630. for (let i = 0; i < this.body.nodeIndices.length; i++) {
  631. let node = this.body.nodes[this.body.nodeIndices[i]];
  632. if (node.edges.length > largestHub) {
  633. largestHub = node.edges.length;
  634. }
  635. average += node.edges.length;
  636. averageSquared += Math.pow(node.edges.length,2);
  637. hubCounter += 1;
  638. }
  639. average = average / hubCounter;
  640. averageSquared = averageSquared / hubCounter;
  641. let variance = averageSquared - Math.pow(average,2);
  642. let standardDeviation = Math.sqrt(variance);
  643. let hubThreshold = Math.floor(average + 2*standardDeviation);
  644. // always have at least one to cluster
  645. if (hubThreshold > largestHub) {
  646. hubThreshold = largestHub;
  647. }
  648. return hubThreshold;
  649. };
  650. }
  651. export default ClusterEngine;