Skip to content

Commit

Permalink
minor fix to MGraph__Export__Dot
Browse files Browse the repository at this point in the history
  • Loading branch information
DinisCruz committed Feb 9, 2025
1 parent 3675f23 commit e3c8638
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
4 changes: 4 additions & 0 deletions mgraph_db/mgraph/actions/exporters/dot/MGraph__Export__Dot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict, Any, Optional, Callable
from osbot_utils.utils.Lists import unique
from mgraph_db.mgraph.actions.exporters.MGraph__Export__Base import MGraph__Export__Base
from mgraph_db.mgraph.actions.exporters.dot.config.MGraph__Export__Dot__Config import MGraph__Export__Dot__Config
from mgraph_db.mgraph.actions.exporters.dot.config.MGraph__Export__Dot__Config__Font import MGraph__Export__Dot__Config__Font
Expand Down Expand Up @@ -32,6 +33,7 @@ def __init__(self, graph, config: Optional[MGraph__Export__Dot__Config] = None):

def create_node_data(self, node) -> Dict[str, Any]: # Create node data for DOT export
attrs = self.node_renderer.create_node_attributes(node)
attrs = unique(attrs) # todo: fix bug in create_node_attributes where multiple style statements can be created
node_data = {'id' : str(node.node_id),
'attrs': attrs }

Expand Down Expand Up @@ -121,9 +123,11 @@ def set_node__type_fill_color(self, node_type: type, color: str): self.ensure_ty
def set_node__type_font_color(self, node_type: type, color: str): self.ensure_type_font (node_type).color = color; return self
def set_node__type_font_size (self, node_type: type, size : int): self.ensure_type_font (node_type).size = size ; return self
def set_node__type_font_name (self, node_type: type, name : str): self.ensure_type_font (node_type).name = name ; return self
def set_node__type_rounded (self, node_type: type ):self.ensure_type_shape(node_type).rounded = True ; return self
def set_node__type_shape (self, node_type: type, shape: str): self.ensure_type_shape(node_type).type = shape; return self
def set_node__type_style (self, node_type: type, style: str): self.ensure_type_shape(node_type).style = style; return self


def set_edge__type_color(self, edge_type: type, color: str):
if not self.config.type.edge_color: self.config.type.edge_color = {}
self.config.type.edge_color[edge_type] = color
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import Dict
from mgraph_db.mgraph.actions.exporters.dot.config.MGraph__Export__Dot__Config__Display import MGraph__Export__Dot__Config__Display
from mgraph_db.mgraph.actions.exporters.dot.config.MGraph__Export__Dot__Config__Edge import MGraph__Export__Dot__Config__Edge
from mgraph_db.mgraph.actions.exporters.dot.config.MGraph__Export__Dot__Config__Graph import MGraph__Export__Dot__Config__Graph
Expand All @@ -7,8 +6,8 @@
from osbot_utils.type_safe.Type_Safe import Type_Safe

class MGraph__Export__Dot__Config(Type_Safe):
graph : MGraph__Export__Dot__Config__Graph # Graph-level settings
node : MGraph__Export__Dot__Config__Node # Global node settings
edge : MGraph__Export__Dot__Config__Edge # Global edge settings
display : MGraph__Export__Dot__Config__Display # Display flags
type : MGraph__Export__Dot__Config__Type # Type-specific overrides
graph : MGraph__Export__Dot__Config__Graph # Graph-level settings
node : MGraph__Export__Dot__Config__Node # Global node settings
edge : MGraph__Export__Dot__Config__Edge # Global edge settings
display : MGraph__Export__Dot__Config__Display # Display flags
type : MGraph__Export__Dot__Config__Type # Type-specific overrides
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class MGraph__Export__Dot__Node__Renderer(MGraph__Export__Dot__Base):

def create_node_attributes(self, node: Domain__MGraph__Node) -> List[str]:
return (self.create_node_base_attributes (node) +
self.create_node_shape_attributes (node) +
self.create_node_shape_attributes (node) + # todo: change how this works since this is not a good way to return the attributes
self.create_node_font_attributes (node) +
self.create_node_style_attributes (node) +
self.create_node_style_attributes (node) + # todo: since for example both create_node_shape_attributes can create an style attribute
self.create_node_label_attributes (node))

def create_node_base_attributes(self, node: Domain__MGraph__Node) -> List[str]:
Expand Down Expand Up @@ -92,7 +92,7 @@ def create_node_style_attributes(self, node: Domain__MGraph__Node) -> List[str]:

if node_type in self.config.type.shapes:
shape_config = self.config.type.shapes[node_type]
#if shape_config.fill_color: styles.add('filled')
if shape_config.fill_color: styles.add('filled')
if shape_config.rounded: styles.add('rounded')
if shape_config.style: styles.update(shape_config.style.split(','))

Expand Down
20 changes: 10 additions & 10 deletions tests/unit/mgraph/actions/exporters/dot/test_MGraph__Export__Dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def test_edge_source_node_styling(self):
assert dot_output == self.exporter.dot_code

