Revision 446

tmp/org.txm.core/src/java/org/txm/core/results/ITXMResult.java (revision 446)
1
package org.txm.core.results;
2

  
3
import java.util.ArrayList;
4

  
5
import org.txm.core.results.ITXMResult;
6

  
7
/**
8
 * 
9
 * @author sjacquot
10
 *
11
 */
12
//FIXME: this interface has been created to manage the result based on interface AND implementation, for example: interface LexicalTable.
13
// need to see if it's useful or not.
14
public interface ITXMResult {
15

  
16
	
17
	/**
18
	 * Deletes the object from its parent, also deletes the children.
19
	 * The <code>TXMResult.clean()</code> methods of this result and children results are applied before the deletion.
20
	 * @return
21
	 */
22
	public boolean delete();
23
	
24
	/**
25
	 * Adds a child result to this result.
26
	 * @param child
27
	 * @return
28
	 */
29
	public boolean addResult(ITXMResult child);
30

  
31
	/**
32
	 * Adds a child result to this result.
33
	 * @param index
34
	 * @param child
35
	 * @return
36
	 */
37
	public boolean addResult(int index, ITXMResult child);
38
	
39
	/**
40
	 * Removes the specified child result. 
41
	 * @param child
42
	 * @return <code>true if the child has been removed, otherwise <code>false</code>
43
	 */
44
	public boolean removeResult(ITXMResult child);
45
	
46
	/**
47
	 * Removes the child result specified by its index.
48
	 * @param index
49
	 * @return <code>true if the child has been removed, otherwise <code>false</code>
50
	 */
51
	public boolean removeResult(int index);
52
	
53
	/**
54
	 * Removes the result from its parent.
55
	 * @return
56
	 */
57
	public boolean removeFromParent();
58
	
59
	/**
60
	 * Gets all the children results.
61
	 * @return
62
	 */
63
	public ArrayList<ITXMResult> getResults();
64
	
65
	/**
66
	 * Gets the children results.
67
	 * @return
68
	 */
69
	public ArrayList<ITXMResult> getResults(boolean onlyVisible);
70
	
71
	/**
72
	 * Checks if the result must be displayed.  
73
	 * @return
74
	 */
75
	public boolean isVisible();
76
	
77
	/**
78
	 * Sets the result visibility state.
79
	 * @param visible
80
	 * @return
81
	 */
82
	public void setVisible(boolean visible);
83
	
84
	/**
85
	 * Gets the children results specified by their class.
86
	 * @param type
87
	 * @return
88
	 */
89
	public ArrayList<ITXMResult> getResults(Class type);
90
	
91
	
92
	/**
93
	 * Gets the children results specified by their class.
94
	 * @param type
95
	 * @return
96
	 */
97
	public ArrayList<ITXMResult> getResults(Class type, boolean onlyVisible);
98
	
99
	/**
100
	 * Gets the children of all the branch in a flat list.
101
	 * @return
102
	 */
103
	public ArrayList<ITXMResult> getDeepResults();
104
	
105
	/**
106
	 * Gets the sibling nodes of this node result.
107
	 */
108
	public ArrayList<ITXMResult> getSiblings();
109
	
110
	/**
111
	 * Gets the sibling nodes of this node result, specified by their class.
112
	 */
113
	public ArrayList<ITXMResult> getSiblings(Class type);
114

  
115

  
116
	/**
117
	 * Sets the parent.
118
	 * @param parent
119
	 */
120
	public void setParent(ITXMResult parent);
121
	
122
	/**
123
	 * Gets the parent.
124
	 * @return the parent
125
	 */
126
	public ITXMResult getParent();
127
	
128
	/**
129
	 * Gets the name of the result.
130
	 * @return
131
	 */
132
	public abstract String getName();
133
	
134
	/**
135
	 * Gets the simple name of the result.
136
	 * @return
137
	 */
138
	public abstract String getSimpleName();
139
	
140
	/**
141
	 * Gets some details about how the result has been computed (eg. additional parameters, etc.).
142
	 * @return
143
	 */
144
	public abstract String getDetails();
145
	
146
	/**
147
	 * Gets a string representing the result and that can be used as a file name.
148
	 * @return
149
	 */
150
	//FIXME: to discuss and/or to move in export layer
151
	public String getValidFileName();
152
	
153
	/**
154
	 * Gets a string representing the concatenation of the simple names of the branch of this result.
155
	 * @return
156
	 */
157
	public String getFullPathSimpleName();
158
	/**
159
	 * Gets the weight of the node. Essentially used for sorting purpose.
160
	 * @return the weight
161
	 */
162
	public int getWeight();
163

  
164
	/**
165
	 * Cleans and free some resources if needed.
166
	 * This methods is applied when calling <code>TXMResult.delete()</code>. 
167
	 */
168
	public abstract void clean();
169
	
170
}
tmp/org.txm.core/src/java/org/txm/core/results/TXMParameters.java (revision 446)
1
/**
2
 * 
3
 */
