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.
78 lines
3.8 KiB
78 lines
3.8 KiB
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getElementNthDegreeIds = getElementNthDegreeIds;
|
|
exports.getNodeNthDegreeIds = getNodeNthDegreeIds;
|
|
const id_1 = require("./id");
|
|
const traverse_1 = require("./traverse");
|
|
/**
|
|
* <zh/> 获取指定元素在 n 度关系内的所有元素的 ID
|
|
*
|
|
* <en/> Get the IDs of all elements within an n-degree relationship from the specified element
|
|
* @remarks
|
|
* <zh/> 对于节点,0 度关系是节点本身,1 度关系包括节点的直接相邻节点和边,以此类推。
|
|
* 对于边,0 度关系是边本身,1 度关系包括边本身及其两个端点,以此类推。
|
|
*
|
|
* <en/> For a node, 0-degree relationship includes the node itself; 1-degree relationship includes the node's direct neighbors and their connecting edges, etc.
|
|
* For an edge, 0-degree relationship includes the edge itself; 1-degree relationship includes the edge and its two endpoints, etc
|
|
* @param graph - <zh/> 图实例 | <en/> graph instance
|
|
* @param elementType - <zh/> 元素类型 | <en/> element type
|
|
* @param elementId - <zh/> 起始元素的 ID | <en/> start element ID
|
|
* @param degree - <zh/> 指定的度数 | <en/> the specified degree
|
|
* @param direction - <zh/> 边的方向 | <en/> edge direction
|
|
* @returns - <zh/> 返回节点和边的 ID 数组 | <en/> Returns an array of node and edge IDs
|
|
*/
|
|
function getElementNthDegreeIds(graph, elementType, elementId, degree, direction = 'both') {
|
|
if (elementType === 'combo' || elementType === 'node') {
|
|
return getNodeNthDegreeIds(graph, elementId, degree, direction);
|
|
}
|
|
const edgeData = graph.getEdgeData(elementId);
|
|
if (!edgeData)
|
|
return [];
|
|
const sourceRelations = getNodeNthDegreeIds(graph, edgeData.source, degree - 1, direction);
|
|
const targetRelations = getNodeNthDegreeIds(graph, edgeData.target, degree - 1, direction);
|
|
return Array.from(new Set([...sourceRelations, ...targetRelations, elementId]));
|
|
}
|
|
/**
|
|
* <zh/> 获取指定节点在 n 度关系内的所有元素的 ID
|
|
*
|
|
* <en/> Get all elements IDs within n-degree relationship of the specified node
|
|
* @remarks
|
|
* <zh/> 节点的 0 度关系是节点本身,1 度关系是节点的直接相邻节点和边,以此类推
|
|
* @param direction
|
|
* <en/> 0-degree relationship of a node is the node itself; 1-degree relationship is the node's neighboring nodes and related edges, etc
|
|
* @param graph - <zh/> 图实例 | <en/> graph instance
|
|
* @param startNodeId - <zh/> 起始节点的 ID | <en/> The ID of the starting node
|
|
* @param degree - <zh/> 指定的度数 | <en/> The specified degree
|
|
* @param direction - <zh/> 边的方向 | <en/> The direction of the edge
|
|
* @returns - <zh/> 返回节点和边的 ID 数组 | <en/> Returns an array of node and edge IDs
|
|
*/
|
|
function getNodeNthDegreeIds(graph, startNodeId, degree, direction = 'both') {
|
|
const visitedNodes = new Set();
|
|
const visitedEdges = new Set();
|
|
const relations = new Set();
|
|
(0, traverse_1.bfs)(startNodeId, (nodeId, depth) => {
|
|
if (depth > degree)
|
|
return;
|
|
relations.add(nodeId);
|
|
graph.getRelatedEdgesData(nodeId, direction).forEach((edge) => {
|
|
const edgeId = (0, id_1.idOf)(edge);
|
|
if (!visitedEdges.has(edgeId) && depth < degree) {
|
|
relations.add(edgeId);
|
|
visitedEdges.add(edgeId);
|
|
}
|
|
});
|
|
}, (nodeId) => {
|
|
return graph
|
|
.getRelatedEdgesData(nodeId, direction)
|
|
.map((edge) => (edge.source === nodeId ? edge.target : edge.source))
|
|
.filter((neighborNodeId) => {
|
|
if (!visitedNodes.has(neighborNodeId)) {
|
|
visitedNodes.add(neighborNodeId);
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
});
|
|
return Array.from(relations);
|
|
}
|
|
//# sourceMappingURL=relation.js.map
|