Derive properties using 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.

For example:

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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; } }