muMLE/concrete_syntax/textual_od/renderer.py

38 lines
No EOL
1.3 KiB
Python

# Renderer for Object Diagrams textual concrete syntax
from services import od
from concrete_syntax.common import display_value
def render_od(state, m_id, mm_id, hide_names=True):
output = ""
m_od = od.OD(mm_id, m_id, state)
def display_name(name: str):
# object names that start with "__" are hidden
return name if (name[0:2] != "__" or not hide_names) else ""
def write_attributes(object_node):
o = ""
slots = m_od.get_slots(object_node)
if len(slots) > 0:
o += " {"
for attr_name, slot_node in slots:
value, type_name = m_od.read_slot(slot_node)
o += f"\n {attr_name} = {display_value(value, type_name, indentation=4)};"
o += "\n}"
return o
for class_name, objects in m_od.get_all_objects().items():
for object_name, object_node in objects.items():
output += f"\n{display_name(object_name)}:{class_name}"
output += write_attributes(object_node)
for assoc_name, links in m_od.get_all_links().items():
for link_name, (link_edge, src_name, tgt_name) in links.items():
output += f"\n{display_name(link_name)}:{assoc_name} ({src_name} -> {tgt_name})"
# links can also have slots:
output += write_attributes(link_edge)
return output