org.rosuda.JRclient
Class REXP

java.lang.Object
  extended byorg.rosuda.JRclient.REXP

public class REXP
extends java.lang.Object

representation of R-eXpressions in Java

Version:
$Id: REXP.java,v 1.17 2004/08/01 20:31:52 urbaneks Exp $

Field Summary
static int XT_ARRAY_BOOL
          xpression type: RBool[]
static int XT_ARRAY_BOOL_UA
          internal use only!
static int XT_ARRAY_DOUBLE
          xpression type: double[]
static int XT_ARRAY_INT
          xpression type: int[]
static int XT_ARRAY_STR
          xpression type: String[] (currently not used, Vector is used instead)
static int XT_BOOL
          xpression type: RBool
static int XT_CLOS
          xpression type: closure (there is no java class for that type (yet?).
static int XT_DOUBLE
          xpression type: double
static int XT_FACTOR
          xpression type: RFactor; this XT is internally generated (ergo is does not come from Rsrv.h) to support RFactor class which is built from XT_ARRAY_INT
static int XT_INT
          xpression type: integer
static int XT_LANG
          xpression type: language construct (currently content is same as list)
static int XT_LIST
          xpression type: RList
static int XT_NULL
          xpression type: NULL
static int XT_STR
          xpression type: String
static int XT_SYM
          xpression type: symbol (content is symbol name: String)
static int XT_UNKNOWN
          xpression type: unknown; no assumptions can be made about the content
static int XT_VECTOR
          xpression type: Vector
 
Constructor Summary
REXP()
          construct a new, empty (NULL) expression w/o attribute
REXP(double[] val)
          construct a new xpression of type XT_ARRAY_DOUBLE and content val
REXP(int[] val)
          construct a new xpression of type XT_ARRAY_INT and content val
REXP(int t, java.lang.Object o)
          construct a new xpression of type t and content o, but no attribute
REXP(int t, java.lang.Object o, REXP at)
          construct a new xpression of type t, content o and attribute at
REXP(java.lang.String[] val)
          construct a new xpression of type XT_ARRAY_INT and content val
 
Method Summary
 RBool asBool()
          get content of the REXP as RBool (if it is one)
 double asDouble()
          get content of the REXP as double (if it is one)
 double[] asDoubleArray()
          get content of the REXP as an array of doubles.
 double[][] asDoubleMatrix()
          returns the content of the REXP as a matrix of doubles (2D-array: m[rows][cols]).
 RFactor asFactor()
          get content of the REXP as RFactor (if it is one)
 int asInt()
          get content of the REXP as int (if it is one)
 int[] asIntArray()
          get content of the REXP as an array of integers.
 RList asList()
          get content of the REXP as RList (if it is one)
 double[][] asMatrix()
          this is just an alias for asDoubleMatrix().
 java.lang.String asString()
          get content of the REXP as string (if it is one)
 java.util.Vector asVector()
          get content of the REXP as Vector (if it is one)
 REXP getAttribute()
          get attribute of the REXP.
 int getBinaryLength()
          Calculates the length of the binary representation of the REXP including all headers.
 int getBinaryRepresentation(byte[] buf, int off)
          Stores the REXP in its binary (ready-to-send) representation including header into a buffer and returns the index of the byte behind the REXP.
 java.lang.Object getContent()
          get raw content.
 int getType()
          get xpression type (see XT_.. constants) of the content.
static int parseREXP(REXP x, byte[] buf, int o)
          parses byte buffer for binary representation of xpressions - read one xpression slot (descends recursively for aggregated xpressions such as lists, vectors etc.)
 java.lang.String toString()
          displayable contents of the expression.
static java.lang.String xtName(int xt)
          returns human-readable name of the xpression type as string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

XT_NULL

public static final int XT_NULL
xpression type: NULL

See Also:
Constant Field Values

XT_INT

public static final int XT_INT
xpression type: integer

See Also:
Constant Field Values

XT_DOUBLE

public static final int XT_DOUBLE
xpression type: double

See Also:
Constant Field Values

XT_STR

public static final int XT_STR
xpression type: String

See Also:
Constant Field Values

XT_LANG

public static final int XT_LANG
xpression type: language construct (currently content is same as list)

See Also:
Constant Field Values

XT_SYM

public static final int XT_SYM
xpression type: symbol (content is symbol name: String)

See Also:
Constant Field Values

XT_BOOL

public static final int XT_BOOL
xpression type: RBool

See Also:
Constant Field Values

XT_VECTOR

public static final int XT_VECTOR
xpression type: Vector

See Also:
Constant Field Values

XT_LIST

public static final int XT_LIST
xpression type: RList

See Also:
Constant Field Values

XT_CLOS

public static final int XT_CLOS
xpression type: closure (there is no java class for that type (yet?). currently the body of the closure is stored in the content part of the REXP. Please note that this may change in the future!)

See Also:
Constant Field Values

XT_ARRAY_INT

public static final int XT_ARRAY_INT
xpression type: int[]

See Also:
Constant Field Values

XT_ARRAY_DOUBLE

public static final int XT_ARRAY_DOUBLE
xpression type: double[]

See Also:
Constant Field Values

XT_ARRAY_STR

public static final int XT_ARRAY_STR
xpression type: String[] (currently not used, Vector is used instead)

See Also:
Constant Field Values

XT_ARRAY_BOOL_UA

public static final int XT_ARRAY_BOOL_UA
internal use only! this constant should never appear in a REXP

See Also:
Constant Field Values

XT_ARRAY_BOOL

public static final int XT_ARRAY_BOOL
xpression type: RBool[]

See Also:
Constant Field Values

XT_UNKNOWN

public static final int XT_UNKNOWN
xpression type: unknown; no assumptions can be made about the content

See Also:
Constant Field Values

XT_FACTOR

public static final int XT_FACTOR
xpression type: RFactor; this XT is internally generated (ergo is does not come from Rsrv.h) to support RFactor class which is built from XT_ARRAY_INT

See Also:
Constant Field Values
Constructor Detail

REXP

public REXP()
construct a new, empty (NULL) expression w/o attribute


REXP

public REXP(int t,
            java.lang.Object o)
construct a new xpression of type t and content o, but no attribute

Parameters:
t - xpression type (XT_...)
o - content

REXP

public REXP(int t,
            java.lang.Object o,
            REXP at)
construct a new xpression of type t, content o and attribute at

Parameters:
t - xpression type
o - content
at - attribute

REXP

public REXP(double[] val)
construct a new xpression of type XT_ARRAY_DOUBLE and content val

Parameters:
val - array of doubles to store in the REXP

REXP

public REXP(int[] val)
construct a new xpression of type XT_ARRAY_INT and content val

Parameters:
val - array of integers to store in the REXP

REXP

public REXP(java.lang.String[] val)
construct a new xpression of type XT_ARRAY_INT and content val

Parameters:
val - array of integers to store in the REXP
Method Detail

getAttribute

public REXP getAttribute()
get attribute of the REXP. In R every object can have attached attribute xpression. Some more complex structures such as classes are built that way.

Returns:
attribute xpression or null if there is none associated

getContent

public java.lang.Object getContent()
get raw content. Use as... methods to retrieve contents of known type.

Returns:
content of the REXP

getType

public int getType()
get xpression type (see XT_.. constants) of the content. It defines the type of the content object.

Returns:
xpression type

parseREXP

public static int parseREXP(REXP x,
                            byte[] buf,
                            int o)
parses byte buffer for binary representation of xpressions - read one xpression slot (descends recursively for aggregated xpressions such as lists, vectors etc.)

Parameters:
x - xpression object to store the parsed xpression in
buf - buffer containing the binary representation
o - offset in the buffer to start at
Returns:
position just behind the parsed xpression. Can be use for successive calls to parseREXP(org.rosuda.JRclient.REXP, byte[], int) if more than one expression is stored in the binary array.

getBinaryLength

public int getBinaryLength()
Calculates the length of the binary representation of the REXP including all headers. This is the amount of memory necessary to store the REXP via getBinaryRepresentation(byte[], int).

Please note that currently only XT_[ARRAY_]INT, XT_[ARRAY_]DOUBLE and XT_[ARRAY_]STR are supported! All other types will return 4 which is the size of the header.

Returns:
length of the REXP including headers (4 or 8 bytes)

getBinaryRepresentation

public int getBinaryRepresentation(byte[] buf,
                                   int off)
Stores the REXP in its binary (ready-to-send) representation including header into a buffer and returns the index of the byte behind the REXP.

Please note that currently only XT_[ARRAY_]INT, XT_[ARRAY_]DOUBLE and XT_[ARRAY_]STR are supported! All other types will be stored as SEXP of the length 0 without any contents.

Parameters:
buf - buffer to store the REXP binary into
off - offset of the first byte where to store the REXP
Returns:
the offset of the first byte behind the stored REXP

xtName

public static java.lang.String xtName(int xt)
returns human-readable name of the xpression type as string. Arrays are denoted by a trailing asterisk (*).

Parameters:
xt - xpression type
Returns:
name of the xpression type

asString

public java.lang.String asString()
get content of the REXP as string (if it is one)

Returns:
string content or null if the REXP is no string

asInt

public int asInt()
get content of the REXP as int (if it is one)

Returns:
int content or 0 if the REXP is no integer

asDouble

public double asDouble()
get content of the REXP as double (if it is one)

Returns:
double content or 0.0 if the REXP is no double

asVector

public java.util.Vector asVector()
get content of the REXP as Vector (if it is one)

Returns:
Vector content or null if the REXP is no Vector

asFactor

public RFactor asFactor()
get content of the REXP as RFactor (if it is one)

Returns:
RFactor content or null if the REXP is no factor

asList

public RList asList()
get content of the REXP as RList (if it is one)

Returns:
RList content or null if the REXP is no list

asBool

public RBool asBool()
get content of the REXP as RBool (if it is one)

Returns:
RBool content or null if the REXP is no logical value

asDoubleArray

public double[] asDoubleArray()
get content of the REXP as an array of doubles. Array of integers, single double and single integer are automatically converted into such an array if necessary.

Returns:
double[] content or null if the REXP is not a array of doubles or integers

asIntArray

public int[] asIntArray()
get content of the REXP as an array of integers. Unlike asDoubleArray() NO automatic conversion is done if the content is not an array of the correct type, because there is no canonical representation of doubles as integers. A single integer is returned as an array of the length 1.

Returns:
double[] content or null if the REXP is not a array of integers

asDoubleMatrix

public double[][] asDoubleMatrix()
returns the content of the REXP as a matrix of doubles (2D-array: m[rows][cols]). This is the same form as used by popular math packages for Java, such as JAMA. This means that following leads to desired results:
Matrix m=new Matrix(c.eval("matrix(c(1,2,3,4,5,6),2,3)").asDoubleMatrix());

Returns:
2D array of doubles in the form double[rows][cols] or null if the contents is no 2-dimensional matrix of doubles

asMatrix

public double[][] asMatrix()
this is just an alias for asDoubleMatrix().


toString

public java.lang.String toString()
displayable contents of the expression. The expression is traversed recursively if aggregation types are used (Vector, List, etc.)

Returns:
String descriptive representation of the xpression