Commit e8dff83a authored by Nico Mack's avatar Nico Mack

Finalized first implementation of RExecutor

parent a1ef1358
......@@ -18,9 +18,10 @@ abstract public class Executor {
private static Logger LOGGER = LoggerFactory.getLogger(Executor.class.getSimpleName());
protected static final String PARAMETER_TEMPLATE = "Parameter -> {} = {}";
protected static final String RESULT_TEMPLATE_PLAIN = "Result <- {} = {}";
protected static final String RESULT_TEMPLATE_EQUATE = "Result <- {}'{'{}'}' = {}";
protected static final String PARAMETER_TEMPLATE = "Parameter -> {} = {}"; //$NON-NLS-1$
protected static final String RESULT_TEMPLATE_PLAIN = "Result <- {} = {}"; //$NON-NLS-1$
protected static final String RESULT_TEMPLATE_EQUATE = "Result <- {}'{'{}'}' = {}"; //$NON-NLS-1$
protected static final String RESULT_TEMPLATE_INDEX = "Result <- {}'[{}]' = {}"; //$NON-NLS-1$
public Executor() {
executionErrors = new CharArrayWriter();
......
......@@ -60,15 +60,9 @@ public class PythonExecutor extends Executor {
}
private static final String SCRIPT_ENGINE_WORKING_DIR = "python.workingDir"; //$NON-NLS-1$
private static final String PARAMETER_TEMPLATE = "Parameter -> {} = {}"; //$NON-NLS-1$
private static final Pattern TUPLE_PATTERN = Pattern.compile("^([a-z0-9_]+)\\.([a-z0-9_]+)", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final Logger LOGGER = LoggerFactory.getLogger(PythonExecutor.class.getSimpleName());
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
......
......@@ -110,6 +110,7 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
// ---------------------------------------------------------------------------
@SuppressWarnings("unchecked")
private <T> T[] retrieve(String identifier) {
REXP result;
T[] array = null;
......@@ -119,6 +120,8 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
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;
} else if (ARRAY_MAPPING.containsKey(result.getType())) {
Object[] arguments = new Object[1];
......@@ -135,7 +138,6 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
}
return array;
}
......@@ -166,6 +168,38 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
// ---------------------------------------------------------------------------
@SuppressWarnings("unchecked")
public <T> Variable<T> retrieveScalarVariable(Declaration declaration) {
Variable<T> scalar = (Variable<T>) declaration.getVariable();
String identifier = (Strings.isNullOrEmpty(declaration.getEquate())) ? scalar.getName() : declaration.getEquate();
int index = declaration.getIndex();
T[] array = this.retrieve(identifier);
if ((array == null) || (array.length == 0)) {
scalar.setValue(null);
return scalar;
}
if (index >= 0) {
if (index < array.length) {
scalar.setValue(array[index]);
} else {
LOGGER.error("Index {} is out of range! {} >= {}!", index, index, array.length);
}
} else {
scalar.setValue(array[0]);
}
if (LOGGER.isInfoEnabled()) {
LOGGER.info(this.RESULT_TEMPLATE_PLAIN, scalar.getName(), scalar.getValue()); // $NON-NLS-1$
}
return scalar;
}
// ---------------------------------------------------------------------------
@SuppressWarnings("unchecked")
public <T> boolean assignVectorVariable(Declaration declaration) {
VectorVariable<T> vector = (VectorVariable<T>) declaration.getVariable();
......@@ -191,6 +225,46 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
return success;
}
// ---------------------------------------------------------------------------
@SuppressWarnings("unchecked")
private <T> VectorVariable<T> retrieveVectorVariable(Declaration declaration) {
VectorVariable<T> vector = (VectorVariable<T>) declaration.getVariable();
String identifier = (Strings.isNullOrEmpty(declaration.getEquate())) ? vector.getName() : declaration.getEquate();
int index = declaration.getIndex();
T[] array = retrieve(identifier);
if ((array == null) || (array.length == 0)) {
vector.clear();
return vector;
}
if (index >= 0) {
vector.set(index, array[0]);
if (LOGGER.isInfoEnabled()) {
LOGGER.info(this.RESULT_TEMPLATE_INDEX, vector.getName(), index, array[0]); // $NON-NLS-1$
}
} else {
vector.suspendListenerNotification(true);
vector.clear();
for (index = 0; index < array.length; index++) {
vector.add(array[index]);
}
vector.suspendListenerNotification(false);
vector.notifyInputChangeListeners();
if (LOGGER.isInfoEnabled()) {
LOGGER.info(this.RESULT_TEMPLATE_PLAIN, vector.getName(), array); // $NON-NLS-1$
}
}
return vector;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
......@@ -251,8 +325,12 @@ public class RExecutor extends Executor implements RMainLoopCallbacks {
@Override
public Variable<?> get(Declaration declaration) {
this.retrieve(declaration.getIdentifier());
return null;
Variable<?> variable = declaration.getVariable();
if (variable instanceof VectorVariable) {
return this.retrieveVectorVariable(declaration);
}
return this.retrieveScalarVariable(declaration);
}
// ---------------------------------------------------------------------------
......
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