Skip to content

Commit 45f8c94

Browse files
committed
Brief test that killAllIncomingEdges does what we expect. Use a list instead of an iterator to find the edges to be deleted (to ensure the iteration doesn't mess up the maps while iterating)
1 parent 1b9203e commit 45f8c94

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/KillAllIncomingEdges.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public boolean evaluate(SemanticGraph sg, SemgrexMatcher sm) {
3131
return false;
3232
}
3333
boolean success = false;
34-
for (SemanticGraphEdge edge : sg.incomingEdgeIterable(tgtNode)) {
34+
// use incomingEdgeList so that deleting an edge
35+
// doesn't affect the iteration
36+
for (SemanticGraphEdge edge : sg.incomingEdgeList(tgtNode)) {
3537
success = success || sg.removeEdge(edge);
3638
}
3739
return success;

test/src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/SsurgeonTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package edu.stanford.nlp.semgraph.semgrex.ssurgeon;
23

34
import static org.junit.Assert.*;
@@ -408,6 +409,31 @@ public void readXMLKillNonRootedIterate() {
408409
assertEquals(pruneSG, expected);
409410
}
410411

412+
@Test
413+
public void readXMLKillIncomingEdges() {
414+
Ssurgeon inst = Ssurgeon.inst();
415+
416+
String cut = String.join(newline,
417+
"<ssurgeon-pattern-list>",
418+
" <ssurgeon-pattern>",
419+
" <uid>38</uid>",
420+
" <notes>Remove all incoming edges for a node</notes>",
421+
" <semgrex>" + XMLUtils.escapeXML("{}=a1 >dep {}=a2") + "</semgrex>",
422+
" <edit-list>killAllIncomingEdges -node a2</edit-list>",
423+
" </ssurgeon-pattern>",
424+
"</ssurgeon-pattern-list>");
425+
List<SsurgeonPattern> patterns = inst.readFromString(cut);
426+
assertEquals(patterns.size(), 1);
427+
SsurgeonPattern ssurgeonCut = patterns.get(0);
428+
429+
// Test a two node only version
430+
SemanticGraph sg = SemanticGraph.valueOf("[A dep> B]");
431+
SemanticGraph cutSG = ssurgeonCut.iterate(sg);
432+
assertEquals(2, cutSG.vertexSet().size());
433+
cutSG.resetRoots();
434+
assertEquals(2, cutSG.getRoots().size());
435+
}
436+
411437
/**
412438
* Simple test of an Ssurgeon edit script. This instances a simple semantic graph,
413439
* a semgrex pattern, and then the resulting actions over the named nodes in the

0 commit comments

Comments
 (0)