Commit 7cb01462 authored by R.W.Majeed's avatar R.W.Majeed
Browse files

rudimentary ECMAScript/javascript support for evaluations using Java 8

Nashorn
parent 167bc1a4
package de.sekmi.histream.eval;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import de.sekmi.histream.Observation;
/**
* Evaluate ECMAScript/Javascript expressions with {@link Observation}.
*
* @author Raphael
*
*/
public class ECMAEvaluator implements Engine{
private ScriptEngine engine;
public ECMAEvaluator(){
ScriptEngineManager m = new ScriptEngineManager();
this.engine = m.getEngineByName("nashorn");
// TODO: add context to control reader/writer
//this.engine.setContext(this);
}
@Override
public boolean test(String expression, Observation fact)
throws ScriptException {
Bindings bindings = engine.createBindings();
bindings.put("fact", fact);
Object ret;
try {
ret = engine.eval(expression, bindings);
} catch (javax.script.ScriptException e) {
throw new ScriptException(e);
}
if( ret == null || !(ret instanceof Boolean) ){
throw new ScriptException("Expression did not return Boolean: "+expression);
}
return ((Boolean)ret).booleanValue();
}
}
......@@ -10,4 +10,8 @@ public class ScriptException extends Exception{
public ScriptException(Throwable cause){
super(cause);
}
public ScriptException(String message){
super(message);
}
}
package de.sekmi.histream.impl;
import javax.xml.bind.JAXBException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import de.sekmi.histream.Observation;
import de.sekmi.histream.ObservationSupplier;
import de.sekmi.histream.eval.ECMAEvaluator;
import de.sekmi.histream.io.FileObservationProviderTest;
public class ECMAEvaluatorTest {
private ECMAEvaluator eval;
@Before
public void initialize() throws JAXBException{
eval = new ECMAEvaluator();
}
@Test
public void testExpressions() throws Exception{
FileObservationProviderTest t = new FileObservationProviderTest();
t.initializeObservationFactory();
ObservationSupplier s = t.getExampleSupplier();
Observation o = s.get();
//System.out.println("XXX:"+eval.evaluateToString("translate(fact/@start,'-:T','')", o));
//System.out.println(XPathEvaluator.toXMLString(o));
// TODO compare startTime
String[] trueExpressions = new String[]{"fact.patientId != null", "fact.conceptId != null", "fact.startTime != null"};
for( String expr : trueExpressions ){
Assert.assertEquals(expr, true, eval.test(expr, o));
}
// skip to string value
for( int i=0; i<6; i++ ){
o = s.get();
}
// compare string value
Assert.assertEquals(true, eval.test("fact.value != null", o));
// compare numeric value
o = s.get();
Assert.assertEquals(true, eval.test("fact.value.numericValue > 122 && fact.value.numericValue < 124", o));
// compare value with unit
// XXX flag not supported yet
o = s.get();
o = s.get();
Assert.assertEquals(true, eval.test("fact.value.units == 'mm' && fact.value.numericValue > 123", o));
// compare modifier values
o = s.get();
//Assert.assertEquals(true, eval.test("fact/modifier[@code='T:mod:3']/value = 78.9 and fact/modifier[@code='T:mod:1']", o));
s.close();
}
}
Supports Markdown
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