use of org.molgenis.script.core.ScriptException in project molgenis by molgenis.
the class AlgorithmServiceImplTest method testApplyWithInvalidScript.
@Test(expectedExceptions = ScriptException.class, expectedExceptionsMessageRegExp = "algorithm is not defined")
public void testApplyWithInvalidScript() {
AttributeMapping attributeMapping = mock(AttributeMapping.class);
String algorithm = "algorithm";
when(attributeMapping.getAlgorithm()).thenReturn(algorithm);
Entity sourceEntity = mock(Entity.class);
when(jsMagmaScriptEvaluator.eval(algorithm, sourceEntity, 3)).thenReturn(new ScriptException("algorithm is not defined"));
algorithmServiceImpl.apply(attributeMapping, sourceEntity, null);
}
use of org.molgenis.script.core.ScriptException in project molgenis by molgenis.
the class JsMagmaScriptEvaluator method eval.
/**
* Evaluate a expression for the given entity.
*
* @param expression JavaScript expression
* @param entity entity
* @return evaluated expression result, return type depends on the expression.
*/
public Object eval(String expression, Entity entity, int depth) {
Stopwatch stopwatch = null;
if (LOG.isTraceEnabled()) {
stopwatch = Stopwatch.createStarted();
}
Object scriptEngineValueMap = toScriptEngineValueMap(entity, depth);
Object value;
try {
value = jsScriptEngine.invokeFunction("evalScript", expression, scriptEngineValueMap);
} catch (Throwable t) {
return new ScriptException(t);
}
if (stopwatch != null) {
stopwatch.stop();
LOG.trace("Script evaluation took {} µs", stopwatch.elapsed(MICROSECONDS));
}
return value;
}
use of org.molgenis.script.core.ScriptException in project molgenis by molgenis.
the class RScriptExecutor method executeScriptExecuteRequest.
/**
* Execute R script using OpenCPU
*
* @param rScript R script
* @return OpenCPU session key
* @throws IOException if error occured during script execution request
*/
private String executeScriptExecuteRequest(String rScript) throws IOException {
URI uri = getScriptExecutionUri();
HttpPost httpPost = new HttpPost(uri);
NameValuePair nameValuePair = new BasicNameValuePair("x", rScript);
httpPost.setEntity(new UrlEncodedFormEntity(singletonList(nameValuePair)));
String openCpuSessionKey;
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
Header openCpuSessionKeyHeader = response.getFirstHeader("X-ocpu-session");
if (openCpuSessionKeyHeader == null) {
throw new IOException("Missing 'X-ocpu-session' header");
}
openCpuSessionKey = openCpuSessionKeyHeader.getValue();
EntityUtils.consume(response.getEntity());
} else if (statusCode == 400) {
HttpEntity entity = response.getEntity();
String rErrorMessage = EntityUtils.toString(entity);
EntityUtils.consume(entity);
throw new ScriptException(rErrorMessage);
} else {
throw new ClientProtocolException(format("Unexpected response status: %d", statusCode));
}
}
return openCpuSessionKey;
}
use of org.molgenis.script.core.ScriptException in project molgenis by molgenis.
the class AlgorithmServiceImplTest method testApplyAlgorithmWitInvalidScript.
@Test
public void testApplyAlgorithmWitInvalidScript() {
Attribute attribute = mock(Attribute.class);
String algorithm = "algorithm";
Entity entity = mock(Entity.class);
when(jsMagmaScriptEvaluator.eval(algorithm, entity, 3)).thenReturn(new ScriptException("algorithm is not defined"));
Iterable<AlgorithmEvaluation> result = algorithmServiceImpl.applyAlgorithm(attribute, algorithm, Lists.newArrayList(entity));
AlgorithmEvaluation eval = result.iterator().next();
assertEquals(eval.getErrorMessage(), "algorithm is not defined");
}
Aggregations