Table of Contents

Class GraphQueryMatcher<T>

Namespace
AiDotNet.RetrievalAugmentedGeneration.Graph
Assembly
AiDotNet.dll

Simple pattern matching for graph queries (inspired by Cypher/SPARQL but simplified).

public class GraphQueryMatcher<T>

Type Parameters

T

The numeric type used for vector operations.

Inheritance
GraphQueryMatcher<T>
Inherited Members

Remarks

Supports basic graph pattern matching queries like: - (Person)-[KNOWS]->(Person) - (Person {name: "Alice"})-[WORKS_AT]->(Company) - (a:Person)-[r:KNOWS]->(b:Person)

For Beginners: Pattern matching is like SQL for graphs.

SQL Example:

SELECT * FROM persons WHERE name = 'Alice'

Graph Pattern Example:

(Person {name: "Alice"})-[KNOWS]->(Person)

Meaning: Find all people that Alice knows

Another Example:

(Person)-[WORKS_AT]->(Company {name: "Google"})

Meaning: Find all people who work at Google

This is much more natural for relationship-heavy data!

Constructors

GraphQueryMatcher(KnowledgeGraph<T>)

Initializes a new instance of the GraphQueryMatcher<T> class.

public GraphQueryMatcher(KnowledgeGraph<T> graph)

Parameters

graph KnowledgeGraph<T>

The knowledge graph to query.

Methods

ExecutePattern(string)

Executes a simple pattern query string.

public List<GraphPath<T>> ExecutePattern(string pattern)

Parameters

pattern string

The pattern string (simplified Cypher-like syntax).

Returns

List<GraphPath<T>>

List of matching paths.

Examples

ExecutePattern("(Person)-[KNOWS]->(Person)") ExecutePattern("(Person {name: "Alice"})-[WORKS_AT]->(Company)")

FindNodes(string, Dictionary<string, object>?)

Finds nodes matching a label and optional property filters.

public List<GraphNode<T>> FindNodes(string label, Dictionary<string, object>? properties = null)

Parameters

label string

The node label to match.

properties Dictionary<string, object>

Optional property filters.

Returns

List<GraphNode<T>>

List of matching nodes.

Examples

FindNodes("Person", new Dictionary<string, object> { { "name", "Alice" } })

FindPaths(string, string, string, Dictionary<string, object>?, Dictionary<string, object>?)

Finds paths matching a pattern: (source label)-[relationship type]->(target label).

public List<GraphPath<T>> FindPaths(string sourceLabel, string relationshipType, string targetLabel, Dictionary<string, object>? sourceProperties = null, Dictionary<string, object>? targetProperties = null)

Parameters

sourceLabel string

The source node label.

relationshipType string

The relationship type.

targetLabel string

The target node label.

sourceProperties Dictionary<string, object>

Optional source node property filters.

targetProperties Dictionary<string, object>

Optional target node property filters.

Returns

List<GraphPath<T>>

List of matching paths.

Examples

FindPaths("Person", "KNOWS", "Person") // Find all KNOWS relationships FindPaths("Person", "WORKS_AT", "Company", new Dictionary<string, object> { { "name", "Alice" } }, new Dictionary<string, object> { { "name", "Google" } })

FindPathsOfLength(string, int, string?)

Finds all paths of specified length from a source node.

public List<List<GraphNode<T>>> FindPathsOfLength(string sourceId, int pathLength, string? relationshipType = null)

Parameters

sourceId string

The source node ID.

pathLength int

The path length (number of hops).

relationshipType string

Optional relationship type filter.

Returns

List<List<GraphNode<T>>>

List of paths.

FindShortestPaths(string, string, int)

Finds all shortest paths between two nodes.

public List<List<GraphNode<T>>> FindShortestPaths(string sourceId, string targetId, int maxDepth = 10)

Parameters

sourceId string

The source node ID.

targetId string

The target node ID.

maxDepth int

Maximum depth to search (prevents infinite loops).

Returns

List<List<GraphNode<T>>>

List of shortest paths.