跳转至

Derive properties using Functions(使用 Functions 派生属性)

Functions can be used to create derived properties on objects. These can be displayed in the object view, shown in extended node labels and used to color nodes in the layer styling options.

Any function that meets the following criteria can be used as a derived property:

  • The method is public.
  • The method uses the @Functions() decorator.
  • The method returns a FunctionsMap.
  • All keys in the FunctionMap are objects.
  • All values in the FunctionsMap are primitives or a custom type with primitives for each field.
  • The method has been tagged for release.
  • The method operates on object sets (ExampleDataRoute[], for example) and not a single object (such as ExampleDataRoute). This ensures the function isn't called for every object on the graph individually, which can be cause very slow performance for large graphs.
  • The method has no other inputs besides the object set.

For example:

import { Function, FunctionsMap, Double } from "@foundry/functions-api";
import { ExampleDataRoute } from "@foundry/ontology-api";

export class VertexDerivedPropertyFunctions {
    @Function()
    public async flightCancellationPercentage(routes: ExampleDataRoute[]): Promise<FunctionsMap<ExampleDataRoute, Double>> {
        const routeMap = new FunctionsMap<ExampleDataRoute, Double>();

        const allFlights = await Promise.all(routes.map(route => route.flights.allAsync()));

        for (let i = 0; i < routes.length; i++) {
            const route = routes[i];
            const flights = allFlights[i];
            const cancelledFlights = flights.filter(flight => flight.cancelled);
            const cancellationPercentage = (cancelledFlights.length / flights.length) * 100;
            routeMap.set(route, cancellationPercentage);
        }

        return routeMap;
    }
}

中文翻译


使用 Functions 派生属性

Functions 可用于在对象上创建派生属性。这些属性可以显示在对象视图中、在扩展节点标签中展示,并用于在图层样式选项中为节点着色。

任何满足以下条件的函数都可以用作派生属性

  • 方法为 public
  • 方法使用了 @Functions() 装饰器。
  • 方法返回一个 FunctionsMap。
  • FunctionMap 中的所有键均为对象。
  • FunctionsMap 中的所有值均为原始类型或每个字段均为原始类型的自定义类型。
  • 方法已标记为发布状态。
  • 方法操作的是对象集(例如 ExampleDataRoute[]),而非单个对象(例如 ExampleDataRoute)。这确保函数不会为图中的每个对象单独调用,从而避免在大规模图中导致性能严重下降。
  • 方法除对象集外没有其他输入。

例如:

import { Function, FunctionsMap, Double } from "@foundry/functions-api";
import { ExampleDataRoute } from "@foundry/ontology-api";

export class VertexDerivedPropertyFunctions {
    @Function()
    public async flightCancellationPercentage(routes: ExampleDataRoute[]): Promise<FunctionsMap<ExampleDataRoute, Double>> {
        const routeMap = new FunctionsMap<ExampleDataRoute, Double>();

        const allFlights = await Promise.all(routes.map(route => route.flights.allAsync()));

        for (let i = 0; i < routes.length; i++) {
            const route = routes[i];
            const flights = allFlights[i];
            const cancelledFlights = flights.filter(flight => flight.cancelled);
            const cancellationPercentage = (cancelledFlights.length / flights.length) * 100;
            routeMap.set(route, cancellationPercentage);
        }

        return routeMap;
    }
}