|
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: 2015-06-15 15:29:16 +0200 (Mon, 15 Jun 2015) $
|
|
25 |
// $LastChangedRevision: 2989 $
|
|
26 |
// $LastChangedBy: mdecorde $
|
|
27 |
//
|
|
28 |
package org.txm.stat.engine.r;
|
|
29 |
|
|
30 |
import java.io.BufferedReader;
|
|
31 |
import java.io.File;
|
|
32 |
import java.io.FileReader;
|
|
33 |
import java.io.FileWriter;
|
|
34 |
import java.io.IOException;
|
|
35 |
import java.sql.Connection;
|
|
36 |
import java.util.ArrayList;
|
|
37 |
import java.util.Arrays;
|
|
38 |
import java.util.UUID;
|
|
39 |
|
|
40 |
import org.apache.commons.lang.StringEscapeUtils;
|
|
41 |
import org.apache.commons.lang.StringUtils;
|
|
42 |
import org.eclipse.core.runtime.Platform;
|
|
43 |
import org.eclipse.osgi.util.NLS;
|
|
44 |
import org.eclipse.ui.internal.util.BundleUtility;
|
|
45 |
import org.osgi.framework.Bundle;
|
|
46 |
import org.rosuda.REngine.REXP;
|
|
47 |
import org.rosuda.REngine.REXPLogical;
|
|
48 |
import org.rosuda.REngine.REXPMismatchException;
|
|
49 |
import org.rosuda.REngine.REngineException;
|
|
50 |
import org.rosuda.REngine.Rserve.RConnection;
|
|
51 |
import org.rosuda.REngine.Rserve.RserveException;
|
|
52 |
import org.txm.Messages;
|
|
53 |
import org.txm.stat.StatException;
|
|
54 |
import org.txm.stat.data.QuantitativeDataStructure;
|
|
55 |
import org.txm.stat.engine.r.data.RObjectAlreadyExist;
|
|
56 |
import org.txm.stat.engine.r.rcolt.RColt;
|
|
57 |
import org.txm.stat.utils.VectorizeArray;
|
|
58 |
import org.txm.utils.OSDetector;
|
|
59 |
import org.txm.utils.StreamHog;
|
|
60 |
import org.txm.utils.SystemProxyDetector;
|
|
61 |
import org.txm.utils.logger.Log;
|
|
62 |
|
|
63 |
import cern.colt.list.DoubleArrayList;
|
|
64 |
import cern.colt.list.IntArrayList;
|
|
65 |
import cern.colt.matrix.DoubleMatrix2D;
|
|
66 |
|
|
67 |
// TODO: Auto-generated Javadoc
|
|
68 |
/**
|
|
69 |
* RWorkspace is the wrapper on top of the R Engine. All data sent to and
|
|
70 |
* retreive from R are using this class. One instance of this class must be
|
|
71 |
* created in a program.
|
|
72 |
*
|
|
73 |
* This class throw exception RWorkspace exception, encapsulating lower level
|
|
74 |
* exception such as <code>RserveException</code>,
|
|
75 |
* <code>REXPMismatchException</code> or <code>REngineException</code>.
|
|
76 |
*
|
|
77 |
* This class offers many methods avoiding other classes in the toolbox to
|
|
78 |
* contain R code. The goal is to concentrate R code, as much as possible, here.
|
|
79 |
* See {@link #extractItemFromListByName(String, String)} for instance, and
|
|
80 |
* other "extract..." methods.
|
|
81 |
*
|
|
82 |
* methods in this class may be organized in the following group:
|
|
83 |
* <ul>
|
|
84 |
* <li>1/ initialization, connection to R, shutdown, etc.</li>
|
|
85 |
* <li>2/ adding object to the R workspace (methods prefixed with "add").</li>
|
|
86 |
* <li>3/ methods encapsulating R code for common fonctions: methods prefixed
|
|
87 |
* with "assign...", "extract..." for instance.</li>
|
|
88 |
* <li>4/ methods for evaluating R expression ({@link #safeEval(String)},
|
|
89 |
* {@link #eval(String)}, {@link #evalToDouble(String)}, etc.)</li>
|
|
90 |
* <li>5/ Static methods for converting result of evaluation in type (
|
|
91 |
* {@link #toDouble(REXP)}, etc.)</li>
|
|
92 |
* <li>6/ methods for checking existence of R object in the R workspace,
|
|
93 |
* removing object from the R workspace, etc.
|
|
94 |
* {@link #checkForDuplicateVariable(String)}, {@link #clearWorkspace()},
|
|
95 |
* {@link #containsVariable(String)},
|
|
96 |
* {@link #removeVariableFromWorkspace(String)}, etc.)</li>
|
|
97 |
* <li>7/ methods for plotting.</li>
|
|
98 |
* </ul>
|
|
99 |
*
|
|
100 |
* TODO : assign/remove... in a textometrie environment, when Rengine.assign()
|
|
101 |
* will be able to deal with R environment.
|
|
102 |
*
|
|
103 |
* TODO : remove unused variables from workspace.
|
|
104 |
*
|
|
105 |
* @author Sylvain Loiseau <sloiseau@ens-lsh.fr>
|
|
106 |
*/
|
|
107 |
|
|
108 |
public final class RWorkspace {
|
|
109 |
|
|
110 |
/** The comm. */
|
|
111 |
private static RFileCommunication comm;
|
|
112 |
|
|
113 |
/** The connection. */
|
|
114 |
private static RConnection connection = null;
|
|
115 |
|
|
116 |
/** The eval logs. */
|
|
117 |
static ArrayList<String> evalLogs = new ArrayList<String>();
|
|
118 |
|
|
119 |
/** The filecommunication. */
|
|
120 |
private static boolean filecommunication = false;
|
|
121 |
|
|
122 |
// --------------------------------------
|
|
123 |
/** The last safeeval expr. */
|
|
124 |
static String lastSafeevalExpr = ""; //$NON-NLS-1$
|
|
125 |
|
|
126 |
/** The Rserve process. */
|
|
127 |
private static Process RserveProcess = null;
|
|
128 |
|
|
129 |
/** The workspace. */
|
|
130 |
private static RWorkspace workspace = null;
|
|
131 |
|
|
132 |
/** The error logger. */
|
|
133 |
private StreamHog errorLogger;
|
|
134 |
|
|
135 |
/** The input logger. */
|
|
136 |
private StreamHog inputLogger;
|
|
137 |
|
|
138 |
/** The loggin. */
|
|
139 |
private boolean logging;
|
|
140 |
|
|
141 |
/** The DEFAUL t_ symbol. */
|
|
142 |
//private String DEFAULT_SYMBOL = "txmresult"; //$NON-NLS-1$
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Folder where to create the R working directory
|
|
146 |
*/
|
|
147 |
protected File userdir = null;
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Appendto eval log.
|
|
151 |
*
|
|
152 |
* @param cmd the cmd
|
|
153 |
*/
|
|
154 |
public static void appendtoEvalLog(String cmd)
|
|
155 |
{
|
|
156 |
evalLogs.add(cmd);
|
|
157 |
}
|
|
158 |
|
|
159 |
/**
|
|
160 |
* Close the connection.
|
|
161 |
*
|
|
162 |
* @throws RWorkspaceException the r workspace exception
|
|
163 |
*/
|
|
164 |
public static final void closeConnection() throws RWorkspaceException {
|
|
165 |
// If no R object have been created during the lif ecycle of the
|
|
166 |
// application,
|
|
167 |
// the connection has never been initialized.
|
|
168 |
if (connection != null)
|
|
169 |
connection.close();
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Connect to Rserve.
|
|
174 |
*
|
|
175 |
* @param host the host
|
|
176 |
* @param port the port
|
|
177 |
* @return true if success
|
|
178 |
* @throws RWorkspaceException the r workspace exception
|
|
179 |
*/
|
|
180 |
public static boolean connect(String host, int port)
|
|
181 |
throws RWorkspaceException {
|
|
182 |
try {
|
|
183 |
Log.info(Messages.RWorkspace_2+host+":"+port); //$NON-NLS-1$
|
|
184 |
connection = new RConnection(host, port);
|
|
185 |
//connection.setStringEncoding("UTF-8");
|
|
186 |
|
|
187 |
return isConnected();
|
|
188 |
} catch (RserveException x) {
|
|
189 |
throw new RWorkspaceException(x);
|
|
190 |
}
|
|
191 |
|
|
192 |
}
|
|
193 |
|
|
194 |
/**
|
|
195 |
* Connect to Rserve.
|
|
196 |
*
|
|
197 |
* @param host the host
|
|
198 |
* @param port the port
|
|
199 |
* @param user the user
|
|
200 |
* @param password the password
|
|
201 |
* @return true, if successful
|
|
202 |
*/
|
|
203 |
public static boolean connect(String host, int port, String user,
|
|
204 |
String password) {
|
|
205 |
try {
|
|
206 |
Log.info(Messages.RWorkspace_2+host+":"+port+" user :"+user); //$NON-NLS-2$ //$NON-NLS-1$
|
|
207 |
connection = new RConnection(host, port);
|
|
208 |
connection.login(user, password);
|
|
209 |
return isConnected();
|
|
210 |
|
|
211 |
} catch (RserveException x) {
|
|
212 |
System.out.println(Messages.RWorkspace_0 + host+ ", " + port + ", " + user); //$NON-NLS-1$//$NON-NLS-2$
|
|
213 |
Log.severe(Messages.RWorkspace_0 + host+ ", " + port + ", " + user); //$NON-NLS-1$//$NON-NLS-2$
|
|
214 |
}
|
|
215 |
return false;
|
|
216 |
}
|
|
217 |
|
|
218 |
/**
|
|
219 |
* Gets the last safe eval expr.
|
|
220 |
*
|
|
221 |
* @return the last safe eval expr
|
|
222 |
*/
|
|
223 |
public static String getLastSafeEvalExpr()
|
|
224 |
{
|
|
225 |
return lastSafeevalExpr;
|
|
226 |
}
|
|
227 |
|
|
228 |
|
|
229 |
public static String getRandomSymbol() {
|
|
230 |
String symbol = "Random"+UUID.randomUUID(); //$NON-NLS-1$
|
|
231 |
return symbol;
|
|
232 |
}
|
|
233 |
|
|
234 |
/**
|
|
235 |
* Entering the R workspace.
|
|
236 |
*
|
|
237 |
* @param
|
|
238 |
* @return the r workspace instance
|
|
239 |
* @throws RWorkspaceException the r workspace exception
|
|
240 |
*/
|
|
241 |
public static final RWorkspace getRWorkspaceInstance()
|
|
242 |
throws RWorkspaceException {
|
|
243 |
Bundle bundle = Platform.getBundle("org.txm.r");
|
|
244 |
File userdir = new File(bundle.getLocation());
|
|
245 |
System.out.println("BUNDLE LOCATION OF 'org.txm.r': "+userdir);
|
|
246 |
if (workspace == null) {
|
|
247 |
workspace = new RWorkspace(userdir);
|
|
248 |
}
|
|
249 |
return workspace;
|
|
250 |
}
|
|
251 |
|
|
252 |
/**
|
|
253 |
* Post initialisation of Rserve : loads libraries.
|
|
254 |
* @deprecated The extensions will load themself the packages they need
|
|
255 |
* @throws RWorkspaceException the r workspace exception
|
|
256 |
*/
|
|
257 |
private static void init() throws RWorkspaceException {
|
|
258 |
String[] packagesToLoad = { }; //$NON-NLS-1$
|
|
259 |
for (int i = 0; i < packagesToLoad.length; i++) {
|
|
260 |
String name = packagesToLoad[i];
|
|
261 |
try {
|
|
262 |
// System.out.println("load R lib : " + name);
|
|
263 |
connection.eval(("library(" + name + ")")); //$NON-NLS-1$ //$NON-NLS-2$
|
|
264 |
// System.out.println("Done : load R lib : " + name);
|
|
265 |
} catch (RserveException e) {
|
|
266 |
throw new RWorkspaceException(NLS.bind(Messages.LIBBRARY_NOT_LOADED, name, e));
|
|
267 |
} catch (Exception e) {
|
|
268 |
org.txm.utils.logger.Log.printStackTrace(e);
|
|
269 |
}
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
/**
|
|
274 |
* Default initialisation of RWorkspace with defautl parameters :
|
|
275 |
* address = 127.0.0.1
|
|
276 |
* port = 6311
|
|
277 |
* debug = false.
|
|
278 |
*
|
|
279 |
* @param pathToRExecutalbe the path to r executalbe
|
|
280 |
* @throws RWorkspaceException the r workspace exception
|
|
281 |
*/
|
|
282 |
public static final void initializeRWorkspace(String pathToRExecutalbe)
|
|
283 |
throws RWorkspaceException {
|
|
284 |
workspace = new RWorkspace(pathToRExecutalbe, "127.0.0.1", 6311, false); //$NON-NLS-1$
|
|
285 |
}
|
|
286 |
|
|
287 |
/**
|
|
288 |
* Initialize r workspace.
|
|
289 |
* debug = false
|
|
290 |
*
|
|
291 |
* @param pathToRExecutalbe the path to r executalbe
|
|
292 |
* @param host the host
|
|
293 |
* @param port the port
|
|
294 |
* @throws RWorkspaceException the r workspace exception
|
|
295 |
*/
|
|
296 |
public static final void initializeRWorkspace(String pathToRExecutalbe,
|
|
297 |
String host, int port) throws RWorkspaceException {
|
|
298 |
workspace = new RWorkspace(pathToRExecutalbe, host, port, false);
|
|
299 |
}
|
|
300 |
|
|
301 |
// --------------------------------------
|
|
302 |
/**
|
|
303 |
* check R path
|
|
304 |
* check if Rserve has already been launched
|
|
305 |
* if not try to launch it.
|
|
306 |
*
|
|
307 |
* @param pathToRExecutable the path to r executable
|
|
308 |
* @param options
|
|
309 |
* @param rServeArgs
|
|
310 |
* @return true if Rserve is launched
|
|
311 |
* @throws RWorkspaceException the r workspace exception
|
|
312 |
*/
|
|
313 |
private static boolean initRserve(String pathToRExecutable, int port, boolean debug, String rargs, String rServeArgs)
|
|
314 |
throws RWorkspaceException {
|
|
315 |
boolean isRServerOk;
|
|
316 |
if (pathToRExecutable == null || pathToRExecutable.length() == 0) {
|
|
317 |
isRServerOk = StartRserve.checkLocalRserve(port, debug, rargs, rServeArgs);
|
|
318 |
if (!isRServerOk) {
|
|
319 |
throw new RWorkspaceException(
|
|
320 |
Messages.RSERVE_PATH_NOT_SET_ERROR);
|
|
321 |
}
|
|
322 |
} else {
|
|
323 |
Log.info(Messages.RWorkspace_10+pathToRExecutable);
|
|
324 |
isRServerOk = StartRserve.launchRserve(pathToRExecutable, port, debug, rargs, rServeArgs);
|
|
325 |
// System.out.println("ap launchRserve");
|
|
326 |
if (!isRServerOk) {
|
|
327 |
throw new RWorkspaceException(Messages.RSERVE_PATH_SET_ERROR+ " : "+pathToRExecutable); //$NON-NLS-1$
|
|
328 |
}
|
|
329 |
}
|
|
330 |
|
|
331 |
if (isRServerOk) {
|
|
332 |
Log.finest("RSERVE_ACTIVATED"); //$NON-NLS-1$
|
|
333 |
System.out.println(Messages.CONNECTED_TO_STATS_MODULE);
|
|
334 |
RserveProcess = StartRserve.Rserveprocess;
|
|
335 |
}
|
|
336 |
return isRServerOk;
|
|
337 |
}
|
|
338 |
|
|
339 |
/**
|
|
340 |
* Checks if is connected.
|
|
341 |
*
|
|
342 |
* @return true if the RWorkspace is connected to RServe
|
|
343 |
*/
|
|
344 |
private static boolean isConnected() {
|
|
345 |
if (!connection.isConnected()) {
|
|
346 |
return false;
|
|
347 |
} else {
|
|
348 |
return true;
|
|
349 |
}
|
|
350 |
}
|
|
351 |
|
|
352 |
|
|
353 |
/**
|
|
354 |
* Checks if is file tranfert.
|
|
355 |
*
|
|
356 |
* @return true, if is file tranfert
|
|
357 |
*/
|
|
358 |
public static boolean isFileTranfert() {
|
|
359 |
return filecommunication;
|
|
360 |
}
|
|
361 |
|
|
362 |
|
|
363 |
|
|
364 |
/**
|
|
365 |
* Load eval log.
|
|
366 |
*
|
|
367 |
* @param file the file
|
|
368 |
* @throws IOException Signals that an I/O exception has occurred.
|
|
369 |
*/
|
|
370 |
public static void loadEvalLog(File file) throws IOException
|
|
371 |
{
|
|
372 |
BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
373 |
String cmd = reader.readLine();
|
|
374 |
while(cmd != null)
|
|
375 |
{
|
|
376 |
evalLogs.add(cmd);
|
|
377 |
cmd = reader.readLine();
|
|
378 |
}
|
|
379 |
reader.close();
|
|
380 |
}
|
|
381 |
|
|
382 |
/**
|
|
383 |
* Prints the last safe eval expr.
|
|
384 |
*/
|
|
385 |
public static void printLastSafeEvalExpr() {
|
|
386 |
System.out.println(Messages.RWorkspace_11+lastSafeevalExpr);
|
|
387 |
}
|
|
388 |
|
|
389 |
/**
|
|
390 |
* Save eval log.
|
|
391 |
*
|
|
392 |
* @param file the file
|
|
393 |
* @throws IOException Signals that an I/O exception has occurred.
|
|
394 |
*/
|
|
395 |
public static void saveEvalLog(File file) throws IOException {
|
|
396 |
FileWriter writer = new FileWriter(file);
|
|
397 |
for (String cmd : evalLogs)
|
|
398 |
writer.write(cmd+"\n"); //$NON-NLS-1$
|
|
399 |
writer.flush();
|
|
400 |
writer.close();
|
|
401 |
}
|
|
402 |
|
|
403 |
/**
|
|
404 |
* if state is true, the matrix will be send to R with a File.
|
|
405 |
*
|
|
406 |
* @param state the state
|
|
407 |
* @return true if success
|
|
408 |
*/
|
|
409 |
public static boolean setUseFileCommunication(boolean state) {
|
|
410 |
filecommunication = state;
|
|
411 |
if (filecommunication) {
|
|
412 |
try {
|
|
413 |
comm = new RFileCommunication();
|
|
414 |
} catch (IOException e) {
|
|
415 |
filecommunication = false;
|
|
416 |
Log.severe(Messages.RWorkspace_6+Log.toString(e));
|
|
417 |
return false;
|
|
418 |
}
|
|
419 |
}
|
|
420 |
return true;
|
|
421 |
}
|
|
422 |
|
|
423 |
/**
|
|
424 |
* Close the connection and destroy the Rserve process.
|
|
425 |
*
|
|
426 |
* @throws RWorkspaceException the r workspace exception
|
|
427 |
*/
|
|
428 |
public static final void shutdown() throws RWorkspaceException {
|
|
429 |
try {
|
|
430 |
// If no R object have been created during the lif ecycle of the
|
|
431 |
// application,
|
|
432 |
// the connection has never been initialized.
|
|
433 |
|
|
434 |
if (connection != null) {
|
|
435 |
if (connection.isConnected()) {
|
|
436 |
// connection.close(); // No: invoquing "close()" make
|
|
437 |
// "shutdown()" to throw a "not connected" exception.
|
|
438 |
connection.shutdown();
|
|
439 |
connection = null; // warning, after shuting down the
|
|
440 |
// server, the connection appears still
|
|
441 |
// ok but using it yield to a
|
|
442 |
// "Broken pipe" exception.
|
|
443 |
}
|
|
444 |
}
|
|
445 |
if (RserveProcess != null) RserveProcess.destroy();
|
|
446 |
} catch (RserveException e) {
|
|
447 |
throw new RWorkspaceException(e);
|
|
448 |
}
|
|
449 |
}
|
|
450 |
|
|
451 |
/**
|
|
452 |
* Start exec.
|
|
453 |
*
|
|
454 |
* @param pathToRExecutable the path to r executable
|
|
455 |
* @param string
|
|
456 |
* @param string
|
|
457 |
* @return true, if successful
|
|
458 |
*/
|
|
459 |
public static boolean startExec(String pathToRExecutable, int port, boolean debug, String rArgs, String rServeArgs) {
|
|
460 |
// System.out.println("Start Exec");
|
|
461 |
try {
|
|
462 |
initRserve(pathToRExecutable, port, debug, rArgs, rServeArgs);
|
|
463 |
return true;
|
|
464 |
} catch (RWorkspaceException e) {
|
|
465 |
System.out.println(Messages.RWorkspace_8+e);
|
|
466 |
org.txm.utils.logger.Log.printStackTrace(e);
|
|
467 |
}
|
|
468 |
return false;
|
|
469 |
}
|
|
470 |
|
|
471 |
/**
|
|
472 |
* To double.
|
|
473 |
*
|
|
474 |
* @param rexp the rexp
|
|
475 |
* @return the double[]
|
|
476 |
* @throws RWorkspaceException the r workspace exception
|
|
477 |
*/
|
|
478 |
public static final double[] toDouble(REXP rexp) throws RWorkspaceException {
|
|
479 |
try {
|
|
480 |
return rexp.asDoubles();
|
|
481 |
} catch (REXPMismatchException e) {
|
|
482 |
throw new RWorkspaceException(e);
|
|
483 |
}
|
|
484 |
}
|
|
485 |
|
|
486 |
/**
|
|
487 |
* protected on purpose. See {@link #getRWorkspaceInstance()}.
|
|
488 |
*
|
|
489 |
* @throws RWorkspaceException the r workspace exception
|
|
490 |
*/
|
|
491 |
public RWorkspace(File userdir) throws RWorkspaceException {
|
|
492 |
this.userdir = userdir;
|
|
493 |
}
|
|
494 |
|
|
495 |
// --------------------------------------
|
|
496 |
|
|
497 |
// /**
|
|
498 |
// * protected on purpose. See {@link #getRWorkspaceInstance()}.
|
|
499 |
// *
|
|
500 |
// * @param pathToRExecutable the path to r executable
|
|
501 |
// * @throws RWorkspaceException the r workspace exception
|
|
502 |
// */
|
|
503 |
// protected RWorkspace(String pathToRExecutable, int port, boolean debug) throws RWorkspaceException {
|
|
504 |
// if (initRserve(pathToRExecutable, port, debug, "","")) {
|
|
505 |
// connect();
|
|
506 |
// init();
|
|
507 |
// }
|
|
508 |
// }
|
|
509 |
|
|
510 |
/**
|
|
511 |
* protected on purpose. See {@link #getRWorkspaceInstance()}.
|
|
512 |
*
|
|
513 |
* @param pathToRExecutable the path to r executable
|
|
514 |
* @param host the host
|
|
515 |
* @param port the port
|
|
516 |
* @throws RWorkspaceException the r workspace exception
|
|
517 |
*/
|
|
518 |
protected RWorkspace(String pathToRExecutable, String host, int port, boolean debug)
|
|
519 |
throws RWorkspaceException {
|
|
520 |
if (initRserve(pathToRExecutable, port, debug, "", "")) {
|
|
521 |
connect(host, port);
|
|
522 |
init();
|
|
523 |
}
|
|
524 |
}
|
|
525 |
|
|
526 |
/**
|
|
527 |
* Add a matrix into the workspace and link it to a name. The inner arrays
|
|
528 |
* of the <code>matrix</code> parameters are the <strong>row</strong> of the
|
|
529 |
* resulting R matrix.
|
|
530 |
*
|
|
531 |
* @param variableName the name to be used in the R workspace.
|
|
532 |
* @param matrix the data to be bound to the name. In the form
|
|
533 |
* <code>matrix[row][column]</code>, with exactly the same number
|
|
534 |
* of column in every row.
|
|
535 |
* @throws RWorkspaceException the r workspace exception
|
|
536 |
*/
|
|
537 |
public void addMatrixToWorkspace(String variableName, double[][] matrix)
|
|
538 |
throws RWorkspaceException {
|
|
539 |
//System.out.println("matrix len: "+matrix.length+" test="+(matrix.length == 0));
|
|
540 |
if (matrix.length == 0) {
|
|
541 |
new RWorkspaceException(Messages.RWorkspace_7);
|
|
542 |
}
|
|
543 |
int ncol = matrix[0].length;
|
|
544 |
int nrow = matrix.length;
|
|
545 |
|
|
546 |
double[] vector = VectorizeArray.vectorizeByInner(matrix);
|
|
547 |
|
|
548 |
if (!filecommunication) {
|
|
549 |
try {
|
|
550 |
connection.assign(variableName, vector);
|
|
551 |
} catch (REngineException e) {
|
|
552 |
throw new RWorkspaceException(e);
|
|
553 |
}
|
|
554 |
try {
|
|
555 |
connection .voidEval(variableName
|
|
556 |
+ "<- matrix(" + variableName + ", nrow=" + nrow + ", ncol=" + ncol + ", byrow=T)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
|
557 |
} catch (RserveException e) {
|
|
558 |
throw new RWorkspaceException(e);
|
|
559 |
}
|
|
560 |
} else {
|
|
561 |
comm.assign(variableName, vector, nrow, ncol);
|
|
562 |
}
|
|
563 |
Log.finest("MATRIX_ADDED_TO_WORKSPACE" + new Object[] { nrow, ncol, variableName }); //$NON-NLS-1$
|
|
564 |
}
|
|
565 |
|
|
566 |
/**
|
|
567 |
* Add a matrix into the workspace and link it to a name. Intended to be
|
|
568 |
* efficient for sparse matrix.
|
|
569 |
*
|
|
570 |
* @param variableName the name to be used in the R workspace.
|
|
571 |
* @param matrix the data to be bound to the name.
|
|
572 |
* @throws RWorkspaceException the r workspace exception
|
|
573 |
*/
|
|
574 |
public void addMatrixToWorkspace(String variableName, DoubleMatrix2D matrix)
|
|
575 |
throws RWorkspaceException {
|
|
576 |
|
|
577 |
double[][] data = RColt.doubleMatrix2D2DoubleDoubleArray(matrix);
|
|
578 |
addMatrixToWorkspace(variableName, data);
|
|
579 |
|
|
580 |
// // IntArrayList x = new IntArrayList();
|
|
581 |
// // IntArrayList y = new IntArrayList();
|
|
582 |
// // DoubleArrayList val = new DoubleArrayList();
|
|
583 |
// //
|
|
584 |
// // matrix.trimToSize();
|
|
585 |
// // matrix.getNonZeros(x, y, val);
|
|
586 |
// // x.trimToSize();
|
|
587 |
// // y.trimToSize();
|
|
588 |
// // val.trimToSize();
|
|
589 |
// //
|
|
590 |
// // for (int i = 0; i < x.size(); i++) {
|
|
591 |
// // x.setQuick(i, x.getQuick(i) + 1);
|
|
592 |
// // }
|
|
593 |
// //
|
|
594 |
// // for (int i = 0; i < y.size(); i++) {
|
|
595 |
// // y.setQuick(i, y.getQuick(i) + 1);
|
|
596 |
// // }
|
|
597 |
// //
|
|
598 |
// // System.out.println("colt");
|
|
599 |
// // System.out.println(x);
|
|
600 |
// // System.out.println(y);
|
|
601 |
// // System.out.println(val);
|
|
602 |
//
|
|
603 |
// int nrow = matrix.rows();
|
|
604 |
// int ncol = matrix.columns();
|
|
605 |
// // System.out.println(matrix);
|
|
606 |
// // System.out.println(Arrays.toString(vald));
|
|
607 |
// try {
|
|
608 |
// // connection.eval(variableName + "<- matrix(0, nrow=" + nrow +
|
|
609 |
// ", ncol=" + ncol + " );");
|
|
610 |
// connection.assign(variableName, m);
|
|
611 |
// } catch (RserveException e) {
|
|
612 |
// throw new RWorkspaceException(e);
|
|
613 |
// }
|
|
614 |
// try {
|
|
615 |
// connection.assign(variableName + ".nonzero", val.elements());
|
|
616 |
// connection.assign(variableName + ".x", x.elements());
|
|
617 |
// connection.assign(variableName + ".y", y.elements());
|
|
618 |
// } catch (REngineException e) {
|
|
619 |
// throw new RWorkspaceException(e);
|
|
620 |
// }
|
|
621 |
// try {
|
|
622 |
// // connection.voidEval("dim(" + variableName + ") <- c(" + nrow +
|
|
623 |
// ", " + ncol + ")");
|
|
624 |
// // connection.voidEval(variableName + "[" + variableName + ".x, " +
|
|
625 |
// variableName + ".y" + " ] <- " + variableName + ".nonzero");
|
|
626 |
// // for (int i = 0; i < x.size(); i++) {
|
|
627 |
// // for (int j = 0; j < y.size(); j++) {
|
|
628 |
// // connection.voidEval(variableName + "[" + x.getQuick(i) + ", " +
|
|
629 |
// y.getQuick(j) + "] <- " + val.getQuick(i * 5 + * j));
|
|
630 |
// // }
|
|
631 |
// // }
|
|
632 |
//
|
|
633 |
// } catch (RserveException e) {
|
|
634 |
// throw new RWorkspaceException(e);
|
|
635 |
// }
|
|
636 |
//
|
|
637 |
// removeVariableFromWorkspace(variableName + ".nonzero");
|
|
638 |
// removeVariableFromWorkspace(variableName + ".x");
|
|
639 |
// removeVariableFromWorkspace(variableName + ".y");
|
|
640 |
//
|
|
641 |
//
|
|
642 |
// System.out.println("--------------matrice d'origine----------");
|
|
643 |
// System.out.println(matrix);
|
|
644 |
//
|
|
645 |
// // DoubleFactory2D.dense.make(arg0)
|
|
646 |
// System.out.println("--------------matrice issue de R----------");
|
|
647 |
// DenseDoubleMatrix2D m = null;
|
|
648 |
// try {
|
|
649 |
// m = new
|
|
650 |
// DenseDoubleMatrix2D(connection.eval(variableName).asDoubleMatrix());
|
|
651 |
// } catch (REXPMismatchException e) {
|
|
652 |
// // TODO Auto-generated catch block
|
|
653 |
// org.txm.utils.logger.Log.printStackTrace(e);
|
|
654 |
// } catch (RserveException e) {
|
|
655 |
// // TODO Auto-generated catch block
|
|
656 |
// org.txm.utils.logger.Log.printStackTrace(e);
|
|
657 |
// }
|
|
658 |
// System.out.println(m);
|
|
659 |
|
|
660 |
}
|
|
661 |
|
|
662 |
//TODO: is addLexiconToWorkspace used ?
|
|
663 |
// /**
|
|
664 |
// * Adds the lexicon to workspace.
|
|
665 |
// *
|
|
666 |
// * @param variableName the variable name
|
|
667 |
// * @param lexicon the lexicon
|
|
668 |
// * @throws RWorkspaceException the r workspace exception
|
|
669 |
// */
|
|
670 |
// public void addLexiconToWorkspace(String variableName, Lexicon lexicon)
|
|
671 |
// throws RWorkspaceException {
|
|
672 |
// int[] freqs = lexicon.getFreq();
|
|
673 |
// String[] forms = lexicon.getForms();
|
|
674 |
// try {
|
|
675 |
// addVectorToWorkspace(variableName, freqs);
|
|
676 |
// addVectorToWorkspace(variableName + "_names", forms); //$NON-NLS-1$
|
|
677 |
// connection
|
|
678 |
// .voidEval("names(" + variableName + ") <- " + variableName + "_names"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
679 |
// connection.voidEval("rm(" + variableName + "_names" + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
680 |
// } catch (RserveException e) {
|
|
681 |
// throw new RWorkspaceException(e);
|
|
682 |
// }
|
|
683 |
// Log.finest("LEXICON_ADDED" + new Object[] { lexicon.nbrOfType() + variableName }); //$NON-NLS-1$
|
|
684 |
// }
|
|
685 |
|
|
686 |
/**
|
|
687 |
* Add a matrix into the workspace and link it to a name. The inner arrays
|
|
688 |
* of the <code>matrix</code> parameters are the <strong>row</strong> of the
|
|
689 |
* resulting R matrix.
|
|
690 |
*
|
|
691 |
* @param variableName the name to be used in the R workspace.
|
|
692 |
* @param matrix the data to be bound to the name. In the form
|
|
693 |
* <code>matrix[row][column]</code>, with exactly the same number
|
|
694 |
* of column in every row.
|
|
695 |
* @throws RWorkspaceException the r workspace exception
|
|
696 |
*/
|
|
697 |
public void addMatrixToWorkspace(String variableName, int[][] matrix)
|
|
698 |
throws RWorkspaceException {
|
|
699 |
int ncol = 0;
|
|
700 |
if (matrix.length == 0) {
|
|
701 |
ncol = 0;
|
|
702 |
} else {
|
|
703 |
ncol = matrix[0].length;
|
|
704 |
}
|
|
705 |
int nrow = matrix.length;
|
|
706 |
int[] vector = VectorizeArray.vectorizeByInner(matrix);
|
|
707 |
if (!filecommunication) {
|
|
708 |
try {
|
|
709 |
connection.assign(variableName, vector);
|
|
710 |
} catch (REngineException e) {
|
|
711 |
throw new RWorkspaceException(e);
|
|
712 |
}
|
|
713 |
try {
|
|
714 |
connection .voidEval(variableName
|
|
715 |
+ "<- matrix(" + variableName + ", nrow=" + nrow + ", ncol=" + ncol + ", byrow=T)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
|
716 |
} catch (RserveException e) {
|
|
717 |
throw new RWorkspaceException(e);
|
|
718 |
}
|
|
719 |
} else {
|
|
720 |
comm.assign(variableName, vector, nrow, ncol);
|
|
721 |
}
|
|
722 |
Log.finest("MATRIX_ADDED_TO_WORKSPACE" + new Object[] { nrow, ncol, variableName }); //$NON-NLS-1$
|
|
723 |
}
|
|
724 |
|
|
725 |
/**
|
|
726 |
* Adds the vector to workspace.
|
|
727 |
*
|
|
728 |
* @param variableName the variable name
|
|
729 |
* @param vector the vector
|
|
730 |
* @throws RWorkspaceException the r workspace exception
|
|
731 |
*/
|
|
732 |
public void addVectorToWorkspace(String variableName, double[] vector)
|
|
733 |
throws RWorkspaceException {
|
|
734 |
if (filecommunication) {
|
|
735 |
comm.assign(variableName, vector);
|
|
736 |
} else {
|
|
737 |
try {
|
|
738 |
connection.assign(variableName, vector);
|
|
739 |
} catch (REngineException e) {
|
|
740 |
throw new RWorkspaceException(e);
|
|
741 |
}
|
|
742 |
}
|
|
743 |
|
|
744 |
Log.finest("DOUBLE_VECTOR_ADDED_TO_WORKSPACE" + new Object[] { vector.length, variableName }); //$NON-NLS-1$
|
|
745 |
}
|
|
746 |
|
|
747 |
/**
|
|
748 |
* Add a vector into the workspace and link it to a name.
|
|
749 |
*
|
|
750 |
* @param variableName the variable name
|
|
751 |
* @param vector the vector
|
|
752 |
* @throws RWorkspaceException the r workspace exception
|
|
753 |
*/
|
|
754 |
public void addVectorToWorkspace(String variableName, DoubleArrayList vector)
|
|
755 |
throws RWorkspaceException {
|
|
756 |
|
|
757 |
vector.trimToSize();
|
|
758 |
double[] vald = vector.elements();
|
|
759 |
if (filecommunication) {
|
|
760 |
comm.assign(variableName, vald);
|
|
761 |
} else {
|
|
762 |
try {
|
|
763 |
connection.assign(variableName, vald);
|
|
764 |
} catch (REngineException e) {
|
|
765 |
throw new RWorkspaceException(e);
|
|
766 |
}
|
|
767 |
}
|
|
768 |
Log.finest("DOUBLE_VECTOR_ADDED_TO_WORKSPACE" + new Object[] { vald.length, variableName }); //$NON-NLS-1$
|
|
769 |
}
|
|
770 |
|
|
771 |
/**
|
|
772 |
* Adds the vector to workspace.
|
|
773 |
*
|
|
774 |
* @param variableName the variable name
|
|
775 |
* @param vector the vector
|
|
776 |
* @throws RWorkspaceException the r workspace exception
|
|
777 |
*/
|
|
778 |
public void addVectorToWorkspace(String variableName, int[] vector)
|
|
779 |
throws RWorkspaceException {
|
|
780 |
//checkForDuplicateVariable(variableName);
|
|
781 |
if (filecommunication) {
|
|
782 |
comm.assign(variableName, vector);
|
|
783 |
} else {
|
|
784 |
try {
|
|
785 |
connection.assign(variableName, vector);
|
|
786 |
} catch (REngineException e) {
|
|
787 |
throw new RWorkspaceException(e);
|
|
788 |
}
|
|
789 |
}
|
|
790 |
|
|
791 |
Log.finest("INT_VECTOR_ADDED_TO_WORKSPACE" + new Object[] { vector.length, variableName }); //$NON-NLS-1$
|
|
792 |
}
|
|
793 |
|
|
794 |
/**
|
|
795 |
* Adds the vector to workspace.
|
|
796 |
*
|
|
797 |
* @param variableName the variable name
|
|
798 |
* @param vector the vector
|
|
799 |
* @throws RWorkspaceException the r workspace exception
|
|
800 |
*/
|
|
801 |
public void addVectorToWorkspace(String variableName, IntArrayList vector)
|
|
802 |
throws RWorkspaceException {
|
|
803 |
vector.trimToSize();
|
|
804 |
int[] vald = vector.elements();
|
|
805 |
if (filecommunication) {
|
|
806 |
comm.assign(variableName, vald);
|
|
807 |
} else {
|
|
808 |
try {
|
|
809 |
connection.assign(variableName, vald);
|
|
810 |
} catch (REngineException e) {
|
|
811 |
throw new RWorkspaceException(e);
|
|
812 |
}
|
|
813 |
}
|
|
814 |
|
|
815 |
Log.finest("INT_VECTOR_ADDED_TO_WORKSPACE" + new Object[] { vald.length, variableName }); //$NON-NLS-1$
|
|
816 |
}
|
|
817 |
|
|
818 |
// --------------------------------------
|
|
819 |
|
|
820 |
/**
|
|
821 |
* Adds the vector to workspace.
|
|
822 |
*
|
|
823 |
* @param variableName the variable name
|
|
824 |
* @param vector the vector
|
|
825 |
* @throws RWorkspaceException the r workspace exception
|
|
826 |
*/
|
|
827 |
public void addVectorToWorkspace(String variableName, String[] vector)
|
|
828 |
throws RWorkspaceException {
|
|
829 |
|
|
830 |
try {
|
|
831 |
connection.assign(variableName, vector);
|
|
832 |
} catch (REngineException e) {
|
|
833 |
throw new RWorkspaceException(e);
|
|
834 |
}
|
|
835 |
|
|
836 |
Log.finest("CHAR_VECTOR_ADDED_TO_WORKSPACE" + new Object[] { vector.length, variableName }); //$NON-NLS-1$
|
|
837 |
}
|
|
838 |
|
|
839 |
/**
|
|
840 |
* Assign col names to matrix.
|
|
841 |
*
|
|
842 |
* @param matrix the matrix
|
|
843 |
* @param colNames the col names
|
|
844 |
* @throws RWorkspaceException the r workspace exception
|
|
845 |
*/
|
|
846 |
public void assignColNamesToMatrix(String matrix, String colNames)
|
|
847 |
throws RWorkspaceException {
|
|
848 |
voidEval("colnames(" + matrix + ") <- " + colNames); //$NON-NLS-1$ //$NON-NLS-2$
|
|
849 |
}
|
|
850 |
|
|
851 |
/**
|
|
852 |
* Assign row names to matrix.
|
|
853 |
*
|
|
854 |
* @param matrix the matrix
|
|
855 |
* @param rowNames the row names
|
|
856 |
* @throws RWorkspaceException the r workspace exception
|
|
857 |
*/
|
|
858 |
public void assignRowNamesToMatrix(String matrix, String rowNames)
|
|
859 |
throws RWorkspaceException {
|
|
860 |
voidEval("rownames(" + matrix + ") <- " + rowNames); //$NON-NLS-1$ //$NON-NLS-2$
|
|
861 |
}
|
|
862 |
|
|
863 |
/**
|
|
864 |
* Builds the string args.
|
|
865 |
*
|
|
866 |
* @param arguments the arguments
|
|
867 |
* @return the string[]
|
|
868 |
* @throws RWorkspaceException the r workspace exception
|
|
869 |
*/
|
|
870 |
private String[] buildStringArgs(QuantitativeDataStructure[] arguments) throws RWorkspaceException {
|
|
871 |
String[] arguments_as_string = new String[arguments.length];
|
|
872 |
for (int i = 0; i < arguments_as_string.length; i++) {
|
|
873 |
if (arguments[i] == null) { // null may be used for specif ying
|
|
874 |
// "NULL" R object.
|
|
875 |
arguments_as_string[i] = "NULL"; //$NON-NLS-1$
|
|
876 |
} else {
|
|
877 |
if (!containsVariable(arguments[i].getSymbol())) {
|
|
878 |
throw new RWorkspaceException(Messages.REQUESTED_OBJECT
|
|
879 |
+ arguments[i].getSymbol()
|
|
880 |
+ Messages.NOT_FOUND_IN_R);
|
|
881 |
}
|
|
882 |
arguments_as_string[i] = arguments[i].getSymbol();
|
|
883 |
}
|
|
884 |
}
|
|
885 |
return arguments_as_string;
|
|
886 |
}
|
|
887 |
|
|
888 |
/**
|
|
889 |
* Call a function, with an array of {@link QuantitativeDataStructure}.
|
|
890 |
*
|
|
891 |
* The <code>QuantitativeDataStructure</code> array is turned into a list of
|
|
892 |
* non-named parameters, and {@link #callFunction(String, String[])} is
|
|
893 |
* called.
|
|
894 |
*
|
|
895 |
* If one of the <code>QuantitativeDataStructure</code> is <code>null</code>
|
|
896 |
* , then the string "NULL" is used (= R null).
|
|
897 |
*
|
|
898 |
* @param functionName the function name
|
|
899 |
* @param arguments of the function, as non-named parameters.
|
|
900 |
* @return a REXP object
|
|
901 |
* @throws RWorkspaceException the r workspace exception
|
|
902 |
*/
|
|
903 |
public REXP callFunction(String functionName, QuantitativeDataStructure[] arguments) throws RWorkspaceException {
|
|
904 |
String[] arguments_as_string = buildStringArgs(arguments);
|
|
905 |
return callFunction(functionName, arguments_as_string);
|
|
906 |
}
|
|
907 |
|
|
908 |
/**
|
|
909 |
* Call a function, with an array of {@link QuantitativeDataStructure}.
|
|
910 |
*
|
|
911 |
* The <code>QuantitativeDataStructure</code> array is turned into a list of
|
|
912 |
* non-named parameters, and {@link #callFunction(String, String[])} is
|
|
913 |
* called.
|
|
914 |
*
|
|
915 |
* If one of the <code>QuantitativeDataStructure</code> is <code>null</code>
|
|
916 |
* , then the string "NULL" is used (= R null).
|
|
917 |
*
|
|
918 |
* @param functionName the function name
|
|
919 |
* @param arguments of the function, as non-named parameters.
|
|
920 |
* @param symbol the symbol
|
|
921 |
* @return a REXP object
|
|
922 |
* @throws RWorkspaceException the r workspace exception
|
|
923 |
*/
|
|
924 |
public REXP callFunction(String functionName, QuantitativeDataStructure[] arguments, String symbol) throws RWorkspaceException {
|
|
925 |
String[] arguments_as_string = buildStringArgs(arguments);
|
|
926 |
return callFunctionAndAffect(functionName, arguments_as_string, symbol);
|
|
927 |
}
|
|
928 |
|
|
929 |
/**
|
|
930 |
* See {@link #callFunction(String, String[])}.
|
|
931 |
*
|
|
932 |
* @param functionName the function name
|
|
933 |
* @param argument the argument
|
|
934 |
* @return the rEXP
|
|
935 |
* @throws RWorkspaceException the r workspace exception
|
|
936 |
*/
|
|
937 |
public REXP callFunction(String functionName, String argument)
|
|
938 |
throws RWorkspaceException {
|
|
939 |
return callFunction(functionName, new String[] { argument });
|
|
940 |
}
|
|
941 |
|
|
942 |
/**
|
|
943 |
* Call a function, with a list of non-named parameters, and return the
|
|
944 |
* result as a <code>REXP</code> object.
|
|
945 |
*
|
|
946 |
* @param functionName the function name
|
|
947 |
* @param arguments of the function, as non-named parameters.
|
|
948 |
* @return a REXP object
|
|
949 |
* @throws RWorkspaceException the r workspace exception
|
|
950 |
*/
|
|
951 |
public REXP callFunction(String functionName, String[] arguments)
|
|
952 |
throws RWorkspaceException {
|
|
953 |
//return eval(makeFunctionCall(functionName, arguments));
|
|
954 |
return callFunctionAndAffect(functionName, arguments, "txmresult"); //$NON-NLS-1$
|
|
955 |
}
|
|
956 |
|
|
957 |
/**
|
|
958 |
* Same as {@link #callFunction(String, String[])}, but affect the result to
|
|
959 |
* a R name in the R workspace.
|
|
960 |
*
|
|
961 |
* @param functionName the function name
|
|
962 |
* @param arguments the arguments
|
|
963 |
* @param symbol the name to which the result of the function will be affected.
|
|
964 |
* @return the rEXP
|
|
965 |
* @throws RWorkspaceException the r workspace exception
|
|
966 |
*/
|
|
967 |
public REXP callFunctionAndAffect(String functionName, String[] arguments, String symbol) throws RWorkspaceException {
|
|
968 |
if (symbol.length() == 0)//symbol must be well formed
|
|
969 |
symbol = "txmresult"; //$NON-NLS-1$
|
|
970 |
StringBuffer sb = new StringBuffer();
|
|
971 |
sb.append(symbol);
|
|
972 |
sb.append(RConstant.AFFECTATION);
|
|
973 |
sb.append(makeFunctionCall(functionName, arguments));
|
|
974 |
return eval(sb.toString());
|
|
975 |
}
|
|
976 |
|
|
977 |
/**
|
|
978 |
* Check for duplicate variable.
|
|
979 |
*
|
|
980 |
* @param variableName the variable name
|
|
981 |
* @throws RWorkspaceException the r workspace exception
|
|
982 |
*/
|
|
983 |
public void checkForDuplicateVariable(String variableName)
|
|
984 |
throws RWorkspaceException {
|
|
985 |
if (containsVariable(variableName)) {
|
|
986 |
throw new RObjectAlreadyExist(
|
|
987 |
"Duplicate variable name: " + variableName + ". Existing variables: " + Arrays.toString(getExistingVariableName())); //$NON-NLS-1$ //$NON-NLS-2$
|
|
988 |
}
|
|
989 |
}
|
|
990 |
|
|
991 |
|
|
992 |
/**
|
|
993 |
* Remove all variables from workspace.
|
|
994 |
*
|
|
995 |
* @throws RWorkspaceException the r workspace exception
|
|
996 |
*/
|
|
997 |
public void clearWorkspace() throws RWorkspaceException {
|
|
998 |
try {
|
|
999 |
connection.voidEval("rm(list=ls(), inherits=FALSE)"); //$NON-NLS-1$
|
|
1000 |
} catch (RserveException e) {
|
|
1001 |
throw new RWorkspaceException(e);
|
|
1002 |
}
|
|
1003 |
Log.finest("WORKSPACE_PURGED"); //$NON-NLS-1$
|
|
1004 |
}
|
|
1005 |
|
|
1006 |
/**
|
|
1007 |
* connect to Rserve using the defaults address and port 127.0.0.1:6311
|
|
1008 |
*
|
|
1009 |
* @return true if success
|
|
1010 |
* @throws RWorkspaceException the r workspace exception
|
|
1011 |
*/
|
|
1012 |
private boolean connect() throws RWorkspaceException {
|
|
1013 |
return connect("127.0.0.1", 6311); //$NON-NLS-1$
|
|
1014 |
}
|
|
1015 |
|
|
1016 |
/**
|
|
1017 |
* Test if a variable exists in the R workspace. A variable exists in the R
|
|
1018 |
* workspace if the R function <code>exists</code> return true on that
|
|
1019 |
* variable name.
|
|
1020 |
*
|
|
1021 |
* @param variableName the variable supposed to exists in the R workspace.
|
|
1022 |
* @return <code>true</code> if the variable exist, <code>false</code>
|
|
1023 |
* otherwise.
|
|
1024 |
* @throws RWorkspaceException the r workspace exception
|
|
1025 |
*/
|
|
1026 |
public boolean containsVariable(String variableName)
|
|
1027 |
throws RWorkspaceException {
|
|
1028 |
REXP res = null;
|
|
1029 |
try {
|
|
1030 |
res = connection.eval("exists(\"" + variableName + "\")"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
1031 |
} catch (RserveException e) {
|
|
1032 |
throw new RWorkspaceException(e);
|
|
1033 |
}
|
|
1034 |
if (res.isLogical() && ((REXPLogical) res).isTRUE()[0]) {
|
|
1035 |
return true;
|
|
1036 |
}
|
|
1037 |
return false;
|
|
1038 |
}
|
|
1039 |
|
|
1040 |
/**
|
|
1041 |
* Bar delegate call to {@link Rengine#eval(String)}.
|
|
1042 |
*
|
|
1043 |
* It is up to the caller to check if everything goes well (testing if the
|
|
1044 |
* return value is not of type XT_NULL).
|
|
1045 |
*
|
|
1046 |
* @param exp the exp
|
|
1047 |
* @return the rEXP
|
|
1048 |
* @throws RWorkspaceException the r workspace exception
|
|
1049 |
*/
|
|
1050 |
public synchronized REXP eval(String exp) throws RWorkspaceException {
|
|
1051 |
REXP res = null;
|
|
1052 |
try {
|
|
1053 |
res = safeEval(exp);
|
|
1054 |
} catch (RserveException e) {
|
|
1055 |
RWorkspace.printLastSafeEvalExpr();
|
|
1056 |
throw new RWorkspaceException(e);
|
|
1057 |
} catch (REXPMismatchException e) {
|
|
1058 |
throw new RException(exp + "; " + e.getMessage(), e); //$NON-NLS-1$
|
|
1059 |
}
|
|
1060 |
//Log.finest("EVALUATED_EXPRESSION" + exp ); //$NON-NLS-1$
|
|
1061 |
return res;
|
|
1062 |
}
|
|
1063 |
|
|
1064 |
/**
|
|
1065 |
* Eval to double.
|
|
1066 |
*
|
|
1067 |
* @param exp the exp
|
|
1068 |
* @return the double[]
|
|
1069 |
* @throws RWorkspaceException the r workspace exception
|
|
1070 |
*/
|
|
1071 |
public double[] evalToDouble(String exp) throws RWorkspaceException {
|
|
1072 |
double[] res = null;
|
|
1073 |
try {
|
|
1074 |
res = eval(exp).asDoubles();
|
|
1075 |
} catch (REXPMismatchException e) {
|
|
1076 |
throw new RWorkspaceException(e);
|
|
1077 |
}
|
|
1078 |
return res;
|
|
1079 |
}
|
|
1080 |
|
|
1081 |
// public REXP safeVoidEval(String s) throws RserveException, RException,
|
|
1082 |
// REXPMismatchException {
|
|
1083 |
// REXP r = connection.voidEval("try({" + s + "}, silent=TRUE)");
|
|
1084 |
// if (r.inherits("try-error")) throw new RException(r.asString());
|
|
1085 |
// return r;
|
|
1086 |
// }
|
|
1087 |
|
|
1088 |
/**
|
|
1089 |
* Eval to double2 d.
|
|
1090 |
*
|
|
1091 |
* @param exp the exp
|
|
1092 |
* @return the double[][]
|
|
1093 |
* @throws RWorkspaceException the r workspace exception
|
|
1094 |
*/
|
|
1095 |
public double[][] evalToDouble2D(String exp) throws RWorkspaceException {
|
|
1096 |
double[][] res = null;
|
|
1097 |
try {
|
|
1098 |
res = eval(exp).asDoubleMatrix();
|
|
1099 |
} catch (REXPMismatchException e) {
|
|
1100 |
throw new RWorkspaceException(e);
|
|
1101 |
}
|
|
1102 |
return res;
|
|
1103 |
}
|
|
1104 |
|
|
1105 |
/*
|
|
1106 |
* Eval to int 2d.
|
|
1107 |
*
|
|
1108 |
* @param exp the exp
|
|
1109 |
* @return the double[][]
|
|
1110 |
* @throws RWorkspaceException the r workspace exception
|
|
1111 |
*/
|
|
1112 |
public int[][] evalToInt2D(String exp) throws RWorkspaceException {
|
|
1113 |
int[][] res = null;
|
|
1114 |
try {
|
|
1115 |
int[] dims = eval(Messages.MatrixImpl_2+exp+")").asIntegers(); //$NON-NLS-2$
|
|
1116 |
int ncol = dims[1];
|
|
1117 |
int nrow = dims[0];
|
|
1118 |
int[] tmp = eval("c("+exp+")").asIntegers(); //$NON-NLS-1$ //$NON-NLS-2$
|
|
1119 |
|
|
1120 |
int c = 0;
|
|
1121 |
res = new int[nrow][ncol];
|