@@ -267,6 +267,71 @@ public void readXMLPruneNodesResetRoots() {
267267 assertEquals (SemanticGraph .valueOf ("[C-2]" ), pruneSG );
268268 }
269269
270+ /**
271+ * Check that cutting a graph with two nodes into two pieces, then
272+ * pruning any disjoint pieces, results in a graph with just the root
273+ */
274+ @ Test
275+ public void readXMLKillNonRootedIterate () {
276+ Ssurgeon inst = Ssurgeon .inst ();
277+
278+ String cut = String .join (newline ,
279+ "<ssurgeon-pattern-list>" ,
280+ " <ssurgeon-pattern>" ,
281+ " <uid>38</uid>" ,
282+ " <notes>Remove dep edges</notes>" ,
283+ " <semgrex>" + XMLUtils .escapeXML ("{}=a1 > {}=a2" ) + "</semgrex>" ,
284+ " <edit-list>removeEdge -gov a1 -dep a2 -reln dep</edit-list>" ,
285+ " </ssurgeon-pattern>" ,
286+ "</ssurgeon-pattern-list>" );
287+ List <SsurgeonPattern > patterns = inst .readFromString (cut );
288+ assertEquals (patterns .size (), 1 );
289+ SsurgeonPattern ssurgeonCut = patterns .get (0 );
290+
291+ String prune = String .join (newline ,
292+ "<ssurgeon-pattern-list>" ,
293+ " <ssurgeon-pattern>" ,
294+ " <uid>38</uid>" ,
295+ " <notes>Match every graph, kill unrooted nodes</notes>" ,
296+ " <semgrex>" + XMLUtils .escapeXML ("{$}" ) + "</semgrex>" ,
297+ " <edit-list>killNonRooted</edit-list>" ,
298+ " </ssurgeon-pattern>" ,
299+ "</ssurgeon-pattern-list>" );
300+ patterns = inst .readFromString (prune );
301+ assertEquals (patterns .size (), 1 );
302+ SsurgeonPattern ssurgeonPrune = patterns .get (0 );
303+
304+ // Test a two node only version
305+ SemanticGraph sg = SemanticGraph .valueOf ("[A dep> B]" );
306+ SemanticGraph cutSG = ssurgeonCut .iterate (sg );
307+ assertEquals (cutSG .vertexSet ().size (), 2 );
308+ SemanticGraph pruneSG = ssurgeonPrune .iterate (cutSG );
309+ SemanticGraph expected = SemanticGraph .valueOf ("[A]" );
310+ assertEquals (pruneSG , expected );
311+
312+ // Test a chain cut at the start
313+ sg = SemanticGraph .valueOf ("[A dep> [B obj> C]]" );
314+ cutSG = ssurgeonCut .iterate (sg );
315+ assertEquals (cutSG .vertexSet ().size (), 3 );
316+ pruneSG = ssurgeonPrune .iterate (cutSG );
317+ assertEquals (pruneSG , expected );
318+
319+ // Test the chain cut at the bottom
320+ sg = SemanticGraph .valueOf ("[A obj> [B dep> C]]" );
321+ cutSG = ssurgeonCut .iterate (sg );
322+ assertEquals (cutSG .vertexSet ().size (), 3 );
323+ pruneSG = ssurgeonPrune .iterate (cutSG );
324+ assertEquals (pruneSG , SemanticGraph .valueOf ("[A obj> B]" ));
325+
326+ // Test a chain cut at the start
327+ // Only the root will be left at the end
328+ sg = SemanticGraph .valueOf ("[A dep> B dep> C]" );
329+ cutSG = ssurgeonCut .iterate (sg );
330+ assertEquals (cutSG .vertexSet ().size (), 3 );
331+ pruneSG = ssurgeonPrune .iterate (cutSG );
332+ assertEquals (pruneSG , expected );
333+ }
334+
270335 /**
271336 * Simple test of an Ssurgeon edit script. This instances a simple semantic graph,
272337 * a semgrex pattern, and then the resulting actions over the named nodes in the
0 commit comments