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
TThe 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
graphKnowledgeGraph<T>The knowledge graph to query.
Methods
ExecutePattern(string)
Executes a simple pattern query string.
public List<GraphPath<T>> ExecutePattern(string pattern)
Parameters
patternstringThe pattern string (simplified Cypher-like syntax).
Returns
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
labelstringThe node label to match.
propertiesDictionary<string, object>Optional property filters.
Returns
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
sourceLabelstringThe source node label.
relationshipTypestringThe relationship type.
targetLabelstringThe target node label.
sourcePropertiesDictionary<string, object>Optional source node property filters.
targetPropertiesDictionary<string, object>Optional target node property filters.
Returns
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
sourceIdstringThe source node ID.
pathLengthintThe path length (number of hops).
relationshipTypestringOptional relationship type filter.
Returns
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
sourceIdstringThe source node ID.
targetIdstringThe target node ID.
maxDepthintMaximum depth to search (prevents infinite loops).