Révision 444

tmp/org.txm.core/src/java/org/txm/functions/Function.java (revision 444)
1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate: 2016-09-19 10:31:00 +0200 (Mon, 19 Sep 2016) $
25
// $LastChangedRevision: 3298 $
26
// $LastChangedBy: mdecorde $ 
27
//
28
package org.txm.functions;
29

  
30
import java.io.File;
31

  
32
import org.txm.core.results.ITXMResult;
33
import org.txm.core.results.TXMResult;
34

  
35
/**
36
 * 
37
 * Function which produces a result of Textometry computation
38
 * 
39
 * The abstract Class Function.
40
 * 
41
 * Has methods to interact with a ProcessWatcher: set work amount, set messages, acquire/release interruption semaphore.
42
 * 
43
 *  @author mdecorde
44
 *  @author nkredens
45
 */
46
public abstract class Function extends TXMResult {
47
	
48
	/**
49
	 * 
50
	 */
51
	protected ProgressWatcher monitor;
52
	
53
	
54
	
55
	/**
56
	 * 
57
	 */
58
	public Function() {
59
		super();
60
	}
61

  
62
	/**
63
	 * 
64
	 * @param parent
65
	 */
66
	public Function(ITXMResult parent) {
67
		super(parent);
68
	}
69

  
70
	/**
71
	 * You must set the current monitor to be available to manage the process progress
72
	 * @param monitor
73
	 */
74
	public void setCurrentMonitor(ProgressWatcher monitor) {
75
		this.monitor = monitor;
76
	}
77
	
78
	/**
79
	 * increment the process 
80
	 * @param amount of work
81
	 */
82
	public void worked(int amount) {
83
		if (monitor != null) {
84
			//System.out.println("worked "+amount);
85
			monitor.worked(amount);
86
		}
87
	}
88
	
89
	/**
90
	 * increment the progress and show a message
91
	 * @param amount
92
	 * @param message
93
	 */
94
	public void worked(int amount, String message) {
95
		if (monitor != null) {
96
			monitor.worked(amount, message);
97
		}
98
	}
99
	
100
	/**
101
	 * show a message in progress
102
	 * @param message
103
	 */
104
	public void setMessage(String message) {
105
		if (monitor != null)
106
			monitor.setMessage(message);
107
	}
108
	
109
	/**
110
	 * 
111
	 * @return true if the monitor has been canceled by the user
112
	 */
113
	public boolean isCanceled() {
114
		if (monitor != null) {
115
			return monitor.isCanceled();
116
		}
117
		return false;
118
	}
119
	
120
	public void done() {
121
		if (monitor != null) {
122
			monitor.done();
123
		}
124
	}
125
	
126
	public void acquireSemaphore() {
127
		if (monitor != null)
128
			monitor.acquireSemaphore();
129
	}
130
	
131
	public boolean tryAcquireSemaphore() {
132
		if (monitor != null)
133
			return monitor.tryAcquireSemaphore();
134
		else
135
			return false;
136
	}
137
	
138
	public void releaseSemaphore() {
139
		if (monitor != null)
140
			monitor.releaseSemaphore();
141
	}
142
	
143
	//public abstract boolean toTxt(File file, String encoding) throws Exception;
144
	@Deprecated
145
	//FIXME: should be moved in an exporter extension
146
	public abstract boolean toTxt(File outfile, String encoding, String colseparator, String txtseparator) throws Exception;
147
	
148
	
149
	
150

  
151
	
152
	/**
153
	 * 
154
	 * @return the array of extensions to show in the FileDialog SWT widget
155
	 */
156
	@Deprecated
157
	//FIXME: should be moved in an exporter extension
158
	public String[] getExportTXTExtensions() {
159
		return new String[]{"*.csv"};
160
	}
161
	
162
	/**
163
	 * 
164
	 * @return the parent Object that contains this result
165
	 */
166
	@Deprecated
167
	// FIXME: to remove when TXMResult2 will be fully implemented
168
	//public abstract HasResults getHasResultsParent();
169
	
170
	/**
171
	 * Used by the Corpora view to open an editor from a double-clicked element.
172
	 * Extension functions must override this method to open an editor from their produced results
173
	 * @return
174
	 */
175
	public boolean openEditor() {
176
		return false;
177
	}
178
}
tmp/org.txm.core/src/java/org/txm/functions/TXMCommand.java (revision 444)
1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate: 2016-09-19 10:31:00 +0200 (Mon, 19 Sep 2016) $
25
// $LastChangedRevision: 3298 $
26
// $LastChangedBy: mdecorde $ 
27
//
28
package org.txm.functions;
29

  
30
import java.io.File;
31

  
32
import org.txm.core.preferences.TXMPreferences;
33
import org.txm.core.results.TXMParameters;
34
import org.txm.core.results.TXMResult;
35

  
36
/**
37
 * 
38
 * Function which produces a result of Textometry computation
39
 * 
40
 * The abstract Class Function.
41
 * 
42
 * Has methods to interact with a ProcessWatcher: set work amount, set messages, acquire/release interruption semaphore.
43
 * 
44
 *  @author mdecorde
45
 *  @author nkredens
46
 */
