Project

General

Profile

1
package eu.dnetlib.functionality.index.query;
2

    
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;
8

    
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
import com.google.common.collect.Iterables;
21
import com.google.common.collect.Lists;
22
import com.google.common.collect.Maps;
23

    
24
import eu.dnetlib.miscutils.collections.MappedCollection;
25
import eu.dnetlib.miscutils.functional.UnaryFunction;
26

    
27
/**
28
 * Use this class to cleanup a CQL tree and obtain all the options
29
 * 
30
 * @author marko & claudio
31
 * 
32
 */
33
public class Pruner {
34
	private static final Log log = LogFactory.getLog(Pruner.class); // NOPMD by marko on 11/24/08 5:02 PM
35

    
36
	/**
37
	 * All options have to be in this namespace.
38
	 */
39
	public static final String DNET_URI = "NAMESPACE";
40

    
41
	private String optionUri = DNET_URI;
42

    
43
	/**
44
	 * Helper method, parse a given CQL string.
45
	 * 
46
	 * @param cqlQuery
47
	 * @return
48
	 * @throws CQLParseException
49
	 * @throws IOException
50
	 */
51
	CQLNode parse(final String cqlQuery) throws CQLParseException, IOException {
52
		return new CQLParser().parse(cqlQuery);
53
	}
54

    
55
	class Result {
56
		private CQLNode node;
57
		private List<String> options;
58

    
59
		public Result(final CQLNode node, final List<String> options) {
60
			super();
61
			this.node = node;
62
			this.options = options;
63
		}
64

    
65
		public Result(final CQLNode node, final Iterable<String> concat) {
66
			this.node = node;
67
			this.options = Lists.newArrayList(concat);
68
		}
69

    
70
		public CQLNode getNode() {
71
			return node;
72
		}
73

    
74
		public void setNode(final CQLNode node) {
75
			this.node = node;
76
		}
77

    
78
		public List<String> getOptions() {
79
			return options;
80
		}
81

    
82
		public void setOptions(final List<String> options) {
83
			this.options = options;
84
		}
85

    
86
		public Map<String, List<String>> getOptionMap() {
87
			Map<String, List<String>> res = new HashMap<String, List<String>>();
88
			for (String opt : options) {
89
				String[] k = opt.split("=");
90
				List<String> l = res.get(k[0]);
91
				if(l == null)
92
					l = new ArrayList<String>();
93
				l.add(k[1]);
94
				res.put(k[0], l);
95
			}
96
			return res;
97
		}
98
	}
99

    
100
	/**
101
	 * Remove all options from a given CQL AST and return all the options.
102
	 * 
103
	 * The CQL tree is modified.
104
	 * 
105
	 * @param root
106
	 *            cql tree
107
	 * @return pair containing a new root node and a list of options
108
	 */
109
	public Result prune(final CQLNode root) {
110
		return prune(new HashMap<String, String>(), root);
111
	}
112

    
113
	/**
114
	 * Actual recursive implementation, dispatches the implementation to the appropriate overloaded method.
115
	 * 
116
	 * @param prefixes
117
	 * @param root
118
	 * @return
119
	 */
120
	public Result prune(final Map<String, String> prefixes, final CQLNode root) {
121

    
122
		if (root instanceof CQLBooleanNode)
123
			return prune(prefixes, (CQLBooleanNode) root);
124

    
125
		if (root instanceof CQLPrefixNode)
126
			return prune(prefixes, (CQLPrefixNode) root);
127

    
128
		if (root instanceof CQLSortNode)
129
			return prune(prefixes, (CQLSortNode) root);		
130
		
131
		return new Result(root, new ArrayList<String>());
132
	}
133
	
134
	/**
135
	 * If the current node is a cql "sort" node, just return the inner subtree.
136
	 * 
137
	 * @param prefixes
138
	 * @param node
139
	 * @return
140
	 */
141
	public Result prune(final Map<String, String> prefixes, final CQLSortNode node) {	
142
		Result res = prune(prefixes, node.subtree);
143
		node.subtree = res.getNode();
144
		res.setNode(node);
145
		return res;
146
	}
147

    
148
	/**
149
	 * If the current node is a cql "prefix" node, add his namespace declaration to the current list of namespaces and
150
	 * return the pruned inner subtree.
151
	 * 
152
	 * If the prefix node contains only one single option element, we have to return null. (TODO: perhaps there is a
153
	 * better solution).
154
	 * 
155
	 * @param prefixes
156
	 * @param node
157
	 * @return
158
	 */
159
	public Result prune(final Map<String, String> prefixes, final CQLPrefixNode node) {
160
		final HashMap<String, String> subPrefixes = Maps.newHashMap(prefixes);
161
		subPrefixes.put(node.prefix.name, node.prefix.identifier);
162
		
163
		if (isOption(subPrefixes, node.subtree))
164
			return new Result(null, Lists.newArrayList(getOption(node.subtree)));
165

    
166
		boolean pruneThisPrefix = node.prefix.identifier.equals(optionUri);
167
		if(pruneThisPrefix)
168
			return prune(subPrefixes, node.subtree);	
169
		
170
		Result res = prune(subPrefixes, node.subtree);
171
		node.subtree = res.getNode();
172
		res.setNode(node);
173
		return res;
174
		
175
	}
176

    
177
	/**
178
	 * boolean prunes are handled in the prune(prefix, node, left, right).
179
	 * 
180
	 * @param prefixes
181
	 * @param node
182
	 * @return
183
	 */
184
	public Result prune(final Map<String, String> prefixes, final CQLBooleanNode node) {
185
		return prune(prefixes, node, node.left, node.right);
186
	}
187

    
188
	/**
189
	 * Detects if a left or right side of a boolean node is a option term, and returns the other side (recursively
190
	 * pruned). It also returns the accumulated options along the way.
191
	 * 
192
	 * @param prefixes
193
	 * @param bool
194
	 * @param left
195
	 * @param right
196
	 * @return
197
	 */
198
	public Result prune(final Map<String, String> prefixes, final CQLBooleanNode bool, final CQLNode left, final CQLNode right) {
199

    
200
		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
					})));
