-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started refactoring of Mermaid__Edit
- Loading branch information
Showing
10 changed files
with
208 additions
and
81 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
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
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,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 |
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
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 |
---|---|---|
@@ -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')) |
Oops, something went wrong.