Skip to content

path

relationalai.semantics.std

RelationalAI path library: pattern-based matching and enumeration of paths through a knowledge graph.

PyRel natively expresses fixed-length paths over concepts and relationships. This builtin standard library adds Model.path (and the standalone path), which builds a PathPattern — a reusable specification of an alternating sequence of nodes (concepts) and edges (relationships). Patterns support variable-length paths via PathPattern.repeat and filters via PathPattern.where.

A path query has three steps:

  1. The pattern says which paths match.
  2. A selector says which matching paths to keep. PathPattern.all keeps every path, PathPattern.any keeps one path per group, and PathPattern.any_shortest keeps one shortest path per group. Selectors that keep one path per group require .per(...) to define the groups.
  3. A method on the selector returns the result. .paths() returns one PathTraversal per kept path — a unique per-path identifier plus the nodes and relationships visited along the way.

You may also query PathPattern.exists, which returns whether endpoint pairs are connected via a matching path. Use this method when you only need to know which endpoints are connected, as it is cheaper than other modes.

Examples

from relationalai.semantics import Model, String
m = Model("Social")
Person = m.Concept("Person", identify_by={"name": String})
Person.follows = m.Relationship(f"{Person:a} follows {Person:b}")
# ... populate people and `follows` edges ...

Enumerate every 1-to-3-hop path starting from Alice:

p = m.path(Person.follows.repeat(1, 3)).where(Person.name == "Alice").all().paths()
m.select(p, p.length, p.nodes["index"], Person(p.nodes).name).inspect()

List who can reach whom in 1 to 5 hops, without enumerating paths:

A, Z = Person.ref(), Person.ref()
pattern = m.path(A, Person.follows.repeat(1, 5), Z)
m.where(pattern.exists()).select(A.name, Z.name).inspect()

Get one shortest path for each connected pair:

p = pattern.any_shortest().per(A, Z).paths()
m.select(A.name, Z.name, p.length).inspect()

Notes

This library is currently in preview. Preview features are publicly available but RelationalAI does not provide stability, SLA, or technical support commitments. They are intended for evaluation and testing purposes only and should not be used in production applications without consultation with the RelationalAI team.

Attributes

Attributes exposed by this module.

Functions

Functions exposed by this module.

Classes

Classes exposed by this module.