blob: c78eee4984cd63a91672996ee33b813c80731b0a [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},
11 * which is an
12 * enumeration. CandidateSpan is used for various purposes, such as
13 * for
14 * collecting spans which will be used in a latter process or next
15 * matching.
Eliza Margaretha609a5be2014-12-18 16:52:20 +000016 *
17 * @author margaretha
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000018 * */
Eliza Margaretha609a5be2014-12-18 16:52:20 +000019public class CandidateSpan implements Comparable<CandidateSpan>, Cloneable {
Nils Diewaldbb33da22015-03-04 16:24:25 +000020 protected int doc, start, end;
Eliza Margaretha609a5be2014-12-18 16:52:20 +000021 private long cost;
22 private Collection<byte[]> payloads = new ArrayList<>();
23 private int position;
24 private CandidateSpan childSpan; // used for example for multiple distance
25 // with unordered constraint
26 protected short spanId;
Nils Diewaldbb33da22015-03-04 16:24:25 +000027 private short leftId, rightId;
28 private int leftStart, leftEnd;
29 private int rightStart, rightEnd;
30
Eliza Margaretha9738c392014-02-03 17:04:53 +000031
Eliza Margaretha609a5be2014-12-18 16:52:20 +000032 /**
33 * Constructs a CandidateSpan for the given Span.
34 *
Nils Diewaldbb33da22015-03-04 16:24:25 +000035 * @param span
36 * a Span
Eliza Margaretha609a5be2014-12-18 16:52:20 +000037 * @throws IOException
38 */
Nils Diewaldbb33da22015-03-04 16:24:25 +000039 public CandidateSpan (Spans span) throws IOException {
Eliza Margaretha609a5be2014-12-18 16:52:20 +000040 this.doc = span.doc();
41 this.start = span.start();
42 this.end = span.end();
43 this.cost = span.cost();
44 if (span.isPayloadAvailable())
45 setPayloads(span.getPayload());
46 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000047
Nils Diewaldbb33da22015-03-04 16:24:25 +000048
Eliza Margaretha609a5be2014-12-18 16:52:20 +000049 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +000050 * Constructs a CandidateSpan for the given Span and element
51 * position (where
52 * the span is included in a document). The element position is
53 * important
Eliza Margaretha609a5be2014-12-18 16:52:20 +000054 * for the matching process in {@link ElementDistanceSpans}.
55 *
Nils Diewaldbb33da22015-03-04 16:24:25 +000056 * @param span
57 * a Span
58 * @param position
59 * an element position
Eliza Margaretha609a5be2014-12-18 16:52:20 +000060 * @throws IOException
61 */
Nils Diewaldbb33da22015-03-04 16:24:25 +000062 public CandidateSpan (Spans span, int position) throws IOException {
Eliza Margaretha609a5be2014-12-18 16:52:20 +000063 this(span);
64 this.position = position;
65 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000066
Nils Diewaldbb33da22015-03-04 16:24:25 +000067
Eliza Margaretha609a5be2014-12-18 16:52:20 +000068 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +000069 * Constructs a CandidateSpan from all the given variables which
70 * are
Eliza Margaretha609a5be2014-12-18 16:52:20 +000071 * properties of a Span.
72 *
Nils Diewaldbb33da22015-03-04 16:24:25 +000073 * @param start
74 * the start position of a span
75 * @param end
76 * the end position of a span
77 * @param doc
78 * the document including the span
79 * @param cost
80 * the cost of finding a span
81 * @param payloads
82 * the payloads of a span
Eliza Margaretha609a5be2014-12-18 16:52:20 +000083 */
Nils Diewaldbb33da22015-03-04 16:24:25 +000084 public CandidateSpan (int start, int end, int doc, long cost,
85 Collection<byte[]> payloads) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +000086 this.start = start;
87 this.end = end;
88 this.doc = doc;
89 this.cost = cost;
90 if (payloads != null)
91 setPayloads(payloads);
92 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000093
Nils Diewaldbb33da22015-03-04 16:24:25 +000094
Eliza Margaretha609a5be2014-12-18 16:52:20 +000095 @Override
Nils Diewaldbb33da22015-03-04 16:24:25 +000096 protected CandidateSpan clone () throws CloneNotSupportedException {
Eliza Margaretha609a5be2014-12-18 16:52:20 +000097 return new CandidateSpan(this.start, this.end, this.doc, this.cost,
98 this.payloads);
99 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +0000100
Nils Diewaldbb33da22015-03-04 16:24:25 +0000101
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000102 /**
103 * Returns the document number containing the CandidateSpan.
104 *
105 * @return the document number
106 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000107 public int getDoc () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000108 return doc;
109 }
Eliza Margaretha8e274e32014-01-28 15:09:30 +0000110
Nils Diewaldbb33da22015-03-04 16:24:25 +0000111
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000112 /**
113 * Sets the document number containing the CandidateSpan.
114 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000115 * @param doc
116 * the document number
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000117 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000118 public void setDoc (int doc) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000119 this.doc = doc;
120 }
Eliza Margaretha8e274e32014-01-28 15:09:30 +0000121
Nils Diewaldbb33da22015-03-04 16:24:25 +0000122
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000123 /**
124 * Returns the start position of the CandidateSpan.
125 *
126 * @return the start position
127 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000128 public int getStart () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000129 return start;
130 }
Eliza Margaretha6651fc32014-02-18 14:57:47 +0000131
Nils Diewaldbb33da22015-03-04 16:24:25 +0000132
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000133 /**
134 * Sets the start position of the CandidateSpan.
135 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000136 * @param start
137 * the start position
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000138 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000139 public void setStart (int start) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000140 this.start = start;
141 }
Eliza Margaretha6651fc32014-02-18 14:57:47 +0000142
Nils Diewaldbb33da22015-03-04 16:24:25 +0000143
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000144 /**
145 * Returns the end position of the CandidateSpan.
146 *
147 * @return the end position
148 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000149 public int getEnd () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000150 return end;
151 }
Eliza Margarethae7938d32014-07-29 12:12:15 +0000152
Nils Diewaldbb33da22015-03-04 16:24:25 +0000153
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000154 /**
155 * Sets the end position of the CandidateSpan.
156 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000157 * @param end
158 * the end position
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000159 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000160 public void setEnd (int end) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000161 this.end = end;
162 }
Eliza Margarethae7938d32014-07-29 12:12:15 +0000163
Nils Diewaldbb33da22015-03-04 16:24:25 +0000164
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000165 /**
166 * Returns the payloads of the CandidateSpan.
167 *
168 * @return the payloads
169 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000170 public Collection<byte[]> getPayloads () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000171 return payloads;
172 }
Eliza Margarethae7938d32014-07-29 12:12:15 +0000173
Nils Diewaldbb33da22015-03-04 16:24:25 +0000174
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000175 /**
176 * Sets the payloads of the CandidateSpan.
177 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000178 * @param payloads
179 * the payloads
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000180 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000181 public void setPayloads (Collection<byte[]> payloads) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000182
183 for (byte[] b : payloads) {
184 if (b == null)
185 this.payloads.add(null);
186 else
187 this.payloads.add(b.clone());
188 }
189 }
190
Nils Diewaldbb33da22015-03-04 16:24:25 +0000191
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000192 /**
193 * Returns the cost of finding the CandidateSpan.
194 *
195 * @return the cost
196 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000197 public long getCost () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000198 return cost;
199 }
200
Nils Diewaldbb33da22015-03-04 16:24:25 +0000201
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000202 /**
203 * Sets the cost of finding the CandidateSpan.
204 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000205 * @param cost
206 * the cost
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000207 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000208 public void setCost (long cost) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000209 this.cost = cost;
210 }
211
Nils Diewaldbb33da22015-03-04 16:24:25 +0000212
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000213 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000214 * Returns the element position number containing the
215 * CandidateSpan.
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000216 *
217 * @return the element position number
218 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000219 public int getPosition () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000220 return position;
221 }
222
Nils Diewaldbb33da22015-03-04 16:24:25 +0000223
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000224 /**
225 * Sets the element position number containing the CandidateSpan.
226 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000227 * @param position
228 * the element position number
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000229 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000230 public void setPosition (int position) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000231 this.position = position;
232 }
233
Nils Diewaldbb33da22015-03-04 16:24:25 +0000234
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000235 /**
236 * Returns a child/sub Span of the CandidateSpan.
237 *
238 * @return a child/sub span of the CandidateSpan
239 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000240 public CandidateSpan getChildSpan () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000241 return childSpan;
242 }
243
Nils Diewaldbb33da22015-03-04 16:24:25 +0000244
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000245 /**
246 * Sets the child/sub span of the CandidateSpan.
247 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000248 * @param childSpan
249 * a child/sub span of the CandidateSpan
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000250 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000251 public void setChildSpan (CandidateSpan childSpan) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000252 this.childSpan = childSpan;
253 }
254
Nils Diewaldbb33da22015-03-04 16:24:25 +0000255
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000256 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000257 * Returns the span id of another Span related to the
258 * CandidateSpan. Only
259 * CandidateSpan of particular Spans such as
260 * {@link AttributeSpans} having
261 * this property. For instance, an AttributeSpan has a spanId of
262 * the element
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000263 * it belongs to.
264 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000265 * @return the span id of another Span related to the
266 * CandidateSpan
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000267 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000268 public short getSpanId () {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000269 return spanId;
270 }
271
Nils Diewaldbb33da22015-03-04 16:24:25 +0000272
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000273 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000274 * Sets the span id of another Span related to the CandidateSpan.
275 * Only
276 * CandidateSpan of particular Spans such as
277 * {@link AttributeSpans} having
278 * this property. For instance, an AttributeSpan has a spanId of
279 * the element
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000280 * it belongs to.
281 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000282 * @param spanId
283 * the span id of another Span related to the
284 * CandidateSpan
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000285 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000286 public void setSpanId (short spanId) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000287 this.spanId = spanId;
288 }
289
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000290
Nils Diewaldbb33da22015-03-04 16:24:25 +0000291 public short getLeftId () {
292 return leftId;
293 }
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000294
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000295
Nils Diewaldbb33da22015-03-04 16:24:25 +0000296 public void setLeftId (short leftId) {
297 this.leftId = leftId;
298 }
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000299
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000300
Nils Diewaldbb33da22015-03-04 16:24:25 +0000301 public short getRightId () {
302 return rightId;
303 }
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000304
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000305
Nils Diewaldbb33da22015-03-04 16:24:25 +0000306 public void setRightId (short rightId) {
307 this.rightId = rightId;
308 }
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000309
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000310
Nils Diewaldbb33da22015-03-04 16:24:25 +0000311 public int getLeftStart () {
312 return leftStart;
313 }
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000314
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000315
Nils Diewaldbb33da22015-03-04 16:24:25 +0000316 public void setLeftStart (int leftStart) {
317 this.leftStart = leftStart;
318 }
Eliza Margaretha2db5e232015-03-04 10:20:01 +0000319
Nils Diewaldbb33da22015-03-04 16:24:25 +0000320
321 public int getLeftEnd () {
322 return leftEnd;
323 }
324
325
326 public void setLeftEnd (int leftEnd) {
327 this.leftEnd = leftEnd;
328 }
329
330
331 public int getRightStart () {
332 return rightStart;
333 }
334
335
336 public void setRightStart (int rightStart) {
337 this.rightStart = rightStart;
338 }
339
340
341 public int getRightEnd () {
342 return rightEnd;
343 }
344
345
346 public void setRightEnd (int rightEnd) {
347 this.rightEnd = rightEnd;
348 }
349
350
351 @Override
352 public int compareTo (CandidateSpan o) {
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000353 if (this.doc == o.doc) {
354 if (this.getStart() == o.getStart()) {
355 if (this.getEnd() == o.getEnd())
356 return 0;
357 if (this.getEnd() > o.getEnd())
358 return 1;
359 else
360 return -1;
Nils Diewaldbb33da22015-03-04 16:24:25 +0000361 }
362 else if (this.getStart() < o.getStart())
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000363 return -1;
364 else
365 return 1;
Nils Diewaldbb33da22015-03-04 16:24:25 +0000366 }
367 else if (this.doc < o.doc)
Eliza Margaretha609a5be2014-12-18 16:52:20 +0000368 return -1;
369 else
370 return 1;
371 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +0000372}