Skip to content

Commit da5199e

Browse files
committed
Turn a bunch more exceptions into RuntimeException instead of checked in Ssurgeon
1 parent 59af3e5 commit da5199e

File tree

9 files changed

+40
-13
lines changed

9 files changed

+40
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public String toString() {
118118
* @param sg SemanticGraph to operate over (NOT destroyed/modified).
119119
* @return True if a match was found and executed, otherwise false.
120120
*/
121-
public Collection<SemanticGraph> execute(SemanticGraph sg) throws Exception {
121+
public Collection<SemanticGraph> execute(SemanticGraph sg) {
122122
Collection<SemanticGraph> generated = new ArrayList<>();
123123
SemgrexMatcher matcher = semgrexPattern.matcher(sg);
124124
nextMatch:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package edu.stanford.nlp.semgraph.semgrex.ssurgeon;
2+
3+
/**
4+
* A runtime exception that indicates something went wrong executing a
5+
* Ssurgeon expression.
6+
*
7+
* @author John Bauer
8+
*/
9+
public class SsurgeonRuntimeException extends RuntimeException {
10+
11+
private static final long serialVersionUID = -278683457698L;
12+
13+
public SsurgeonRuntimeException(String message) {
14+
super(message);
15+
}
16+
17+
public SsurgeonRuntimeException(String message, Throwable cause) {
18+
super(message, cause);
19+
}
20+
21+
}

src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/pred/NodeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public abstract class NodeTest implements SsurgPred {
1515

1616
public NodeTest(String matchName) { this.matchName = matchName; }
1717

18-
public boolean test(SemgrexMatcher matcher) throws Exception { return evaluate(matcher.getNode(matchName)); }
18+
public boolean test(SemgrexMatcher matcher) { return evaluate(matcher.getNode(matchName)); }
1919

2020
// This is the custom routine to implement
21-
protected abstract boolean evaluate(IndexedWord node) throws Exception;
21+
protected abstract boolean evaluate(IndexedWord node);
2222

2323
// Use this for debugging, and dual re-use of the code outside of Ssurgeon
24-
public boolean test(IndexedWord node) throws Exception {
24+
public boolean test(IndexedWord node) {
2525
return evaluate(node);
2626
}
2727

src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/pred/SsurgAndPred.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class SsurgAndPred extends ArrayList<SsurgPred> implements SsurgPred {
1313
*/
1414
private static final long serialVersionUID = 760573332472162149L;
1515

16-
public boolean test(SemgrexMatcher matcher) throws Exception {
16+
public boolean test(SemgrexMatcher matcher) {
1717
for (SsurgPred term : this) {
1818
if (term.test(matcher) == false)
1919
return false;

src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/pred/SsurgOrPred.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class SsurgOrPred extends ArrayList<SsurgPred> implements SsurgPred {
1212
*/
1313
private static final long serialVersionUID = 4581463857927967518L;
1414

15-
public boolean test(SemgrexMatcher matcher) throws Exception {
15+
public boolean test(SemgrexMatcher matcher) {
1616
for (SsurgPred term : this) {
1717
if (term.test(matcher))
1818
return true;

src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/pred/SsurgPred.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
public interface SsurgPred {
66
// Given the current setup (each of the args in place), what is the truth value?
7-
public boolean test(SemgrexMatcher matched) throws Exception;
7+
public boolean test(SemgrexMatcher matched);
88
}

src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/pred/SsurgTestManager.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.*;
44

5+
import edu.stanford.nlp.semgraph.semgrex.ssurgeon.SsurgeonRuntimeException;
56
import edu.stanford.nlp.util.Generics;
67

78
/**
@@ -39,8 +40,12 @@ public void registerNodeTest(NodeTest nodeTestObj) {
3940
* of the given NodeTest, otherwise throws an exception if not available.
4041
* @throws Exception
4142
*/
42-
public NodeTest getNodeTest(String id, String matchName) throws Exception {
43-
NodeTest test = (NodeTest) nodeTests.get(id).getConstructor(String.class).newInstance(matchName);
44-
return test;
43+
public NodeTest getNodeTest(String id, String matchName) {
44+
try {
45+
NodeTest test = (NodeTest) nodeTests.get(id).getConstructor(String.class).newInstance(matchName);
46+
return test;
47+
} catch (ReflectiveOperationException e) {
48+
throw new SsurgeonRuntimeException("Could not create a new instance of test " + id, e);
49+
}
4550
}
4651
}

src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/pred/WordlistTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import edu.stanford.nlp.ling.IndexedWord;
44
import edu.stanford.nlp.semgraph.semgrex.ssurgeon.Ssurgeon;
5+
import edu.stanford.nlp.semgraph.semgrex.ssurgeon.SsurgeonRuntimeException;
56
import edu.stanford.nlp.semgraph.semgrex.ssurgeon.SsurgeonWordlist;
67

78
public class WordlistTest extends NodeTest {
@@ -23,10 +24,10 @@ public WordlistTest(String myID, String resourceID, String type, String matchNam
2324
* Checks to see if the given node's field matches the resource
2425
*/
2526
@Override
26-
protected boolean evaluate(IndexedWord node) throws Exception {
27+
protected boolean evaluate(IndexedWord node) {
2728
SsurgeonWordlist wl = Ssurgeon.inst().getResource(resourceID);
2829
if (wl == null) {
29-
throw new Exception("No wordlist resource with ID="+resourceID);
30+
throw new SsurgeonRuntimeException("No wordlist resource with ID="+resourceID);
3031
}
3132
if (type == TYPE.lemma)
3233
return wl.contains(node.lemma().toLowerCase());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class SsurgeonTest {
2020
* semgrex match.
2121
*/
2222
@Test
23-
public void simpleTest() throws Exception {
23+
public void simpleTest() {
2424
SemanticGraph sg = SemanticGraph.valueOf("[mixed/VBN nsubj>[Joe/NNP appos>[bartender/NN det>the/DT]] obj>[drink/NN det>a/DT]]");
2525
SemgrexPattern semgrexPattern = SemgrexPattern.compile("{}=a1 >appos=e1 {}=a2 <nsubj=e2 {}=a3");
2626
SsurgeonPattern pattern = new SsurgeonPattern(semgrexPattern);

0 commit comments

Comments
 (0)