| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.wrap; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import org.apache.lucene.search.spans.SpanQuery; |
| 7 | import org.apache.lucene.search.spans.SpanTermQuery; |
| 8 | |
| 9 | import de.ids_mannheim.korap.query.SpanAttributeQuery; |
| 10 | import de.ids_mannheim.korap.query.SpanWithAttributeQuery; |
| 11 | import de.ids_mannheim.korap.query.SpanWithIdQuery; |
| 12 | import de.ids_mannheim.korap.util.QueryException; |
| 13 | |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 14 | /** |
| 15 | * No optimization using expansion |
| 16 | * |
| 17 | * @author margaretha |
| 18 | * */ |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 19 | public class SpanWithAttributeQueryWrapper extends SpanQueryWrapper { |
| 20 | |
| 21 | private SpanQueryWrapper withIdQueryWrapper = null; |
| 22 | private SpanQueryWrapper attrQueryWrapper = null; |
| 23 | private List<SpanQueryWrapper> queryWrapperList = null; |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 24 | private boolean isSingleAttribute = false; |
| 25 | |
| Eliza Margaretha | 1969c2b | 2015-02-23 15:13:03 +0000 | [diff] [blame] | 26 | public SpanWithAttributeQueryWrapper(SpanQueryWrapper attrQuery) |
| 27 | throws QueryException { |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 28 | if (attrQuery != null) isNull = false; |
| 29 | if (attrQuery.isEmpty()) { |
| 30 | isEmpty = true; |
| 31 | return; |
| 32 | } |
| Eliza Margaretha | 1969c2b | 2015-02-23 15:13:03 +0000 | [diff] [blame] | 33 | if (attrQuery.isNegative) { |
| 34 | throw new QueryException("The query requires a positive attribute."); |
| 35 | } |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 36 | this.attrQueryWrapper = attrQuery; |
| 37 | this.isSingleAttribute = true; |
| 38 | } |
| 39 | |
| 40 | public SpanWithAttributeQueryWrapper(List<SpanQueryWrapper> attrList) |
| 41 | throws QueryException { |
| 42 | |
| 43 | if (attrList != null) isNull = false; |
| 44 | if (attrList.isEmpty()) { |
| 45 | throw new QueryException("No attribute is defined."); |
| 46 | } |
| 47 | |
| Eliza Margaretha | 1969c2b | 2015-02-23 15:13:03 +0000 | [diff] [blame] | 48 | boolean isAllNegative = true; |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 49 | for (SpanQueryWrapper sqw : attrList) { |
| 50 | if (sqw == null) { |
| 51 | isNull = true; |
| 52 | return; |
| 53 | } |
| 54 | if (sqw.isEmpty) { |
| 55 | isEmpty = true; |
| 56 | return; |
| 57 | } |
| Eliza Margaretha | 1969c2b | 2015-02-23 15:13:03 +0000 | [diff] [blame] | 58 | if (!sqw.isNegative) { |
| 59 | isAllNegative = false; |
| 60 | } |
| 61 | } |
| 62 | if (isAllNegative) { |
| 63 | throw new QueryException("No positive attribute is defined."); |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 64 | } |
| 65 | this.queryWrapperList = attrList; |
| 66 | } |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 67 | |
| 68 | public SpanWithAttributeQueryWrapper(SpanQueryWrapper withIdQuery, |
| 69 | SpanQueryWrapper attrQuery) { |
| 70 | |
| 71 | if (withIdQuery != null || attrQuery != null) { |
| 72 | isNull = false; |
| 73 | } |
| 74 | if (withIdQuery.isEmpty || attrQuery.isEmpty()) { |
| 75 | isEmpty = true; |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | this.attrQueryWrapper = attrQuery; |
| 80 | this.withIdQueryWrapper = withIdQuery; |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 81 | this.isSingleAttribute = true; |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | public SpanWithAttributeQueryWrapper(SpanQueryWrapper withIdQuery, |
| 85 | List<SpanQueryWrapper> attrList) { |
| 86 | |
| 87 | if (withIdQuery != null || attrList != null) { |
| 88 | isNull = false; |
| 89 | } |
| 90 | if (withIdQuery.isEmpty) { |
| 91 | isEmpty = true; |
| 92 | return; |
| 93 | } |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 94 | // if (attrList.isEmpty()) { |
| 95 | // not withattribute query, just a normal query |
| 96 | // } |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 97 | |
| 98 | for (SpanQueryWrapper sqw : attrList) { |
| 99 | if (sqw == null) { |
| 100 | isNull = true; |
| 101 | return; |
| 102 | } |
| 103 | if (sqw.isEmpty) { |
| 104 | isEmpty = true; |
| 105 | return; |
| 106 | } |
| 107 | } |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 108 | |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 109 | this.queryWrapperList = attrList; |
| 110 | this.withIdQueryWrapper = withIdQuery; |
| 111 | } |
| 112 | |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 113 | @Override |
| 114 | public SpanQuery toQuery() throws QueryException { |
| 115 | |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 116 | if (isNull || isEmpty) return null; |
| 117 | if (withIdQueryWrapper != null){ |
| 118 | return createSpecificSpanWithAttributeQuery(); |
| 119 | } |
| 120 | else{ |
| 121 | return createArbitrarySpanWithAttributeQuery(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private SpanQuery createSpecificSpanWithAttributeQuery() |
| 126 | throws QueryException { |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 127 | SpanWithIdQuery withIdQuery = (SpanWithIdQuery) withIdQueryWrapper |
| 128 | .toQuery(); |
| 129 | if (withIdQuery == null) { |
| 130 | isNull = true; |
| 131 | return null; |
| 132 | } |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 133 | if (isSingleAttribute) { |
| 134 | return createSpanWithSingleAttributeQuery(withIdQuery); |
| 135 | } |
| 136 | else if (queryWrapperList.isEmpty()) { |
| 137 | return withIdQuery; |
| 138 | } |
| 139 | else{ |
| 140 | return createSpanWithAttributeListQuery(withIdQuery); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private SpanWithAttributeQuery createSpanWithSingleAttributeQuery( |
| 145 | SpanWithIdQuery withIdQuery) |
| 146 | throws QueryException { |
| 147 | SpanAttributeQuery attrQuery = createSpanAttributeQuery(this.attrQueryWrapper); |
| 148 | if (attrQuery != null) { |
| 149 | if (withIdQuery != null) { |
| 150 | return new SpanWithAttributeQuery(withIdQuery, attrQuery, true); |
| 151 | } |
| 152 | else { |
| 153 | return new SpanWithAttributeQuery(attrQuery, true); |
| 154 | } |
| 155 | } |
| 156 | isNull = true; |
| 157 | return null; |
| 158 | } |
| 159 | |
| 160 | private SpanAttributeQuery createSpanAttributeQuery( |
| 161 | SpanQueryWrapper attrQueryWrapper) throws QueryException { |
| 162 | SpanQuery sq = attrQueryWrapper.toQuery(); |
| 163 | if (sq != null) { |
| 164 | if (sq instanceof SpanTermQuery) { |
| 165 | return new SpanAttributeQuery((SpanTermQuery) sq, |
| 166 | attrQueryWrapper.isNegative, true); |
| 167 | } |
| 168 | else { |
| 169 | throw new IllegalArgumentException( |
| 170 | "The subquery is not a SpanTermQuery."); |
| 171 | } |
| 172 | } |
| 173 | return null; |
| 174 | } |
| 175 | |
| 176 | private SpanWithAttributeQuery createSpanWithAttributeListQuery( |
| 177 | SpanWithIdQuery withIdQuery) |
| 178 | throws QueryException { |
| 179 | List<SpanQuery> attrQueries = new ArrayList<SpanQuery>(); |
| 180 | SpanQuery attrQuery = null; |
| 181 | for (SpanQueryWrapper sqw : queryWrapperList) { |
| 182 | attrQuery = createSpanAttributeQuery(sqw); |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 183 | if (attrQuery == null) { |
| 184 | isNull = true; |
| 185 | return null; |
| 186 | } |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 187 | attrQueries.add(attrQuery); |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 188 | } |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 189 | |
| 190 | if (withIdQuery != null) { |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 191 | return new SpanWithAttributeQuery(withIdQuery, attrQueries, true); |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 192 | } |
| 193 | else { |
| 194 | return new SpanWithAttributeQuery(attrQueries, true); |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 195 | } |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 196 | } |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 197 | |
| 198 | private SpanQuery createArbitrarySpanWithAttributeQuery() |
| 199 | throws QueryException { |
| 200 | if (isSingleAttribute) { |
| 201 | return createSpanWithSingleAttributeQuery(null); |
| 202 | } |
| 203 | else if (queryWrapperList.isEmpty()) { |
| 204 | throw new QueryException("No attribute is defined."); |
| 205 | } |
| 206 | else{ |
| 207 | return createSpanWithAttributeListQuery(null); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
| Eliza Margaretha | 641b20a | 2015-02-16 17:23:13 +0000 | [diff] [blame] | 212 | } |