|
1 | 1 | package edu.stanford.nlp.semgraph.semgrex;
|
2 | 2 |
|
| 3 | +import edu.stanford.nlp.graph.CyclicGraphException; |
3 | 4 | import edu.stanford.nlp.semgraph.SemanticGraph;
|
4 | 5 | import edu.stanford.nlp.ling.*;
|
5 | 6 | import edu.stanford.nlp.util.logging.Redwood;
|
@@ -117,67 +118,36 @@ public boolean matchesAt(IndexedWord node) {
|
117 | 118 |
|
118 | 119 |
|
119 | 120 | /**
|
120 |
| - * Topological sorting actually takes a rather large amount of time, if you call multiple |
121 |
| - * patterns on the same tree. |
122 |
| - * This is a weak cache that stores all the trees sorted since the garbage collector last kicked in. |
123 |
| - * The key on this map is the identity hash code (i.e., memory address) of the semantic graph; the |
124 |
| - * value is the sorted list of vertices. |
125 |
| - * <p> |
126 |
| - * Note that this optimization will cause strange things to happen if you mutate a semantic graph between |
127 |
| - * calls to Semgrex. |
| 121 | + * Find the next match of the pattern in the graph. |
| 122 | + * |
| 123 | + * @return whether there is a match somewhere in the graph |
128 | 124 | */
|
129 |
| - private static final WeakHashMap<Integer, List<IndexedWord>> topologicalSortCache = new WeakHashMap<>(); |
130 |
| - |
131 |
| - private void setupFindIterator() { |
132 |
| - try { |
| 125 | + public boolean find() { |
| 126 | + // log.info("hyp: " + hyp); |
| 127 | + // there was a cache of the topological sorts to reuse across |
| 128 | + // SemgrexPatterns which used IdentityHashMap to remember |
| 129 | + // SemanticGraphs, but it was apparently the cause of various |
| 130 | + // thread safety bugs when the results were used for an old |
| 131 | + // SemanticGraph |
| 132 | + if (findIterator == null) { |
133 | 133 | if (hyp) {
|
134 |
| - synchronized (topologicalSortCache) { |
135 |
| - List<IndexedWord> topoSort = topologicalSortCache.get(System.identityHashCode(sg)); |
136 |
| - if (topoSort == null || topoSort.size() != sg.size()) { // size check to mitigate a stale cache |
137 |
| - topoSort = sg.topologicalSort(); |
138 |
| - topologicalSortCache.put(System.identityHashCode(sg), topoSort); |
139 |
| - } |
140 |
| - findIterator = topoSort.iterator(); |
| 134 | + try { |
| 135 | + findIterator = sg.topologicalSort().iterator(); |
| 136 | + } catch (CyclicGraphException e) { |
| 137 | + findIterator = sg.vertexSet().iterator(); |
141 | 138 | }
|
142 | 139 | } else if (sg_aligned == null) {
|
143 |
| - return; |
| 140 | + return false; |
144 | 141 | } else {
|
145 |
| - synchronized (topologicalSortCache) { |
146 |
| - List<IndexedWord> topoSort = topologicalSortCache.get(System.identityHashCode(sg_aligned)); |
147 |
| - if (topoSort == null || topoSort.size() != sg_aligned.size()) { // size check to mitigate a stale cache |
148 |
| - topoSort = sg_aligned.topologicalSort(); |
149 |
| - topologicalSortCache.put(System.identityHashCode(sg_aligned), topoSort); |
150 |
| - } |
151 |
| - findIterator = topoSort.iterator(); |
| 142 | + try { |
| 143 | + findIterator = sg_aligned.topologicalSort().iterator(); |
| 144 | + } catch (CyclicGraphException e) { |
| 145 | + findIterator = sg_aligned.vertexSet().iterator(); |
152 | 146 | }
|
153 | 147 | }
|
154 |
| - } catch (Exception ex) { |
155 |
| - if (hyp) { |
156 |
| - findIterator = sg.vertexSet().iterator(); |
157 |
| - } else if (sg_aligned == null) { |
158 |
| - return; |
159 |
| - } else { |
160 |
| - findIterator = sg_aligned.vertexSet().iterator(); |
161 |
| - } |
162 | 148 | }
|
163 |
| - } |
164 | 149 |
|
165 |
| - /** |
166 |
| - * Find the next match of the pattern in the graph. |
167 |
| - * |
168 |
| - * @return whether there is a match somewhere in the graph |
169 |
| - */ |
170 |
| - public boolean find() { |
171 |
| - // log.info("hyp: " + hyp); |
172 |
| - if (findIterator == null) { |
173 |
| - setupFindIterator(); |
174 |
| - } |
175 |
| - if (findIterator == null) { |
176 |
| - return false; |
177 |
| - } |
178 |
| - // System.out.println("first"); |
179 | 150 | if (findCurrent != null && matches()) {
|
180 |
| - // log.info("find first: " + findCurrent.word()); |
181 | 151 | return true;
|
182 | 152 | }
|
183 | 153 | //log.info("here");
|
|
0 commit comments