getContext method

Set<String> getContext(
  1. String nodeId, {
  2. int depth = 2,
})

Get context around a node up to a certain depth.

Returns nodes within depth edges of nodeId in both directions. Useful for building AI context or understanding local graph structure.

Implementation

Set<String> getContext(String nodeId, {int depth = 2}) {
  final result = <String>{nodeId};
  var frontier = <String>{nodeId};

  for (var i = 0; i < depth && frontier.isNotEmpty; i++) {
    final nextFrontier = <String>{};
    for (final id in frontier) {
      final node = _nodes[id];
      if (node == null) continue;

      // Add neighbors in both directions
      for (final neighborId in [...node.imports, ...node.importedBy]) {
        if (result.add(neighborId)) {
          nextFrontier.add(neighborId);
        }
      }
    }
    frontier = nextFrontier;
  }

  return result;
}