Skip to content

Commit

Permalink
fixed bug in node and edge creation, added more tests for Mermaid models
Browse files Browse the repository at this point in the history
added new schema Schema__MGraph__Default__Types to Schema__Mermaid__Graph which will hold the types that should be created by default (removed default types from Schema__MGraph__Graph__Config)
added respective Schema__Mermaid__Default__Types class
  • Loading branch information
DinisCruz committed Jan 9, 2025
1 parent da03a53 commit 4f6b52d
Show file tree
Hide file tree
Showing 22 changed files with 468 additions and 223 deletions.
30 changes: 17 additions & 13 deletions mgraph_ai/mgraph/models/Model__MGraph__Graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ def new_node(self, value : Any
attributes: Dict[Random_Guid, Schema__MGraph__Attribute] = None) -> Model__MGraph__Node: # Create and add a new node to the graph

if node_type is None:
node_type = self.data.graph_config.default_node_type
node_type = self.data.default_types.node_type or Schema__MGraph__Node
config_type = self.data.default_types.node_config_type or Schema__MGraph__Node__Config

node_config = Schema__MGraph__Node__Config(value_type = type(value))
node = Schema__MGraph__Node (attributes = attributes ,
node_config = node_config ,
node_type = node_type ,
value = value )
node_config = config_type(value_type = type(value))
node = node_type (attributes = attributes ,
node_config = node_config ,
node_type = node_type ,
value = value )

return self.add_node(node)

Expand All @@ -57,13 +58,16 @@ def new_edge(self, from_node_id: Random_Guid,
if to_node is None:
raise ValueError(f"To node {to_node_id} not found")

edge_config = Schema__MGraph__Edge__Config(edge_id = Random_Guid(),
from_node_type = self.data.nodes[from_node_id].node_type,
to_node_type = self.data.nodes[to_node_id ].node_type)
edge = Schema__MGraph__Edge (attributes = {} ,
edge_config = edge_config ,
from_node_id = from_node_id ,
to_node_id = to_node_id )
edge_type = self.data.default_types.edge_type or Schema__MGraph__Edge
config_type = self.data.default_types.edge_config_type or Schema__MGraph__Edge__Config

edge_config = config_type(edge_id = Random_Guid(),
from_node_type = self.data.nodes[from_node_id].node_type,
to_node_type = self.data.nodes[to_node_id ].node_type)
edge = edge_type (attributes = {} ,
edge_config = edge_config ,
from_node_id = from_node_id ,
to_node_id = to_node_id )

return self.add_edge(edge)

Expand Down
16 changes: 16 additions & 0 deletions mgraph_ai/mgraph/schemas/Schema__MGraph__Default__Types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import Type
from osbot_utils.type_safe.Type_Safe import Type_Safe
from mgraph_ai.mgraph.schemas.Schema__MGraph__Attribute import Schema__MGraph__Attribute
from mgraph_ai.mgraph.schemas.Schema__MGraph__Edge import Schema__MGraph__Edge
from mgraph_ai.mgraph.schemas.Schema__MGraph__Edge__Config import Schema__MGraph__Edge__Config
from mgraph_ai.mgraph.schemas.Schema__MGraph__Graph__Config import Schema__MGraph__Graph__Config
from mgraph_ai.mgraph.schemas.Schema__MGraph__Node import Schema__MGraph__Node
from mgraph_ai.mgraph.schemas.Schema__MGraph__Node__Config import Schema__MGraph__Node__Config

class Schema__MGraph__Default__Types(Type_Safe):
attribute_type : Type[Schema__MGraph__Attribute ]
edge_type : Type[Schema__MGraph__Edge ]
edge_config_type : Type[Schema__MGraph__Edge__Config ]
graph_config_type: Type[Schema__MGraph__Graph__Config]
node_type : Type[Schema__MGraph__Node ]
node_config_type : Type[Schema__MGraph__Node__Config ]
22 changes: 12 additions & 10 deletions mgraph_ai/mgraph/schemas/Schema__MGraph__Graph.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import Dict, Type
from mgraph_ai.mgraph.schemas.Schema__MGraph__Edge import Schema__MGraph__Edge
from mgraph_ai.mgraph.schemas.Schema__MGraph__Graph__Config import Schema__MGraph__Graph__Config
from mgraph_ai.mgraph.schemas.Schema__MGraph__Node import Schema__MGraph__Node
from osbot_utils.type_safe.Type_Safe import Type_Safe
from osbot_utils.helpers.Random_Guid import Random_Guid
from typing import Dict, Type
from mgraph_ai.mgraph.schemas.Schema__MGraph__Default__Types import Schema__MGraph__Default__Types
from mgraph_ai.mgraph.schemas.Schema__MGraph__Edge import Schema__MGraph__Edge
from mgraph_ai.mgraph.schemas.Schema__MGraph__Graph__Config import Schema__MGraph__Graph__Config
from mgraph_ai.mgraph.schemas.Schema__MGraph__Node import Schema__MGraph__Node
from osbot_utils.type_safe.Type_Safe import Type_Safe
from osbot_utils.helpers.Random_Guid import Random_Guid

class Schema__MGraph__Graph(Type_Safe):
edges : Dict[Random_Guid, Schema__MGraph__Edge]
graph_config: Schema__MGraph__Graph__Config
graph_type : Type['Schema__MGraph__Graph']
nodes : Dict[Random_Guid, Schema__MGraph__Node]
default_types: Schema__MGraph__Default__Types
edges : Dict[Random_Guid, Schema__MGraph__Edge]
graph_config : Schema__MGraph__Graph__Config
graph_type : Type['Schema__MGraph__Graph']
nodes : Dict[Random_Guid, Schema__MGraph__Node]
5 changes: 0 additions & 5 deletions mgraph_ai/mgraph/schemas/Schema__MGraph__Graph__Config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
from typing import Type
from mgraph_ai.mgraph.schemas.Schema__MGraph__Edge import Schema__MGraph__Edge
from mgraph_ai.mgraph.schemas.Schema__MGraph__Node import Schema__MGraph__Node
from osbot_utils.helpers.Random_Guid import Random_Guid
from osbot_utils.type_safe.Type_Safe import Type_Safe

class Schema__MGraph__Graph__Config(Type_Safe):
default_edge_type: Type[Schema__MGraph__Edge]
default_node_type: Type[Schema__MGraph__Node]
graph_id : Random_Guid
4 changes: 1 addition & 3 deletions mgraph_ai/providers/mermaid/models/Model__Mermaid__Edge.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Edge import Schema__Mermaid__Edge
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Edge__Config import Schema__Mermaid__Edge__Config
from mgraph_ai.mgraph.models.Model__MGraph__Edge import Model__MGraph__Edge


class Model__Mermaid__Edge(Model__MGraph__Edge):
data : Schema__Mermaid__Edge
config: Schema__Mermaid__Edge__Config
data : Schema__Mermaid__Edge
34 changes: 14 additions & 20 deletions mgraph_ai/providers/mermaid/models/Model__Mermaid__Graph.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
from typing import Dict
from mgraph_ai.providers.mermaid.models.Model__Mermaid__Node import Model__Mermaid__Node
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Graph__Config import Schema__Mermaid__Graph__Config
from mgraph_ai.mgraph.models.Model__MGraph__Edge import Model__MGraph__Edge
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Graph import Schema__Mermaid__Graph
from mgraph_ai.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph
from mgraph_ai.mgraph.models.Model__MGraph__Node import Model__MGraph__Node
from osbot_utils.helpers import Random_Guid

class Model__Mermaid__Graph(Model__MGraph__Graph):
edges : Dict[Random_Guid, Model__MGraph__Edge]
nodes : Dict[Random_Guid, Model__MGraph__Node]
config : Schema__Mermaid__Graph__Config
data: Schema__Mermaid__Graph

def add_node(self, **kwargs):
new_node = Model__Mermaid__Node(**kwargs)
self.nodes[new_node.node_id] = new_node
return new_node

def nodes(self):
for node in self.model.nodes.values():
yield Model__Mermaid__Node(data = node)

def edges(self):
for node in self.model.edges.values():
yield Model__Mermaid__Node(data = node)
# def add_node(self, **kwargs):
# new_node = Model__Mermaid__Node(**kwargs)
# self.nodes[new_node.node_id] = new_node
# return new_node
#
# def nodes(self):
# for node in self.model.nodes.values():
# yield Model__Mermaid__Node(data = node)
#
# def edges(self):
# for node in self.model.edges.values():
# yield Model__Mermaid__Node(data = node)
8 changes: 0 additions & 8 deletions mgraph_ai/providers/mermaid/models/Model__Mermaid__Node.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node import Schema__Mermaid__Node
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node__Config import Schema__Mermaid__Node__Config
from mgraph_ai.mgraph.models.Model__MGraph__Node import Model__MGraph__Node

class Model__Mermaid__Node(Model__MGraph__Node):
data : Schema__Mermaid__Node
config: Schema__Mermaid__Node__Config

def __init__(self, **kwargs):
super().__init__(**kwargs)

from osbot_utils.utils.Dev import pprint
pprint(kwargs.get('data' , {}))
pprint(Schema__Mermaid__Node.from_json({}))
#self.data = Schema__Mermaid__Node .from_json(kwargs.get('data' , {}))
#self.config = Schema__Mermaid__Node__Config.from_json(kwargs.get('config', {}))
self.ensure_label_is_set()

def ensure_label_is_set(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import Type
from osbot_utils.type_safe.Type_Safe import Type_Safe
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Edge import Schema__Mermaid__Edge
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Edge__Config import Schema__Mermaid__Edge__Config
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Graph__Config import Schema__Mermaid__Graph__Config
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node import Schema__Mermaid__Node
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node__Config import Schema__Mermaid__Node__Config
from mgraph_ai.mgraph.schemas.Schema__MGraph__Attribute import Schema__MGraph__Attribute

class Schema__Mermaid__Default__Types(Type_Safe):
attribute_type : Type[Schema__MGraph__Attribute ]
edge_type : Type[Schema__Mermaid__Edge ]
edge_config_type : Type[Schema__Mermaid__Edge__Config ]
graph_config_type: Type[Schema__Mermaid__Graph__Config]
node_type : Type[Schema__Mermaid__Node ]
node_config_type : Type[Schema__Mermaid__Node__Config ]
14 changes: 8 additions & 6 deletions mgraph_ai/providers/mermaid/schemas/Schema__Mermaid__Graph.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import List, Dict, Type
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Edge import Schema__Mermaid__Edge
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Graph__Config import Schema__Mermaid__Graph__Config
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node import Schema__Mermaid__Node
from mgraph_ai.mgraph.schemas.Schema__MGraph__Graph import Schema__MGraph__Graph
from osbot_utils.helpers.Random_Guid import Random_Guid
from typing import List, Dict, Type
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Default__Types import Schema__Mermaid__Default__Types
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Edge import Schema__Mermaid__Edge
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Graph__Config import Schema__Mermaid__Graph__Config
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node import Schema__Mermaid__Node
from mgraph_ai.mgraph.schemas.Schema__MGraph__Graph import Schema__MGraph__Graph
from osbot_utils.helpers.Random_Guid import Random_Guid


class Schema__Mermaid__Graph(Schema__MGraph__Graph):
default_types: Schema__Mermaid__Default__Types
edges : Dict[Random_Guid, Schema__Mermaid__Edge]
graph_config : Schema__Mermaid__Graph__Config
graph_type : Type['Schema__Mermaid__Graph']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@


class Schema__Mermaid__Graph__Config(Schema__MGraph__Graph__Config):
default_edge_type : Type[Schema__Mermaid__Edge]
default_node_type : Type[Schema__Mermaid__Node]
allow_circle_edges : bool
allow_duplicate_edges : bool
graph_title : str
16 changes: 13 additions & 3 deletions tests/unit/mgraph/actions/test_MGraph__Edit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from unittest import TestCase

from osbot_utils.utils.Objects import __

from mgraph_ai.mgraph.actions.MGraph__Edit import MGraph__Edit
from mgraph_ai.mgraph.domain.MGraph__Graph import MGraph__Graph
from mgraph_ai.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph
Expand Down Expand Up @@ -61,9 +64,16 @@ def test_deletion(self):
assert _.graph.edge (edge_2.id()) is None

def test_node_with_custom_type(self):
class CustomNode(Schema__MGraph__Node): pass
class Custom_Node(Schema__MGraph__Node): pass
custom_node = Custom_Node()

assert custom_node.obj() == __(attributes = __(),
node_config = __(node_id = custom_node.node_config.node_id,
value_type = None ),
node_type = 'test_MGraph__Edit.Custom_Node' ,
value = None )

node = self.edit.new_node("custom_value", node_type=CustomNode) # Create node with custom type
node = self.edit.new_node("custom_value", node_type=Custom_Node) # Create node with custom type

assert node.node.data.node_type is CustomNode
assert node.node.data.node_type is Custom_Node
assert node.value() == "custom_value"
26 changes: 14 additions & 12 deletions tests/unit/mgraph/domain/test_MGraph__Graph.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from unittest import TestCase

from mgraph_ai.mgraph.schemas.Schema__MGraph__Default__Types import Schema__MGraph__Default__Types
from osbot_utils.helpers.Safe_Id import Safe_Id
from mgraph_ai.mgraph.domain.MGraph__Edge import MGraph__Edge
from mgraph_ai.mgraph.domain.MGraph__Node import MGraph__Node
Expand All @@ -16,17 +18,17 @@ class Simple_Node(Schema__MGraph__Node): pass
class test_MGraph__Graph(TestCase):

def setUp(self): # Initialize test data
self.graph_config = Schema__MGraph__Graph__Config(graph_id = Random_Guid(),
default_node_type = Simple_Node,
default_edge_type = None)
self.schema_graph = Schema__MGraph__Graph (nodes = {} ,
edges = {} ,
graph_config = self.graph_config ,
graph_type = Schema__MGraph__Graph)


self.model_graph = Model__MGraph__Graph(data=self.schema_graph) # Create model graph
self.graph = MGraph__Graph (graph=self.model_graph)
self.default_types = Schema__MGraph__Default__Types (node_type = Simple_Node ,
edge_type = None )
self.graph_config = Schema__MGraph__Graph__Config (graph_id = Random_Guid() )
self.schema_graph = Schema__MGraph__Graph (default_types = self.default_types ,
nodes = {} ,
edges = {} ,
graph_config = self.graph_config ,
graph_type = Schema__MGraph__Graph)

self.model_graph = Model__MGraph__Graph (data=self.schema_graph) # Create model graph
self.graph = MGraph__Graph (graph=self.model_graph)

def test_init(self): # Tests basic initialization
assert type(self.graph) is MGraph__Graph
Expand All @@ -40,7 +42,7 @@ def test_node_operations(self):
assert node.value() == "test_value"
assert type(node ) is MGraph__Node
assert type(node.node ) is Model__MGraph__Node
assert type(node.node.data) is Schema__MGraph__Node
assert type(node.node.data) is Simple_Node

retrieved_node = self.graph.node(node.id()) # Retrieve node by ID
assert retrieved_node is not None
Expand Down
8 changes: 3 additions & 5 deletions tests/unit/mgraph/models/test_Model__MGraph__Edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ class test_Model__MGraph__Edge(TestCase):
def setUp(self): # Initialize test data
self.from_node_id = Random_Guid()
self.to_node_id = Random_Guid()
self.edge_config = Schema__MGraph__Edge__Config(
edge_id = Random_Guid(),
from_node_type = Schema__MGraph__Node,
to_node_type = Schema__MGraph__Node
)
self.edge_config = Schema__MGraph__Edge__Config(edge_id = Random_Guid(),
from_node_type = Schema__MGraph__Node,
to_node_type = Schema__MGraph__Node)
self.edge = Schema__MGraph__Edge(
attributes = {},
edge_config = self.edge_config,
Expand Down
Loading

0 comments on commit 4f6b52d

Please sign in to comment.