001/* 002 * Copyright (c) 2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author$' 006 * '$Date$' 007 * '$Revision$' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package ptolemy.actor.lib.r; 031 032import org.rosuda.JRI.RMainLoopCallbacks; 033import org.rosuda.JRI.Rengine; 034 035/** 036 * A simple buffering console that is used to cache the output from an R session 037 * and then can be used to return the console output as a string. 038 * 039 * @author Matt Jones 040 * @version $Id$ 041 * @since Ptolemy II 11.0 042 */ 043public class RConsole implements RMainLoopCallbacks { 044 /** 045 * Construct the R Console class and initialize the buffer containing the 046 * text. 047 */ 048 public RConsole() { 049 super(); 050 _consoleText = new StringBuffer(); 051 } 052 053 /** Clear the console. */ 054 public void clear() { 055 _consoleText = new StringBuffer(); 056 } 057 058 /** 059 * After an R session has ended, get a String representation of the output 060 * of the R session. 061 * 062 * @return String containing the text of the R session output 063 */ 064 public String getConsoleOutput() { 065 return _consoleText.toString(); 066 } 067 068 /** 069 * Callback that is called when text is available from the R Engine and 070 * should be written to the console. 071 * @param re The R engine 072 * @param text The text to by written. 073 * @param oType Ignored in this method. 074 */ 075 @Override 076 public void rWriteConsole(Rengine re, String text, int oType) { 077 _consoleText.append(text); 078 } 079 080 // 081 // The remaining callback methods are not used, but need to have 082 // implementations to satisfy the interface definition. 083 // 084 /** Print a busy message. 085 * @param re The R engine 086 * @param which Unknown. 087 */ 088 @Override 089 public void rBusy(Rengine re, int which) { 090 System.out.println("rBusy(" + which + ")"); 091 } 092 093 /** Read from the console. 094 * In this class, null is returned. 095 * @param re The R engine 096 * @param prompt The prompt 097 * @param addToHistory unknown 098 * @return the string read from the console 099 */ 100 @Override 101 public String rReadConsole(Rengine re, String prompt, int addToHistory) { 102 return null; 103 } 104 105 /** Show a message. 106 * @param re The R engine 107 * @param message The message 108 */ 109 @Override 110 public void rShowMessage(Rengine re, String message) { 111 System.out.println("rShowMessage \"" + message + "\""); 112 } 113 114 /** Choose a file. 115 * In this class return the empty string. 116 * @param re The R engine 117 * @param newFile unused 118 * @return The file name. 119 */ 120 @Override 121 public String rChooseFile(Rengine re, int newFile) { 122 return ""; 123 } 124 125 /** Flush the console. 126 * In this class, do nothing. 127 * @param re The R engine 128 */ 129 @Override 130 public void rFlushConsole(Rengine re) { 131 } 132 133 /** Load the history. 134 * In this class, do nothing. 135 * @param re The R engine 136 * @param filename The file that contains the history. 137 */ 138 @Override 139 public void rLoadHistory(Rengine re, String filename) { 140 } 141 142 /** Save history. 143 * In this class, do nothing. 144 * @param re The R engine 145 * @param filename the file in which to save the history. 146 */ 147 @Override 148 public void rSaveHistory(Rengine re, String filename) { 149 } 150 151 /** 152 * A buffer which caches the output of the R session standard output. 153 */ 154 private StringBuffer _consoleText = null; 155}