6035 |
29 Oct 20 |
nicklas |
1 |
package net.sf.basedb.reggie.script; |
2863 |
28 Oct 14 |
nicklas |
2 |
|
2991 |
02 Dec 14 |
nicklas |
3 |
import java.util.ArrayList; |
2991 |
02 Dec 14 |
nicklas |
4 |
import java.util.List; |
2863 |
28 Oct 14 |
nicklas |
5 |
|
2863 |
28 Oct 14 |
nicklas |
6 |
import net.sf.basedb.reggie.Reggie; |
2863 |
28 Oct 14 |
nicklas |
7 |
|
2863 |
28 Oct 14 |
nicklas |
8 |
/** |
2863 |
28 Oct 14 |
nicklas |
Helper class to generate and execute a local R script. This class can |
3571 |
30 Oct 15 |
nicklas |
be used to specify which script to use {@link #setScript(String)}, call |
2991 |
02 Dec 14 |
nicklas |
functions in the file {@link #addFunction(String)} with |
3571 |
30 Oct 15 |
nicklas |
a given set of parameters {@link RFunction#setParameter(String, Object)}. |
2863 |
28 Oct 14 |
nicklas |
13 |
|
2863 |
28 Oct 14 |
nicklas |
@author nicklas |
2863 |
28 Oct 14 |
nicklas |
@since 2.18 |
2863 |
28 Oct 14 |
nicklas |
16 |
*/ |
2863 |
28 Oct 14 |
nicklas |
17 |
public class RScriptDefinition |
6036 |
02 Nov 20 |
nicklas |
18 |
extends ScriptDefinition |
2863 |
28 Oct 14 |
nicklas |
19 |
{ |
2863 |
28 Oct 14 |
nicklas |
20 |
|
2863 |
28 Oct 14 |
nicklas |
21 |
private String script; |
3506 |
23 Sep 15 |
nicklas |
22 |
private String scriptDir; |
2991 |
02 Dec 14 |
nicklas |
23 |
private List<RFunction> functions; |
2863 |
28 Oct 14 |
nicklas |
24 |
|
2863 |
28 Oct 14 |
nicklas |
25 |
/** |
2863 |
28 Oct 14 |
nicklas |
Create a new R script builder. |
2863 |
28 Oct 14 |
nicklas |
27 |
*/ |
2863 |
28 Oct 14 |
nicklas |
28 |
public RScriptDefinition() |
2863 |
28 Oct 14 |
nicklas |
29 |
{ |
2991 |
02 Dec 14 |
nicklas |
30 |
functions = new ArrayList<RFunction>(); |
2863 |
28 Oct 14 |
nicklas |
31 |
} |
2863 |
28 Oct 14 |
nicklas |
32 |
|
2863 |
28 Oct 14 |
nicklas |
33 |
/** |
2863 |
28 Oct 14 |
nicklas |
Path to the script file. |
2863 |
28 Oct 14 |
nicklas |
35 |
*/ |
2863 |
28 Oct 14 |
nicklas |
36 |
public void setScript(String script) |
2863 |
28 Oct 14 |
nicklas |
37 |
{ |
3506 |
23 Sep 15 |
nicklas |
38 |
int lastSlash = script.lastIndexOf("/"); |
3506 |
23 Sep 15 |
nicklas |
39 |
this.scriptDir = script.substring(0, lastSlash); |
3506 |
23 Sep 15 |
nicklas |
40 |
this.script = script.substring(lastSlash+1); |
2863 |
28 Oct 14 |
nicklas |
41 |
} |
2863 |
28 Oct 14 |
nicklas |
42 |
|
2863 |
28 Oct 14 |
nicklas |
43 |
/** |
3506 |
23 Sep 15 |
nicklas |
Get the path to the script file. |
3506 |
23 Sep 15 |
nicklas |
45 |
*/ |
3506 |
23 Sep 15 |
nicklas |
46 |
public String getScript() |
3506 |
23 Sep 15 |
nicklas |
47 |
{ |
3506 |
23 Sep 15 |
nicklas |
48 |
return script; |
3506 |
23 Sep 15 |
nicklas |
49 |
} |
3506 |
23 Sep 15 |
nicklas |
50 |
|
3506 |
23 Sep 15 |
nicklas |
51 |
/** |
3506 |
23 Sep 15 |
nicklas |
Get the path to the directory with the script file. |
3506 |
23 Sep 15 |
nicklas |
@since 3.7 |
3506 |
23 Sep 15 |
nicklas |
54 |
*/ |
3506 |
23 Sep 15 |
nicklas |
55 |
public String getScriptDir() |
3506 |
23 Sep 15 |
nicklas |
56 |
{ |
3506 |
23 Sep 15 |
nicklas |
57 |
return scriptDir; |
3506 |
23 Sep 15 |
nicklas |
58 |
} |
3506 |
23 Sep 15 |
nicklas |
59 |
|
3506 |
23 Sep 15 |
nicklas |
60 |
/** |
2863 |
28 Oct 14 |
nicklas |
The name of the function to call. |
2863 |
28 Oct 14 |
nicklas |
62 |
*/ |
2991 |
02 Dec 14 |
nicklas |
63 |
public RFunction addFunction(String function) |
2863 |
28 Oct 14 |
nicklas |
64 |
{ |
2991 |
02 Dec 14 |
nicklas |
65 |
RFunction f = new RFunction(function); |
2991 |
02 Dec 14 |
nicklas |
66 |
functions.add(f); |
2991 |
02 Dec 14 |
nicklas |
67 |
return f; |
2863 |
28 Oct 14 |
nicklas |
68 |
} |
2863 |
28 Oct 14 |
nicklas |
69 |
|
2863 |
28 Oct 14 |
nicklas |
70 |
/** |
2863 |
28 Oct 14 |
nicklas |
Run the R script with the current parameters. It is possible to |
2863 |
28 Oct 14 |
nicklas |
call this method multiple times with different parameters. |
2863 |
28 Oct 14 |
nicklas |
73 |
*/ |
6036 |
02 Nov 20 |
nicklas |
74 |
public ScriptResult run() |
2863 |
28 Oct 14 |
nicklas |
75 |
{ |
6036 |
02 Nov 20 |
nicklas |
76 |
return run(new ScriptResult()); |
2871 |
28 Oct 14 |
nicklas |
77 |
} |
2871 |
28 Oct 14 |
nicklas |
78 |
|
2871 |
28 Oct 14 |
nicklas |
79 |
/** |
2871 |
28 Oct 14 |
nicklas |
Run the R script and put the result in the given result object. |
2871 |
28 Oct 14 |
nicklas |
81 |
*/ |
6036 |
02 Nov 20 |
nicklas |
82 |
protected <R extends ScriptResult> R run(R result) |
2871 |
28 Oct 14 |
nicklas |
83 |
{ |
5962 |
03 Jun 20 |
nicklas |
84 |
return run(result, functions.toArray(new RFunction[functions.size()])); |
5962 |
03 Jun 20 |
nicklas |
85 |
} |
5962 |
03 Jun 20 |
nicklas |
86 |
|
5962 |
03 Jun 20 |
nicklas |
87 |
/** |
5962 |
03 Jun 20 |
nicklas |
Run only the specified functions script and put the result in the given result object. |
5962 |
03 Jun 20 |
nicklas |
89 |
*/ |
6036 |
02 Nov 20 |
nicklas |
90 |
protected <R extends ScriptResult> R run(R result, RFunction... functions) |
5962 |
03 Jun 20 |
nicklas |
91 |
{ |
3581 |
06 Nov 15 |
nicklas |
92 |
String rscript_path = Reggie.getConfig().getRequiredConfig("rscript/path", null); |
3581 |
06 Nov 15 |
nicklas |
93 |
String rscript_locale = Reggie.getConfig().getConfig("rscript/locale", null, null); |
3581 |
06 Nov 15 |
nicklas |
94 |
|
2863 |
28 Oct 14 |
nicklas |
// Generate the command to execute: |
3581 |
06 Nov 15 |
nicklas |
// Sys.setlocale(locale='<locale>'); source('<script-file>'); <function>(<parameters>); |
2863 |
28 Oct 14 |
nicklas |
97 |
StringBuilder rCmd = new StringBuilder(); |
3581 |
06 Nov 15 |
nicklas |
98 |
if (rscript_locale != null) |
3581 |
06 Nov 15 |
nicklas |
99 |
{ |
3581 |
06 Nov 15 |
nicklas |
100 |
rCmd.append("Sys.setlocale(locale='").append(rscript_locale).append("');"); |
3581 |
06 Nov 15 |
nicklas |
101 |
} |
3541 |
14 Oct 15 |
nicklas |
102 |
rCmd.append("source('").append(scriptDir).append("/").append(script).append("', encoding='UTF-8');"); |
3506 |
23 Sep 15 |
nicklas |
103 |
result.setScript(script, scriptDir); |
2991 |
02 Dec 14 |
nicklas |
104 |
for (RFunction f : functions) |
2863 |
28 Oct 14 |
nicklas |
105 |
{ |
5962 |
03 Jun 20 |
nicklas |
106 |
if (f != null) rCmd.append(f); |
2863 |
28 Oct 14 |
nicklas |
107 |
} |
2863 |
28 Oct 14 |
nicklas |
108 |
|
6036 |
02 Nov 20 |
nicklas |
109 |
return run(result, rscript_path, "-e", rCmd.toString()); |
2863 |
28 Oct 14 |
nicklas |
110 |
} |
2863 |
28 Oct 14 |
nicklas |
111 |
|
2863 |
28 Oct 14 |
nicklas |
112 |
} |