blob: 80497820703f41e5b6b78c1d820b623ea6e52fc2 [file] [log] [blame]
Eliza Margarethaa2603fa2014-01-22 10:59:25 +00001package de.ids_mannheim.korap.query.spans;
2
3import java.io.IOException;
Eliza Margarethad28469f2014-03-10 12:42:21 +00004import java.util.ArrayList;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +00005import java.util.Collection;
6
7import org.apache.lucene.search.spans.Spans;
8
Eliza Margaretha609a5be2014-12-18 16:52:20 +00009/**
Nils Diewaldbb33da22015-03-04 16:24:25 +000010 * CandidateSpan stores the current state of a Lucene {@link Spans},
margarethac66265c2016-12-14 13:48:45 +010011 * which is an enumeration. CandidateSpan is used for various
12 * purposes, such as for collecting spans which will be used in a
13 * latter process or next matching.
Eliza Margaretha609a5be2014-12-18 16:52:20 +000014 *
15 * @author margaretha
Eliza Margaretha6f989202016-10-14 21:48:29 +020016 */
Eliza Margaretha609a5be2014-12-18 16:52:20 +000017public class CandidateSpan implements Comparable<CandidateSpan>, Cloneable {
Nils Diewaldbb33da22015-03-04 16:24:25 +000018 protected int doc, start, end;
Eliza Margaretha609a5be2014-12-18 16:52:20 +000019 private long cost;
margaretha50c76332015-03-19 10:10:39 +010020 private Collection<byte[]> payloads;
Eliza Margaretha609a5be2014-12-18 16:52:20 +000021 private int position;
margarethac66265c2016-12-14 13:48:45 +010022
23 // Child spans are used in multiple distance queries with unordered constraint
24 private CandidateSpan childSpan;
25 private CandidateSpan secondChildSpan;
26
Akron42993552016-02-04 13:24:24 +010027 protected short spanId;
28 protected boolean hasSpanId;
margaretha10da63e2015-12-18 15:42:52 +010029
Nils Diewaldbb33da22015-03-04 16:24:25 +000030 private short leftId, rightId;
31 private int leftStart, leftEnd;
32 private int rightStart, rightEnd;
33
Akron42993552016-02-04 13:24:24 +010034 protected byte payloadTypeIdentifier;
35
Eliza Margaretha9738c392014-02-03 17:04:53 +000036
Eliza Margaretha609a5be2014-12-18 16:52:20 +000037 /**
38 * Constructs a CandidateSpan for the given Span.
39 *
Nils Diewaldbb33da22015-03-04 16:24:25 +000040 * @param span
41 * a Span
Eliza Margaretha609a5be2014-12-18 16:52:20 +000042 * @throws IOException
43 */
Nils Diewaldbb33da22015-03-04 16:24:25 +000044 public CandidateSpan (Spans span) throws IOException {
Eliza Margaretha609a5be2014-12-18 16:52:20 +000045 this.doc = span.doc();
46 this.start = span.start();
47 this.end = span.end();
48 this.cost = span.cost();
margaretha50c76332015-03-19 10:10:39 +010049
50 this.payloads = new ArrayList<>();
51 if (span.isPayloadAvailable()) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +000052 setPayloads(span.getPayload());
margaretha50c76332015-03-19 10:10:39 +010053 }
54 if (span instanceof SimpleSpans) {
55 SimpleSpans temp = (SimpleSpans) span;
56 this.spanId = temp.getSpanId();
Akron42993552016-02-04 13:24:24 +010057 this.hasSpanId = temp.hasSpanId;
margaretha50c76332015-03-19 10:10:39 +010058 }
59 else if (span instanceof ClassSpans) {
60 this.spanId = ((ClassSpans) span).getNumber();
Akron42993552016-02-04 13:24:24 +010061 this.hasSpanId = true;
margaretha50c76332015-03-19 10:10:39 +010062 }
Eliza Margaretha609a5be2014-12-18 16:52:20 +000063 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000064
Nils Diewaldbb33da22015-03-04 16:24:25 +000065
Eliza Margaretha609a5be2014-12-18 16:52:20 +000066 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +000067 * Constructs a CandidateSpan for the given Span and element
68 * position (where
69 * the span is included in a document). The element position is
70 * important
Eliza Margaretha609a5be2014-12-18 16:52:20 +000071 * for the matching process in {@link ElementDistanceSpans}.
72 *
Nils Diewaldbb33da22015-03-04 16:24:25 +000073 * @param span
74 * a Span
75 * @param position
76 * an element position
Eliza Margaretha609a5be2014-12-18 16:52:20 +000077 * @throws IOException
78 */
Nils Diewaldbb33da22015-03-04 16:24:25 +000079 public CandidateSpan (Spans span, int position) throws IOException {
Eliza Margaretha609a5be2014-12-18 16:52:20 +000080 this(span);
81 this.position = position;
82 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000083
Nils Diewaldbb33da22015-03-04 16:24:25 +000084
Eliza Margaretha609a5be2014-12-18 16:52:20 +000085 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +000086 * Constructs a CandidateSpan from all the given variables which
87 * are
Eliza Margaretha609a5be2014-12-18 16:52:20 +000088 * properties of a Span.
89 *
Nils Diewaldbb33da22015-03-04 16:24:25 +000090 * @param start
91 * the start position of a span
92 * @param end
93 * the end position of a span
94 * @param doc
95 * the document including the span
96 * @param cost
97 * the cost of finding a span
98 * @param payloads
99 * the payloads of a span
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000100 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000101 public CandidateSpan (int start, int end, int doc, long cost,
102 Collection<byte[]> payloads) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000103 this.start = start;
104 this.end = end;
105 this.doc = doc;
106 this.cost = cost;
107 if (payloads != null)
108 setPayloads(payloads);
109 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +0000110
Nils Diewaldbb33da22015-03-04 16:24:25 +0000111
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000112 @Override
Nils Diewaldbb33da22015-03-04 16:24:25 +0000113 protected CandidateSpan clone () throws CloneNotSupportedException {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000114 return new CandidateSpan(this.start, this.end, this.doc, this.cost,
115 this.payloads);
116 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +0000117
Nils Diewaldbb33da22015-03-04 16:24:25 +0000118
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000119 /**
120 * Returns the document number containing the CandidateSpan.
121 *
122 * @return the document number
123 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000124 public int getDoc () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000125 return doc;
126 }
Eliza Margaretha8e274e32014-01-28 15:09:30 +0000127
Nils Diewaldbb33da22015-03-04 16:24:25 +0000128
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000129 /**
130 * Sets the document number containing the CandidateSpan.
131 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000132 * @param doc
133 * the document number
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000134 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000135 public void setDoc (int doc) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000136 this.doc = doc;
137 }
Eliza Margaretha8e274e32014-01-28 15:09:30 +0000138
Nils Diewaldbb33da22015-03-04 16:24:25 +0000139
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000140 /**
141 * Returns the start position of the CandidateSpan.
142 *
143 * @return the start position
144 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000145 public int getStart () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000146 return start;
147 }
Eliza Margaretha6651fc32014-02-18 14:57:47 +0000148
Nils Diewaldbb33da22015-03-04 16:24:25 +0000149
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000150 /**
151 * Sets the start position of the CandidateSpan.
152 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000153 * @param start
154 * the start position
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000155 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000156 public void setStart (int start) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000157 this.start = start;
158 }
Eliza Margaretha6651fc32014-02-18 14:57:47 +0000159
Nils Diewaldbb33da22015-03-04 16:24:25 +0000160
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000161 /**
162 * Returns the end position of the CandidateSpan.
163 *
164 * @return the end position
165 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000166 public int getEnd () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000167 return end;
168 }
Eliza Margarethae7938d32014-07-29 12:12:15 +0000169
Nils Diewaldbb33da22015-03-04 16:24:25 +0000170
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000171 /**
172 * Sets the end position of the CandidateSpan.
173 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000174 * @param end
175 * the end position
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000176 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000177 public void setEnd (int end) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000178 this.end = end;
179 }
Eliza Margarethae7938d32014-07-29 12:12:15 +0000180
Nils Diewaldbb33da22015-03-04 16:24:25 +0000181
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000182 /**
183 * Returns the payloads of the CandidateSpan.
184 *
185 * @return the payloads
186 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000187 public Collection<byte[]> getPayloads () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000188 return payloads;
189 }
Eliza Margarethae7938d32014-07-29 12:12:15 +0000190
Nils Diewaldbb33da22015-03-04 16:24:25 +0000191
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000192 /**
193 * Sets the payloads of the CandidateSpan.
194 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000195 * @param payloads
196 * the payloads
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000197 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000198 public void setPayloads (Collection<byte[]> payloads) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000199
margaretha50c76332015-03-19 10:10:39 +0100200 this.payloads = new ArrayList<>();
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000201 for (byte[] b : payloads) {
202 if (b == null)
203 this.payloads.add(null);
204 else
205 this.payloads.add(b.clone());
206 }
207 }
208
Nils Diewaldbb33da22015-03-04 16:24:25 +0000209
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000210 /**
211 * Returns the cost of finding the CandidateSpan.
212 *
213 * @return the cost
214 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000215 public long getCost () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000216 return cost;
217 }
218
Nils Diewaldbb33da22015-03-04 16:24:25 +0000219
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000220 /**
221 * Sets the cost of finding the CandidateSpan.
222 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000223 * @param cost
224 * the cost
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000225 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000226 public void setCost (long cost) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000227 this.cost = cost;
228 }
229
Nils Diewaldbb33da22015-03-04 16:24:25 +0000230
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000231 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000232 * Returns the element position number containing the
233 * CandidateSpan.
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000234 *
235 * @return the element position number
236 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000237 public int getPosition () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000238 return position;
239 }
240
Nils Diewaldbb33da22015-03-04 16:24:25 +0000241
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000242 /**
243 * Sets the element position number containing the CandidateSpan.
244 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000245 * @param position
246 * the element position number
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000247 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000248 public void setPosition (int position) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000249 this.position = position;
250 }
251
Nils Diewaldbb33da22015-03-04 16:24:25 +0000252
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000253 /**
254 * Returns a child/sub Span of the CandidateSpan.
255 *
256 * @return a child/sub span of the CandidateSpan
257 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000258 public CandidateSpan getChildSpan () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000259 return childSpan;
260 }
261
Nils Diewaldbb33da22015-03-04 16:24:25 +0000262
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000263 /**
264 * Sets the child/sub span of the CandidateSpan.
265 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000266 * @param childSpan
267 * a child/sub span of the CandidateSpan
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000268 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000269 public void setChildSpan (CandidateSpan childSpan) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000270 this.childSpan = childSpan;
271 }
272
Nils Diewaldbb33da22015-03-04 16:24:25 +0000273
margarethac66265c2016-12-14 13:48:45 +0100274 public CandidateSpan getSecondChildSpan () {
275 return secondChildSpan;
276 }
277
278
279 public void setSecondChildSpan (CandidateSpan secondChildSpan) {
280 this.secondChildSpan = secondChildSpan;
281 }
282
283
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000284 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000285 * Returns the span id of another Span related to the
286 * CandidateSpan. Only
287 * CandidateSpan of particular Spans such as
288 * {@link AttributeSpans} having
289 * this property. For instance, an AttributeSpan has a spanId of
290 * the element
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000291 * it belongs to.
292 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000293 * @return the span id of another Span related to the
294 * CandidateSpan
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000295 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000296 public short getSpanId () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000297 return spanId;
298 }
299
Nils Diewaldbb33da22015-03-04 16:24:25 +0000300
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000301 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000302 * Sets the span id of another Span related to the CandidateSpan.
303 * Only
304 * CandidateSpan of particular Spans such as
305 * {@link AttributeSpans} having
306 * this property. For instance, an AttributeSpan has a spanId of
307 * the element
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000308 * it belongs to.
309 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000310 * @param spanId
311 * the span id of another Span related to the
312 * CandidateSpan
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000313 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000314 public void setSpanId (short spanId) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000315 this.spanId = spanId;
Akron42993552016-02-04 13:24:24 +0100316 if (spanId > 0)
317 this.hasSpanId = true;
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000318 }
319
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000320
Nils Diewaldbb33da22015-03-04 16:24:25 +0000321 public short getLeftId () {
322 return leftId;
323 }
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000324
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000325
Nils Diewaldbb33da22015-03-04 16:24:25 +0000326 public void setLeftId (short leftId) {
327 this.leftId = leftId;
328 }
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000329
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000330
Nils Diewaldbb33da22015-03-04 16:24:25 +0000331 public short getRightId () {
332 return rightId;
333 }
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000334
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000335
Nils Diewaldbb33da22015-03-04 16:24:25 +0000336 public void setRightId (short rightId) {
337 this.rightId = rightId;
338 }
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000339
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000340
Nils Diewaldbb33da22015-03-04 16:24:25 +0000341 public int getLeftStart () {
342 return leftStart;
343 }
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000344
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000345
Nils Diewaldbb33da22015-03-04 16:24:25 +0000346 public void setLeftStart (int leftStart) {
347 this.leftStart = leftStart;
348 }
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000349
Nils Diewaldbb33da22015-03-04 16:24:25 +0000350
351 public int getLeftEnd () {
352 return leftEnd;
353 }
354
355
356 public void setLeftEnd (int leftEnd) {
357 this.leftEnd = leftEnd;
358 }
359
360
361 public int getRightStart () {
362 return rightStart;
363 }
364
365
366 public void setRightStart (int rightStart) {
367 this.rightStart = rightStart;
368 }
369
370
371 public int getRightEnd () {
372 return rightEnd;
373 }
374
375
376 public void setRightEnd (int rightEnd) {
377 this.rightEnd = rightEnd;
378 }
379
380
Akron42993552016-02-04 13:24:24 +0100381 public byte getPayloadTypeIdentifier () {
382 return payloadTypeIdentifier;
383 }
margaretha14f918d2015-12-11 11:48:07 +0100384
margaretha14f918d2015-12-11 11:48:07 +0100385
Akron42993552016-02-04 13:24:24 +0100386 public void setPayloadTypeIdentifier (byte payloadTypeIdentifier) {
387 this.payloadTypeIdentifier = payloadTypeIdentifier;
388 }
389
390
391 @Override
Nils Diewaldbb33da22015-03-04 16:24:25 +0000392 public int compareTo (CandidateSpan o) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000393 if (this.doc == o.doc) {
394 if (this.getStart() == o.getStart()) {
395 if (this.getEnd() == o.getEnd())
396 return 0;
397 if (this.getEnd() > o.getEnd())
398 return 1;
399 else
400 return -1;
Nils Diewaldbb33da22015-03-04 16:24:25 +0000401 }
402 else if (this.getStart() < o.getStart())
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000403 return -1;
404 else
405 return 1;
Nils Diewaldbb33da22015-03-04 16:24:25 +0000406 }
407 else if (this.doc < o.doc)
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000408 return -1;
409 else
410 return 1;
411 }
margaretha1bbd1be2018-11-12 14:46:42 +0100412
413 @Override
414 public String toString () {
415 return getClass().getName() + "@" +doc + ":" + start + "-" + end;
416 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +0000417}