210
		}
211

    
212
		Result res = anyNotNull(trimOption(prefixes, left, right), trimOption(prefixes, right, left));
213

    
214
		if (res != null)
215
			return res;
216

    
217
		final Result leftResult = prune(prefixes, left);
218
		final Result rightResult = prune(prefixes, right);
219

    
220
		bool.left = leftResult.getNode();
221
		bool.right = rightResult.getNode();
222
		return new Result(clean(bool), Iterables.concat(leftResult.getOptions(), rightResult.getOptions()));
223
	}
224

    
225
	public <T> T anyNotNull(T a, T b) {
226
		if (a != null)
227
			return a;
228
		return b;
229
	}
230

    
231
	/**
232
	 * Trims an option from a boolean node if one if it's sides is an option term.
233
	 * 
234
	 * Intended to be used once for each sides and then swap.
235
	 * 
236
	 * @param prefixes
237
	 * @param a
238
	 * @param b
239
	 * @return
240
	 */
241
	public Result trimOption(final Map<String, String> prefixes, final CQLNode a, final CQLNode b) {
242
		log.debug("trim option?" + prefixes + " a " + a.toCQL());
243
		if (isOption(prefixes, a)) {
244
			log.debug("IS OPTION...");
245
			return trimOption(prefixes, prefixFromOption(a), getOption(a), b);
246
		}
247
		log.debug("IS NOT OPTION");
248
		return null;
249
	}
250

    
251
	/**
252
	 * prune(prefixes, bool, left, right) uses this helper method to do the dirty job:
253
	 * 
254
	 * we have to detect if a term node is a term option node. by checking the namespace uri associated with the term
255
	 * prefix according the the current namespace prefix scope (held in prefixes, which is passed down recursively by
256
	 * copy).
257
	 * 
258
	 * @param prefixes
259
	 * @param ns
260
	 * @param o
261
	 * @param subtree
262
	 * @return
263
	 */
264
	public Result trimOption(final Map<String, String> prefixes, final String ns, final String o, final CQLNode subtree) {
265
		log.debug("trimming " + prefixes + " ns " + ns + " o " + o);
266
		
267
		final String namespaceUri = prefixes.get(ns);
268

    
269
		if (!optionUri.equals(namespaceUri)) {
270
			return null;
271
		}
272

    
273
		final Result res = prune(prefixes, subtree);
274
		return new Result(res.getNode(), Iterables.concat(Lists.newArrayList(o), res.getOptions()));
275
	}
276

    
277
	/**
278
	 * Drop a boolean node (and, or etc) if one of the sides has been dropped.
279
	 * 
280
	 * @param bool
281
	 * @return
282
	 */
283
	private CQLNode clean(final CQLBooleanNode bool) {
284
		if (bool.left == null)
285
			return bool.right;
286
		if (bool.right == null)
287
			return bool.left;
288
		return bool;
289
	}
290

    
291
	////////////////// helpers
292

    
293
	public String getOption(final CQLNode node) {
294
		return indexFromOption(node) + "=" + termFromOption(node);
295
	}
296

    
297
	private String indexFromOption(final CQLNode node) {
298
		return ((CQLTermNode) node).getIndex().replaceAll("[a-z]*\\.(.+)", "$1");
299
	}
300

    
301
	private String termFromOption(final CQLNode node) {
302
		return ((CQLTermNode) node).getTerm();
303
	}
304

    
305
	public String prefixFromOption(final String option) {
306
		return option.replaceAll("([a-z]*)\\..+", "$1");
307
	}
308

    
309
	public String prefixFromOption(final CQLNode node) {
310
		if (node instanceof CQLTermNode)
311
			return prefixFromOption(((CQLTermNode) node).getIndex());
312

    
313
		return null;
314
	}
315

    
316
	public boolean isOption(final Map<String, String> prefixes, final String option) {
317
		return prefixes.containsKey(prefixFromOption(option)) && prefixes.get(prefixFromOption(option)).equals(getOptionUri());
318
	}
319

    
320
	public boolean isOption(final Map<String, String> prefixes, final CQLNode node) {
321
		if (node instanceof CQLTermNode)
322
			return isOption(prefixes, ((CQLTermNode) node).getIndex());
323

    
324
		return false;
325
	}
326

    
327
	public String getOptionUri() {
328
		return optionUri;
329
	}
330

    
331
	public void setOptionUri(String optionUri) {
332
		this.optionUri = optionUri;
333
	}
334
}
(5-5/9)