merging (meta-)models works (but it's dirty!)

This commit is contained in:
Joeri Exelmans 2024-12-03 17:58:03 +01:00
parent c31c8bf3ea
commit 9883e09ac2
17 changed files with 474 additions and 46 deletions

View file

@ -30,8 +30,10 @@ BOOL: "True" | "False"
CODE: /`[^`]*`/
INDENTED_CODE: /```[^`]*```/
type_name: IDENTIFIER
# name (optional) type
object: [IDENTIFIER] ":" IDENTIFIER [link_spec] ["{" slot* "}"]
object: [IDENTIFIER] ":" type_name [link_spec] ["{" slot* "}"]
link_spec: "(" IDENTIFIER "->" IDENTIFIER ")"
@ -41,7 +43,8 @@ slot: IDENTIFIER "=" literal ";"
parser = Lark(grammar, parser='lalr')
# given a concrete syntax text string, and a meta-model, parses the CS
def parse_od(state, m_text, mm):
# Parameter 'type_transform' is useful for adding prefixes to the type names, when parsing a model and pretending it is an instance of a prefixed meta-model.
def parse_od(state, m_text, mm, type_transform=lambda type_name: type_name):
tree = parser.parse(m_text)
m = state.create_node()
@ -60,6 +63,13 @@ def parse_od(state, m_text, mm):
def link_spec(self, el):
[src, tgt] = el
return (src, tgt)
def type_name(self, el):
type_name = el[0]
if type_name in primitive_types:
return type_name
else:
return type_transform(el[0])
def slot(self, el):
[attr_name, value] = el
@ -69,7 +79,10 @@ def parse_od(state, m_text, mm):
[obj_name, type_name, link] = el[0:3]
slots = el[3:]
if state.read_dict(m, obj_name) != None:
raise Exception(f"Element '{obj_name}:{type_name}': name '{obj_name}' already in use. Object names must be unique.")
msg = f"Element '{obj_name}:{type_name}': name '{obj_name}' already in use."
# raise Exception(msg + " Names must be unique")
print(msg + " Ignoring.")
return
if obj_name == None:
# object/link names are optional
# generate a unique name if no name given

View file

@ -9,7 +9,7 @@ def render_od(state, m_id, mm_id, hide_names=True):
m_od = od.OD(mm_id, m_id, state)
serialized = set()
serialized = set(["Integer", "String", "Boolean", "ActionCode"]) # assume these types always already exist
def display_name(name: str):
# object names that start with "__" are hidden
@ -28,15 +28,17 @@ def render_od(state, m_id, mm_id, hide_names=True):
for class_name, objects in m_od.get_all_objects().items():
for object_name, object_node in objects.items():
if class_name == "ModelRef":
continue # skip modelrefs, they fuckin ma shit up
output += f"\n{display_name(object_name)}:{class_name}"
output += write_attributes(object_node)
serialized.add(object_name)
links = m_od.get_all_links()
todo_links = m_od.get_all_links()
while len(links) != 0:
while len(todo_links) != 0:
postponed = {}
for assoc_name, links in links.items():
for assoc_name, links in todo_links.items():
for link_name, (link_edge, src_name, tgt_name) in links.items():
if link_name in serialized:
continue
@ -47,6 +49,8 @@ def render_od(state, m_id, mm_id, hide_names=True):
# links can also have slots:
output += write_attributes(link_edge)
serialized.add(link_name)
links = postponed
if len(postponed) == len(todo_links):
raise Exception(f"We got stuck! Links = {postponed}")
todo_links = postponed
return output