Commit fd2a47ea authored by Jan Pöppel's avatar Jan Pöppel
Browse files

added change_node_name method and some first tests for it

parent d491928f
......@@ -89,7 +89,25 @@ class BayesianNetwork(object):
Will have to modify all occurances of the old name.
"""
if oldName in self.node_lookup:
pass
n = self.node_lookup[oldName]
children = list(self.graph.succ[n])
for child in children:
del child.parents[oldName]
child.parents[newName] = n
idx = child.parentOrder.index(oldName)
child.parentOrder[idx] = newName
#Fix nx graph
self.graph.remove_node(n)
n.name = newName
del self.node_lookup[oldName]
self.node_lookup[n] = n
self.graph.add_node(n)
for child in children:
self.graph.add_edge(n, self.node_lookup[child])
else:
raise Exception("There is no node with name {} in the network.".format(oldName))
......
......@@ -80,6 +80,9 @@ class BayesNetTest(unittest.TestCase):
self.assertEqual(str(cm.exception), "There is no node with name Node2 in the BayesianNetwork")
self.assertEqual(n1.parentOrder, ["NewName"])
self.assertTrue(n2 in self.bn.graph.nodes())
self.assertTrue(n2 in self.bn.node_lookup)
self.assertTrue(n1 in self.bn.graph.succ[n2])
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment