blob: d0ac4928c65215c3f7821f8a4c6057bd536f71d2 [file] [log] [blame]
Eliza Margarethaa2603fa2014-01-22 10:59:25 +00001package de.ids_mannheim.korap.query.spans;
2
3import java.io.IOException;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +00004import java.util.Map;
5
Akron700c1eb2015-09-25 16:57:30 +02006import org.apache.lucene.index.LeafReaderContext;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +00007import org.apache.lucene.index.Term;
8import org.apache.lucene.index.TermContext;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +00009import org.apache.lucene.util.Bits;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000010
Eliza Margaretha609fcc62014-02-13 14:10:20 +000011import de.ids_mannheim.korap.query.SpanDistanceQuery;
12import de.ids_mannheim.korap.query.SpanMultipleDistanceQuery;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000013
Eliza Margarethac8d59202014-12-16 16:21:16 +000014/**
Nils Diewaldbb33da22015-03-04 16:24:25 +000015 * DistanceSpan is a base class for enumeration of span matches, whose
Eliza Margarethadc98dc12016-11-16 14:33:42 +010016 * two child spans have a specific range of distance (within a min and
17 * a max distance) and other constraints (i.e. order and
18 * co-occurrence) depending on the {@link SpanDistanceQuery}. All
19 * distance related spans extends this class.
Eliza Margarethac8d59202014-12-16 16:21:16 +000020 *
21 * @see DistanceExclusionSpans
22 * @see ElementDistanceExclusionSpans
23 * @see OrderedDistanceSpans
24 * @see UnorderedDistanceSpans
25 * @see MultipleDistanceSpans
Eliza Margaretha8e274e32014-01-28 15:09:30 +000026 *
27 * @author margaretha
Eliza Margaretha6f989202016-10-14 21:48:29 +020028 */
Eliza Margarethac8d59202014-12-16 16:21:16 +000029public abstract class DistanceSpans extends SimpleSpans {
Eliza Margarethae18d62e2014-02-11 11:30:48 +000030
Eliza Margarethac8d59202014-12-16 16:21:16 +000031 protected CandidateSpan matchFirstSpan, matchSecondSpan;
Eliza Margarethac8d59202014-12-16 16:21:16 +000032 protected boolean exclusion; // for MultipleDistanceQuery
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000033
Nils Diewaldbb33da22015-03-04 16:24:25 +000034
Eliza Margarethac8d59202014-12-16 16:21:16 +000035 /**
36 * Constructs a DistanceSpans enumeration for the given
37 * {@link SpanDistanceQuery}.
38 *
Nils Diewaldbb33da22015-03-04 16:24:25 +000039 * @param query
40 * a SpanDistanceQuery
Eliza Margarethac8d59202014-12-16 16:21:16 +000041 * @param context
42 * @param acceptDocs
43 * @param termContexts
44 * @throws IOException
45 */
Akron700c1eb2015-09-25 16:57:30 +020046 public DistanceSpans (SpanDistanceQuery query, LeafReaderContext context,
Nils Diewaldbb33da22015-03-04 16:24:25 +000047 Bits acceptDocs, Map<Term, TermContext> termContexts)
Eliza Margarethac8d59202014-12-16 16:21:16 +000048 throws IOException {
49 super(query, context, acceptDocs, termContexts);
50 exclusion = query.isExclusion();
51 }
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000052
Nils Diewaldbb33da22015-03-04 16:24:25 +000053
Eliza Margarethac8d59202014-12-16 16:21:16 +000054 /**
55 * Constructs a DistanceSpans enumeration for the given
56 * {@link SpanMultipleDistanceQuery}.
57 *
Nils Diewaldbb33da22015-03-04 16:24:25 +000058 * @param query
59 * a SpanMultipleDistanceQuery
Eliza Margarethac8d59202014-12-16 16:21:16 +000060 * @param context
61 * @param acceptDocs
62 * @param termContexts
63 * @throws IOException
64 */
Nils Diewaldbb33da22015-03-04 16:24:25 +000065 public DistanceSpans (SpanMultipleDistanceQuery query,
Akron700c1eb2015-09-25 16:57:30 +020066 LeafReaderContext context, Bits acceptDocs,
Nils Diewaldbb33da22015-03-04 16:24:25 +000067 Map<Term, TermContext> termContexts)
68 throws IOException {
Eliza Margarethac8d59202014-12-16 16:21:16 +000069 super(query, context, acceptDocs, termContexts);
70 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000071
Nils Diewaldbb33da22015-03-04 16:24:25 +000072
Eliza Margarethac8d59202014-12-16 16:21:16 +000073 @Override
Nils Diewaldbb33da22015-03-04 16:24:25 +000074 public boolean next () throws IOException {
Eliza Margarethac8d59202014-12-16 16:21:16 +000075 isStartEnumeration = false;
76 matchPayload.clear();
77 return advance();
78 }
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000079
Nils Diewaldbb33da22015-03-04 16:24:25 +000080
Eliza Margarethac8d59202014-12-16 16:21:16 +000081 /**
82 * Advances the current span enumeration to the next span match.
83 *
84 * @return <code>true</code> if a span match is available,
85 * <code>false</code> otherwise.
Eliza Margaretha6f989202016-10-14 21:48:29 +020086 */
Nils Diewaldbb33da22015-03-04 16:24:25 +000087 protected abstract boolean advance () throws IOException;
88
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000089
Eliza Margarethac8d59202014-12-16 16:21:16 +000090 /**
91 * Returns the first span of the current match.
92 *
93 * @return the first span of the current match.
94 */
Nils Diewaldbb33da22015-03-04 16:24:25 +000095 public CandidateSpan getMatchFirstSpan () {
Eliza Margarethac8d59202014-12-16 16:21:16 +000096 return matchFirstSpan;
97 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000098
Nils Diewaldbb33da22015-03-04 16:24:25 +000099
Eliza Margarethac8d59202014-12-16 16:21:16 +0000100 /**
101 * Sets the first span of the current match.
102 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000103 * @param matchFirstSpan
104 * the first span of the current match.
Eliza Margarethac8d59202014-12-16 16:21:16 +0000105 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000106 public void setMatchFirstSpan (CandidateSpan matchFirstSpan) {
Eliza Margarethac8d59202014-12-16 16:21:16 +0000107 this.matchFirstSpan = matchFirstSpan;
108 }
Eliza Margaretha01929182014-02-19 11:48:59 +0000109
Nils Diewaldbb33da22015-03-04 16:24:25 +0000110
Eliza Margarethac8d59202014-12-16 16:21:16 +0000111 /**
112 * Returns the second span of the current match.
113 *
114 * @return the second span of the current match.
115 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000116 public CandidateSpan getMatchSecondSpan () {
Eliza Margarethac8d59202014-12-16 16:21:16 +0000117 return matchSecondSpan;
118 }
119
Nils Diewaldbb33da22015-03-04 16:24:25 +0000120
Eliza Margarethac8d59202014-12-16 16:21:16 +0000121 /**
122 * Sets the second span of the current match.
123 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000124 * @param matchSecondSpan
125 * the second span of the current match.
Eliza Margarethac8d59202014-12-16 16:21:16 +0000126 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000127 public void setMatchSecondSpan (CandidateSpan matchSecondSpan) {
Eliza Margarethac8d59202014-12-16 16:21:16 +0000128 this.matchSecondSpan = matchSecondSpan;
129 }
130
Nils Diewaldbb33da22015-03-04 16:24:25 +0000131
Eliza Margarethac8d59202014-12-16 16:21:16 +0000132 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000133 * Tells if the second span must occur together with the first
134 * span, or not.
Eliza Margarethac8d59202014-12-16 16:21:16 +0000135 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000136 * @return <code>true</code> if the second span must <em>not</em>
137 * occur
138 * together with the first span, <code>false</code>
139 * otherwise.
Eliza Margarethac8d59202014-12-16 16:21:16 +0000140 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000141 public boolean isExclusion () {
Eliza Margarethac8d59202014-12-16 16:21:16 +0000142 return exclusion;
143 }
144
Nils Diewaldbb33da22015-03-04 16:24:25 +0000145
Eliza Margarethac8d59202014-12-16 16:21:16 +0000146 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000147 * Sets <code>true</code> if the second span must <em>not</em>
148 * occur
Eliza Margarethac8d59202014-12-16 16:21:16 +0000149 * together with the first span, <code>false</code> otherwise.
150 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000151 * @param exclusion
152 * a boolean with the value <code>true</code> if the
153 * second
154 * span must <em>not</em> occur together with the first
155 * span,
156 * <code>false</code> otherwise.
Eliza Margarethac8d59202014-12-16 16:21:16 +0000157 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000158 public void setExclusion (boolean exclusion) {
Eliza Margarethac8d59202014-12-16 16:21:16 +0000159 this.exclusion = exclusion;
160 }
Eliza Margaretha01929182014-02-19 11:48:59 +0000161
Eliza Margarethaa2603fa2014-01-22 10:59:25 +0000162}