| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.wrap; |
| 2 | |
| 3 | import org.apache.lucene.search.spans.SpanQuery; |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame^] | 4 | import de.ids_mannheim.korap.util.QueryException; |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 5 | |
| 6 | // TODO: Add warning and error |
| 7 | |
| 8 | /** |
| 9 | * A wrapper class for Lucene Spanqueries that add certain information |
| 10 | * to the queries, necessary for correct deserialization of nested queries. |
| 11 | * |
| 12 | * @author Nils Diewald |
| 13 | */ |
| 14 | public class SpanQueryWrapper { |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 15 | protected int min = 1, |
| 16 | max = 1; |
| 17 | |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame^] | 18 | protected byte number = (byte) 0; |
| 19 | protected boolean hasClass = false; |
| 20 | |
| 21 | protected boolean isNull = true, |
| 22 | isOptional = false, |
| 23 | isNegative = false, |
| 24 | isEmpty = false; |
| 25 | |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 26 | // Serialize query to Lucene SpanQuery |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame^] | 27 | public SpanQuery toQuery () throws QueryException { |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 28 | return (SpanQuery) null; |
| 29 | }; |
| 30 | |
| 31 | // The subquery is not necessary, like in |
| 32 | // "the [pos=ADJ]? tree" |
| 33 | // The adjective can be there, but it's not necessary |
| 34 | public boolean isOptional () { |
| 35 | return this.isOptional; |
| 36 | }; |
| 37 | |
| 38 | // The subquery won't match anything at all, |
| 39 | // like in |
| 40 | // "the [pos=ADJ]{0} tree" |
| 41 | public boolean isNull () { |
| 42 | return this.isNull; |
| 43 | }; |
| 44 | |
| 45 | // The subquery should match if the condition does not hold true like in |
| 46 | // "the [base!=tree]" |
| 47 | public boolean isNegative () { |
| 48 | return this.isNegative; |
| 49 | }; |
| 50 | |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame^] | 51 | // The subquery should match everything, like in |
| 52 | // "the []" |
| 53 | public boolean isEmpty () { |
| 54 | return this.isEmpty; |
| 55 | }; |
| 56 | |
| 57 | // Check, if the query may be an anchor |
| 58 | // in a SpanSequenceQueryWrapper |
| 59 | public boolean maybeAnchor () { |
| 60 | if (this.isNegative()) |
| 61 | return false; |
| 62 | |
| 63 | if (this.isOptional()) |
| 64 | return false; |
| 65 | |
| 66 | if (this.isEmpty()) |
| 67 | return false; |
| 68 | |
| 69 | return true; |
| 70 | }; |
| 71 | |
| 72 | public boolean maybeExtension () { |
| 73 | return !this.maybeAnchor(); |
| 74 | }; |
| 75 | |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 76 | // Repetition queries may be more specific regarding repetition |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame^] | 77 | // Get minimum repetition value |
| 78 | public int getMin () { |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 79 | return this.min; |
| 80 | }; |
| 81 | |
| 82 | // Repetition queries may be more specific regarding repetition |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame^] | 83 | // Get maximum repetition value |
| 84 | public int getMax () { |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 85 | return this.max; |
| 86 | }; |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame^] | 87 | |
| 88 | // Set minimum repetition value |
| 89 | public SpanQueryWrapper setMin (int min) { |
| 90 | this.min = min; |
| 91 | return this; |
| 92 | }; |
| 93 | |
| 94 | // Set maximum repetition value |
| 95 | public SpanQueryWrapper setMax (int max) { |
| 96 | this.max = max; |
| 97 | return this; |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | // Empty tokens may have class information |
| 102 | public boolean hasClass () { |
| 103 | return this.hasClass; |
| 104 | }; |
| 105 | |
| 106 | public SpanQueryWrapper hasClass (boolean value) { |
| 107 | this.hasClass = value; |
| 108 | return this; |
| 109 | }; |
| 110 | |
| 111 | // Get class number |
| 112 | public byte getClassNumber () { |
| 113 | return this.number; |
| 114 | }; |
| 115 | |
| 116 | // Set class number |
| 117 | public SpanQueryWrapper setClassNumber (byte number) { |
| 118 | this.hasClass = true; |
| 119 | this.number = number; |
| 120 | return this; |
| 121 | }; |
| 122 | |
| 123 | // Set class number |
| 124 | public SpanQueryWrapper setClassNumber (short number) { |
| 125 | return this.setClassNumber((byte) number); |
| 126 | }; |
| 127 | |
| 128 | // Set class number |
| 129 | public SpanQueryWrapper setClassNumber (int number) { |
| 130 | return this.setClassNumber((byte) number); |
| 131 | }; |
| 132 | |
| 133 | public String toString () { |
| 134 | String string = "" + |
| 135 | (this.isNull() ? "isNull" : "notNull") + |
| 136 | "-" + |
| 137 | (this.isEmpty() ? "isEmpty" : "notEmpty") + |
| 138 | "-" + |
| 139 | (this.isOptional() ? "isOptional" : "notOptional"); |
| 140 | return string; |
| 141 | }; |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 142 | }; |