41 |
41 |
import java.util.List;
|
42 |
42 |
import java.util.Locale;
|
43 |
43 |
|
|
44 |
import org.apache.commons.lang.StringUtils;
|
44 |
45 |
import org.eclipse.osgi.util.NLS;
|
45 |
46 |
import org.txm.core.messages.TXMCoreMessages;
|
|
47 |
import org.txm.core.preferences.TBXPreferences;
|
|
48 |
import org.txm.core.preferences.TXMPreferences;
|
|
49 |
import org.txm.core.results.Parameter;
|
|
50 |
import org.txm.core.results.TXMParameters;
|
46 |
51 |
import org.txm.core.results.TXMResult;
|
47 |
52 |
import org.txm.objects.TxmObject;
|
|
53 |
import org.txm.searchengine.cqp.CQPPreferences;
|
48 |
54 |
import org.txm.searchengine.cqp.CQPSearchEngine;
|
49 |
55 |
import org.txm.searchengine.cqp.clientExceptions.CqiClientException;
|
50 |
56 |
import org.txm.searchengine.cqp.corpus.query.Query;
|
51 |
57 |
import org.txm.searchengine.cqp.corpus.query.QueryPart;
|
|
58 |
import org.txm.utils.io.IOUtils;
|
52 |
59 |
import org.txm.utils.logger.Log;
|
53 |
60 |
import org.w3c.dom.Document;
|
54 |
61 |
import org.w3c.dom.Element;
|
... | ... | |
69 |
76 |
/** The Constant serialVersionUID. */
|
70 |
77 |
private static final long serialVersionUID = 4420732757687971253L;
|
71 |
78 |
|
72 |
|
/** The parts. */
|
73 |
|
private List<Part> parts;
|
|
79 |
/**
|
|
80 |
* Get the safe values (spacial char regexp safe).
|
|
81 |
*
|
|
82 |
* @param property the property
|
|
83 |
* @param corpus the corpus
|
|
84 |
* @return the safe values
|
|
85 |
* @throws CqiClientException the cqi client exception
|
|
86 |
*/
|
|
87 |
private static List<String> getSafeValues(StructuralUnitProperty property, Corpus corpus) throws CqiClientException {
|
|
88 |
List<String> values = property.getValues(corpus);
|
|
89 |
for(int i = 0 ; i < values.size() ; i++)
|
|
90 |
values.set(i, Query.addBackSlash(values.get(i)));
|
|
91 |
return values;
|
|
92 |
}
|
74 |
93 |
|
75 |
|
/** The structure. */
|
76 |
|
private StructuralUnit structure;
|
|
94 |
/** The property, if not null the partition will forge its own queries */
|
|
95 |
@Parameter(key=TXMPreferences.STRUCTURAL_UNIT_PROPERTY)
|
|
96 |
protected StructuralUnitProperty pProperty;
|
|
97 |
/**
|
|
98 |
* the parts property values to use, may be null
|
|
99 |
*/
|
|
100 |
@Parameter(key=TXMPreferences.VALUES)
|
|
101 |
protected List<String> pValues;
|
|
102 |
/**
|
|
103 |
* the parts queries
|
|
104 |
*/
|
|
105 |
@Parameter(key=TXMPreferences.QUERIES)
|
|
106 |
protected List<String> pQueries;
|
|
107 |
/**
|
|
108 |
* the parts names -> do the parts order
|
|
109 |
*/
|
|
110 |
@Parameter(key=CQPPreferences.PARTNAMES)
|
|
111 |
protected List<String> pPartNames;
|
|
112 |
/**
|
|
113 |
* the partition name to set
|
|
114 |
*/
|
|
115 |
@Parameter(key=TXMPreferences.LAZY_NAME)
|
|
116 |
protected String pName;
|
|
117 |
/** sum of parts size, stored for efficiency reasons**/
|
|
118 |
int totalsize = -1;
|
77 |
119 |
|
78 |
|
/** The property. */
|
79 |
|
private Property property;
|
80 |
120 |
|
81 |
|
|
82 |
|
|
83 |
|
int totalsize = -1;
|
84 |
|
int[] partSizes;
|
|
121 |
// /**
|
|
122 |
// * Create a partition with a set of raw queries.
|
|
123 |
// *
|
|
124 |
// * @param corpus the corpus
|
|
125 |
// * @param name the name
|
|
126 |
// * @param queries the queries
|
|
127 |
// * @throws CqiClientException the cqi client exception
|
|
128 |
// * @author Sylvain Loiseau
|
|
129 |
// */
|
|
130 |
// protected Partition(Corpus corpus, String name, List<String> queries) throws CqiClientException {
|
|
131 |
// this(corpus, name, queries, null);
|
|
132 |
// }
|
85 |
133 |
|
|
134 |
public Partition(Corpus corpus) {
|
|
135 |
super(corpus);
|
|
136 |
}
|
|
137 |
|
|
138 |
public Partition(String uuid) {
|
|
139 |
super(uuid);
|
|
140 |
}
|
86 |
141 |
/**
|
|
142 |
* Instantiates a new partition.
|
|
143 |
*
|
|
144 |
* @param corpus the corpus
|
|
145 |
* @param name the name
|
|
146 |
* @param queries the queries
|
|
147 |
* @param partnames the partnames
|
|
148 |
* @throws Exception
|
|
149 |
*/
|
|
150 |
public boolean _compute_with_lists() throws Exception {
|
|
151 |
|
|
152 |
if (pName == null || pName.length() == 0) {
|
|
153 |
pName = "no_name";
|
|
154 |
}
|
|
155 |
|
|
156 |
if (name == null || name.trim().length() == 0) {
|
|
157 |
this.name = pName;
|
|
158 |
}
|
|
159 |
|
|
160 |
Log.info(NLS.bind(TXMCoreMessages.NEW_PARTION, this.getParent(), this.name));
|
|
161 |
long start = System.currentTimeMillis();
|
|
162 |
for (int i = 0; i < pQueries.size(); i++) {
|
|
163 |
String queryS = pQueries.get(i);
|
|
164 |
String partitionName = this.getName();
|
|
165 |
|
|
166 |
String partName = String.valueOf(i);
|
|
167 |
if(pPartNames != null && i < pPartNames.size()) {
|
|
168 |
partName = pPartNames.get(i);
|
|
169 |
}
|
|
170 |
if (partName.trim().length() == 0) {
|
|
171 |
partName = "-"; //$NON-NLS-1$
|
|
172 |
}
|
|
173 |
|
|
174 |
Part part = createPart(partitionName, partName, queryS);
|
|
175 |
}
|
|
176 |
long end = System.currentTimeMillis();
|
|
177 |
Log.info(NLS.bind(TXMCoreMessages.PARTITION_CREATED, this.name, (end - start)));
|
|
178 |
|
|
179 |
return pQueries.size() > 0;
|
|
180 |
}
|
|
181 |
|
|
182 |
/**
|
87 |
183 |
* Creates a new partition on a corpus given a structure, a property of this
|
88 |
|
* structure and a liste of values this property can take
|
|
184 |
* structure and a list of values this property can take
|
89 |
185 |
*
|
90 |
186 |
* The partition has as many parts as the size of the list of value. The ith
|
91 |
187 |
* part is composed by all the structure <code>structure</code> such as
|
... | ... | |
98 |
194 |
* @param property
|
99 |
195 |
* the property
|
100 |
196 |
* @param values
|
101 |
|
* the values
|
102 |
|
*
|
103 |
|
* @throws CqiClientException
|
104 |
|
* the cqi client exception
|
|
197 |
* the values, raw values special chars will be backslashed
|
|
198 |
* @throws Exception
|
105 |
199 |
*/
|
106 |
|
protected Partition(Corpus corpus, StructuralUnit structure, StructuralUnitProperty property, List<String> values) throws CqiClientException {
|
107 |
|
super(corpus);
|
108 |
|
|
109 |
|
this.name = corpus.getName()+ "_" + structure.getName() + "_" + property.getName(); //$NON-NLS-1$ //$NON-NLS-2$
|
110 |
|
|
111 |
|
Log.finest(NLS.bind(TXMCoreMessages.NEW_PARTION, this.getCorpus(), this.name));
|
112 |
|
|
113 |
|
long start = System.currentTimeMillis();
|
114 |
|
this.structure = structure;
|
115 |
|
this.property = property;
|
116 |
|
this.parts = new ArrayList<Part>();
|
117 |
|
HashMap<String, String> infos = this.getCorpus().getSAttributesInfos().get(property.getFullName());
|
|
200 |
|
|
201 |
protected boolean _compute_with_property() throws Exception {
|
|
202 |
|
|
203 |
Corpus corpus = getParent();
|
|
204 |
if (pName == null || pName.length() == 0) {
|
|
205 |
pName = corpus.getName()+ "_" + pProperty.getFullName(); //$NON-NLS-1$
|
|
206 |
}
|
|
207 |
|
|
208 |
if (pValues == null) {
|
|
209 |
pValues = pProperty.getValues(corpus);
|
|
210 |
}
|
|
211 |
|
|
212 |
// sort parts
|
|
213 |
HashMap<String, String> infos = this.getParent().getSAttributesInfos().get(pProperty.getFullName());
|
|
214 |
|
118 |
215 |
String type = "String"; //$NON-NLS-1$
|
119 |
216 |
if (infos != null) {
|
120 |
217 |
type = infos.get("type"); //$NON-NLS-1$
|
121 |
218 |
}
|
122 |
219 |
if (type == null || type.trim().length() == 0 || type.equals("String")) { //$NON-NLS-1$
|
123 |
|
Collections.sort(values, Collator.getInstance(Locale.getDefault())); // alpha sort
|
|
220 |
Collections.sort(pValues, Collator.getInstance(Locale.getDefault())); // alpha sort
|
124 |
221 |
} else if (type.equals("Integer")) { //$NON-NLS-1$
|
125 |
|
Collections.sort(values, new Comparator<String>() {
|
|
222 |
Collections.sort(pValues, new Comparator<String>() {
|
126 |
223 |
@Override
|
127 |
224 |
public int compare(String arg0, String arg1) {
|
128 |
225 |
int i0 = Integer.parseInt(arg0);
|
... | ... | |
133 |
230 |
} else if (type.equals("Date")) { //$NON-NLS-1$
|
134 |
231 |
String format = infos.get("inputFormat"); //$NON-NLS-1$
|
135 |
232 |
final DateFormat formater = new SimpleDateFormat(format);
|
136 |
|
Collections.sort(values, new Comparator<String>() {
|
|
233 |
Collections.sort(pValues, new Comparator<String>() {
|
137 |
234 |
@Override
|
138 |
235 |
public int compare(String arg0, String arg1) {
|
139 |
236 |
try {
|
... | ... | |
147 |
244 |
});
|
148 |
245 |
} else if(type.contains("|")){ //$NON-NLS-1$
|
149 |
246 |
final List<String> sortedValues = Arrays.asList(type.split("\\|")); //$NON-NLS-1$
|
150 |
|
Collections.sort(values, new Comparator<String>() {
|
|
247 |
Collections.sort(pValues, new Comparator<String>() {
|
151 |
248 |
@Override
|
152 |
249 |
public int compare(String arg0, String arg1) {
|
153 |
|
int i0 = sortedValues.indexOf(arg0);
|
154 |
|
int i1 = sortedValues.indexOf(arg1);
|
155 |
|
return i0-i1;
|
|
250 |
int i0 = sortedValues.indexOf(arg0);
|
|
251 |
int i1 = sortedValues.indexOf(arg1);
|
|
252 |
return i0-i1;
|
156 |
253 |
}
|
157 |
254 |
});
|
158 |
255 |
}
|
159 |
|
//System.out.println("Parts sorted with type: "+type);
|
160 |
|
//System.out.println("values: "+values);
|
161 |
|
for (String value : values) {
|
162 |
|
String partitionName = this.getName();
|
|
256 |
|
|
257 |
pQueries = new ArrayList<>();
|
|
258 |
pPartNames = new ArrayList<>();
|
|
259 |
for (String value : pValues) {
|
163 |
260 |
String partName = value.replace("\\",""); //$NON-NLS-1$ //$NON-NLS-2$
|
164 |
|
//System.out.println("VALUE: "+value);
|
165 |
|
Query query = new QueryPart(structure, property, Query.addBackSlash(value));
|
166 |
|
Part part = createPart(partitionName, partName, query);
|
167 |
|
// System.out.println("part id: "+part.getQualifiedCqpId());
|
168 |
|
parts.add(part);
|
|
261 |
pPartNames.add(partName);
|
|
262 |
Query query = new QueryPart(pProperty.getStructuralUnit(), pProperty, Query.addBackSlash(value));
|
|
263 |
pQueries.add(query.getQueryString());
|
169 |
264 |
}
|
170 |
|
long end = System.currentTimeMillis();
|
171 |
|
Log.finest(NLS.bind(TXMCoreMessages.PARTITION_CREATED, this.name, (end - start)));
|
|
265 |
|
|
266 |
return _compute_with_lists();
|
172 |
267 |
}
|
173 |
268 |
|
174 |
|
|
175 |
|
/**
|
176 |
|
* Instantiates a new partition.
|
177 |
|
*
|
178 |
|
* @param corpus the corpus
|
179 |
|
* @param name the name
|
180 |
|
* @param queries the queries
|
181 |
|
* @param partnames the partnames
|
182 |
|
* @throws CqiClientException the cqi client exception
|
|
269 |
|
|
270 |
@Override
|
|
271 |
protected boolean _compute() throws Exception {
|
|
272 |
|
|
273 |
name = null;
|
|
274 |
Log.finest(NLS.bind(TXMCoreMessages.NEW_PARTION, this.getParent(), this.name));
|
|
275 |
|
|
276 |
if (pProperty != null) {
|
|
277 |
_compute_with_property();
|
|
278 |
} else {
|
|
279 |
_compute_with_lists();
|
|
280 |
}
|
|
281 |
return true;
|
|
282 |
}
|
|
283 |
|
|
284 |
/* (non-Javadoc)
|
|
285 |
* @see org.txm.objects.TxmObject#load()
|
183 |
286 |
*/
|
184 |
|
public Partition(Corpus corpus, String name, List<String> queries, List<String> partnames) throws CqiClientException {
|
185 |
|
super(corpus);
|
|
287 |
@Override
|
|
288 |
protected boolean _load() {
|
|
289 |
if (this.getUUID().length() > 0) return true; // partition already persisted
|
186 |
290 |
|
187 |
|
this.structure = null;
|
188 |
|
this.property = null;
|
189 |
|
if (name == null || name.trim().length() == 0) {
|
190 |
|
name = "noname"; //$NON-NLS-1$
|
191 |
|
}
|
192 |
|
this.name = name;
|
193 |
|
Log.info(NLS.bind(TXMCoreMessages.NEW_PARTION, this.getCorpus(), this.name));
|
194 |
|
long start = System.currentTimeMillis();
|
195 |
|
this.parts = new ArrayList<Part>();
|
196 |
|
for (int i = 0; i < queries.size(); i++) {
|
197 |
|
String queryS = queries.get(i);
|
198 |
|
String partitionName = this.getName();
|
199 |
|
|
200 |
|
String partName = String.valueOf(i);
|
201 |
|
if(partnames != null && i < partnames.size()) {
|
202 |
|
partName = partnames.get(i);
|
|
291 |
if (this.getSelfElement() != null && this.getParent() != null) {
|
|
292 |
NodeList partList = getSelfElement().getChildNodes();
|
|
293 |
for (int i = 0; i < partList.getLength(); i++) {
|
|
294 |
if (partList.item(i).getNodeType() == 1) {
|
|
295 |
Element c = (Element) partList.item(i);
|
|
296 |
if (c.getNodeName().equals("part")) //$NON-NLS-1$
|
|
297 |
{
|
|
298 |
String pname = c.getAttribute("name"); //$NON-NLS-1$
|
|
299 |
String pshortname = c.getAttribute("shortname"); //$NON-NLS-1$
|
|
300 |
String pquery = c.getAttribute("query"); //$NON-NLS-1$
|
|
301 |
for (Part part : getParts())
|
|
302 |
if (part.getName().equals(pname)) {
|
|
303 |
part.setName(pname);
|
|
304 |
part.setShortName(pshortname);
|
|
305 |
part.setQuery(pquery);
|
|
306 |
}
|
|
307 |
}
|
|
308 |
}
|
203 |
309 |
}
|
204 |
|
if (partName.trim().length() == 0) {
|
205 |
|
partName = "-"; //$NON-NLS-1$
|
206 |
|
}
|
207 |
|
|
208 |
|
Part part = createPart(partitionName, partName, queryS);
|
209 |
|
parts.add(part);
|
|
310 |
this.loadMetadata();
|
|
311 |
return true;
|
210 |
312 |
}
|
211 |
|
long end = System.currentTimeMillis();
|
212 |
|
Log.info(NLS.bind(TXMCoreMessages.PARTITION_CREATED, this.name, (end - start)));
|
|
313 |
|
|
314 |
return false;
|
213 |
315 |
}
|
214 |
316 |
|
215 |
|
|
216 |
|
|
217 |
|
/**
|
218 |
|
* Instantiates a new partition.
|
219 |
|
*
|
220 |
|
* @param corpus the corpus
|
221 |
|
* @param structure the structure
|
222 |
|
* @param property the property
|
223 |
|
* @throws CqiClientException the cqi client exception
|
224 |
|
*/
|
225 |
|
protected Partition(Corpus corpus, StructuralUnit structure, StructuralUnitProperty property) throws CqiClientException {
|
226 |
|
this(corpus, structure, property, property.getValues(corpus));
|
|
317 |
@Override
|
|
318 |
protected boolean _save() {
|
|
319 |
return false;
|
227 |
320 |
}
|
228 |
321 |
|
|
322 |
@Override
|
|
323 |
public boolean canCompute() {
|
|
324 |
return getParent() != null && (pProperty != null || (pQueries != null && pQueries.size() > 0));
|
|
325 |
}
|
229 |
326 |
|
230 |
|
/**
|
231 |
|
* Create a partition with a set of raw queries.
|
232 |
|
*
|
233 |
|
* @param corpus the corpus
|
234 |
|
* @param name the name
|
235 |
|
* @param queries the queries
|
236 |
|
* @throws CqiClientException the cqi client exception
|
237 |
|
* @author Sylvain Loiseau
|
238 |
|
*/
|
239 |
|
protected Partition(Corpus corpus, String name, List<String> queries) throws CqiClientException {
|
240 |
|
this(corpus, name, queries, null);
|
|
327 |
@Override
|
|
328 |
public void clean() {
|
|
329 |
// TODO Auto-generated method stub
|
|
330 |
|
241 |
331 |
}
|
242 |
332 |
|
|
333 |
@Override
|
|
334 |
public int compareTo(TXMResult o) {
|
|
335 |
// Compare node weights
|
|
336 |
int output = super.compareTo(o);
|
|
337 |
// Compare simple names if weights are equal
|
|
338 |
if(output == 0) {
|
|
339 |
output = this.getSimpleName().compareToIgnoreCase(o.getSimpleName());
|
|
340 |
}
|
|
341 |
return output;
|
|
342 |
}
|
243 |
343 |
|
244 |
344 |
/**
|
245 |
|
* Get the safe values (spacial char regexp safe).
|
|
345 |
* Creates the part.
|
246 |
346 |
*
|
247 |
|
* @param property the property
|
248 |
|
* @param corpus the corpus
|
249 |
|
* @return the safe values
|
|
347 |
* @param partitionName the partition name
|
|
348 |
* @param partName the part name
|
|
349 |
* @param query the query
|
|
350 |
* @return the part
|
250 |
351 |
* @throws CqiClientException the cqi client exception
|
251 |
352 |
*/
|
252 |
|
private static List<String> getSafeValues(StructuralUnitProperty property, Corpus corpus) throws CqiClientException {
|
253 |
|
List<String> values = property.getValues(corpus);
|
254 |
|
//System.out.println("CORPUS: "+corpus);
|
255 |
|
//System.out.println("VALUES: "+values);
|
256 |
|
for(int i = 0 ; i < values.size() ; i++)
|
257 |
|
values.set(i, Query.addBackSlash(values.get(i)));
|
258 |
|
return values;
|
|
353 |
private Part createPart(String partitionName, String partName, Query query) throws CqiClientException {
|
|
354 |
if (partName.length() == 0) {
|
|
355 |
partName = "-"; //$NON-NLS-1$
|
|
356 |
}
|
|
357 |
return createPart(partitionName, partName, query.getQueryString());
|
259 |
358 |
}
|
260 |
359 |
|
261 |
|
|
262 |
360 |
/**
|
263 |
361 |
* Create a part with a raw query.
|
264 |
362 |
*
|
... | ... | |
276 |
374 |
Log.finest(NLS.bind(TXMCoreMessages.CREATING_PART,partName, query));
|
277 |
375 |
long start = System.currentTimeMillis();
|
278 |
376 |
try {
|
279 |
|
CorpusManager.getCorpusManager().getCqiClient().cqpQuery(this.getCorpus().getQualifiedCqpId(), partCqpId, query);
|
|
377 |
CorpusManager.getCorpusManager().getCqiClient().cqpQuery(this.getParent().getQualifiedCqpId(), partCqpId, query);
|
280 |
378 |
part = new Part(partCqpId, partitionName, partName, this, new Query(query));
|
281 |
379 |
|
282 |
380 |
// FIXME: persistence tests: define the UUID as the concatenation of all part CQP id
|
283 |
|
this.uniqueID += partCqpId;
|
284 |
|
|
|
381 |
// this.uniqueID += partCqpId;
|
|
382 |
|
285 |
383 |
} catch (Exception e) {
|
286 |
384 |
try {
|
287 |
385 |
throw new CqiClientException(TXMCoreMessages.Partition_9 + partitionName + "_" + partName + e + " last error: " + CQPSearchEngine.getCqiClient().getLastCQPError()); //$NON-NLS-1$ //$NON-NLS-2$
|
... | ... | |
293 |
391 |
}
|
294 |
392 |
long end = System.currentTimeMillis();
|
295 |
393 |
Log.finest(NLS.bind(TXMCoreMessages.PART_CREATED, partitionName + "_" + partName, (end - start))); //$NON-NLS-1
|
296 |
|
|
|
394 |
|
297 |
395 |
return part;
|
298 |
396 |
}
|
299 |
397 |
|
300 |
|
/**
|
301 |
|
* Creates the part.
|
302 |
|
*
|
303 |
|
* @param partitionName the partition name
|
304 |
|
* @param partName the part name
|
305 |
|
* @param query the query
|
306 |
|
* @return the part
|
307 |
|
* @throws CqiClientException the cqi client exception
|
308 |
|
*/
|
309 |
|
private Part createPart(String partitionName, String partName, Query query) throws CqiClientException {
|
310 |
|
if (partName.length() == 0) {
|
311 |
|
partName = "-"; //$NON-NLS-1$
|
|
398 |
@Override
|
|
399 |
public boolean delete() {
|
|
400 |
try {
|
|
401 |
this.getParent().dropPartition(this);
|
|
402 |
super.delete();
|
|
403 |
if (this.getSelfElement() != null) {
|
|
404 |
this.getSelfElement().getParentNode().removeChild(getSelfElement());
|
|
405 |
}
|
|
406 |
return true;
|
|
407 |
} catch (CqiClientException e) {
|
|
408 |
// TODO Auto-generated catch block
|
|
409 |
org.txm.utils.logger.Log.printStackTrace(e);
|
312 |
410 |
}
|
313 |
|
return createPart(partitionName, partName, query.getQueryString());
|
|
411 |
return false;
|
314 |
412 |
}
|
315 |
|
|
|
413 |
|
316 |
414 |
/**
|
317 |
|
* Gets the corpus.
|
|
415 |
* Gets the dirty states.
|
318 |
416 |
*
|
319 |
|
* @return the corpus
|
|
417 |
* @return
|
320 |
418 |
*/
|
321 |
|
public Corpus getCorpus() {
|
322 |
|
return (Corpus) this.parent;
|
|
419 |
public boolean isDirty() {
|
|
420 |
return this.dirty;
|
323 |
421 |
}
|
324 |
422 |
|
|
423 |
@Override
|
|
424 |
public String getDetails() {
|
|
425 |
return getName();
|
|
426 |
}
|
|
427 |
|
|
428 |
@Override
|
|
429 |
public String getName() {
|
|
430 |
return this.name;
|
|
431 |
}
|
|
432 |
|
325 |
433 |
/**
|
326 |
|
* Gets the parts.
|
|
434 |
* Gets the number of parts.
|
327 |
435 |
*
|
328 |
|
* @return the parts
|
|
436 |
* @return the the number of parts.
|
329 |
437 |
*/
|
330 |
|
public List<Part> getParts() {
|
331 |
|
return parts;
|
|
438 |
public int getNPart() {
|
|
439 |
try {
|
|
440 |
if (!hasBeenComputedOnce()) {
|
|
441 |
compute();
|
|
442 |
}
|
|
443 |
|
|
444 |
return getParts().size();
|
|
445 |
} catch(Exception e) { }
|
|
446 |
return 0;
|
332 |
447 |
}
|
|
448 |
|
|
449 |
/**
|
|
450 |
* @return true for now
|
|
451 |
*/
|
|
452 |
public boolean hasBeenComputedOnce() {
|
|
453 |
return hasBeenComputedOnce;
|
|
454 |
}
|
333 |
455 |
|
334 |
456 |
/**
|
335 |
457 |
* Gets the name of the parts.
|
... | ... | |
337 |
459 |
* @return the part names
|
338 |
460 |
*/
|
339 |
461 |
public List<String> getPartNames() {
|
|
462 |
List<Part> parts = getParts();
|
340 |
463 |
List<String> partNames = new ArrayList<String>(parts.size());
|
341 |
464 |
for (Subcorpus part : parts) {
|
342 |
465 |
partNames.add(part.getName());
|
... | ... | |
345 |
468 |
}
|
346 |
469 |
|
347 |
470 |
/**
|
|
471 |
* Gets the parts.
|
|
472 |
*
|
|
473 |
* @return the parts
|
|
474 |
*/
|
|
475 |
@SuppressWarnings("unchecked")
|
|
476 |
public List<Part> getParts() {
|
|
477 |
return (List<Part>) getChildren(Part.class);
|
|
478 |
}
|
|
479 |
|
|
480 |
/**
|
348 |
481 |
* Gets the short name of the parts.
|
349 |
482 |
*
|
350 |
483 |
* @return the part names
|
351 |
484 |
*/
|
352 |
485 |
public List<String> getPartShortNames() {
|
|
486 |
List<Part> parts = getParts();
|
353 |
487 |
List<String> partNames = new ArrayList<String>(parts.size());
|
354 |
488 |
for (Part part : parts) {
|
355 |
489 |
partNames.add(part.getShortName());
|
... | ... | |
358 |
492 |
}
|
359 |
493 |
|
360 |
494 |
/**
|
361 |
|
* Run a query on all the parts of this partition.
|
|
495 |
* Gets the part sizes as int array.
|
362 |
496 |
*
|
363 |
|
* @param query
|
364 |
|
* the query
|
365 |
|
* @param name
|
366 |
|
* the name
|
|
497 |
* Do N CQi requests -> avoid calling multiple times this method
|
367 |
498 |
*
|
368 |
|
* @return the list< subcorpus>
|
369 |
|
*
|
|
499 |
* @return
|
370 |
500 |
* @throws CqiClientException
|
371 |
|
* the cqi client exception
|
372 |
501 |
*/
|
373 |
|
public List<QueryResult> query(Query query, String name) throws CqiClientException {
|
374 |
|
Log.finest(TXMCoreMessages.QUERYING_PARTITION + this.name);
|
375 |
|
List<QueryResult> results = new ArrayList<QueryResult>(parts.size());
|
376 |
|
for (Subcorpus part : parts) {
|
377 |
|
results.add(part.query(query, part.getName() + "_" + name, false)); //$NON-NLS-1$
|
|
502 |
public int[] getPartSizes() throws CqiClientException {
|
|
503 |
int[] partSizes = new int[getParts().size()];
|
|
504 |
for (int i = 0 ; i < getParts().size() ; i++) {
|
|
505 |
Part p = getParts().get(i);
|
|
506 |
partSizes[i] = p.getSize();
|
378 |
507 |
}
|
379 |
|
return results;
|
|
508 |
return partSizes;
|
380 |
509 |
}
|
381 |
510 |
|
382 |
511 |
/**
|
383 |
|
* Gets the number of parts.
|
384 |
|
*
|
385 |
|
* @return the the number of parts.
|
|
512 |
* Gets the property.
|
|
513 |
*
|
|
514 |
* @return the property
|
386 |
515 |
*/
|
387 |
|
public int getNPart() {
|
388 |
|
try {
|
389 |
|
return parts.size();
|
390 |
|
}
|
391 |
|
catch(Exception e) {
|
392 |
|
}
|
393 |
|
return 0;
|
|
516 |
public Property getProperty() {
|
|
517 |
return pProperty;
|
394 |
518 |
}
|
395 |
519 |
|
|
520 |
@Override
|
|
521 |
public String getSimpleName() {
|
|
522 |
return this.pName;
|
|
523 |
}
|
|
524 |
|
396 |
525 |
/**
|
397 |
526 |
* Gets the structure.
|
398 |
527 |
*
|
399 |
528 |
* @return the structure
|
400 |
529 |
*/
|
401 |
530 |
public StructuralUnit getStructure() {
|
402 |
|
return structure;
|
|
531 |
if (pProperty == null) return null;
|
|
532 |
return pProperty.getStructuralUnit();
|
403 |
533 |
}
|
404 |
534 |
|
405 |
535 |
/**
|
406 |
|
* Gets the property.
|
407 |
|
*
|
408 |
|
* @return the property
|
|
536 |
* Gets the total of all part sizes.
|
|
537 |
* @return
|
|
538 |
* @throws CqiClientException
|
409 |
539 |
*/
|
410 |
|
public Property getProperty() {
|
411 |
|
return property;
|
|
540 |
public int getTotalSize() throws CqiClientException {
|
|
541 |
if (!hasBeenComputedOnce()) {
|
|
542 |
try {
|
|
543 |
compute();
|
|
544 |
} catch (Exception e) {
|
|
545 |
// TODO Auto-generated catch block
|
|
546 |
e.printStackTrace();
|
|
547 |
}
|
|
548 |
}
|
|
549 |
if (totalsize == -1) {
|
|
550 |
totalsize = 0;
|
|
551 |
for (Part p : getParts()) {
|
|
552 |
totalsize += p.getSize();
|
|
553 |
}
|
|
554 |
}
|
|
555 |
return totalsize;
|
412 |
556 |
}
|
413 |
557 |
|
414 |
558 |
|
415 |
559 |
/**
|
416 |
|
* Sets the name.
|
417 |
|
*
|
418 |
|
* @param name2 the new name
|
|
560 |
* Run a query on all the parts of this partition.
|
|
561 |
*
|
|
562 |
* @param query
|
|
563 |
* the query
|
|
564 |
* @param name
|
|
565 |
* the name
|
|
566 |
*
|
|
567 |
* @return the list< subcorpus>
|
|
568 |
*
|
|
569 |
* @throws CqiClientException
|
|
570 |
* the cqi client exception
|
419 |
571 |
*/
|
420 |
|
public void setName(String name2) {
|
421 |
|
this.name = name2;
|
422 |
|
if(this.getSelfElement() != null)
|
423 |
|
this.getSelfElement().setAttribute("name", name); //$NON-NLS-1$
|
|
572 |
public List<QueryResult> query(Query query, String name) throws CqiClientException {
|
|
573 |
Log.finest(TXMCoreMessages.QUERYING_PARTITION + this.name);
|
|
574 |
List<Part> parts = getParts();
|
|
575 |
List<QueryResult> results = new ArrayList<QueryResult>(parts.size());
|
|
576 |
for (Subcorpus part : parts) {
|
|
577 |
results.add(part.query(query, part.getName() + "_" + name, false)); //$NON-NLS-1$
|
|
578 |
}
|
|
579 |
return results;
|
424 |
580 |
}
|
425 |
581 |
|
426 |
582 |
/**
|
427 |
583 |
* Register to parent.
|
428 |
|
*
|
429 |
|
* @param partitionName the partition name
|
430 |
584 |
*/
|
431 |
|
private void registerToParent(String partitionName) {
|
432 |
|
if (this.getSelfElement() == null && this.getParent() != null)
|
433 |
|
try {
|
434 |
|
Element parentElem = (Element) this.getCorpus().getSelfElement();
|
435 |
|
if (parentElem == null) return;
|
436 |
|
Document doc = parentElem.getOwnerDocument();
|
437 |
|
Element corporaElem;
|
438 |
|
if (this.getParent() instanceof MainCorpus) {
|
439 |
|
NodeList corporaList = parentElem.getElementsByTagName("preBuild"); //$NON-NLS-1$
|
440 |
|
corporaElem = (Element) corporaList.item(0);
|
441 |
|
} else {
|
442 |
|
corporaElem = parentElem;
|
443 |
|
}
|
444 |
|
Element partitionElem = doc.createElement("partition"); //$NON-NLS-1$
|
445 |
|
partitionElem.setAttribute("name", partitionName); //$NON-NLS-1$
|
446 |
|
corporaElem.appendChild(partitionElem);
|
447 |
|
this.setSelfElement(partitionElem);
|
448 |
|
for (Part p : parts) {
|
449 |
|
Element partElem = doc.createElement("part"); //$NON-NLS-1$
|
450 |
|
partElem.setAttribute("name", p.getName()); //$NON-NLS-1$
|
451 |
|
partElem.setAttribute("shortname", p.getShortName()); //$NON-NLS-1$
|
452 |
|
partElem.setAttribute("query", p.getQuery().getQueryString()); //$NON-NLS-1$
|
453 |
|
partitionElem.appendChild(partElem);
|
454 |
|
}
|
455 |
|
} catch (Exception e) {
|
456 |
|
System.out.println(TXMCoreMessages.Partition_21+e);
|
457 |
|
}
|
458 |
|
this._load();
|
|
585 |
public void registerToParent() {
|
|
586 |
//registerToParent(this.name);
|
459 |
587 |
}
|
460 |
588 |
|
461 |
589 |
/**
|
462 |
590 |
* Register to parent.
|
|
591 |
*
|
|
592 |
* @param partitionName the partition name
|
463 |
593 |
*/
|
464 |
|
public void registerToParent() {
|
465 |
|
registerToParent(this.name);
|
|
594 |
private void registerToParent(String partitionName) {
|
|
595 |
// if (this.getSelfElement() == null && this.getParent() != null)
|
|
596 |
// try {
|
|
597 |
// Element parentElem = (Element) this.getParent().getSelfElement();
|
|
598 |
// if (parentElem == null) return;
|
|
599 |
// Document doc = parentElem.getOwnerDocument();
|
|
600 |
// Element corporaElem;
|
|
601 |
// if (this.getParent() instanceof MainCorpus) {
|
|
602 |
// NodeList corporaList = parentElem.getElementsByTagName("preBuild"); //$NON-NLS-1$
|
|
603 |
// corporaElem = (Element) corporaList.item(0);
|
|
604 |
// } else {
|
|
605 |
// corporaElem = parentElem;
|
|
606 |
// }
|
|
607 |
// Element partitionElem = doc.createElement("partition"); //$NON-NLS-1$
|
|
608 |
// partitionElem.setAttribute("name", partitionName); //$NON-NLS-1$
|
|
609 |
// corporaElem.appendChild(partitionElem);
|
|
610 |
// this.setSelfElement(partitionElem);
|
|
611 |
// for (Part p : getParts()) {
|
|
612 |
// Element partElem = doc.createElement("part"); //$NON-NLS-1$
|
|
613 |
// partElem.setAttribute("name", p.getName()); //$NON-NLS-1$
|
|
614 |
// partElem.setAttribute("shortname", p.getShortName()); //$NON-NLS-1$
|
|
615 |
// partElem.setAttribute("query", p.getQuery().getQueryString()); //$NON-NLS-1$
|
|
616 |
// partitionElem.appendChild(partElem);
|
|
617 |
// }
|
|
618 |
// } catch (Exception e) {
|
|
619 |
// System.out.println(TXMCoreMessages.Partition_21+e);
|
|
620 |
// }
|
|
621 |
// this._load();
|
466 |
622 |
}
|
467 |
623 |
|
468 |
|
// /**
|
469 |
|
// * Load.
|
470 |
|
// *
|
471 |
|
// * @param e the e
|
472 |
|
// * @return true, if successful
|
473 |
|
// */
|
474 |
|
// protected boolean _load(Element e) {
|
475 |
|
// this.setSelfElement(e);
|
476 |
|
// return this._load();
|
477 |
|
// }
|
478 |
|
|
479 |
|
/* (non-Javadoc)
|
480 |
|
* @see org.txm.objects.TxmObject#load()
|
481 |
|
*/
|
482 |
|
@Override
|
483 |
|
protected boolean _load() {
|
484 |
|
if (this.getSelfElement() != null && this.getParent() != null) {
|
485 |
|
NodeList partList = getSelfElement().getChildNodes();
|
486 |
|
for (int i = 0; i < partList.getLength(); i++) {
|
487 |
|
if (partList.item(i).getNodeType() == 1) {
|
488 |
|
Element c = (Element) partList.item(i);
|
489 |
|
if (c.getNodeName().equals("part")) //$NON-NLS-1$
|
490 |
|
{
|
491 |
|
String pname = c.getAttribute("name"); //$NON-NLS-1$
|
492 |
|
String pshortname = c.getAttribute("shortname"); //$NON-NLS-1$
|
493 |
|
String pquery = c.getAttribute("query"); //$NON-NLS-1$
|
494 |
|
for (Part part : parts)
|
495 |
|
if (part.getName().equals(pname)) {
|
496 |
|
part.setName(pname);
|
497 |
|
part.setShortName(pshortname);
|
498 |
|
part.setQuery(pquery);
|
499 |
|
}
|
500 |
|
}
|
501 |
|
}
|
502 |
|
}
|
503 |
|
this.loadMetadata();
|
504 |
|
return true;
|
505 |
|
}
|
506 |
|
|
507 |
|
return false;
|
508 |
|
}
|
509 |
|
|
510 |
624 |
public TxmObject removeChildren(TxmObject children) {
|
511 |
625 |
if (children instanceof Part) {
|
512 |
626 |
if (children.getSelfElement() != null && children.getSelfElement().getParentNode() == getSelfElement())
|
513 |
627 |
getSelfElement().removeChild(children.getSelfElement());
|
514 |
|
parts.remove(children);
|
515 |
628 |
return children;
|
516 |
629 |
}
|
517 |
630 |
return null;
|
518 |
631 |
}
|
519 |
632 |
|
520 |
|
@Override
|
521 |
|
protected boolean _save() {
|
522 |
|
return false;
|
|
633 |
public Corpus getParent() {
|
|
634 |
return (Corpus) super.getParent();
|
523 |
635 |
}
|
524 |
636 |
|
525 |
|
@Override
|
526 |
|
public boolean delete() {
|
527 |
|
try {
|
528 |
|
this.getCorpus().dropPartition(this);
|
529 |
|
super.delete();
|
530 |
|
this.getSelfElement().getParentNode().removeChild(getSelfElement());
|
531 |
|
return true;
|
532 |
|
} catch (CqiClientException e) {
|
533 |
|
// TODO Auto-generated catch block
|
534 |
|
org.txm.utils.logger.Log.printStackTrace(e);
|
535 |
|
}
|
536 |
|
return false;
|
537 |
|
}
|
538 |
|
|
539 |
637 |
/**
|
540 |
|
* Gets the total of all part sizes.
|
541 |
|
* @return
|
542 |
|
* @throws CqiClientException
|
543 |
|
*/
|
544 |
|
public int getTotalSize() throws CqiClientException {
|
545 |
|
if (totalsize == -1) {
|
546 |
|
totalsize = 0;
|
547 |
|
for (Part p : parts) {
|
548 |
|
totalsize += p.getSize();
|
549 |
|
}
|
550 |
|
}
|
551 |
|
return totalsize;
|
552 |
|
}
|
553 |
|
|
554 |
|
/**
|
555 |
|
* Gets the part sizes as int array.
|
556 |
|
* @return
|
557 |
|
* @throws CqiClientException
|
558 |
|
*/
|
559 |
|
public int[] getPartSizes() throws CqiClientException {
|
560 |
|
if (partSizes != null) {
|
561 |
|
return partSizes;
|
562 |
|
}
|
563 |
|
partSizes = new int[getParts().size()];
|
564 |
|
for (int i = 0 ; i < getParts().size() ; i++) {
|
565 |
|
Part p = getParts().get(i);
|
566 |
|
partSizes[i] = p.getSize();
|
567 |
|
}
|
568 |
|
return partSizes;
|
569 |
|
}
|
570 |
|
|
571 |
|
/**
|
572 |
638 |
* Remove the specified <code>Part</code> from the partition.
|
573 |
639 |
* @param p
|
574 |
640 |
* @throws CqiClientException
|
575 |
641 |
*/
|
576 |
642 |
public void removePart(Part p) throws CqiClientException {
|
577 |
|
parts.remove(p);
|
|
643 |
p.delete();
|
578 |
644 |
p.dropSubcorpus(p);
|
579 |
645 |
}
|
580 |
646 |
|
581 |
|
@Override
|
582 |
|
public String getName() {
|
583 |
|
return this.name + ": "+ this.getNPart();
|
|
647 |
/**
|
|
648 |
* Sets the name.
|
|
649 |
*
|
|
650 |
* @param name2 the new name
|
|
651 |
*/
|
|
652 |
public void setName(String name2) {
|
|
653 |
this.name = name2;
|
|
654 |
if(this.getSelfElement() != null)
|
|
655 |
this.getSelfElement().setAttribute("name", name); //$NON-NLS-1$
|
584 |
656 |
}
|
585 |
657 |
|
586 |
658 |
|
587 |
|
@Override
|
588 |
|
public String getSimpleName() {
|
589 |
|
return this.name;
|
|
659 |
public String toString() {
|
|
660 |
if (name != null) {
|
|
661 |
return name;
|
|
662 |
}
|
|
663 |
return super.toString();
|
590 |
664 |
}
|
591 |
665 |
|
|
666 |
|
592 |
667 |
@Override
|
593 |
|
public String getDetails() {
|
594 |
|
return getName();
|
|
668 |
public boolean toTxt(File output, String encoding, String colseparator, String txtseparator) throws Exception {
|
|
669 |
IOUtils.write(output, this.dumpPreferences());
|
|
670 |
return output.exists();
|
595 |
671 |
}
|
596 |
672 |
|
597 |
|
@Override
|
598 |
|
public void clean() {
|
599 |
|
// TODO Auto-generated method stub
|
600 |
|
|
|
673 |
public boolean setParameters(String name, StructuralUnitProperty property, List<String> values) {
|
|
674 |
this.pProperty = property;
|
|
675 |
this.pValues = values;
|
|
676 |
this.pName = name;
|
|
677 |
this.pQueries = null;
|
|
678 |
this.pPartNames = null;
|
|
679 |
return true;
|
601 |
680 |
}
|
602 |
681 |
|
603 |
|
@Override
|
604 |
|
public boolean canCompute() {
|
605 |
|
return getCorpus() != null;
|
|
682 |
public boolean setParameters(String name, List<String> queries, List<String> names) {
|
|
683 |
this.pProperty = null;
|
|
684 |
this.pValues = null;
|
|
685 |
this.pName = name;
|
|
686 |
this.pQueries = queries;
|
|
687 |
this.pPartNames = names;
|
|
688 |
return true;
|
606 |
689 |
}
|
607 |
|
|
|
690 |
|
608 |
691 |
@Override
|
609 |
|
protected boolean _compute() throws Exception {
|
610 |
|
// TODO Auto-generated method stub
|
|
692 |
public boolean setParameters(TXMParameters parameters) {
|
|
693 |
this.pName = parameters.getString(TXMPreferences.LAZY_NAME);
|
|
694 |
String tmp = parameters.getString(CQPPreferences.PARTNAMES);
|
|
695 |
if (tmp != null) {
|
|
696 |
this.pPartNames = Arrays.asList(tmp.split("\t"));
|
|
697 |
} else {
|
|
698 |
this.pPartNames = null;
|
|
699 |
}
|
|
700 |
|
|
701 |
try {
|
|
702 |
this.pProperty = StructuralUnitProperty.stringToStructuralUnitProperty(getParent(), parameters.getString(TXMPreferences.STRUCTURAL_UNIT_PROPERTY));
|
|
703 |
} catch (CqiClientException e) {
|
|
704 |
// TODO Auto-generated catch block
|
|
705 |
e.printStackTrace();
|
|
706 |
this.pProperty = null;
|
|
707 |
}
|
|
708 |
|
|
709 |
tmp = parameters.getString(CQPPreferences.QUERIES);
|
|
710 |
if (tmp != null) {
|
|
711 |
this.pQueries = Arrays.asList(tmp.split("\t"));
|
|
712 |
} else {
|
|
713 |
this.pQueries = null;
|
|
714 |
}
|
|
715 |
|
|
716 |
tmp = parameters.getString(CQPPreferences.VALUES);
|
|
717 |
if (tmp != null) {
|
|
718 |
this.pValues = Arrays.asList(tmp.split("\t"));
|
|
719 |
} else {
|
|
720 |
this.pValues = null;
|
|
721 |
}
|
611 |
722 |
return true;
|
612 |
723 |
}
|
613 |
724 |
|
614 |
725 |
@Override
|
615 |
|
public boolean toTxt(File arg0, String arg1, String arg2, String arg3) throws Exception {
|
616 |
|
// TODO Auto-generated method stub
|
617 |
|
return false;
|
618 |
|
}
|
|
726 |
public boolean loadParameters() {
|
|
727 |
String tmp = null;
|
|
728 |
try {
|
|
729 |
tmp = this.getStringParameterValue(TBXPreferences.STRUCTURAL_UNIT_PROPERTY);
|
|
730 |
if (!tmp.isEmpty()) {
|
|
731 |
this.pProperty = StructuralUnitProperty.stringToStructuralUnitProperty(getParent(), tmp);
|
|
732 |
}
|
|
733 |
}
|
|
734 |
catch (CqiClientException e) {
|
|
735 |
// TODO Auto-generated catch block
|
|
736 |
e.printStackTrace();
|
|
737 |
}
|
|
738 |
|
|
739 |
tmp = this.getStringParameterValue(CQPPreferences.PARTNAMES);
|
|
740 |
if (tmp != null) {
|
|
741 |
this.pPartNames = Arrays.asList(tmp.split("\t"));
|
|
742 |
} else {
|
|
743 |
this.pPartNames = null;
|
|
744 |
}
|
619 |
745 |
|
620 |
|
|
621 |
|
public String toString() {
|
622 |
|
if (name != null) {
|
623 |
|
return name;
|
|
746 |
tmp = this.getStringParameterValue(CQPPreferences.QUERIES);
|
|
747 |
if (tmp != null) {
|
|
748 |
this.pQueries = Arrays.asList(tmp.split("\t"));
|
|
749 |
} else {
|
|
750 |
this.pQueries = null;
|
624 |
751 |
}
|
625 |
|
return super.toString();
|
|
752 |
|
|
753 |
tmp = this.getStringParameterValue(CQPPreferences.VALUES);
|
|
754 |
if (tmp != null) {
|
|
755 |
this.pValues = Arrays.asList(tmp.split("\t"));
|
|
756 |
} else {
|
|
757 |
this.pValues = null;
|
|
758 |
}
|
|
759 |
return true;
|
626 |
760 |
}
|
627 |
|
|
628 |
761 |
|
629 |
762 |
@Override
|
630 |
|
public int compareTo(TXMResult o) {
|
631 |
|
// Compare node weights
|
632 |
|
int output = super.compareTo(o);
|
633 |
|
// Compare simple names if weights are equal
|
634 |
|
if(output == 0) {
|
635 |
|
output = this.getSimpleName().compareToIgnoreCase(o.getSimpleName());
|
|
763 |
public boolean saveParameters() {
|
|
764 |
|
|
765 |
if (this.pProperty != null) {
|
|
766 |
this.saveParameter(TBXPreferences.STRUCTURAL_UNIT_PROPERTY, this.pProperty.getName());
|
636 |
767 |
}
|
637 |
|
return output;
|
|
768 |
|
|
769 |
if (this.pPartNames != null) {
|
|
770 |
this.saveParameter(CQPPreferences.PARTNAMES, StringUtils.join(pPartNames, "\t"));
|
|
771 |
}
|
|
772 |
|
|
773 |
if (this.pQueries != null) {
|
|
774 |
this.saveParameter(TBXPreferences.QUERIES, StringUtils.join(pQueries, "\t"));
|
|
775 |
}
|
|
776 |
|
|
777 |
if (this.pValues != null) {
|
|
778 |
this.saveParameter(TBXPreferences.VALUES, StringUtils.join(pValues, "\t"));
|
|
779 |
}
|
|
780 |
|
|
781 |
return true;
|
638 |
782 |
}
|
639 |
783 |
|
640 |
|
|
641 |
784 |
}
|