Clarity.Graph.Filter (Clarity v0.4.0)

View Source

Composable filter functions for graph vertices.

Filters are higher-order functions that take a graph for preparation and return a predicate function that can be called for each vertex.

Example Usage

# Single filter
subgraph = Graph.filter(graph, Filter.within_steps(vertex, 2, 1))

# Composed filters
filters = [
  Filter.within_steps(vertex, 2, 1),
  Filter.reachable_from(root_vertex),
  Filter.custom(fn v -> String.contains?(Vertex.name(v), "MyApp") end)
]
subgraph = Graph.filter(graph, filters)

Summary

Functions

Combines multiple filters using AND logic.

Combines multiple filters using OR logic.

Negates a filter using NOT logic.

Creates a filter that includes vertices reachable from any of the specified vertices.

Creates a filter that includes only vertices of the specified types.

Creates a filter that includes vertices within the specified number of steps from a center vertex.

Types

filter()

Functions

all(filters)

@spec all([filter()]) :: filter()

Combines multiple filters using AND logic.

A vertex must pass ALL filters to be included in the result.

Examples

Filter.all([
  Filter.within_steps(vertex, 2, 0),
  Filter.vertex_type([Module])
])

any(filters)

@spec any([filter()]) :: filter()

Combines multiple filters using OR logic.

A vertex must pass ANY of the filters to be included in the result.

Examples

Filter.any([
  Filter.vertex_type([Module]),
  Filter.vertex_type([Application])
])

negate(filter)

@spec negate(filter()) :: filter()

Negates a filter using NOT logic.

A vertex must NOT pass the filter to be included in the result.

Examples

# Everything except modules
Filter.negate(Filter.vertex_type([Module]))

reachable_from(source_vertices)

@spec reachable_from([Clarity.Vertex.t()]) :: filter()

Creates a filter that includes vertices reachable from any of the specified vertices.

vertex_type(filter_types)

@spec vertex_type([module()]) :: filter()

Creates a filter that includes only vertices of the specified types.

Takes a list of modules (struct types) and includes only vertices whose __struct__ field matches one of the specified types.

Examples

# Only application vertices
Filter.vertex_type([Clarity.Vertex.Application])

# Only modules and applications
Filter.vertex_type([Clarity.Vertex.Module, Clarity.Vertex.Application])

within_steps(center_vertex, max_outgoing_steps, max_incoming_steps)

@spec within_steps(Clarity.Vertex.t(), non_neg_integer(), non_neg_integer()) ::
  filter()

Creates a filter that includes vertices within the specified number of steps from a center vertex.

This filter includes vertices reachable within max_outgoing_steps forward hops OR max_incoming_steps backward hops from the center vertex.