Skip to content

Commit edaaf83

Browse files
committed
Add an exception for a missing node name to setRoots. Test the results of some basic setRoots operations
1 parent e94c812 commit edaaf83

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ public SetRoots(List<String> newRootNames) {
2626
@Override
2727
public boolean evaluate(SemanticGraph sg, SemgrexMatcher sm) {
2828
Set<IndexedWord> newRoots = new LinkedHashSet<>();
29-
for (String name : newRootNames)
30-
newRoots.add(getNamedNode(name, sm));
29+
for (String name : newRootNames) {
30+
IndexedWord root = getNamedNode(name, sm);
31+
if (root == null) {
32+
throw new SsurgeonRuntimeException("Ssurgeon rule tried to set root to " + name + " but that name does not exist in the semgrex results");
33+
}
34+
newRoots.add(root);
35+
}
3136
if (newRoots.equals(sg.getRoots()))
3237
return false;
3338
sg.setRoots(newRoots);

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,68 @@ public void readXMLKillIncomingEdges() {
434434
assertEquals(2, cutSG.getRoots().size());
435435
}
436436

437+
/**
438+
* Check the result of rearranging an edge and then setting the root to a new label
439+
*/
440+
@Test
441+
public void readXMLSetRoots() {
442+
Ssurgeon inst = Ssurgeon.inst();
443+
444+
String cut = String.join(newline,
445+
"<ssurgeon-pattern-list>",
446+
" <ssurgeon-pattern>",
447+
" <uid>38</uid>",
448+
" <notes>Test the effects of setRoots on a simple change</notes>",
449+
" <semgrex>" + XMLUtils.escapeXML("{word:A}=a >dep=dep {word:B}=b") + "</semgrex>",
450+
" <edit-list>removeNamedEdge -edge dep</edit-list>",
451+
" <edit-list>addEdge -gov b -dep a -reln dep</edit-list>",
452+
" <edit-list>setRoots b</edit-list>",
453+
" </ssurgeon-pattern>",
454+
"</ssurgeon-pattern-list>");
455+
List<SsurgeonPattern> patterns = inst.readFromString(cut);
456+
assertEquals(patterns.size(), 1);
457+
SsurgeonPattern rearrange = patterns.get(0);
458+
459+
// Test a two node only version
460+
SemanticGraph sg = SemanticGraph.valueOf("[A-1 dep> B-2]");
461+
SemanticGraph newSG = rearrange.iterate(sg);
462+
SemanticGraph expected = SemanticGraph.valueOf("[B-2 dep> A-1]");
463+
assertEquals(expected, newSG);
464+
}
465+
466+
467+
/**
468+
* Check that a readable exception is thrown if the expected node doesn't exist for setRoots
469+
*/
470+
@Test
471+
public void readXMLSetRootsException() {
472+
Ssurgeon inst = Ssurgeon.inst();
473+
474+
String cut = String.join(newline,
475+
"<ssurgeon-pattern-list>",
476+
" <ssurgeon-pattern>",
477+
" <uid>38</uid>",
478+
" <notes>Remove all incoming edges for a node</notes>",
479+
" <semgrex>" + XMLUtils.escapeXML("{word:A}=a >dep~dep {word:B}=b") + "</semgrex>",
480+
" <edit-list>removeNamedEdge -edge dep</edit-list>",
481+
" <edit-list>addEdge -gov b -dep a -reln dep</edit-list>",
482+
" <edit-list>setRoots c</edit-list>",
483+
" </ssurgeon-pattern>",
484+
"</ssurgeon-pattern-list>");
485+
List<SsurgeonPattern> patterns = inst.readFromString(cut);
486+
assertEquals(patterns.size(), 1);
487+
SsurgeonPattern rearrange = patterns.get(0);
488+
489+
// Test a two node only version
490+
SemanticGraph sg = SemanticGraph.valueOf("[A-1 dep> B-2]");
491+
try {
492+
SemanticGraph newSG = rearrange.iterate(sg);
493+
throw new AssertionError("Expected a specific exception SsurgeonRuntimeException here");
494+
} catch (SsurgeonRuntimeException e) {
495+
// yay
496+
}
497+
}
498+
437499
/**
438500
* Simple test of an Ssurgeon edit script. This instances a simple semantic graph,
439501
* a semgrex pattern, and then the resulting actions over the named nodes in the

0 commit comments

Comments
 (0)