blob: 7b633c01176290b9768d197650b2e7c95c8edd93 [file] [log] [blame]
Eliza Margaretha641b20a2015-02-16 17:23:13 +00001package de.ids_mannheim.korap.query.wrap;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import org.apache.lucene.search.spans.SpanQuery;
7import org.apache.lucene.search.spans.SpanTermQuery;
8
9import de.ids_mannheim.korap.query.SpanAttributeQuery;
10import de.ids_mannheim.korap.query.SpanWithAttributeQuery;
11import de.ids_mannheim.korap.query.SpanWithIdQuery;
12import de.ids_mannheim.korap.util.QueryException;
13
Eliza Margaretha05bff462015-02-18 18:18:26 +000014/**
15 * No optimization using expansion
16 *
17 * @author margaretha
18 * */
Eliza Margaretha641b20a2015-02-16 17:23:13 +000019public class SpanWithAttributeQueryWrapper extends SpanQueryWrapper {
20
21 private SpanQueryWrapper withIdQueryWrapper = null;
22 private SpanQueryWrapper attrQueryWrapper = null;
23 private List<SpanQueryWrapper> queryWrapperList = null;
Eliza Margaretha05bff462015-02-18 18:18:26 +000024 private boolean isSingleAttribute = false;
25
Eliza Margaretha1969c2b2015-02-23 15:13:03 +000026 public SpanWithAttributeQueryWrapper(SpanQueryWrapper attrQuery)
27 throws QueryException {
Eliza Margaretha05bff462015-02-18 18:18:26 +000028 if (attrQuery != null) isNull = false;
29 if (attrQuery.isEmpty()) {
30 isEmpty = true;
31 return;
32 }
Eliza Margaretha1969c2b2015-02-23 15:13:03 +000033 if (attrQuery.isNegative) {
34 throw new QueryException("The query requires a positive attribute.");
35 }
Eliza Margaretha05bff462015-02-18 18:18:26 +000036 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 Margaretha1969c2b2015-02-23 15:13:03 +000048 boolean isAllNegative = true;
Eliza Margaretha05bff462015-02-18 18:18:26 +000049 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 Margaretha1969c2b2015-02-23 15:13:03 +000058 if (!sqw.isNegative) {
59 isAllNegative = false;
60 }
61 }
62 if (isAllNegative) {
63 throw new QueryException("No positive attribute is defined.");
Eliza Margaretha05bff462015-02-18 18:18:26 +000064 }
65 this.queryWrapperList = attrList;
66 }
Eliza Margaretha641b20a2015-02-16 17:23:13 +000067
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 Margaretha05bff462015-02-18 18:18:26 +000081 this.isSingleAttribute = true;
Eliza Margaretha641b20a2015-02-16 17:23:13 +000082 }
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 Margaretha05bff462015-02-18 18:18:26 +000094 // if (attrList.isEmpty()) {
95 // not withattribute query, just a normal query
96 // }
Eliza Margaretha641b20a2015-02-16 17:23:13 +000097
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 Margaretha05bff462015-02-18 18:18:26 +0000108
Eliza Margaretha641b20a2015-02-16 17:23:13 +0000109 this.queryWrapperList = attrList;
110 this.withIdQueryWrapper = withIdQuery;
111 }
112
Eliza Margaretha641b20a2015-02-16 17:23:13 +0000113 @Override
114 public SpanQuery toQuery() throws QueryException {
115
Eliza Margaretha05bff462015-02-18 18:18:26 +0000116 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 Margaretha641b20a2015-02-16 17:23:13 +0000127 SpanWithIdQuery withIdQuery = (SpanWithIdQuery) withIdQueryWrapper
128 .toQuery();
129 if (withIdQuery == null) {
130 isNull = true;
131 return null;
132 }
Eliza Margaretha05bff462015-02-18 18:18:26 +0000133 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 Margaretha641b20a2015-02-16 17:23:13 +0000183 if (attrQuery == null) {
184 isNull = true;
185 return null;
186 }
Eliza Margaretha05bff462015-02-18 18:18:26 +0000187 attrQueries.add(attrQuery);
Eliza Margaretha641b20a2015-02-16 17:23:13 +0000188 }
Eliza Margaretha05bff462015-02-18 18:18:26 +0000189
190 if (withIdQuery != null) {
Eliza Margaretha641b20a2015-02-16 17:23:13 +0000191 return new SpanWithAttributeQuery(withIdQuery, attrQueries, true);
Eliza Margaretha05bff462015-02-18 18:18:26 +0000192 }
193 else {
194 return new SpanWithAttributeQuery(attrQueries, true);
Eliza Margaretha641b20a2015-02-16 17:23:13 +0000195 }
Eliza Margaretha641b20a2015-02-16 17:23:13 +0000196 }
Eliza Margaretha05bff462015-02-18 18:18:26 +0000197
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 Margaretha641b20a2015-02-16 17:23:13 +0000212}