跳转至

Functions on Objects(对象函数)

Typescript

Get tree from root object for Vertex

How do I get the full tree from a root object in a tree like structure to display in Vertex?

This code performs a breadth first search from the root object of a tree-like object structure. It then returns an IGraphSearchAroundResultV1 to display this tree in Vertex.

import { Function, Integer, OntologyObject } from "@foundry/functions-api"
import { ExampleObjectType } from "@foundry/ontology-api";

export interface IGraphSearchAroundResultV1 {
    directEdges?: IGraphSearchAroundResultDirectEdgeV1[];
    intermediateEdges?: IGraphSearchAroundResultIntermediateEdgeV1[];
    orphanObjectRids?: string[];
    objectRidsToGroup?: string[][];
}

export interface IGraphSearchAroundResultDirectEdgeV1 {
    sourceObjectRid: string;
    targetObjectRid: string;
    linkTypeRid?: string;
}

export interface IGraphSearchAroundResultIntermediateEdgeV1 {
    sourceObjectRid: string;
    sourceToIntermediateLinkTypeRid?: string;
    intermediateObjectRid: string;
    intermediateToTargetLinkTypeRid?: string;
    targetObjectRid: string;
}

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!,
                                    targetObjectRid: child.rid!
                                })
                            }
                        }
                    }
                }
            }
        }

        const result: IGraphSearchAroundResultV1 = {
            directEdges
        };
        return result;
    }
}
  • Date submitted: 2024-04-30
  • Tags: graph, typescript, searcharound, breadth-first search, vertex

Search around for children and grandchildren

How do I get the children and grandchildren of an object structured like a tree or graph?

This code defines a function that takes a list of input nodes and returns an object set containing the children and grand children of the input nodes by searching around the parent nodes.

@Function()
// Get the children and grand children of one given input node.
private async getGrandChildrenAndChildrenViaList(item: ItemStructure[]): Promise<ObjectSet<ItemStructure>> {
    if(item.length === 0){
        // Return empty set
        return Objects.search().itemStructure([]);
    } else {
        // Defines an object set from the list of object as input
        var itemSet: ObjectSet<ItemStructure>;
        itemSet = Objects.search().itemStructure(item);
        // Search around those objects
        return itemSet.searchAroundItemStructureParent().union(itemSet.searchAroundItemStructureParent().searchAroundItemStructureParent());
    }
}
  • Date submitted: 2024-03-20
  • Tags: functions on objects, foo, typescript, searcharound

中文翻译

对象函数

TypeScript

从根对象获取树结构以在Vertex中展示

如何从树状结构中的根对象获取完整的树结构,以便在Vertex中展示?

以下代码对树状对象结构中的根对象执行广度优先搜索(breadth first search),然后返回一个IGraphSearchAroundResultV1,用于在Vertex中展示该树结构。

import { Function, Integer, OntologyObject } from "@foundry/functions-api"
import { ExampleObjectType } from "@foundry/ontology-api";

export interface IGraphSearchAroundResultV1 {
    directEdges?: IGraphSearchAroundResultDirectEdgeV1[];
    intermediateEdges?: IGraphSearchAroundResultIntermediateEdgeV1[];
    orphanObjectRids?: string[];
    objectRidsToGroup?: string[][];
}

export interface IGraphSearchAroundResultDirectEdgeV1 {
    sourceObjectRid: string;
    targetObjectRid: string;
    linkTypeRid?: string;
}

export interface IGraphSearchAroundResultIntermediateEdgeV1 {
    sourceObjectRid: string;
    sourceToIntermediateLinkTypeRid?: string;
    intermediateObjectRid: string;
    intermediateToTargetLinkTypeRid?: string;
    targetObjectRid: string;
}

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!,
                                    targetObjectRid: child.rid!
                                })
                            }
                        }
                    }
                }
            }
        }

        const result: IGraphSearchAroundResultV1 = {
            directEdges
        };
        return result;
    }
}
  • 提交日期:2024-04-30
  • 标签:graphtypescriptsearcharoundbreadth-first searchvertex

搜索子对象和孙对象

如何获取树状或图状结构中某个对象的子对象和孙对象?

以下代码定义了一个函数,该函数接收一个输入节点列表,并通过搜索父节点(parent nodes)返回包含输入节点的子对象和孙对象的对象集(object set)。

@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());
    }
}
  • 提交日期:2024-03-20
  • 标签:functions on objectsfootypescriptsearcharound