Skip to content

Commit

Permalink
Proper (?) fix for merging a newly-introduced left-hand node.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgritter committed Dec 1, 2019
1 parent 38056ae commit 359e104
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
15 changes: 11 additions & 4 deletions soffit/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,17 +591,24 @@ def _mergeNodes( self, g ):
# join has just the nodes that need to be changed
# rename has the canonical renaming
alreadyMerged = set()

for v in self.join:
u = self.rename[ v ]
m_u = self.match.node( u )
try:
m_u = self.match.node( u )
m_v = self.match.node( v )
except KeyError:
# A merge was specified for a node introduced in the right-hand
# side of the rule.
assert v not in self.left.node
continue
# Add an alias as if it were specified in the left-hand side.
if u not in self.left:
self.match.addMap( u, self.match.node( v ) )
continue
elif v not in self.left:
self.match.addMap( v, self.match.node( u ) )
continue
else:
assert False

# Matching could have identified two nodes that are both to be merged
# or are already merged!
Expand Down
19 changes: 19 additions & 0 deletions test/test_rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ def test_merge_new(self):
self.assertIn( (n_3,n_4), g2.edges )
self.assertIn( (n_2,n_3), g2.edges )

def test_merge_two_new(self):
self.perform_rewrite( l = "A[1]; B[2]",
r = "A^C[3]; D^B[4]; C--D",
g = "A[1]; B[2]; G[5]; A--G; B--G" )
g2 = self.after
self.assertEqual( len( g2.nodes ), 3 )
self.assertEqual( len( g2.edges ), 3 )

( n_1, n_2, n_3, n_4, n_5 ) = self.find_any_tags( g2, '1', '2', '3', '4', '5' )

self.assertIsNone( n_1 )
self.assertIsNone( n_2 )
self.assertIsNotNone( n_3 )
self.assertIsNotNone( n_4 )
self.assertIsNotNone( n_5 )
self.assertIn( (n_3,n_4), g2.edges )
self.assertIn( (n_3,n_5), g2.edges )
self.assertIn( (n_4,n_5), g2.edges )

def test_two_merges(self):
# The merged edge could get *either* tag, but it must get one.
self.perform_rewrite( l = "A[1]; B[2]; C[3]; D[4];",
Expand Down

0 comments on commit 359e104

Please sign in to comment.