Skip to content

Commit

Permalink
Fix node renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
mph- committed Feb 26, 2024
1 parent 6b0bc7a commit 52b966e
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 8 deletions.
116 changes: 110 additions & 6 deletions lcapy/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,122 @@ def name(self):
@name.setter
def name(self, name):
"""Rename node name."""
# There are two cases:
# 1. The node name is new so just have a simple rename
# 2. The node name exists so need to rename and update connected
# components.

return self.rename(name)

def _rename1(self, name):
# Case 1. The name is new and is applied to all components

if name in self.cct.nodes:
self._connected.extend(self.cct.nodes[name]._connected)
self._count += self.cct.nodes[name]._count
raise ValueError('Node %s is not new' % name)

self.cct.nodes[name] = self.cct.nodes.pop(self._name)
self._name = name

def _rename2(self, name):
# Case 2. The name is not new and is applied to all components

if name not in self.cct.nodes:
raise ValueError('Node %s is new' % name)

self._connected.extend(self.cct.nodes[name]._connected)
self._count += self.cct.nodes[name]._count

self.cct.nodes[name] = self.cct.nodes.pop(self._name)
self._name = name

def _rename3(self, name, cpts):
# Case 3. The name is new and is applied to some of the components

if name in self.cct.nodes:
raise ValueError('Node %s is not new' % name)

new_node = Node(self.cct, name)
self.cct.nodes[name] = new_node

for cpt in cpts:
for m, node in enumerate(cpt.nodes):
if node.name == self.name:
if cpt.type not in ('A', 'O'):
self._count -= 1
new_node.append(cpt)
cpt.nodes[m] = new_node

def _rename4(self, name, cpts):
# Case 4. The name is not new and is applied to some of the components

if name not in self.cct.nodes:
raise ValueError('Node %s is new' % name)

new_node = self.cct.nodes[name]
self.cct.nodes[name] = new_node

for cpt in cpts:
for m, node in enumerate(cpt.nodes):
if node.name == self.name:
if cpt.type not in ('A', 'O'):
self._count -= 1
new_node.append(cpt)
cpt.nodes[m] = new_node

def rename(self, name, cpts=None):
"""Rename node name if connected to a component in the list cpts or if
cpts is None."""

# Case 1. The name is new and is applied to all components
# Case 2. The name is not new and is applied to all components
# Case 3. The name is new and is applied to some of the components
# Case 4. The name is not new and is applied to some of the components
#
# all and some applies to the components that are connected
# to this node

if cpts is None:
if name not in self.cct.nodes:
# Case 1.
self._rename1(name)
return
else:
# Case 2.
self._rename2(name)
return

# Convert names to cpts
if not isinstance(cpts, (list, tuple)):
cpts = [cpts]

tcpts = []
for cpt in cpts:
if isinstance(cpt, str):
tcpts.append(self.cct[cpt])
else:
tcpts.append(cpt)
cpts = tcpts

other = []
for cpt in self.connected:
if cpt not in cpts:
other.append(cpt)

if other == []:
# Case 1
if name not in self.cct.nodes:
self._rename1(name)
return

# Case 2
self._rename2(name)
return

# Case 3
if name not in self.cct.nodes:
self._rename3(name, cpts)
return

# Case 4
self._rename4(name, cpts)
return

@property
def x(self):

Expand Down
14 changes: 12 additions & 2 deletions lcapy/tests/test_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,18 @@ def test_nodes(self):
self.assertEqual(a['3'].count, 1, 'connections count')
self.assertEqual(len(a['1'].connected), 1, 'connected count')

# Case 1
a['2'].name = '7'
self.assertEqual(a['7'].count, 2, 'rename new connections count')
self.assertEqual(a['7'].count, 2, 'node rename case1')

# Case 2
a['4'].name = '3'
self.assertEqual(a['3'].count, 2, 'rename existing connections count')
self.assertEqual(a['3'].count, 2, 'node rename case2')

# Case 3
a['3'].rename('4', 'C')
self.assertEqual(a['4'].count, 1, 'node rename case3')

# case 4
a['7'].rename('5', 'R')
self.assertEqual(a['5'].count, 2, 'node rename case4')

0 comments on commit 52b966e

Please sign in to comment.