47
public abstract class TXMCommand extends TXMResult {
48

  
49

  
50
	/**
51
	 * 
52
	 */
53
	protected ProgressWatcher monitor;
54
	
55
	
56
	
57
	public TXMCommand() {
58
		super();
59
		// TODO Auto-generated constructor stub
60
	}
61

  
62
	public TXMCommand(TXMResult parent, TXMParameters parameters) {
63
		super(parent, parameters);
64
		// TODO Auto-generated constructor stub
65
	}
66

  
67
	public TXMCommand(TXMResult parent) {
68
		super(parent);
69
		// TODO Auto-generated constructor stub
70
	}
71

  
72
	
73
	/**
74
	 * You must set the current monitor to be available to manage the process progress
75
	 * @param monitor
76
	 */
77
	public void setCurrentMonitor(ProgressWatcher monitor) {
78
		this.monitor = monitor;
79
	}
80
	
81
	/**
82
	 * increment the process 
83
	 * @param amount of work
84
	 */
85
	public void worked(int amount) {
86
		if (monitor != null) {
87
			//System.out.println("worked "+amount);
88
			monitor.worked(amount);
89
		}
90
	}
91
	
92
	/**
93
	 * increment the progress and show a message
94
	 * @param amount
95
	 * @param message
96
	 */
97
	public void worked(int amount, String message) {
98
		if (monitor != null) {
99
			monitor.worked(amount, message);
100
		}
101
	}
102
	
103
	/**
104
	 * show a message in progress
105
	 * @param message
106
	 */
107
	public void setMessage(String message) {
108
		if (monitor != null)
109
			monitor.setMessage(message);
110
	}
111
	
112
	/**
113
	 * 
114
	 * @return true if the monitor has been canceled by the user
115
	 */
116
	public boolean isCanceled() {
117
		if (monitor != null) {
118
			return monitor.isCanceled();
119
		}
120
		return false;
121
	}
122
	
123
	public void done() {
124
		if (monitor != null) {
125
			monitor.done();
126
		}
127
	}
128
	
129
	public void acquireSemaphore() {
130
		if (monitor != null)
131
			monitor.acquireSemaphore();
132
	}
133
	
134
	public boolean tryAcquireSemaphore() {
135
		if (monitor != null)
136
			return monitor.tryAcquireSemaphore();
137
		else
138
			return false;
139
	}
140
	
141
	public void releaseSemaphore() {
142
		if (monitor != null)
143
			monitor.releaseSemaphore();
144
	}
145
	
146
	//public abstract boolean toTxt(File file, String encoding) throws Exception;
147
	@Deprecated
148
	//FIXME: should be moved in an exporter extension
149
	public abstract boolean toTxt(File outfile, String encoding, String colseparator, String txtseparator) throws Exception;
150
	
151
	
152
	
153

  
154
	
155
	/**
156
	 * 
157
	 * @return the array of extensions to show in the FileDialog SWT widget
158
	 */
159
	@Deprecated
160
	//FIXME: should be moved in an exporter extension
161
	public String[] getExportTXTExtensions() {
162
		return new String[]{"*.csv"};
163
	}
164
	
165
	/**
166
	 * 
167
	 * @return the parent Object that contains this result
168
	 */
169
	@Deprecated
170
	// FIXME: to remove when TXMResult2 will be fully implemented
171
	//public abstract HasResults getHasResultsParent();
172
	
173
	/**
174
	 * Used by the Corpora view to open an editor from a double-clicked element.
175
	 * Extension functions must override this method to open an editor from their produced results
176
	 * @return
177
	 */
178
	public boolean openEditor() {
179
		return false;
180
	}
181
	
182
	
183
	/**
184
	 * Computes the result according to specified command parameters.
185
	 * @param parameters
186
	 * @return
187
	 */
188
	public boolean compute()	{
189
		return this.compute(null);
190
	}
191
	
192

  
193
	/**
194
	 * Computes the result according to stored command parameters.
195
	 * @param watcher
196
	 * @return
197
	 */
198
	public abstract boolean compute(ProgressWatcher watcher);
199
	
200

  
201
	/**
202
	 * Computes the result according to specified command parameters.
203
	 * @param watcher
204
	 * @param parameters
205
	 * @return
206
	 */
207
	public boolean compute(ProgressWatcher watcher, TXMParameters parameters) {
208
		this.parameters = parameters;
209
		return this.compute(watcher);
210
	}
211
	
212
	
213
	//FIXME: tests for forcing to redefine, ex. Cooccurrence.compute(int minLeft, int maxLeft, etc.)
214
//	public abstract boolean compute(Object... parameters);
215
		
216
	
217
	
218
	/**
219
	 * 
220
	 * @param step
221
	 */
222
	//FIXME: proposals to computing
223
	//public abstract void compute(int step);
224
	//FIXME: proposals to computing
225
//	public boolean compute(int start, int end)	{
226
//		for(int i = start; i <= end; i++) {
227
//			if(Thread.interrupted())	{
228
//				this.clean();
229
//				return false;
230
//			}
231
//			this.compute(i);
232
//		}
233
//		return true;
234
//	}
235
	
236
	
237
	
238

  
239
	
240
}
0 241

  
tmp/org.txm.core/src/java/org/txm/functions/CommandsAPI.java (revision 444)
3 3
import java.util.HashMap;
4 4
import java.util.HashSet;
5 5

  
6
import org.txm.core.results.TXMParameters;
6 7
import org.txm.core.results.TXMResult;
7 8
import org.txm.utils.logger.Log;
8 9

  
......
24 25
	
25 26
	protected static final HashMap<String, Command> cmds = new HashMap<String, Command>();
26 27
	
27
	public static void install(String cmd, Class<TXMResult> clazz) {
28
	public static void install(String cmd, Class<TXMCommand> clazz) {
28 29
		CommandsAPI.cmds.put(cmd, new Command(cmd,clazz));
29 30
	}
30 31
	
......
49 50
	 * @param clazz
50 51
	 * @param mandatory_parameters
51 52
	 */
52
	public static void install(String cmd, Class<TXMResult> clazz, HashSet<String> mandatoryParameters) {
53
	public static void install(String cmd, Class<TXMCommand> clazz, HashSet<String> mandatoryParameters) {
53 54
		Command command = new Command(cmd,clazz);
54 55
		command.mandatoryParameters.addAll(mandatoryParameters);
55 56
		CommandsAPI.cmds.put(cmd, command);
......
63 64
	 * @param mandatory_parameters
64 65
	 * @param optional_parameters
65 66
	 */
66
	public static void install(String cmd, Class<TXMResult> clazz, HashSet<String> mandatoryParameters, HashSet<String> optionalParameters) {
67
	public static void install(String cmd, Class<TXMCommand> clazz, HashSet<String> mandatoryParameters, HashSet<String> optionalParameters) {
67 68
		Command command = new Command(cmd,clazz);
68 69
		command.mandatoryParameters.addAll(mandatoryParameters);
69 70
		command.optionalParameters.addAll(optionalParameters);
......
79 80
		CommandsAPI.cmds.remove(cmd);
80 81
	}
81 82
	
82
	public Object call(String cmd, TXMResult parent, HashMap<String, Object> parameters) {
83
	//FIXME: the source is not used, see if we decide to put it the TXMParameters or not, otherwise define a method TXMResult.setSource() or maybe better a method TXMResult.compute(TXMResul source)
84
	public Object call(String cmd, TXMResult source, TXMParameters parameters) {
83 85
		
84 86
		Command command = cmds.get(cmd);
85 87
		if (command == null) {
......
88 90
		}
89 91
		
90 92
		// compute directly the command using the parameters 
91
		TXMResult result = null;
93
		TXMCommand result = null;
92 94
		try {
93 95
			if (command.mandatoryParameters.containsAll(parameters.keySet())) {
94 96
				System.out.println("Computing "+command.cmd+"...");
95 97
				result = command.clazz.newInstance();
96
				//result.compute(parameters);
97
				//System.out.println(result);
98
				result.setParameters(parameters);
99
				result.compute();
100
				//FIXME: debug
101
				System.out.println(result);
98 102
			} else {
99 103
				System.out.println("Failed to compute result with "+cmd+" command: missing some mandatory parameters: "+command.mandatoryParameters);
100 104
			}
tmp/org.txm.core/src/java/org/txm/functions/Command.java (revision 444)
13 13
public class Command {
14 14
	
15 15
	public final String cmd;
16
	public final Class<TXMResult> clazz;
16
	public final Class<TXMCommand> clazz;
17 17
	public final HashSet<String> mandatoryParameters = new HashSet<String>();
18 18
	public final HashSet<String> optionalParameters = new HashSet<String>();
19 19
	
20
	public Command(Class<TXMResult> clazz) {
20
	public Command(Class<TXMCommand> clazz) {
21 21
		this.cmd = clazz.getSimpleName();
22 22
		this.clazz = clazz;
23 23
	}
24 24
	
25
	public Command(String cmd, Class<TXMResult> clazz) {
25
	public Command(String cmd, Class<TXMCommand> clazz) {
26 26
		this.cmd = cmd;
27 27
		this.clazz = clazz;
28 28
	}

Formats disponibles : Unified diff