Revision 55755
Added by Claudio Atzori about 5 years ago
Pruner.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.functionality.index.query; |
2 | 2 |
|
3 | 3 |
import java.io.IOException; |
4 |
import java.util.ArrayList; |
|
5 |
import java.util.HashMap; |
|
6 |
import java.util.List; |
|
7 |
import java.util.Map; |
|
4 |
import java.util.*; |
|
5 |
import java.util.stream.Collectors; |
|
6 |
import java.util.stream.Stream; |
|
8 | 7 |
|
9 |
import org.apache.commons.logging.Log; |
|
10 |
import org.apache.commons.logging.LogFactory; |
|
11 |
import org.z3950.zing.cql.CQLBooleanNode; |
|
12 |
import org.z3950.zing.cql.CQLNode; |
|
13 |
import org.z3950.zing.cql.CQLParseException; |
|
14 |
import org.z3950.zing.cql.CQLParser; |
|
15 |
import org.z3950.zing.cql.CQLPrefixNode; |
|
16 |
import org.z3950.zing.cql.CQLSortNode; |
|
17 |
import org.z3950.zing.cql.CQLTermNode; |
|
18 |
|
|
19 |
import com.google.common.base.Predicates; |
|
20 | 8 |
import com.google.common.collect.Iterables; |
21 | 9 |
import com.google.common.collect.Lists; |
22 | 10 |
import com.google.common.collect.Maps; |
11 |
import org.apache.commons.logging.Log; |
|
12 |
import org.apache.commons.logging.LogFactory; |
|
13 |
import org.z3950.zing.cql.*; |
|
23 | 14 |
|
24 |
import eu.dnetlib.miscutils.collections.MappedCollection; |
|
25 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
26 |
|
|
27 | 15 |
/** |
28 | 16 |
* Use this class to cleanup a CQL tree and obtain all the options |
29 | 17 |
* |
... | ... | |
44 | 32 |
* Helper method, parse a given CQL string. |
45 | 33 |
* |
46 | 34 |
* @param cqlQuery |
47 |
* @return |
|
35 |
* @return the parsed CQLNode
|
|
48 | 36 |
* @throws CQLParseException |
49 | 37 |
* @throws IOException |
50 | 38 |
*/ |
... | ... | |
115 | 103 |
* |
116 | 104 |
* @param prefixes |
117 | 105 |
* @param root |
118 |
* @return |
|
106 |
* @return the pruned result
|
|
119 | 107 |
*/ |
120 | 108 |
public Result prune(final Map<String, String> prefixes, final CQLNode root) { |
121 | 109 |
|
... | ... | |
136 | 124 |
* |
137 | 125 |
* @param prefixes |
138 | 126 |
* @param node |
139 |
* @return |
|
127 |
* @return the pruned result
|
|
140 | 128 |
*/ |
141 | 129 |
public Result prune(final Map<String, String> prefixes, final CQLSortNode node) { |
142 | 130 |
Result res = prune(prefixes, node.subtree); |
... | ... | |
154 | 142 |
* |
155 | 143 |
* @param prefixes |
156 | 144 |
* @param node |
157 |
* @return |
|
145 |
* @return the pruned result
|
|
158 | 146 |
*/ |
159 | 147 |
public Result prune(final Map<String, String> prefixes, final CQLPrefixNode node) { |
160 | 148 |
final HashMap<String, String> subPrefixes = Maps.newHashMap(prefixes); |
... | ... | |
179 | 167 |
* |
180 | 168 |
* @param prefixes |
181 | 169 |
* @param node |
182 |
* @return |
|
170 |
* @return the pruned result
|
|
183 | 171 |
*/ |
184 | 172 |
public Result prune(final Map<String, String> prefixes, final CQLBooleanNode node) { |
185 | 173 |
return prune(prefixes, node, node.left, node.right); |
... | ... | |
193 | 181 |
* @param bool |
194 | 182 |
* @param left |
195 | 183 |
* @param right |
196 |
* @return |
|
184 |
* @return the pruned result
|
|
197 | 185 |
*/ |
198 | 186 |
public Result prune(final Map<String, String> prefixes, final CQLBooleanNode bool, final CQLNode left, final CQLNode right) { |
199 | 187 |
|
200 | 188 |
if (isOption(prefixes, left) && isOption(prefixes, right)) { |
201 |
List<Result> r = Lists.newArrayList(trimOption(prefixes, left, right), trimOption(prefixes, right, left)); |
|
202 |
|
|
203 |
return new Result(null, Iterables.concat(MappedCollection.map(Iterables.filter(r, Predicates.notNull()), |
|
204 |
new UnaryFunction<Iterable<String>, Result>() { |
|
205 |
@Override |
|
206 |
public Iterable<String> evaluate(Result res) { |
|
207 |
return res.getOptions(); |
|
208 |
} |
|
209 |
}))); |
|
189 |
return new Result(null, Stream.of(trimOption(prefixes, left, right), trimOption(prefixes, right, left)) |
|
190 |
.filter(Objects::nonNull) |
|
191 |
.map(i -> i.getOptions()) |
|
192 |
.map(i -> i.stream()) |
|
193 |
.flatMap(i -> i) |
|
194 |
.collect(Collectors.toList())); |
|
210 | 195 |
} |
211 | 196 |
|
212 | 197 |
Result res = anyNotNull(trimOption(prefixes, left, right), trimOption(prefixes, right, left)); |
... | ... | |
236 | 221 |
* @param prefixes |
237 | 222 |
* @param a |
238 | 223 |
* @param b |
239 |
* @return |
|
224 |
* @return the pruned result
|
|
240 | 225 |
*/ |
241 | 226 |
public Result trimOption(final Map<String, String> prefixes, final CQLNode a, final CQLNode b) { |
242 | 227 |
log.debug("trim option?" + prefixes + " a " + a.toCQL()); |
... | ... | |
259 | 244 |
* @param ns |
260 | 245 |
* @param o |
261 | 246 |
* @param subtree |
262 |
* @return |
|
247 |
* @return the pruned result
|
|
263 | 248 |
*/ |
264 | 249 |
public Result trimOption(final Map<String, String> prefixes, final String ns, final String o, final CQLNode subtree) { |
265 | 250 |
log.debug("trimming " + prefixes + " ns " + ns + " o " + o); |
... | ... | |
278 | 263 |
* Drop a boolean node (and, or etc) if one of the sides has been dropped. |
279 | 264 |
* |
280 | 265 |
* @param bool |
281 |
* @return |
|
266 |
* @return the pruned result
|
|
282 | 267 |
*/ |
283 | 268 |
private CQLNode clean(final CQLBooleanNode bool) { |
284 | 269 |
if (bool.left == null) |
... | ... | |
331 | 316 |
public void setOptionUri(String optionUri) { |
332 | 317 |
this.optionUri = optionUri; |
333 | 318 |
} |
334 |
} |
|
319 |
} |
Also available in: Unified diff
reintegrated solr75 branch (-r53777:HEAD)