Skip to content

Commit

Permalink
mps-cli-py: enhance node by enabling parent node
Browse files Browse the repository at this point in the history
  • Loading branch information
MLanghammer committed Jun 10, 2024
1 parent 7150c71 commit eafe6f4
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 6 deletions.
3 changes: 2 additions & 1 deletion mps-cli-py/src/mpscli/model/SNode.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

class SNode:

def __init__(self, uuid, concept, role_in_parent):
def __init__(self, uuid, concept, role_in_parent, parent):
self.uuid = uuid
self.concept = concept
self.role_in_parent = role_in_parent
self.properties = {}
self.references = {}
self.children = []
self.parent = parent

def get_property(self, name):
return self.properties.get(name)
Expand Down
6 changes: 3 additions & 3 deletions mps-cli-py/src/mpscli/model/builder/SModelBuilderBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def __init__(self):
self.index_2_reference_role = {}
self.index_2_imported_model_uuid = {}

def extract_node(self, my_model, node_xml):
def extract_node(self, my_model, node_xml, parent):
root_node_id = node_xml.get("id")
root_node_concept = self.index_2_concept[node_xml.get("concept")]
child_role_index = node_xml.get("role")
if child_role_index is None:
child_role = None
else:
child_role = self.index_2_child_role_in_parent[child_role_index]
s_node = SNode(root_node_id, root_node_concept, child_role)
s_node = SNode(root_node_id, root_node_concept, child_role, parent)
for property_xml_node in node_xml.findall("property"):
property_role = property_xml_node.get("role")
property_value = property_xml_node.get("value")
Expand All @@ -40,7 +40,7 @@ def extract_node(self, my_model, node_xml):
ref_name = self.index_2_reference_role[ref_role]
s_node.references[ref_name] = s_node_ref
for child_node_xml in node_xml.findall("node"):
child_node = self.extract_node(my_model, child_node_xml)
child_node = self.extract_node(my_model, child_node_xml, s_node)
s_node.children.append(child_node)

return s_node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def build(self, path):
self.extract_imports_and_registry(model_xml_node)

for node_xml_node in model_xml_node.findall("node"):
root_node = self.extract_node(model, node_xml_node)
root_node = self.extract_node(model, node_xml_node, None)
model.root_nodes.append(root_node)

return model
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def extract_root_node(self, model, mpsr_file):
model_xml_node = tree.getroot()
self.extract_imports_and_registry(model_xml_node)
root_node = model_xml_node.find("node")
return self.extract_node(model, root_node)
return self.extract_node(model, root_node, None)



2 changes: 2 additions & 0 deletions mps-cli-py/tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_build_root_nodes(self, test_data_location, library_top_authors_top_mode
self.assertEqual('4Yb5JA31NUu', root_node.uuid)
self.assertEqual("mps.cli.landefs.people.structure.PersonsContainer", root_node.concept.name)
self.assertEqual("_010_classical_authors", root_node.properties["name"])
self.assertEqual(None, root_node.parent)

@parameterized.expand([('mps_cli_lanuse_file_per_root',
'mps.cli.lanuse.library_top.library_top'),
Expand All @@ -48,6 +49,7 @@ def test_build_nodes(self, test_data_location, library_top_library_top_model_nam
self.assertEqual("Tom Sawyer", tom_sawyer.get_property("name"))
author_of_tom_sawyer = tom_sawyer.get_children("authors")[0].get_reference("person").resolve(self.repo)
self.assertEqual("Mark Twain", author_of_tom_sawyer.get_property("name"))
self.assertEqual(root_node, tom_sawyer.parent)

def get_name(node):
return node.get_property("name")
Expand Down

0 comments on commit eafe6f4

Please sign in to comment.