Révision 2515
tmp/org.txm.statsengine.r.core/src/org/txm/statsengine/r/core/RWorkspace.java (revision 2515) | ||
---|---|---|
108 | 108 |
*/ |
109 | 109 |
|
110 | 110 |
public final class RWorkspace { |
111 |
|
|
111 |
|
|
112 | 112 |
/** The comm. */ |
113 | 113 |
private static RFileCommunication comm; |
114 |
|
|
114 |
|
|
115 | 115 |
/** The connection. */ |
116 | 116 |
private static RConnection connection = null; |
117 |
|
|
117 |
|
|
118 | 118 |
/** The eval logs. */ |
119 |
static ArrayList<String> evalLogs = new ArrayList<String>();
|
|
120 |
|
|
119 |
static ArrayList<String> evalLogs = new ArrayList<>(); |
|
120 |
|
|
121 | 121 |
/** The filecommunication. */ |
122 | 122 |
private static boolean filecommunication = false; |
123 |
|
|
123 |
|
|
124 | 124 |
// -------------------------------------- |
125 | 125 |
/** The last safeeval expr. */ |
126 | 126 |
static String lastSafeevalExpr = ""; //$NON-NLS-1$ |
127 |
|
|
127 |
|
|
128 | 128 |
/** The Rserve process. */ |
129 | 129 |
private static Process RserveProcess = null; |
130 |
|
|
130 |
|
|
131 | 131 |
/** The workspace. */ |
132 | 132 |
private static RWorkspace workspace = null; |
133 | 133 |
|
134 | 134 |
/** The error logger. */ |
135 | 135 |
private StreamHog errorLogger; |
136 |
|
|
136 |
|
|
137 | 137 |
/** The input logger. */ |
138 | 138 |
private StreamHog inputLogger; |
139 |
|
|
139 |
|
|
140 | 140 |
/** The loggin. */ |
141 | 141 |
private boolean logging; |
142 |
|
|
142 |
|
|
143 | 143 |
/** The DEFAUL t_ symbol. */ |
144 |
//private String DEFAULT_SYMBOL = "txmresult"; //$NON-NLS-1$ |
|
145 |
|
|
144 |
// private String DEFAULT_SYMBOL = "txmresult"; //$NON-NLS-1$
|
|
145 |
|
|
146 | 146 |
/** |
147 | 147 |
* Folder where to create the R working directory |
148 | 148 |
*/ |
149 | 149 |
protected File userdir = null; |
150 |
|
|
151 | 150 |
|
152 |
|
|
153 | 151 |
/** |
154 | 152 |
* protected on purpose. See {@link #getRWorkspaceInstance()}. |
155 | 153 |
* |
156 | 154 |
* @throws RWorkspaceException the r workspace exception |
157 | 155 |
*/ |
158 |
public RWorkspace(File userDir) throws RWorkspaceException {
|
|
156 |
private RWorkspace(File userDir) throws RWorkspaceException {
|
|
159 | 157 |
this.userdir = userDir; |
160 | 158 |
this.setLog(isLoggingEvalCommandLines()); |
161 | 159 |
} |
162 |
|
|
160 |
|
|
163 | 161 |
/** |
164 | 162 |
* Append to eval log. |
165 | 163 |
* |
166 | 164 |
* @param cmd the cmd |
167 | 165 |
*/ |
168 |
public static void appendtoEvalLog(String cmd) |
|
169 |
{ |
|
166 |
public static void appendtoEvalLog(String cmd) { |
|
170 | 167 |
evalLogs.add(cmd); |
171 | 168 |
} |
172 |
|
|
169 |
|
|
173 | 170 |
/** |
174 | 171 |
* Close the connection. |
175 | 172 |
* |
... | ... | |
183 | 180 |
connection.close(); |
184 | 181 |
} |
185 | 182 |
} |
186 |
|
|
183 |
|
|
187 | 184 |
/** |
188 | 185 |
* Connect to Rserve. |
189 | 186 |
* |
... | ... | |
198 | 195 |
connection = new RConnection(host, port); |
199 | 196 |
connection.setStringEncoding("utf8"); // mandatory ! |
200 | 197 |
return isConnected(); |
201 |
} catch (RserveException x) { |
|
198 |
} |
|
199 |
catch (RserveException x) { |
|
202 | 200 |
throw new RWorkspaceException(x); |
203 | 201 |
} |
204 |
|
|
202 |
|
|
205 | 203 |
} |
206 |
|
|
204 |
|
|
207 | 205 |
/** |
208 | 206 |
* Connect to Rserve. |
209 | 207 |
* |
... | ... | |
215 | 213 |
*/ |
216 | 214 |
public static boolean connect(String host, int port, String user, String password) { |
217 | 215 |
try { |
218 |
Log.fine(TXMCoreMessages.bind(RCoreMessages.info_connectingToRAtWithUser, new Object[] {host, port, user}));
|
|
216 |
Log.fine(TXMCoreMessages.bind(RCoreMessages.info_connectingToRAtWithUser, new Object[] { host, port, user }));
|
|
219 | 217 |
connection = new RConnection(host, port); |
220 | 218 |
connection.login(user, password); |
221 | 219 |
connection.setStringEncoding("utf8"); // mandatory ! |
222 | 220 |
return isConnected(); |
223 |
|
|
224 |
} catch (RserveException x) { |
|
225 |
Log.severe(TXMCoreMessages.bind(RCoreMessages.error_failedToConnectToTheRWorkspace, host, port, user)); |
|
221 |
|
|
226 | 222 |
} |
223 |
catch (RserveException x) { |
|
224 |
Log.severe(TXMCoreMessages.bind(RCoreMessages.error_failedToConnectToTheRWorkspace, host, port, user)); |
|
225 |
} |
|
227 | 226 |
return false; |
228 | 227 |
} |
229 |
|
|
228 |
|
|
230 | 229 |
/** |
231 | 230 |
* Gets the last safe eval expr. |
232 | 231 |
* |
... | ... | |
235 | 234 |
public static String getLastSafeEvalExpr() { |
236 | 235 |
return lastSafeevalExpr; |
237 | 236 |
} |
238 |
|
|
239 |
|
|
237 |
|
|
238 |
|
|
240 | 239 |
/** |
241 | 240 |
* |
242 | 241 |
* @return |
243 | 242 |
*/ |
244 | 243 |
public static String getRandomSymbol() { |
245 |
return "Random" + UUID.randomUUID(); //$NON-NLS-1$; |
|
244 |
return "Random" + UUID.randomUUID(); //$NON-NLS-1$ ;
|
|
246 | 245 |
} |
247 |
|
|
246 |
|
|
248 | 247 |
/** |
249 | 248 |
* Entering the R workspace. |
250 | 249 |
* |
251 |
* @param
|
|
250 |
* @param |
|
252 | 251 |
* @return the r workspace instance |
253 | 252 |
* @throws RWorkspaceException the r workspace exception |
254 | 253 |
*/ |
255 | 254 |
public static final RWorkspace getRWorkspaceInstance() throws RWorkspaceException { |
256 | 255 |
if (workspace == null) { |
257 |
//TODO: this method uses the right directory ? |
|
258 |
//Bundle bundle = FrameworkUtil.getBundle(RWorkspace.class); |
|
259 |
//File userdir = new File(bundle.getLocation()); |
|
256 |
// TODO: this method uses the right directory ?
|
|
257 |
// Bundle bundle = FrameworkUtil.getBundle(RWorkspace.class);
|
|
258 |
// File userdir = new File(bundle.getLocation());
|
|
260 | 259 |
File userDir = new File(Toolbox.getTxmHomePath(), "R"); |
261 |
//System.out.println("BUNDLE LOCATION OF '" + bundle.getSymbolicName() + "': " + userDir); |
|
260 |
// System.out.println("BUNDLE LOCATION OF '" + bundle.getSymbolicName() + "': " + userDir);
|
|
262 | 261 |
workspace = new RWorkspace(userDir); |
263 | 262 |
} |
264 | 263 |
return workspace; |
... | ... | |
266 | 265 |
|
267 | 266 |
/** |
268 | 267 |
* Post initialisation of Rserve : loads libraries. |
269 |
* @deprecated The extensions will load themself the packages they need |
|
268 |
* |
|
269 |
* @deprecated The extensions will load themself the packages they need |
|
270 | 270 |
* @throws RWorkspaceException the r workspace exception |
271 | 271 |
*/ |
272 |
@Deprecated |
|
272 | 273 |
private static void init() throws RWorkspaceException { |
273 |
String[] packagesToLoad = { }; //$NON-NLS-1$
|
|
274 |
String[] packagesToLoad = {};
|
|
274 | 275 |
for (int i = 0; i < packagesToLoad.length; i++) { |
275 | 276 |
String name = packagesToLoad[i]; |
276 | 277 |
try { |
277 | 278 |
// System.out.println("load R lib : " + name); |
278 | 279 |
connection.eval(("library(" + name + ")")); //$NON-NLS-1$ //$NON-NLS-2$ |
279 | 280 |
// System.out.println("Done : load R lib : " + name); |
280 |
} catch (RserveException e) { |
|
281 |
} |
|
282 |
catch (RserveException e) { |
|
281 | 283 |
throw new RWorkspaceException(NLS.bind(RCoreMessages.error_failedToLoadLibrary, name, e)); |
282 |
} catch (Exception e) { |
|
284 |
} |
|
285 |
catch (Exception e) { |
|
283 | 286 |
org.txm.utils.logger.Log.printStackTrace(e); |
284 | 287 |
} |
285 | 288 |
} |
286 | 289 |
} |
287 |
|
|
288 |
|
|
290 |
|
|
291 |
|
|
289 | 292 |
// -------------------------------------- |
290 | 293 |
/** |
291 | 294 |
* check R path |
... | ... | |
293 | 296 |
* if not try to launch it. |
294 | 297 |
* |
295 | 298 |
* @param pathToRExecutable the path to r executable |
296 |
* @param options
|
|
297 |
* @param rServeArgs
|
|
299 |
* @param options |
|
300 |
* @param rServeArgs |
|
298 | 301 |
* @return true if Rserve is launched |
299 | 302 |
* @throws RWorkspaceException the r workspace exception |
300 | 303 |
*/ |
... | ... | |
306 | 309 |
|
307 | 310 |
File execFile = new File(pathToRExecutable); |
308 | 311 |
if (!execFile.exists() || pathToRExecutable == null || pathToRExecutable.length() == 0) { |
309 |
//isRServerOk = StartRserve.checkLocalRserve(port, debug, rargs, rServeArgs); |
|
310 |
// if (!isRServerOk) { |
|
311 |
throw new RWorkspaceException(RCoreMessages.error_rservePathNotSet); |
|
312 |
// } |
|
313 |
} else { |
|
312 |
// isRServerOk = StartRserve.checkLocalRserve(port, debug, rargs, rServeArgs); |
|
313 |
// if (!isRServerOk) { |
|
314 |
throw new RWorkspaceException(RCoreMessages.error_rservePathNotSet); |
|
315 |
// } |
|
316 |
} |
|
317 |
else { |
|
314 | 318 |
isRServerOk = StartRserve.launchRserve(pathToRExecutable, port, debug, rargs, rServeArgs); |
315 | 319 |
// System.out.println("ap launchRserve"); |
316 | 320 |
if (!isRServerOk) { |
317 | 321 |
Log.severe(NLS.bind(RCoreMessages.rserveNotStartedUsingTheP0Path, pathToRExecutable)); |
318 |
//isRServerOk = StartRserve.checkLocalRserve(port, debug, rargs, rServeArgs); |
|
322 |
// isRServerOk = StartRserve.checkLocalRserve(port, debug, rargs, rServeArgs);
|
|
319 | 323 |
} |
320 | 324 |
|
321 | 325 |
if (!isRServerOk) { |
322 | 326 |
throw new RWorkspaceException(RCoreMessages.bind(RCoreMessages.error_cantFindRServeInPath, pathToRExecutable)); |
323 | 327 |
} |
324 | 328 |
} |
325 |
|
|
329 |
|
|
326 | 330 |
if (isRServerOk) { |
327 | 331 |
Log.finest("RSERVE_ACTIVATED"); //$NON-NLS-1$ |
328 |
//System.out.println(RCoreMessages.info_connected);
|
|
332 |
// System.out.println(RCoreMessages.info_connected);
|
|
329 | 333 |
RserveProcess = StartRserve.rProcess; |
330 | 334 |
} |
331 | 335 |
return isRServerOk; |
332 | 336 |
} |
333 |
|
|
337 |
|
|
334 | 338 |
/** |
335 | 339 |
* Checks if is connected. |
336 | 340 |
* |
... | ... | |
339 | 343 |
private static boolean isConnected() { |
340 | 344 |
if (!connection.isConnected()) { |
341 | 345 |
return false; |
342 |
} else { |
|
346 |
} |
|
347 |
else { |
|
343 | 348 |
return true; |
344 | 349 |
} |
345 | 350 |
} |
346 |
|
|
351 |
|
|
347 | 352 |
/** |
348 | 353 |
* |
349 | 354 |
* @return The executable path set in the R Preferences |
... | ... | |
360 | 365 |
public static boolean isFileTranfert() { |
361 | 366 |
return filecommunication; |
362 | 367 |
} |
363 |
|
|
364 | 368 |
|
365 | 369 |
|
370 |
|
|
366 | 371 |
/** |
367 | 372 |
* Load eval log. |
368 | 373 |
* |
... | ... | |
372 | 377 |
public static void loadEvalLog(File file) throws IOException { |
373 | 378 |
BufferedReader reader = new BufferedReader(new FileReader(file)); |
374 | 379 |
String cmd = reader.readLine(); |
375 |
while(cmd != null) { |
|
380 |
while (cmd != null) {
|
|
376 | 381 |
evalLogs.add(cmd); |
377 | 382 |
cmd = reader.readLine(); |
378 | 383 |
} |
379 | 384 |
reader.close(); |
380 | 385 |
} |
381 |
|
|
386 |
|
|
382 | 387 |
/** |
383 | 388 |
* Plot. |
384 | 389 |
* |
... | ... | |
390 | 395 |
public File plot(File file, String expr) throws REXPMismatchException, StatException, RserveException { |
391 | 396 |
return plot(file, expr, "svg"); |
392 | 397 |
} |
398 |
|
|
393 | 399 |
/** |
394 | 400 |
* Plot. |
395 | 401 |
* |
... | ... | |
398 | 404 |
* @param device the device (svg, png, jpeg, ...) |
399 | 405 |
* @throws REXPMismatchException the rEXP mismatch exception |
400 | 406 |
* @throws StatException the stat exception |
401 |
* @throws RserveException
|
|
407 |
* @throws RserveException |
|
402 | 408 |
*/ |
403 |
public File plot(File file, String expr, String devicename) throws REXPMismatchException, StatException, RserveException {
|
|
409 |
public File plot(File file, String expr, String devicename) throws REXPMismatchException, StatException, RserveException {
|
|
404 | 410 |
|
405 | 411 |
try { |
406 | 412 |
file.createNewFile(); |
407 |
|
|
413 |
|
|
408 | 414 |
if (!file.canWrite()) { |
409 | 415 |
System.out.println(NLS.bind("{0} cannot be written.", file)); //$NON-NLS-1$ |
410 | 416 |
return null; |
411 | 417 |
} |
412 | 418 |
|
413 | 419 |
String name; |
414 |
if (OSDetector.isFamilyWindows()) { //$NON-NLS-1$ //$NON-NLS-2$
|
|
420 |
if (OSDetector.isFamilyWindows()) { |
|
415 | 421 |
name = file.getCanonicalPath().replaceAll("/", "\\"); |
416 | 422 |
} |
417 | 423 |
else { |
418 | 424 |
name = file.getAbsolutePath(); |
419 | 425 |
} |
420 |
|
|
426 |
|
|
421 | 427 |
if (devicename.equals("devSVG")) safeEval("library(RSvgDevice);"); |
422 |
|
|
428 |
|
|
423 | 429 |
// FIXME : test for window dimensions so the large plots can be entirely displayed. Need to pass width and height to RWorkspace.plot() so |
424 | 430 |
// RChartsEngine will be able to dynamically compute the width from the number of bars in a barplot for example. |
425 |
//REXP xp = eval("try("+devicename+"(\"" + name + "\", width=20, height=10))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
426 |
|
|
427 |
REXP xp = eval("try("+devicename+"(\"" + name + "\"))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
428 |
|
|
431 |
// REXP xp = eval("try("+devicename+"(\"" + name + "\", width=20, height=10))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
432 |
|
|
433 |
REXP xp = eval("try(" + devicename + "(\"" + name + "\"))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
434 |
|
|
429 | 435 |
if (xp.isString() && xp.asString() != null) { // if there's a string |
430 | 436 |
// then we have a |
431 | 437 |
// problem, R sent an |
... | ... | |
437 | 443 |
// if (w.asString()!=null) System.out.println(w.asString()); |
438 | 444 |
throw new StatException(w.asString()); |
439 | 445 |
} |
440 |
|
|
446 |
|
|
441 | 447 |
// ok, so the device should be fine - let's plot |
442 | 448 |
safeEval(expr); |
443 | 449 |
safeEval("dev.off()"); //$NON-NLS-1$ |
... | ... | |
457 | 463 |
public static void printLastSafeEvalExpr() { |
458 | 464 |
Log.fine(RCoreMessages.bind(RCoreMessages.info_lastSafeEvalExpression, lastSafeevalExpr)); |
459 | 465 |
} |
460 |
|
|
466 |
|
|
461 | 467 |
/** |
462 | 468 |
* Save eval log. |
463 | 469 |
* |
... | ... | |
467 | 473 |
public static void saveEvalLog(File file) throws IOException { |
468 | 474 |
FileWriter writer = new FileWriter(file); |
469 | 475 |
for (String cmd : evalLogs) |
470 |
writer.write(cmd+"\n"); //$NON-NLS-1$
|
|
476 |
writer.write(cmd + "\n"); //$NON-NLS-1$
|
|
471 | 477 |
writer.flush(); |
472 | 478 |
writer.close(); |
473 | 479 |
} |
474 |
|
|
480 |
|
|
475 | 481 |
/** |
476 | 482 |
* if state is true, the matrix will be send to R with a File. |
477 | 483 |
* |
... | ... | |
483 | 489 |
if (filecommunication) { |
484 | 490 |
try { |
485 | 491 |
comm = new RFileCommunication(); |
486 |
} catch (IOException e) { |
|
492 |
} |
|
493 |
catch (IOException e) { |
|
487 | 494 |
filecommunication = false; |
488 | 495 |
Log.severe(RCoreMessages.bind(RCoreMessages.error_failedToInitializeFileTransfert, Log.toString(e))); |
489 | 496 |
return false; |
... | ... | |
491 | 498 |
} |
492 | 499 |
return true; |
493 | 500 |
} |
494 |
|
|
501 |
|
|
495 | 502 |
/** |
496 | 503 |
* Close the connection and destroy the Rserve process. |
497 | 504 |
* |
... | ... | |
502 | 509 |
// If no R object have been created during the lif ecycle of the |
503 | 510 |
// application, |
504 | 511 |
// the connection has never been initialized. |
505 |
|
|
512 |
|
|
506 | 513 |
if (connection != null && connection.isConnected()) { |
507 | 514 |
// connection.close(); // No: invoquing "close()" make |
508 | 515 |
// "shutdown()" to throw a "not connected" exception. |
... | ... | |
513 | 520 |
// ok but using it yield to a |
514 | 521 |
// "Broken pipe" exception. |
515 | 522 |
} |
516 |
} catch (RserveException e) { |
|
523 |
} |
|
524 |
catch (RserveException e) { |
|
517 | 525 |
throw new RWorkspaceException(e); |
518 | 526 |
} |
519 | 527 |
} |
520 |
|
|
528 |
|
|
521 | 529 |
/** |
522 | 530 |
* Start exec. |
523 | 531 |
* |
524 | 532 |
* @param pathToRExecutable the path to r executable |
525 |
* @param string
|
|
526 |
* @param string
|
|
533 |
* @param string |
|
534 |
* @param string |
|
527 | 535 |
* @return true, if successful |
528 | 536 |
*/ |
529 | 537 |
public static boolean startExec(String pathToRExecutable, int port, boolean debug, String rArgs, String rServeArgs) { |
... | ... | |
531 | 539 |
try { |
532 | 540 |
initRserve(pathToRExecutable, port, debug, rArgs, rServeArgs); |
533 | 541 |
return true; |
534 |
} catch (RWorkspaceException e) { |
|
542 |
} |
|
543 |
catch (RWorkspaceException e) { |
|
535 | 544 |
Log.severe(RCoreMessages.error_failedToStartRServe); |
536 | 545 |
org.txm.utils.logger.Log.printStackTrace(e); |
537 | 546 |
} |
538 | 547 |
return false; |
539 | 548 |
} |
540 |
|
|
549 |
|
|
541 | 550 |
/** |
542 | 551 |
* To double. |
543 | 552 |
* |
... | ... | |
548 | 557 |
public static final double[] toDouble(REXP rexp) throws RWorkspaceException { |
549 | 558 |
try { |
550 | 559 |
return rexp.asDoubles(); |
551 |
} catch (Exception e) { |
|
560 |
} |
|
561 |
catch (Exception e) { |
|
552 | 562 |
throw new RWorkspaceException(e); |
553 | 563 |
} |
554 | 564 |
} |
555 |
|
|
556 |
|
|
557 |
|
|
565 |
|
|
566 |
|
|
567 |
|
|
558 | 568 |
/** |
559 | 569 |
* Add a matrix into the workspace and link it to a name. The inner arrays |
560 | 570 |
* of the <code>matrix</code> parameters are the <strong>row</strong> of the |
... | ... | |
562 | 572 |
* |
563 | 573 |
* @param variableName the name to be used in the R workspace. |
564 | 574 |
* @param matrix the data to be bound to the name. In the form |
565 |
* <code>matrix[row][column]</code>, with exactly the same number |
|
566 |
* of column in every row. |
|
575 |
* <code>matrix[row][column]</code>, with exactly the same number
|
|
576 |
* of column in every row.
|
|
567 | 577 |
* @throws RWorkspaceException the r workspace exception |
568 | 578 |
*/ |
569 | 579 |
public void addMatrixToWorkspace(String variableName, double[][] matrix) throws RWorkspaceException { |
570 |
//System.out.println("matrix len: "+matrix.length+" test="+(matrix.length == 0)); |
|
580 |
// System.out.println("matrix len: "+matrix.length+" test="+(matrix.length == 0));
|
|
571 | 581 |
if (matrix.length == 0) { |
572 | 582 |
new RWorkspaceException(RCoreMessages.error_matrixIsEmpty); |
573 | 583 |
} |
... | ... | |
584 | 594 |
* |
585 | 595 |
* @param variableName the name to be used in the R workspace. |
586 | 596 |
* @param matrix the data to be bound to the name. In the form |
587 |
* <code>matrix[row][column]</code>, with exactly the same number |
|
588 |
* of column in every row. |
|
597 |
* <code>matrix[row][column]</code>, with exactly the same number
|
|
598 |
* of column in every row.
|
|
589 | 599 |
* @throws RWorkspaceException the r workspace exception |
590 | 600 |
*/ |
591 | 601 |
public void addMatrixToWorkspace(String variableName, double[][] matrix, int ncol, int nrow) |
592 | 602 |
throws RWorkspaceException { |
593 |
|
|
603 |
|
|
594 | 604 |
double[] vector = VectorizeArray.vectorizeByInner(matrix); |
595 |
|
|
605 |
|
|
596 | 606 |
if (!filecommunication) { |
597 | 607 |
try { |
598 | 608 |
connection.assign(variableName, vector); |
599 |
} catch (REngineException e) { |
|
609 |
} |
|
610 |
catch (REngineException e) { |
|
600 | 611 |
throw new RWorkspaceException(e); |
601 | 612 |
} |
602 | 613 |
try { |
603 |
connection .voidEval(variableName
|
|
614 |
connection.voidEval(variableName |
|
604 | 615 |
+ "<- matrix(" + variableName + ", nrow=" + nrow + ", ncol=" + ncol + ", byrow=T)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
605 |
} catch (RserveException e) { |
|
616 |
} |
|
617 |
catch (RserveException e) { |
|
606 | 618 |
throw new RWorkspaceException(e); |
607 | 619 |
} |
608 |
} else { |
|
620 |
} |
|
621 |
else { |
|
609 | 622 |
comm.assign(variableName, vector, nrow, ncol); |
610 | 623 |
} |
611 | 624 |
Log.finest("MATRIX_ADDED_TO_WORKSPACE" + new Object[] { nrow, ncol, variableName }); //$NON-NLS-1$ |
612 | 625 |
} |
613 |
|
|
626 |
|
|
614 | 627 |
/** |
615 | 628 |
* Add a matrix into the workspace and link it to a name. Intended to be |
616 | 629 |
* efficient for sparse matrix. |
... | ... | |
621 | 634 |
*/ |
622 | 635 |
public void addMatrixToWorkspace(String variableName, DoubleMatrix2D matrix) |
623 | 636 |
throws RWorkspaceException { |
624 |
|
|
637 |
|
|
625 | 638 |
double[][] data = RColt.doubleMatrix2D2DoubleDoubleArray(matrix); |
626 | 639 |
addMatrixToWorkspace(variableName, data, matrix.columns(), matrix.rows()); |
627 |
|
|
640 |
|
|
628 | 641 |
// // IntArrayList x = new IntArrayList(); |
629 | 642 |
// // IntArrayList y = new IntArrayList(); |
630 | 643 |
// // DoubleArrayList val = new DoubleArrayList(); |
... | ... | |
647 | 660 |
// // System.out.println(x); |
648 | 661 |
// // System.out.println(y); |
649 | 662 |
// // System.out.println(val); |
650 |
//
|
|
663 |
// |
|
651 | 664 |
// int nrow = matrix.rows(); |
652 | 665 |
// int ncol = matrix.columns(); |
653 | 666 |
// // System.out.println(matrix); |
... | ... | |
677 | 690 |
// y.getQuick(j) + "] <- " + val.getQuick(i * 5 + * j)); |
678 | 691 |
// // } |
679 | 692 |
// // } |
680 |
//
|
|
693 |
// |
|
681 | 694 |
// } catch (RserveException e) { |
682 | 695 |
// throw new RWorkspaceException(e); |
683 | 696 |
// } |
684 |
//
|
|
697 |
// |
|
685 | 698 |
// removeVariableFromWorkspace(variableName + ".nonzero"); |
686 | 699 |
// removeVariableFromWorkspace(variableName + ".x"); |
687 | 700 |
// removeVariableFromWorkspace(variableName + ".y"); |
688 |
//
|
|
689 |
//
|
|
701 |
// |
|
702 |
// |
|
690 | 703 |
// System.out.println("--------------matrice d'origine----------"); |
691 | 704 |
// System.out.println(matrix); |
692 |
//
|
|
705 |
// |
|
693 | 706 |
// // DoubleFactory2D.dense.make(arg0) |
694 | 707 |
// System.out.println("--------------matrice issue de R----------"); |
695 | 708 |
// DenseDoubleMatrix2D m = null; |
... | ... | |
704 | 717 |
// org.txm.utils.logger.Log.printStackTrace(e); |
705 | 718 |
// } |
706 | 719 |
// System.out.println(m); |
707 |
|
|
720 |
|
|
708 | 721 |
} |
709 |
|
|
710 |
//TODO: is addLexiconToWorkspace used ?
|
|
711 |
// /**
|
|
712 |
// * Adds the lexicon to workspace.
|
|
713 |
// *
|
|
714 |
// * @param variableName the variable name
|
|
715 |
// * @param lexicon the lexicon
|
|
716 |
// * @throws RWorkspaceException the r workspace exception
|
|
717 |
// */
|
|
718 |
// public void addLexiconToWorkspace(String variableName, Lexicon lexicon)
|
|
719 |
// throws RWorkspaceException {
|
|
720 |
// int[] freqs = lexicon.getFreq();
|
|
721 |
// String[] forms = lexicon.getForms();
|
|
722 |
// try {
|
|
723 |
// addVectorToWorkspace(variableName, freqs);
|
|
724 |
// addVectorToWorkspace(variableName + "_names", forms); //$NON-NLS-1$
|
|
725 |
// connection
|
|
726 |
// .voidEval("names(" + variableName + ") <- " + variableName + "_names"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
727 |
// connection.voidEval("rm(" + variableName + "_names" + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
728 |
// } catch (RserveException e) {
|
|
729 |
// throw new RWorkspaceException(e);
|
|
730 |
// }
|
|
731 |
// Log.finest("LEXICON_ADDED" + new Object[] { lexicon.nbrOfType() + variableName }); //$NON-NLS-1$
|
|
732 |
// }
|
|
733 |
|
|
722 |
|
|
723 |
// TODO: is addLexiconToWorkspace used ?
|
|
724 |
// /**
|
|
725 |
// * Adds the lexicon to workspace.
|
|
726 |
// *
|
|
727 |
// * @param variableName the variable name
|
|
728 |
// * @param lexicon the lexicon
|
|
729 |
// * @throws RWorkspaceException the r workspace exception
|
|
730 |
// */
|
|
731 |
// public void addLexiconToWorkspace(String variableName, Lexicon lexicon)
|
|
732 |
// throws RWorkspaceException {
|
|
733 |
// int[] freqs = lexicon.getFreq();
|
|
734 |
// String[] forms = lexicon.getForms();
|
|
735 |
// try {
|
|
736 |
// addVectorToWorkspace(variableName, freqs);
|
|
737 |
// addVectorToWorkspace(variableName + "_names", forms); //$NON-NLS-1$
|
|
738 |
// connection
|
|
739 |
// .voidEval("names(" + variableName + ") <- " + variableName + "_names"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
740 |
// connection.voidEval("rm(" + variableName + "_names" + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
741 |
// } catch (RserveException e) {
|
|
742 |
// throw new RWorkspaceException(e);
|
|
743 |
// }
|
|
744 |
// Log.finest("LEXICON_ADDED" + new Object[] { lexicon.nbrOfType() + variableName }); //$NON-NLS-1$
|
|
745 |
// }
|
|
746 |
|
|
734 | 747 |
/** |
735 | 748 |
* Add a matrix into the workspace and link it to a name. The inner arrays |
736 | 749 |
* of the <code>matrix</code> parameters are the <strong>row</strong> of the |
... | ... | |
738 | 751 |
* |
739 | 752 |
* @param variableName the name to be used in the R workspace. |
740 | 753 |
* @param matrix the data to be bound to the name. In the form |
741 |
* <code>matrix[row][column]</code>, with exactly the same number |
|
742 |
* of column in every row. |
|
754 |
* <code>matrix[row][column]</code>, with exactly the same number
|
|
755 |
* of column in every row.
|
|
743 | 756 |
* @throws RWorkspaceException the r workspace exception |
744 | 757 |
*/ |
745 | 758 |
public void addMatrixToWorkspace(String variableName, int[][] matrix) throws RWorkspaceException { |
... | ... | |
752 | 765 |
|
753 | 766 |
addMatrixToWorkspace(variableName, matrix, ncol, nrow); |
754 | 767 |
} |
768 |
|
|
755 | 769 |
/** |
756 | 770 |
* Add a matrix into the workspace and link it to a name. The inner arrays |
757 | 771 |
* of the <code>matrix</code> parameters are the <strong>row</strong> of the |
... | ... | |
759 | 773 |
* |
760 | 774 |
* @param variableName the name to be used in the R workspace. |
761 | 775 |
* @param matrix the data to be bound to the name. In the form |
762 |
* <code>matrix[row][column]</code>, with exactly the same number |
|
763 |
* of column in every row. |
|
776 |
* <code>matrix[row][column]</code>, with exactly the same number
|
|
777 |
* of column in every row.
|
|
764 | 778 |
* @throws RWorkspaceException the r workspace exception |
765 | 779 |
*/ |
766 | 780 |
public void addMatrixToWorkspace(String variableName, int[][] matrix, int ncol, int nrow) throws RWorkspaceException { |
... | ... | |
769 | 783 |
if (!filecommunication) { |
770 | 784 |
try { |
771 | 785 |
connection.assign(variableName, vector); |
772 |
} catch (REngineException e) { |
|
786 |
} |
|
787 |
catch (REngineException e) { |
|
773 | 788 |
throw new RWorkspaceException(e); |
774 | 789 |
} |
775 | 790 |
try { |
... | ... | |
777 | 792 |
|
778 | 793 |
Log.finest("MATRIX_ADDED_TO_WORKSPACE: " + command); //$NON-NLS-1$ |
779 | 794 |
|
780 |
connection.voidEval(command); |
|
781 |
} catch (RserveException e) { |
|
795 |
connection.voidEval(command); |
|
796 |
} |
|
797 |
catch (RserveException e) { |
|
782 | 798 |
throw new RWorkspaceException(e); |
783 | 799 |
} |
784 | 800 |
} |
... | ... | |
787 | 803 |
} |
788 | 804 |
Log.finest("MATRIX_ADDED_TO_WORKSPACE" + new Object[] { nrow, ncol, variableName }); //$NON-NLS-1$ |
789 | 805 |
} |
790 |
|
|
806 |
|
|
791 | 807 |
/** |
792 | 808 |
* Adds the vector to workspace. |
793 | 809 |
* |
... | ... | |
802 | 818 |
else { |
803 | 819 |
try { |
804 | 820 |
connection.assign(variableName, vector); |
805 |
} catch (REngineException e) { |
|
821 |
} |
|
822 |
catch (REngineException e) { |
|
806 | 823 |
throw new RWorkspaceException(e); |
807 | 824 |
} |
808 | 825 |
} |
809 |
|
|
826 |
|
|
810 | 827 |
Log.finest("DOUBLE_VECTOR_ADDED_TO_WORKSPACE" + new Object[] { vector.length, variableName }); //$NON-NLS-1$ |
811 | 828 |
} |
812 |
|
|
829 |
|
|
813 | 830 |
/** |
814 | 831 |
* Add a vector into the workspace and link it to a name. |
815 | 832 |
* |
... | ... | |
818 | 835 |
* @throws RWorkspaceException the r workspace exception |
819 | 836 |
*/ |
820 | 837 |
public void addVectorToWorkspace(String variableName, DoubleArrayList vector) throws RWorkspaceException { |
821 |
|
|
838 |
|
|
822 | 839 |
vector.trimToSize(); |
823 | 840 |
double[] vald = vector.elements(); |
824 | 841 |
if (filecommunication) { |
... | ... | |
827 | 844 |
else { |
828 | 845 |
try { |
829 | 846 |
connection.assign(variableName, vald); |
830 |
} catch (REngineException e) { |
|
847 |
} |
|
848 |
catch (REngineException e) { |
|
831 | 849 |
throw new RWorkspaceException(e); |
832 | 850 |
} |
833 | 851 |
} |
834 | 852 |
Log.finest("DOUBLE_VECTOR_ADDED_TO_WORKSPACE" + new Object[] { vald.length, variableName }); //$NON-NLS-1$ |
835 | 853 |
} |
836 |
|
|
854 |
|
|
837 | 855 |
/** |
838 | 856 |
* Adds the vector to workspace. |
839 | 857 |
* |
... | ... | |
842 | 860 |
* @throws RWorkspaceException the r workspace exception |
843 | 861 |
*/ |
844 | 862 |
public void addVectorToWorkspace(String variableName, int[] vector) throws RWorkspaceException { |
845 |
//checkForDuplicateVariable(variableName); |
|
863 |
// checkForDuplicateVariable(variableName);
|
|
846 | 864 |
if (filecommunication) { |
847 | 865 |
comm.assign(variableName, vector); |
848 | 866 |
} |
849 | 867 |
else { |
850 | 868 |
try { |
851 | 869 |
connection.assign(variableName, vector); |
852 |
} catch (REngineException e) { |
|
870 |
} |
|
871 |
catch (REngineException e) { |
|
853 | 872 |
throw new RWorkspaceException(e); |
854 | 873 |
} |
855 | 874 |
} |
856 |
|
|
875 |
|
|
857 | 876 |
Log.finest("INT_VECTOR_ADDED_TO_WORKSPACE" + new Object[] { vector.length, variableName }); //$NON-NLS-1$ |
858 | 877 |
} |
859 |
|
|
878 |
|
|
860 | 879 |
/** |
861 | 880 |
* Adds the vector to workspace. |
862 | 881 |
* |
... | ... | |
869 | 888 |
int[] vald = vector.elements(); |
870 | 889 |
if (filecommunication) { |
871 | 890 |
comm.assign(variableName, vald); |
872 |
} else { |
|
891 |
} |
|
892 |
else { |
|
873 | 893 |
try { |
874 | 894 |
connection.assign(variableName, vald); |
875 |
} catch (REngineException e) { |
|
895 |
} |
|
896 |
catch (REngineException e) { |
|
876 | 897 |
throw new RWorkspaceException(e); |
877 | 898 |
} |
878 | 899 |
} |
879 |
|
|
900 |
|
|
880 | 901 |
Log.finest("INT_VECTOR_ADDED_TO_WORKSPACE" + new Object[] { vald.length, variableName }); //$NON-NLS-1$ |
881 | 902 |
} |
882 |
|
|
903 |
|
|
883 | 904 |
// -------------------------------------- |
884 |
|
|
905 |
|
|
885 | 906 |
/** |
886 | 907 |
* Adds the vector to workspace. |
887 | 908 |
* |
... | ... | |
890 | 911 |
* @throws RWorkspaceException the r workspace exception |
891 | 912 |
*/ |
892 | 913 |
public void addVectorToWorkspace(String variableName, String[] vector) throws RWorkspaceException { |
893 |
|
|
914 |
|
|
894 | 915 |
try { |
895 | 916 |
connection.assign(variableName, vector); |
896 |
} catch (REngineException e) { |
|
917 |
} |
|
918 |
catch (REngineException e) { |
|
897 | 919 |
throw new RWorkspaceException(e); |
898 | 920 |
} |
899 |
|
|
921 |
|
|
900 | 922 |
Log.finest("CHAR_VECTOR_ADDED_TO_WORKSPACE" + new Object[] { vector.length, variableName }); //$NON-NLS-1$ |
901 | 923 |
} |
902 |
|
|
924 |
|
|
903 | 925 |
/** |
904 | 926 |
* Assign col names to matrix. |
905 | 927 |
* |
... | ... | |
910 | 932 |
public void assignColNamesToMatrix(String matrix, String colNames) throws RWorkspaceException { |
911 | 933 |
voidEval("colnames(" + matrix + ") <- " + colNames); //$NON-NLS-1$ //$NON-NLS-2$ |
912 | 934 |
} |
913 |
|
|
935 |
|
|
914 | 936 |
/** |
915 | 937 |
* Assign row names to matrix. |
916 | 938 |
* |
... | ... | |
922 | 944 |
throws RWorkspaceException { |
923 | 945 |
voidEval("rownames(" + matrix + ") <- " + rowNames); //$NON-NLS-1$ //$NON-NLS-2$ |
924 | 946 |
} |
925 |
|
|
947 |
|
|
926 | 948 |
/** |
927 | 949 |
* Builds the string args. |
928 | 950 |
* |
... | ... | |
936 | 958 |
if (arguments[i] == null) { // null may be used for specif ying |
937 | 959 |
// "NULL" R object. |
938 | 960 |
arguments_as_string[i] = "NULL"; //$NON-NLS-1$ |
939 |
} else { |
|
961 |
} |
|
962 |
else { |
|
940 | 963 |
if (!containsVariable(arguments[i].getSymbol())) { |
941 |
throw new RWorkspaceException(NLS.bind(RCoreMessages.error_requestedObjectNotFoundInR, arguments[i].getSymbol()));
|
|
964 |
throw new RWorkspaceException(NLS.bind(RCoreMessages.error_requestedObjectNotFoundInR, arguments[i].getSymbol())); |
|
942 | 965 |
} |
943 | 966 |
arguments_as_string[i] = arguments[i].getSymbol(); |
944 | 967 |
} |
945 | 968 |
} |
946 | 969 |
return arguments_as_string; |
947 | 970 |
} |
948 |
|
|
971 |
|
|
949 | 972 |
/** |
950 | 973 |
* Call a function, with an array of {@link QuantitativeDataStructure}. |
951 | 974 |
* |
... | ... | |
965 | 988 |
String[] arguments_as_string = buildStringArgs(arguments); |
966 | 989 |
return callFunction(functionName, arguments_as_string); |
967 | 990 |
} |
968 |
|
|
991 |
|
|
969 | 992 |
/** |
970 | 993 |
* Call a function, with an array of {@link QuantitativeDataStructure}. |
971 | 994 |
* |
... | ... | |
986 | 1009 |
String[] arguments_as_string = buildStringArgs(arguments); |
987 | 1010 |
return callFunctionAndAffect(functionName, arguments_as_string, symbol); |
988 | 1011 |
} |
989 |
|
|
1012 |
|
|
990 | 1013 |
/** |
991 | 1014 |
* See {@link #callFunction(String, String[])}. |
992 | 1015 |
* |
... | ... | |
998 | 1021 |
public REXP callFunction(String functionName, String argument) throws RWorkspaceException { |
999 | 1022 |
return callFunction(functionName, new String[] { argument }); |
1000 | 1023 |
} |
1001 |
|
|
1024 |
|
|
1002 | 1025 |
/** |
1003 | 1026 |
* Call a function, with a list of non-named parameters, and return the |
1004 | 1027 |
* result as a <code>REXP</code> object. |
... | ... | |
1010 | 1033 |
*/ |
1011 | 1034 |
public REXP callFunction(String functionName, String[] arguments) |
1012 | 1035 |
throws RWorkspaceException { |
1013 |
//return eval(makeFunctionCall(functionName, arguments)); |
|
1036 |
// return eval(makeFunctionCall(functionName, arguments));
|
|
1014 | 1037 |
return callFunctionAndAffect(functionName, arguments, "txmresult"); //$NON-NLS-1$ |
1015 | 1038 |
} |
1016 |
|
|
1039 |
|
|
1017 | 1040 |
/** |
1018 | 1041 |
* Same as {@link #callFunction(String, String[])}, but affect the result to |
1019 | 1042 |
* a R name in the R workspace. |
... | ... | |
1025 | 1048 |
* @throws RWorkspaceException the r workspace exception |
1026 | 1049 |
*/ |
1027 | 1050 |
public REXP callFunctionAndAffect(String functionName, String[] arguments, String symbol) throws RWorkspaceException { |
1028 |
//symbol must be well formed |
|
1051 |
// symbol must be well formed
|
|
1029 | 1052 |
if (symbol.length() == 0) { |
1030 | 1053 |
symbol = "txmresult"; //$NON-NLS-1$ |
1031 | 1054 |
} |
... | ... | |
1035 | 1058 |
sb.append(makeFunctionCall(functionName, arguments)); |
1036 | 1059 |
return eval(sb.toString()); |
1037 | 1060 |
} |
1038 |
|
|
1061 |
|
|
1039 | 1062 |
/** |
1040 | 1063 |
* Check for duplicate variable. |
1041 | 1064 |
* |
... | ... | |
1048 | 1071 |
throw new RObjectAlreadyExist("Duplicate variable name: " + variableName + ". Existing variables: " + Arrays.toString(this.getExistingVariableName())); //$NON-NLS-1$ //$NON-NLS-2$ |
1049 | 1072 |
} |
1050 | 1073 |
} |
1051 |
|
|
1052 |
|
|
1074 |
|
|
1075 |
|
|
1053 | 1076 |
/** |
1054 | 1077 |
* Remove all variables from workspace. |
1055 | 1078 |
* |
... | ... | |
1058 | 1081 |
public void clearWorkspace() throws RWorkspaceException { |
1059 | 1082 |
try { |
1060 | 1083 |
connection.voidEval("rm(list=ls(), inherits=FALSE)"); //$NON-NLS-1$ |
1061 |
} catch (RserveException e) { |
|
1084 |
} |
|
1085 |
catch (RserveException e) { |
|
1062 | 1086 |
throw new RWorkspaceException(e); |
1063 | 1087 |
} |
1064 | 1088 |
Log.finest("R WORKSPACE_PURGED"); //$NON-NLS-1$ |
1065 | 1089 |
} |
1066 |
|
|
1090 |
|
|
1067 | 1091 |
/** |
1068 | 1092 |
* connect to Rserve using the defaults address and port 127.0.0.1:6311 |
1069 | 1093 |
* |
... | ... | |
1073 | 1097 |
private boolean connect() throws RWorkspaceException { |
1074 | 1098 |
return connect("127.0.0.1", 6311); //$NON-NLS-1$ |
1075 | 1099 |
} |
1076 |
|
|
1100 |
|
|
1077 | 1101 |
/** |
1078 | 1102 |
* Test if a variable exists in the R workspace. A variable exists in the R |
1079 | 1103 |
* workspace if the R function <code>exists</code> return true on that |
... | ... | |
1081 | 1105 |
* |
1082 | 1106 |
* @param variableName the variable supposed to exists in the R workspace. |
1083 | 1107 |
* @return <code>true</code> if the variable exist, <code>false</code> |
1084 |
* otherwise. |
|
1108 |
* otherwise.
|
|
1085 | 1109 |
* @throws RWorkspaceException the r workspace exception |
1086 | 1110 |
*/ |
1087 | 1111 |
public boolean containsVariable(String variableName) |
... | ... | |
1089 | 1113 |
REXP res = null; |
1090 | 1114 |
try { |
1091 | 1115 |
res = connection.eval("exists(\"" + variableName + "\")"); //$NON-NLS-1$ //$NON-NLS-2$ |
1092 |
} catch (RserveException e) { |
|
1116 |
} |
|
1117 |
catch (RserveException e) { |
|
1093 | 1118 |
throw new RWorkspaceException(e); |
1094 | 1119 |
} |
1095 | 1120 |
if (res.isLogical() && ((REXPLogical) res).isTRUE()[0]) { |
... | ... | |
1097 | 1122 |
} |
1098 | 1123 |
return false; |
1099 | 1124 |
} |
1100 |
|
|
1125 |
|
|
1101 | 1126 |
/** |
1102 | 1127 |
* Bar delegate call to {@link Rengine#eval(String)}. |
1103 | 1128 |
* |
... | ... | |
1112 | 1137 |
REXP res = null; |
1113 | 1138 |
try { |
1114 | 1139 |
res = safeEval(exp); |
1115 |
} catch (RserveException e) { |
|
1140 |
} |
|
1141 |
catch (RserveException e) { |
|
1116 | 1142 |
RWorkspace.printLastSafeEvalExpr(); |
1117 | 1143 |
throw new RWorkspaceException(e); |
1118 |
} catch (Exception e) { |
|
1144 |
} |
|
1145 |
catch (Exception e) { |
|
1119 | 1146 |
throw new RException(exp + "; " + e.getMessage(), e); //$NON-NLS-1$ |
1120 | 1147 |
} |
1121 |
|
|
1148 |
|
|
1122 | 1149 |
return res; |
1123 | 1150 |
} |
1124 |
|
|
1151 |
|
|
1125 | 1152 |
/** |
1126 | 1153 |
* Eval to double. |
1127 | 1154 |
* |
... | ... | |
1133 | 1160 |
double[] res = null; |
1134 | 1161 |
try { |
1135 | 1162 |
res = eval(exp).asDoubles(); |
1136 |
} catch (Exception e) { |
|
1163 |
} |
|
1164 |
catch (Exception e) { |
|
1137 | 1165 |
throw new RWorkspaceException(e); |
1138 | 1166 |
} |
1139 | 1167 |
return res; |
1140 | 1168 |
} |
1141 |
|
|
1169 |
|
|
1142 | 1170 |
// public REXP safeVoidEval(String s) throws RserveException, RException, |
1143 | 1171 |
// REXPMismatchException { |
1144 | 1172 |
// REXP r = connection.voidEval("try({" + s + "}, silent=TRUE)"); |
1145 | 1173 |
// if (r.inherits("try-error")) throw new RException(r.asString()); |
1146 | 1174 |
// return r; |
1147 | 1175 |
// } |
1148 |
|
|
1176 |
|
|
1149 | 1177 |
/** |
1150 | 1178 |
* Eval to double2 d. |
1151 | 1179 |
* |
... | ... | |
1157 | 1185 |
double[][] res = null; |
1158 | 1186 |
try { |
1159 | 1187 |
res = eval(exp).asDoubleMatrix(); |
1160 |
} catch (Exception e) { |
|
1188 |
} |
|
1189 |
catch (Exception e) { |
|
1161 | 1190 |
throw new RWorkspaceException(e); |
1162 | 1191 |
} |
1163 | 1192 |
return res; |
1164 | 1193 |
} |
1165 |
|
|
1194 |
|
|
1166 | 1195 |
/* |
1167 | 1196 |
* Eval to int 2d. |
1168 |
* |
|
1169 | 1197 |
* @param exp the exp |
1170 | 1198 |
* @return the double[][] |
1171 | 1199 |
* @throws RWorkspaceException the r workspace exception |
... | ... | |
1176 | 1204 |
int[] dims = eval("dim(" + exp + ")").asIntegers(); //$NON-NLS-1$ //$NON-NLS-2$ |
1177 | 1205 |
int ncol = dims[1]; |
1178 | 1206 |
int nrow = dims[0]; |
1179 |
int[] tmp = eval("c("+exp+")").asIntegers(); //$NON-NLS-1$ //$NON-NLS-2$
|
|
1180 |
|
|
1207 |
int[] tmp = eval("c(" + exp + ")").asIntegers(); //$NON-NLS-1$ //$NON-NLS-2$
|
|
1208 |
|
|
1181 | 1209 |
int c = 0; |
1182 | 1210 |
res = new int[nrow][ncol]; |
1183 |
for (int i = 0 ; i < ncol ; i++) {
|
|
1184 |
for (int j = 0 ; j < nrow ; j++) {
|
|
1211 |
for (int i = 0; i < ncol; i++) {
|
|
1212 |
for (int j = 0; j < nrow; j++) {
|
|
1185 | 1213 |
res[j][i] = tmp[c++]; |
1186 | 1214 |
} |
1187 | 1215 |
} |
1188 |
} catch (Exception e) { |
|
1216 |
} |
|
1217 |
catch (Exception e) { |
|
1189 | 1218 |
throw new RWorkspaceException(e); |
1190 | 1219 |
} |
1191 | 1220 |
return res; |
1192 | 1221 |
} |
1193 |
|
|
1222 |
|
|
1194 | 1223 |
/** |
1195 | 1224 |
* Eval to string. |
1196 | 1225 |
* |
... | ... | |
1202 | 1231 |
String[] res = null; |
1203 | 1232 |
try { |
1204 | 1233 |
res = eval(exp).asStrings(); |
1205 |
} catch (Exception e) { |
|
1234 |
} |
|
1235 |
catch (Exception e) { |
|
1206 | 1236 |
throw new RWorkspaceException(e); |
1207 | 1237 |
} |
1208 | 1238 |
return res; |
1209 | 1239 |
} |
1210 |
|
|
1240 |
|
|
1211 | 1241 |
/** |
1212 | 1242 |
* Extract item from list by name. |
1213 | 1243 |
* |
... | ... | |
1219 | 1249 |
public REXP extractItemFromListByName(REXP list, String item_name) throws RWorkspaceException { |
1220 | 1250 |
try { |
1221 | 1251 |
return list.asList().at(item_name); |
1222 |
} catch (Exception e) { |
|
1252 |
} |
|
1253 |
catch (Exception e) { |
|
1223 | 1254 |
throw new RWorkspaceException(RCoreMessages.error_errorDuringItemsListExtraction |
1224 |
+ e.getMessage());
|
|
1255 |
+ e.getMessage()); |
|
1225 | 1256 |
} |
1226 | 1257 |
} |
1227 |
|
|
1258 |
|
|
1228 | 1259 |
/** |
1229 | 1260 |
* Extract item from list by name. |
1230 | 1261 |
* |
... | ... | |
1236 | 1267 |
public REXP extractItemFromListByName(String list_symbol, String item_name) throws RWorkspaceException { |
1237 | 1268 |
return eval(list_symbol + RConstant.LIST_EXTRACTOR + item_name); |
1238 | 1269 |
} |
1239 |
|
|
1270 |
|
|
1240 | 1271 |
/** |
1241 | 1272 |
* Gets the connection. |
1242 | 1273 |
* |
... | ... | |
1245 | 1276 |
public RConnection getConnection() { |
1246 | 1277 |
return connection; |
1247 | 1278 |
} |
1248 |
|
|
1279 |
|
|
1249 | 1280 |
public StreamHog getErrorLogger() { |
1250 | 1281 |
return errorLogger; |
1251 | 1282 |
} |
1252 |
|
|
1283 |
|
|
1253 | 1284 |
// -------------------------------------- |
1254 |
|
|
1285 |
|
|
1255 | 1286 |
/** |
1256 | 1287 |
* Gets the existing variable name. |
1257 | 1288 |
* |
... | ... | |
1262 | 1293 |
REXP res; |
1263 | 1294 |
try { |
1264 | 1295 |
res = connection.eval("ls()"); //$NON-NLS-1$ |
1265 |
} catch (RserveException e) { |
|
1296 |
} |
|
1297 |
catch (RserveException e) { |
|
1266 | 1298 |
throw new RWorkspaceException(e); |
1267 | 1299 |
} |
1268 | 1300 |
try { |
1269 | 1301 |
return res.asStrings(); |
1270 |
} catch (Exception e) { |
|
1302 |
} |
|
1303 |
catch (Exception e) { |
|
1271 | 1304 |
throw new RWorkspaceException(e); |
1272 | 1305 |
} |
1273 | 1306 |
} |
1274 |
|
|
1307 |
|
|
1275 | 1308 |
// -------------------------------------- |
1276 |
|
|
1309 |
|
|
1277 | 1310 |
public StreamHog getInputLogger() { |
1278 | 1311 |
return inputLogger; |
1279 | 1312 |
} |
1280 |
|
|
1313 |
|
|
1281 | 1314 |
/** |
1282 | 1315 |
* Gets the last error line. |
1283 | 1316 |
* |
... | ... | |
1286 | 1319 |
public String getLastErrorLine() { |
1287 | 1320 |
return this.errorLogger.lastline; |
1288 | 1321 |
} |
1289 |
|
|
1322 |
|
|
1290 | 1323 |
/** |
1291 | 1324 |
* Gets the last log line. |
1292 | 1325 |
* |
... | ... | |
1295 | 1328 |
public String getLastLogLine() { |
1296 | 1329 |
return this.inputLogger.lastline; |
1297 | 1330 |
} |
1298 |
|
|
1331 |
|
|
1299 | 1332 |
/** |
1300 |
* Initializes the R connection configuration (time out and proxy configuration, if needed). |
|
1333 |
* Initializes the R connection configuration (time out and proxy configuration, if needed). |
|
1334 |
* |
|
1301 | 1335 |
* @param conf |
1302 | 1336 |
* @throws RserveException |
1303 | 1337 |
*/ |
... | ... | |
1314 | 1348 |
this.getConnection().eval("Sys.setenv(http_proxy=\"" + proxyUrl + "\")"); //$NON-NLS-1$ //$NON-NLS-2$ |
1315 | 1349 |
} |
1316 | 1350 |
} |
1317 |
|
|
1351 |
|
|
1318 | 1352 |
/** |
1319 | 1353 |
* Checks if is logging. |
1320 | 1354 |
* |
... | ... | |
1324 | 1358 |
// TODO Auto-generated method stub |
1325 | 1359 |
return logging; |
1326 | 1360 |
} |
1327 |
|
|
1361 |
|
|
1328 | 1362 |
// -------------------------------------- |
1329 |
|
|
1363 |
|
|
1330 | 1364 |
/** |
1331 | 1365 |
* Make function call. |
1332 | 1366 |
* |
... | ... | |
1347 | 1381 |
sb.append(RConstant.CLOSE_PAREN); |
1348 | 1382 |
return sb.toString(); |
1349 | 1383 |
} |
1350 |
|
|
1384 |
|
|
1351 | 1385 |
@SuppressWarnings("rawtypes") |
1352 | 1386 |
/** |
1353 | 1387 |
* Post configuration. |
... | ... | |
1360 | 1394 |
try { |
1361 | 1395 |
// more logs |
1362 | 1396 |
connection.eval("try(options(max.print=5000))"); //$NON-NLS-1$ |
1363 |
|
|
1397 |
|
|
1364 | 1398 |
// set TXM R workspace directory |
1365 |
File RWorkspaceDirectory = new File(userdir, "R"); //$NON-NLS-1$ |
|
1366 |
RWorkspaceDirectory.mkdir(); |
|
1367 |
if (!RWorkspaceDirectory.exists()) { |
|
1368 |
System.out.println("TXM R workspace directory not found: "+RWorkspaceDirectory); |
|
1369 |
} else { |
|
1370 |
Log.fine("Setting TXM R workspace directory: "+RWorkspaceDirectory.getAbsolutePath()); |
|
1371 |
connection.eval("setwd(\""+RWorkspaceDirectory.getAbsolutePath().replace("\\", "/")+"\")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
1399 |
userdir.mkdirs(); |
|
1400 |
if (!userdir.exists()) { |
|
1401 |
System.out.println("TXM R workspace directory not found: " + userdir); |
|
1372 | 1402 |
} |
1373 |
|
|
1374 |
File RLibrariesWorkspaceDirectory = new File(RWorkspaceDirectory, "libraries"); |
|
1403 |
else { |
|
1404 |
Log.fine("Setting TXM R workspace directory: " + userdir.getAbsolutePath()); |
|
1405 |
connection.eval("setwd(\"" + userdir.getAbsolutePath().replace("\\", "/") + "\")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
1406 |
} |
|
1407 |
|
|
1408 |
File RLibrariesWorkspaceDirectory = new File(userdir, "libraries"); |
|
1375 | 1409 |
RLibrariesWorkspaceDirectory.mkdir(); |
1376 | 1410 |
if (!RLibrariesWorkspaceDirectory.exists()) { |
1377 |
System.out.println("TXM R libraries workspace directory not found: "+RLibrariesWorkspaceDirectory); |
|
1378 |
} else { |
|
1379 |
Log.fine("Setting TXM R libraries workspace directory: "+RLibrariesWorkspaceDirectory.getAbsolutePath()); |
|
1380 |
connection.eval(".libPaths(c(\""+RLibrariesWorkspaceDirectory.getAbsolutePath().replace("\\", "/")+"\",.libPaths()))");//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
1411 |
System.out.println("TXM R libraries workspace directory not found: " + RLibrariesWorkspaceDirectory); |
|
1381 | 1412 |
} |
1382 |
|
|
1383 |
if (OSDetector.isFamilyWindows()) { |
|
1413 |
else { |
|
1414 |
Log.fine("Setting TXM R libraries workspace directory: " + RLibrariesWorkspaceDirectory.getAbsolutePath()); |
|
1415 |
connection.eval(".libPaths(c(\"" + RLibrariesWorkspaceDirectory.getAbsolutePath().replace("\\", "/") + "\",.libPaths()))");//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
1416 |
} |
|
1417 |
|
|
1418 |
if (OSDetector.isFamilyWindows()) { |
|
1384 | 1419 |
rPackagesPath = rPackagesPath.replace("\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$ |
1385 | 1420 |
} |
1386 |
|
|
1421 |
|
|
1387 | 1422 |
// set proxy configuration using detected system proxy if exists |
1388 | 1423 |
SystemProxyDetector conf = new SystemProxyDetector(); |
1389 | 1424 |
this.initializeRWorkspaceConnectionConfiguration(conf); |
1390 |
|
|
1425 |
|
|
1391 | 1426 |
// load R libraries: this is now done by extensions/commands themself |
1392 | 1427 |
try { |
1393 | 1428 |
init(); // load libs |
1394 |
} catch (RWorkspaceException e) { |
|
1429 |
} |
|
1430 |
catch (RWorkspaceException e) { |
|
1395 | 1431 |
org.txm.utils.logger.Log.printStackTrace(e); |
1396 | 1432 |
return false; |
1397 | 1433 |
} |
1398 |
|
|
1399 |
//connection.eval("try(.libPaths(\"" + rPackagesPath + "\"))"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
1400 |
|
|
1434 |
|
|
1435 |
// connection.eval("try(.libPaths(\"" + rPackagesPath + "\"))"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
1436 |
|
|
1401 | 1437 |
return true; |
1402 |
} catch (RserveException e) { |
|
1438 |
} |
|
1439 |
catch (RserveException e) { |
|
1403 | 1440 |
org.txm.utils.logger.Log.printStackTrace(e); |
1404 | 1441 |
} |
1405 | 1442 |
return false; |
1406 | 1443 |
} |
1407 |
|
|
1444 |
|
|
1408 | 1445 |
/** |
1409 | 1446 |
* Start logger. |
1410 | 1447 |
* |
... | ... | |
1417 | 1454 |
this.inputLogger = streamHog2; |
1418 | 1455 |
return null; |
1419 | 1456 |
} |
1420 |
|
|
1457 |
|
|
1421 | 1458 |
/** |
1422 | 1459 |
* Remove a variable from the workspace. The content is lost, and the name |
1423 | 1460 |
* refers to nothing. |
... | ... | |
1431 | 1468 |
|
1432 | 1469 |
try { |
1433 | 1470 |
connection.voidEval("rm(" + variableName + ")"); //$NON-NLS-1$ //$NON-NLS-2$ |
1434 |
} catch (RserveException e) { |
|
1471 |
} |
|
1472 |
catch (RserveException e) { |
|
1435 | 1473 |
throw new RWorkspaceException(e); |
1436 | 1474 |
} |
1437 | 1475 |
Log.finest("R VARIABLE_REMOVED: " + new Object[] { variableName }); //$NON-NLS-1$ |
1438 | 1476 |
} |
1439 |
|
|
1477 |
|
|
1440 | 1478 |
/** |
1441 | 1479 |
* Casts the specified REXP to a displayable object. |
1442 | 1480 |
* |
1443 | 1481 |
* @param rexp |
1444 | 1482 |
* @return the casted object : String, int, etc. |
1445 | 1483 |
*/ |
1446 |
public Object getCastedREXP(REXP rexp) {
|
|
1484 |
public Object getCastedREXP(REXP rexp) {
|
|
1447 | 1485 |
try { |
1448 | 1486 |
if (rexp instanceof org.rosuda.REngine.REXPString) { |
1449 | 1487 |
if (rexp.isVector()) { |
... | ... | |
1451 | 1489 |
} |
1452 | 1490 |
else { |
1453 | 1491 |
return rexp.asString(); |
1454 |
}
|
|
1455 |
}
|
|
1492 |
} |
|
1493 |
} |
|
1456 | 1494 |
else if (rexp instanceof org.rosuda.REngine.REXPDouble) { |
1457 | 1495 |
if (rexp.isVector()) { |
1458 | 1496 |
return Arrays.toString(rexp.asDoubles()); |
... | ... | |
1490 | 1528 |
* @throws REXPMismatchException the rEXP mismatch exception |
1491 | 1529 |
*/ |
1492 | 1530 |
public REXP safeEval(String expr) throws RserveException, RException, REXPMismatchException { |
1493 |
if (logging && inputLogger != null ) {
|
|
1531 |
if (logging && inputLogger != null) { |
|
1494 | 1532 |
inputLogger.printMessage(expr); |
1495 | 1533 |
} |
1496 | 1534 |
lastSafeevalExpr = expr; |
1497 | 1535 |
|
1498 | 1536 |
expr = "try({" + expr + "}, silent=FALSE)"; |
1499 | 1537 |
|
1500 |
if(isLoggingEvalCommandLines()) {
|
|
1501 |
//TODO SJ: for debuggin puprose |
|
1502 |
//Log.info("R safeEval: " + expr, 8); //$NON-NLS-1$ |
|
1538 |
if (isLoggingEvalCommandLines()) {
|
|
1539 |
// TODO SJ: for debuggin puprose
|
|
1540 |
// Log.info("R safeEval: " + expr, 8); //$NON-NLS-1$
|
|
1503 | 1541 |
Log.info("R safeEval: " + expr); //$NON-NLS-1$ |
1504 | 1542 |
} |
1505 | 1543 |
|
1506 | 1544 |
REXP r = null; |
1507 | 1545 |
try { |
1508 | 1546 |
r = connection.eval(expr); |
1509 |
if(isLoggingEvalCommandLines()) {
|
|
1547 |
if (isLoggingEvalCommandLines()) {
|
|
1510 | 1548 |
Log.info(" return: " + this.getCastedREXP(r)); //$NON-NLS-1$ |
1511 | 1549 |
} |
1512 | 1550 |
} |
... | ... | |
1514 | 1552 |
// TODO Auto-generated catch block |
1515 | 1553 |
e.printStackTrace(); |
1516 | 1554 |
} |
1517 |
finally {
|
|
1518 |
if (r !=null && r.inherits("try-error")) { |
|
1519 |
throw new RException(expr, r.asString()); //$NON-NLS-1$
|
|
1555 |
finally {
|
|
1556 |
if (r != null && r.inherits("try-error")) {
|
|
1557 |
throw new RException(expr, r.asString()); |
|
1520 | 1558 |
} |
1521 | 1559 |
} |
1522 | 1560 |
return r; |
1523 | 1561 |
} |
1524 |
|
|
1525 | 1562 |
|
1563 |
|
|
1526 | 1564 |
/** |
1527 |
|
|
1565 |
* |
|
1528 | 1566 |
* Bar delegate call to {@link Connection#voidEval(String)}. |
1529 | 1567 |
* |
1530 | 1568 |
* It is up to the caller to check if everything goes well (testing if the |
... | ... | |
1537 | 1575 |
public String userEval(String exp) throws RWorkspaceException { |
1538 | 1576 |
if (exp.trim().length() == 0) |
1539 | 1577 |
return ""; //$NON-NLS-1$ |
1540 |
//if (loggin) |
|
1541 |
//System.out.println("R>"+exp); //$NON-NLS-1$ |
|
1542 |
|
|
1578 |
// if (loggin)
|
|
1579 |
// System.out.println("R>"+exp); //$NON-NLS-1$
|
|
1580 |
|
|
1543 | 1581 |
if (exp.endsWith(";")) //$NON-NLS-1$ |
1544 |
exp = exp.substring(0, exp.length()-1);
|
|
1545 |
|
|
1546 |
// String trybegin = "try("; //$NON-NLS-1$
|
|
1547 |
// String tryend = ")"; //$NON-NLS-1$
|
|
1548 |
|
|
1582 |
exp = exp.substring(0, exp.length() - 1);
|
|
1583 |
|
|
1584 |
// String trybegin = "try("; //$NON-NLS-1$
|
|
1585 |
// String tryend = ")"; //$NON-NLS-1$
|
|
1586 |
|
|
1549 | 1587 |
String out = null; |
1550 | 1588 |
try { |
1551 |
if (isLoggingEvalCommandLines()) {
|
|
1552 |
Log.info("R userEval: " + exp ); //$NON-NLS-1$
|
|
1589 |
if (isLoggingEvalCommandLines()) {
|
|
1590 |
Log.info("R userEval: " + exp); //$NON-NLS-1$ |
|
1553 | 1591 |
} |
1554 |
//out = connection.eval("paste(capture.output(print(" + trybegin + trybegin +"("+ exp+")" + tryend + tryend + ")),collapse=\"\\n\")").asString(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
1555 |
//String cmd = "paste(capture.output(print(" + trybegin + "("+ exp+")" + tryend + ")),collapse=\"\\n\")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
1556 |
REXP ret = connection.eval(exp); |
|
1557 |
//REXP ret = connection.eval(exp); |
|
1558 |
|
|
1592 |
// out = connection.eval("paste(capture.output(print(" + trybegin + trybegin +"("+ exp+")" + tryend + tryend + ")),collapse=\"\\n\")").asString(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
1593 |
// //$NON-NLS-4$ |
|
1594 |
// String cmd = "paste(capture.output(print(" + trybegin + "("+ exp+")" + tryend + ")),collapse=\"\\n\")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
1595 |
REXP ret = connection.eval(exp); |
|
1596 |
// REXP ret = connection.eval(exp); |
|
1597 |
|
|
1559 | 1598 |
if (ret instanceof org.rosuda.REngine.REXPString) { |
1560 | 1599 |
if (ret.isVector()) { |
1561 |
out = "result: "+StringUtils.join(ret.asStrings(), ", "); |
|
1562 |
} else { |
|
1563 |
out = "result: "+ret.asString(); |
|
1564 |
} |
|
1565 |
} else if (ret instanceof org.rosuda.REngine.REXPDouble) { |
|
1600 |
out = "result: " + StringUtils.join(ret.asStrings(), ", "); |
|
1601 |
} |
|
1602 |
else { |
|
1603 |
out = "result: " + ret.asString(); |
|
1604 |
} |
|
1605 |
} |
|
1606 |
else if (ret instanceof org.rosuda.REngine.REXPDouble) { |
|
1566 | 1607 |
if (ret.isVector()) { |
1567 |
out = "result: "+ret.asDouble(); |
|
1568 |
} else { |
|
1569 |
out = "result: "+Arrays.toString(ret.asDoubles()); |
|
1608 |
out = "result: " + ret.asDouble(); |
|
1570 | 1609 |
} |
1571 |
} else if (ret instanceof org.rosuda.REngine.REXPInteger) { |
|
1610 |
else { |
|
1611 |
out = "result: " + Arrays.toString(ret.asDoubles()); |
|
1612 |
} |
|
1613 |
} |
|
1614 |
else if (ret instanceof org.rosuda.REngine.REXPInteger) { |
|
1572 | 1615 |
if (ret.isVector()) { |
1573 |
out = "result: "+ret.asInteger(); |
|
1574 |
} else { |
|
1575 |
out = "result: "+Arrays.toString(ret.asIntegers()); |
|
1616 |
out = "result: " + ret.asInteger(); |
|
1576 | 1617 |
} |
1577 |
} else if (ret instanceof org.rosuda.REngine.REXPNull) { |
|
1578 |
out = "NULL"; |
|
1579 |
} else { |
|
1580 |
out = "result: "+ret; |
|
1618 |
else { |
|
1619 |
out = "result: " + Arrays.toString(ret.asIntegers()); |
|
1620 |
} |
|
1581 | 1621 |
} |
1622 |
else if (ret instanceof org.rosuda.REngine.REXPNull) { |
|
1623 |
out = "NULL"; |
|
1624 |
} |
|
1625 |
else { |
|
1626 |
out = "result: " + ret; |
|
1627 |
} |
|
1582 | 1628 |
|
1583 |
} catch (RserveException e) { |
|
1629 |
} |
|
1630 |
catch (RserveException e) { |
|
1584 | 1631 |
System.out.println(NLS.bind(RCoreMessages.error_evaluationError, e.getMessage(), exp)); |
1585 | 1632 |
Log.printStackTrace(e); |
1586 |
} catch (Exception e) { |
|
1587 |
System.out.println("REXPMismatchException: "+e); |
Formats disponibles : Unified diff