注: 以下の翻訳の正確性は検証されていません。AIPを利用して英語版の原文から機械的に翻訳されたものです。

Functions を使用してプロパティを導出する

Functions を使用して、オブジェクトに派生プロパティを作成できます。派生プロパティは、Object View や拡張ノードラベルに表示したり、レイヤーのスタイル設定オプションでノードの色分けに使用したりできます。

次の条件を満たす関数は、派生プロパティとして使用できます。

  • メソッドが関数としてエクスポートされていること(TypeScript v1 では @Function() デコレーター、TypeScript v2 では export default を使用)。
  • メソッドが public であること。
  • メソッドがオブジェクトから値へのマップを返すこと(TypeScript v1 では FunctionsMap<ObjectType, CustomType>、TypeScript v2 では Record<ObjectSpecifier<ObjectType>, CustomType>)。
  • マップ内のすべてのキーがオブジェクトであること。
  • マップ内のすべての値がプリミティブ、または各フィールドがプリミティブであるカスタム型であること。
  • メソッドにリリース用のタグが付けられていること。
  • メソッドが単一のオブジェクト(ExampleDataRoute など)ではなく、オブジェクトセット(たとえば ExampleDataRoute[] または ObjectSet<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 26 27 28 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; } }
Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import { ObjectSet, ObjectSpecifier } from "@osdk/client"; import { Double } from "@osdk/functions"; import { ExampleDataRoute } from "@ontology/sdk"; export default async function flightCancellationPercentage( routes: ObjectSet<ExampleDataRoute>, ): Promise<Record<ObjectSpecifier<ExampleDataRoute>, Double>> { const routeMap: Record<ObjectSpecifier<ExampleDataRoute>, Double> = {}; for await (const route of routes.asyncIter()) { const flights = await Array.fromAsync(route.$link.flights.asyncIter()); const cancelledFlights = flights.filter((flight) => flight.cancelled); const cancellationPercentage = (cancelledFlights.length / flights.length) * 100; routeMap[route.$objectSpecifier] = cancellationPercentage; } return routeMap; }