assert dot_output == ('digraph {\n'
f' "{self.nodes_ids[0]}" [shape="circle", fillcolor="red", style="filled", fontsize="14", fontcolor="blue"]\n'
f' "{self.nodes_ids[0]}" [fillcolor="red", fontcolor="blue", fontsize="14", shape="circle", style="filled"]\n'
f' "{self.nodes_ids[1]}"\n'
f' "{self.nodes_ids[2]}"\n'
f' "{self.nodes_ids[0]}" -> "{self.nodes_ids[1]}"\n'
Expand All @@ -252,8 +252,8 @@ def test_edge_target_node_styling(self):

assert dot_output == ('digraph {\n'
f' "{self.nodes_ids[0]}"\n'
f' "{self.nodes_ids[1]}" [shape="diamond", fillcolor="blue", style="filled", fontsize="14", fontcolor="yellow"]\n'
f' "{self.nodes_ids[2]}" [shape="diamond", fillcolor="blue", style="filled", fontsize="14", fontcolor="yellow"]\n'
f' "{self.nodes_ids[1]}" [fillcolor="blue", fontcolor="yellow", fontsize="14", shape="diamond", style="filled"]\n'
f' "{self.nodes_ids[2]}" [fillcolor="blue", fontcolor="yellow", fontsize="14", shape="diamond", style="filled"]\n'
f' "{self.nodes_ids[0]}" -> "{self.nodes_ids[1]}"\n'
f' "{self.nodes_ids[0]}" -> "{self.nodes_ids[2]}"\n'
'}')
Expand All @@ -272,9 +272,9 @@ def test_edge_mixed_styling(self):
assert dot_output == self.exporter.dot_code

assert dot_output == ('digraph {\n'
f' "{self.nodes_ids[0]}" [shape="box", fillcolor="red", style="filled", fontsize="14"]\n'
f' "{self.nodes_ids[1]}" [shape="diamond", fillcolor="blue", style="filled", fontsize="16"]\n'
f' "{self.nodes_ids[2]}" [shape="diamond", fillcolor="blue", style="filled", fontsize="16"]\n'
f' "{self.nodes_ids[0]}" [fillcolor="red", fontsize="14", shape="box", style="filled"]\n'
f' "{self.nodes_ids[1]}" [fillcolor="blue", fontsize="16", shape="diamond", style="filled"]\n'
f' "{self.nodes_ids[2]}" [fillcolor="blue", fontsize="16", shape="diamond", style="filled"]\n'
f' "{self.nodes_ids[0]}" -> "{self.nodes_ids[1]}"\n'
f' "{self.nodes_ids[0]}" -> "{self.nodes_ids[2]}"\n'
'}')
Expand All @@ -291,9 +291,9 @@ def test_edge_type_style_override(self):
assert dot_output == _.dot_code

assert dot_output == ('digraph {\n'
f' "{self.nodes_ids[0]}" [shape="box", fillcolor="red", style="filled"]\n'
f' "{self.nodes_ids[1]}" [shape="circle", fillcolor="green", style="filled"]\n'
f' "{self.nodes_ids[2]}" [shape="circle", fillcolor="green", style="filled"]\n'
f' "{self.nodes_ids[0]}" [fillcolor="red", shape="box", style="filled"]\n'
f' "{self.nodes_ids[1]}" [fillcolor="green", shape="circle", style="filled"]\n'
f' "{self.nodes_ids[2]}" [fillcolor="green", shape="circle", style="filled"]\n'
f' "{self.nodes_ids[0]}" -> "{self.nodes_ids[1]}"\n'
f' "{self.nodes_ids[0]}" -> "{self.nodes_ids[2]}"\n'
'}')
Expand All @@ -312,7 +312,7 @@ class Custom_Edge_Type(Schema__MGraph__Edge): pass
assert dot_output == self.exporter.dot_code

assert dot_output == ('digraph {\n'
f' "{self.nodes_ids[0]}" [fillcolor="red", style="filled", fontsize="14"]\n'
f' "{self.nodes_ids[0]}" [fillcolor="red", fontsize="14", style="filled"]\n'
f' "{self.nodes_ids[1]}"\n'
f' "{self.nodes_ids[2]}"\n'
f' "{self.nodes_ids[0]}" -> "{self.nodes_ids[1]}"\n'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def tearDown(self):
(_.export().export_dot()
# Basic node and edge display configuration
.show_node__value()
#.show_node__type()
.show_edge__type()
.show_edge__ids()
#.show_node__type_full_name()
#.show_edge__type()
#.show_edge__ids()
.set_node__font__size(12) # Slightly larger font for better readability
.set_node__font__name('Arial')
.set_edge__font__name('Arial')
Expand All @@ -62,7 +62,7 @@ def tearDown(self):
.set_node__type_fill_color(Schema__MGraph__Node__Time_Point, '#1a365d')
.set_node__type_font_color(Schema__MGraph__Node__Time_Point, 'white')
.set_node__type_shape (Schema__MGraph__Node__Time_Point, 'box')
.set_node__type_style (Schema__MGraph__Node__Time_Point, 'rounded')
.set_node__type_rounded (Schema__MGraph__Node__Time_Point)

# Time component styling
.set_node__type_fill_color(Schema__MGraph__Node__Value__Int, '#D8E6F3') # Soft blue for time values
Expand Down

0 comments on commit e3c8638

Please sign in to comment.