Skip to content

relationalai.std.graphs.EdgeInstance

class relationalai.std.graphs.EdgeInstance

EdgeInstance is a subclass of Producer that produces edges in a graph. They are created by called a graph’s Edge object or the Edge.add() method. An EdgeInstance is similar to an Instance, but rather than representing an object, it represents a pairs of objects. As a result, you can’t add EdgeInstance objects to a Type.

import relationalai as rai
from relationalai.std.graphs import Graph
# Create a model with a 'Person' type.
model = rai.Model("socialNetwork")
Person = model.Type("Person")
# Add people to the model connected by a multi-valued 'follows' property.
with model.rule():
alice = Person.add(name="Alice")
bob = Person.add(name="Bob")
alice.follows.add(bob)
# Create a directed graph.
graph = Graph(model)
# Add edges to the graph from the 'Person.follows' property.
graph.Edge.extend(Person.follows, type="follows")
# Query the edges in the graph.
with model.query() as select:
# 'edge' is an EdgeInstance object.
edge = graph.Edge()
# Use the '.from_' and '.to' attributes to access the nodes
# connected by the edge, and the '.type' attribute to access
# the edge's 'type' property.
response = select(edge.from_.name, edge.to.name, edge.type)
print(response.results)
# Output:
# name name2 v
# 0 Alice Bob follows
# Use the '.set()' method to set properties on an edge.
with model.rule():
edge = graph.Edge()
# Set the 'weight' property of all edges to 1.0.
edge.set(weight=1.0)
# NOTE: Setting the 'weight' property does not turn the graph
# into a weighted graph. You can only create a weighted graph
# by passing 'weighted=True' to the 'Graph' constructor.
# Query the edges in the graph.
with model.query() as select:
edge = graph.Edge()
response = select(edge.from_.name, edge.to.name, edge.weight)
print(response.results)
# Output:
# name name2 v
# 0 Alice Bob 1.0
NameTypeDescription
.from_ProducerThe node at the start of the edge.
.toProducerThe node at the end of the edge.
EdgeInstance.from_

Returns a Producer object that produces the node at the start of an edge:

import relationalai as rai
from relationalai.std.graphs import Graph
# Create a model with a 'Person' type.
model = rai.Model("socialNetwork")
Person = model.Type("Person")
# Add people to the model connected by a multi-valued 'follows' property.
with model.rule():
alice = Person.add(name="Alice")
bob = Person.add(name="Bob")
alice.follows.add(bob)
# Create a directed graph with 'Person' nodes and 'follows' edges.
graph = Graph(model)
graph.Edge.extend(Person.follows)
# Get the names of all people following Bob.
with model.query() as select:
edge = graph.Edge(to=Person(name="Bob"))
follower = edge.from_
response = select(follower.name)
print(response.results)
# Output:
# name
# 0 Alice
EdgeInstance.to

Returns a Producer object that produces the terminal node of an edge:

import relationalai as rai
from relationalai.std.graphs import Graph
# Create a model with a 'Person' type.
model = rai.Model("socialNetwork")
Person = model.Type("Person")
# Add people to the model connected by a multi-valued 'follows' property.
with model.rule():
alice = Person.add(name="Alice")
bob = Person.add(name="Bob")
alice.follows.add(bob)
# Create a directed graph with 'Person' nodes and 'follows' edges.
graph = Graph(model)
graph.Edge.extend(Person.follows)
# Get the names of all people Alice follows.
with model.query() as select:
edge = graph.Edge(from_=Person(name="Alice"))
follows = edge.to
response = select(follows.name)
print(response.results)
# Output:
# name
# 0 Bob
NameDescriptionReturns
.set()Set properties on an edge.EdgeInstance
EdgeInstance.set(**kwargs) -> EdgeInstance

Sets properties on an EdgeInstance object and returns the EdgeInstance. Note that unlike Instance.set(), you can’t set a Type on an edge. Must be called in a rule or query context.

NameTypeDescription
*kwargsAnyProperties and values to set on the EdgeInstance.

An EdgeInstance object.

import relationalai as rai
from relationalai.std.graphs import Graph
# Create a model with a 'Person' type.
model = rai.Model("socialNetwork")
Person = model.Type("Person")
# Add people to the model connected by a multi-valued 'follows' property.
with model.rule():
alice = Person.add(name="Alice")
bob = Person.add(name="Bob")
alice.follows.add(bob)
# Create a directed graph with 'Person' nodes and 'follows' edges.
graph = Graph(model)
graph.Edge.extend(Person.follows)
# Set the 'color' property of all edges to 'red'.
with model.rule():
edge = graph.Edge()
edge.set(color="red")
# Query the edges in the graph.
with model.query() as select:
edge = graph.Edge()
response = select(edge.from_.name, edge.to.name, edge.color)
print(response.results)
# Output:
# name name2 v
# 0 Alice Bob red