import "Serialization"\r
include "R/RExp"\r
\r
+"""The effect <R> for a routine means that it needs to be dispatched to an R session using a suitable\r
+R execution routine. (syncExec, asyncExec, withConfiguration.)"""\r
effect R \r
"r" \r
"org.rosuda.REngine.Rserve.RConnection"\r
\r
importJava "org.simantics.r.scl.RSessionConfiguration" where\r
+ """SessionConfiguration is a configuration element for the Session.\r
+ \r
+ The fields of the type are: SessionConfiguration <host name> <port number> <user name> <password>"""\r
data SessionConfiguration = \r
@FieldNames [host, port, username, password]\r
SessionConfiguration String Integer String String\r
\r
importJava "org.simantics.r.scl.RSession" where\r
+ "Session represents an open R session with an Rserve instance."\r
data Session\r
+ "Execute a given R operation synchronously. sychExec <session> <r operation>." \r
syncExec :: Session -> (<R> a) -> <Proc> a\r
+ "Execute a given R operation asynchronously. asychExec <session> <r operation>." \r
asyncExec :: Session -> (<R> a) -> <Proc> ()\r
+ "Get the session id of an open R session." \r
@JavaName getId\r
sessionIdOf :: Session -> String\r
+ "Close the given R session." \r
@JavaName close\r
closeSession :: Session -> <Proc> () \r
+ "Refresh the variables of a given R session." \r
@JavaName refreshVariablesSync\r
refreshVariables :: Session -> <Proc> ()\r
\r
importJava "org.simantics.r.scl.RSessionManager" where\r
+ "Get a session using a given session id." \r
@JavaName getSession\r
sessionById :: String -> <Proc> Maybe Session\r
+ "Create a new session with a given configuration." \r
createSession :: SessionConfiguration -> <Proc> Session\r
+ "Execute a given R operation using a temporary session with the given configuration." \r
withConfiguration :: SessionConfiguration-> (<R,e> a) -> <Proc,e> a\r
+ "Get a session with the given session id or create a new session, if not found." \r
getOrCreateSession :: SessionConfiguration -> String -> <Proc> Session\r
\r
importJava "org.rosuda.REngine.Rserve.RConnection" where\r
+ "Evaluate an R expression, returning the result as an RExp object." \r
@JavaName eval\r
evalRExp:: String -> <R> RExp\r
+ "Evaluate an R expression and ignore the result." \r
@JavaName voidEval\r
evalR_ :: String -> <R> ()\r
+ "Evaluate an R expression and return the result as a string." \r
@JavaName voidEvalS\r
evalRS_ :: String -> <R> String\r
+ "Assign an RExp value to a variable in R." \r
@JavaName assign\r
assignRExp :: String -> RExp -> <R> ()\r
+ "Create a file on the R server and a return an OutputStream for it." \r
@JavaName createFile\r
createFileR :: String -> <R> OutputStream\r
+ "Open a file on the R server for reading and return a InputStream for it." \r
@JavaName openFile\r
openFileR :: String -> <R> InputStream\r
\r
+"Evaluate an R expression and return the result as an SCL value. May fail, if the value is not compatible with the expected data type."\r
evalR :: RCompatible a => String -> <R> a\r
evalR = fromRExp . evalRExp\r
\r
+"Assign an SCL value to a variable in R." \r
assignR :: RCompatible a => String -> a -> <R> ()\r
assignR name = assignRExp name . toRExp\r
\r
+"Read the contents of a file on the R server into a string."\r
readFileAsStringR :: String -> <R> String\r
readFileAsStringR name = runProc (readAllString (openFileR name))\r
-\r
-test () = do\r
- conf = SessionConfiguration "localhost" 6311 "simupedia" "simupedia"\r
- session = createSession conf\r
- print $ syncExec session (evalR "1+1")\r
- closeSession session
\ No newline at end of file
import "http://www.simantics.org/R-1.0" as ROntology\r
import "http://www.simantics.org/Layer0-1.1" as L0\r
\r
-"""Creates a new session configuration to graph. This function does\r
+"""Creates a new session configuration to graph as a part of the parent resource. This function does\r
not link it to any other resources."""\r
createSessionConfiguration :: Resource -> R.SessionConfiguration -> <WriteGraph> Resource\r
createSessionConfiguration parent (R.SessionConfiguration host port username password) = do \r
claimRelatedValue r ROntology.SessionConfiguration.password password\r
r\r
\r
+"""Add an R script into the session configuration resource. The script is executed when a new session is opened with the createSession function."""\r
addScript :: Resource -> String -> <WriteGraph> Resource\r
addScript sessionConfiguration scriptText = do \r
r = newResource ()\r
claimRelatedValue r ROntology.Script.text scriptText\r
r\r
\r
+"""Read session configuration from the database."""\r
readSessionConfiguration :: Resource -> <ReadGraph> R.SessionConfiguration\r
readSessionConfiguration r = R.SessionConfiguration\r
(relatedValue r ROntology.SessionConfiguration.host)\r
(relatedValue r ROntology.SessionConfiguration.username)\r
(relatedValue r ROntology.SessionConfiguration.password)\r
\r
+"""Write an R session into the database."""\r
writeSession :: R.Session -> <WriteGraph> Resource\r
writeSession session = do \r
r = newResource ()\r
claimRelatedValue r L0.HasName $ R.sessionIdOf session\r
r\r
\r
+"""Execute the scripts defined for a session configuration using the given session."""\r
+executeScripts :: Resource -> R.Session -> <ReadGraph,Proc> ()\r
+executeScripts configurationResource session = for (configurationResource # ROntology.SessionConfiguration.hasScript) $ \s -> do\r
+ scriptText = relatedValue s ROntology.Script.text\r
+ R.asyncExec session (R.evalR_ scriptText)\r
+\r
+"""Read an R session from the database. If the session has been closed, a new session is opened with the\r
+same configuration."""\r
readSession :: Resource -> <ReadGraph,Proc> R.Session\r
readSession r = let\r
sessionId = relatedValue r L0.HasName\r
in match R.sessionById sessionId with\r
- Just session -> session\r
- Nothing -> R.getOrCreateSession (readSessionConfiguration $ singleObject r L0.PartOf) sessionId\r
+ Just session -> session\r
+ Nothing -> do\r
+ configurationResource = singleObject r L0.PartOf\r
+ session = R.getOrCreateSession (readSessionConfiguration $ configurationResource) sessionId\r
+ executeScripts configurationResource session\r
+ session\r
\r
-\r
+"""Create a session based on a given session configuration resource."""\r
createSession :: Resource -> <WriteGraph,Proc> Resource\r
createSession configurationResource = do\r
session = R.createSession $ readSessionConfiguration configurationResource\r
- for (configurationResource # ROntology.SessionConfiguration.hasScript) $ \s -> do\r
- scriptText = relatedValue s ROntology.Script.text\r
- R.asyncExec session (R.evalR_ scriptText)\r
+ executeScripts configurationResource session\r
sessionResource = writeSession session\r
claim configurationResource L0.ConsistsOf sessionResource\r
sessionResource\r
\r
+"""Delete a session resource and close the session that it represents."""\r
deleteSession :: Resource -> <WriteGraph,Proc> ()\r
deleteSession r = do\r
session = readSession r\r