-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27dd7ca
commit b0ad380
Showing
4 changed files
with
130 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from graphai.graph import Graph | ||
from graphai.nodes import node | ||
|
||
__all__ = ["node"] | ||
__all__ = ["node", "Graph"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
class Graph: | ||
def __init__(self): | ||
self.nodes = [] | ||
self.edges = [] | ||
self.start_node = None | ||
self.end_nodes = [] | ||
|
||
def add_node(self, node): | ||
self.nodes.append(node) | ||
if node.is_start: | ||
if self.start_node is not None: | ||
raise Exception("Multiple start nodes are not allowed.") | ||
self.start_node = node | ||
if node.is_end: | ||
self.end_nodes.append(node) | ||
|
||
def add_edge(self, source, destination): | ||
# TODO add logic to check that source and destination are nodes | ||
# and they exist in the graph object already | ||
edge = Edge(source, destination) | ||
self.edges.append(edge) | ||
|
||
def set_start_node(self, node): | ||
self.start_node = node | ||
|
||
def set_end_node(self, node): | ||
self.end_node = node | ||
|
||
def compile(self): | ||
if not self.start_node: | ||
raise Exception("Start node not defined.") | ||
if not self.end_nodes: | ||
raise Exception("No end nodes defined.") | ||
if not self._is_valid(): | ||
raise Exception("Graph is not valid.") | ||
print("Graph compiled successfully.") | ||
|
||
def _is_valid(self): | ||
# Implement validation logic, e.g., checking for cycles, disconnected components, etc. | ||
return True | ||
|
||
def execute(self, input): | ||
current_node = self.start_node | ||
while current_node not in self.end_nodes: | ||
output = current_node.invoke(input=input) | ||
current_node = self._get_next_node(current_node, output) | ||
if current_node.is_end: | ||
break | ||
return output | ||
|
||
def _get_next_node(self, current_node, output): | ||
for edge in self.edges: | ||
if edge.source == current_node: | ||
return edge.destination | ||
raise Exception("No outgoing edge found for current node.") | ||
|
||
|
||
class Edge: | ||
def __init__(self, source, destination): | ||
self.source = source | ||
self.destination = destination |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters