Skip to content

Commit

Permalink
started refactoring of Mermaid__Edit
Browse files Browse the repository at this point in the history
  • Loading branch information
DinisCruz committed Jan 10, 2025
1 parent 10e3759 commit 3b93303
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 81 deletions.
3 changes: 3 additions & 0 deletions mgraph_ai/mgraph/actions/MGraph__Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ def edge(self, edge_id: Random_Guid) -> MGraph__Edge:
def edges(self) -> List[MGraph__Edge]: # Get all edges in the graph
return self.graph.edges()

def graph_id(self):
return self.graph.graph_id()

def nodes(self) -> List[MGraph__Node]: # Get all nodes in the graph
return self.graph.nodes()
7 changes: 4 additions & 3 deletions mgraph_ai/mgraph/actions/MGraph__Edit.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import Any, Type, Dict
from osbot_utils.helpers.Random_Guid import Random_Guid
from osbot_utils.type_safe.Type_Safe import Type_Safe
from osbot_utils.type_safe.decorators.type_safe import type_safe
from mgraph_ai.mgraph.domain.MGraph__Edge import MGraph__Edge
from mgraph_ai.mgraph.domain.MGraph__Graph import MGraph__Graph
from mgraph_ai.mgraph.domain.MGraph__Node import MGraph__Node
from mgraph_ai.mgraph.schemas.Schema__MGraph__Attribute import Schema__MGraph__Attribute
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
from osbot_utils.type_safe.decorators.type_safe import type_safe


class MGraph__Edit(Type_Safe):
graph: MGraph__Graph
Expand Down
7 changes: 5 additions & 2 deletions mgraph_ai/mgraph/domain/MGraph__Graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ def mgraph_edge(self, edge: Model__MGraph__Edge) -> MGraph__Edge:
def mgraph_node(self, node: Model__MGraph__Node) -> MGraph__Edge:
return self.node_domain_type(node=node, graph=self.model)

def new_edge(self, from_node_id: Random_Guid, to_node_id : Random_Guid) -> MGraph__Edge:
edge = self.model.new_edge(from_node_id=from_node_id, to_node_id=to_node_id)
def new_edge(self, from_node_id: Random_Guid,
to_node_id : Random_Guid,
attributes: Dict[Random_Guid, Schema__MGraph__Attribute] = None) -> MGraph__Edge:

edge = self.model.new_edge(from_node_id=from_node_id, to_node_id=to_node_id, attributes=attributes)
return self.mgraph_edge(edge=edge)

def new_node(self, value : Any ,
Expand Down
5 changes: 3 additions & 2 deletions mgraph_ai/mgraph/models/Model__MGraph__Graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def new_node(self, value : Any

@type_safe
def new_edge(self, from_node_id: Random_Guid,
to_node_id : Random_Guid) -> Model__MGraph__Edge: # Create and add a new edge between nodes
to_node_id : Random_Guid,
attributes : Dict[Random_Guid, Schema__MGraph__Attribute] = None) -> Model__MGraph__Edge: # Create and add a new edge between nodes

from_node = self.data.nodes.get(from_node_id)
to_node = self.data.nodes.get(to_node_id )
Expand All @@ -66,7 +67,7 @@ def new_edge(self, from_node_id: Random_Guid,
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 = edge_type (attributes = attributes ,
edge_config = edge_config ,
from_node_id = from_node_id ,
to_node_id = to_node_id )
Expand Down
70 changes: 67 additions & 3 deletions mgraph_ai/providers/mermaid/actions/Mermaid__Edit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,70 @@
from mgraph_ai.mgraph.actions.MGraph__Edit import MGraph__Edit
from mgraph_ai.providers.mermaid.domain.Mermaid__Graph import Mermaid__Graph
from typing import Dict
from mgraph_ai.mgraph.schemas.Schema__MGraph__Attribute import Schema__MGraph__Attribute
from osbot_utils.helpers.Safe_Id import Safe_Id
from mgraph_ai.providers.mermaid.actions.Mermaid__Data import Mermaid__Data
from mgraph_ai.providers.mermaid.actions.Mermaid__Render import Mermaid__Render
from mgraph_ai.providers.mermaid.configs.Mermaid__Render__Config import Mermaid__Render__Config
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Diagram__Type import Schema__Mermaid__Diagram__Type
from osbot_utils.decorators.methods.cache_on_self import cache_on_self
from mgraph_ai.mgraph.actions.MGraph__Edit import MGraph__Edit
from mgraph_ai.providers.mermaid.domain.Mermaid__Graph import Mermaid__Graph


class Mermaid__Edit(MGraph__Edit):
graph : Mermaid__Graph
graph : Mermaid__Graph

def add_directive(self, directive):
self.render_config().directives.append(directive)
return self

def add_edge(self, from_node_key:Safe_Id, to_node_key:Safe_Id, label:str=None, attributes:Dict=None):
nodes__by_key = self.data().nodes__by_key()
from_node = nodes__by_key.get(from_node_key) # todo: add method to data to get these nodes
to_node = nodes__by_key.get(to_node_key ) # todo: add config option to auto create node on edges (where that node doesn't exist)
if from_node is None:
from_node = self.add_node(key=from_node_key)
if to_node is None:
to_node = self.add_node(key=to_node_key)

from_node_id = from_node.node_id()
to_node_id = to_node.node_id()
edge_attributes = {}
for key,value in attributes.items(): # todo: refactor this logic of creating attributes into a separate method (since this will also be needed for the nodes)
attribute = Schema__MGraph__Attribute(attribute_name=key, attribute_value=value, attribute_type=type(value))
edge_attributes[attribute.attribute_id] = attribute

edge = self.graph.new_edge(from_node_id=from_node_id, to_node_id=to_node_id, attributes=edge_attributes)

return edge
# from_node = nodes__by_key.get(from_node_key)
# to_node = nodes__by_key.get(to_node_key)
# if not from_node:
# from_node = self.add_node(key=from_node_key, label=from_node_key)
# if not to_node:
# to_node = self.add_node(key=to_node_key, label=to_node_key)
#
# kwargs = dict(from_node_id = from_node.node_id,
# to_node_id = to_node .node_id,
# label = label ,
# attributes = attributes )
# mermaid_edge = Mermaid__Edge(**kwargs)
# self.graph.add_edge(mermaid_edge.id, mermaid_edge)
# return mermaid_edge

def code(self) -> str:
return self.graph_render().code()

@cache_on_self
def data(self):
return Mermaid__Data(graph=self.graph) # todo: look at the best way to do this (i.e. give access to this class the info inside data)

@cache_on_self
def graph_render(self) -> Mermaid__Render: # todo: review this since we really shouldn't need be able to access the Mermaid__Render here
return Mermaid__Render(graph=self.graph)

def render_config(self) -> Mermaid__Render__Config: # todo: review this since we really should be able to access the Mermaid__Render__Config outside the Mermaid__Render object
return self.graph_render().config

def set_diagram_type(self, diagram_type):
if isinstance(diagram_type, Schema__Mermaid__Diagram__Type):
self.graph_render().diagram_type = diagram_type
2 changes: 1 addition & 1 deletion mgraph_ai/providers/mermaid/domain/Mermaid__Edge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from mgraph_ai.mgraph.domain.MGraph__Edge import MGraph__Edge
from mgraph_ai.providers.mermaid.models.Model__Mermaid__Edge import Model__Mermaid__Edge
from mgraph_ai.providers.mermaid.models.Model__Mermaid__Graph import Model__Mermaid__Graph
from mgraph_ai.providers.mermaid.models.Model__Mermaid__Graph import Model__Mermaid__Graph


class Mermaid__Edge(MGraph__Edge):
Expand Down
35 changes: 8 additions & 27 deletions mgraph_ai/providers/mermaid/domain/__to_do__Mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,20 @@
class Mermaid(Type_Safe):
graph : Mermaid__Graph

def add_directive(self, directive):
self.render().config.directives.append(directive)
return self

def add_edge(self, from_node_key, to_node_key, label=None,attributes=None):
nodes__by_key = self.data().nodes__by_key()
from_node = nodes__by_key.get(from_node_key)
to_node = nodes__by_key.get(to_node_key)
if not from_node:
from_node = self.add_node(key=from_node_key, label=from_node_key)
if not to_node:
to_node = self.add_node(key=to_node_key, label=to_node_key)

kwargs = dict(from_node_id = from_node.node_id,
to_node_id = to_node .node_id,
label = label ,
attributes = attributes )
mermaid_edge = Mermaid__Edge(**kwargs)
self.graph.add_edge(mermaid_edge.id, mermaid_edge)
return mermaid_edge



def add_node(self, **kwargs):
return self.graph.add_node(Schema__Mermaid__Node(**kwargs))

def data(self):
return Mermaid__Data(graph=self.graph)
# def add_node(self, **kwargs):
# return self.graph.add_node(Schema__Mermaid__Node(**kwargs))

def code(self):
return self.render().code()
# def data(self):
# return Mermaid__Data(graph=self.graph)

# def code(self):
# return self.render().code()

def code_markdown(self):
#self.code_create()
Expand Down Expand Up @@ -83,9 +66,7 @@ def set_direction(self, direction):
self.render().diagram_direction = Schema__Mermaid__Diagram__Direction[direction]
return self # If the value can't be set (not a valid name), do nothing

def set_diagram_type(self, diagram_type):
if isinstance(diagram_type, Schema__Mermaid__Diagram__Type):
self.render().diagram_type = diagram_type


def save(self, target_file=None):
file_path = target_file or '/tmp/mermaid.md'
Expand Down
56 changes: 56 additions & 0 deletions tests/unit/providers/mermaid/actions/test_Mermaid__Edit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from unittest import TestCase
from mgraph_ai.providers.mermaid.domain.Mermaid__Edge import Mermaid__Edge
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Diagram__Type import Schema__Mermaid__Diagram__Type
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node__Config import Schema__Mermaid__Node__Config
from osbot_utils.helpers.Safe_Id import Safe_Id
from osbot_utils.utils.Objects import __
Expand All @@ -21,6 +23,60 @@ def test__init__(self):
assert isinstance(_, MGraph__Edit) is True
assert type(_.graph) is Mermaid__Graph

def test_add_directive(self):
with self.mermaid__edit as _:
_.set_diagram_type(Schema__Mermaid__Diagram__Type.flowchart)
_.add_directive ('init: {"flowchart": {"htmlLabels": false}} ')
_.add_node (key='markdown', label='This **is** _Markdown_').markdown()

assert _.code() == ('%%{init: {"flowchart": {"htmlLabels": false}} }%%\n'
'flowchart LR\n'
' markdown["`This **is** _Markdown_`"]\n')

def test_add_edge(self):
with self.mermaid__edit as _:
from_node_key = 'from_key'
to_node_key = 'to_key'
edge = _.add_edge(from_node_key=from_node_key, to_node_key=to_node_key, label='an_label', attributes={'answer': '42'})
edge_attributes = edge.attributes()
attribute_id = edge_attributes[0].attribute.data.attribute_id
nodes__by_key = _.data().nodes__by_key()
from_node = nodes__by_key.get(from_node_key)
to_node = nodes__by_key.get(to_node_key )

assert is_guid(attribute_id) is True
assert type(from_node) == Mermaid__Node
assert type(from_node) == Mermaid__Node
assert type(edge ) == Mermaid__Edge

assert from_node.node_key() == from_node_key
assert to_node .node_key() == to_node_key
assert edge_attributes[0].attribute.obj() == __(data=__(attribute_id = attribute_id,
attribute_name = 'answer',
attribute_value = '42',
attribute_type = 'builtins.str'))

# assert edge.edge.obj() == __(data=__(label='',
# edge_config=__(from_node_type='mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node.Schema__Mermaid__Node',
# to_node_type='mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node.Schema__Mermaid__Node',
# edge_id='40f2fca9-eb5a-4254-b106-302c9ae7e7a6'),
# edge_type='mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Edge.Schema__Mermaid__Edge',
# attributes=__(7a233d96-25fa-4c8a-8899-3403720662d9=__(attribute_id='7a233d96-25fa-4c8a-8899-3403720662d9',
# attribute_name='answer',
# attribute_value='42',
# attribute_type='builtins.str')),
# from_node_id='49b37fc7-b40f-4926-87b6-603dfb106021',
# to_node_id='e98b4258-fa45-48b0-9efb-756f2c8ebdc4'))

# assert edge.obj() == __(config = __(output_node_from=False, output_node_to=False, edge_mode='') ,
# edge_id = edge.edge_id ,
# from_node_id = from_node.node_id ,
# attributes = __(answer='42') ,
# from_node_type = 'mgraph_ai.mermaid.domain.Mermaid__Node.Mermaid__Node' ,
# label = 'an_label' ,
# to_node_id = to_node.node_id ,
# to_node_type = 'mgraph_ai.mermaid.domain.Mermaid__Node.Mermaid__Node' )

def test_add_node(self):
with self.mermaid__edit as _:
node = _.add_node()
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/providers/mermaid/domain/test_Mermaid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from unittest import TestCase

from osbot_utils.utils.Misc import is_guid

from osbot_utils.utils.Objects import __

from mgraph_ai.providers.mermaid.domain.Mermaid import Mermaid


class test_Mermaid(TestCase):

@classmethod
def setUpClass(cls):
cls.mermaid = Mermaid()

def test__init__(self):
with self.mermaid as _:
graph_id = _.data().graph_id()
assert type(_) is Mermaid
assert is_guid(graph_id) is True
assert _.obj() == __(graph=__(model=__(data=__(default_types = __(attribute_type = 'mgraph_ai.mgraph.schemas.Schema__MGraph__Attribute.Schema__MGraph__Attribute' ,
edge_type = 'mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Edge.Schema__Mermaid__Edge' ,
edge_config_type = 'mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Edge__Config.Schema__Mermaid__Edge__Config' ,
graph_config_type = 'mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Graph__Config.Schema__Mermaid__Graph__Config',
node_type = 'mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node.Schema__Mermaid__Node' ,
node_config_type = 'mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node__Config.Schema__Mermaid__Node__Config' ),
edges = __(),
graph_config = __(allow_circle_edges=False,
allow_duplicate_edges=False,
graph_title='',
graph_id=graph_id),
graph_type = 'mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Graph.Schema__Mermaid__Graph',
mermaid_code = [],
nodes = __()),
node_model_type='mgraph_ai.providers.mermaid.models.Model__Mermaid__Node.Model__Mermaid__Node',
edge_model_type='mgraph_ai.providers.mermaid.models.Model__Mermaid__Edge.Model__Mermaid__Edge'),
node_domain_type='mgraph_ai.providers.mermaid.domain.Mermaid__Node.Mermaid__Node',
edge_domain_type='mgraph_ai.providers.mermaid.domain.Mermaid__Edge.Mermaid__Edge'))
Loading

0 comments on commit 3b93303

Please sign in to comment.