Commit be5a2e9a authored by Nico Mack's avatar Nico Mack

Added MapVariables to VariableBootstrapper

Bug fixes in RExecutor
parent 10d42ee0
......@@ -15,6 +15,7 @@ package lu.list.itis.dkd.tui.bootstrapping;
import lu.list.itis.dkd.tui.cps.utility.EquationSystemBundle;
import lu.list.itis.dkd.tui.cps.variable.BooleanVariable;
import lu.list.itis.dkd.tui.cps.variable.MapVariable;
import lu.list.itis.dkd.tui.cps.variable.NumericalVariable;
import lu.list.itis.dkd.tui.cps.variable.TextVariable;
import lu.list.itis.dkd.tui.cps.variable.Variable;
......@@ -59,6 +60,11 @@ public class VariableBootstrapper {
variable = numerical;
break;
case EquationSystemBundle.MAP_TYPE:
MapVariable<?, ?> mapVariable = new MapVariable<>(name);
variable = mapVariable;
break;
case EquationSystemBundle.VECTOR_TYPE:
unit = BootstrappingUtils.getContentAsString(variableNode, EquationSystemBundle.UNIT_ATTRIBUTE, BootstrappingUtils.OPTIONAL, EquationSystemBundle.EMPTY_STRING, context);
VectorVariable<?> vectorVariable = new VectorVariable<>(name, unit);
......
......@@ -11,17 +11,21 @@ import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.RMainLoopCallbacks;
import org.rosuda.JRI.RVector;
import org.rosuda.JRI.Rengine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
// ***************************************************************************
// * Class Definition *
......@@ -42,7 +46,9 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
static {
SCALAR_MAPPING.put(REXP.XT_BOOL, "asBool"); //$NON-NLS-1$
SCALAR_MAPPING.put(REXP.XT_DOUBLE, "asDouble"); //$NON-NLS-1$
SCALAR_MAPPING.put(REXP.REALSXP, "asDouble"); //$NON-NLS-1$
SCALAR_MAPPING.put(REXP.XT_INT, "asInt"); //$NON-NLS-1$
SCALAR_MAPPING.put(REXP.INTSXP, "asInt"); //$NON-NLS-1$
SCALAR_MAPPING.put(REXP.XT_STR, "asString"); //$NON-NLS-1$
}
......@@ -55,11 +61,14 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
private static final String[] R_ARGS = new String[] {"--vanilla"}; //$NON-NLS-1$
private static final String CLEAR_WORKSPACE_CMD = "rm(list = ls())"; //$NON-NLS-1$
private static final String LIB_PATH = "r.lib.path"; //$NON-NLS-1$
private static final String JRI_PATH = "r.jri.path"; //$NON-NLS-1$
private static final String R_WORKING_DIR = "r.workingDir"; //$NON-NLS-1$
private static final Logger LOGGER = LoggerFactory.getLogger(RExecutor.class.getSimpleName());
private static final Pattern TRAILING_NEWLINE = Pattern.compile("(.*?)\n$", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
......@@ -74,6 +83,13 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
LOGGER.debug("JRI Path => {}", properties.getProperty(JRI_PATH)); //$NON-NLS-1$
}
StringBuilder libraryDir = new StringBuilder(".libPaths(\"") //$NON-NLS-1$
.append(properties.getProperty(LIB_PATH))
.append("\")"); //$NON-NLS-1$
if (LOGGER.isInfoEnabled()) {
LOGGER.info("R Library Path => {}", libraryDir); //$NON-NLS-1$
}
Path currentRelativePath = Paths.get(""); //$NON-NLS-1$
StringBuilder workingDir = new StringBuilder("setwd(\"") //$NON-NLS-1$
.append(currentRelativePath.toAbsolutePath())
......@@ -89,6 +105,7 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
LOGGER.error("Failed to load R Engine!"); //$NON-NLS-1$
}
engine.eval(workingDir.toString());
engine.eval(libraryDir.toString());
}
// ---------------------------------------------------------------------------
......@@ -127,6 +144,32 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
return success;
}
// ---------------------------------------------------------------------------
@SuppressWarnings("unchecked")
private <T> T[] retrieveScalar(REXP rObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
T[] array = null;
T value = (T) MethodUtils.invokeMethod(rObject, SCALAR_MAPPING.get(rObject.getType()));
array = (T[]) Array.newInstance(value.getClass(), 1);
array[0] = value;
return array;
}
// ---------------------------------------------------------------------------
@SuppressWarnings("unchecked")
private <T> T[] retrieveArray(REXP rObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
T[] array = null;
Object[] arguments = new Object[1];
Class<?>[] argumentTypes = new Class<?>[1];
arguments[0] = MethodUtils.invokeMethod(rObject, ARRAY_MAPPING.get(rObject.getType()));
argumentTypes[0] = arguments[0].getClass();
array = (T[]) MethodUtils.invokeStaticMethod(ArrayUtils.class, TO_OBJECT_METHOD, arguments, argumentTypes);
return array;
}
// ---------------------------------------------------------------------------
/**
* Retrieves the value of the R symbol specified by identifier.
......@@ -145,19 +188,29 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
try {
result = engine.eval(identifier);
if (result != null) {
if (SCALAR_MAPPING.containsKey(result.getType())) {
T value = (T) MethodUtils.invokeMethod(result, SCALAR_MAPPING.get(result.getType()));
array = (T[]) Array.newInstance(value.getClass(), 1);
array[0] = value;
if (REXP.XT_VECTOR == result.getType()) {
RVector vector = result.asVector();
List<T> extracted = new ArrayList<>();
for (int index = 0; index < vector.size(); index++) {
REXP element = vector.at(index);
if (SCALAR_MAPPING.containsKey(element.getType())) {
T[] values = this.retrieveScalar(result);
extracted.add(values[0]);
} else if (ARRAY_MAPPING.containsKey(element.getType())) {
T[] values = this.retrieveArray(element);
extracted.add(values[0]);
}
}
array = (T[]) extracted.toArray();
} else if (SCALAR_MAPPING.containsKey(result.getType())) {
array = this.retrieveScalar(result);
} else if (ARRAY_MAPPING.containsKey(result.getType())) {
Object[] arguments = new Object[1];
Class<?>[] argumentTypes = new Class<?>[1];
array = this.retrieveArray(result);
}
arguments[0] = MethodUtils.invokeMethod(result, ARRAY_MAPPING.get(result.getType()));
argumentTypes[0] = arguments[0].getClass();
array = (T[]) MethodUtils.invokeStaticMethod(ArrayUtils.class, TO_OBJECT_METHOD, arguments, argumentTypes);
}
} else {
LOGGER.warn("No value returned for variable {}!", identifier); //$NON-NLS-1$
}
} catch (Exception exception) {
LOGGER.error("Failed to retrieve variable {}!", identifier, exception); //$NON-NLS-1$
......@@ -372,7 +425,7 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
/** {@inheritDoc} */
@Override
public void resetContext() {
engine.eval(CLEAR_WORKSPACE_CMD);
// engine.eval(CLEAR_WORKSPACE_CMD);
}
// ---------------------------------------------------------------------------
......@@ -434,8 +487,7 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
@Override
public void rWriteConsole(Rengine engine, String arg1, int arg2) {
// TODO Auto-generated method stub
public void rWriteConsole(Rengine engine, String message, int type) {
LOGGER.info(message);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment