]> gerrit.simantics Code Review - simantics/r.git/blob - org.simantics.r.scl/src/org/rosuda/REngine/REXPEnvironment.java
d4d4ab386c2475062b8c12a26f7c08f80a6d7211
[simantics/r.git] / org.simantics.r.scl / src / org / rosuda / REngine / REXPEnvironment.java
1 package org.rosuda.REngine;
2 // environments are like REXPReferences except that they cannot be resolved
3
4 /** REXPEnvironment represents an environment in R. Very much like {@link org.rosuda.REngine.REXPReference} this is a proxy object to the actual object on the R side. It provides methods for accessing the content of the environment. The actual implementation may vary by the back-end used and not all engines support environments. Check {@link org.rosuda.REngine.REngine.supportsEnvironments()} for the given engine. Environments are specific for a given engine, they cannot be passed across engines.
5  */
6 public class REXPEnvironment extends REXP {
7         /** engine associated with this environment */
8         REngine eng;
9         /** transparent handle that can be used by the engine to indentify the environment. It is not used by REngine API itself. */
10         Object handle;
11         
12         /** create a new environemnt reference - this constructor should never be used directly, use {@link REngine.newEnvironment()} instead.
13          *  @param eng engine responsible for this environment
14          *  @param handle handle used by the engine to identify this environment
15          */
16         public REXPEnvironment(REngine eng, Object handle) {
17                 super();
18                 this.eng = eng;
19                 this.handle = handle;
20         }
21         
22         public boolean isEnvironment() { return true; }
23         
24         /** returns the handle used to identify this environemnt in the engine - for internal use by engine implementations only
25          *  @return handle of this environment */
26         public Object getHandle() { return handle; }
27         
28         /** get a value from this environment
29          *  @param name name of the value
30          *  @param resolve if <code>false</code> returns a reference to the object, if <code>false</code> the reference is resolved
31          *  @return value corresponding to the symbol name or possibly <code>null</code> if the value is unbound (the latter is currently engine-specific) */
32         public REXP get(String name, boolean resolve) throws REngineException {
33                 try {
34                         return eng.get(name, this, resolve);
35                 } catch (REXPMismatchException e) { // this should never happen because this is always guaranteed to be REXPEnv
36                         throw(new REngineException(eng, "REXPMismatchException:"+e+" in get()"));
37                 }
38         }
39
40         /** get a value from this environment - equavalent to <code>get(name, true)</code>.
41          *  @param name name of the value
42          *  @return value (see {@link #get(String,boolean)}) */
43         public REXP get(String name) throws REngineException {
44                 return get(name, true);
45         }
46         
47         /** assigns a value to a given symbol name
48          *  @param name symbol name
49          *  @param value value */
50         public void assign(String name, REXP value) throws REngineException, REXPMismatchException {
51                 eng.assign(name, value, this);
52         }
53         
54         /** returns the parent environment or a reference to it
55          *  @param resolve if <code>true</code> returns the environemnt, otherwise a reference. 
56          *  @return parent environemnt (or a reference to it) */
57         public REXP parent(boolean resolve) throws REngineException {
58                 try {
59                         return eng.getParentEnvironment(this, resolve);
60                 } catch (REXPMismatchException e) { // this should never happen because this is always guaranteed to be REXPEnv
61                         throw(new REngineException(eng, "REXPMismatchException:"+e+" in parent()"));
62                 }
63         }
64
65         /** returns the parent environment. This is equivalent to <code>parent(true)</code>.
66          *  @return parent environemnt */
67         public REXPEnvironment parent() throws REngineException {
68                 return (REXPEnvironment) parent(true);
69         }
70 }