注意:以下翻译的准确性尚未经过验证。这是使用 AIP ↗ 从原始英文文本进行的机器翻译。
如何从树状结构中的根Object获取完整树以显示在Vertex中?
此代码从树状Object结构的根Object执行广度优先搜索。然后返回一个IGraphSearchAroundResultV1
以在Vertex中显示此树。
Copied!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
import { Function, Integer, OntologyObject } from "@foundry/functions-api" import { ExampleObjectType } from "@foundry/ontology-api"; export interface IGraphSearchAroundResultV1 { directEdges?: IGraphSearchAroundResultDirectEdgeV1[]; // 直接边 intermediateEdges?: IGraphSearchAroundResultIntermediateEdgeV1[]; // 中间边 orphanObjectRids?: string[]; // 孤立对象ID objectRidsToGroup?: string[][]; // 对象ID分组 } export interface IGraphSearchAroundResultDirectEdgeV1 { sourceObjectRid: string; // 源对象ID targetObjectRid: string; // 目标对象ID linkTypeRid?: string; // 链接类型ID } export interface IGraphSearchAroundResultIntermediateEdgeV1 { sourceObjectRid: string; // 源对象ID sourceToIntermediateLinkTypeRid?: string; // 源到中间对象的链接类型ID intermediateObjectRid: string; // 中间对象ID intermediateToTargetLinkTypeRid?: string; // 中间对象到目标对象的链接类型ID targetObjectRid: string; // 目标对象ID } class Queue<T> { private items: T[]; // 队列元素 constructor() { this.items = []; } enqueue(item: T): void { this.items.push(item); // 入队 } dequeue(): T | undefined { return this.items.shift(); // 出队 } isEmpty(): boolean { return this.items.length === 0; // 判断队列是否为空 } } export class VertexSearchArounds { @Function() public async fullBomTree(exampleObject: ExampleObjectType): Promise<IGraphSearchAroundResultV1> { var directEdges: IGraphSearchAroundResultDirectEdgeV1[] = []; // 存储直接边的数组 if (exampleObject.parent.all().length == 0) { // 如果对象没有父节点 const visited: ExampleObjectType[] = []; // 访问过的节点 const queue: Queue<ExampleObjectType> = new Queue<ExampleObjectType>(); // 队列用于广度优先搜索 visited.push(exampleObject); // 将初始对象标记为已访问 queue.enqueue(exampleObject); // 初始对象入队 while (!queue.isEmpty()) { // 当队列不为空时 const currentNode = queue.dequeue(); // 出队当前节点 if (currentNode !== undefined) { const children = exampleObject.child.all(); // 获取当前节点的子节点 if (children) { for (const child of children) { // 遍历每个子节点 if (!visited.includes(child)) { // 如果子节点未被访问过 visited.push(child); // 标记为已访问 queue.enqueue(child); // 子节点入队 directEdges.push({ sourceObjectRid: currentNode.rid!, // 当前节点ID targetObjectRid: child.rid! // 子节点ID }) } } } } } } const result: IGraphSearchAroundResultV1 = { directEdges // 将找到的直接边加入结果 }; return result; // 返回结果 } }
graph
, typescript
, searcharound
, breadth-first search
, vertex
如何获取像树或图一样结构化的Object的子节点和孙节点?
此代码定义了一个函数,该函数接受一个输入节点列表,并通过搜索父节点周围,返回一个包含输入节点的子节点和孙节点的对象集。
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14
@Function() // 获取一个给定输入节点的子节点和孙子节点。 private async getGrandChildrenAndChildrenViaList(item: ItemStructure[]): Promise<ObjectSet<ItemStructure>> { if(item.length === 0){ // 返回空集合 return Objects.search().itemStructure([]); } else { // 从输入对象列表定义一个对象集合 var itemSet: ObjectSet<ItemStructure>; itemSet = Objects.search().itemStructure(item); // 搜索这些对象周围的节点 return itemSet.searchAroundItemStructureParent().union(itemSet.searchAroundItemStructureParent().searchAroundItemStructureParent()); } }
这段代码的功能是获取一个给定输入节点的子节点和孙子节点。如果输入的节点列表为空,则返回一个空集合。否则,将输入的对象列表转换为一个对象集合,并搜索这些对象的父节点,最后返回子节点和孙子节点的并集。
函数在Objects上
, foo
, typescript
, searcharound