blob: 7a87bddfdca89f9dd1b7e133086534a6f6599685 [file] [log] [blame]
Eliza Margaretha198e4ef2014-02-10 13:50:50 +00001package de.ids_mannheim.korap.query;
2
Eliza Margarethad69516c2014-11-24 11:36:02 +00003/**
Eliza Margaretha7ebd6d92014-12-02 11:48:36 +00004 * DistanceConstraint specifies the constraints used in
Eliza Margaretha8551e5b2014-12-15 16:46:18 +00005 * {@link SpanDistanceQuery SpanDistanceQueries} or
6 * {@link SpanMultipleDistanceQuery SpanMultipleDistanceQueries}. The
Nils Diewaldbb33da22015-03-04 16:24:25 +00007 * constraints comprise the distance unit, the minimum and maximum
8 * distances,
Eliza Margaretha8551e5b2014-12-15 16:46:18 +00009 * the order and the co-occurence of the compared spans.
Eliza Margarethad69516c2014-11-24 11:36:02 +000010 *
Eliza Margaretha7ebd6d92014-12-02 11:48:36 +000011 * Distance constraint examples:
Eliza Margarethad69516c2014-11-24 11:36:02 +000012 *
Eliza Margaretha7ebd6d92014-12-02 11:48:36 +000013 * <ol>
Nils Diewaldbb33da22015-03-04 16:24:25 +000014 * <li>Two terms x and y are separated by minimum two and maximum
15 * three other
Eliza Margaretha7ebd6d92014-12-02 11:48:36 +000016 * words. The order of x and y does not matter.
17 *
18 * <pre>
19 * DistanceConstraint dc = new DistanceConstraint(2, 3, false, false);
20 * </pre>
21 *
22 * </li>
Nils Diewaldbb33da22015-03-04 16:24:25 +000023 * <li>Two terms x and y are separated by minimum two and maximum
24 * three other
Eliza Margaretha7ebd6d92014-12-02 11:48:36 +000025 * words. X must precede y.
26 *
27 * <pre>
28 * DistanceConstraint dc = new DistanceConstraint(2, 3, true, false);
29 * </pre>
30 *
31 * </li>
32 * <li>
Nils Diewaldbb33da22015-03-04 16:24:25 +000033 * Term x do not occur with term y in minimum two and maximum three
34 * other words.
Eliza Margaretha7ebd6d92014-12-02 11:48:36 +000035 * X must precede y.
36 *
37 * <pre>
38 * DistanceConstraint dc = new DistanceConstraint(2, 3, true, true);
39 * </pre>
40 *
41 * </li>
42 * <li>Two terms x and y separated by minimum one and maximum two
43 * <em>sentences</em>. X must precede y.
44 *
45 * <pre>
Nils Diewaldbb33da22015-03-04 16:24:25 +000046 * SpanElementQuery e = new SpanElementQuery(&quot;tokens&quot;,
47 * &quot;s&quot;);
48 * DistanceConstraint dc = new DistanceConstraint(e, 2, 3, true,
49 * false);
Eliza Margaretha7ebd6d92014-12-02 11:48:36 +000050 * </pre>
51 *
52 * </li>
53 * </ol>
Eliza Margarethad69516c2014-11-24 11:36:02 +000054 *
55 * @author margaretha
Eliza Margaretha0a412512014-02-28 12:52:16 +000056 * */
57
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000058public class DistanceConstraint {
Eliza Margarethad69516c2014-11-24 11:36:02 +000059 private int minDistance, maxDistance;
60 private String unit;
61 private SpanElementQuery elementQuery;
62 private boolean exclusion;
63 private boolean isOrdered;
Eliza Margaretha609fcc62014-02-13 14:10:20 +000064
Nils Diewaldbb33da22015-03-04 16:24:25 +000065
Eliza Margaretha8551e5b2014-12-15 16:46:18 +000066 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +000067 * Constructs a DistanceConstraint object with the specified
68 * minimum and
69 * maximum distances. If isOrdered is true, the spans must occur
70 * in order.
71 * If exclusion is true, the second span must <em>not</em> occur
72 * together
Eliza Margaretha8551e5b2014-12-15 16:46:18 +000073 * with the first span.
74 *
Nils Diewaldbb33da22015-03-04 16:24:25 +000075 * @param min
76 * the minimum distance
77 * @param max
78 * the maximum distance
79 * @param isOrdered
80 * a boolean flag representing the value
81 * <code>true</code>
82 * if the spans should occur in order, false otherwise.
83 * @param exclusion
84 * a boolean flag representing the value
85 * <code>true</code>,
86 * if the second span should occur together with the
87 * first span,
88 * false otherwise.
Eliza Margaretha8551e5b2014-12-15 16:46:18 +000089 */
Nils Diewaldbb33da22015-03-04 16:24:25 +000090 public DistanceConstraint (int min, int max, boolean isOrdered,
91 boolean exclusion) {
Eliza Margarethad69516c2014-11-24 11:36:02 +000092 this.unit = "w";
93 this.minDistance = min;
94 this.maxDistance = max;
95 this.isOrdered = isOrdered;
96 this.exclusion = exclusion;
97 }
Eliza Margaretha609fcc62014-02-13 14:10:20 +000098
Nils Diewaldbb33da22015-03-04 16:24:25 +000099
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000100 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000101 * Constructs a DistanceContraint object with the specified
102 * SpanElementQuery
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000103 * as the distance unit, and the specified minimum and the maximum
Nils Diewaldbb33da22015-03-04 16:24:25 +0000104 * distances. If isOrdered is true, the spans must occur in order.
105 * If
106 * exclusion is true, the second span must <em>not</em> occur
107 * together with
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000108 * the first span.
109 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000110 * @param elementQuery
111 * the distance unit
112 * @param min
113 * the minimum distance
114 * @param max
115 * the maximum distance
116 * @param isOrdered
117 * a boolean flag representing the value
118 * <code>true</code>
119 * if the spans should occur in order, false otherwise.
120 * @param exclusion
121 * a boolean flag representing the value
122 * <code>true</code>,
123 * if the second span should occur together with the
124 * first span,
125 * false otherwise.
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000126 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000127 public DistanceConstraint (SpanElementQuery elementQuery, int min, int max,
128 boolean isOrdered, boolean exclusion) {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000129 if (elementQuery == null) {
130 throw new IllegalArgumentException("Element query cannot be null.");
131 }
Eliza Margarethad4693462014-03-17 13:16:18 +0000132
Eliza Margarethad69516c2014-11-24 11:36:02 +0000133 this.unit = elementQuery.getElementStr();
134 this.minDistance = min;
135 this.maxDistance = max;
136 this.isOrdered = isOrdered;
137 this.exclusion = exclusion;
138 this.elementQuery = elementQuery;
139 }
Eliza Margarethad4693462014-03-17 13:16:18 +0000140
Nils Diewaldbb33da22015-03-04 16:24:25 +0000141
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000142 /**
143 * Returns the minimum distance.
144 *
145 * @return the minimum distance
146 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000147 public int getMinDistance () {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000148 return minDistance;
149 }
150
Nils Diewaldbb33da22015-03-04 16:24:25 +0000151
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000152 /**
153 * Sets the minimum distance.
154 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000155 * @param minDistance
156 * the minimum distance
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000157 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000158 public void setMinDistance (int minDistance) {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000159 this.minDistance = minDistance;
160 }
161
Nils Diewaldbb33da22015-03-04 16:24:25 +0000162
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000163 /**
164 * Returns the maximum distance.
165 *
166 * @return the maximum distance
167 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000168 public int getMaxDistance () {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000169 return maxDistance;
170 }
171
Nils Diewaldbb33da22015-03-04 16:24:25 +0000172
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000173 /**
174 * Sets the maximum distance.
175 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000176 * @param maxDistance
177 * the maximum distance
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000178 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000179 public void setMaxDistance (int maxDistance) {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000180 this.maxDistance = maxDistance;
181 }
182
Nils Diewaldbb33da22015-03-04 16:24:25 +0000183
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000184 /**
185 * Returns the distance unit.
186 *
187 * @return the distance unit
188 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000189 public String getUnit () {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000190 return unit;
191 }
192
Nils Diewaldbb33da22015-03-04 16:24:25 +0000193
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000194 /**
195 * Sets the distance unit.
196 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000197 * @param unit
198 * the distance unit
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000199 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000200 public void setUnit (String unit) {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000201 this.unit = unit;
202 }
203
Nils Diewaldbb33da22015-03-04 16:24:25 +0000204
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000205 /**
206 * Returns the element query used as the distance unit.
207 *
208 * @return the element query used as the distance unit
209 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000210 public SpanElementQuery getElementQuery () {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000211 return elementQuery;
212 }
213
Nils Diewaldbb33da22015-03-04 16:24:25 +0000214
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000215 /**
216 * Sets the element query used as the distance unit.
217 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000218 * @param elementQuery
219 * the element query used as the distance unit.
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000220 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000221 public void setElementQuery (SpanElementQuery elementQuery) {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000222 this.elementQuery = elementQuery;
223 }
224
Nils Diewaldbb33da22015-03-04 16:24:25 +0000225
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000226 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000227 * Tells if the second span must occur together with the first
228 * span, or not.
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000229 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000230 * @return <code>true</code> if the second span must <em>not</em>
231 * occur
232 * together with the first span, <code>false</code>
233 * otherwise.
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000234 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000235 public boolean isExclusion () {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000236 return exclusion;
237 }
238
Nils Diewaldbb33da22015-03-04 16:24:25 +0000239
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000240 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000241 * Sets <code>true</code> if the second span must <em>not</em>
242 * occur
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000243 * together with the first span, <code>false</code> otherwise.
244 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000245 * @param exclusion
246 * a boolean with value <code>true</code> if the second
247 * span must <em>not</em> occur together with the first
248 * span,
249 * <code>false</code> otherwise.
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000250 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000251 public void setExclusion (boolean exclusion) {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000252 this.exclusion = exclusion;
253 }
254
Nils Diewaldbb33da22015-03-04 16:24:25 +0000255
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000256 /**
257 * Tells if the spans must occur in order or not.
258 *
259 * @return <code>true</code> if the spans must occur in order,
260 * <code>false</code> otherwise.
261 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000262 public boolean isOrdered () {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000263 return isOrdered;
264 }
265
Nils Diewaldbb33da22015-03-04 16:24:25 +0000266
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000267 /**
268 * Sets if the spans must occur in order or not.
269 *
Nils Diewaldbb33da22015-03-04 16:24:25 +0000270 * @param isOrdered
271 * a boolean with value <code>true</code> if the spans
272 * must
273 * occur in order, <code>false</code> otherwise.
Eliza Margaretha8551e5b2014-12-15 16:46:18 +0000274 */
Nils Diewaldbb33da22015-03-04 16:24:25 +0000275 public void setOrdered (boolean isOrdered) {
Eliza Margarethad69516c2014-11-24 11:36:02 +0000276 this.isOrdered = isOrdered;
277 }
Eliza Margaretha198e4ef2014-02-10 13:50:50 +0000278}