public code v1

This commit is contained in:
2026-05-22 11:14:29 +02:00
parent 427197ec5a
commit b8141736eb
28859 changed files with 575079 additions and 0 deletions
@@ -0,0 +1,60 @@
package afryca.ase.engine.R;
import javax.script.ScriptException;
import org.rosuda.JRI.Rengine;
import afryca.ase.engine.IEngine;
/**
* R engine for ASE
*
* @author Sinbad²
* @version 3.0
*/
public class R implements IEngine {
public static final String ID = "R"; //$NON-NLS-1$
private Rengine rengine;
private String name;
private R() {
super();
name = ID;
rengine = null;
}
/**
* Build R engine
*
* @param rengine
* JRI Rengine
*/
public R(Rengine rengine) {
this();
assert rengine != null;
this.rengine = rengine;
}
@Override
public String getName() {
return name;
}
@Override
public Object getEngine() {
return rengine;
}
@Override
public Object eval(String script) throws ScriptException {
return rengine.eval(script);
}
@Override
public void end() {
rengine.end();
}
}
@@ -0,0 +1,16 @@
package afryca.ase.engine.R.l10n;
/**
* Localization messages
*
* @author Sinbad²
* @version 3.0
*/
public class Messages {
public String invalid_jri;
public String invalid_jri_version;
public String invalid_r_home;
public String r_home_not_setted;
public String r_manual_linux;
public String r_manual_windows;
}
@@ -0,0 +1,61 @@
invalid_jri=Invalid JRI
invalid_jri_version=Invalid JRI version
invalid_r_home=Invalid R_HOME
r_home_not_setted=R_HOME not setted
r_manual_linux=R configuration for AFRYCA\
\n\n\
1 - Open R\
\n\n\
2 - Set R_HOME environment var. Execute in R the following command:\
\n\
\tR.home()\
\n\
\tSet the return at system environment var R_HOME (without '"' character).\
\n\n\
3 - Optional - Install rJava (if it is not installed yet)\
\n\
\tinstall.packages("rJava")\
\n\n\
4 - Set JRI value in afryca.ini. Execute in R the following command:\
\n\
\tsystem.file("jri",package="rJava")\
\n\
\tModify the REPLACE value in afryca.ini with the previous value\
\n\
\t(without '"' character) adding a final '/' character.
r_manual_windows=Configuring R for AFRYCA\
\n\n\
1 - Open Rgui.exe with administrator permissions\
\n\n\
2 - set the environment variable R_HOME. Execute the command in Rgui:\
\n\
\tR.home()\
\n\
\tEstablish the value for the variable R_HOME (without the signs '"').\
\n\n\
3 - Add the path R_HOME\\bin[\\x64] to the PATH environment variable.\
\n\n\
4 - Optional - Install rJava (if it is not already installed)\
\n\
\tinstall.packages ("rJava")\
\n\n\
5 - Optional - Install Rsolnp (if it is not already installed)\
\n\
\tinstall.packages ("Rsolnp")\
\n\n\
6 - Execute the command in Rgui:\
\n\tsystem.file ("jri", package = "rJava")\
\n\
\tAdd to the PATH variable the previous value\\[x64]\
\n\n\
7 - set JRI in file afryca.ini. Execute the command in Rgui:\
\n\
\tsystem.file ("jri", package = "rJava")\
\n\
\tModify the REPLACE value of afryca.ini with this value.\
\n\
\tIf you are using 64-bit binaries add /x64 to the previous value\
\n\
\t (is usually the name of this directory ...).\
\n\
\tC: / Users /.../rJava/jri -> C: / Users /.../ jri / x64
@@ -0,0 +1,52 @@
invalid_jri=JRI inválido
invalid_jri_version=Versión de JRI inválida
invalid_r_home=Variable de entorno ''R_HOME'' inválida
r_home_not_setted=Variable de entorno ''R_HOME'' no establecida
r_manual_linux=Configuración de R para AFRYCA\
\n\n\
1 - Abrir R\
\n\n\
2 - Establecer la variable de entorno R_HOME. Ejecutar en R el comando:\
\n\
\tR.home()\
\n\
\tEstablecer el valor para la variable R_HOME (sin los signos '"').\
\n\n\
3 - Opcional - Instalar rJava (si aún no está instalado)\
\n\
\tinstall.packages("rJava")\
\n\n\
4 - Establecer JRI en afryca.ini. Ejecutar en R el comando:\
\n\
\tsystem.file("jri",package="rJava")\
\n\
\tModificar el valor REPLACE de afryca.ini con este valor\
\n\
\t(sin los signos '"') añadiendo al final el signo '/'.
r_manual_windows=Configuración de R para AFRYCA\
\n\n\
1 - Abrir Rgui.exe\
\n\n\
2 - Establecer la variable de entorno R_HOME. Ejecutar en Rgui el comando:\
\n\
\tR.home()\
\n\
\tEstablecer el valor para la variable R_HOME (sin los signos '"').\
\n\n\
3 - Añadir Rcmd.exe al PATH (<R_HOME>\\bin[\\x64]).\
\n\n\
4 - Opcional - Instalar rJava (si aún no está instalado)\
\n\
\tinstall.packages("rJava")\
\n\n\
5 - Establecer JRI en afryca.ini. Ejecutar en Rgui el comando:\
\n\
\tsystem.file("jri",package="rJava")\
\n\
\tModificar el valor REPLACE de afryca.ini con este valor.\
\n\
\tSi está usando los binarios de 64 bits añada /x64 al valor anterior\
\n\
\t(es habitualmente el nombre de este directorio...).\
\n\
\tC:/Users /.../rJava/jri -> C:/Users/.../jri/x64
@@ -0,0 +1,25 @@
package afryca.ase.engine.R.provider;
import afryca.ase.engine.IEngine;
/**
* R engine provider interface
*
* @author Sinbad²
* @version 3.0
*/
public interface IREngineProvider {
public static final String R = "R"; //$NON-NLS-1$
public static final String R_STATUS = "R_STATUS"; //$NON-NLS-1$
public static final String R_MANUAL = "R_MANUAL"; //$NON-NLS-1$
public static final String R_VERSION = "R_VERSION";
public static final String R_PATHS = "R_PATHS"; //$NON-NLS-1$
/**
* Return R engine instance
*
* @return R engine instance
*/
IEngine getEngine();
}
@@ -0,0 +1,187 @@
package afryca.ase.engine.R.provider.internal;
import java.io.File;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.nls.Translation;
import org.rosuda.JRI.Rengine;
import afryca.ase.engine.IEngine;
import afryca.ase.engine.R.R;
import afryca.ase.engine.R.l10n.Messages;
import afryca.ase.engine.R.provider.IREngineProvider;
/**
* R engine default provider
*
* @author Sinbad²
* @version 3.0
*/
public class REngineDefaultProvider implements IREngineProvider {
private static final String VERSION = "--vanilla"; //$NON-NLS-1$
private static final String JRI_IGNORE_FLAG = "jri.ignore.ule"; //$NON-NLS-1$
private static final String JRI_IGNORE_FLAG_VALUE = "yes"; //$NON-NLS-1$
private static final String R_HOME = "R_HOME"; //$NON-NLS-1$
private static final String BINARIES_DIRECTORY = "bin"; //$NON-NLS-1$
private static final String LIBRARIES_DIRECTORY = "library"; //$NON-NLS-1$
private static final String R_VERSION_CMD = "R.version.string"; //$NON-NLS-1$
private static final String R_JRI_CMD = "system.file(\"jri\",package=\"rJava\")"; //$NON-NLS-1$
@Inject
@Translation
private Messages messages;
@Inject
private IEclipseContext context;
private IEclipseContext topContext;
private R engine;
private Job job;
@PostConstruct
private void initialize() {
getTopContext();
saveRConfigurationInstructions();
saveRStatus();
createEngineJob(R);
}
private void getTopContext() {
IEclipseContext parent = context;
do {
topContext = parent;
parent = topContext.getParent();
} while (parent != null);
}
private void saveRConfigurationInstructions() {
topContext.set(R_MANUAL, getOSManual());
}
private String getOSManual() {
return Platform.OS_LINUX.equals(Platform.getOS())
? messages.r_manual_linux
: messages.r_manual_windows;
}
@Override
public IEngine getEngine() {
try {
job.join();
} catch (InterruptedException e) {
// Nothing to do
}
return (afryca.ase.engine.IEngine) topContext.get(R);
}
private void saveRStatus() {
String status = checkDependenciesStatus();
topContext.set(R_STATUS, status);
}
private String checkDependenciesStatus() {
System.setProperty(JRI_IGNORE_FLAG, JRI_IGNORE_FLAG_VALUE);
try {
if (!Rengine.jriLoaded) {
return messages.invalid_jri;
}
if (!Rengine.versionCheck()) {
return messages.invalid_jri_version;
}
if (System.getenv(R_HOME) == null) {
return messages.r_home_not_setted;
}
if (!validateR_HOME()) {
return messages.invalid_r_home;
}
} catch (UnsatisfiedLinkError error) {
return messages.invalid_jri;
}
return null;
}
private boolean validateR_HOME() {
String home = System.getenv(R_HOME);
File dir = new File(home);
if (!dir.exists()) {
return false;
}
if (!dir.canRead()) {
return false;
}
if (!dir.isDirectory()) {
return false;
}
List<String> files = Arrays.asList(dir.list());
if (!files.contains(BINARIES_DIRECTORY) || !files.contains(LIBRARIES_DIRECTORY)) {
return false;
}
return true;
}
private void createEngineJob(String scriptName) {
job = new Job(scriptName) {
@Override
protected IStatus run(IProgressMonitor monitor) {
createEngine();
return (topContext.get(scriptName) != null)
? Status.OK_STATUS
: Status.CANCEL_STATUS;
}
};
job.schedule();
}
private void createEngine() {
Rengine rengine = getRengine();
topContext.set(R, (rengine != null)
? new R(rengine)
: null);
}
private Rengine getRengine() {
Rengine result = null;
if (topContext.get(R_STATUS) == null) {
result = new Rengine(new String[] { VERSION }, false, null);
if (!result.waitForR()) {
result = null;
} else {
String rVersion = result.eval(R_VERSION_CMD).asString();
String rJRI = result.eval(R_JRI_CMD).asString();
String rHome = System.getenv(R_HOME);
topContext.set(R_VERSION, rVersion);
topContext.set(
R_PATHS,
MessageFormat.format(
"{0}\n\nJRI:\n{1}\n\nR_HOME:\n{2}", //$NON-NLS-1$
rVersion,
rJRI,
rHome));
}
}
return result;
}
public void end() {
if (engine != null) {
engine.end();
}
}
}
@@ -0,0 +1,27 @@
package afryca.ase.engine.R.provider.internal;
import org.eclipse.e4.core.contexts.ContextFunction;
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.model.application.MApplication;
import afryca.ase.engine.R.provider.IREngineProvider;
/**
* R engine provider context function
*
* @author Sinbad²
* @version 3.0
*/
public class REngineProviderContextFunction extends ContextFunction {
@Override
public Object compute(IEclipseContext context, String contextKey) {
IREngineProvider rEngineProvider = ContextInjectionFactory.make(REngineDefaultProvider.class,
context);
MApplication application = context.get(MApplication.class);
IEclipseContext applicationContext = application.getContext();
applicationContext.set(IREngineProvider.class, rEngineProvider);
return rEngineProvider;
}
}