Table of Contents

Class GraphNode<T>

Namespace
AiDotNet.RetrievalAugmentedGeneration.Graph
Assembly
AiDotNet.dll

Represents a node in a knowledge graph, typically an entity extracted from text.

public class GraphNode<T>

Type Parameters

T

The numeric type used for vector operations.

Inheritance
GraphNode<T>
Inherited Members

Remarks

A graph node stores an entity (person, place, concept, etc.) along with its properties and embeddings. Nodes are connected by edges to form a knowledge graph that captures relationships between entities.

For Beginners: Think of a node as a person in a social network.

Just like a Facebook profile has:

  • Name: "John Smith"
  • Properties: age, location, interests
  • Connections: friends, family, coworkers

A GraphNode has:

  • Id: Unique identifier
  • Label: Entity type (PERSON, ORGANIZATION, LOCATION)
  • Properties: Additional metadata
  • Embedding: Numeric representation for similarity search

For example:

  • Id: "person_123"
  • Label: "PERSON"
  • Properties: { "name": "Albert Einstein", "occupation": "Physicist" }
  • Embedding: [0.23, -0.45, 0.67, ...] (vector representation)

Constructors

GraphNode(string, string)

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

public GraphNode(string id, string label)

Parameters

id string

Unique identifier for the node.

label string

The entity label or type.

Properties

CreatedAt

Timestamp when this node was created.

public DateTime CreatedAt { get; set; }

Property Value

DateTime

Embedding

Vector embedding for similarity search and clustering.

public Vector<T>? Embedding { get; set; }

Property Value

Vector<T>

Id

Unique identifier for this node.

public string Id { get; set; }

Property Value

string

Label

The entity label or type (e.g., PERSON, ORGANIZATION, LOCATION).

public string Label { get; set; }

Property Value

string

Properties

Additional properties and metadata for this entity.

public Dictionary<string, object> Properties { get; set; }

Property Value

Dictionary<string, object>

UpdatedAt

Timestamp when this node was last updated.

public DateTime UpdatedAt { get; set; }

Property Value

DateTime

Methods

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

GetProperty<TValue>(string)

Gets a property value from this node.

public TValue? GetProperty<TValue>(string key)

Parameters

key string

The property key.

Returns

TValue

The property value, or default if not found or conversion fails.

Type Parameters

TValue

The expected type of the property value.

Remarks

This method handles JSON deserialization quirks where numeric types may differ (e.g., int stored as long after JSON round-trip). It uses Convert.ChangeType for IConvertible types to handle such conversions gracefully.

The method catches and handles the following exceptions during conversion: - InvalidCastException: When the types are incompatible - FormatException: When the string representation is invalid - OverflowException: When the value is outside the target type's range

SetProperty(string, object)

Adds or updates a property on this node.

public void SetProperty(string key, object value)

Parameters

key string

The property key.

value object

The property value.

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.