4
package org.txm.core.results;
5

  
6
import java.util.HashMap;
7
import java.util.Iterator;
8
import java.util.Map;
9
import java.util.Set;
10

  
11
/**
12
 * Wrapper for semantic purpose.
13
 * @author sjacquot
14
 *
15
 */
16
//FIXME: inherit from Properties may be better.
17
public class TXMParameters extends HashMap<String, Object> {
18

  
19
	/**
20
	 * 
21
	 */
22
	public TXMParameters() {
23
		// TODO Auto-generated constructor stub
24
	}
25

  
26
	/**
27
	 * @param arg0
28
	 */
29
	public TXMParameters(int arg0) {
30
		super(arg0);
31
		// TODO Auto-generated constructor stub
32
	}
33

  
34
	/**
35
	 * @param arg0
36
	 */
37
	public TXMParameters(Map<? extends String, ? extends Object> arg0) {
38
		super(arg0);
39
		// TODO Auto-generated constructor stub
40
	}
41

  
42
	/**
43
	 * @param arg0
44
	 * @param arg1
45
	 */
46
	public TXMParameters(int arg0, float arg1) {
47
		super(arg0, arg1);
48
	}
49
	
50
	//FIXME: use super.toString()?
51
	public void dump()	{
52
		Set keys = this.keySet();
53
		Iterator it = keys.iterator();
54
		while (it.hasNext()){
55
		   Object key = it.next();
56
		   Object value = this.get(key);
57
		   System.out.println("TXMCommandParameters.dump(): key = " + key + " / value = " + value);
58
		}
59
	}
60

  
61
}
0 62

  
tmp/org.txm.core/src/java/org/txm/core/results/TXMResult.java (revision 446)
4 4
package org.txm.core.results;
5 5

  
6 6
import java.util.ArrayList;
7
import java.util.UUID;
7 8

  
9
import org.osgi.framework.FrameworkUtil;
10
import org.txm.core.preferences.TXMPreferences;
11

  
8 12
/**
9 13
 * Root class of all TXM results.
10 14
 * This class contains methods to get some different version of the name of the result and some additional computing information.
......
15 19
 *
16 20
 */
