Path reasoning
Path reasoning helps you ask questions about paths through your semantic model: which entities are connected through one or more relationships, how they are connected, and how many ways there are to get from one to the other. Use this overview to decide when path reasoning fits your question and to choose the right next guide.
What path reasoning is
Section titled “What path reasoning is”A path is a sequence of entities joined by relationships. For example, in a payments model, “account A sent money to account B, which sent money to account C” is a path of two hops.
PyRel can already express paths with a fixed number of hops: you join the relationships in a where() clause, the same way you write any other rule.
The path library adds what fixed-length rules cannot do:
- Patterns with a variable number of hops.
repeat(1, 5)matches paths of one to five relationships. - Connectivity checks. A condition that holds when a matching path connects two endpoints. Check it per pair of endpoints, per initial node, per final node, or overall, without listing the paths.
- Shortest paths. Keep one, or all, of the shortest paths between each pair of endpoints.
- Counting without enumerating. Count matching paths, or shortest paths, in time polynomial in the size of your data, even when the number of paths is exponential.
- Path enumeration. Materialize each matching path, with the entities and relationships it visits, as a
PathTraversalvalue.
To use path reasoning in PyRel:
- Build a path pattern with
m.path(...): an alternating sequence of node and edge positions, with optional repeats and filters. A node position matches a concept entity, and an edge position matches a relationship. - Choose what to return.
.exists()on the pattern tells you whether a matching path exists. Otherwise, choose a selector that says which matching paths to keep (every path, one path per pair of endpoints, or the shortest paths), then ask for the paths or their count. - Define the result as a relationship in your model, so it is refreshed with the model and available to other rules and queries.
Path reasoning works on the concepts and relationships you already have. You do not build a separate graph object first.
When to use path reasoning
Section titled “When to use path reasoning”Use path reasoning when your question is about paths of variable or unknown length, or when you need the paths themselves. If every path has the same, small number of hops, write the joins directly with rules-based reasoning instead. If your question is about the structure of a single network with homogeneous node and edge types, use graph reasoning.
| If you need to… | Use… | To get… |
|---|---|---|
| Know whether entities are connected by a matching path | .exists() | A boolean condition representing existence of a matching path |
| Know how many paths connect each endpoint pair | .all().count() or .all_shortest().per(...).count() | A count for each connected pair, computed without enumerating paths |
| See one path between each connected pair | .any_shortest().per(...).paths() | One path per pair, with the sequence of traversed entities and relationships |
| See every path, or every shortest path | .all().paths() or .all_shortest().per(...).paths() | Every matching path, with the sequence of traversed entities and relationships |
Where to go next
Section titled “Where to go next”Follow the links below to learn more about path reasoning in PyRel.