Revision 2273

tmp/org.txm.searchengine.cqp.core/src/org/txm/searchengine/cqp/corpus/QueryResult.java (revision 2273)
51 51
 * @author jmague
52 52
 */
53 53
public class QueryResult extends Selection implements CqpObject {
54

  
54
	
55 55
	/** The cqp id. */
56 56
	protected String cqpId;
57

  
57
	
58 58
	/** The mother main corpus. */
59 59
	protected MainCorpus motherMainCorpus;
60

  
60
	
61 61
	/** The name. */
62 62
	protected String name;
63

  
63
	
64 64
	/** The n match. */
65 65
	protected int nMatch = -1;
66

  
66
	
67 67
	/** The queried corpus. */
68 68
	protected CQPCorpus queriedCorpus;
69

  
69
	
70 70
	/** The query. */
71 71
	protected CQLQuery query;
72

  
72
	
73 73
	/** The size. */
74 74
	protected int size = -1;
75

  
75
	
76 76
	/**
77 77
	 * Instantiates a new query result.
78 78
	 * 
......
92 92
	 */
93 93
	public QueryResult(String cqpId, String name, CQPCorpus queriedCorpus,
94 94
			CQLQuery query) throws InvalidCqpIdException {
95
		
95 96
		// super(cqpId);
96 97
		checkCqpId(cqpId);
97 98
		this.cqpId = cqpId;
......
100 101
		if (queriedCorpus instanceof Subcorpus) {
101 102
			this.motherMainCorpus = ((Subcorpus) queriedCorpus)
102 103
					.getMainCorpus();
103
		} else {
104
		}
105
		else {
104 106
			this.motherMainCorpus = ((MainCorpus) queriedCorpus);
105 107
		}
106 108
		this.query = query;
107 109
	}
108

  
110
	
109 111
	/*
110 112
	 * (non-Javadoc)
111
	 * 
112 113
	 * @see
113 114
	 * org.txm.searchengine.cqp.corpus.CqpObject#checkCqpId(java.lang
114 115
	 * .String)
115 116
	 */
116 117
	@Override
117 118
	public boolean checkCqpId(String cqpId) throws InvalidCqpIdException {
119
		
118 120
		if (!AbstractCqiClient.checkSubcorpusId(cqpId))
119 121
			throw new InvalidCqpIdException(cqpId + TXMCoreMessages.isNotAValidCQPIdForASubcorpusItMustBeAnUppercaseCharactersFollowedByLowercaseCharacters);
120 122
		return true;
121 123
	}
122

  
124
	
123 125
	/**
124 126
	 * Drops the query results. More specifically, it drops its CQP counterpart.
125 127
	 * 
......
129 131
	 *             the cqi client exception
130 132
	 */
131 133
	public void drop() throws CqiClientException {
134
		
132 135
		try {
133 136
			CorpusManager.getCorpusManager().getCqiClient().dropSubCorpus(this.getQualifiedCqpId());
134
		} catch (Exception e) {
137
		}
138
		catch (Exception e) {
135 139
			throw new CqiClientException(e);
136 140
		}
137 141
	}
138

  
142
	
139 143
	@Override
140 144
	public void finalize() {
141
		//		try { drop(); }
142
		//		catch(Exception e) { }
145
		// try { drop(); }
146
		// catch(Exception e) { }
143 147
	}
144

  
145
	/* (non-Javadoc)
148
	
149
	/*
150
	 * (non-Javadoc)
146 151
	 * @see org.txm.searchengine.cqp.corpus.CqpObject#getCqpId()
147 152
	 */
148 153
	@Override
149 154
	public String getCqpId() {
155
		
150 156
		return cqpId;
151 157
	}
152

  
158
	
153 159
	public int[] getEnds() throws CqiClientException, IOException, CqiServerError {
160
		
154 161
		int n = this.getNMatch();
155
		if( n > 0)
162
		if (n > 0)
156 163
			return getEnds(0, n - 1);
157 164
		else return new int[0];
158 165
	}
159

  
160
	public int[] getEnds(int from, int to) throws UnexpectedAnswerException, IOException, CqiServerError
161
	{
166
	
167
	public int[] getEnds(int from, int to) throws UnexpectedAnswerException, IOException, CqiServerError {
168
		
162 169
		return CorpusManager.getCorpusManager().getCqiClient()
163 170
				.dumpSubCorpus(getQualifiedCqpId(),
164 171
						NetCqiClient.CQI_CONST_FIELD_MATCHEND, from, to);
165 172
	}
166

  
167
	/* (non-Javadoc)
173
	
174
	/*
175
	 * (non-Javadoc)
168 176
	 * @see org.txm.searchengine.cqp.corpus.CqpObject#getMotherMainCorpus()
169 177
	 */
170 178
	@Override
171 179
	public MainCorpus getMainCorpus() {
180
		
172 181
		return motherMainCorpus;
173 182
	}
174

  
183
	
175 184
	/**
176 185
	 * get the Nth match
177 186
	 * 
......
182 191
	 * @throws CqiServerError
183 192
	 */
184 193
	public Match getMatch(int i) throws UnexpectedAnswerException, IOException, CqiServerError {
194
		
185 195
		int[] starts = CorpusManager.getCorpusManager().getCqiClient()
186 196
				.dumpSubCorpus(getQualifiedCqpId(),
187 197
						NetCqiClient.CQI_CONST_FIELD_MATCH, i, i);
......
190 200
						NetCqiClient.CQI_CONST_FIELD_MATCHEND, i, i);
191 201
		return new Match(starts[i], ends[i]);
192 202
	}
193

  
203
	
194 204
	/**
195 205
	 * Gets all the matches.
196 206
	 * 
......
202 212
	 *             the cqi client exception
203 213
	 */
204 214
	public List<Match> getMatches() throws CqiClientException {
215
		
205 216
		int n = this.getNMatch();
206
		if( n > 0)
217
		if (n > 0)
207 218
			return getMatches(0, n - 1);
208 219
		else return new ArrayList<Match>();
209 220
	}
210

  
211

  
212

  
221
	
222
	
223
	
213 224
	/**
214 225
	 * Retrieves some of the matches of a query
215 226
	 * 
......
232 243
	 * @throws CqiClientException
233 244
	 */
234 245
	public List<Match> getMatches(int from, int to) throws CqiClientException {
246
		
235 247
		int[] starts, ends, targets;
236 248
		List<Match> res = new ArrayList<Match>();
237 249
		try {
238 250
			starts = getStarts(from, to);
239 251
			ends = getEnds(from, to);
240
			targets = getTargets(from ,to);
241
		} catch (Exception e) {
252
			targets = getTargets(from, to);
253
		}
254
		catch (Exception e) {
242 255
			throw new CqiClientException(e);
243 256
		}
244 257
		for (int i = 0; i <= to - from; i++) {
......
246 259
		}
247 260
		return res;
248 261
	}
249

  
262
	
250 263
	private int[] getTargets(int from, int to) throws UnexpectedAnswerException, IOException, CqiServerError {
264
		
251 265
		return CorpusManager.getCorpusManager().getCqiClient()
252 266
				.dumpSubCorpus(getQualifiedCqpId(),
253 267
						NetCqiClient.CQI_CONST_FIELD_TARGET, from, to);
254 268
	}
255

  
256
	/* (non-Javadoc)
269
	
270
	/*
271
	 * (non-Javadoc)
257 272
	 * @see org.txm.searchengine.cqp.corpus.CqpObject#getName()
258 273
	 */
259 274
	@Override
260 275
	public String getName() {
276
		
261 277
		return name;
262 278
	}
263

  
279
	
264 280
	/**
265 281
	 * Gets the number of matches.
266 282
	 * 
......
270 286
	 *             the cqi client exception
271 287
	 */
272 288
	public int getNMatch() throws CqiClientException {
273
		//if (nMatch == -1) //
289
		
290
		// if (nMatch == -1) //
274 291
		try {
275 292
			nMatch = CorpusManager.getCorpusManager().getCqiClient()
276 293
					.subCorpusSize(getQualifiedCqpId());
277
		} catch (Exception e) {
294
		}
295
		catch (Exception e) {
278 296
			return 0;
279 297
		}
280 298
		return nMatch;
281 299
	}
282

  
300
	
283 301
	/*
284 302
	 * (non-Javadoc)
285
	 * 
286 303
	 * @see
287 304
	 * org.txm.searchengine.cqp.corpus.CqpObject#getQualifiedCqpId()
288 305
	 */
289 306
	@Override
290 307
	public String getQualifiedCqpId() {
308
		
291 309
		return motherMainCorpus.getCqpId() + ":" + this.cqpId; //$NON-NLS-1$
292 310
	}
293

  
311
	
294 312
	/**
295 313
	 * Gets the queried corpus.
296 314
	 * 
297 315
	 * @return the queried corpus
298 316
	 */
299 317
	public CQPCorpus getQueriedCorpus() {
318
		
300 319
		return queriedCorpus;
301 320
	}
302

  
303
	//	/**
304
	//	 * Gets all the matches.
305
	//	 * WARNING: on win32 OS, this function may takesQUETE a long time
306
	//	 * 
307
	//	 * @return the matches
308
	//	 * 
309
	//	 * @throws CqiClientException
310
	//	 *             the cqi client exception
311
	//	 */
312
	//	public List<Match> getRandomMatches(float perc) throws CqiClientException {
313
	//		
314
	//		ArrayList<Match> allmatches = new ArrayList<Match>(getMatches(0, getNMatch() - 1));
315
	//		ArrayList<Match> sub = new ArrayList<Match>();
316
	//		int count = 0;
317
	//		for(Match m : allmatches)
318
	//		{
319
	//			if(count >= size)
320
	//				return sub;
321
	//			count++;
322
	//			if(Math.random() > perc)
323
	//				sub.add(m);
324
	//		}
325
	//		return sub;
326
	//	}
327

  
321
	
322
	// /**
323
	// * Gets all the matches.
324
	// * WARNING: on win32 OS, this function may takesQUETE a long time
325
	// *
326
	// * @return the matches
327
	// *
328
	// * @throws CqiClientException
329
	// * the cqi client exception
330
	// */
331
	// public List<Match> getRandomMatches(float perc) throws CqiClientException {
332
	//
333
	// ArrayList<Match> allmatches = new ArrayList<Match>(getMatches(0, getNMatch() - 1));
334
	// ArrayList<Match> sub = new ArrayList<Match>();
335
	// int count = 0;
336
	// for(Match m : allmatches)
337
	// {
338
	// if(count >= size)
339
	// return sub;
340
	// count++;
341
	// if(Math.random() > perc)
342
	// sub.add(m);
343
	// }
344
	// return sub;
345
	// }
346
	
328 347
	/**
329 348
	 * Gets the query.
330 349
	 * 
331 350
	 * @return the query
332 351
	 */
333 352
	public CQLQuery getQuery() {
353
		
334 354
		return query;
335 355
	}
336

  
356
	
337 357
	/**
338 358
	 * Retrieves some of the matches of a query
339 359
	 * 
......
356 376
	 * @throws CqiClientException
357 377
	 */
358 378
	public int[] getStartMatches(int from, int to) throws CqiClientException {
379
		
359 380
		int n = this.getNMatch();
360 381
		if (to > n) to = n;
361 382
		try {
362 383
			return CorpusManager.getCorpusManager().getCqiClient()
363 384
					.dumpSubCorpus(getQualifiedCqpId(),
364 385
							NetCqiClient.CQI_CONST_FIELD_MATCH, from, to);
365
		} catch (Exception e) {
386
		}
387
		catch (Exception e) {
366 388
			throw new CqiClientException(e);
367 389
		}
368 390
	}
369

  
391
	
370 392
	public int[] getStarts() throws CqiClientException, IOException, CqiServerError {
393
		
371 394
		int n = this.getNMatch();
372 395
		if (n > 0)
373 396
			return getStarts(0, n - 1);
374 397
		else return new int[0];
375 398
	}
376

  
377
	public int[] getStarts(int from, int to) throws UnexpectedAnswerException, IOException, CqiServerError
378
	{
399
	
400
	public int[] getStarts(int from, int to) throws UnexpectedAnswerException, IOException, CqiServerError {
401
		
379 402
		return CorpusManager.getCorpusManager().getCqiClient()
380 403
				.dumpSubCorpus(getQualifiedCqpId(),
381 404
						NetCqiClient.CQI_CONST_FIELD_MATCH, from, to);
382 405
	}
383

  
406
	
384 407
	public String toString() {
408
		
385 409
		return this.getQualifiedCqpId();
386 410
	}
387

  
411
	
388 412
	@Override
389 413
	public boolean isTargetUsed() throws UnexpectedAnswerException, IOException, CqiServerError {
414
		
390 415
		return CQPSearchEngine.getCqiClient().subCorpusHasField(this.getQualifiedCqpId(), ICqiClient.CQI_CONST_FIELD_TARGET);
391 416
	}
392

  
417
	
393 418
	@Override
394 419
	public boolean delete(int iMatch) {
420
		
395 421
		AbstractCqiClient CQI = CorpusManager.getCorpusManager().getCqiClient();
396 422
		if (CQI instanceof MemCqiClient) {
397
			MemCqiClient MCQI = (MemCqiClient)CQI;
398

  
399
			String query = "delete "+this.getQualifiedCqpId()+" "+iMatch+";";
423
			MemCqiClient MCQI = (MemCqiClient) CQI;
424
			
425
			String query = "delete " + this.getQualifiedCqpId() + " " + iMatch + ";";
400 426
			try {
401 427
				MCQI.query(query);
402 428
				size = -1;
403 429
				nMatch = -1;
404 430
				return true;
405
			} catch (UnexpectedAnswerException | IOException | CqiServerError e) {
431
			}
432
			catch (UnexpectedAnswerException | IOException | CqiServerError e) {
406 433
				// TODO Auto-generated catch block
407 434
				e.printStackTrace();
408 435
			}
409 436
		}
410 437
		return false;
411 438
	}
412

  
439
	
413 440
	@Override
414 441
	public boolean delete(org.txm.objects.Match match) {
415

  
442
		
416 443
		AbstractCqiClient CQI = CorpusManager.getCorpusManager().getCqiClient();
417 444
		if (CQI instanceof MemCqiClient) {
418 445
			try {
419
				MemCqiClient MCQI = (MemCqiClient)CQI;
420

  
446
				MemCqiClient MCQI = (MemCqiClient) CQI;
447
				
421 448
				Match m2;
422 449
				List<? extends Match> allMatches = getMatches();
423 450
				int to = allMatches.size();
424
				for (int i = 0 ; i < to; i++) {
451
				for (int i = 0; i < to; i++) {
425 452
					m2 = allMatches.get(i);
426 453
					if (m2.getStart() == match.getStart() && m2.getEnd() == match.getEnd()) {
427
						String query = "delete "+this.getQualifiedCqpId()+" "+i+";";
428

  
454
						String query = "delete " + this.getQualifiedCqpId() + " " + i + ";";
455
						
429 456
						MCQI.query(query);
430

  
457
						
431 458
						size = -1;
432 459
						nMatch = -1;
433 460
						return true;
434 461
					}
435 462
				}
436
			} catch (Exception e) {
463
			}
464
			catch (Exception e) {
437 465
				// TODO Auto-generated catch block
438 466
				e.printStackTrace();
439 467
			}
440 468
		}
441

  
469
		
442 470
		return false;
443 471
	}
444

  
472
	
445 473
	@Override
446 474
	public boolean delete(ArrayList<org.txm.objects.Match> matchesToRemove) {
475
		
447 476
		AbstractCqiClient CQI = CorpusManager.getCorpusManager().getCqiClient();
448 477
		List<? extends Match> allMatches;
449 478
		try {
450 479
			allMatches = getMatches();
451

  
480
			
452 481
			int to = allMatches.size();
453 482
			int c = 0;
454 483
			if (CQI instanceof MemCqiClient) {
455
				MemCqiClient MCQI = (MemCqiClient)CQI;
484
				MemCqiClient MCQI = (MemCqiClient) CQI;
456 485
				for (org.txm.objects.Match m : matchesToRemove) {
457 486
					Match m2;
458

  
459
					for (int i = 0 ; i < to; i++) {
487
					
488
					for (int i = 0; i < to; i++) {// TODO may be a bit faster since allMatches&matchesToRemove are ordered
460 489
						m2 = allMatches.get(i);
461 490
						if (m2.getStart() == m.getStart() && m2.getEnd() == m.getEnd()) {
462
							String query = "delete "+this.getQualifiedCqpId()+" "+(i-c)+";"; // i-c because lines have been removed and matches are ordered
491
							String query = "delete " + this.getQualifiedCqpId() + " " + (i) + ";";
463 492
							MCQI.query(query);
464 493
							c++;
465 494
							break; // one match per line
......
468 497
				}
469 498
				return c > 0;
470 499
			}
471
		} catch (Exception e1) {
500
		}
501
		catch (Exception e1) {
472 502
			// TODO Auto-generated catch block
473 503
			e1.printStackTrace();
474 504
		}
475 505
		return false;
476 506
	}
477
}
507
}

Also available in: Unified diff