注意:以下翻译的准确性尚未经过验证。这是使用 AIP ↗ 从原始英文文本进行的机器翻译。
函数 被用于在对象上创建派生属性。这些属性可以显示在对象视图中,显示在扩展节点标签中,并用于在图层样式选项中为节点着色。
任何符合以下条件的函数都可用作派生属性:
public
。@Functions()
装饰器。FunctionMap
中的所有键都是对象。FunctionsMap
中的所有值都是原始类型或具有原始类型的自定义类型。ExampleDataRoute[]
),而不是单个对象(例如 ExampleDataRoute
)。这确保了函数不会针对图中每个对象单独调用,从而避免在大型图中导致非常慢的性能。例如:
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
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>(); // 使用 Promise.all 并行获取所有航线的航班信息 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; // 将结果存入 FunctionsMap routeMap.set(route, cancellationPercentage); } return routeMap; } }
此代码定义了一个类 VertexDerivedPropertyFunctions
,其中包含一个异步函数 flightCancellationPercentage
,用于计算每个给定航线的航班取消百分比。函数使用 FunctionsMap
来存储每个 ExampleDataRoute
对应的取消百分比。通过 Promise.all
并行获取航线的所有航班信息,提高了效率。