17 21
//FIXME: At this moment, an empty list is created for children, to not return null when calling getChildren(), see if that's what we want 
18
public abstract class TXMResult implements ITXMResult, Cloneable {
22
public abstract class TXMResult implements Cloneable {
19 23
	
20 24
	/**
25
	 * Unique ID of the object.
26
	 */
27
	protected String uniqueID;
28
	
29
	/**
21 30
	 * The weight, essentially used for sorting purpose.
22 31
	 */
23 32
	protected int weight;
......
25 34
	/**
26 35
	 * Parent.
27 36
	 */
28
	protected ITXMResult parent;
37
	protected TXMResult parent;
29 38
	
30 39
	
31 40
	/**
32 41
	 * Children results.
33 42
	 */
34
	protected ArrayList<ITXMResult> children;
43
	protected ArrayList<TXMResult> children;
35 44
	
36 45
	/**
37 46
	 * The visibility state.
38 47
	 */
39 48
	protected boolean visible;
40 49
	
50
	protected TXMParameters parameters;
41 51

  
52

  
42 53
	/**
54
	 * The command preferences node qualifier.
55
	 */
56
	protected String preferencesNodeQualifier;
57
	
58
	/**
43 59
	 * Creates a new TXMResult with no parent.
44 60
	 */
45 61
	public TXMResult() {
......
51 67
	 * Creates a new TXMResult, child of the specified parent.
52 68
	 * @param parent
53 69
	 */
54
	public TXMResult(ITXMResult parent) {
70
	public TXMResult(TXMResult parent) {
71
		this(parent, null);
72
	}
73
	
74
	
75
	/**
76
	 * 
77
	 * @param parent
78
	 * @param parameters
79
	 */
80
	public TXMResult(TXMResult parent, TXMParameters parameters) {
81
		
82
		// FIXME: discuss about this method and validate it. Also discuss about a getInternalName() method that should also return an unique ID.
83
		this.uniqueID = this.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(this)) + "_" + UUID.randomUUID();
84
		
55 85
		this.weight = 0;
56 86
		this.visible = true;
57 87
		if(parent != null)	{
58 88
			parent.addResult(this);
59 89
		}
60
		this.children = new ArrayList<ITXMResult>(1);
90
		this.children = new ArrayList<TXMResult>(1);
91
		this.preferencesNodeQualifier = FrameworkUtil.getBundle(getClass()).getSymbolicName();
92
		this.parameters = parameters;
93
		//FIXME: Debug
94
		System.out.println("TXMResult.TXMResult(): default preferences node qualifier = " + this.preferencesNodeQualifier + ", class = " + getClass());
61 95
	}
62 96
	
97
	/**
98
	 * Gets the preferences node qualifier of the result command.
99
	 * @return
100
	 */
101
	public String getPreferencesNodeQualifier() {
102
		return preferencesNodeQualifier;
103
	}
104
	
105
	
106
	
107
	/**
108
	 * Gets the value of the specified key in parameters, local result node or default preferences nodes.
109
	 * @param key
110
	 * @return
111
	 */
112
	public int getIntParameterValue(String key)	{
113
		return TXMPreferences.getInt(key, this.parameters, this, this.preferencesNodeQualifier);
114
	}
115
	
63 116

  
117
	/**
118
	 * Gets the value of the specified key in parameters, local result node or default preferences nodes.
119
	 * @param key
120
	 * @return
121
	 */
122
	public float getFloatParameterValue(String key)	{
123
		return TXMPreferences.getFloat(key, this.parameters, this, this.preferencesNodeQualifier);
124
	}
64 125

  
65
	@Override
126
	/**
127
	 * Gets the value of the specified key in parameters, local result node or default preferences nodes.
128
	 * @param key
129
	 * @return
130
	 */
131
	public double getDoubleParameterValue(String key)	{
132
		return TXMPreferences.getDouble(key, this.parameters, this, this.preferencesNodeQualifier);
133
	}
134

  
135
	
136
	/**
137
	 * Gets the value of the specified key in parameters, local result node or default preferences nodes.
138
	 * @param key
139
	 * @return
140
	 */
141
	public boolean getBooleanParameterValue(String key)	{
142
		return TXMPreferences.getBoolean(key, this.parameters, this, this.preferencesNodeQualifier);
143
	}
144

  
145
	/**
146
	 * Gets the value of the specified key in parameters, local result node or default preferences nodes.
147
	 * @param key
148
	 * @return
149
	 */
150
	public long getLongParameterValue(String key)	{
151
		return TXMPreferences.getLong(key, this.parameters, this, this.preferencesNodeQualifier);
152
	}
153
	
154
	/**
155
	 * Gets the value of the specified key in parameters, local result node or default preferences nodes.
156
	 * @param key
157
	 * @return
158
	 */
159
	public String getStringParameterValue(String key)	{
160
		return TXMPreferences.getString(key, this.parameters, this, this.preferencesNodeQualifier);
161
	}
162
	
163
	/**
164
	 * Deletes the object from its parent, also deletes the children.
165
	 * The <code>TXMResult.clean()</code> methods of this result and children results are applied before the deletion.
166
	 * @return
167
	 */
66 168
	synchronized public boolean delete()	{
67 169

  
68 170
		try {
69 171
			//FIXME: debug
70 172
			//System.err.println("TXMResult.delete()");
71 173

  
174
			// delete the local node
175
			TXMPreferences.delete(this);
72 176
			
73 177
			// remove children and clean resources
74 178
			for(int i = 0; i < this.children.size(); i++) {
75 179
				this.children.get(i).clean();
180
				TXMPreferences.delete(this.children.get(i));
76 181
				this.removeResult(i);
77 182
			}
78 183
			
......
80 185
			
81 186
			this.parent.removeResult(this);
82 187
			this.parent = null;
83

  
84 188
			return true;
85 189
		}
190
		catch(NullPointerException e) {
191
			// do nothing
192
		}
86 193
		catch(Exception e) {
194
			e.printStackTrace();
87 195
		}
196
		
88 197
		return false;
89 198
	}
90 199
	
91 200

  
92
	@Override
93
	synchronized public boolean addResult(ITXMResult child)	{
201
	/**
202
	 * Adds a child result to this result.
203
	 * @param child
204
	 * @return
205
	 */
206
	synchronized public boolean addResult(TXMResult child)	{
94 207
		try {
95 208
			//remove from current parent if exists 
96 209
			child.removeFromParent();
......
108 221
	}
109 222

  
110 223
	
111
	@Override
112
	synchronized public boolean addResult(int index, ITXMResult child)	{
224
	/**
225
	 * Adds a child result to this result.
226
	 * @param index
227
	 * @param child
228
	 * @return
229
	 */
230
	synchronized public boolean addResult(int index, TXMResult child)	{
113 231
		try {
114 232
			if(!this.children.contains(child))	{
115 233
				child.setParent(this);
......
124 242
		return false;
125 243
	}
126 244
	
127
	@Override
128
	synchronized public boolean removeResult(ITXMResult child)	{
245
	/**
246
	 * Removes the specified child result. 
247
	 * @param child
248
	 * @return <code>true if the child has been removed, otherwise <code>false</code>
249
	 */
250
	synchronized public boolean removeResult(TXMResult child)	{
129 251
		try {
130 252
			child.setParent(null);
131 253
			return this.children.remove(child);
......
135 257
		return false;
136 258
	}
137 259
	
138
	@Override
260
	/**
261
	 * Removes the child result specified by its index.
262
	 * @param index
263
	 * @return <code>true if the child has been removed, otherwise <code>false</code>
264
	 */
139 265
	synchronized public boolean removeResult(int index)	{
140 266
		try {
141 267
			return this.removeResult(this.children.get(index));
......
145 271
		return false;
146 272
	}
147 273

  
148
	@Override
274
	/**
275
	 * Removes the result from its parent.
276
	 * @return
277
	 */
149 278
	synchronized public boolean removeFromParent()	{
150 279
		try {
151 280
			return this.parent.removeResult(this);
......
155 284
		return false;
156 285
	}
157 286
	
158
	@Override
159
	synchronized public ArrayList<ITXMResult> getResults()	{
287
	/**
288
	 * Gets all the children results.
289
	 * @return
290
	 */
291
	synchronized public ArrayList<TXMResult> getResults()	{
160 292
		return this.children;
161 293
	}
162 294
	
163 295

  
164
	@Override
165
	synchronized public ArrayList<ITXMResult> getResults(boolean onlyVisible)	{
296
	/**
297
	 * Gets the children results.
298
	 * @return
299
	 */
300
	synchronized public ArrayList<TXMResult> getResults(boolean onlyVisible)	{
166 301
		return this.getResults(null, onlyVisible);
167 302
	}
168 303

  
169
	@Override
170
	synchronized public ArrayList<ITXMResult> getResults(Class type)	{
304
	/**
305
	 * Gets the children results specified by their class.
306
	 * @param type
307
	 * @return
308
	 */
309
	synchronized public ArrayList<TXMResult> getResults(Class type)	{
171 310
		return TXMResult.getNodes(this.children, type, false);
172 311
	}
173 312

  
174
	@Override
175
	synchronized public ArrayList<ITXMResult> getResults(Class type, boolean onlyVisible)	{
313
	/**
314
	 * Gets the children results specified by their class.
315
	 * @param type
316
	 * @return
317
	 */
318
	synchronized public ArrayList<TXMResult> getResults(Class type, boolean onlyVisible)	{
176 319
		return TXMResult.getNodes(this.children, type, onlyVisible);
177 320
	}
178 321
	
......
181 324
	 * Gets the children of all the branch in a flat list.
182 325
	 * @return
183 326
	 */
184
	synchronized protected ArrayList<ITXMResult> getDeepResults(ITXMResult parent, ArrayList<ITXMResult> results)	{
327
	synchronized protected ArrayList<TXMResult> getDeepResults(TXMResult parent, ArrayList<TXMResult> results)	{
185 328
		//FIXME: need to prove this method
186
		System.err.println("TXMResult2.getDeepResults(ITXMResult parent, ArrayList<ITXMResult> results): NEED TO VALIDATE THIS METHOD");
329
		System.err.println("TXMResult2.getDeepResults(TXMResult parent, ArrayList<TXMResult> results): NEED TO VALIDATE THIS METHOD");
187 330
		for(int i = 0; i < parent.getResults().size(); i++) {
188 331
			results.addAll(parent.getResults().get(i).getDeepResults());
189 332
		}
190 333
		return results; 
191 334
	}
192 335
	
193
	@Override
194
	synchronized public ArrayList<ITXMResult> getDeepResults()	{
336
	/**
337
	 * Gets the children of all the branch in a flat list.
338
	 * @return
339
	 */
340
	synchronized public ArrayList<TXMResult> getDeepResults()	{
195 341
		//FIXME: need to prove this method
196 342
		System.err.println("TXMResult2.getDeepResults(): NEED TO VALIDATE THIS METHOD");
197
		return this.getDeepResults(this, new ArrayList<ITXMResult>());
343
		return this.getDeepResults(this, new ArrayList<TXMResult>());
198 344
	}
199 345
	
200 346

  
......
204 350
	 * @param type
205 351
	 * @return
206 352
	 */
207
	public static ArrayList<ITXMResult> getNodes(ArrayList<ITXMResult> srcNodes, Class type, boolean onlyVisible)	{
208
		ArrayList<ITXMResult> nodes = new ArrayList<ITXMResult>();
353
	public static ArrayList<TXMResult> getNodes(ArrayList<TXMResult> srcNodes, Class type, boolean onlyVisible)	{
354
		ArrayList<TXMResult> nodes = new ArrayList<TXMResult>();
209 355
		for(int i = 0; i < srcNodes.size(); i++) {
210 356
			if((type == null || srcNodes.get(i).getClass().equals(type)) && (onlyVisible == false || srcNodes.get(i).isVisible()))	{
211 357
				nodes.add(srcNodes.get(i));
......
216 362
	
217 363
	
218 364
	
219
	@Override
220
	synchronized public ArrayList<ITXMResult> getSiblings()	{
221
		ArrayList<ITXMResult> sibling = new ArrayList<ITXMResult>();
365
	/**
366
	 * Gets the sibling nodes of this node result.
367
	 */
368
	synchronized public ArrayList<TXMResult> getSiblings()	{
369
		ArrayList<TXMResult> sibling = new ArrayList<TXMResult>();
222 370
		if(this.parent != null)	{
223 371
			sibling.addAll(this.parent.getResults());
224 372
			sibling.remove(this);
......
230 378
	/**
231 379
	 * Gets the sibling nodes of this node result, specified by their class.
232 380
	 */
233
	synchronized public ArrayList<ITXMResult> getSiblings(Class type)	{
234
		ArrayList<ITXMResult> sibling = new ArrayList<ITXMResult>();
381
	synchronized public ArrayList<TXMResult> getSiblings(Class type)	{
382
		ArrayList<TXMResult> sibling = new ArrayList<TXMResult>();
235 383
		if(this.parent != null)	{
236 384
			sibling.addAll(TXMResult.getNodes(this.parent.getResults(), type, true));
237 385
			sibling.remove(this);
......
240 388
	}
241 389

  
242 390
	
243
	@Override
391
	/**
392
	 * Checks if the result must be displayed.  
393
	 * @return
394
	 */
244 395
	public boolean isVisible()	{
245 396
		return this.visible;
246 397
	}
247 398
	
248
	@Override
399
	/**
400
	 * Sets the result visibility state.
401
	 * @param visible
402
	 * @return
403
	 */
249 404
	public void setVisible(boolean visible)	{
250 405
		this.visible = visible;
251 406
	}
252 407
	
253
	
254 408

  
255
	@Override
256
	public void setParent(ITXMResult parent)	{
409
	/**
410
	 * Sets the parent.
411
	 * @param parent
412
	 */
413
	public void setParent(TXMResult parent)	{
257 414
		this.parent = parent;
258 415
	}
259 416
	
260 417
	
261
	@Override
262
	public ITXMResult getParent() {
418
	/**
419
	 * Gets the parent.
420
	 * @return the parent
421
	 */
422
	public TXMResult getParent() {
263 423
		return parent;
264 424
	}
265 425
	
266 426
	
267
	@Override
427
	/**
428
	 * Gets the name of the result.
429
	 * @return
430
	 */
268 431
	public abstract String getName();
269 432
	
270
	@Override
433
	/**
434
	 * Gets the simple name of the result.
435
	 * @return
436
	 */
271 437
	public abstract String getSimpleName();
272 438
	
273
	@Override
439
	/**
440
	 * Gets some details about how the result has been computed (eg. additional parameters, etc.).
441
	 * @return
442
	 */
274 443
	public abstract String getDetails();
275 444
	
445
	/**
446
	 * Returns a string representation of a unique ID of the result.
447
	 * @return
448
	 */
449
	public String getUUID()	{
450
		return this.uniqueID;
451
	}
452
	
453
	/**
454
	 * Gets a string representing the result and that can be used as a file name.
455
	 * @return
456
	 */
276 457
	//FIXME: to discuss and/or to move in export layer
277
	@Override
278 458
	public String getValidFileName()	{
279 459
		try {
280 460
			return this.getName().replaceAll("[^a-zA-Z0-9\\._-]+", "_");
......
284 464
		return "";
285 465
	}
286 466
	
287
	@Override
467
	/**
468
	 * Gets a string representing the concatenation of the simple names of the branch of this result.
469
	 * @return
470
	 */
288 471
	synchronized public String getFullPathSimpleName()	{
289 472

  
290 473
		// fill the branch
291
		ArrayList<ITXMResult> branch = new ArrayList<ITXMResult>();
292
		ITXMResult node = this;
474
		ArrayList<TXMResult> branch = new ArrayList<TXMResult>();
475
		TXMResult node = this;
293 476
		do	{
294 477
			branch.add(node);
295 478
			node = node.getParent();
......
318 501
//	}
319 502

  
320 503

  
321
	@Override
504
	/**
505
	 * Gets the weight of the node. Essentially used for sorting purpose.
506
	 * @return the weight
507
	 */
322 508
	public int getWeight() {
323 509
		return weight;
324 510
	}
325 511

  
326
	@Override
327
	public abstract void clean();
328
	
329
	
330 512
	/**
331
	 * 
332
	 * @param step
513
	 * Cleans and free some resources if needed.
514
	 * This methods is applied when calling <code>TXMResult.delete()</code>. 
333 515
	 */
334
	//FIXME: proposals to computing
335
	//public abstract void compute(int step);
336
	//FIXME: proposals to computing
337
//	public boolean compute(int start, int end)	{
338
//		for(int i = start; i <= end; i++) {
339
//			if(Thread.interrupted())	{
340
//				this.clean();
341
//				return false;
342
//			}
343
//			this.compute(i);
344
//		}
345
//		return true;
346
//	}
516
	public abstract void clean();
347 517
	
348
	
518

  
349 519
	@Override
350 520
	public Object clone() {
351 521
	    TXMResult clone = null;
......
357 527
		}
358 528
	    return clone;
359 529
	}
530

  
531

  
532
	/**
533
	 * @param parameters the parameters to set
534
	 */
535
	public void setParameters(TXMParameters parameters) {
536
		this.parameters = parameters;
537
	}
360 538
	
361 539
}
362 540

  

Also available in: Unified diff