Skip to content

Commit 820144f

Browse files
committed
Bomb-proof the RelabelNamedEdge operation
1 parent 9ea4f39 commit 820144f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public boolean evaluate(SemanticGraph sg, SemgrexMatcher sm) {
6161
SemanticGraphEdge edge = sm.getEdge(edgeName);
6262

6363
if (edge != null) {
64+
// if the edge is already named what we want, then our work here is done
65+
// this bomb-proofs the operation in the event someone writes an edit that
66+
// generically changes any edge to nsubj, for example
67+
if (edge.getRelation().equals(this.relation)) {
68+
return false;
69+
}
6470
boolean success = sg.removeEdge(edge);
6571
if (!success) {
6672
// maybe it was already removed somehow by a previous operation

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,33 @@ public void readXMLRelabelEdgeIterate() {
228228
assertEquals(newSg, expected);
229229
}
230230

231+
/**
232+
* Test that the relabel named edge operation is bomb-proof.
233+
* The initial version would relabel an edge to the new edge name
234+
* even if the existing edge had the same name
235+
*/
236+
@Test
237+
public void readXMLRelabelEdgeBombProof() {
238+
String doc = String.join(newline,
239+
"<ssurgeon-pattern-list>",
240+
" <ssurgeon-pattern>",
241+
" <uid>38</uid>",
242+
" <notes>This is a simple test of RelabelNamedEdge</notes>",
243+
" <semgrex>" + XMLUtils.escapeXML("{}=a1 >=foo {}=a2") + "</semgrex>",
244+
" <edit-list>relabelNamedEdge -edge foo -reln dep</edit-list>",
245+
" </ssurgeon-pattern>",
246+
"</ssurgeon-pattern-list>");
247+
Ssurgeon inst = Ssurgeon.inst();
248+
List<SsurgeonPattern> patterns = inst.readFromString(doc);
249+
assertEquals(patterns.size(), 1);
250+
SsurgeonPattern pattern = patterns.get(0);
251+
252+
// check a simple case of relabeling
253+
SemanticGraph sg = SemanticGraph.valueOf("[A-0 obj> B-1]");
254+
SemanticGraph expected = SemanticGraph.valueOf("[A-0 dep> B-1]");
255+
SemanticGraph newSg = pattern.iterate(sg);
256+
assertEquals(newSg, expected);
257+
}
231258

232259
/**
233260
* Test that the RelabelNamedEdge operation updates the name of the edge in the SemgrexMatcher

0 commit comments

Comments
 (0)