Class GraphEdge<T>
- Namespace
- AiDotNet.RetrievalAugmentedGeneration.Graph
- Assembly
- AiDotNet.dll
Represents a directed edge (relationship) between two nodes in a knowledge graph.
public class GraphEdge<T>
Type Parameters
TThe numeric type used for vector operations.
- Inheritance
-
GraphEdge<T>
- Inherited Members
Remarks
A graph edge represents a relationship between two entities, such as "works_for", "located_in", or "friend_of". Edges are directed (from source to target) and can have properties to store additional relationship metadata.
For Beginners: Think of an edge as a relationship or connection between two people.
In a social network:
- "Alice WORKS_FOR Microsoft" (Alice is the source, Microsoft is the target)
- "Bob LIVES_IN Seattle" (Bob is the source, Seattle is the target)
- "Charlie KNOWS David" (Charlie knows David, but maybe David doesn't know Charlie - it's directional!)
In a knowledge graph:
- Source: The entity the relationship starts from
- Target: The entity the relationship points to
- Type: The kind of relationship (WORKS_FOR, LIVES_IN, KNOWS)
- Properties: Extra info (since: "2020", strength: 0.9)
- Weight: How important or strong this relationship is (0.0 to 1.0)
For example: Source: "Albert Einstein" (PERSON) Target: "Princeton University" (ORGANIZATION) Type: "WORKED_AT" Properties: { "from": "1933", "to": "1955" } Weight: 0.95 (very strong relationship)
Constructors
GraphEdge(string, string, string, double)
Initializes a new instance of the GraphEdge<T> class.
public GraphEdge(string sourceId, string targetId, string relationType, double weight = 1)
Parameters
sourceIdstringThe source node ID.
targetIdstringThe target node ID.
relationTypestringThe relationship type.
weightdoubleThe relationship weight (default: 1.0).
Properties
CreatedAt
Timestamp when this edge was created.
public DateTime CreatedAt { get; set; }
Property Value
Id
Unique identifier for this edge.
public string Id { get; set; }
Property Value
Properties
Additional properties and metadata for this relationship.
public Dictionary<string, object> Properties { get; set; }
Property Value
RelationType
The relationship type (e.g., WORKS_FOR, LOCATED_IN, FRIEND_OF).
public string RelationType { get; set; }
Property Value
SourceId
The source node ID (where the relationship starts).
public string SourceId { get; set; }
Property Value
TargetId
The target node ID (where the relationship points to).
public string TargetId { get; set; }
Property Value
Weight
Weight or strength of this relationship (0.0 to 1.0).
public double Weight { get; set; }
Property Value
Methods
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
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 edge.
public TValue? GetProperty<TValue>(string key)
Parameters
keystringThe property key.
Returns
- TValue
The property value, or default if not found or conversion fails.
Type Parameters
TValueThe 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 edge.
public void SetProperty(string key, object value)
Parameters
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.